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