chiark / gitweb /
more consistent language use in docs
[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
8a7ccdfe
RK
461#define check_base64(encoded, decoded) do { \
462 check_string(mime_base64(encoded, 0), decoded); \
463 check_string(mime_to_base64((const uint8_t *)decoded, \
464 (sizeof decoded) - 1), \
465 encoded); \
466 } while(0)
467
468
469 check_base64("", "");
470 check_base64("BBBB", "\x04\x10\x41");
471 check_base64("////", "\xFF\xFF\xFF");
472 check_base64("//BB", "\xFF\xF0\x41");
473 check_base64("BBBB//BB////",
474 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
475 check_base64("BBBBBA==",
476 "\x04\x10\x41" "\x04");
477 check_base64("BBBBBBA=",
478 "\x04\x10\x41" "\x04\x10");
479
480 /* Check that decoding handles various kinds of rubbish OK */
481 check_string(mime_base64("B B B B / / B B / / / /", 0),
482 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
483 check_string(mime_base64("B\r\nBBB.// B-B//~//", 0),
460b9539 484 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
8a7ccdfe
RK
485 check_string(mime_base64("BBBB BB==", 0),
486 "\x04\x10\x41" "\x04");
487 check_string(mime_base64("BBBB BB = =", 0),
488 "\x04\x10\x41" "\x04");
489 check_string(mime_base64("BBBB BBB=", 0),
490 "\x04\x10\x41" "\x04\x10");
491 check_string(mime_base64("BBBB BBB = ", 0),
492 "\x04\x10\x41" "\x04\x10");
493 check_string(mime_base64("BBBB=", 0),
460b9539 494 "\x04\x10\x41");
8a7ccdfe 495 check_string(mime_base64("BBBBBB==", 0),
460b9539 496 "\x04\x10\x41" "\x04");
8a7ccdfe 497 check_string(mime_base64("BBBBBBB=", 0),
460b9539 498 "\x04\x10\x41" "\x04\x10");
8a7ccdfe
RK
499 /* Not actually valid base64 */
500 check_string(mime_base64("BBBBx=", 0),
501 "\x04\x10\x41");
460b9539 502}
503
39d4aa6b
RK
504static void test_cookies(void) {
505 struct cookiedata cd[1];
506
507 fprintf(stderr, "test_cookies\n");
508
509 /* These are the examples from RFC2109 */
510 insist(!parse_cookie("$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"", cd));
511 insist(!strcmp(cd->version, "1"));
512 insist(cd->ncookies = 1);
513 insist(find_cookie(cd, "Customer") == &cd->cookies[0]);
514 check_string(cd->cookies[0].value, "WILE_E_COYOTE");
515 check_string(cd->cookies[0].path, "/acme");
516 insist(cd->cookies[0].domain == 0);
517 insist(!parse_cookie("$Version=\"1\";\n"
518 "Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\";\n"
519 "Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\"",
520 cd));
521 insist(cd->ncookies = 2);
522 insist(find_cookie(cd, "Customer") == &cd->cookies[0]);
523 insist(find_cookie(cd, "Part_Number") == &cd->cookies[1]);
524 check_string(cd->cookies[0].value, "WILE_E_COYOTE");
525 check_string(cd->cookies[0].path, "/acme");
526 insist(cd->cookies[0].domain == 0);
527 check_string(cd->cookies[1].value, "Rocket_Launcher_0001");
528 check_string(cd->cookies[1].path, "/acme");
529 insist(cd->cookies[1].domain == 0);
530 insist(!parse_cookie("$Version=\"1\";\n"
531 "Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\";\n"
532 "Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\";\n"
533 "Shipping=\"FedEx\"; $Path=\"/acme\"",
534 cd));
535 insist(cd->ncookies = 3);
536 insist(find_cookie(cd, "Customer") == &cd->cookies[0]);
537 insist(find_cookie(cd, "Part_Number") == &cd->cookies[1]);
538 insist(find_cookie(cd, "Shipping") == &cd->cookies[2]);
539 check_string(cd->cookies[0].value, "WILE_E_COYOTE");
540 check_string(cd->cookies[0].path, "/acme");
541 insist(cd->cookies[0].domain == 0);
542 check_string(cd->cookies[1].value, "Rocket_Launcher_0001");
543 check_string(cd->cookies[1].path, "/acme");
544 insist(cd->cookies[1].domain == 0);
545 check_string(cd->cookies[2].value, "FedEx");
546 check_string(cd->cookies[2].path, "/acme");
547 insist(cd->cookies[2].domain == 0);
548}
549
460b9539 550static void test_hex(void) {
551 unsigned n;
552 static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F };
553 uint8_t *u;
554 size_t ul;
555
033fd4e3
RK
556 fprintf(stderr, "test_hex\n");
557
460b9539 558 for(n = 0; n <= UCHAR_MAX; ++n) {
559 if(!isxdigit(n))
560 insist(unhexdigitq(n) == -1);
561 }
562 insist(unhexdigitq('0') == 0);
563 insist(unhexdigitq('1') == 1);
564 insist(unhexdigitq('2') == 2);
565 insist(unhexdigitq('3') == 3);
566 insist(unhexdigitq('4') == 4);
567 insist(unhexdigitq('5') == 5);
568 insist(unhexdigitq('6') == 6);
569 insist(unhexdigitq('7') == 7);
570 insist(unhexdigitq('8') == 8);
571 insist(unhexdigitq('9') == 9);
572 insist(unhexdigitq('a') == 10);
573 insist(unhexdigitq('b') == 11);
574 insist(unhexdigitq('c') == 12);
575 insist(unhexdigitq('d') == 13);
576 insist(unhexdigitq('e') == 14);
577 insist(unhexdigitq('f') == 15);
578 insist(unhexdigitq('A') == 10);
579 insist(unhexdigitq('B') == 11);
580 insist(unhexdigitq('C') == 12);
581 insist(unhexdigitq('D') == 13);
582 insist(unhexdigitq('E') == 14);
583 insist(unhexdigitq('F') == 15);
584 check_string(hex(h, sizeof h), "00ff807f");
585 check_string(hex(0, 0), "");
586 u = unhex("00ff807f", &ul);
587 insist(ul == 4);
588 insist(memcmp(u, h, 4) == 0);
589 u = unhex("00FF807F", &ul);
590 insist(ul == 4);
591 insist(memcmp(u, h, 4) == 0);
592 u = unhex("", &ul);
593 insist(ul == 0);
033fd4e3 594 fprintf(stderr, "2 ERROR reports expected {\n");
460b9539 595 insist(unhex("F", 0) == 0);
596 insist(unhex("az", 0) == 0);
033fd4e3 597 fprintf(stderr, "}\n");
460b9539 598}
599
600static void test_casefold(void) {
e5a5a138 601 uint32_t c, l;
56fd389c 602 const char *input, *canon_folded, *compat_folded, *canon_expected, *compat_expected;
460b9539 603
033fd4e3 604 fprintf(stderr, "test_casefold\n");
56fd389c
RK
605
606 /* This isn't a very exhaustive test. Unlike for normalization, there don't
607 * seem to be any public test vectors for these algorithms. */
e5a5a138 608
460b9539 609 for(c = 1; c < 256; ++c) {
e5a5a138 610 input = utf32_to_utf8(&c, 1, 0);
56fd389c
RK
611 canon_folded = utf8_casefold_canon(input, strlen(input), 0);
612 compat_folded = utf8_casefold_compat(input, strlen(input), 0);
460b9539 613 switch(c) {
614 default:
615 if((c >= 'A' && c <= 'Z')
616 || (c >= 0xC0 && c <= 0xDE && c != 0xD7))
617 l = c ^ 0x20;
618 else
619 l = c;
620 break;
621 case 0xB5: /* MICRO SIGN */
e5a5a138 622 l = 0x3BC; /* GREEK SMALL LETTER MU */
460b9539 623 break;
624 case 0xDF: /* LATIN SMALL LETTER SHARP S */
22896b25
RK
625 check_string(canon_folded, "ss");
626 check_string(compat_folded, "ss");
460b9539 627 l = 0;
628 break;
629 }
630 if(l) {
caecd4f4 631 uint32_t *d;
e5a5a138 632 /* Case-folded data is now normalized */
caecd4f4
RK
633 d = utf32_decompose_canon(&l, 1, 0);
634 canon_expected = utf32_to_utf8(d, utf32_len(d), 0);
56fd389c
RK
635 if(strcmp(canon_folded, canon_expected)) {
636 fprintf(stderr, "%s:%d: canon-casefolding %#lx got '%s', expected '%s'\n",
637 __FILE__, __LINE__, (unsigned long)c,
638 format(canon_folded), format(canon_expected));
bb48024f 639 count_error();
56fd389c
RK
640 }
641 ++tests;
caecd4f4
RK
642 d = utf32_decompose_compat(&l, 1, 0);
643 compat_expected = utf32_to_utf8(d, utf32_len(d), 0);
56fd389c
RK
644 if(strcmp(compat_folded, compat_expected)) {
645 fprintf(stderr, "%s:%d: compat-casefolding %#lx got '%s', expected '%s'\n",
460b9539 646 __FILE__, __LINE__, (unsigned long)c,
56fd389c 647 format(compat_folded), format(compat_expected));
bb48024f 648 count_error();
460b9539 649 }
650 ++tests;
651 }
652 }
c85b7022 653 check_string(utf8_casefold_canon("", 0, 0), "");
460b9539 654}
655
8818b7fc
RK
656struct {
657 const char *in;
658 const char *expect[10];
659} wtest[] = {
660 /* Empty string */
661 { "", { 0 } },
662 /* Only whitespace and punctuation */
663 { " ", { 0 } },
664 { " ' ", { 0 } },
665 { " ! ", { 0 } },
666 { " \"\" ", { 0 } },
667 { " @ ", { 0 } },
668 /* Basics */
669 { "wibble", { "wibble", 0 } },
670 { " wibble", { "wibble", 0 } },
671 { " wibble ", { "wibble", 0 } },
672 { "wibble ", { "wibble", 0 } },
673 { "wibble spong", { "wibble", "spong", 0 } },
674 { " wibble spong", { "wibble", "spong", 0 } },
675 { " wibble spong ", { "wibble", "spong", 0 } },
676 { "wibble spong ", { "wibble", "spong", 0 } },
677 { "wibble spong splat foo zot ", { "wibble", "spong", "splat", "foo", "zot", 0 } },
678 /* Apostrophes */
679 { "wibble 'spong", { "wibble", "spong", 0 } },
680 { " wibble's", { "wibble's", 0 } },
681 { " wibblespong' ", { "wibblespong", 0 } },
682 { "wibble sp''ong ", { "wibble", "sp", "ong", 0 } },
683};
684#define NWTEST (sizeof wtest / sizeof *wtest)
685
686static void test_words(void) {
687 size_t t, nexpect, ngot, i;
688 int right;
689
690 fprintf(stderr, "test_words\n");
691 for(t = 0; t < NWTEST; ++t) {
c85b7022 692 char **got = utf8_word_split(wtest[t].in, strlen(wtest[t].in), &ngot, 0);
8818b7fc
RK
693
694 for(nexpect = 0; wtest[t].expect[nexpect]; ++nexpect)
695 ;
696 if(nexpect == ngot) {
697 for(i = 0; i < ngot; ++i)
698 if(strcmp(wtest[t].expect[i], got[i]))
699 break;
700 right = i == ngot;
701 } else
702 right = 0;
703 if(!right) {
704 fprintf(stderr, "word split %zu failed\n", t);
705 fprintf(stderr, "input: %s\n", wtest[t].in);
706 fprintf(stderr, " | %-30s | %-30s\n",
707 "expected", "got");
708 for(i = 0; i < nexpect || i < ngot; ++i) {
709 const char *e = i < nexpect ? wtest[t].expect[i] : "<none>";
710 const char *g = i < ngot ? got[i] : "<none>";
711 fprintf(stderr, " %2zu | %-30s | %-30s\n", i, e, g);
712 }
713 count_error();
714 }
715 ++tests;
716 }
717}
718
033fd4e3
RK
719/** @brief Less-than comparison function for integer heap */
720static inline int int_lt(int a, int b) { return a < b; }
721
dab22732
RK
722/** @struct iheap
723 * @brief A heap with @c int elements */
033fd4e3 724HEAP_TYPE(iheap, int, int_lt);
8e3fe3d8 725HEAP_DEFINE(iheap, int, int_lt);
033fd4e3
RK
726
727/** @brief Tests for @ref heap.h */
728static void test_heap(void) {
729 struct iheap h[1];
730 int n;
731 int last = -1;
732
733 fprintf(stderr, "test_heap\n");
734
735 iheap_init(h);
736 for(n = 0; n < 1000; ++n)
737 iheap_insert(h, random() % 100);
738 for(n = 0; n < 1000; ++n) {
739 const int latest = iheap_remove(h);
740 if(last > latest)
741 fprintf(stderr, "should have %d <= %d\n", last, latest);
742 insist(last <= latest);
743 last = latest;
744 }
745 putchar('\n');
746}
747
e2452add
RK
748/** @brief Open a Unicode test file */
749static FILE *open_unicode_test(const char *path) {
750 const char *base;
751 FILE *fp;
752 char buffer[1024];
753 int w;
754
755 if((base = strrchr(path, '/')))
756 ++base;
757 else
758 base = path;
759 if(!(fp = fopen(base, "r"))) {
760 snprintf(buffer, sizeof buffer,
761 "wget http://www.unicode.org/Public/5.0.0/ucd/%s", path);
762 if((w = system(buffer)))
763 fatal(0, "%s: %s", buffer, wstat(w));
764 if(chmod(base, 0444) < 0)
765 fatal(errno, "chmod %s", base);
766 if(!(fp = fopen(base, "r")))
767 fatal(errno, "%s", base);
768 }
769 return fp;
770}
771
1625e11a 772/** @brief Run breaking tests for utf32_grapheme_boundary() etc */
bb48024f
RK
773static void breaktest(const char *path,
774 int (*breakfn)(const uint32_t *, size_t, size_t)) {
775 FILE *fp = open_unicode_test(path);
776 int lineno = 0;
777 char *l, *lp;
778 size_t bn, n;
779 char break_allowed[1024];
780 uint32_t buffer[1024];
781
782 while(!inputline(path, fp, &l, '\n')) {
783 ++lineno;
784 if(l[0] == '#') continue;
785 bn = 0;
786 lp = l;
787 while(*lp) {
788 if(*lp == ' ' || *lp == '\t') {
789 ++lp;
790 continue;
791 }
792 if(*lp == '#')
793 break;
794 if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0xB7) {
795 /* 00F7 DIVISION SIGN */
796 break_allowed[bn] = 1;
797 lp += 2;
798 continue;
799 }
800 if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0x97) {
801 /* 00D7 MULTIPLICATION SIGN */
802 break_allowed[bn] = 0;
803 lp += 2;
804 continue;
805 }
806 if(isxdigit((unsigned char)*lp)) {
807 buffer[bn++] = strtoul(lp, &lp, 16);
808 continue;
809 }
810 fatal(0, "%s:%d: evil line: %s", path, lineno, l);
811 }
812 for(n = 0; n <= bn; ++n) {
813 if(breakfn(buffer, bn, n) != break_allowed[n]) {
814 fprintf(stderr,
b21a155c
RK
815 "%s:%d: offset %zu: mismatch\n"
816 "%s\n"
817 "\n",
818 path, lineno, n, l);
bb48024f
RK
819 count_error();
820 }
821 ++tests;
822 }
823 xfree(l);
824 }
825 fclose(fp);
826}
827
e5a5a138
RK
828/** @brief Tests for @ref lib/unicode.h */
829static void test_unicode(void) {
830 FILE *fp;
831 int lineno = 0;
832 char *l, *lp;
833 uint32_t buffer[1024];
16506c9d 834 uint32_t *c[6], *NFD_c[6], *NFKD_c[6], *NFC_c[6], *NFKC_c[6]; /* 1-indexed */
e5a5a138
RK
835 int cn, bn;
836
837 fprintf(stderr, "test_unicode\n");
e2452add 838 fp = open_unicode_test("NormalizationTest.txt");
e5a5a138
RK
839 while(!inputline("NormalizationTest.txt", fp, &l, '\n')) {
840 ++lineno;
841 if(*l == '#' || *l == '@')
842 continue;
843 bn = 0;
844 cn = 1;
845 lp = l;
846 c[cn++] = &buffer[bn];
847 while(*lp && *lp != '#') {
848 if(*lp == ' ') {
849 ++lp;
850 continue;
851 }
852 if(*lp == ';') {
853 buffer[bn++] = 0;
854 if(cn == 6)
855 break;
856 c[cn++] = &buffer[bn];
857 ++lp;
858 continue;
859 }
860 buffer[bn++] = strtoul(lp, &lp, 16);
861 }
862 buffer[bn] = 0;
863 assert(cn == 6);
864 for(cn = 1; cn <= 5; ++cn) {
865 NFD_c[cn] = utf32_decompose_canon(c[cn], utf32_len(c[cn]), 0);
866 NFKD_c[cn] = utf32_decompose_compat(c[cn], utf32_len(c[cn]), 0);
16506c9d
RK
867 NFC_c[cn] = utf32_compose_canon(c[cn], utf32_len(c[cn]), 0);
868 NFKC_c[cn] = utf32_compose_compat(c[cn], utf32_len(c[cn]), 0);
e5a5a138
RK
869 }
870#define unt_check(T, A, B) do { \
871 ++tests; \
872 if(utf32_cmp(c[A], T##_c[B])) { \
e2452add
RK
873 fprintf(stderr, \
874 "NormalizationTest.txt:%d: c%d != "#T"(c%d)\n", \
875 lineno, A, B); \
16506c9d 876 fprintf(stderr, " c%d:%s\n", \
e5a5a138 877 A, format_utf32(c[A])); \
16506c9d
RK
878 fprintf(stderr, " c%d:%s\n", \
879 B, format_utf32(c[B])); \
880 fprintf(stderr, "%4s(c%d):%s\n", \
e5a5a138 881 #T, B, format_utf32(T##_c[B])); \
bcf9ed7f 882 count_error(); \
e5a5a138
RK
883 } \
884 } while(0)
885 unt_check(NFD, 3, 1);
886 unt_check(NFD, 3, 2);
887 unt_check(NFD, 3, 3);
888 unt_check(NFD, 5, 4);
889 unt_check(NFD, 5, 5);
890 unt_check(NFKD, 5, 1);
891 unt_check(NFKD, 5, 2);
892 unt_check(NFKD, 5, 3);
893 unt_check(NFKD, 5, 4);
894 unt_check(NFKD, 5, 5);
16506c9d
RK
895 unt_check(NFC, 2, 1);
896 unt_check(NFC, 2, 2);
897 unt_check(NFC, 2, 3);
898 unt_check(NFC, 4, 4);
899 unt_check(NFC, 4, 5);
900 unt_check(NFKC, 4, 1);
901 unt_check(NFKC, 4, 2);
902 unt_check(NFKC, 4, 3);
903 unt_check(NFKC, 4, 4);
904 unt_check(NFKC, 4, 5);
e5a5a138
RK
905 for(cn = 1; cn <= 5; ++cn) {
906 xfree(NFD_c[cn]);
907 xfree(NFKD_c[cn]);
908 }
909 xfree(l);
910 }
e2452add 911 fclose(fp);
1625e11a 912 breaktest("auxiliary/GraphemeBreakTest.txt", utf32_is_grapheme_boundary);
bb48024f 913 breaktest("auxiliary/WordBreakTest.txt", utf32_is_word_boundary);
c35e83d9
RK
914 insist(utf32_combining_class(0x40000) == 0);
915 insist(utf32_combining_class(0xE0000) == 0);
e5a5a138
RK
916}
917
ea387d53
RK
918static void test_signame(void) {
919 fprintf(stderr, "test_signame\n");
920 insist(find_signal("SIGTERM") == SIGTERM);
921 insist(find_signal("SIGHUP") == SIGHUP);
922 insist(find_signal("SIGINT") == SIGINT);
923 insist(find_signal("SIGQUIT") == SIGQUIT);
924 insist(find_signal("SIGKILL") == SIGKILL);
925 insist(find_signal("SIGYOURMUM") == -1);
926}
927
9f28e855
RK
928static void test_cache(void) {
929 const struct cache_type t1 = { 1 }, t2 = { 10 };
930 const char v11[] = "spong", v12[] = "wibble", v2[] = "blat";
931 fprintf(stderr, "test_cache\n");
932 cache_put(&t1, "1_1", v11);
933 cache_put(&t1, "1_2", v12);
934 cache_put(&t2, "2", v2);
935 insist(cache_count() == 3);
936 insist(cache_get(&t2, "2") == v2);
937 insist(cache_get(&t1, "1_1") == v11);
938 insist(cache_get(&t1, "1_2") == v12);
939 insist(cache_get(&t1, "2") == 0);
940 insist(cache_get(&t2, "1_1") == 0);
941 insist(cache_get(&t2, "1_2") == 0);
942 insist(cache_get(&t1, "2") == 0);
943 insist(cache_get(&t2, "1_1") == 0);
944 insist(cache_get(&t2, "1_2") == 0);
945 sleep(2);
946 cache_expire();
947 insist(cache_count() == 1);
948 insist(cache_get(&t1, "1_1") == 0);
949 insist(cache_get(&t1, "1_2") == 0);
950 insist(cache_get(&t2, "2") == v2);
951 cache_clean(0);
952 insist(cache_count() == 0);
953 insist(cache_get(&t2, "2") == 0);
954}
955
00e36cd0
RK
956static void test_filepart(void) {
957 fprintf(stderr, "test_filepart\n");
958 check_string(d_dirname("/"), "/");
011e0b89 959 check_string(d_dirname("////"), "/");
00e36cd0 960 check_string(d_dirname("/spong"), "/");
011e0b89 961 check_string(d_dirname("////spong"), "/");
00e36cd0 962 check_string(d_dirname("/foo/bar"), "/foo");
011e0b89 963 check_string(d_dirname("////foo/////bar"), "////foo");
00e36cd0 964 check_string(d_dirname("./bar"), ".");
011e0b89 965 check_string(d_dirname(".//bar"), ".");
00e36cd0
RK
966 check_string(d_dirname("."), ".");
967 check_string(d_dirname(".."), ".");
968 check_string(d_dirname("../blat"), "..");
011e0b89 969 check_string(d_dirname("..//blat"), "..");
00e36cd0
RK
970 check_string(d_dirname("wibble"), ".");
971 check_string(extension("foo.c"), ".c");
972 check_string(extension(".c"), ".c");
973 check_string(extension("."), ".");
974 check_string(extension("foo"), "");
975 check_string(extension("./foo"), "");
976 check_string(extension("./foo.c"), ".c");
011e0b89
RK
977 check_string(strip_extension("foo.c"), "foo");
978 check_string(strip_extension("foo.mp3"), "foo");
979 check_string(strip_extension("foo.---"), "foo.---");
980 check_string(strip_extension("foo.---xyz"), "foo.---xyz");
981 check_string(strip_extension("foo.bar/wibble.spong"), "foo.bar/wibble");
00e36cd0
RK
982}
983
65bb0fff
RK
984static void test_selection(void) {
985 hash *h;
986 fprintf(stderr, "test_selection\n");
987 insist((h = selection_new()) != 0);
988 selection_set(h, "one", 1);
989 selection_set(h, "two", 1);
990 selection_set(h, "three", 0);
991 selection_set(h, "four", 1);
992 insist(selection_selected(h, "one") == 1);
993 insist(selection_selected(h, "two") == 1);
994 insist(selection_selected(h, "three") == 0);
995 insist(selection_selected(h, "four") == 1);
996 insist(selection_selected(h, "five") == 0);
997 insist(hash_count(h) == 3);
998 selection_flip(h, "one");
999 selection_flip(h, "three");
1000 insist(selection_selected(h, "one") == 0);
1001 insist(selection_selected(h, "three") == 1);
1002 insist(hash_count(h) == 3);
1003 selection_live(h, "one");
1004 selection_live(h, "two");
1005 selection_live(h, "three");
1006 selection_cleanup(h);
1007 insist(selection_selected(h, "one") == 0);
1008 insist(selection_selected(h, "two") == 1);
1009 insist(selection_selected(h, "three") == 1);
1010 insist(selection_selected(h, "four") == 0);
1011 insist(selection_selected(h, "five") == 0);
1012 insist(hash_count(h) == 2);
1013 selection_empty(h);
1014 insist(selection_selected(h, "one") == 0);
1015 insist(selection_selected(h, "two") == 0);
1016 insist(selection_selected(h, "three") == 0);
1017 insist(selection_selected(h, "four") == 0);
1018 insist(selection_selected(h, "five") == 0);
1019 insist(hash_count(h) == 0);
1020}
1021
71b90230
RK
1022static void test_wstat(void) {
1023 pid_t pid;
1024 int w;
1025
1026 fprintf(stderr, "test_wstat\n");
1027 if(!(pid = xfork())) {
1028 _exit(1);
1029 }
1030 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
1031 ;
1032 check_string(wstat(w), "exited with status 1");
1033 if(!(pid = xfork())) {
1034 kill(getpid(), SIGTERM);
1035 _exit(-1);
1036 }
1037 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
1038 ;
1039 check_string_prefix(wstat(w), "terminated by signal 15");
1040}
1041
22f61603
RK
1042static void test_kvp(void) {
1043 struct kvp *k;
632637c7 1044 size_t n;
22f61603
RK
1045
1046 fprintf(stderr, "test_kvp\n");
632637c7 1047 /* decoding */
22f61603
RK
1048#define KVP_URLDECODE(S) kvp_urldecode((S), strlen(S))
1049 insist(KVP_URLDECODE("=%zz") == 0);
1050 insist(KVP_URLDECODE("=%0") == 0);
1051 insist(KVP_URLDECODE("=%0z") == 0);
1052 insist(KVP_URLDECODE("=%%") == 0);
1053 insist(KVP_URLDECODE("==%") == 0);
1054 insist(KVP_URLDECODE("wibble") == 0);
1055 insist(KVP_URLDECODE("") == 0);
1056 insist(KVP_URLDECODE("wibble&") == 0);
1057 insist((k = KVP_URLDECODE("one=bl%61t+foo")) != 0);
1058 check_string(kvp_get(k, "one"), "blat foo");
1059 insist(kvp_get(k, "ONE") == 0);
1060 insist(k->next == 0);
1061 insist((k = KVP_URLDECODE("wibble=splat&bar=spong")) != 0);
1062 check_string(kvp_get(k, "wibble"), "splat");
1063 check_string(kvp_get(k, "bar"), "spong");
1064 insist(kvp_get(k, "ONE") == 0);
1065 insist(k->next->next == 0);
632637c7
RK
1066 /* encoding */
1067 insist(kvp_set(&k, "bar", "spong") == 0);
1068 insist(kvp_set(&k, "bar", "foo") == 1);
1069 insist(kvp_set(&k, "zog", "%") == 1);
1070 insist(kvp_set(&k, "wibble", 0) == 1);
1071 insist(kvp_set(&k, "wibble", 0) == 0);
1072 check_string(kvp_urlencode(k, 0),
1073 "bar=foo&zog=%25");
1074 check_string(kvp_urlencode(k, &n),
1075 "bar=foo&zog=%25");
1076 insist(n == strlen("bar=foo&zog=%25"));
1077 check_string(urlencodestring("abc% +\n"),
1078 "abc%25%20%2b%0a");
22f61603
RK
1079}
1080
6049fe0e
RK
1081static void test_sink(void) {
1082 struct sink *s;
1083 struct dynstr d[1];
1084 FILE *fp;
1085 char *l;
1086
1087 fprintf(stderr, "test_sink\n");
1088
1089 fp = tmpfile();
1090 assert(fp != 0);
1091 s = sink_stdio("tmpfile", fp);
1092 insist(sink_printf(s, "test: %d\n", 999) == 10);
1093 insist(sink_printf(s, "wibble: %s\n", "foobar") == 15);
1094 rewind(fp);
1095 insist(inputline("tmpfile", fp, &l, '\n') == 0);
1096 check_string(l, "test: 999");
1097 insist(inputline("tmpfile", fp, &l, '\n') == 0);
1098 check_string(l, "wibble: foobar");
1099 insist(inputline("tmpfile", fp, &l, '\n') == -1);
1100
1101 dynstr_init(d);
1102 s = sink_dynstr(d);
1103 insist(sink_printf(s, "test: %d\n", 999) == 10);
1104 insist(sink_printf(s, "wibble: %s\n", "foobar") == 15);
1105 dynstr_terminate(d);
1106 check_string(d->vec, "test: 999\nwibble: foobar\n");
1107}
1108
0c740e59
RK
1109static const char *do_printf(const char *fmt, ...) {
1110 va_list ap;
1111 char *s;
1112 int rc;
1113
1114 va_start(ap, fmt);
1115 rc = byte_vasprintf(&s, fmt, ap);
1116 va_end(ap);
1117 if(rc < 0)
1118 return 0;
1119 return s;
1120}
1121
1122static void test_printf(void) {
1123 char c;
1124 short s;
1125 int i;
1126 long l;
1127 long long ll;
1128 intmax_t m;
1129 ssize_t ssz;
1130 ptrdiff_t p;
011e0b89
RK
1131 char *cp;
1132 char buffer[16];
0c740e59
RK
1133
1134 fprintf(stderr, "test_printf\n");
1135 check_string(do_printf("%d", 999), "999");
1136 check_string(do_printf("%d", -999), "-999");
1137 check_string(do_printf("%i", 999), "999");
1138 check_string(do_printf("%i", -999), "-999");
1139 check_string(do_printf("%u", 999), "999");
1140 check_string(do_printf("%2u", 999), "999");
1141 check_string(do_printf("%10u", 999), " 999");
1142 check_string(do_printf("%-10u", 999), "999 ");
1143 check_string(do_printf("%010u", 999), "0000000999");
1144 check_string(do_printf("%-10d", -999), "-999 ");
1145 check_string(do_printf("%-010d", -999), "-999 "); /* "-" beats "0" */
1146 check_string(do_printf("%66u", 999), " 999");
1147 check_string(do_printf("%o", 999), "1747");
1148 check_string(do_printf("%#o", 999), "01747");
1149 check_string(do_printf("%#o", 0), "0");
1150 check_string(do_printf("%x", 999), "3e7");
1151 check_string(do_printf("%#x", 999), "0x3e7");
1152 check_string(do_printf("%#X", 999), "0X3E7");
1153 check_string(do_printf("%#x", 0), "0");
1154 check_string(do_printf("%hd", (short)999), "999");
1155 check_string(do_printf("%hhd", (short)99), "99");
1156 check_string(do_printf("%ld", 100000L), "100000");
1157 check_string(do_printf("%lld", 10000000000LL), "10000000000");
1158 check_string(do_printf("%qd", 10000000000LL), "10000000000");
1159 check_string(do_printf("%jd", (intmax_t)10000000000LL), "10000000000");
1160 check_string(do_printf("%zd", (ssize_t)2000000000), "2000000000");
1161 check_string(do_printf("%td", (ptrdiff_t)2000000000), "2000000000");
1162 check_string(do_printf("%hu", (short)999), "999");
1163 check_string(do_printf("%hhu", (short)99), "99");
1164 check_string(do_printf("%lu", 100000L), "100000");
1165 check_string(do_printf("%llu", 10000000000LL), "10000000000");
1166 check_string(do_printf("%ju", (uintmax_t)10000000000LL), "10000000000");
1167 check_string(do_printf("%zu", (size_t)2000000000), "2000000000");
1168 check_string(do_printf("%tu", (ptrdiff_t)2000000000), "2000000000");
1169 check_string(do_printf("%p", (void *)0x100), "0x100");
1170 check_string(do_printf("%s", "wibble"), "wibble");
1171 check_string(do_printf("%s-%s", "wibble", "wobble"), "wibble-wobble");
1172 check_string(do_printf("%10s", "wibble"), " wibble");
1173 check_string(do_printf("%010s", "wibble"), " wibble"); /* 0 ignored for %s */
1174 check_string(do_printf("%-10s", "wibble"), "wibble ");
1175 check_string(do_printf("%2s", "wibble"), "wibble");
1176 check_string(do_printf("%.2s", "wibble"), "wi");
1177 check_string(do_printf("%.2s", "w"), "w");
1178 check_string(do_printf("%4.2s", "wibble"), " wi");
1179 check_string(do_printf("%c", 'a'), "a");
1180 check_string(do_printf("%4c", 'a'), " a");
1181 check_string(do_printf("%-4c", 'a'), "a ");
1182 check_string(do_printf("%*c", 0, 'a'), "a");
1183 check_string(do_printf("x%hhny", &c), "xy");
1184 insist(c == 1);
1185 check_string(do_printf("xx%hnyy", &s), "xxyy");
1186 insist(s == 2);
1187 check_string(do_printf("xxx%nyyy", &i), "xxxyyy");
1188 insist(i == 3);
1189 check_string(do_printf("xxxx%lnyyyy", &l), "xxxxyyyy");
1190 insist(l == 4);
1191 check_string(do_printf("xxxxx%llnyyyyy", &ll), "xxxxxyyyyy");
1192 insist(ll == 5);
1193 check_string(do_printf("xxxxxx%jnyyyyyy", &m), "xxxxxxyyyyyy");
1194 insist(m == 6);
1195 check_string(do_printf("xxxxxxx%znyyyyyyy", &ssz), "xxxxxxxyyyyyyy");
1196 insist(ssz == 7);
1197 check_string(do_printf("xxxxxxxx%tnyyyyyyyy", &p), "xxxxxxxxyyyyyyyy");
1198 insist(p == 8);
1199 check_string(do_printf("%*d", 5, 99), " 99");
1200 check_string(do_printf("%*d", -5, 99), "99 ");
1201 check_string(do_printf("%.*d", 5, 99), "00099");
1202 check_string(do_printf("%.*d", -5, 99), "99");
1203 check_string(do_printf("%.0d", 0), "");
1204 check_string(do_printf("%.d", 0), "");
1205 check_string(do_printf("%.d", 0), "");
1206 check_string(do_printf("%%"), "%");
1207 check_string(do_printf("wibble"), "wibble");
1208 insist(do_printf("%") == 0);
1209 insist(do_printf("%=") == 0);
011e0b89
RK
1210 i = byte_asprintf(&cp, "xyzzy %d", 999);
1211 insist(i == 9);
1212 check_string(cp, "xyzzy 999");
1213 i = byte_snprintf(buffer, sizeof buffer, "xyzzy %d", 999);
1214 insist(i == 9);
1215 check_string(buffer, "xyzzy 999");
1216 i = byte_snprintf(buffer, sizeof buffer, "%*d", 32, 99);
1217 insist(i == 32);
1218 check_string(buffer, " ");
1219 {
1220 /* bizarre workaround for compiler checking of format strings */
1221 char f[] = "xyzzy %";
1222 i = byte_asprintf(&cp, f);
1223 insist(i == -1);
1224 }
0c740e59
RK
1225}
1226
abe28bfe
RK
1227static void test_basen(void) {
1228 unsigned long v[64];
1229 char buffer[1024];
1230
1231 fprintf(stderr, "test_basen\n");
1232 v[0] = 999;
1233 insist(basen(v, 1, buffer, sizeof buffer, 10) == 0);
1234 check_string(buffer, "999");
1235
1236 v[0] = 1+2*7+3*7*7+4*7*7*7;
1237 insist(basen(v, 1, buffer, sizeof buffer, 7) == 0);
1238 check_string(buffer, "4321");
1239
1240 v[0] = 0x00010203;
1241 v[1] = 0x04050607;
1242 v[2] = 0x08090A0B;
1243 v[3] = 0x0C0D0E0F;
1244 insist(basen(v, 4, buffer, sizeof buffer, 256) == 0);
1245 check_string(buffer, "123456789abcdef");
1246
1247 v[0] = 0x00010203;
1248 v[1] = 0x04050607;
1249 v[2] = 0x08090A0B;
1250 v[3] = 0x0C0D0E0F;
1251 insist(basen(v, 4, buffer, sizeof buffer, 16) == 0);
1252 check_string(buffer, "102030405060708090a0b0c0d0e0f");
1253
1254 v[0] = 0x00010203;
1255 v[1] = 0x04050607;
1256 v[2] = 0x08090A0B;
1257 v[3] = 0x0C0D0E0F;
1258 insist(basen(v, 4, buffer, 10, 16) == -1);
1259}
1260
5e49fa7f
RK
1261static void test_split(void) {
1262 char **v;
1263 int nv;
1264
1265 fprintf(stderr, "test_split\n");
1266 insist(split("\"misquoted", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1267 insist(split("\'misquoted", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1268 insist(split("\'misquoted\\", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1269 insist(split("\'misquoted\\\"", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1270 insist(split("\'mis\\escaped\'", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1271
1272 insist((v = split("", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1273 check_integer(nv, 0);
1274 insist(*v == 0);
1275
1276 insist((v = split("wibble", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1277 check_integer(nv, 1);
1278 check_string(v[0], "wibble");
1279 insist(v[1] == 0);
1280
1281 insist((v = split(" wibble \t\r\n wobble ", &nv,
1282 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1283 check_integer(nv, 2);
1284 check_string(v[0], "wibble");
1285 check_string(v[1], "wobble");
1286 insist(v[2] == 0);
1287
1288 insist((v = split("wibble wobble #splat", &nv,
1289 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1290 check_integer(nv, 2);
1291 check_string(v[0], "wibble");
1292 check_string(v[1], "wobble");
1293 insist(v[2] == 0);
1294
1295 insist((v = split("\"wibble wobble\" #splat", &nv,
1296 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1297 check_integer(nv, 1);
1298 check_string(v[0], "wibble wobble");
1299 insist(v[1] == 0);
1300
1301 insist((v = split("\"wibble \\\"\\nwobble\"", &nv,
1302 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1303 check_integer(nv, 1);
1304 check_string(v[0], "wibble \"\nwobble");
1305 insist(v[1] == 0);
1306
1307 insist((v = split("\"wibble wobble\" #splat", &nv,
1308 SPLIT_QUOTES, 0, 0)));
1309 check_integer(nv, 2);
1310 check_string(v[0], "wibble wobble");
1311 check_string(v[1], "#splat");
1312 insist(v[2] == 0);
1313
1314 insist((v = split("\"wibble wobble\" #splat", &nv,
1315 SPLIT_COMMENTS, 0, 0)));
1316 check_integer(nv, 2);
1317 check_string(v[0], "\"wibble");
1318 check_string(v[1], "wobble\"");
1319 insist(v[2] == 0);
1320
1321 check_string(quoteutf8("wibble"), "wibble");
1322 check_string(quoteutf8(" wibble "), "\" wibble \"");
1323 check_string(quoteutf8("wibble wobble"), "\"wibble wobble\"");
1324 check_string(quoteutf8("wibble\"wobble"), "\"wibble\\\"wobble\"");
1325 check_string(quoteutf8("wibble\nwobble"), "\"wibble\\nwobble\"");
1326 check_string(quoteutf8("wibble\\wobble"), "\"wibble\\\\wobble\"");
1327 check_string(quoteutf8("wibble'wobble"), "\"wibble'wobble\"");
1328}
1329
fe575537
RK
1330static void test_hash(void) {
1331 hash *h;
1332 int i, *ip;
1333 char **keys;
1334
1335 fprintf(stderr, "test_hash\n");
1336 h = hash_new(sizeof(int));
1337 for(i = 0; i < 10000; ++i)
1338 insist(hash_add(h, do_printf("%d", i), &i, HASH_INSERT) == 0);
1339 check_integer(hash_count(h), 10000);
1340 for(i = 0; i < 10000; ++i) {
1341 insist((ip = hash_find(h, do_printf("%d", i))) != 0);
1342 check_integer(*ip, i);
1343 insist(hash_add(h, do_printf("%d", i), &i, HASH_REPLACE) == 0);
1344 }
1345 check_integer(hash_count(h), 10000);
1346 keys = hash_keys(h);
1347 for(i = 0; i < 10000; ++i)
1348 insist(keys[i] != 0);
1349 insist(keys[10000] == 0);
1350 for(i = 0; i < 10000; ++i)
1351 insist(hash_remove(h, do_printf("%d", i)) == 0);
1352 check_integer(hash_count(h), 0);
1353}
1354
d6ea854a
RK
1355static void test_addr(void) {
1356 struct stringlist a;
1357 const char *s[2];
1358 struct addrinfo *ai;
1359 char *name;
1360 const struct sockaddr_in *sin;
1361
1362 static const struct addrinfo pref = {
1363 AI_PASSIVE,
1364 PF_INET,
1365 SOCK_STREAM,
1366 0,
1367 0,
1368 0,
1369 0,
1370 0
1371 };
1372
1373 a.n = 1;
1374 a.s = (char **)s;
1375 s[0] = "smtp";
1376 ai = get_address(&a, &pref, &name);
1377 insist(ai != 0);
1378 check_integer(ai->ai_family, PF_INET);
1379 check_integer(ai->ai_socktype, SOCK_STREAM);
1380 check_integer(ai->ai_protocol, IPPROTO_TCP);
1381 check_integer(ai->ai_addrlen, sizeof(struct sockaddr_in));
1382 sin = (const struct sockaddr_in *)ai->ai_addr;
1383 check_integer(sin->sin_family, AF_INET);
1384 check_integer(sin->sin_addr.s_addr, 0);
1385 check_integer(ntohs(sin->sin_port), 25);
1386 check_string(name, "host * service smtp");
1387
1388 a.n = 2;
1389 s[0] = "localhost";
1390 s[1] = "nntp";
1391 ai = get_address(&a, &pref, &name);
1392 insist(ai != 0);
1393 check_integer(ai->ai_family, PF_INET);
1394 check_integer(ai->ai_socktype, SOCK_STREAM);
1395 check_integer(ai->ai_protocol, IPPROTO_TCP);
1396 check_integer(ai->ai_addrlen, sizeof(struct sockaddr_in));
1397 sin = (const struct sockaddr_in *)ai->ai_addr;
1398 check_integer(sin->sin_family, AF_INET);
1399 check_integer(ntohl(sin->sin_addr.s_addr), 0x7F000001);
1400 check_integer(ntohs(sin->sin_port), 119);
1401 check_string(name, "host localhost service nntp");
1402}
1403
460b9539 1404int main(void) {
fe575537 1405 mem_init();
bb48024f 1406 fail_first = !!getenv("FAIL_FIRST");
460b9539 1407 insist('\n' == 0x0A);
1408 insist('\r' == 0x0D);
1409 insist(' ' == 0x20);
1410 insist('0' == 0x30);
1411 insist('9' == 0x39);
1412 insist('A' == 0x41);
1413 insist('Z' == 0x5A);
1414 insist('a' == 0x61);
1415 insist('z' == 0x7A);
1416 /* addr.c */
d6ea854a 1417 test_addr();
460b9539 1418 /* asprintf.c */
1419 /* authhash.c */
1420 /* basen.c */
abe28bfe 1421 test_basen();
460b9539 1422 /* charset.c */
1423 /* client.c */
1424 /* configuration.c */
1425 /* event.c */
00e36cd0
RK
1426 /* filepart.c */
1427 test_filepart();
460b9539 1428 /* fprintf.c */
033fd4e3
RK
1429 /* heap.c */
1430 test_heap();
460b9539 1431 /* hex.c */
1432 test_hex();
1433 /* inputline.c */
1434 /* kvp.c */
22f61603 1435 test_kvp();
460b9539 1436 /* log.c */
1437 /* mem.c */
1438 /* mime.c */
1439 test_mime();
39d4aa6b 1440 test_cookies();
460b9539 1441 /* mixer.c */
1442 /* plugin.c */
1443 /* printf.c */
0c740e59 1444 test_printf();
460b9539 1445 /* queue.c */
1446 /* sink.c */
6049fe0e 1447 test_sink();
460b9539 1448 /* snprintf.c */
1449 /* split.c */
5e49fa7f 1450 test_split();
460b9539 1451 /* syscalls.c */
1452 /* table.c */
e5a5a138
RK
1453 /* unicode.c */
1454 test_unicode();
460b9539 1455 /* utf8.c */
1456 test_utf8();
1457 /* vector.c */
1458 /* words.c */
1459 test_casefold();
8818b7fc 1460 test_words();
460b9539 1461 /* wstat.c */
71b90230 1462 test_wstat();
ea387d53
RK
1463 /* signame.c */
1464 test_signame();
9f28e855
RK
1465 /* cache.c */
1466 test_cache();
65bb0fff
RK
1467 /* selection.c */
1468 test_selection();
fe575537 1469 test_hash();
460b9539 1470 fprintf(stderr, "%d errors out of %d tests\n", errors, tests);
1471 return !!errors;
1472}
1473
1474/*
1475Local Variables:
1476c-basic-offset:2
1477comment-column:40
56fd389c
RK
1478fill-column:79
1479indent-tabs-mode:nil
460b9539 1480End:
1481*/