2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file lib/test.c @brief Library tests */
31 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
49 #include "inputline.h"
55 #include "selection.h"
62 #include "configuration.h"
67 static int tests
, errors
;
68 static int fail_first
;
70 static void count_error() {
76 /** @brief Checks that @p expr is nonzero */
77 #define insist(expr) do { \
80 fprintf(stderr, "%s:%d: error checking %s\n", \
81 __FILE__, __LINE__, #expr); \
86 static const char *format(const char *s
) {
92 while((c
= (unsigned char)*s
++)) {
93 if(c
>= ' ' && c
<= '~')
96 sprintf(buf
, "\\x%02X", (unsigned)c
);
97 dynstr_append_string(&d
, buf
);
100 dynstr_terminate(&d
);
104 static const char *format_utf32(const uint32_t *s
) {
111 sprintf(buf
, " %04lX", (long)c
);
112 dynstr_append_string(&d
, buf
);
114 dynstr_terminate(&d
);
118 #define check_string(GOT, WANT) do { \
119 const char *got = GOT; \
120 const char *want = WANT; \
123 fprintf(stderr, "%s:%d: %s returned 0\n", \
124 __FILE__, __LINE__, #GOT); \
126 } else if(strcmp(want, got)) { \
127 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \
128 __FILE__, __LINE__, #GOT, format(got), format(want)); \
134 #define check_string_prefix(GOT, WANT) do { \
135 const char *got = GOT; \
136 const char *want = WANT; \
139 fprintf(stderr, "%s:%d: %s returned 0\n", \
140 __FILE__, __LINE__, #GOT); \
142 } else if(strncmp(want, got, strlen(want))) { \
143 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n", \
144 __FILE__, __LINE__, #GOT, format(got), format(want)); \
150 #define check_integer(GOT, WANT) do { \
151 const intmax_t got = GOT, want = WANT; \
153 fprintf(stderr, "%s:%d: %s returned: %jd expected: %jd\n", \
154 __FILE__, __LINE__, #GOT, got, want); \
160 static uint32_t *ucs4parse(const char *s
) {
161 struct dynstr_ucs4 d
;
164 dynstr_ucs4_init(&d
);
167 dynstr_ucs4_append(&d
, strtoul(s
, &e
, 0));
168 if(errno
) fatal(errno
, "strtoul (%s)", s
);
171 dynstr_ucs4_terminate(&d
);
175 static void test_utf8(void) {
176 /* Test validutf8, convert to UCS-4, check the answer is right,
177 * convert back to UTF-8, check we got to where we started */
178 #define U8(CHARS, WORDS) do { \
179 uint32_t *w = ucs4parse(WORDS); \
183 insist(validutf8(CHARS)); \
184 ucs = utf8_to_utf32(CHARS, strlen(CHARS), 0); \
186 insist(!utf32_cmp(w, ucs)); \
187 u8 = utf32_to_utf8(ucs, utf32_len(ucs), 0); \
189 check_string(u8, CHARS); \
192 fprintf(stderr
, "test_utf8\n");
193 #define validutf8(S) utf8_valid((S), strlen(S))
199 /* ASCII characters */
201 U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
202 "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d "
203 "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a "
204 "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 "
205 "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 "
206 "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 "
207 "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e "
208 "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b "
210 U8("\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\177",
211 "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 "
212 "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d "
217 /* UTF8-2 = %xC2-DF UTF8-tail */
218 insist(!validutf8("\xC0\x80"));
219 insist(!validutf8("\xC1\x80"));
220 insist(!validutf8("\xC2\x7F"));
221 U8("\xC2\x80", "0x80");
222 U8("\xDF\xBF", "0x7FF");
223 insist(!validutf8("\xDF\xC0"));
225 /* UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
226 * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
228 insist(!validutf8("\xE0\x9F\x80"));
229 U8("\xE0\xA0\x80", "0x800");
230 U8("\xE0\xBF\xBF", "0xFFF");
231 insist(!validutf8("\xE0\xC0\xBF"));
233 insist(!validutf8("\xE1\x80\x7F"));
234 U8("\xE1\x80\x80", "0x1000");
235 U8("\xEC\xBF\xBF", "0xCFFF");
236 insist(!validutf8("\xEC\xC0\xBF"));
238 U8("\xED\x80\x80", "0xD000");
239 U8("\xED\x9F\xBF", "0xD7FF");
240 insist(!validutf8("\xED\xA0\xBF"));
242 insist(!validutf8("\xEE\x7f\x80"));
243 U8("\xEE\x80\x80", "0xE000");
244 U8("\xEF\xBF\xBF", "0xFFFF");
245 insist(!validutf8("\xEF\xC0\xBF"));
247 /* UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
248 * %xF4 %x80-8F 2( UTF8-tail )
250 insist(!validutf8("\xF0\x8F\x80\x80"));
251 U8("\xF0\x90\x80\x80", "0x10000");
252 U8("\xF0\xBF\xBF\xBF", "0x3FFFF");
253 insist(!validutf8("\xF0\xC0\x80\x80"));
255 insist(!validutf8("\xF1\x80\x80\x7F"));
256 U8("\xF1\x80\x80\x80", "0x40000");
257 U8("\xF3\xBF\xBF\xBF", "0xFFFFF");
258 insist(!validutf8("\xF3\xC0\x80\x80"));
260 insist(!validutf8("\xF4\x80\x80\x7F"));
261 U8("\xF4\x80\x80\x80", "0x100000");
262 U8("\xF4\x8F\xBF\xBF", "0x10FFFF");
263 insist(!validutf8("\xF4\x90\x80\x80"));
264 insist(!validutf8("\xF4\x80\xFF\x80"));
266 /* miscellaneous non-UTF-8 rubbish */
267 insist(!validutf8("\x80"));
268 insist(!validutf8("\xBF"));
269 insist(!validutf8("\xC0"));
270 insist(!validutf8("\xC0\x7F"));
271 insist(!validutf8("\xC0\xC0"));
272 insist(!validutf8("\xE0"));
273 insist(!validutf8("\xE0\x7F"));
274 insist(!validutf8("\xE0\xC0"));
275 insist(!validutf8("\xE0\x80"));
276 insist(!validutf8("\xE0\x80\x7f"));
277 insist(!validutf8("\xE0\x80\xC0"));
278 insist(!validutf8("\xF0"));
279 insist(!validutf8("\xF0\x7F"));
280 insist(!validutf8("\xF0\xC0"));
281 insist(!validutf8("\xF0\x80"));
282 insist(!validutf8("\xF0\x80\x7f"));
283 insist(!validutf8("\xF0\x80\xC0"));
284 insist(!validutf8("\xF0\x80\x80\x7f"));
285 insist(!validutf8("\xF0\x80\x80\xC0"));
286 insist(!validutf8("\xF5\x80\x80\x80"));
287 insist(!validutf8("\xF8"));
290 static int test_multipart_callback(const char *s
, void *u
) {
291 struct vector
*parts
= u
;
293 vector_append(parts
, (char *)s
);
297 static void test_mime(void) {
299 struct vector parts
[1];
301 fprintf(stderr
, "test_mime\n");
304 insist(!mime_content_type("text/plain", &t
, &n
, &v
));
305 check_string(t
, "text/plain");
309 insist(mime_content_type("TEXT ((broken) comment", &t
, &n
, &v
) < 0);
310 insist(mime_content_type("TEXT ((broken) comment\\", &t
, &n
, &v
) < 0);
313 insist(!mime_content_type("TEXT ((nested)\\ comment) /plain", &t
, &n
, &v
));
314 check_string(t
, "text/plain");
319 insist(!mime_content_type(" text/plain ; Charset=\"utf-\\8\"", &t
, &n
, &v
));
320 check_string(t
, "text/plain");
321 check_string(n
, "charset");
322 check_string(v
, "utf-8");
325 insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t
, &n
, &v
));
326 check_string(t
, "text/plain");
327 check_string(n
, "charset");
328 check_string(v
, "ISO-8859-1");
331 insist(!mime_rfc2388_content_disposition("form-data; name=\"field1\"", &t
, &n
, &v
));
332 check_string(t
, "form-data");
333 check_string(n
, "name");
334 check_string(v
, "field1");
336 insist(!mime_rfc2388_content_disposition("inline", &t
, &n
, &v
));
337 check_string(t
, "inline");
341 /* Current versions of the code only understand a single arg to these
342 * headers. This is a bug at the level they work at but suffices for
343 * DisOrder's current purposes. */
345 insist(!mime_rfc2388_content_disposition(
346 "attachment; filename=genome.jpeg;\n"
347 "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"",
349 check_string(t
, "attachment");
350 check_string(n
, "filename");
351 check_string(v
, "genome.jpeg");
354 insist(mime_multipart("--outer\r\n"
355 "Content-Type: text/plain\r\n"
356 "Content-Disposition: inline\r\n"
357 "Content-Description: text-part-1\r\n"
359 "Some text goes here\r\n"
362 "Content-Type: multipart/mixed; boundary=inner\r\n"
363 "Content-Disposition: attachment\r\n"
364 "Content-Description: multipart-2\r\n"
367 "Content-Type: text/plain\r\n"
368 "Content-Disposition: inline\r\n"
369 "Content-Description: text-part-2\r\n"
371 "Some more text here.\r\n"
374 "Content-Type: image/jpeg\r\n"
375 "Content-Disposition: attachment\r\n"
376 "Content-Description: jpeg-1\r\n"
381 test_multipart_callback
,
384 check_integer(parts
->nvec
, 2);
385 check_string(parts
->vec
[0],
386 "Content-Type: text/plain\r\n"
387 "Content-Disposition: inline\r\n"
388 "Content-Description: text-part-1\r\n"
390 "Some text goes here\r\n");
391 check_string(parts
->vec
[1],
392 "Content-Type: multipart/mixed; boundary=inner\r\n"
393 "Content-Disposition: attachment\r\n"
394 "Content-Description: multipart-2\r\n"
397 "Content-Type: text/plain\r\n"
398 "Content-Disposition: inline\r\n"
399 "Content-Description: text-part-2\r\n"
401 "Some more text here.\r\n"
404 "Content-Type: image/jpeg\r\n"
405 "Content-Disposition: attachment\r\n"
406 "Content-Description: jpeg-1\r\n"
410 /* No trailing CRLF is _correct_ - see RFC2046 5.1.1 note regarding CRLF
411 * preceding the boundary delimiter line. An implication of this is that we
412 * must cope with partial lines at the end of the input when recursively
413 * decomposing a multipart message. */
415 insist(mime_multipart("--inner\r\n"
416 "Content-Type: text/plain\r\n"
417 "Content-Disposition: inline\r\n"
418 "Content-Description: text-part-2\r\n"
420 "Some more text here.\r\n"
423 "Content-Type: image/jpeg\r\n"
424 "Content-Disposition: attachment\r\n"
425 "Content-Description: jpeg-1\r\n"
429 test_multipart_callback
,
432 check_integer(parts
->nvec
, 2);
433 check_string(parts
->vec
[0],
434 "Content-Type: text/plain\r\n"
435 "Content-Disposition: inline\r\n"
436 "Content-Description: text-part-2\r\n"
438 "Some more text here.\r\n");
439 check_string(parts
->vec
[1],
440 "Content-Type: image/jpeg\r\n"
441 "Content-Disposition: attachment\r\n"
442 "Content-Description: jpeg-1\r\n"
448 check_string(mime_qp(""), "");
449 check_string(mime_qp("foobar"), "foobar");
450 check_string(mime_qp("foo=20bar"), "foo bar");
451 check_string(mime_qp("x \r\ny"), "x\r\ny");
452 check_string(mime_qp("x=\r\ny"), "xy");
453 check_string(mime_qp("x= \r\ny"), "xy");
454 check_string(mime_qp("x =\r\ny"), "x y");
455 check_string(mime_qp("x = \r\ny"), "x y");
457 check_string(mime_to_qp(""), "");
458 check_string(mime_to_qp("foobar\n"), "foobar\n");
459 check_string(mime_to_qp("foobar \n"), "foobar=20\n");
460 check_string(mime_to_qp("foobar\t\n"), "foobar=09\n");
461 check_string(mime_to_qp("foobar \t \n"), "foobar=20=09=20\n");
462 check_string(mime_to_qp(" foo=bar"), " foo=3Dbar\n");
463 check_string(mime_to_qp("copyright \xC2\xA9"), "copyright =C2=A9\n");
464 check_string(mime_to_qp("foo\nbar\nbaz\n"), "foo\nbar\nbaz\n");
465 check_string(mime_to_qp("wibble wobble wibble wobble wibble wobble wibble wobble wibble wobble wibble"), "wibble wobble wibble wobble wibble wobble wibble wobble wibble wobble wibb=\nle\n");
468 check_string(mime_qp("Now's the time =\r\n"
469 "for all folk to come=\r\n"
470 " to the aid of their country."),
471 "Now's the time for all folk to come to the aid of their country.");
473 #define check_base64(encoded, decoded) do { \
474 check_string(mime_base64(encoded, 0), decoded); \
475 check_string(mime_to_base64((const uint8_t *)decoded, \
476 (sizeof decoded) - 1), \
481 check_base64("", "");
482 check_base64("BBBB", "\x04\x10\x41");
483 check_base64("////", "\xFF\xFF\xFF");
484 check_base64("//BB", "\xFF\xF0\x41");
485 check_base64("BBBB//BB////",
486 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
487 check_base64("BBBBBA==",
488 "\x04\x10\x41" "\x04");
489 check_base64("BBBBBBA=",
490 "\x04\x10\x41" "\x04\x10");
492 /* Check that decoding handles various kinds of rubbish OK */
493 check_string(mime_base64("B B B B / / B B / / / /", 0),
494 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
495 check_string(mime_base64("B\r\nBBB.// B-B//~//", 0),
496 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
497 check_string(mime_base64("BBBB BB==", 0),
498 "\x04\x10\x41" "\x04");
499 check_string(mime_base64("BBBB BB = =", 0),
500 "\x04\x10\x41" "\x04");
501 check_string(mime_base64("BBBB BBB=", 0),
502 "\x04\x10\x41" "\x04\x10");
503 check_string(mime_base64("BBBB BBB = ", 0),
504 "\x04\x10\x41" "\x04\x10");
505 check_string(mime_base64("BBBB=", 0),
507 check_string(mime_base64("BBBBBB==", 0),
508 "\x04\x10\x41" "\x04");
509 check_string(mime_base64("BBBBBBB=", 0),
510 "\x04\x10\x41" "\x04\x10");
511 /* Not actually valid base64 */
512 check_string(mime_base64("BBBBx=", 0),
516 static void test_cookies(void) {
517 struct cookiedata cd
[1];
519 fprintf(stderr
, "test_cookies\n");
521 /* These are the examples from RFC2109 */
522 insist(!parse_cookie("$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"", cd
));
523 insist(!strcmp(cd
->version
, "1"));
524 insist(cd
->ncookies
= 1);
525 insist(find_cookie(cd
, "Customer") == &cd
->cookies
[0]);
526 check_string(cd
->cookies
[0].value
, "WILE_E_COYOTE");
527 check_string(cd
->cookies
[0].path
, "/acme");
528 insist(cd
->cookies
[0].domain
== 0);
529 insist(!parse_cookie("$Version=\"1\";\n"
530 "Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\";\n"
531 "Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\"",
533 insist(cd
->ncookies
= 2);
534 insist(find_cookie(cd
, "Customer") == &cd
->cookies
[0]);
535 insist(find_cookie(cd
, "Part_Number") == &cd
->cookies
[1]);
536 check_string(cd
->cookies
[0].value
, "WILE_E_COYOTE");
537 check_string(cd
->cookies
[0].path
, "/acme");
538 insist(cd
->cookies
[0].domain
== 0);
539 check_string(cd
->cookies
[1].value
, "Rocket_Launcher_0001");
540 check_string(cd
->cookies
[1].path
, "/acme");
541 insist(cd
->cookies
[1].domain
== 0);
542 insist(!parse_cookie("$Version=\"1\";\n"
543 "Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\";\n"
544 "Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\";\n"
545 "Shipping=\"FedEx\"; $Path=\"/acme\"",
547 insist(cd
->ncookies
= 3);
548 insist(find_cookie(cd
, "Customer") == &cd
->cookies
[0]);
549 insist(find_cookie(cd
, "Part_Number") == &cd
->cookies
[1]);
550 insist(find_cookie(cd
, "Shipping") == &cd
->cookies
[2]);
551 check_string(cd
->cookies
[0].value
, "WILE_E_COYOTE");
552 check_string(cd
->cookies
[0].path
, "/acme");
553 insist(cd
->cookies
[0].domain
== 0);
554 check_string(cd
->cookies
[1].value
, "Rocket_Launcher_0001");
555 check_string(cd
->cookies
[1].path
, "/acme");
556 insist(cd
->cookies
[1].domain
== 0);
557 check_string(cd
->cookies
[2].value
, "FedEx");
558 check_string(cd
->cookies
[2].path
, "/acme");
559 insist(cd
->cookies
[2].domain
== 0);
562 static void test_hex(void) {
564 static const unsigned char h
[] = { 0x00, 0xFF, 0x80, 0x7F };
568 fprintf(stderr
, "test_hex\n");
570 for(n
= 0; n
<= UCHAR_MAX
; ++n
) {
572 insist(unhexdigitq(n
) == -1);
574 insist(unhexdigitq('0') == 0);
575 insist(unhexdigitq('1') == 1);
576 insist(unhexdigitq('2') == 2);
577 insist(unhexdigitq('3') == 3);
578 insist(unhexdigitq('4') == 4);
579 insist(unhexdigitq('5') == 5);
580 insist(unhexdigitq('6') == 6);
581 insist(unhexdigitq('7') == 7);
582 insist(unhexdigitq('8') == 8);
583 insist(unhexdigitq('9') == 9);
584 insist(unhexdigitq('a') == 10);
585 insist(unhexdigitq('b') == 11);
586 insist(unhexdigitq('c') == 12);
587 insist(unhexdigitq('d') == 13);
588 insist(unhexdigitq('e') == 14);
589 insist(unhexdigitq('f') == 15);
590 insist(unhexdigitq('A') == 10);
591 insist(unhexdigitq('B') == 11);
592 insist(unhexdigitq('C') == 12);
593 insist(unhexdigitq('D') == 13);
594 insist(unhexdigitq('E') == 14);
595 insist(unhexdigitq('F') == 15);
596 check_string(hex(h
, sizeof h
), "00ff807f");
597 check_string(hex(0, 0), "");
598 u
= unhex("00ff807f", &ul
);
600 insist(memcmp(u
, h
, 4) == 0);
601 u
= unhex("00FF807F", &ul
);
603 insist(memcmp(u
, h
, 4) == 0);
606 fprintf(stderr
, "2 ERROR reports expected {\n");
607 insist(unhex("F", 0) == 0);
608 insist(unhex("az", 0) == 0);
609 fprintf(stderr
, "}\n");
612 static void test_casefold(void) {
614 const char *input
, *canon_folded
, *compat_folded
, *canon_expected
, *compat_expected
;
616 fprintf(stderr
, "test_casefold\n");
618 /* This isn't a very exhaustive test. Unlike for normalization, there don't
619 * seem to be any public test vectors for these algorithms. */
621 for(c
= 1; c
< 256; ++c
) {
622 input
= utf32_to_utf8(&c
, 1, 0);
623 canon_folded
= utf8_casefold_canon(input
, strlen(input
), 0);
624 compat_folded
= utf8_casefold_compat(input
, strlen(input
), 0);
627 if((c
>= 'A' && c
<= 'Z')
628 || (c
>= 0xC0 && c
<= 0xDE && c
!= 0xD7))
633 case 0xB5: /* MICRO SIGN */
634 l
= 0x3BC; /* GREEK SMALL LETTER MU */
636 case 0xDF: /* LATIN SMALL LETTER SHARP S */
637 check_string(canon_folded
, "ss");
638 check_string(compat_folded
, "ss");
644 /* Case-folded data is now normalized */
645 d
= utf32_decompose_canon(&l
, 1, 0);
646 canon_expected
= utf32_to_utf8(d
, utf32_len(d
), 0);
647 if(strcmp(canon_folded
, canon_expected
)) {
648 fprintf(stderr
, "%s:%d: canon-casefolding %#lx got '%s', expected '%s'\n",
649 __FILE__
, __LINE__
, (unsigned long)c
,
650 format(canon_folded
), format(canon_expected
));
654 d
= utf32_decompose_compat(&l
, 1, 0);
655 compat_expected
= utf32_to_utf8(d
, utf32_len(d
), 0);
656 if(strcmp(compat_folded
, compat_expected
)) {
657 fprintf(stderr
, "%s:%d: compat-casefolding %#lx got '%s', expected '%s'\n",
658 __FILE__
, __LINE__
, (unsigned long)c
,
659 format(compat_folded
), format(compat_expected
));
665 check_string(utf8_casefold_canon("", 0, 0), "");
670 const char *expect
[10];
674 /* Only whitespace and punctuation */
681 { "wibble", { "wibble", 0 } },
682 { " wibble", { "wibble", 0 } },
683 { " wibble ", { "wibble", 0 } },
684 { "wibble ", { "wibble", 0 } },
685 { "wibble spong", { "wibble", "spong", 0 } },
686 { " wibble spong", { "wibble", "spong", 0 } },
687 { " wibble spong ", { "wibble", "spong", 0 } },
688 { "wibble spong ", { "wibble", "spong", 0 } },
689 { "wibble spong splat foo zot ", { "wibble", "spong", "splat", "foo", "zot", 0 } },
691 { "wibble 'spong", { "wibble", "spong", 0 } },
692 { " wibble's", { "wibble's", 0 } },
693 { " wibblespong' ", { "wibblespong", 0 } },
694 { "wibble sp''ong ", { "wibble", "sp", "ong", 0 } },
696 #define NWTEST (sizeof wtest / sizeof *wtest)
698 static void test_words(void) {
699 size_t t
, nexpect
, ngot
, i
;
702 fprintf(stderr
, "test_words\n");
703 for(t
= 0; t
< NWTEST
; ++t
) {
704 char **got
= utf8_word_split(wtest
[t
].in
, strlen(wtest
[t
].in
), &ngot
, 0);
706 for(nexpect
= 0; wtest
[t
].expect
[nexpect
]; ++nexpect
)
708 if(nexpect
== ngot
) {
709 for(i
= 0; i
< ngot
; ++i
)
710 if(strcmp(wtest
[t
].expect
[i
], got
[i
]))
716 fprintf(stderr
, "word split %zu failed\n", t
);
717 fprintf(stderr
, "input: %s\n", wtest
[t
].in
);
718 fprintf(stderr
, " | %-30s | %-30s\n",
720 for(i
= 0; i
< nexpect
|| i
< ngot
; ++i
) {
721 const char *e
= i
< nexpect ? wtest
[t
].expect
[i
] : "<none>";
722 const char *g
= i
< ngot ? got
[i
] : "<none>";
723 fprintf(stderr
, " %2zu | %-30s | %-30s\n", i
, e
, g
);
731 /** @brief Less-than comparison function for integer heap */
732 static inline int int_lt(int a
, int b
) { return a
< b
; }
735 * @brief A heap with @c int elements */
736 HEAP_TYPE(iheap
, int, int_lt
);
737 HEAP_DEFINE(iheap
, int, int_lt
);
739 /** @brief Tests for @ref heap.h */
740 static void test_heap(void) {
745 fprintf(stderr
, "test_heap\n");
748 for(n
= 0; n
< 1000; ++n
)
749 iheap_insert(h
, random() % 100);
750 for(n
= 0; n
< 1000; ++n
) {
751 const int latest
= iheap_remove(h
);
753 fprintf(stderr
, "should have %d <= %d\n", last
, latest
);
754 insist(last
<= latest
);
760 /** @brief Open a Unicode test file */
761 static FILE *open_unicode_test(const char *path
) {
767 if((base
= strrchr(path
, '/')))
771 if(!(fp
= fopen(base
, "r"))) {
772 snprintf(buffer
, sizeof buffer
,
773 "wget http://www.unicode.org/Public/5.0.0/ucd/%s", path
);
774 if((w
= system(buffer
)))
775 fatal(0, "%s: %s", buffer
, wstat(w
));
776 if(chmod(base
, 0444) < 0)
777 fatal(errno
, "chmod %s", base
);
778 if(!(fp
= fopen(base
, "r")))
779 fatal(errno
, "%s", base
);
784 /** @brief Run breaking tests for utf32_grapheme_boundary() etc */
785 static void breaktest(const char *path
,
786 int (*breakfn
)(const uint32_t *, size_t, size_t)) {
787 FILE *fp
= open_unicode_test(path
);
791 char break_allowed
[1024];
792 uint32_t buffer
[1024];
794 while(!inputline(path
, fp
, &l
, '\n')) {
796 if(l
[0] == '#') continue;
800 if(*lp
== ' ' || *lp
== '\t') {
806 if((unsigned char)*lp
== 0xC3 && (unsigned char)lp
[1] == 0xB7) {
807 /* 00F7 DIVISION SIGN */
808 break_allowed
[bn
] = 1;
812 if((unsigned char)*lp
== 0xC3 && (unsigned char)lp
[1] == 0x97) {
813 /* 00D7 MULTIPLICATION SIGN */
814 break_allowed
[bn
] = 0;
818 if(isxdigit((unsigned char)*lp
)) {
819 buffer
[bn
++] = strtoul(lp
, &lp
, 16);
822 fatal(0, "%s:%d: evil line: %s", path
, lineno
, l
);
824 for(n
= 0; n
<= bn
; ++n
) {
825 if(breakfn(buffer
, bn
, n
) != break_allowed
[n
]) {
827 "%s:%d: offset %zu: mismatch\n"
840 /** @brief Tests for @ref lib/unicode.h */
841 static void test_unicode(void) {
845 uint32_t buffer
[1024];
846 uint32_t *c
[6], *NFD_c
[6], *NFKD_c
[6], *NFC_c
[6], *NFKC_c
[6]; /* 1-indexed */
849 fprintf(stderr
, "test_unicode\n");
850 fp
= open_unicode_test("NormalizationTest.txt");
851 while(!inputline("NormalizationTest.txt", fp
, &l
, '\n')) {
853 if(*l
== '#' || *l
== '@')
858 c
[cn
++] = &buffer
[bn
];
859 while(*lp
&& *lp
!= '#') {
868 c
[cn
++] = &buffer
[bn
];
872 buffer
[bn
++] = strtoul(lp
, &lp
, 16);
876 for(cn
= 1; cn
<= 5; ++cn
) {
877 NFD_c
[cn
] = utf32_decompose_canon(c
[cn
], utf32_len(c
[cn
]), 0);
878 NFKD_c
[cn
] = utf32_decompose_compat(c
[cn
], utf32_len(c
[cn
]), 0);
879 NFC_c
[cn
] = utf32_compose_canon(c
[cn
], utf32_len(c
[cn
]), 0);
880 NFKC_c
[cn
] = utf32_compose_compat(c
[cn
], utf32_len(c
[cn
]), 0);
882 #define unt_check(T, A, B) do { \
884 if(utf32_cmp(c[A], T##_c[B])) { \
886 "NormalizationTest.txt:%d: c%d != "#T"(c%d)\n", \
888 fprintf(stderr, " c%d:%s\n", \
889 A, format_utf32(c[A])); \
890 fprintf(stderr, " c%d:%s\n", \
891 B, format_utf32(c[B])); \
892 fprintf(stderr, "%4s(c%d):%s\n", \
893 #T, B, format_utf32(T##_c[B])); \
897 unt_check(NFD
, 3, 1);
898 unt_check(NFD
, 3, 2);
899 unt_check(NFD
, 3, 3);
900 unt_check(NFD
, 5, 4);
901 unt_check(NFD
, 5, 5);
902 unt_check(NFKD
, 5, 1);
903 unt_check(NFKD
, 5, 2);
904 unt_check(NFKD
, 5, 3);
905 unt_check(NFKD
, 5, 4);
906 unt_check(NFKD
, 5, 5);
907 unt_check(NFC
, 2, 1);
908 unt_check(NFC
, 2, 2);
909 unt_check(NFC
, 2, 3);
910 unt_check(NFC
, 4, 4);
911 unt_check(NFC
, 4, 5);
912 unt_check(NFKC
, 4, 1);
913 unt_check(NFKC
, 4, 2);
914 unt_check(NFKC
, 4, 3);
915 unt_check(NFKC
, 4, 4);
916 unt_check(NFKC
, 4, 5);
917 for(cn
= 1; cn
<= 5; ++cn
) {
924 breaktest("auxiliary/GraphemeBreakTest.txt", utf32_is_grapheme_boundary
);
925 breaktest("auxiliary/WordBreakTest.txt", utf32_is_word_boundary
);
926 insist(utf32_combining_class(0x40000) == 0);
927 insist(utf32_combining_class(0xE0000) == 0);
930 static void test_signame(void) {
931 fprintf(stderr
, "test_signame\n");
932 insist(find_signal("SIGTERM") == SIGTERM
);
933 insist(find_signal("SIGHUP") == SIGHUP
);
934 insist(find_signal("SIGINT") == SIGINT
);
935 insist(find_signal("SIGQUIT") == SIGQUIT
);
936 insist(find_signal("SIGKILL") == SIGKILL
);
937 insist(find_signal("SIGYOURMUM") == -1);
940 static void test_cache(void) {
941 const struct cache_type t1
= { 1 }, t2
= { 10 };
942 const char v11
[] = "spong", v12
[] = "wibble", v2
[] = "blat";
943 fprintf(stderr
, "test_cache\n");
944 cache_put(&t1
, "1_1", v11
);
945 cache_put(&t1
, "1_2", v12
);
946 cache_put(&t2
, "2", v2
);
947 insist(cache_count() == 3);
948 insist(cache_get(&t2
, "2") == v2
);
949 insist(cache_get(&t1
, "1_1") == v11
);
950 insist(cache_get(&t1
, "1_2") == v12
);
951 insist(cache_get(&t1
, "2") == 0);
952 insist(cache_get(&t2
, "1_1") == 0);
953 insist(cache_get(&t2
, "1_2") == 0);
954 insist(cache_get(&t1
, "2") == 0);
955 insist(cache_get(&t2
, "1_1") == 0);
956 insist(cache_get(&t2
, "1_2") == 0);
959 insist(cache_count() == 1);
960 insist(cache_get(&t1
, "1_1") == 0);
961 insist(cache_get(&t1
, "1_2") == 0);
962 insist(cache_get(&t2
, "2") == v2
);
964 insist(cache_count() == 0);
965 insist(cache_get(&t2
, "2") == 0);
968 static void test_filepart(void) {
969 fprintf(stderr
, "test_filepart\n");
970 check_string(d_dirname("/"), "/");
971 check_string(d_dirname("////"), "/");
972 check_string(d_dirname("/spong"), "/");
973 check_string(d_dirname("////spong"), "/");
974 check_string(d_dirname("/foo/bar"), "/foo");
975 check_string(d_dirname("////foo/////bar"), "////foo");
976 check_string(d_dirname("./bar"), ".");
977 check_string(d_dirname(".//bar"), ".");
978 check_string(d_dirname("."), ".");
979 check_string(d_dirname(".."), ".");
980 check_string(d_dirname("../blat"), "..");
981 check_string(d_dirname("..//blat"), "..");
982 check_string(d_dirname("wibble"), ".");
983 check_string(extension("foo.c"), ".c");
984 check_string(extension(".c"), ".c");
985 check_string(extension("."), ".");
986 check_string(extension("foo"), "");
987 check_string(extension("./foo"), "");
988 check_string(extension("./foo.c"), ".c");
989 check_string(strip_extension("foo.c"), "foo");
990 check_string(strip_extension("foo.mp3"), "foo");
991 check_string(strip_extension("foo.---"), "foo.---");
992 check_string(strip_extension("foo.---xyz"), "foo.---xyz");
993 check_string(strip_extension("foo.bar/wibble.spong"), "foo.bar/wibble");
996 static void test_selection(void) {
998 fprintf(stderr
, "test_selection\n");
999 insist((h
= selection_new()) != 0);
1000 selection_set(h
, "one", 1);
1001 selection_set(h
, "two", 1);
1002 selection_set(h
, "three", 0);
1003 selection_set(h
, "four", 1);
1004 insist(selection_selected(h
, "one") == 1);
1005 insist(selection_selected(h
, "two") == 1);
1006 insist(selection_selected(h
, "three") == 0);
1007 insist(selection_selected(h
, "four") == 1);
1008 insist(selection_selected(h
, "five") == 0);
1009 insist(hash_count(h
) == 3);
1010 selection_flip(h
, "one");
1011 selection_flip(h
, "three");
1012 insist(selection_selected(h
, "one") == 0);
1013 insist(selection_selected(h
, "three") == 1);
1014 insist(hash_count(h
) == 3);
1015 selection_live(h
, "one");
1016 selection_live(h
, "two");
1017 selection_live(h
, "three");
1018 selection_cleanup(h
);
1019 insist(selection_selected(h
, "one") == 0);
1020 insist(selection_selected(h
, "two") == 1);
1021 insist(selection_selected(h
, "three") == 1);
1022 insist(selection_selected(h
, "four") == 0);
1023 insist(selection_selected(h
, "five") == 0);
1024 insist(hash_count(h
) == 2);
1026 insist(selection_selected(h
, "one") == 0);
1027 insist(selection_selected(h
, "two") == 0);
1028 insist(selection_selected(h
, "three") == 0);
1029 insist(selection_selected(h
, "four") == 0);
1030 insist(selection_selected(h
, "five") == 0);
1031 insist(hash_count(h
) == 0);
1034 static void test_wstat(void) {
1038 fprintf(stderr
, "test_wstat\n");
1039 if(!(pid
= xfork())) {
1042 while(waitpid(pid
, &w
, 0) < 0 && errno
== EINTR
)
1044 check_string(wstat(w
), "exited with status 1");
1045 if(!(pid
= xfork())) {
1046 kill(getpid(), SIGTERM
);
1049 while(waitpid(pid
, &w
, 0) < 0 && errno
== EINTR
)
1051 check_string_prefix(wstat(w
), "terminated by signal 15");
1054 static void test_kvp(void) {
1058 fprintf(stderr
, "test_kvp\n");
1060 #define KVP_URLDECODE(S) kvp_urldecode((S), strlen(S))
1061 insist(KVP_URLDECODE("=%zz") == 0);
1062 insist(KVP_URLDECODE("=%0") == 0);
1063 insist(KVP_URLDECODE("=%0z") == 0);
1064 insist(KVP_URLDECODE("=%%") == 0);
1065 insist(KVP_URLDECODE("==%") == 0);
1066 insist(KVP_URLDECODE("wibble") == 0);
1067 insist(KVP_URLDECODE("") == 0);
1068 insist(KVP_URLDECODE("wibble&") == 0);
1069 insist((k
= KVP_URLDECODE("one=bl%61t+foo")) != 0);
1070 check_string(kvp_get(k
, "one"), "blat foo");
1071 insist(kvp_get(k
, "ONE") == 0);
1072 insist(k
->next
== 0);
1073 insist((k
= KVP_URLDECODE("wibble=splat&bar=spong")) != 0);
1074 check_string(kvp_get(k
, "wibble"), "splat");
1075 check_string(kvp_get(k
, "bar"), "spong");
1076 insist(kvp_get(k
, "ONE") == 0);
1077 insist(k
->next
->next
== 0);
1079 insist(kvp_set(&k
, "bar", "spong") == 0);
1080 insist(kvp_set(&k
, "bar", "foo") == 1);
1081 insist(kvp_set(&k
, "zog", "%") == 1);
1082 insist(kvp_set(&k
, "wibble", 0) == 1);
1083 insist(kvp_set(&k
, "wibble", 0) == 0);
1084 check_string(kvp_urlencode(k
, 0),
1086 check_string(kvp_urlencode(k
, &n
),
1088 insist(n
== strlen("bar=foo&zog=%25"));
1089 check_string(urlencodestring("abc% +\n"),
1093 static void test_sink(void) {
1099 fprintf(stderr
, "test_sink\n");
1103 s
= sink_stdio("tmpfile", fp
);
1104 insist(sink_printf(s
, "test: %d\n", 999) == 10);
1105 insist(sink_printf(s
, "wibble: %s\n", "foobar") == 15);
1107 insist(inputline("tmpfile", fp
, &l
, '\n') == 0);
1108 check_string(l
, "test: 999");
1109 insist(inputline("tmpfile", fp
, &l
, '\n') == 0);
1110 check_string(l
, "wibble: foobar");
1111 insist(inputline("tmpfile", fp
, &l
, '\n') == -1);
1115 insist(sink_printf(s
, "test: %d\n", 999) == 10);
1116 insist(sink_printf(s
, "wibble: %s\n", "foobar") == 15);
1117 dynstr_terminate(d
);
1118 check_string(d
->vec
, "test: 999\nwibble: foobar\n");
1121 static const char *do_printf(const char *fmt
, ...) {
1127 rc
= byte_vasprintf(&s
, fmt
, ap
);
1134 static void test_printf(void) {
1146 fprintf(stderr
, "test_printf\n");
1147 check_string(do_printf("%d", 999), "999");
1148 check_string(do_printf("%d", -999), "-999");
1149 check_string(do_printf("%i", 999), "999");
1150 check_string(do_printf("%i", -999), "-999");
1151 check_string(do_printf("%u", 999), "999");
1152 check_string(do_printf("%2u", 999), "999");
1153 check_string(do_printf("%10u", 999), " 999");
1154 check_string(do_printf("%-10u", 999), "999 ");
1155 check_string(do_printf("%010u", 999), "0000000999");
1156 check_string(do_printf("%-10d", -999), "-999 ");
1157 check_string(do_printf("%-010d", -999), "-999 "); /* "-" beats "0" */
1158 check_string(do_printf("%66u", 999), " 999");
1159 check_string(do_printf("%o", 999), "1747");
1160 check_string(do_printf("%#o", 999), "01747");
1161 check_string(do_printf("%#o", 0), "0");
1162 check_string(do_printf("%x", 999), "3e7");
1163 check_string(do_printf("%#x", 999), "0x3e7");
1164 check_string(do_printf("%#X", 999), "0X3E7");
1165 check_string(do_printf("%#x", 0), "0");
1166 check_string(do_printf("%hd", (short)999), "999");
1167 check_string(do_printf("%hhd", (short)99), "99");
1168 check_string(do_printf("%ld", 100000L), "100000");
1169 check_string(do_printf("%lld", 10000000000LL), "10000000000");
1170 check_string(do_printf("%qd", 10000000000LL), "10000000000");
1171 check_string(do_printf("%jd", (intmax_t)10000000000LL), "10000000000");
1172 check_string(do_printf("%zd", (ssize_t
)2000000000), "2000000000");
1173 check_string(do_printf("%td", (ptrdiff_t)2000000000), "2000000000");
1174 check_string(do_printf("%hu", (short)999), "999");
1175 check_string(do_printf("%hhu", (short)99), "99");
1176 check_string(do_printf("%lu", 100000L), "100000");
1177 check_string(do_printf("%llu", 10000000000LL), "10000000000");
1178 check_string(do_printf("%ju", (uintmax_t)10000000000LL), "10000000000");
1179 check_string(do_printf("%zu", (size_t)2000000000), "2000000000");
1180 check_string(do_printf("%tu", (ptrdiff_t)2000000000), "2000000000");
1181 check_string(do_printf("%p", (void *)0x100), "0x100");
1182 check_string(do_printf("%s", "wibble"), "wibble");
1183 check_string(do_printf("%s-%s", "wibble", "wobble"), "wibble-wobble");
1184 check_string(do_printf("%10s", "wibble"), " wibble");
1185 check_string(do_printf("%010s", "wibble"), " wibble"); /* 0 ignored for %s */
1186 check_string(do_printf("%-10s", "wibble"), "wibble ");
1187 check_string(do_printf("%2s", "wibble"), "wibble");
1188 check_string(do_printf("%.2s", "wibble"), "wi");
1189 check_string(do_printf("%.2s", "w"), "w");
1190 check_string(do_printf("%4.2s", "wibble"), " wi");
1191 check_string(do_printf("%c", 'a'), "a");
1192 check_string(do_printf("%4c", 'a'), " a");
1193 check_string(do_printf("%-4c", 'a'), "a ");
1194 check_string(do_printf("%*c", 0, 'a'), "a");
1195 check_string(do_printf("x%hhny", &c
), "xy");
1197 check_string(do_printf("xx%hnyy", &s
), "xxyy");
1199 check_string(do_printf("xxx%nyyy", &i
), "xxxyyy");
1201 check_string(do_printf("xxxx%lnyyyy", &l
), "xxxxyyyy");
1203 check_string(do_printf("xxxxx%llnyyyyy", &ll
), "xxxxxyyyyy");
1205 check_string(do_printf("xxxxxx%jnyyyyyy", &m
), "xxxxxxyyyyyy");
1207 check_string(do_printf("xxxxxxx%znyyyyyyy", &ssz
), "xxxxxxxyyyyyyy");
1209 check_string(do_printf("xxxxxxxx%tnyyyyyyyy", &p
), "xxxxxxxxyyyyyyyy");
1211 check_string(do_printf("%*d", 5, 99), " 99");
1212 check_string(do_printf("%*d", -5, 99), "99 ");
1213 check_string(do_printf("%.*d", 5, 99), "00099");
1214 check_string(do_printf("%.*d", -5, 99), "99");
1215 check_string(do_printf("%.0d", 0), "");
1216 check_string(do_printf("%.d", 0), "");
1217 check_string(do_printf("%.d", 0), "");
1218 check_string(do_printf("%%"), "%");
1219 check_string(do_printf("wibble"), "wibble");
1220 insist(do_printf("%") == 0);
1221 insist(do_printf("%=") == 0);
1222 i
= byte_asprintf(&cp
, "xyzzy %d", 999);
1224 check_string(cp
, "xyzzy 999");
1225 i
= byte_snprintf(buffer
, sizeof buffer
, "xyzzy %d", 999);
1227 check_string(buffer
, "xyzzy 999");
1228 i
= byte_snprintf(buffer
, sizeof buffer
, "%*d", 32, 99);
1230 check_string(buffer
, " ");
1232 /* bizarre workaround for compiler checking of format strings */
1233 char f
[] = "xyzzy %";
1234 i
= byte_asprintf(&cp
, f
);
1239 static void test_basen(void) {
1240 unsigned long v
[64];
1243 fprintf(stderr
, "test_basen\n");
1245 insist(basen(v
, 1, buffer
, sizeof buffer
, 10) == 0);
1246 check_string(buffer
, "999");
1248 v
[0] = 1+2*7+3*7*7+4*7*7*7;
1249 insist(basen(v
, 1, buffer
, sizeof buffer
, 7) == 0);
1250 check_string(buffer
, "4321");
1256 insist(basen(v
, 4, buffer
, sizeof buffer
, 256) == 0);
1257 check_string(buffer
, "123456789abcdef");
1263 insist(basen(v
, 4, buffer
, sizeof buffer
, 16) == 0);
1264 check_string(buffer
, "102030405060708090a0b0c0d0e0f");
1270 insist(basen(v
, 4, buffer
, 10, 16) == -1);
1273 static void test_split(void) {
1277 fprintf(stderr
, "test_split\n");
1278 insist(split("\"misquoted", &nv
, SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0) == 0);
1279 insist(split("\'misquoted", &nv
, SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0) == 0);
1280 insist(split("\'misquoted\\", &nv
, SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0) == 0);
1281 insist(split("\'misquoted\\\"", &nv
, SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0) == 0);
1282 insist(split("\'mis\\escaped\'", &nv
, SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0) == 0);
1284 insist((v
= split("", &nv
, SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0)));
1285 check_integer(nv
, 0);
1288 insist((v
= split("wibble", &nv
, SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0)));
1289 check_integer(nv
, 1);
1290 check_string(v
[0], "wibble");
1293 insist((v
= split(" wibble \t\r\n wobble ", &nv
,
1294 SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0)));
1295 check_integer(nv
, 2);
1296 check_string(v
[0], "wibble");
1297 check_string(v
[1], "wobble");
1300 insist((v
= split("wibble wobble #splat", &nv
,
1301 SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0)));
1302 check_integer(nv
, 2);
1303 check_string(v
[0], "wibble");
1304 check_string(v
[1], "wobble");
1307 insist((v
= split("\"wibble wobble\" #splat", &nv
,
1308 SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0)));
1309 check_integer(nv
, 1);
1310 check_string(v
[0], "wibble wobble");
1313 insist((v
= split("\"wibble \\\"\\nwobble\"", &nv
,
1314 SPLIT_COMMENTS
|SPLIT_QUOTES
, 0, 0)));
1315 check_integer(nv
, 1);
1316 check_string(v
[0], "wibble \"\nwobble");
1319 insist((v
= split("\"wibble wobble\" #splat", &nv
,
1320 SPLIT_QUOTES
, 0, 0)));
1321 check_integer(nv
, 2);
1322 check_string(v
[0], "wibble wobble");
1323 check_string(v
[1], "#splat");
1326 insist((v
= split("\"wibble wobble\" #splat", &nv
,
1327 SPLIT_COMMENTS
, 0, 0)));
1328 check_integer(nv
, 2);
1329 check_string(v
[0], "\"wibble");
1330 check_string(v
[1], "wobble\"");
1333 check_string(quoteutf8("wibble"), "wibble");
1334 check_string(quoteutf8(" wibble "), "\" wibble \"");
1335 check_string(quoteutf8("wibble wobble"), "\"wibble wobble\"");
1336 check_string(quoteutf8("wibble\"wobble"), "\"wibble\\\"wobble\"");
1337 check_string(quoteutf8("wibble\nwobble"), "\"wibble\\nwobble\"");
1338 check_string(quoteutf8("wibble\\wobble"), "\"wibble\\\\wobble\"");
1339 check_string(quoteutf8("wibble'wobble"), "\"wibble'wobble\"");
1342 static void test_hash(void) {
1347 fprintf(stderr
, "test_hash\n");
1348 h
= hash_new(sizeof(int));
1349 for(i
= 0; i
< 10000; ++i
)
1350 insist(hash_add(h
, do_printf("%d", i
), &i
, HASH_INSERT
) == 0);
1351 check_integer(hash_count(h
), 10000);
1352 for(i
= 0; i
< 10000; ++i
) {
1353 insist((ip
= hash_find(h
, do_printf("%d", i
))) != 0);
1354 check_integer(*ip
, i
);
1355 insist(hash_add(h
, do_printf("%d", i
), &i
, HASH_REPLACE
) == 0);
1357 check_integer(hash_count(h
), 10000);
1358 keys
= hash_keys(h
);
1359 for(i
= 0; i
< 10000; ++i
)
1360 insist(keys
[i
] != 0);
1361 insist(keys
[10000] == 0);
1362 for(i
= 0; i
< 10000; ++i
)
1363 insist(hash_remove(h
, do_printf("%d", i
)) == 0);
1364 check_integer(hash_count(h
), 0);
1367 static void test_addr(void) {
1368 struct stringlist a
;
1370 struct addrinfo
*ai
;
1372 const struct sockaddr_in
*sin
;
1374 static const struct addrinfo pref
= {
1385 printf("test_addr\n");
1390 ai
= get_address(&a
, &pref
, &name
);
1392 check_integer(ai
->ai_family
, PF_INET
);
1393 check_integer(ai
->ai_socktype
, SOCK_STREAM
);
1394 check_integer(ai
->ai_protocol
, IPPROTO_TCP
);
1395 check_integer(ai
->ai_addrlen
, sizeof(struct sockaddr_in
));
1396 sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1397 check_integer(sin
->sin_family
, AF_INET
);
1398 check_integer(sin
->sin_addr
.s_addr
, 0);
1399 check_integer(ntohs(sin
->sin_port
), 25);
1400 check_string(name
, "host * service smtp");
1405 ai
= get_address(&a
, &pref
, &name
);
1407 check_integer(ai
->ai_family
, PF_INET
);
1408 check_integer(ai
->ai_socktype
, SOCK_STREAM
);
1409 check_integer(ai
->ai_protocol
, IPPROTO_TCP
);
1410 check_integer(ai
->ai_addrlen
, sizeof(struct sockaddr_in
));
1411 sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1412 check_integer(sin
->sin_family
, AF_INET
);
1413 check_integer(ntohl(sin
->sin_addr
.s_addr
), 0x7F000001);
1414 check_integer(ntohs(sin
->sin_port
), 119);
1415 check_string(name
, "host localhost service nntp");
1418 static void test_url(void) {
1421 printf("test_url\n");
1423 insist(parse_url("http://www.example.com/example/path", &p
) == 0);
1424 check_string(p
.scheme
, "http");
1425 check_string(p
.host
, "www.example.com");
1426 insist(p
.port
== -1);
1427 check_string(p
.path
, "/example/path");
1428 insist(p
.query
== 0);
1430 insist(parse_url("https://www.example.com:82/example%2fpath?+query+", &p
) == 0);
1431 check_string(p
.scheme
, "https");
1432 check_string(p
.host
, "www.example.com");
1433 insist(p
.port
== 82);
1434 check_string(p
.path
, "/example/path");
1435 check_string(p
.query
, "+query+");
1440 fail_first
= !!getenv("FAIL_FIRST");
1441 insist('\n' == 0x0A);
1442 insist('\r' == 0x0D);
1443 insist(' ' == 0x20);
1444 insist('0' == 0x30);
1445 insist('9' == 0x39);
1446 insist('A' == 0x41);
1447 insist('Z' == 0x5A);
1448 insist('a' == 0x61);
1449 insist('z' == 0x7A);
1458 /* configuration.c */
1505 fprintf(stderr
, "%d errors out of %d tests\n", errors
, tests
);
1514 indent-tabs-mode:nil