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