Remove useless comparison
[pidgin-git.git] / libpurple / tests / test_jabber_jutil.c
blob713115cab8900b5f4b01cc241c32f8530cc06d37
1 #include <string.h>
3 #include "tests.h"
4 #include "../account.h"
5 #include "../conversation.h"
6 #include "../xmlnode.h"
7 #include "../protocols/jabber/jutil.h"
9 START_TEST(test_get_resource)
11 assert_string_equal_free("baz", jabber_get_resource("foo@bar/baz"));
12 assert_string_equal_free("baz", jabber_get_resource("bar/baz"));
13 assert_string_equal_free("baz/bat", jabber_get_resource("foo@bar/baz/bat"));
14 assert_string_equal_free("baz/bat", jabber_get_resource("bar/baz/bat"));
16 END_TEST
18 START_TEST(test_get_resource_no_resource)
21 fail_unless(NULL == jabber_get_resource("foo@bar"));
22 fail_unless(NULL == jabber_get_resource("bar"));
24 END_TEST
26 START_TEST(test_get_bare_jid)
28 assert_string_equal_free("foo@bar", jabber_get_bare_jid("foo@bar"));
29 assert_string_equal_free("foo@bar", jabber_get_bare_jid("foo@bar/baz"));
30 assert_string_equal_free("bar", jabber_get_bare_jid("bar"));
31 assert_string_equal_free("bar", jabber_get_bare_jid("bar/baz"));
33 END_TEST
35 START_TEST(test_nodeprep_validate)
37 char *longnode;
39 fail_unless(jabber_nodeprep_validate(NULL));
40 fail_unless(jabber_nodeprep_validate("foo"));
41 fail_unless(jabber_nodeprep_validate("%d"));
42 fail_unless(jabber_nodeprep_validate("y\\z"));
43 fail_unless(jabber_nodeprep_validate("a="));
44 fail_unless(jabber_nodeprep_validate("a,"));
46 longnode = g_strnfill(1023, 'a');
47 fail_unless(jabber_nodeprep_validate(longnode));
48 g_free(longnode);
50 longnode = g_strnfill(1024, 'a');
51 fail_if(jabber_nodeprep_validate(longnode));
52 g_free(longnode);
54 END_TEST
56 START_TEST(test_nodeprep_validate_illegal_chars)
58 fail_if(jabber_nodeprep_validate("don't"));
59 fail_if(jabber_nodeprep_validate("m@ke"));
60 fail_if(jabber_nodeprep_validate("\"me\""));
61 fail_if(jabber_nodeprep_validate("&ngry"));
62 fail_if(jabber_nodeprep_validate("c:"));
63 fail_if(jabber_nodeprep_validate("a/b"));
64 fail_if(jabber_nodeprep_validate("4>2"));
65 fail_if(jabber_nodeprep_validate("4<7"));
67 END_TEST
69 START_TEST(test_nodeprep_validate_too_long)
71 char *longnode = g_strnfill(1024, 'a');
72 fail_if(jabber_nodeprep_validate(longnode));
73 g_free(longnode);
75 END_TEST
77 #define assert_valid_jid(str) { \
78 JabberID *jid = jabber_id_new(str); \
79 fail_if(jid == NULL, "JID '%s' is valid but jabber_id_new() rejected it", str); \
80 jabber_id_free(jid); \
83 #define assert_invalid_jid(str) { \
84 JabberID *jid = jabber_id_new(str); \
85 fail_if(jid != NULL, "JID '%s' is invalid but jabber_id_new() allowed it", str); \
86 jabber_id_free(jid); \
89 #define assert_jid_parts(expect_node, expect_domain, str) { \
90 JabberID *jid = jabber_id_new(str); \
91 fail_if(jid == NULL, "JID '%s' is valid but jabber_id_new() rejected it", str); \
92 fail_if(jid->node == NULL, "JID '%s' is valid but jabber_id_new() didn't return a node", str); \
93 fail_if(jid->domain == NULL, "JID '%s' is valid but jabber_id_new() didn't return a domain", str); \
94 fail_if(jid->resource != NULL, "JID '%s' doesn't contain a resource", str); \
95 assert_string_equal(expect_node, jid->node); \
96 assert_string_equal(expect_domain, jid->domain); \
97 jabber_id_free(jid); \
100 START_TEST(test_jabber_id_new)
102 assert_valid_jid("gmail.com");
103 assert_valid_jid("gmail.com/Test");
104 assert_valid_jid("gmail.com/Test@");
105 assert_valid_jid("gmail.com/@");
106 assert_valid_jid("gmail.com/Test@alkjaweflkj");
107 assert_valid_jid("mark.doliner@gmail.com");
108 assert_valid_jid("mark.doliner@gmail.com/Test12345");
109 assert_valid_jid("mark.doliner@gmail.com/Test@12345");
110 assert_valid_jid("mark.doliner@gmail.com/Te/st@12@//345");
111 assert_valid_jid("わいど@conference.jabber.org");
112 assert_valid_jid("まりるーむ@conference.jabber.org");
113 assert_valid_jid("mark.doliner@gmail.com/まりるーむ");
114 assert_valid_jid("mark.doliner@gmail/stuff.org");
115 assert_valid_jid("stuart@nödåtXäYZ.se");
116 assert_valid_jid("stuart@nödåtXäYZ.se/まりるーむ");
117 assert_valid_jid("mark.doliner@わいど.org");
118 assert_valid_jid("nick@まつ.おおかみ.net");
119 assert_valid_jid("paul@10.0.42.230/s");
120 assert_valid_jid("paul@[::1]"); /* IPv6 */
121 assert_valid_jid("paul@[2001:470:1f05:d58::2]");
122 assert_valid_jid("paul@[2001:470:1f05:d58::2]/foo");
123 assert_valid_jid("pa=ul@10.0.42.230");
124 assert_valid_jid("pa,ul@10.0.42.230");
126 assert_invalid_jid("@gmail.com");
127 assert_invalid_jid("@@gmail.com");
128 assert_invalid_jid("mark.doliner@@gmail.com/Test12345");
129 assert_invalid_jid("mark@doliner@gmail.com/Test12345");
130 assert_invalid_jid("@gmail.com/Test@12345");
131 assert_invalid_jid("/Test@12345");
132 assert_invalid_jid("mark.doliner@");
133 assert_invalid_jid("mark.doliner/");
134 assert_invalid_jid("mark.doliner@gmail_stuff.org");
135 assert_invalid_jid("mark.doliner@gmail[stuff.org");
136 assert_invalid_jid("mark.doliner@gmail\\stuff.org");
137 assert_invalid_jid("paul@[::1]124");
138 assert_invalid_jid("paul@2[::1]124/as");
139 assert_invalid_jid("paul@まつ.おおかみ/\x01");
142 * RFC 3454 Section 6 reads, in part,
143 * "If a string contains any RandALCat character, the
144 * string MUST NOT contain any LCat character."
145 * The character is U+066D (ARABIC FIVE POINTED STAR).
147 assert_invalid_jid("foo@example.com/٭simplexe٭");
149 /* Ensure that jabber_id_new is properly lowercasing node and domains */
150 assert_jid_parts("paul", "darkrain42.org", "PaUL@darkrain42.org");
151 assert_jid_parts("paul", "darkrain42.org", "paul@DaRkRaIn42.org");
153 /* These case-mapping tests culled from examining RFC3454 B.2 */
155 /* Cyrillic capital EF (U+0424) maps to lowercase EF (U+0444) */
156 assert_jid_parts("ф", "darkrain42.org", "Ф@darkrain42.org");
158 #ifdef USE_IDN
160 * These character (U+A664 and U+A665) are not mapped to anything in
161 * RFC3454 B.2. This first test *fails* when not using IDN because glib's
162 * case-folding/utf8_strdown improperly (for XMPP) lowercases the character.
164 * This is known, but not (very?) likely to actually cause a problem, so
165 * this test is commented out when using glib's functions.
167 assert_jid_parts("Ꙥ", "darkrain42.org", "Ꙥ@darkrain42.org");
168 assert_jid_parts("ꙥ", "darkrain42.org", "ꙥ@darkrain42.org");
169 #endif
171 /* U+04E9 to U+04E9 */
172 assert_jid_parts("paul", "өarkrain42.org", "paul@Өarkrain42.org");
174 END_TEST
176 START_TEST(test_jabber_normalize)
178 assert_string_equal("paul@darkrain42.org", jabber_normalize(NULL, "PaUL@DaRkRain42.org"));
179 assert_string_equal("paul@darkrain42.org", jabber_normalize(NULL, "PaUL@DaRkRain42.org/"));
180 assert_string_equal("paul@darkrain42.org", jabber_normalize(NULL, "PaUL@DaRkRain42.org/resource"));
182 END_TEST
184 Suite *
185 jabber_jutil_suite(void)
187 Suite *s = suite_create("Jabber Utility Functions");
189 TCase *tc = tcase_create("Get Resource");
190 tcase_add_test(tc, test_get_resource);
191 tcase_add_test(tc, test_get_resource_no_resource);
192 suite_add_tcase(s, tc);
194 tc = tcase_create("Get Bare JID");
195 tcase_add_test(tc, test_get_bare_jid);
196 suite_add_tcase(s, tc);
198 tc = tcase_create("JID validate");
199 tcase_add_test(tc, test_nodeprep_validate);
200 tcase_add_test(tc, test_nodeprep_validate_illegal_chars);
201 tcase_add_test(tc, test_nodeprep_validate_too_long);
202 tcase_add_test(tc, test_jabber_id_new);
203 tcase_add_test(tc, test_jabber_normalize);
204 suite_add_tcase(s, tc);
206 return s;