From a0a956ee0878d24bd06be7f5d75dc4ccd4e7901d Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sun, 22 Dec 2019 04:10:53 +0100 Subject: Properly check Jids everywhere --- xmpp-vala/tests/common.vala | 8 ++++ xmpp-vala/tests/jid.vala | 93 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 xmpp-vala/tests/jid.vala (limited to 'xmpp-vala/tests') diff --git a/xmpp-vala/tests/common.vala b/xmpp-vala/tests/common.vala index b91bbf7c..c616d2e7 100644 --- a/xmpp-vala/tests/common.vala +++ b/xmpp-vala/tests/common.vala @@ -5,6 +5,7 @@ int main(string[] args) { GLib.Test.set_nonfatal_assertions(); TestSuite.get_root().add_suite(new Xmpp.Test.StanzaTest().get_suite()); TestSuite.get_root().add_suite(new Xmpp.Test.UtilTest().get_suite()); + TestSuite.get_root().add_suite(new Xmpp.Test.JidTest().get_suite()); return GLib.Test.run(); } @@ -74,6 +75,13 @@ bool fail_if_not_eq_str(string? left, string? right, string? reason = null) { return fail_if_not(!nullcheck && left == right, @"$(reason + ": " ?? "")$left != $right"); } +bool fail_if_eq_str(string? left, string? right, string? reason = null) { + bool nullcheck = (left == null && right != null) || (left != null && right == null); + if (left == null) left = "(null)"; + if (right == null) right = "(null)"; + return fail_if(!nullcheck && left == right, @"$(reason + ": " ?? "")$left == $right"); +} + bool fail_if_not_eq_uint8_arr(uint8[] left, uint8[] right, string? reason = null) { if (fail_if_not_eq_int(left.length, right.length, @"$(reason + ": " ?? "")array length not equal")) return true; return fail_if_not_eq_str(Base64.encode(left), Base64.encode(right), reason); diff --git a/xmpp-vala/tests/jid.vala b/xmpp-vala/tests/jid.vala new file mode 100644 index 00000000..8928dc97 --- /dev/null +++ b/xmpp-vala/tests/jid.vala @@ -0,0 +1,93 @@ +namespace Xmpp.Test { + +class JidTest : Gee.TestCase { + public JidTest() { + base("Jid"); + + add_test("jid_valid_domain_only", () => { test_jid_valid("example.com"); }); + add_test("jid_valid_bare", () => { test_jid_valid("test@example.com"); }); + add_test("jid_valid_domain_with_resource", () => { test_jid_valid("example.com/test"); }); + add_test("jid_valid_full", () => { test_jid_valid("test@example.com/test"); }); + + // Should those actually be valid? + add_test("jid_valid_emoji_local", () => { test_jid_valid("๐Ÿ˜…@example.com"); }); + add_test("jid_valid_emoji_resource", () => { test_jid_valid("test@example.com/๐Ÿ˜…"); }); + + add_test("jid_invalid_emoji_domain", () => { test_jid_invalid("test@๐Ÿ˜….com"); }); + add_test("jid_invalid_bidi_local", () => { test_jid_invalid("teโ€st@example.com"); }); + add_test("jid_invalid_bidi_resource", () => { test_jid_invalid("test@example.com/teโ€st"); }); + add_test("jid_invalid_bidi_domain", () => { test_jid_invalid("test@exaโ€mple.com"); }); + add_test("jid_invalid_overlong_idn", () => { test_jid_invalid("test@รงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรง.com"); }); + + add_test("jid_equal_end_domain", () => { test_jids_equal("test@example.com", "test@example.com."); }); + add_test("jid_equal_case_domain", () => { test_jids_equal("test@example.com", "test@eXample.com"); }); + add_test("jid_equal_norm_domain", () => { test_jids_equal("test@garรงon.com", "test@garcฬงon.com"); }); + add_test("jid_equal_puny_domain", () => { test_jids_equal("test@garรงon.com", "test@xn--garon-0ra.com"); }); + add_test("jid_equal_case_local", () => { test_jids_equal("test@example.com", "tEst@example.com"); }); + add_test("jid_equal_norm_local", () => { test_jids_equal("garรงon@example.com", "garcฬงon@example.com"); }); + add_test("jid_equal_norm_resource", () => { test_jids_equal("test@example.com/garรงon", "test@example.com/garcฬงon"); }); + + add_test("jid_non_equal_case_resource", () => { test_jids_unequal("example.com/test", "example.com/tEst"); }); + + add_test("jid_to_string_end_domain", () => { test_jid_to_string("test@example.com.", "test@example.com"); }); + add_test("jid_to_string_case_domain", () => { test_jid_to_string("test@eXample.com", "test@example.com"); }); + add_test("jid_to_string_norm_domain", () => { test_jid_to_string("test@garcฬงon.com", "test@garรงon.com"); }); + add_test("jid_to_string_puny_domain", () => { test_jid_to_string("test@xn--garon-0ra.com", "test@garรงon.com"); }); + add_test("jid_to_string_case_local", () => { test_jid_to_string("tEst@example.com", "test@example.com"); }); + add_test("jid_to_string_norm_local", () => { test_jid_to_string("garcฬงon@example.com", "garรงon@example.com"); }); + add_test("jid_to_string_case_resource", () => { test_jid_to_string("example.com/tEst", "example.com/tEst"); }); + add_test("jid_to_string_norm_resource", () => { test_jid_to_string("test@example.com/garcฬงon", "test@example.com/garรงon"); }); + } + + private void test_jid_valid(string jid) { + try { + new Jid(jid); + } catch (Error e) { + fail_if_reached(); + } + } + + private void test_jid_invalid(string jid) { + try { + new Jid(jid); + fail_if_reached(); + } catch (Error e) { +// try { +// fail_if_not_eq_str(Jid.parse(jid).to_string(), jid); +// } catch (Error e) { +// fail_if_reached(); +// } + } + } + + private void test_jids_equal(string jid1, string jid2) { + try { + var t1 = new Jid(jid1); + var t2 = new Jid(jid2); + fail_if_not_eq_str(t1.to_string(), t2.to_string()); + } catch (Error e) { + fail_if_reached(); + } + } + + private void test_jid_to_string(string jid1, string jid2) { + try { + var t1 = new Jid(jid1); + fail_if_not_eq_str(t1.to_string(), jid2); + } catch (Error e) { + fail_if_reached(); + } + } + + private void test_jids_unequal(string jid1, string jid2) { + try { + var t1 = new Jid(jid1); + var t2 = new Jid(jid2); + fail_if_eq_str(t1.to_string(), t2.to_string()); + } catch (Error e) { + fail_if_reached(); + } + } +} + +} \ No newline at end of file -- cgit v1.2.3-70-g09d2