Remove useless comparison
[pidgin-git.git] / libpurple / tests / test_util.c
blob303a056117ee5fb7aee448993ff65b8091673b01
1 #include <string.h>
3 #include "tests.h"
4 #include "../util.h"
6 START_TEST(test_util_base16_encode)
8 assert_string_equal_free("68656c6c6f2c20776f726c642100", purple_base16_encode((const unsigned char *)"hello, world!", 14));
10 END_TEST
12 START_TEST(test_util_base16_decode)
14 gsize sz = 0;
15 guchar *out = purple_base16_decode("21646c726f77202c6f6c6c656800", &sz);
16 fail_unless(sz == 14, NULL);
17 assert_string_equal_free("!dlrow ,olleh", (char *)out);
19 END_TEST
21 START_TEST(test_util_base64_encode)
23 assert_string_equal_free("Zm9ydHktdHdvAA==", purple_base64_encode((const unsigned char *)"forty-two", 10));
25 END_TEST
27 START_TEST(test_util_base64_decode)
29 gsize sz;
30 guchar *out = purple_base64_decode("b3d0LXl0cm9mAA==", &sz);
31 fail_unless(sz == 10, NULL);
32 assert_string_equal_free("owt-ytrof", (char *)out);
34 END_TEST
36 START_TEST(test_util_escape_filename)
38 assert_string_equal("foo", purple_escape_filename("foo"));
39 assert_string_equal("@oo", purple_escape_filename("@oo"));
40 assert_string_equal("#oo", purple_escape_filename("#oo"));
41 assert_string_equal("-oo", purple_escape_filename("-oo"));
42 assert_string_equal("_oo", purple_escape_filename("_oo"));
43 assert_string_equal(".oo", purple_escape_filename(".oo"));
44 assert_string_equal("%25oo", purple_escape_filename("%oo"));
45 assert_string_equal("%21oo", purple_escape_filename("!oo"));
47 END_TEST
49 START_TEST(test_util_unescape_filename)
51 assert_string_equal("bar", purple_unescape_filename("bar"));
52 assert_string_equal("@ar", purple_unescape_filename("@ar"));
53 assert_string_equal("!ar", purple_unescape_filename("!ar"));
54 assert_string_equal("!ar", purple_unescape_filename("%21ar"));
55 assert_string_equal("%ar", purple_unescape_filename("%25ar"));
57 END_TEST
60 START_TEST(test_util_text_strip_mnemonic)
62 assert_string_equal_free("", purple_text_strip_mnemonic(""));
63 assert_string_equal_free("foo", purple_text_strip_mnemonic("foo"));
64 assert_string_equal_free("foo", purple_text_strip_mnemonic("_foo"));
67 END_TEST
70 * Many of the valid and invalid email addresses lised below are from
71 * http://fightingforalostcause.net/misc/2006/compare-email-regex.php
73 const char *valid_emails[] = {
74 "purple-devel@lists.sf.net",
75 "l3tt3rsAndNumb3rs@domain.com",
76 "has-dash@domain.com",
77 "hasApostrophe.o'leary@domain.org",
78 "uncommonTLD@domain.museum",
79 "uncommonTLD@domain.travel",
80 "uncommonTLD@domain.mobi",
81 "countryCodeTLD@domain.uk",
82 "countryCodeTLD@domain.rw",
83 "lettersInDomain@911.com",
84 "underscore_inLocal@domain.net",
85 "IPInsteadOfDomain@127.0.0.1",
86 /* "IPAndPort@127.0.0.1:25", */
87 "subdomain@sub.domain.com",
88 "local@dash-inDomain.com",
89 "dot.inLocal@foo.com",
90 "a@singleLetterLocal.org",
91 "singleLetterDomain@x.org",
92 "&*=?^+{}'~@validCharsInLocal.net",
93 "foor@bar.newTLD",
94 "HenryTheGreatWhiteCricket@live.ca",
95 "HenryThe__WhiteCricket@hotmail.com"
98 const char *invalid_emails[] = {
99 "purple-devel@@lists.sf.net",
100 "purple@devel@lists.sf.net",
101 "purple-devel@list..sf.net",
102 "purple-devel",
103 "purple-devel@",
104 "@lists.sf.net",
105 "totally bogus",
106 "missingDomain@.com",
107 "@missingLocal.org",
108 "missingatSign.net",
109 "missingDot@com",
110 "two@@signs.com",
111 "colonButNoPort@127.0.0.1:",
113 /* "someone-else@127.0.0.1.26", */
114 ".localStartsWithDot@domain.com",
115 /* "localEndsWithDot.@domain.com", */ /* I don't think this is invalid -- Stu */
116 /* "two..consecutiveDots@domain.com", */ /* I don't think this is invalid -- Stu */
117 "domainStartsWithDash@-domain.com",
118 "domainEndsWithDash@domain-.com",
119 /* "numbersInTLD@domain.c0m", */
120 /* "missingTLD@domain.", */ /* This certainly isn't invalid -- Stu */
121 "! \"#$%(),/;<>[]`|@invalidCharsInLocal.org",
122 "invalidCharsInDomain@! \"#$%(),/;<>_[]`|.org",
123 /* "local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org" */
126 START_TEST(test_util_email_is_valid)
128 size_t i;
130 for (i = 0; i < G_N_ELEMENTS(valid_emails); i++)
131 fail_unless(purple_email_is_valid(valid_emails[i]), "Email address was: %s", valid_emails[i]);
133 for (i = 0; i < G_N_ELEMENTS(invalid_emails); i++)
134 fail_if(purple_email_is_valid(invalid_emails[i]), "Email address was: %s", invalid_emails[i]);
136 END_TEST
138 START_TEST(test_util_ipv6_is_valid)
140 fail_unless(purple_ipv6_address_is_valid("2001:0db8:85a3:0000:0000:8a2e:0370:7334"));
141 fail_unless(purple_ipv6_address_is_valid("2001:db8:85a3:0:0:8a2e:370:7334"));
142 fail_unless(purple_ipv6_address_is_valid("2001:db8:85a3::8a2e:370:7334"));
143 fail_unless(purple_ipv6_address_is_valid("2001:0db8:0:0::1428:57ab"));
144 fail_unless(purple_ipv6_address_is_valid("::1"));
145 fail_unless(purple_ipv6_address_is_valid("1::"));
146 fail_unless(purple_ipv6_address_is_valid("1::1"));
147 fail_unless(purple_ipv6_address_is_valid("::"));
148 fail_if(purple_ipv6_address_is_valid(""));
149 fail_if(purple_ipv6_address_is_valid(":"));
150 fail_if(purple_ipv6_address_is_valid("1.2.3.4"));
151 fail_if(purple_ipv6_address_is_valid("2001::FFD3::57ab"));
152 fail_if(purple_ipv6_address_is_valid("200000000::1"));
153 fail_if(purple_ipv6_address_is_valid("QWERTY::1"));
155 END_TEST
157 START_TEST(test_util_str_to_time)
159 struct tm tm;
160 long tz_off;
161 const char *rest;
162 time_t timestamp;
164 fail_unless(377182200 == purple_str_to_time("19811214T12:50:00", TRUE, NULL, NULL, NULL));
165 fail_unless(1175919261 == purple_str_to_time("20070407T04:14:21", TRUE, NULL, NULL, NULL));
166 fail_unless(1282941722 == purple_str_to_time("2010-08-27.204202", TRUE, NULL, NULL, NULL));
168 timestamp = purple_str_to_time("2010-08-27.134202-0700PDT", FALSE, &tm, &tz_off, &rest);
169 fail_unless(1282941722 == timestamp);
170 fail_unless((-7 * 60 * 60) == tz_off);
171 assert_string_equal("PDT", rest);
173 END_TEST
175 START_TEST(test_markup_html_to_xhtml)
177 gchar *xhtml = NULL;
178 gchar *plaintext = NULL;
179 purple_markup_html_to_xhtml("<a>", &xhtml, &plaintext);
180 assert_string_equal_free("<a href=\"\"></a>", xhtml);
181 assert_string_equal_free("", plaintext);
184 purple_markup_html_to_xhtml("<FONT>x</FONT>", &xhtml, &plaintext);
185 assert_string_equal_free("x", xhtml);
186 assert_string_equal_free("x", plaintext);
188 END_TEST
190 START_TEST(test_utf8_strip_unprintables)
192 fail_unless(NULL == purple_utf8_strip_unprintables(NULL));
193 /* invalid UTF-8 */
194 #if 0
195 /* disabled because make check fails on an assertion */
196 fail_unless(NULL == purple_utf8_strip_unprintables("abc\x80\x7f"));
197 #endif
198 /* \t, \n, \r, space */
199 assert_string_equal_free("ab \tcd\nef\r ", purple_utf8_strip_unprintables("ab \tcd\nef\r "));
200 /* ASCII control characters (stripped) */
201 assert_string_equal_free(" aaaa ", purple_utf8_strip_unprintables(
202 "\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10 aaaa "
203 "\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"));
204 /* Basic ASCII */
205 assert_string_equal_free("Foobar", purple_utf8_strip_unprintables("Foobar"));
206 /* 0xE000 - 0xFFFD (UTF-8 encoded) */
207 /* U+F1F7 */
208 assert_string_equal_free("aaaa\xef\x87\xb7", purple_utf8_strip_unprintables("aaaa\xef\x87\xb7"));
209 #if 0
210 /* disabled because make check fails on an assertion */
211 /* U+DB80 (Private Use High Surrogate, First) -- should be stripped */
212 assert_string_equal_free("aaaa", purple_utf8_strip_unprintables("aaaa\xed\xa0\x80"));
213 /* U+FFFE (should be stripped) */
214 assert_string_equal_free("aaaa", purple_utf8_strip_unprintables("aaaa\xef\xbf\xbe"));
215 #endif
216 /* U+FEFF (should not be stripped) */
217 assert_string_equal_free("aaaa\xef\xbb\xbf", purple_utf8_strip_unprintables("aaaa\xef\xbb\xbf"));
219 END_TEST
221 START_TEST(test_mime_decode_field)
223 gchar *result = purple_mime_decode_field("=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=");
224 assert_string_equal_free("Keld Jørn Simonsen", result);
226 END_TEST
228 START_TEST(test_strdup_withhtml)
230 gchar *result = purple_strdup_withhtml("hi\r\nthere\n");
231 assert_string_equal_free("hi<BR>there<BR>", result);
233 END_TEST
235 Suite *
236 util_suite(void)
238 Suite *s = suite_create("Utility Functions");
240 TCase *tc = tcase_create("Base16");
241 tcase_add_test(tc, test_util_base16_encode);
242 tcase_add_test(tc, test_util_base16_decode);
243 suite_add_tcase(s, tc);
245 tc = tcase_create("Base64");
246 tcase_add_test(tc, test_util_base64_encode);
247 tcase_add_test(tc, test_util_base64_decode);
248 suite_add_tcase(s, tc);
250 tc = tcase_create("Filenames");
251 tcase_add_test(tc, test_util_escape_filename);
252 tcase_add_test(tc, test_util_unescape_filename);
253 suite_add_tcase(s, tc);
255 tc = tcase_create("Strip Mnemonic");
256 tcase_add_test(tc, test_util_text_strip_mnemonic);
257 suite_add_tcase(s, tc);
259 tc = tcase_create("Email");
260 tcase_add_test(tc, test_util_email_is_valid);
261 suite_add_tcase(s, tc);
263 tc = tcase_create("IPv6");
264 tcase_add_test(tc, test_util_ipv6_is_valid);
265 suite_add_tcase(s, tc);
267 tc = tcase_create("Time");
268 tcase_add_test(tc, test_util_str_to_time);
269 suite_add_tcase(s, tc);
271 tc = tcase_create("Markup");
272 tcase_add_test(tc, test_markup_html_to_xhtml);
273 suite_add_tcase(s, tc);
275 tc = tcase_create("Stripping Unparseables");
276 tcase_add_test(tc, test_utf8_strip_unprintables);
277 suite_add_tcase(s, tc);
279 tc = tcase_create("MIME");
280 tcase_add_test(tc, test_mime_decode_field);
281 suite_add_tcase(s, tc);
283 tc = tcase_create("strdup_withhtml");
284 tcase_add_test(tc, test_strdup_withhtml);
285 suite_add_tcase(s, tc);
287 return s;