Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / tests / test_util.c
blobd4ec495e4cab6da47bcf5e14fcd28f60345e8768
1 /*
2 * purple
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include <glib.h>
25 #include "../util.h"
27 /******************************************************************************
28 * base 16 tests
29 *****************************************************************************/
30 static void
31 test_util_base_16_encode(void) {
32 gchar *in = purple_base16_encode((const guchar *)"hello, world!", 14);
33 g_assert_cmpstr("68656c6c6f2c20776f726c642100", ==, in);
36 static void
37 test_util_base_16_decode(void) {
38 gsize sz = 0;
39 guchar *out = purple_base16_decode("21646c726f77202c6f6c6c656800", &sz);
41 g_assert_cmpint(sz, ==, 14);
42 g_assert_cmpstr("!dlrow ,olleh", ==, (const gchar *)out);
45 /******************************************************************************
46 * filename escape tests
47 *****************************************************************************/
48 static void
49 test_util_filename_escape(void) {
50 g_assert_cmpstr("foo", ==, purple_escape_filename("foo"));
51 g_assert_cmpstr("@oo", ==, purple_escape_filename("@oo"));
52 g_assert_cmpstr("#oo", ==, purple_escape_filename("#oo"));
53 g_assert_cmpstr("-oo", ==, purple_escape_filename("-oo"));
54 g_assert_cmpstr("_oo", ==, purple_escape_filename("_oo"));
55 g_assert_cmpstr(".oo", ==, purple_escape_filename(".oo"));
56 g_assert_cmpstr("%25oo", ==, purple_escape_filename("%oo"));
57 g_assert_cmpstr("%21oo", ==, purple_escape_filename("!oo"));
60 static void
61 test_util_filename_unescape(void) {
62 g_assert_cmpstr("bar", ==, purple_unescape_filename("bar"));
63 g_assert_cmpstr("@ar", ==, purple_unescape_filename("@ar"));
64 g_assert_cmpstr("!ar", ==, purple_unescape_filename("!ar"));
65 g_assert_cmpstr("!ar", ==, purple_unescape_filename("%21ar"));
66 g_assert_cmpstr("%ar", ==, purple_unescape_filename("%25ar"));
69 /******************************************************************************
70 * text_strip tests
71 *****************************************************************************/
72 static void
73 test_util_text_strip_mnemonic(void) {
74 g_assert_cmpstr("", ==, purple_text_strip_mnemonic(""));
75 g_assert_cmpstr("foo", ==, purple_text_strip_mnemonic("foo"));
76 g_assert_cmpstr("foo", ==, purple_text_strip_mnemonic("_foo"));
80 /******************************************************************************
81 * email tests
82 *****************************************************************************/
84 * Many of the valid and invalid email addresses lised below are from
85 * http://fightingforalostcause.net/misc/2006/compare-email-regex.php
87 const gchar *valid_emails[] = {
88 "purple-devel@lists.sf.net",
89 "l3tt3rsAndNumb3rs@domain.com",
90 "has-dash@domain.com",
91 "hasApostrophe.o'leary@domain.org",
92 "uncommonTLD@domain.museum",
93 "uncommonTLD@domain.travel",
94 "uncommonTLD@domain.mobi",
95 "countryCodeTLD@domain.uk",
96 "countryCodeTLD@domain.rw",
97 "lettersInDomain@911.com",
98 "underscore_inLocal@domain.net",
99 "IPInsteadOfDomain@127.0.0.1",
100 /* "IPAndPort@127.0.0.1:25", */
101 "subdomain@sub.domain.com",
102 "local@dash-inDomain.com",
103 "dot.inLocal@foo.com",
104 "a@singleLetterLocal.org",
105 "singleLetterDomain@x.org",
106 "&*=?^+{}'~@validCharsInLocal.net",
107 "foor@bar.newTLD",
108 "HenryTheGreatWhiteCricket@live.ca",
109 "HenryThe__WhiteCricket@hotmail.com"
112 static void
113 test_util_email_is_valid(void) {
114 size_t i;
116 for (i = 0; i < G_N_ELEMENTS(valid_emails); i++)
117 g_assert_true(purple_email_is_valid(valid_emails[i]));
120 const gchar *invalid_emails[] = {
121 "purple-devel@@lists.sf.net",
122 "purple@devel@lists.sf.net",
123 "purple-devel@list..sf.net",
124 "purple-devel",
125 "purple-devel@",
126 "@lists.sf.net",
127 "totally bogus",
128 "missingDomain@.com",
129 "@missingLocal.org",
130 "missingatSign.net",
131 "missingDot@com",
132 "two@@signs.com",
133 "colonButNoPort@127.0.0.1:",
135 /* "someone-else@127.0.0.1.26", */
136 ".localStartsWithDot@domain.com",
137 /* "localEndsWithDot.@domain.com", */ /* I don't think this is invalid -- Stu */
138 /* "two..consecutiveDots@domain.com", */ /* I don't think this is invalid -- Stu */
139 "domainStartsWithDash@-domain.com",
140 "domainEndsWithDash@domain-.com",
141 /* "numbersInTLD@domain.c0m", */
142 /* "missingTLD@domain.", */ /* This certainly isn't invalid -- Stu */
143 "! \"#$%(),/;<>[]`|@invalidCharsInLocal.org",
144 "invalidCharsInDomain@! \"#$%(),/;<>_[]`|.org",
145 /* "local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org" */
148 static void
149 test_util_email_is_invalid(void) {
150 size_t i;
152 for (i = 0; i < G_N_ELEMENTS(invalid_emails); i++)
153 g_assert_false(purple_email_is_valid(invalid_emails[i]));
156 /******************************************************************************
157 * str_to_time tests
158 *****************************************************************************/
159 static void
160 test_util_str_to_time(void) {
161 struct tm tm;
162 glong tz_off;
163 const gchar *rest;
164 time_t timestamp;
166 g_assert_cmpint(377182200, ==, purple_str_to_time("19811214T12:50:00", TRUE, NULL, NULL, NULL));
167 g_assert_cmpint(1175919261, ==, purple_str_to_time("20070407T04:14:21", TRUE, NULL, NULL, NULL));
168 g_assert_cmpint(1282941722, ==, purple_str_to_time("2010-08-27.204202", TRUE, NULL, NULL, NULL));
170 timestamp = purple_str_to_time("2010-08-27.134202-0700PDT", FALSE, &tm, &tz_off, &rest);
171 g_assert_cmpint(1282941722, ==, timestamp);
172 g_assert_cmpint((-7 * 60 * 60), ==, tz_off);
173 g_assert_cmpstr("PDT", ==, rest);
176 /******************************************************************************
177 * str_to_date_time tests
178 *****************************************************************************/
179 static void
180 test_util_str_to_date_time(void)
182 GDateTime *dt;
184 dt = purple_str_to_date_time("19811214T12:50:00", TRUE);
185 g_assert_cmpint(377182200, ==, g_date_time_to_unix(dt));
186 g_assert_cmpint(0, ==, g_date_time_get_utc_offset(dt));
187 g_date_time_unref(dt);
189 dt = purple_str_to_date_time("20070407T04:14:21.1234", TRUE);
190 g_assert_cmpint(1175919261, ==, g_date_time_to_unix(dt));
191 g_assert_cmpint(0, ==, g_date_time_get_utc_offset(dt));
192 g_assert_cmpint(123400, ==, g_date_time_get_microsecond(dt));
193 g_date_time_unref(dt);
195 dt = purple_str_to_date_time("2010-08-27.204202", TRUE);
196 g_assert_cmpint(1282941722, ==, g_date_time_to_unix(dt));
197 g_assert_cmpint(0, ==, g_date_time_get_utc_offset(dt));
198 g_date_time_unref(dt);
200 dt = purple_str_to_date_time("2010-08-27.204202.123456", TRUE);
201 g_assert_cmpint(1282941722, ==, g_date_time_to_unix(dt));
202 g_assert_cmpint(0, ==, g_date_time_get_utc_offset(dt));
203 g_assert_cmpint(123456, ==, g_date_time_get_microsecond(dt));
204 g_date_time_unref(dt);
206 dt = purple_str_to_date_time("2010-08-27.134202-0700PDT", FALSE);
207 g_assert_cmpint(1282941722, ==, g_date_time_to_unix(dt));
208 g_assert_cmpint((-7LL * 60 * 60 * G_USEC_PER_SEC), ==, g_date_time_get_utc_offset(dt));
210 dt = purple_str_to_date_time("2010-08-27.134202.1234-0700PDT", FALSE);
211 g_assert_cmpint(1282941722, ==, g_date_time_to_unix(dt));
212 g_assert_cmpint(123400, ==, g_date_time_get_microsecond(dt));
213 g_assert_cmpint((-7LL * 60 * 60 * G_USEC_PER_SEC), ==, g_date_time_get_utc_offset(dt));
216 /******************************************************************************
217 * Markup tests
218 *****************************************************************************/
219 typedef struct {
220 gchar *markup;
221 gchar *xhtml;
222 gchar *plaintext;
223 } MarkupTestData;
225 static void
226 test_util_markup_html_to_xhtml(void) {
227 gint i;
228 MarkupTestData data[] = {
230 "<a>",
231 "<a href=\"\"></a>",
233 }, {
234 "<A href='URL'>ABOUT</a>",
235 "<a href=\"URL\">ABOUT</a>",
236 "ABOUT <URL>",
237 }, {
238 "<a href='URL'>URL</a>",
239 "<a href=\"URL\">URL</a>",
240 "URL",
241 }, {
242 "<a href='mailto:mail'>mail</a>",
243 "<a href=\"mailto:mail\">mail</a>",
244 "mail",
245 }, {
246 "<A href='\"U&apos;R&L'>ABOUT</a>",
247 "<a href=\"&quot;U&apos;R&amp;L\">ABOUT</a>",
248 "ABOUT <\"U'R&L>",
249 }, {
250 "<img src='SRC' alt='ALT'/>",
251 "<img src='SRC' alt='ALT' />",
252 "ALT",
253 }, {
254 "<img src=\"'S&apos;R&C\" alt=\"'A&apos;L&T\"/>",
255 "<img src='&apos;S&apos;R&amp;C' alt='&apos;A&apos;L&amp;T' />",
256 "'A'L&T",
257 }, {
258 "<unknown>",
259 "&lt;unknown>",
260 "<unknown>",
261 }, {
262 "&eacute;&amp;",
263 "&eacute;&amp;",
264 "&eacute;&",
265 }, {
266 "<h1>A<h2>B</h2>C</h1>",
267 "<h1>A<h2>B</h2>C</h1>",
268 "ABC",
269 }, {
270 "<h1><h2><h3><h4>",
271 "<h1><h2><h3><h4></h4></h3></h2></h1>",
273 }, {
274 "<italic/>",
275 "<em/>",
277 }, {
278 "</",
279 "&lt;/",
280 "</",
281 }, {
282 "</div>",
285 }, {
286 "<hr/>",
287 "<br/>",
288 "\n",
289 }, {
290 "<hr>",
291 "<br/>",
292 "\n",
293 }, {
294 "<br />",
295 "<br/>",
296 "\n",
297 }, {
298 "<br>INSIDE</br>",
299 "<br/>INSIDE",
300 "\nINSIDE",
301 }, {
302 "<div></div>",
303 "<div></div>",
305 }, {
306 "<div/>",
307 "<div/>",
309 }, {
310 "<div attr='\"&<>'/>",
311 "<div attr='&quot;&amp;&lt;&gt;'/>",
313 }, {
314 "<div attr=\"'\"/>",
315 "<div attr=\"&apos;\"/>",
317 }, {
318 "<div/> < <div/>",
319 "<div/> &lt; <div/>",
320 " < ",
321 }, {
322 "<div>x</div>",
323 "<div>x</div>",
324 "x",
325 }, {
326 "<b>x</b>",
327 "<span style='font-weight: bold;'>x</span>",
328 "x",
329 }, {
330 "<bold>x</bold>",
331 "<span style='font-weight: bold;'>x</span>",
332 "x",
333 }, {
334 "<strong>x</strong>",
335 "<span style='font-weight: bold;'>x</span>",
336 "x",
337 }, {
338 "<u>x</u>",
339 "<span style='text-decoration: underline;'>x</span>",
340 "x",
341 }, {
342 "<underline>x</underline>",
343 "<span style='text-decoration: underline;'>x</span>",
344 "x",
345 }, {
346 "<s>x</s>",
347 "<span style='text-decoration: line-through;'>x</span>",
348 "x",
349 }, {
350 "<strike>x</strike>",
351 "<span style='text-decoration: line-through;'>x</span>",
352 "x",
353 }, {
354 "<sub>x</sub>",
355 "<span style='vertical-align:sub;'>x</span>",
356 "x",
357 }, {
358 "<sup>x</sup>",
359 "<span style='vertical-align:super;'>x</span>",
360 "x",
361 }, {
362 "<FONT>x</FONT>",
363 "x",
364 "x",
365 }, {
366 "<font face=\"'Times&gt;New & Roman'\">x</font>",
367 "<span style='font-family: \"Times&gt;New &amp; Roman\";'>x</span>",
368 "x",
369 }, {
370 "<font back=\"'color&gt;blue&red'\">x</font>",
371 "<span style='background: \"color&gt;blue&amp;red\";'>x</span>",
372 "x",
373 }, {
374 "<font color=\"'color&gt;blue&red'\">x</font>",
375 "<span style='color: \"color&gt;blue&amp;red\";'>x</span>",
376 "x",
377 }, {
378 "<font size=1>x</font>",
379 "<span style='font-size: xx-small;'>x</span>",
380 "x",
381 }, {
382 "<font size=432>x</font>",
383 "<span style='font-size: medium;'>x</span>",
384 "x",
385 }, {
386 "<!--COMMENT-->",
387 "<!--COMMENT-->",
388 "COMMENT-->",
389 }, {
390 "<br />",
391 "&lt;br />",
392 "<br />",
393 }, {
394 "<hr />",
395 "&lt;hr />",
396 "<hr />"
397 }, {
398 NULL, NULL, NULL,
402 for(i = 0; data[i].markup; i++) {
403 gchar *xhtml = NULL, *plaintext = NULL;
405 purple_markup_html_to_xhtml(data[i].markup, &xhtml, &plaintext);
407 g_assert_cmpstr(data[i].xhtml, ==, xhtml);
408 g_free(xhtml);
410 g_assert_cmpstr(data[i].plaintext, ==, plaintext);
411 g_free(plaintext);
415 /******************************************************************************
416 * UTF8 tests
417 *****************************************************************************/
418 typedef struct {
419 gchar *input;
420 gchar *output;
421 } UTF8TestData;
423 static void
424 test_util_utf8_strip_unprintables(void) {
425 gint i;
426 UTF8TestData data[] = {
428 /* \t, \n, \r, space */
429 "ab \tcd\nef\r ",
430 "ab \tcd\nef\r ",
431 }, {
432 /* ASCII control characters (stripped) */
433 "\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10 aaaa "
434 "\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
435 " aaaa ",
436 }, {
437 /* Basic ASCII */
438 "Foobar",
439 "Foobar",
440 }, {
441 /* 0xE000 - 0xFFFD (UTF-8 encoded) */
442 /* U+F1F7 */
443 "aaaa\xef\x87\xb7",
444 "aaaa\xef\x87\xb7",
445 }, {
446 /* U+FEFF (should not be stripped) */
447 "aaaa\xef\xbb\xbf",
448 "aaaa\xef\xbb\xbf",
449 }, {
450 /* U+FFFE (should be stripped) */
451 "aaaa\xef\xbf\xbe",
452 "aaaa",
453 }, {
454 NULL,
455 NULL,
459 for(i = 0; ; i++) {
460 gchar *result = purple_utf8_strip_unprintables(data[i].input);
462 g_assert_cmpstr(data[i].output, ==, result);
464 g_free(result);
466 /* NULL as input is a valid test, but it's the last test, so we break
467 * after it.
469 if(data[i].input == NULL)
470 break;
474 /******************************************************************************
475 * strdup_withhtml tests
476 *****************************************************************************/
477 static void
478 test_util_strdup_withhtml(void) {
479 gchar *result = purple_strdup_withhtml("hi\r\nthere\n");
481 g_assert_cmpstr("hi<BR>there<BR>", ==, result);
483 g_free(result);
486 /******************************************************************************
487 * URI Escaping
488 *****************************************************************************/
489 static void
490 test_uri_escape_for_open(void) {
491 /* make sure shell stuff is escaped... */
492 gchar *result = purple_uri_escape_for_open("https://$(xterm)");
493 g_assert_cmpstr("https://%24%28xterm%29", ==, result);
494 g_free(result);
496 result = purple_uri_escape_for_open("https://`xterm`");
497 g_assert_cmpstr("https://%60xterm%60", ==, result);
498 g_free(result);
500 result = purple_uri_escape_for_open("https://$((25 + 13))");
501 g_assert_cmpstr("https://%24%28%2825%20+%2013%29%29", ==, result);
502 g_free(result);
504 /* ...but keep brackets so that ipv6 links can be opened. */
505 result = purple_uri_escape_for_open("https://[123:4567:89a::::]");
506 g_assert_cmpstr("https://[123:4567:89a::::]", ==, result);
507 g_free(result);
510 /******************************************************************************
511 * MANE
512 *****************************************************************************/
513 gint
514 main(gint argc, gchar **argv) {
515 g_test_init(&argc, &argv, NULL);
517 g_test_add_func("/util/base/16/encode",
518 test_util_base_16_encode);
519 g_test_add_func("/util/base/16/decode",
520 test_util_base_16_decode);
522 g_test_add_func("/util/filename/escape",
523 test_util_filename_escape);
524 g_test_add_func("/util/filename/unescape",
525 test_util_filename_unescape);
527 g_test_add_func("/util/mnemonic/strip",
528 test_util_text_strip_mnemonic);
530 g_test_add_func("/util/email/is valid",
531 test_util_email_is_valid);
532 g_test_add_func("/util/email/is invalid",
533 test_util_email_is_invalid);
535 g_test_add_func("/util/str to time",
536 test_util_str_to_time);
538 g_test_add_func("/util/str to date time",
539 test_util_str_to_date_time);
541 g_test_add_func("/util/markup/html to xhtml",
542 test_util_markup_html_to_xhtml);
544 g_test_add_func("/util/utf8/strip unprintables",
545 test_util_utf8_strip_unprintables);
547 g_test_add_func("/util/test_strdup_withhtml",
548 test_util_strdup_withhtml);
550 g_test_add_func("/util/test_uri_escape_for_open",
551 test_uri_escape_for_open);
553 return g_test_run();