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
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
27 /******************************************************************************
29 *****************************************************************************/
31 test_util_base_16_encode(void) {
32 gchar
*in
= purple_base16_encode((const guchar
*)"hello, world!", 14);
33 g_assert_cmpstr("68656c6c6f2c20776f726c642100", ==, in
);
37 test_util_base_16_decode(void) {
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 *****************************************************************************/
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"));
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 /******************************************************************************
71 *****************************************************************************/
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 /******************************************************************************
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",
108 "HenryTheGreatWhiteCricket@live.ca",
109 "HenryThe__WhiteCricket@hotmail.com"
113 test_util_email_is_valid(void) {
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",
128 "missingDomain@.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" */
149 test_util_email_is_invalid(void) {
152 for (i
= 0; i
< G_N_ELEMENTS(invalid_emails
); i
++)
153 g_assert_false(purple_email_is_valid(invalid_emails
[i
]));
156 /******************************************************************************
158 *****************************************************************************/
160 test_util_str_to_time(void) {
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 *****************************************************************************/
180 test_util_str_to_date_time(void)
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 /******************************************************************************
218 *****************************************************************************/
226 test_util_markup_html_to_xhtml(void) {
228 MarkupTestData data
[] = {
234 "<A href='URL'>ABOUT</a>",
235 "<a href=\"URL\">ABOUT</a>",
238 "<a href='URL'>URL</a>",
239 "<a href=\"URL\">URL</a>",
242 "<a href='mailto:mail'>mail</a>",
243 "<a href=\"mailto:mail\">mail</a>",
246 "<A href='\"U'R&L'>ABOUT</a>",
247 "<a href=\""U'R&L\">ABOUT</a>",
250 "<img src='SRC' alt='ALT'/>",
251 "<img src='SRC' alt='ALT' />",
254 "<img src=\"'S'R&C\" alt=\"'A'L&T\"/>",
255 "<img src=''S'R&C' alt=''A'L&T' />",
266 "<h1>A<h2>B</h2>C</h1>",
267 "<h1>A<h2>B</h2>C</h1>",
271 "<h1><h2><h3><h4></h4></h3></h2></h1>",
310 "<div attr='\"&<>'/>",
311 "<div attr='"&<>'/>",
315 "<div attr=\"'\"/>",
319 "<div/> < <div/>",
327 "<span style='font-weight: bold;'>x</span>",
331 "<span style='font-weight: bold;'>x</span>",
334 "<strong>x</strong>",
335 "<span style='font-weight: bold;'>x</span>",
339 "<span style='text-decoration: underline;'>x</span>",
342 "<underline>x</underline>",
343 "<span style='text-decoration: underline;'>x</span>",
347 "<span style='text-decoration: line-through;'>x</span>",
350 "<strike>x</strike>",
351 "<span style='text-decoration: line-through;'>x</span>",
355 "<span style='vertical-align:sub;'>x</span>",
359 "<span style='vertical-align:super;'>x</span>",
366 "<font face=\"'Times>New & Roman'\">x</font>",
367 "<span style='font-family: \"Times>New & Roman\";'>x</span>",
370 "<font back=\"'color>blue&red'\">x</font>",
371 "<span style='background: \"color>blue&red\";'>x</span>",
374 "<font color=\"'color>blue&red'\">x</font>",
375 "<span style='color: \"color>blue&red\";'>x</span>",
378 "<font size=1>x</font>",
379 "<span style='font-size: xx-small;'>x</span>",
382 "<font size=432>x</font>",
383 "<span style='font-size: medium;'>x</span>",
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
);
410 g_assert_cmpstr(data
[i
].plaintext
, ==, plaintext
);
415 /******************************************************************************
417 *****************************************************************************/
424 test_util_utf8_strip_unprintables(void) {
426 UTF8TestData data
[] = {
428 /* \t, \n, \r, space */
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",
441 /* 0xE000 - 0xFFFD (UTF-8 encoded) */
446 /* U+FEFF (should not be stripped) */
450 /* U+FFFE (should be stripped) */
460 gchar
*result
= purple_utf8_strip_unprintables(data
[i
].input
);
462 g_assert_cmpstr(data
[i
].output
, ==, result
);
466 /* NULL as input is a valid test, but it's the last test, so we break
469 if(data
[i
].input
== NULL
)
474 /******************************************************************************
475 * strdup_withhtml tests
476 *****************************************************************************/
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
);
486 /******************************************************************************
488 *****************************************************************************/
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
);
496 result
= purple_uri_escape_for_open("https://`xterm`");
497 g_assert_cmpstr("https://%60xterm%60", ==, result
);
500 result
= purple_uri_escape_for_open("https://$((25 + 13))");
501 g_assert_cmpstr("https://%24%28%2825%20+%2013%29%29", ==, 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
);
510 /******************************************************************************
512 *****************************************************************************/
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
);