chiark / gitweb /
merge extra MIME parsing
[disorder] / lib / test.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
033fd4e3 3 * Copyright (C) 2005, 2007 Richard Kettlewell
460b9539 4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
033fd4e3 20/** @file lib/test.c @brief Library tests */
460b9539 21
22#include <config.h>
23#include "types.h"
24
25#include <stdio.h>
26#include <string.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <ctype.h>
033fd4e3 30#include <assert.h>
e5a5a138
RK
31#include <sys/types.h>
32#include <sys/stat.h>
9f28e855 33#include <unistd.h>
0cd21fc4 34#include <signal.h>
6f4d1e2d 35#include <sys/wait.h>
0c740e59 36#include <stddef.h>
d6ea854a
RK
37#include <sys/socket.h>
38#include <netdb.h>
39#include <netinet/in.h>
460b9539 40
460b9539 41#include "mem.h"
42#include "log.h"
43#include "vector.h"
44#include "charset.h"
45#include "mime.h"
46#include "hex.h"
033fd4e3 47#include "heap.h"
e5a5a138
RK
48#include "unicode.h"
49#include "inputline.h"
e2452add 50#include "wstat.h"
ea387d53 51#include "signame.h"
9f28e855 52#include "cache.h"
00e36cd0 53#include "filepart.h"
65bb0fff
RK
54#include "hash.h"
55#include "selection.h"
71b90230 56#include "syscalls.h"
22f61603 57#include "kvp.h"
6049fe0e 58#include "sink.h"
0c740e59 59#include "printf.h"
abe28bfe 60#include "basen.h"
5e49fa7f 61#include "split.h"
d6ea854a
RK
62#include "configuration.h"
63#include "addr.h"
460b9539 64
65static int tests, errors;
bb48024f
RK
66static int fail_first;
67
68static void count_error() {
69 ++errors;
70 if(fail_first)
71 abort();
72}
460b9539 73
033fd4e3 74/** @brief Checks that @p expr is nonzero */
460b9539 75#define insist(expr) do { \
033fd4e3 76 if(!(expr)) { \
bb48024f 77 count_error(); \
460b9539 78 fprintf(stderr, "%s:%d: error checking %s\n", \
79 __FILE__, __LINE__, #expr); \
80 } \
81 ++tests; \
82} while(0)
83
84static const char *format(const char *s) {
85 struct dynstr d;
86 int c;
87 char buf[10];
88
89 dynstr_init(&d);
90 while((c = (unsigned char)*s++)) {
91 if(c >= ' ' && c <= '~')
92 dynstr_append(&d, c);
93 else {
94 sprintf(buf, "\\x%02X", (unsigned)c);
95 dynstr_append_string(&d, buf);
96 }
97 }
98 dynstr_terminate(&d);
99 return d.vec;
100}
101
e5a5a138
RK
102static const char *format_utf32(const uint32_t *s) {
103 struct dynstr d;
104 uint32_t c;
105 char buf[64];
106
107 dynstr_init(&d);
108 while((c = *s++)) {
16506c9d
RK
109 sprintf(buf, " %04lX", (long)c);
110 dynstr_append_string(&d, buf);
e5a5a138
RK
111 }
112 dynstr_terminate(&d);
113 return d.vec;
114}
115
6f4d1e2d
RK
116#define check_string(GOT, WANT) do { \
117 const char *got = GOT; \
118 const char *want = WANT; \
119 \
120 if(want == 0) { \
121 fprintf(stderr, "%s:%d: %s returned 0\n", \
122 __FILE__, __LINE__, #GOT); \
123 count_error(); \
124 } else if(strcmp(want, got)) { \
125 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \
126 __FILE__, __LINE__, #GOT, format(got), format(want)); \
127 count_error(); \
128 } \
129 ++tests; \
460b9539 130 } while(0)
131
71b90230 132#define check_string_prefix(GOT, WANT) do { \
6f4d1e2d
RK
133 const char *got = GOT; \
134 const char *want = WANT; \
71b90230 135 \
6f4d1e2d 136 if(want == 0) { \
71b90230
RK
137 fprintf(stderr, "%s:%d: %s returned 0\n", \
138 __FILE__, __LINE__, #GOT); \
6f4d1e2d
RK
139 count_error(); \
140 } else if(strncmp(want, got, strlen(want))) { \
141 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n", \
142 __FILE__, __LINE__, #GOT, format(got), format(want)); \
143 count_error(); \
71b90230
RK
144 } \
145 ++tests; \
146 } while(0)
147
22896b25
RK
148#define check_integer(GOT, WANT) do { \
149 const intmax_t got = GOT, want = WANT; \
150 if(got != want) { \
151 fprintf(stderr, "%s:%d: %s returned: %jd expected: %jd\n", \
152 __FILE__, __LINE__, #GOT, got, want); \
153 count_error(); \
154 } \
155 ++tests; \
156} while(0)
157
460b9539 158static uint32_t *ucs4parse(const char *s) {
159 struct dynstr_ucs4 d;
160 char *e;
161
162 dynstr_ucs4_init(&d);
163 while(*s) {
164 errno = 0;
165 dynstr_ucs4_append(&d, strtoul(s, &e, 0));
166 if(errno) fatal(errno, "strtoul (%s)", s);
167 s = e;
168 }
169 dynstr_ucs4_terminate(&d);
170 return d.vec;
171}
172
173static void test_utf8(void) {
174 /* Test validutf8, convert to UCS-4, check the answer is right,
175 * convert back to UTF-8, check we got to where we started */
176#define U8(CHARS, WORDS) do { \
177 uint32_t *w = ucs4parse(WORDS); \
178 uint32_t *ucs; \
179 char *u8; \
180 \
181 insist(validutf8(CHARS)); \
caecd4f4 182 ucs = utf8_to_utf32(CHARS, strlen(CHARS), 0); \
460b9539 183 insist(ucs != 0); \
caecd4f4
RK
184 insist(!utf32_cmp(w, ucs)); \
185 u8 = utf32_to_utf8(ucs, utf32_len(ucs), 0); \
460b9539 186 insist(u8 != 0); \
22896b25 187 check_string(u8, CHARS); \
460b9539 188} while(0)
189
033fd4e3 190 fprintf(stderr, "test_utf8\n");
18cda350 191#define validutf8(S) utf8_valid((S), strlen(S))
033fd4e3 192
460b9539 193 /* empty string */
194
195 U8("", "");
196
197 /* ASCII characters */
198
199 U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
200 "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d "
201 "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a "
202 "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 "
203 "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 "
204 "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 "
205 "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e "
206 "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b "
207 "0x7c 0x7d 0x7e");
208 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",
209 "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 "
210 "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d "
211 "0x1e 0x1f 0x7f");
212
213 /* from RFC3629 */
214
215 /* UTF8-2 = %xC2-DF UTF8-tail */
216 insist(!validutf8("\xC0\x80"));
217 insist(!validutf8("\xC1\x80"));
218 insist(!validutf8("\xC2\x7F"));
219 U8("\xC2\x80", "0x80");
220 U8("\xDF\xBF", "0x7FF");
221 insist(!validutf8("\xDF\xC0"));
222
223 /* UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
224 * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
225 */
226 insist(!validutf8("\xE0\x9F\x80"));
227 U8("\xE0\xA0\x80", "0x800");
228 U8("\xE0\xBF\xBF", "0xFFF");
229 insist(!validutf8("\xE0\xC0\xBF"));
230
231 insist(!validutf8("\xE1\x80\x7F"));
232 U8("\xE1\x80\x80", "0x1000");
233 U8("\xEC\xBF\xBF", "0xCFFF");
234 insist(!validutf8("\xEC\xC0\xBF"));
235
236 U8("\xED\x80\x80", "0xD000");
237 U8("\xED\x9F\xBF", "0xD7FF");
238 insist(!validutf8("\xED\xA0\xBF"));
239
240 insist(!validutf8("\xEE\x7f\x80"));
241 U8("\xEE\x80\x80", "0xE000");
242 U8("\xEF\xBF\xBF", "0xFFFF");
243 insist(!validutf8("\xEF\xC0\xBF"));
244
245 /* UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
246 * %xF4 %x80-8F 2( UTF8-tail )
247 */
248 insist(!validutf8("\xF0\x8F\x80\x80"));
249 U8("\xF0\x90\x80\x80", "0x10000");
250 U8("\xF0\xBF\xBF\xBF", "0x3FFFF");
251 insist(!validutf8("\xF0\xC0\x80\x80"));
252
253 insist(!validutf8("\xF1\x80\x80\x7F"));
254 U8("\xF1\x80\x80\x80", "0x40000");
255 U8("\xF3\xBF\xBF\xBF", "0xFFFFF");
256 insist(!validutf8("\xF3\xC0\x80\x80"));
257
258 insist(!validutf8("\xF4\x80\x80\x7F"));
259 U8("\xF4\x80\x80\x80", "0x100000");
260 U8("\xF4\x8F\xBF\xBF", "0x10FFFF");
261 insist(!validutf8("\xF4\x90\x80\x80"));
c35e83d9 262 insist(!validutf8("\xF4\x80\xFF\x80"));
460b9539 263
264 /* miscellaneous non-UTF-8 rubbish */
265 insist(!validutf8("\x80"));
266 insist(!validutf8("\xBF"));
267 insist(!validutf8("\xC0"));
268 insist(!validutf8("\xC0\x7F"));
269 insist(!validutf8("\xC0\xC0"));
270 insist(!validutf8("\xE0"));
271 insist(!validutf8("\xE0\x7F"));
272 insist(!validutf8("\xE0\xC0"));
273 insist(!validutf8("\xE0\x80"));
274 insist(!validutf8("\xE0\x80\x7f"));
275 insist(!validutf8("\xE0\x80\xC0"));
276 insist(!validutf8("\xF0"));
277 insist(!validutf8("\xF0\x7F"));
278 insist(!validutf8("\xF0\xC0"));
279 insist(!validutf8("\xF0\x80"));
280 insist(!validutf8("\xF0\x80\x7f"));
281 insist(!validutf8("\xF0\x80\xC0"));
282 insist(!validutf8("\xF0\x80\x80\x7f"));
283 insist(!validutf8("\xF0\x80\x80\xC0"));
284 insist(!validutf8("\xF5\x80\x80\x80"));
285 insist(!validutf8("\xF8"));
286}
287
22896b25
RK
288static int test_multipart_callback(const char *s, void *u) {
289 struct vector *parts = u;
290
291 vector_append(parts, (char *)s);
292 return 0;
293}
294
460b9539 295static void test_mime(void) {
296 char *t, *n, *v;
22896b25 297 struct vector parts[1];
460b9539 298
033fd4e3
RK
299 fprintf(stderr, "test_mime\n");
300
460b9539 301 t = n = v = 0;
302 insist(!mime_content_type("text/plain", &t, &n, &v));
22896b25 303 check_string(t, "text/plain");
460b9539 304 insist(n == 0);
305 insist(v == 0);
306
22896b25
RK
307 insist(mime_content_type("TEXT ((broken) comment", &t, &n, &v) < 0);
308 insist(mime_content_type("TEXT ((broken) comment\\", &t, &n, &v) < 0);
309
460b9539 310 t = n = v = 0;
22896b25
RK
311 insist(!mime_content_type("TEXT ((nested)\\ comment) /plain", &t, &n, &v));
312 check_string(t, "text/plain");
460b9539 313 insist(n == 0);
314 insist(v == 0);
315
316 t = n = v = 0;
22896b25
RK
317 insist(!mime_content_type(" text/plain ; Charset=\"utf-\\8\"", &t, &n, &v));
318 check_string(t, "text/plain");
319 check_string(n, "charset");
320 check_string(v, "utf-8");
460b9539 321
322 t = n = v = 0;
323 insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t, &n, &v));
22896b25
RK
324 check_string(t, "text/plain");
325 check_string(n, "charset");
326 check_string(v, "ISO-8859-1");
327
328 t = n = v = 0;
329 insist(!mime_rfc2388_content_disposition("form-data; name=\"field1\"", &t, &n, &v));
330 check_string(t, "form-data");
331 check_string(n, "name");
332 check_string(v, "field1");
333
334 insist(!mime_rfc2388_content_disposition("inline", &t, &n, &v));
335 check_string(t, "inline");
336 insist(n == 0);
337 insist(v == 0);
460b9539 338
22896b25
RK
339 /* Current versions of the code only understand a single arg to these
340 * headers. This is a bug at the level they work at but suffices for
341 * DisOrder's current purposes. */
342
343 insist(!mime_rfc2388_content_disposition(
344 "attachment; filename=genome.jpeg;\n"
345 "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"",
346 &t, &n, &v));
347 check_string(t, "attachment");
348 check_string(n, "filename");
349 check_string(v, "genome.jpeg");
350
351 vector_init(parts);
352 insist(mime_multipart("--outer\r\n"
353 "Content-Type: text/plain\r\n"
354 "Content-Disposition: inline\r\n"
355 "Content-Description: text-part-1\r\n"
356 "\r\n"
357 "Some text goes here\r\n"
358 "\r\n"
359 "--outer\r\n"
360 "Content-Type: multipart/mixed; boundary=inner\r\n"
361 "Content-Disposition: attachment\r\n"
362 "Content-Description: multipart-2\r\n"
363 "\r\n"
364 "--inner\r\n"
365 "Content-Type: text/plain\r\n"
366 "Content-Disposition: inline\r\n"
367 "Content-Description: text-part-2\r\n"
368 "\r\n"
369 "Some more text here.\r\n"
370 "\r\n"
371 "--inner\r\n"
372 "Content-Type: image/jpeg\r\n"
373 "Content-Disposition: attachment\r\n"
374 "Content-Description: jpeg-1\r\n"
375 "\r\n"
376 "<jpeg data>\r\n"
377 "--inner--\r\n"
378 "--outer--\r\n",
379 test_multipart_callback,
380 "outer",
381 parts) == 0);
382 check_integer(parts->nvec, 2);
383 check_string(parts->vec[0],
384 "Content-Type: text/plain\r\n"
385 "Content-Disposition: inline\r\n"
386 "Content-Description: text-part-1\r\n"
387 "\r\n"
388 "Some text goes here\r\n");
389 check_string(parts->vec[1],
390 "Content-Type: multipart/mixed; boundary=inner\r\n"
391 "Content-Disposition: attachment\r\n"
392 "Content-Description: multipart-2\r\n"
393 "\r\n"
394 "--inner\r\n"
395 "Content-Type: text/plain\r\n"
396 "Content-Disposition: inline\r\n"
397 "Content-Description: text-part-2\r\n"
398 "\r\n"
399 "Some more text here.\r\n"
400 "\r\n"
401 "--inner\r\n"
402 "Content-Type: image/jpeg\r\n"
403 "Content-Disposition: attachment\r\n"
404 "Content-Description: jpeg-1\r\n"
405 "\r\n"
406 "<jpeg data>\r\n"
407 "--inner--");
408 /* No trailing CRLF is _correct_ - see RFC2046 5.1.1 note regarding CRLF
409 * preceding the boundary delimiter line. An implication of this is that we
410 * must cope with partial lines at the end of the input when recursively
411 * decomposing a multipart message. */
412 vector_init(parts);
413 insist(mime_multipart("--inner\r\n"
414 "Content-Type: text/plain\r\n"
415 "Content-Disposition: inline\r\n"
416 "Content-Description: text-part-2\r\n"
417 "\r\n"
418 "Some more text here.\r\n"
419 "\r\n"
420 "--inner\r\n"
421 "Content-Type: image/jpeg\r\n"
422 "Content-Disposition: attachment\r\n"
423 "Content-Description: jpeg-1\r\n"
424 "\r\n"
425 "<jpeg data>\r\n"
426 "--inner--",
427 test_multipart_callback,
428 "inner",
429 parts) == 0);
430 check_integer(parts->nvec, 2);
431 check_string(parts->vec[0],
432 "Content-Type: text/plain\r\n"
433 "Content-Disposition: inline\r\n"
434 "Content-Description: text-part-2\r\n"
435 "\r\n"
436 "Some more text here.\r\n");
437 check_string(parts->vec[1],
438 "Content-Type: image/jpeg\r\n"
439 "Content-Disposition: attachment\r\n"
440 "Content-Description: jpeg-1\r\n"
441 "\r\n"
442 "<jpeg data>");
443
460b9539 444 /* XXX mime_parse */
460b9539 445
446 check_string(mime_qp(""), "");
447 check_string(mime_qp("foobar"), "foobar");
448 check_string(mime_qp("foo=20bar"), "foo bar");
449 check_string(mime_qp("x \r\ny"), "x\r\ny");
450 check_string(mime_qp("x=\r\ny"), "xy");
451 check_string(mime_qp("x= \r\ny"), "xy");
452 check_string(mime_qp("x =\r\ny"), "x y");
453 check_string(mime_qp("x = \r\ny"), "x y");
454
455 /* from RFC2045 */
456 check_string(mime_qp("Now's the time =\r\n"
457"for all folk to come=\r\n"
458" to the aid of their country."),
459 "Now's the time for all folk to come to the aid of their country.");
460
461 check_string(mime_base64(""), "");
462 check_string(mime_base64("BBBB"), "\x04\x10\x41");
463 check_string(mime_base64("////"), "\xFF\xFF\xFF");
464 check_string(mime_base64("//BB"), "\xFF\xF0\x41");
465 check_string(mime_base64("BBBB//BB////"),
466 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
467 check_string(mime_base64("B B B B / / B B / / / /"),
468 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
469 check_string(mime_base64("B\r\nBBB.// B-B//~//"),
470 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
471 check_string(mime_base64("BBBB="),
472 "\x04\x10\x41");
473 check_string(mime_base64("BBBBx="), /* not actually valid base64 */
474 "\x04\x10\x41");
475 check_string(mime_base64("BBBB BB=="),
476 "\x04\x10\x41" "\x04");
477 check_string(mime_base64("BBBB BBB="),
478 "\x04\x10\x41" "\x04\x10");
479}
480
39d4aa6b
RK
481static void test_cookies(void) {
482 struct cookiedata cd[1];
483
484 fprintf(stderr, "test_cookies\n");
485
486 /* These are the examples from RFC2109 */
487 insist(!parse_cookie("$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"", cd));
488 insist(!strcmp(cd->version, "1"));
489 insist(cd->ncookies = 1);
490 insist(find_cookie(cd, "Customer") == &cd->cookies[0]);
491 check_string(cd->cookies[0].value, "WILE_E_COYOTE");
492 check_string(cd->cookies[0].path, "/acme");
493 insist(cd->cookies[0].domain == 0);
494 insist(!parse_cookie("$Version=\"1\";\n"
495 "Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\";\n"
496 "Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\"",
497 cd));
498 insist(cd->ncookies = 2);
499 insist(find_cookie(cd, "Customer") == &cd->cookies[0]);
500 insist(find_cookie(cd, "Part_Number") == &cd->cookies[1]);
501 check_string(cd->cookies[0].value, "WILE_E_COYOTE");
502 check_string(cd->cookies[0].path, "/acme");
503 insist(cd->cookies[0].domain == 0);
504 check_string(cd->cookies[1].value, "Rocket_Launcher_0001");
505 check_string(cd->cookies[1].path, "/acme");
506 insist(cd->cookies[1].domain == 0);
507 insist(!parse_cookie("$Version=\"1\";\n"
508 "Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\";\n"
509 "Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\";\n"
510 "Shipping=\"FedEx\"; $Path=\"/acme\"",
511 cd));
512 insist(cd->ncookies = 3);
513 insist(find_cookie(cd, "Customer") == &cd->cookies[0]);
514 insist(find_cookie(cd, "Part_Number") == &cd->cookies[1]);
515 insist(find_cookie(cd, "Shipping") == &cd->cookies[2]);
516 check_string(cd->cookies[0].value, "WILE_E_COYOTE");
517 check_string(cd->cookies[0].path, "/acme");
518 insist(cd->cookies[0].domain == 0);
519 check_string(cd->cookies[1].value, "Rocket_Launcher_0001");
520 check_string(cd->cookies[1].path, "/acme");
521 insist(cd->cookies[1].domain == 0);
522 check_string(cd->cookies[2].value, "FedEx");
523 check_string(cd->cookies[2].path, "/acme");
524 insist(cd->cookies[2].domain == 0);
525}
526
460b9539 527static void test_hex(void) {
528 unsigned n;
529 static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F };
530 uint8_t *u;
531 size_t ul;
532
033fd4e3
RK
533 fprintf(stderr, "test_hex\n");
534
460b9539 535 for(n = 0; n <= UCHAR_MAX; ++n) {
536 if(!isxdigit(n))
537 insist(unhexdigitq(n) == -1);
538 }
539 insist(unhexdigitq('0') == 0);
540 insist(unhexdigitq('1') == 1);
541 insist(unhexdigitq('2') == 2);
542 insist(unhexdigitq('3') == 3);
543 insist(unhexdigitq('4') == 4);
544 insist(unhexdigitq('5') == 5);
545 insist(unhexdigitq('6') == 6);
546 insist(unhexdigitq('7') == 7);
547 insist(unhexdigitq('8') == 8);
548 insist(unhexdigitq('9') == 9);
549 insist(unhexdigitq('a') == 10);
550 insist(unhexdigitq('b') == 11);
551 insist(unhexdigitq('c') == 12);
552 insist(unhexdigitq('d') == 13);
553 insist(unhexdigitq('e') == 14);
554 insist(unhexdigitq('f') == 15);
555 insist(unhexdigitq('A') == 10);
556 insist(unhexdigitq('B') == 11);
557 insist(unhexdigitq('C') == 12);
558 insist(unhexdigitq('D') == 13);
559 insist(unhexdigitq('E') == 14);
560 insist(unhexdigitq('F') == 15);
561 check_string(hex(h, sizeof h), "00ff807f");
562 check_string(hex(0, 0), "");
563 u = unhex("00ff807f", &ul);
564 insist(ul == 4);
565 insist(memcmp(u, h, 4) == 0);
566 u = unhex("00FF807F", &ul);
567 insist(ul == 4);
568 insist(memcmp(u, h, 4) == 0);
569 u = unhex("", &ul);
570 insist(ul == 0);
033fd4e3 571 fprintf(stderr, "2 ERROR reports expected {\n");
460b9539 572 insist(unhex("F", 0) == 0);
573 insist(unhex("az", 0) == 0);
033fd4e3 574 fprintf(stderr, "}\n");
460b9539 575}
576
577static void test_casefold(void) {
e5a5a138 578 uint32_t c, l;
56fd389c 579 const char *input, *canon_folded, *compat_folded, *canon_expected, *compat_expected;
460b9539 580
033fd4e3 581 fprintf(stderr, "test_casefold\n");
56fd389c
RK
582
583 /* This isn't a very exhaustive test. Unlike for normalization, there don't
584 * seem to be any public test vectors for these algorithms. */
e5a5a138 585
460b9539 586 for(c = 1; c < 256; ++c) {
e5a5a138 587 input = utf32_to_utf8(&c, 1, 0);
56fd389c
RK
588 canon_folded = utf8_casefold_canon(input, strlen(input), 0);
589 compat_folded = utf8_casefold_compat(input, strlen(input), 0);
460b9539 590 switch(c) {
591 default:
592 if((c >= 'A' && c <= 'Z')
593 || (c >= 0xC0 && c <= 0xDE && c != 0xD7))
594 l = c ^ 0x20;
595 else
596 l = c;
597 break;
598 case 0xB5: /* MICRO SIGN */
e5a5a138 599 l = 0x3BC; /* GREEK SMALL LETTER MU */
460b9539 600 break;
601 case 0xDF: /* LATIN SMALL LETTER SHARP S */
22896b25
RK
602 check_string(canon_folded, "ss");
603 check_string(compat_folded, "ss");
460b9539 604 l = 0;
605 break;
606 }
607 if(l) {
caecd4f4 608 uint32_t *d;
e5a5a138 609 /* Case-folded data is now normalized */
caecd4f4
RK
610 d = utf32_decompose_canon(&l, 1, 0);
611 canon_expected = utf32_to_utf8(d, utf32_len(d), 0);
56fd389c
RK
612 if(strcmp(canon_folded, canon_expected)) {
613 fprintf(stderr, "%s:%d: canon-casefolding %#lx got '%s', expected '%s'\n",
614 __FILE__, __LINE__, (unsigned long)c,
615 format(canon_folded), format(canon_expected));
bb48024f 616 count_error();
56fd389c
RK
617 }
618 ++tests;
caecd4f4
RK
619 d = utf32_decompose_compat(&l, 1, 0);
620 compat_expected = utf32_to_utf8(d, utf32_len(d), 0);
56fd389c
RK
621 if(strcmp(compat_folded, compat_expected)) {
622 fprintf(stderr, "%s:%d: compat-casefolding %#lx got '%s', expected '%s'\n",
460b9539 623 __FILE__, __LINE__, (unsigned long)c,
56fd389c 624 format(compat_folded), format(compat_expected));
bb48024f 625 count_error();
460b9539 626 }
627 ++tests;
628 }
629 }
c85b7022 630 check_string(utf8_casefold_canon("", 0, 0), "");
460b9539 631}
632
8818b7fc
RK
633struct {
634 const char *in;
635 const char *expect[10];
636} wtest[] = {
637 /* Empty string */
638 { "", { 0 } },
639 /* Only whitespace and punctuation */
640 { " ", { 0 } },
641 { " ' ", { 0 } },
642 { " ! ", { 0 } },
643 { " \"\" ", { 0 } },
644 { " @ ", { 0 } },
645 /* Basics */
646 { "wibble", { "wibble", 0 } },
647 { " wibble", { "wibble", 0 } },
648 { " wibble ", { "wibble", 0 } },
649 { "wibble ", { "wibble", 0 } },
650 { "wibble spong", { "wibble", "spong", 0 } },
651 { " wibble spong", { "wibble", "spong", 0 } },
652 { " wibble spong ", { "wibble", "spong", 0 } },
653 { "wibble spong ", { "wibble", "spong", 0 } },
654 { "wibble spong splat foo zot ", { "wibble", "spong", "splat", "foo", "zot", 0 } },
655 /* Apostrophes */
656 { "wibble 'spong", { "wibble", "spong", 0 } },
657 { " wibble's", { "wibble's", 0 } },
658 { " wibblespong' ", { "wibblespong", 0 } },
659 { "wibble sp''ong ", { "wibble", "sp", "ong", 0 } },
660};
661#define NWTEST (sizeof wtest / sizeof *wtest)
662
663static void test_words(void) {
664 size_t t, nexpect, ngot, i;
665 int right;
666
667 fprintf(stderr, "test_words\n");
668 for(t = 0; t < NWTEST; ++t) {
c85b7022 669 char **got = utf8_word_split(wtest[t].in, strlen(wtest[t].in), &ngot, 0);
8818b7fc
RK
670
671 for(nexpect = 0; wtest[t].expect[nexpect]; ++nexpect)
672 ;
673 if(nexpect == ngot) {
674 for(i = 0; i < ngot; ++i)
675 if(strcmp(wtest[t].expect[i], got[i]))
676 break;
677 right = i == ngot;
678 } else
679 right = 0;
680 if(!right) {
681 fprintf(stderr, "word split %zu failed\n", t);
682 fprintf(stderr, "input: %s\n", wtest[t].in);
683 fprintf(stderr, " | %-30s | %-30s\n",
684 "expected", "got");
685 for(i = 0; i < nexpect || i < ngot; ++i) {
686 const char *e = i < nexpect ? wtest[t].expect[i] : "<none>";
687 const char *g = i < ngot ? got[i] : "<none>";
688 fprintf(stderr, " %2zu | %-30s | %-30s\n", i, e, g);
689 }
690 count_error();
691 }
692 ++tests;
693 }
694}
695
033fd4e3
RK
696/** @brief Less-than comparison function for integer heap */
697static inline int int_lt(int a, int b) { return a < b; }
698
dab22732
RK
699/** @struct iheap
700 * @brief A heap with @c int elements */
033fd4e3 701HEAP_TYPE(iheap, int, int_lt);
8e3fe3d8 702HEAP_DEFINE(iheap, int, int_lt);
033fd4e3
RK
703
704/** @brief Tests for @ref heap.h */
705static void test_heap(void) {
706 struct iheap h[1];
707 int n;
708 int last = -1;
709
710 fprintf(stderr, "test_heap\n");
711
712 iheap_init(h);
713 for(n = 0; n < 1000; ++n)
714 iheap_insert(h, random() % 100);
715 for(n = 0; n < 1000; ++n) {
716 const int latest = iheap_remove(h);
717 if(last > latest)
718 fprintf(stderr, "should have %d <= %d\n", last, latest);
719 insist(last <= latest);
720 last = latest;
721 }
722 putchar('\n');
723}
724
e2452add
RK
725/** @brief Open a Unicode test file */
726static FILE *open_unicode_test(const char *path) {
727 const char *base;
728 FILE *fp;
729 char buffer[1024];
730 int w;
731
732 if((base = strrchr(path, '/')))
733 ++base;
734 else
735 base = path;
736 if(!(fp = fopen(base, "r"))) {
737 snprintf(buffer, sizeof buffer,
738 "wget http://www.unicode.org/Public/5.0.0/ucd/%s", path);
739 if((w = system(buffer)))
740 fatal(0, "%s: %s", buffer, wstat(w));
741 if(chmod(base, 0444) < 0)
742 fatal(errno, "chmod %s", base);
743 if(!(fp = fopen(base, "r")))
744 fatal(errno, "%s", base);
745 }
746 return fp;
747}
748
1625e11a 749/** @brief Run breaking tests for utf32_grapheme_boundary() etc */
bb48024f
RK
750static void breaktest(const char *path,
751 int (*breakfn)(const uint32_t *, size_t, size_t)) {
752 FILE *fp = open_unicode_test(path);
753 int lineno = 0;
754 char *l, *lp;
755 size_t bn, n;
756 char break_allowed[1024];
757 uint32_t buffer[1024];
758
759 while(!inputline(path, fp, &l, '\n')) {
760 ++lineno;
761 if(l[0] == '#') continue;
762 bn = 0;
763 lp = l;
764 while(*lp) {
765 if(*lp == ' ' || *lp == '\t') {
766 ++lp;
767 continue;
768 }
769 if(*lp == '#')
770 break;
771 if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0xB7) {
772 /* 00F7 DIVISION SIGN */
773 break_allowed[bn] = 1;
774 lp += 2;
775 continue;
776 }
777 if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0x97) {
778 /* 00D7 MULTIPLICATION SIGN */
779 break_allowed[bn] = 0;
780 lp += 2;
781 continue;
782 }
783 if(isxdigit((unsigned char)*lp)) {
784 buffer[bn++] = strtoul(lp, &lp, 16);
785 continue;
786 }
787 fatal(0, "%s:%d: evil line: %s", path, lineno, l);
788 }
789 for(n = 0; n <= bn; ++n) {
790 if(breakfn(buffer, bn, n) != break_allowed[n]) {
791 fprintf(stderr,
b21a155c
RK
792 "%s:%d: offset %zu: mismatch\n"
793 "%s\n"
794 "\n",
795 path, lineno, n, l);
bb48024f
RK
796 count_error();
797 }
798 ++tests;
799 }
800 xfree(l);
801 }
802 fclose(fp);
803}
804
e5a5a138
RK
805/** @brief Tests for @ref lib/unicode.h */
806static void test_unicode(void) {
807 FILE *fp;
808 int lineno = 0;
809 char *l, *lp;
810 uint32_t buffer[1024];
16506c9d 811 uint32_t *c[6], *NFD_c[6], *NFKD_c[6], *NFC_c[6], *NFKC_c[6]; /* 1-indexed */
e5a5a138
RK
812 int cn, bn;
813
814 fprintf(stderr, "test_unicode\n");
e2452add 815 fp = open_unicode_test("NormalizationTest.txt");
e5a5a138
RK
816 while(!inputline("NormalizationTest.txt", fp, &l, '\n')) {
817 ++lineno;
818 if(*l == '#' || *l == '@')
819 continue;
820 bn = 0;
821 cn = 1;
822 lp = l;
823 c[cn++] = &buffer[bn];
824 while(*lp && *lp != '#') {
825 if(*lp == ' ') {
826 ++lp;
827 continue;
828 }
829 if(*lp == ';') {
830 buffer[bn++] = 0;
831 if(cn == 6)
832 break;
833 c[cn++] = &buffer[bn];
834 ++lp;
835 continue;
836 }
837 buffer[bn++] = strtoul(lp, &lp, 16);
838 }
839 buffer[bn] = 0;
840 assert(cn == 6);
841 for(cn = 1; cn <= 5; ++cn) {
842 NFD_c[cn] = utf32_decompose_canon(c[cn], utf32_len(c[cn]), 0);
843 NFKD_c[cn] = utf32_decompose_compat(c[cn], utf32_len(c[cn]), 0);
16506c9d
RK
844 NFC_c[cn] = utf32_compose_canon(c[cn], utf32_len(c[cn]), 0);
845 NFKC_c[cn] = utf32_compose_compat(c[cn], utf32_len(c[cn]), 0);
e5a5a138
RK
846 }
847#define unt_check(T, A, B) do { \
848 ++tests; \
849 if(utf32_cmp(c[A], T##_c[B])) { \
e2452add
RK
850 fprintf(stderr, \
851 "NormalizationTest.txt:%d: c%d != "#T"(c%d)\n", \
852 lineno, A, B); \
16506c9d 853 fprintf(stderr, " c%d:%s\n", \
e5a5a138 854 A, format_utf32(c[A])); \
16506c9d
RK
855 fprintf(stderr, " c%d:%s\n", \
856 B, format_utf32(c[B])); \
857 fprintf(stderr, "%4s(c%d):%s\n", \
e5a5a138 858 #T, B, format_utf32(T##_c[B])); \
bcf9ed7f 859 count_error(); \
e5a5a138
RK
860 } \
861 } while(0)
862 unt_check(NFD, 3, 1);
863 unt_check(NFD, 3, 2);
864 unt_check(NFD, 3, 3);
865 unt_check(NFD, 5, 4);
866 unt_check(NFD, 5, 5);
867 unt_check(NFKD, 5, 1);
868 unt_check(NFKD, 5, 2);
869 unt_check(NFKD, 5, 3);
870 unt_check(NFKD, 5, 4);
871 unt_check(NFKD, 5, 5);
16506c9d
RK
872 unt_check(NFC, 2, 1);
873 unt_check(NFC, 2, 2);
874 unt_check(NFC, 2, 3);
875 unt_check(NFC, 4, 4);
876 unt_check(NFC, 4, 5);
877 unt_check(NFKC, 4, 1);
878 unt_check(NFKC, 4, 2);
879 unt_check(NFKC, 4, 3);
880 unt_check(NFKC, 4, 4);
881 unt_check(NFKC, 4, 5);
e5a5a138
RK
882 for(cn = 1; cn <= 5; ++cn) {
883 xfree(NFD_c[cn]);
884 xfree(NFKD_c[cn]);
885 }
886 xfree(l);
887 }
e2452add 888 fclose(fp);
1625e11a 889 breaktest("auxiliary/GraphemeBreakTest.txt", utf32_is_grapheme_boundary);
bb48024f 890 breaktest("auxiliary/WordBreakTest.txt", utf32_is_word_boundary);
c35e83d9
RK
891 insist(utf32_combining_class(0x40000) == 0);
892 insist(utf32_combining_class(0xE0000) == 0);
e5a5a138
RK
893}
894
ea387d53
RK
895static void test_signame(void) {
896 fprintf(stderr, "test_signame\n");
897 insist(find_signal("SIGTERM") == SIGTERM);
898 insist(find_signal("SIGHUP") == SIGHUP);
899 insist(find_signal("SIGINT") == SIGINT);
900 insist(find_signal("SIGQUIT") == SIGQUIT);
901 insist(find_signal("SIGKILL") == SIGKILL);
902 insist(find_signal("SIGYOURMUM") == -1);
903}
904
9f28e855
RK
905static void test_cache(void) {
906 const struct cache_type t1 = { 1 }, t2 = { 10 };
907 const char v11[] = "spong", v12[] = "wibble", v2[] = "blat";
908 fprintf(stderr, "test_cache\n");
909 cache_put(&t1, "1_1", v11);
910 cache_put(&t1, "1_2", v12);
911 cache_put(&t2, "2", v2);
912 insist(cache_count() == 3);
913 insist(cache_get(&t2, "2") == v2);
914 insist(cache_get(&t1, "1_1") == v11);
915 insist(cache_get(&t1, "1_2") == v12);
916 insist(cache_get(&t1, "2") == 0);
917 insist(cache_get(&t2, "1_1") == 0);
918 insist(cache_get(&t2, "1_2") == 0);
919 insist(cache_get(&t1, "2") == 0);
920 insist(cache_get(&t2, "1_1") == 0);
921 insist(cache_get(&t2, "1_2") == 0);
922 sleep(2);
923 cache_expire();
924 insist(cache_count() == 1);
925 insist(cache_get(&t1, "1_1") == 0);
926 insist(cache_get(&t1, "1_2") == 0);
927 insist(cache_get(&t2, "2") == v2);
928 cache_clean(0);
929 insist(cache_count() == 0);
930 insist(cache_get(&t2, "2") == 0);
931}
932
00e36cd0
RK
933static void test_filepart(void) {
934 fprintf(stderr, "test_filepart\n");
935 check_string(d_dirname("/"), "/");
011e0b89 936 check_string(d_dirname("////"), "/");
00e36cd0 937 check_string(d_dirname("/spong"), "/");
011e0b89 938 check_string(d_dirname("////spong"), "/");
00e36cd0 939 check_string(d_dirname("/foo/bar"), "/foo");
011e0b89 940 check_string(d_dirname("////foo/////bar"), "////foo");
00e36cd0 941 check_string(d_dirname("./bar"), ".");
011e0b89 942 check_string(d_dirname(".//bar"), ".");
00e36cd0
RK
943 check_string(d_dirname("."), ".");
944 check_string(d_dirname(".."), ".");
945 check_string(d_dirname("../blat"), "..");
011e0b89 946 check_string(d_dirname("..//blat"), "..");
00e36cd0
RK
947 check_string(d_dirname("wibble"), ".");
948 check_string(extension("foo.c"), ".c");
949 check_string(extension(".c"), ".c");
950 check_string(extension("."), ".");
951 check_string(extension("foo"), "");
952 check_string(extension("./foo"), "");
953 check_string(extension("./foo.c"), ".c");
011e0b89
RK
954 check_string(strip_extension("foo.c"), "foo");
955 check_string(strip_extension("foo.mp3"), "foo");
956 check_string(strip_extension("foo.---"), "foo.---");
957 check_string(strip_extension("foo.---xyz"), "foo.---xyz");
958 check_string(strip_extension("foo.bar/wibble.spong"), "foo.bar/wibble");
00e36cd0
RK
959}
960
65bb0fff
RK
961static void test_selection(void) {
962 hash *h;
963 fprintf(stderr, "test_selection\n");
964 insist((h = selection_new()) != 0);
965 selection_set(h, "one", 1);
966 selection_set(h, "two", 1);
967 selection_set(h, "three", 0);
968 selection_set(h, "four", 1);
969 insist(selection_selected(h, "one") == 1);
970 insist(selection_selected(h, "two") == 1);
971 insist(selection_selected(h, "three") == 0);
972 insist(selection_selected(h, "four") == 1);
973 insist(selection_selected(h, "five") == 0);
974 insist(hash_count(h) == 3);
975 selection_flip(h, "one");
976 selection_flip(h, "three");
977 insist(selection_selected(h, "one") == 0);
978 insist(selection_selected(h, "three") == 1);
979 insist(hash_count(h) == 3);
980 selection_live(h, "one");
981 selection_live(h, "two");
982 selection_live(h, "three");
983 selection_cleanup(h);
984 insist(selection_selected(h, "one") == 0);
985 insist(selection_selected(h, "two") == 1);
986 insist(selection_selected(h, "three") == 1);
987 insist(selection_selected(h, "four") == 0);
988 insist(selection_selected(h, "five") == 0);
989 insist(hash_count(h) == 2);
990 selection_empty(h);
991 insist(selection_selected(h, "one") == 0);
992 insist(selection_selected(h, "two") == 0);
993 insist(selection_selected(h, "three") == 0);
994 insist(selection_selected(h, "four") == 0);
995 insist(selection_selected(h, "five") == 0);
996 insist(hash_count(h) == 0);
997}
998
71b90230
RK
999static void test_wstat(void) {
1000 pid_t pid;
1001 int w;
1002
1003 fprintf(stderr, "test_wstat\n");
1004 if(!(pid = xfork())) {
1005 _exit(1);
1006 }
1007 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
1008 ;
1009 check_string(wstat(w), "exited with status 1");
1010 if(!(pid = xfork())) {
1011 kill(getpid(), SIGTERM);
1012 _exit(-1);
1013 }
1014 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
1015 ;
1016 check_string_prefix(wstat(w), "terminated by signal 15");
1017}
1018
22f61603
RK
1019static void test_kvp(void) {
1020 struct kvp *k;
632637c7 1021 size_t n;
22f61603
RK
1022
1023 fprintf(stderr, "test_kvp\n");
632637c7 1024 /* decoding */
22f61603
RK
1025#define KVP_URLDECODE(S) kvp_urldecode((S), strlen(S))
1026 insist(KVP_URLDECODE("=%zz") == 0);
1027 insist(KVP_URLDECODE("=%0") == 0);
1028 insist(KVP_URLDECODE("=%0z") == 0);
1029 insist(KVP_URLDECODE("=%%") == 0);
1030 insist(KVP_URLDECODE("==%") == 0);
1031 insist(KVP_URLDECODE("wibble") == 0);
1032 insist(KVP_URLDECODE("") == 0);
1033 insist(KVP_URLDECODE("wibble&") == 0);
1034 insist((k = KVP_URLDECODE("one=bl%61t+foo")) != 0);
1035 check_string(kvp_get(k, "one"), "blat foo");
1036 insist(kvp_get(k, "ONE") == 0);
1037 insist(k->next == 0);
1038 insist((k = KVP_URLDECODE("wibble=splat&bar=spong")) != 0);
1039 check_string(kvp_get(k, "wibble"), "splat");
1040 check_string(kvp_get(k, "bar"), "spong");
1041 insist(kvp_get(k, "ONE") == 0);
1042 insist(k->next->next == 0);
632637c7
RK
1043 /* encoding */
1044 insist(kvp_set(&k, "bar", "spong") == 0);
1045 insist(kvp_set(&k, "bar", "foo") == 1);
1046 insist(kvp_set(&k, "zog", "%") == 1);
1047 insist(kvp_set(&k, "wibble", 0) == 1);
1048 insist(kvp_set(&k, "wibble", 0) == 0);
1049 check_string(kvp_urlencode(k, 0),
1050 "bar=foo&zog=%25");
1051 check_string(kvp_urlencode(k, &n),
1052 "bar=foo&zog=%25");
1053 insist(n == strlen("bar=foo&zog=%25"));
1054 check_string(urlencodestring("abc% +\n"),
1055 "abc%25%20%2b%0a");
22f61603
RK
1056}
1057
6049fe0e
RK
1058static void test_sink(void) {
1059 struct sink *s;
1060 struct dynstr d[1];
1061 FILE *fp;
1062 char *l;
1063
1064 fprintf(stderr, "test_sink\n");
1065
1066 fp = tmpfile();
1067 assert(fp != 0);
1068 s = sink_stdio("tmpfile", fp);
1069 insist(sink_printf(s, "test: %d\n", 999) == 10);
1070 insist(sink_printf(s, "wibble: %s\n", "foobar") == 15);
1071 rewind(fp);
1072 insist(inputline("tmpfile", fp, &l, '\n') == 0);
1073 check_string(l, "test: 999");
1074 insist(inputline("tmpfile", fp, &l, '\n') == 0);
1075 check_string(l, "wibble: foobar");
1076 insist(inputline("tmpfile", fp, &l, '\n') == -1);
1077
1078 dynstr_init(d);
1079 s = sink_dynstr(d);
1080 insist(sink_printf(s, "test: %d\n", 999) == 10);
1081 insist(sink_printf(s, "wibble: %s\n", "foobar") == 15);
1082 dynstr_terminate(d);
1083 check_string(d->vec, "test: 999\nwibble: foobar\n");
1084}
1085
0c740e59
RK
1086static const char *do_printf(const char *fmt, ...) {
1087 va_list ap;
1088 char *s;
1089 int rc;
1090
1091 va_start(ap, fmt);
1092 rc = byte_vasprintf(&s, fmt, ap);
1093 va_end(ap);
1094 if(rc < 0)
1095 return 0;
1096 return s;
1097}
1098
1099static void test_printf(void) {
1100 char c;
1101 short s;
1102 int i;
1103 long l;
1104 long long ll;
1105 intmax_t m;
1106 ssize_t ssz;
1107 ptrdiff_t p;
011e0b89
RK
1108 char *cp;
1109 char buffer[16];
0c740e59
RK
1110
1111 fprintf(stderr, "test_printf\n");
1112 check_string(do_printf("%d", 999), "999");
1113 check_string(do_printf("%d", -999), "-999");
1114 check_string(do_printf("%i", 999), "999");
1115 check_string(do_printf("%i", -999), "-999");
1116 check_string(do_printf("%u", 999), "999");
1117 check_string(do_printf("%2u", 999), "999");
1118 check_string(do_printf("%10u", 999), " 999");
1119 check_string(do_printf("%-10u", 999), "999 ");
1120 check_string(do_printf("%010u", 999), "0000000999");
1121 check_string(do_printf("%-10d", -999), "-999 ");
1122 check_string(do_printf("%-010d", -999), "-999 "); /* "-" beats "0" */
1123 check_string(do_printf("%66u", 999), " 999");
1124 check_string(do_printf("%o", 999), "1747");
1125 check_string(do_printf("%#o", 999), "01747");
1126 check_string(do_printf("%#o", 0), "0");
1127 check_string(do_printf("%x", 999), "3e7");
1128 check_string(do_printf("%#x", 999), "0x3e7");
1129 check_string(do_printf("%#X", 999), "0X3E7");
1130 check_string(do_printf("%#x", 0), "0");
1131 check_string(do_printf("%hd", (short)999), "999");
1132 check_string(do_printf("%hhd", (short)99), "99");
1133 check_string(do_printf("%ld", 100000L), "100000");
1134 check_string(do_printf("%lld", 10000000000LL), "10000000000");
1135 check_string(do_printf("%qd", 10000000000LL), "10000000000");
1136 check_string(do_printf("%jd", (intmax_t)10000000000LL), "10000000000");
1137 check_string(do_printf("%zd", (ssize_t)2000000000), "2000000000");
1138 check_string(do_printf("%td", (ptrdiff_t)2000000000), "2000000000");
1139 check_string(do_printf("%hu", (short)999), "999");
1140 check_string(do_printf("%hhu", (short)99), "99");
1141 check_string(do_printf("%lu", 100000L), "100000");
1142 check_string(do_printf("%llu", 10000000000LL), "10000000000");
1143 check_string(do_printf("%ju", (uintmax_t)10000000000LL), "10000000000");
1144 check_string(do_printf("%zu", (size_t)2000000000), "2000000000");
1145 check_string(do_printf("%tu", (ptrdiff_t)2000000000), "2000000000");
1146 check_string(do_printf("%p", (void *)0x100), "0x100");
1147 check_string(do_printf("%s", "wibble"), "wibble");
1148 check_string(do_printf("%s-%s", "wibble", "wobble"), "wibble-wobble");
1149 check_string(do_printf("%10s", "wibble"), " wibble");
1150 check_string(do_printf("%010s", "wibble"), " wibble"); /* 0 ignored for %s */
1151 check_string(do_printf("%-10s", "wibble"), "wibble ");
1152 check_string(do_printf("%2s", "wibble"), "wibble");
1153 check_string(do_printf("%.2s", "wibble"), "wi");
1154 check_string(do_printf("%.2s", "w"), "w");
1155 check_string(do_printf("%4.2s", "wibble"), " wi");
1156 check_string(do_printf("%c", 'a'), "a");
1157 check_string(do_printf("%4c", 'a'), " a");
1158 check_string(do_printf("%-4c", 'a'), "a ");
1159 check_string(do_printf("%*c", 0, 'a'), "a");
1160 check_string(do_printf("x%hhny", &c), "xy");
1161 insist(c == 1);
1162 check_string(do_printf("xx%hnyy", &s), "xxyy");
1163 insist(s == 2);
1164 check_string(do_printf("xxx%nyyy", &i), "xxxyyy");
1165 insist(i == 3);
1166 check_string(do_printf("xxxx%lnyyyy", &l), "xxxxyyyy");
1167 insist(l == 4);
1168 check_string(do_printf("xxxxx%llnyyyyy", &ll), "xxxxxyyyyy");
1169 insist(ll == 5);
1170 check_string(do_printf("xxxxxx%jnyyyyyy", &m), "xxxxxxyyyyyy");
1171 insist(m == 6);
1172 check_string(do_printf("xxxxxxx%znyyyyyyy", &ssz), "xxxxxxxyyyyyyy");
1173 insist(ssz == 7);
1174 check_string(do_printf("xxxxxxxx%tnyyyyyyyy", &p), "xxxxxxxxyyyyyyyy");
1175 insist(p == 8);
1176 check_string(do_printf("%*d", 5, 99), " 99");
1177 check_string(do_printf("%*d", -5, 99), "99 ");
1178 check_string(do_printf("%.*d", 5, 99), "00099");
1179 check_string(do_printf("%.*d", -5, 99), "99");
1180 check_string(do_printf("%.0d", 0), "");
1181 check_string(do_printf("%.d", 0), "");
1182 check_string(do_printf("%.d", 0), "");
1183 check_string(do_printf("%%"), "%");
1184 check_string(do_printf("wibble"), "wibble");
1185 insist(do_printf("%") == 0);
1186 insist(do_printf("%=") == 0);
011e0b89
RK
1187 i = byte_asprintf(&cp, "xyzzy %d", 999);
1188 insist(i == 9);
1189 check_string(cp, "xyzzy 999");
1190 i = byte_snprintf(buffer, sizeof buffer, "xyzzy %d", 999);
1191 insist(i == 9);
1192 check_string(buffer, "xyzzy 999");
1193 i = byte_snprintf(buffer, sizeof buffer, "%*d", 32, 99);
1194 insist(i == 32);
1195 check_string(buffer, " ");
1196 {
1197 /* bizarre workaround for compiler checking of format strings */
1198 char f[] = "xyzzy %";
1199 i = byte_asprintf(&cp, f);
1200 insist(i == -1);
1201 }
0c740e59
RK
1202}
1203
abe28bfe
RK
1204static void test_basen(void) {
1205 unsigned long v[64];
1206 char buffer[1024];
1207
1208 fprintf(stderr, "test_basen\n");
1209 v[0] = 999;
1210 insist(basen(v, 1, buffer, sizeof buffer, 10) == 0);
1211 check_string(buffer, "999");
1212
1213 v[0] = 1+2*7+3*7*7+4*7*7*7;
1214 insist(basen(v, 1, buffer, sizeof buffer, 7) == 0);
1215 check_string(buffer, "4321");
1216
1217 v[0] = 0x00010203;
1218 v[1] = 0x04050607;
1219 v[2] = 0x08090A0B;
1220 v[3] = 0x0C0D0E0F;
1221 insist(basen(v, 4, buffer, sizeof buffer, 256) == 0);
1222 check_string(buffer, "123456789abcdef");
1223
1224 v[0] = 0x00010203;
1225 v[1] = 0x04050607;
1226 v[2] = 0x08090A0B;
1227 v[3] = 0x0C0D0E0F;
1228 insist(basen(v, 4, buffer, sizeof buffer, 16) == 0);
1229 check_string(buffer, "102030405060708090a0b0c0d0e0f");
1230
1231 v[0] = 0x00010203;
1232 v[1] = 0x04050607;
1233 v[2] = 0x08090A0B;
1234 v[3] = 0x0C0D0E0F;
1235 insist(basen(v, 4, buffer, 10, 16) == -1);
1236}
1237
5e49fa7f
RK
1238static void test_split(void) {
1239 char **v;
1240 int nv;
1241
1242 fprintf(stderr, "test_split\n");
1243 insist(split("\"misquoted", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1244 insist(split("\'misquoted", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1245 insist(split("\'misquoted\\", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1246 insist(split("\'misquoted\\\"", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1247 insist(split("\'mis\\escaped\'", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1248
1249 insist((v = split("", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1250 check_integer(nv, 0);
1251 insist(*v == 0);
1252
1253 insist((v = split("wibble", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1254 check_integer(nv, 1);
1255 check_string(v[0], "wibble");
1256 insist(v[1] == 0);
1257
1258 insist((v = split(" wibble \t\r\n wobble ", &nv,
1259 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1260 check_integer(nv, 2);
1261 check_string(v[0], "wibble");
1262 check_string(v[1], "wobble");
1263 insist(v[2] == 0);
1264
1265 insist((v = split("wibble wobble #splat", &nv,
1266 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1267 check_integer(nv, 2);
1268 check_string(v[0], "wibble");
1269 check_string(v[1], "wobble");
1270 insist(v[2] == 0);
1271
1272 insist((v = split("\"wibble wobble\" #splat", &nv,
1273 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1274 check_integer(nv, 1);
1275 check_string(v[0], "wibble wobble");
1276 insist(v[1] == 0);
1277
1278 insist((v = split("\"wibble \\\"\\nwobble\"", &nv,
1279 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1280 check_integer(nv, 1);
1281 check_string(v[0], "wibble \"\nwobble");
1282 insist(v[1] == 0);
1283
1284 insist((v = split("\"wibble wobble\" #splat", &nv,
1285 SPLIT_QUOTES, 0, 0)));
1286 check_integer(nv, 2);
1287 check_string(v[0], "wibble wobble");
1288 check_string(v[1], "#splat");
1289 insist(v[2] == 0);
1290
1291 insist((v = split("\"wibble wobble\" #splat", &nv,
1292 SPLIT_COMMENTS, 0, 0)));
1293 check_integer(nv, 2);
1294 check_string(v[0], "\"wibble");
1295 check_string(v[1], "wobble\"");
1296 insist(v[2] == 0);
1297
1298 check_string(quoteutf8("wibble"), "wibble");
1299 check_string(quoteutf8(" wibble "), "\" wibble \"");
1300 check_string(quoteutf8("wibble wobble"), "\"wibble wobble\"");
1301 check_string(quoteutf8("wibble\"wobble"), "\"wibble\\\"wobble\"");
1302 check_string(quoteutf8("wibble\nwobble"), "\"wibble\\nwobble\"");
1303 check_string(quoteutf8("wibble\\wobble"), "\"wibble\\\\wobble\"");
1304 check_string(quoteutf8("wibble'wobble"), "\"wibble'wobble\"");
1305}
1306
fe575537
RK
1307static void test_hash(void) {
1308 hash *h;
1309 int i, *ip;
1310 char **keys;
1311
1312 fprintf(stderr, "test_hash\n");
1313 h = hash_new(sizeof(int));
1314 for(i = 0; i < 10000; ++i)
1315 insist(hash_add(h, do_printf("%d", i), &i, HASH_INSERT) == 0);
1316 check_integer(hash_count(h), 10000);
1317 for(i = 0; i < 10000; ++i) {
1318 insist((ip = hash_find(h, do_printf("%d", i))) != 0);
1319 check_integer(*ip, i);
1320 insist(hash_add(h, do_printf("%d", i), &i, HASH_REPLACE) == 0);
1321 }
1322 check_integer(hash_count(h), 10000);
1323 keys = hash_keys(h);
1324 for(i = 0; i < 10000; ++i)
1325 insist(keys[i] != 0);
1326 insist(keys[10000] == 0);
1327 for(i = 0; i < 10000; ++i)
1328 insist(hash_remove(h, do_printf("%d", i)) == 0);
1329 check_integer(hash_count(h), 0);
1330}
1331
d6ea854a
RK
1332static void test_addr(void) {
1333 struct stringlist a;
1334 const char *s[2];
1335 struct addrinfo *ai;
1336 char *name;
1337 const struct sockaddr_in *sin;
1338
1339 static const struct addrinfo pref = {
1340 AI_PASSIVE,
1341 PF_INET,
1342 SOCK_STREAM,
1343 0,
1344 0,
1345 0,
1346 0,
1347 0
1348 };
1349
1350 a.n = 1;
1351 a.s = (char **)s;
1352 s[0] = "smtp";
1353 ai = get_address(&a, &pref, &name);
1354 insist(ai != 0);
1355 check_integer(ai->ai_family, PF_INET);
1356 check_integer(ai->ai_socktype, SOCK_STREAM);
1357 check_integer(ai->ai_protocol, IPPROTO_TCP);
1358 check_integer(ai->ai_addrlen, sizeof(struct sockaddr_in));
1359 sin = (const struct sockaddr_in *)ai->ai_addr;
1360 check_integer(sin->sin_family, AF_INET);
1361 check_integer(sin->sin_addr.s_addr, 0);
1362 check_integer(ntohs(sin->sin_port), 25);
1363 check_string(name, "host * service smtp");
1364
1365 a.n = 2;
1366 s[0] = "localhost";
1367 s[1] = "nntp";
1368 ai = get_address(&a, &pref, &name);
1369 insist(ai != 0);
1370 check_integer(ai->ai_family, PF_INET);
1371 check_integer(ai->ai_socktype, SOCK_STREAM);
1372 check_integer(ai->ai_protocol, IPPROTO_TCP);
1373 check_integer(ai->ai_addrlen, sizeof(struct sockaddr_in));
1374 sin = (const struct sockaddr_in *)ai->ai_addr;
1375 check_integer(sin->sin_family, AF_INET);
1376 check_integer(ntohl(sin->sin_addr.s_addr), 0x7F000001);
1377 check_integer(ntohs(sin->sin_port), 119);
1378 check_string(name, "host localhost service nntp");
1379}
1380
460b9539 1381int main(void) {
fe575537 1382 mem_init();
bb48024f 1383 fail_first = !!getenv("FAIL_FIRST");
460b9539 1384 insist('\n' == 0x0A);
1385 insist('\r' == 0x0D);
1386 insist(' ' == 0x20);
1387 insist('0' == 0x30);
1388 insist('9' == 0x39);
1389 insist('A' == 0x41);
1390 insist('Z' == 0x5A);
1391 insist('a' == 0x61);
1392 insist('z' == 0x7A);
1393 /* addr.c */
d6ea854a 1394 test_addr();
460b9539 1395 /* asprintf.c */
1396 /* authhash.c */
1397 /* basen.c */
abe28bfe 1398 test_basen();
460b9539 1399 /* charset.c */
1400 /* client.c */
1401 /* configuration.c */
1402 /* event.c */
00e36cd0
RK
1403 /* filepart.c */
1404 test_filepart();
460b9539 1405 /* fprintf.c */
033fd4e3
RK
1406 /* heap.c */
1407 test_heap();
460b9539 1408 /* hex.c */
1409 test_hex();
1410 /* inputline.c */
1411 /* kvp.c */
22f61603 1412 test_kvp();
460b9539 1413 /* log.c */
1414 /* mem.c */
1415 /* mime.c */
1416 test_mime();
39d4aa6b 1417 test_cookies();
460b9539 1418 /* mixer.c */
1419 /* plugin.c */
1420 /* printf.c */
0c740e59 1421 test_printf();
460b9539 1422 /* queue.c */
1423 /* sink.c */
6049fe0e 1424 test_sink();
460b9539 1425 /* snprintf.c */
1426 /* split.c */
5e49fa7f 1427 test_split();
460b9539 1428 /* syscalls.c */
1429 /* table.c */
e5a5a138
RK
1430 /* unicode.c */
1431 test_unicode();
460b9539 1432 /* utf8.c */
1433 test_utf8();
1434 /* vector.c */
1435 /* words.c */
1436 test_casefold();
8818b7fc 1437 test_words();
460b9539 1438 /* wstat.c */
71b90230 1439 test_wstat();
ea387d53
RK
1440 /* signame.c */
1441 test_signame();
9f28e855
RK
1442 /* cache.c */
1443 test_cache();
65bb0fff
RK
1444 /* selection.c */
1445 test_selection();
fe575537 1446 test_hash();
460b9539 1447 fprintf(stderr, "%d errors out of %d tests\n", errors, tests);
1448 return !!errors;
1449}
1450
1451/*
1452Local Variables:
1453c-basic-offset:2
1454comment-column:40
56fd389c
RK
1455fill-column:79
1456indent-tabs-mode:nil
460b9539 1457End:
1458*/