Commit | Line | Data |
---|---|---|
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> |
460b9539 | 35 | |
36 | #include "utf8.h" | |
37 | #include "mem.h" | |
38 | #include "log.h" | |
39 | #include "vector.h" | |
40 | #include "charset.h" | |
41 | #include "mime.h" | |
42 | #include "hex.h" | |
033fd4e3 | 43 | #include "heap.h" |
e5a5a138 RK |
44 | #include "unicode.h" |
45 | #include "inputline.h" | |
e2452add | 46 | #include "wstat.h" |
ea387d53 | 47 | #include "signame.h" |
9f28e855 | 48 | #include "cache.h" |
00e36cd0 | 49 | #include "filepart.h" |
65bb0fff RK |
50 | #include "hash.h" |
51 | #include "selection.h" | |
71b90230 | 52 | #include "syscalls.h" |
22f61603 | 53 | #include "kvp.h" |
460b9539 | 54 | |
55 | static int tests, errors; | |
bb48024f RK |
56 | static int fail_first; |
57 | ||
58 | static void count_error() { | |
59 | ++errors; | |
60 | if(fail_first) | |
61 | abort(); | |
62 | } | |
460b9539 | 63 | |
033fd4e3 | 64 | /** @brief Checks that @p expr is nonzero */ |
460b9539 | 65 | #define insist(expr) do { \ |
033fd4e3 | 66 | if(!(expr)) { \ |
bb48024f | 67 | count_error(); \ |
460b9539 | 68 | fprintf(stderr, "%s:%d: error checking %s\n", \ |
69 | __FILE__, __LINE__, #expr); \ | |
70 | } \ | |
71 | ++tests; \ | |
72 | } while(0) | |
73 | ||
74 | static const char *format(const char *s) { | |
75 | struct dynstr d; | |
76 | int c; | |
77 | char buf[10]; | |
78 | ||
79 | dynstr_init(&d); | |
80 | while((c = (unsigned char)*s++)) { | |
81 | if(c >= ' ' && c <= '~') | |
82 | dynstr_append(&d, c); | |
83 | else { | |
84 | sprintf(buf, "\\x%02X", (unsigned)c); | |
85 | dynstr_append_string(&d, buf); | |
86 | } | |
87 | } | |
88 | dynstr_terminate(&d); | |
89 | return d.vec; | |
90 | } | |
91 | ||
e5a5a138 RK |
92 | static const char *format_utf32(const uint32_t *s) { |
93 | struct dynstr d; | |
94 | uint32_t c; | |
95 | char buf[64]; | |
96 | ||
97 | dynstr_init(&d); | |
98 | while((c = *s++)) { | |
16506c9d RK |
99 | sprintf(buf, " %04lX", (long)c); |
100 | dynstr_append_string(&d, buf); | |
e5a5a138 RK |
101 | } |
102 | dynstr_terminate(&d); | |
103 | return d.vec; | |
104 | } | |
105 | ||
460b9539 | 106 | #define check_string(GOT, WANT) do { \ |
107 | const char *g = GOT; \ | |
108 | const char *w = WANT; \ | |
109 | \ | |
110 | if(w == 0) { \ | |
111 | fprintf(stderr, "%s:%d: %s returned 0\n", \ | |
112 | __FILE__, __LINE__, #GOT); \ | |
71b90230 | 113 | count_error(); \ |
460b9539 | 114 | } else if(strcmp(w, g)) { \ |
115 | fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \ | |
116 | __FILE__, __LINE__, #GOT, format(g), format(w)); \ | |
71b90230 | 117 | count_error(); \ |
460b9539 | 118 | } \ |
119 | ++tests; \ | |
120 | } while(0) | |
121 | ||
71b90230 RK |
122 | #define check_string_prefix(GOT, WANT) do { \ |
123 | const char *g = GOT; \ | |
124 | const char *w = WANT; \ | |
125 | \ | |
126 | if(w == 0) { \ | |
127 | fprintf(stderr, "%s:%d: %s returned 0\n", \ | |
128 | __FILE__, __LINE__, #GOT); \ | |
129 | count_error(); \ | |
130 | } else if(strncmp(w, g, strlen(w))) { \ | |
131 | fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n", \ | |
132 | __FILE__, __LINE__, #GOT, format(g), format(w)); \ | |
133 | count_error(); \ | |
134 | } \ | |
135 | ++tests; \ | |
136 | } while(0) | |
137 | ||
460b9539 | 138 | static uint32_t *ucs4parse(const char *s) { |
139 | struct dynstr_ucs4 d; | |
140 | char *e; | |
141 | ||
142 | dynstr_ucs4_init(&d); | |
143 | while(*s) { | |
144 | errno = 0; | |
145 | dynstr_ucs4_append(&d, strtoul(s, &e, 0)); | |
146 | if(errno) fatal(errno, "strtoul (%s)", s); | |
147 | s = e; | |
148 | } | |
149 | dynstr_ucs4_terminate(&d); | |
150 | return d.vec; | |
151 | } | |
152 | ||
153 | static void test_utf8(void) { | |
154 | /* Test validutf8, convert to UCS-4, check the answer is right, | |
155 | * convert back to UTF-8, check we got to where we started */ | |
156 | #define U8(CHARS, WORDS) do { \ | |
157 | uint32_t *w = ucs4parse(WORDS); \ | |
158 | uint32_t *ucs; \ | |
159 | char *u8; \ | |
160 | \ | |
161 | insist(validutf8(CHARS)); \ | |
caecd4f4 | 162 | ucs = utf8_to_utf32(CHARS, strlen(CHARS), 0); \ |
460b9539 | 163 | insist(ucs != 0); \ |
caecd4f4 RK |
164 | insist(!utf32_cmp(w, ucs)); \ |
165 | u8 = utf32_to_utf8(ucs, utf32_len(ucs), 0); \ | |
460b9539 | 166 | insist(u8 != 0); \ |
167 | insist(!strcmp(u8, CHARS)); \ | |
168 | } while(0) | |
169 | ||
033fd4e3 | 170 | fprintf(stderr, "test_utf8\n"); |
18cda350 | 171 | #define validutf8(S) utf8_valid((S), strlen(S)) |
033fd4e3 | 172 | |
460b9539 | 173 | /* empty string */ |
174 | ||
175 | U8("", ""); | |
176 | ||
177 | /* ASCII characters */ | |
178 | ||
179 | U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", | |
180 | "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d " | |
181 | "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a " | |
182 | "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 " | |
183 | "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 " | |
184 | "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 " | |
185 | "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e " | |
186 | "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b " | |
187 | "0x7c 0x7d 0x7e"); | |
188 | 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", | |
189 | "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 " | |
190 | "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d " | |
191 | "0x1e 0x1f 0x7f"); | |
192 | ||
193 | /* from RFC3629 */ | |
194 | ||
195 | /* UTF8-2 = %xC2-DF UTF8-tail */ | |
196 | insist(!validutf8("\xC0\x80")); | |
197 | insist(!validutf8("\xC1\x80")); | |
198 | insist(!validutf8("\xC2\x7F")); | |
199 | U8("\xC2\x80", "0x80"); | |
200 | U8("\xDF\xBF", "0x7FF"); | |
201 | insist(!validutf8("\xDF\xC0")); | |
202 | ||
203 | /* UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) / | |
204 | * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail ) | |
205 | */ | |
206 | insist(!validutf8("\xE0\x9F\x80")); | |
207 | U8("\xE0\xA0\x80", "0x800"); | |
208 | U8("\xE0\xBF\xBF", "0xFFF"); | |
209 | insist(!validutf8("\xE0\xC0\xBF")); | |
210 | ||
211 | insist(!validutf8("\xE1\x80\x7F")); | |
212 | U8("\xE1\x80\x80", "0x1000"); | |
213 | U8("\xEC\xBF\xBF", "0xCFFF"); | |
214 | insist(!validutf8("\xEC\xC0\xBF")); | |
215 | ||
216 | U8("\xED\x80\x80", "0xD000"); | |
217 | U8("\xED\x9F\xBF", "0xD7FF"); | |
218 | insist(!validutf8("\xED\xA0\xBF")); | |
219 | ||
220 | insist(!validutf8("\xEE\x7f\x80")); | |
221 | U8("\xEE\x80\x80", "0xE000"); | |
222 | U8("\xEF\xBF\xBF", "0xFFFF"); | |
223 | insist(!validutf8("\xEF\xC0\xBF")); | |
224 | ||
225 | /* UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) / | |
226 | * %xF4 %x80-8F 2( UTF8-tail ) | |
227 | */ | |
228 | insist(!validutf8("\xF0\x8F\x80\x80")); | |
229 | U8("\xF0\x90\x80\x80", "0x10000"); | |
230 | U8("\xF0\xBF\xBF\xBF", "0x3FFFF"); | |
231 | insist(!validutf8("\xF0\xC0\x80\x80")); | |
232 | ||
233 | insist(!validutf8("\xF1\x80\x80\x7F")); | |
234 | U8("\xF1\x80\x80\x80", "0x40000"); | |
235 | U8("\xF3\xBF\xBF\xBF", "0xFFFFF"); | |
236 | insist(!validutf8("\xF3\xC0\x80\x80")); | |
237 | ||
238 | insist(!validutf8("\xF4\x80\x80\x7F")); | |
239 | U8("\xF4\x80\x80\x80", "0x100000"); | |
240 | U8("\xF4\x8F\xBF\xBF", "0x10FFFF"); | |
241 | insist(!validutf8("\xF4\x90\x80\x80")); | |
c35e83d9 | 242 | insist(!validutf8("\xF4\x80\xFF\x80")); |
460b9539 | 243 | |
244 | /* miscellaneous non-UTF-8 rubbish */ | |
245 | insist(!validutf8("\x80")); | |
246 | insist(!validutf8("\xBF")); | |
247 | insist(!validutf8("\xC0")); | |
248 | insist(!validutf8("\xC0\x7F")); | |
249 | insist(!validutf8("\xC0\xC0")); | |
250 | insist(!validutf8("\xE0")); | |
251 | insist(!validutf8("\xE0\x7F")); | |
252 | insist(!validutf8("\xE0\xC0")); | |
253 | insist(!validutf8("\xE0\x80")); | |
254 | insist(!validutf8("\xE0\x80\x7f")); | |
255 | insist(!validutf8("\xE0\x80\xC0")); | |
256 | insist(!validutf8("\xF0")); | |
257 | insist(!validutf8("\xF0\x7F")); | |
258 | insist(!validutf8("\xF0\xC0")); | |
259 | insist(!validutf8("\xF0\x80")); | |
260 | insist(!validutf8("\xF0\x80\x7f")); | |
261 | insist(!validutf8("\xF0\x80\xC0")); | |
262 | insist(!validutf8("\xF0\x80\x80\x7f")); | |
263 | insist(!validutf8("\xF0\x80\x80\xC0")); | |
264 | insist(!validutf8("\xF5\x80\x80\x80")); | |
265 | insist(!validutf8("\xF8")); | |
266 | } | |
267 | ||
268 | static void test_mime(void) { | |
269 | char *t, *n, *v; | |
270 | ||
033fd4e3 RK |
271 | fprintf(stderr, "test_mime\n"); |
272 | ||
460b9539 | 273 | t = n = v = 0; |
274 | insist(!mime_content_type("text/plain", &t, &n, &v)); | |
275 | insist(!strcmp(t, "text/plain")); | |
276 | insist(n == 0); | |
277 | insist(v == 0); | |
278 | ||
279 | t = n = v = 0; | |
280 | insist(!mime_content_type("TEXT ((nested) comment) /plain", &t, &n, &v)); | |
281 | insist(!strcmp(t, "text/plain")); | |
282 | insist(n == 0); | |
283 | insist(v == 0); | |
284 | ||
285 | t = n = v = 0; | |
286 | insist(!mime_content_type(" text/plain ; Charset=utf-8", &t, &n, &v)); | |
287 | insist(!strcmp(t, "text/plain")); | |
288 | insist(!strcmp(n, "charset")); | |
289 | insist(!strcmp(v, "utf-8")); | |
290 | ||
291 | t = n = v = 0; | |
292 | insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t, &n, &v)); | |
293 | insist(!strcmp(t, "text/plain")); | |
294 | insist(!strcmp(n, "charset")); | |
295 | insist(!strcmp(v, "ISO-8859-1")); | |
296 | ||
297 | /* XXX mime_parse */ | |
298 | /* XXX mime_multipart */ | |
299 | /* XXX mime_rfc2388_content_disposition */ | |
300 | ||
301 | check_string(mime_qp(""), ""); | |
302 | check_string(mime_qp("foobar"), "foobar"); | |
303 | check_string(mime_qp("foo=20bar"), "foo bar"); | |
304 | check_string(mime_qp("x \r\ny"), "x\r\ny"); | |
305 | check_string(mime_qp("x=\r\ny"), "xy"); | |
306 | check_string(mime_qp("x= \r\ny"), "xy"); | |
307 | check_string(mime_qp("x =\r\ny"), "x y"); | |
308 | check_string(mime_qp("x = \r\ny"), "x y"); | |
309 | ||
310 | /* from RFC2045 */ | |
311 | check_string(mime_qp("Now's the time =\r\n" | |
312 | "for all folk to come=\r\n" | |
313 | " to the aid of their country."), | |
314 | "Now's the time for all folk to come to the aid of their country."); | |
315 | ||
316 | check_string(mime_base64(""), ""); | |
317 | check_string(mime_base64("BBBB"), "\x04\x10\x41"); | |
318 | check_string(mime_base64("////"), "\xFF\xFF\xFF"); | |
319 | check_string(mime_base64("//BB"), "\xFF\xF0\x41"); | |
320 | check_string(mime_base64("BBBB//BB////"), | |
321 | "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF"); | |
322 | check_string(mime_base64("B B B B / / B B / / / /"), | |
323 | "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF"); | |
324 | check_string(mime_base64("B\r\nBBB.// B-B//~//"), | |
325 | "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF"); | |
326 | check_string(mime_base64("BBBB="), | |
327 | "\x04\x10\x41"); | |
328 | check_string(mime_base64("BBBBx="), /* not actually valid base64 */ | |
329 | "\x04\x10\x41"); | |
330 | check_string(mime_base64("BBBB BB=="), | |
331 | "\x04\x10\x41" "\x04"); | |
332 | check_string(mime_base64("BBBB BBB="), | |
333 | "\x04\x10\x41" "\x04\x10"); | |
334 | } | |
335 | ||
336 | static void test_hex(void) { | |
337 | unsigned n; | |
338 | static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F }; | |
339 | uint8_t *u; | |
340 | size_t ul; | |
341 | ||
033fd4e3 RK |
342 | fprintf(stderr, "test_hex\n"); |
343 | ||
460b9539 | 344 | for(n = 0; n <= UCHAR_MAX; ++n) { |
345 | if(!isxdigit(n)) | |
346 | insist(unhexdigitq(n) == -1); | |
347 | } | |
348 | insist(unhexdigitq('0') == 0); | |
349 | insist(unhexdigitq('1') == 1); | |
350 | insist(unhexdigitq('2') == 2); | |
351 | insist(unhexdigitq('3') == 3); | |
352 | insist(unhexdigitq('4') == 4); | |
353 | insist(unhexdigitq('5') == 5); | |
354 | insist(unhexdigitq('6') == 6); | |
355 | insist(unhexdigitq('7') == 7); | |
356 | insist(unhexdigitq('8') == 8); | |
357 | insist(unhexdigitq('9') == 9); | |
358 | insist(unhexdigitq('a') == 10); | |
359 | insist(unhexdigitq('b') == 11); | |
360 | insist(unhexdigitq('c') == 12); | |
361 | insist(unhexdigitq('d') == 13); | |
362 | insist(unhexdigitq('e') == 14); | |
363 | insist(unhexdigitq('f') == 15); | |
364 | insist(unhexdigitq('A') == 10); | |
365 | insist(unhexdigitq('B') == 11); | |
366 | insist(unhexdigitq('C') == 12); | |
367 | insist(unhexdigitq('D') == 13); | |
368 | insist(unhexdigitq('E') == 14); | |
369 | insist(unhexdigitq('F') == 15); | |
370 | check_string(hex(h, sizeof h), "00ff807f"); | |
371 | check_string(hex(0, 0), ""); | |
372 | u = unhex("00ff807f", &ul); | |
373 | insist(ul == 4); | |
374 | insist(memcmp(u, h, 4) == 0); | |
375 | u = unhex("00FF807F", &ul); | |
376 | insist(ul == 4); | |
377 | insist(memcmp(u, h, 4) == 0); | |
378 | u = unhex("", &ul); | |
379 | insist(ul == 0); | |
033fd4e3 | 380 | fprintf(stderr, "2 ERROR reports expected {\n"); |
460b9539 | 381 | insist(unhex("F", 0) == 0); |
382 | insist(unhex("az", 0) == 0); | |
033fd4e3 | 383 | fprintf(stderr, "}\n"); |
460b9539 | 384 | } |
385 | ||
386 | static void test_casefold(void) { | |
e5a5a138 | 387 | uint32_t c, l; |
56fd389c | 388 | const char *input, *canon_folded, *compat_folded, *canon_expected, *compat_expected; |
460b9539 | 389 | |
033fd4e3 | 390 | fprintf(stderr, "test_casefold\n"); |
56fd389c RK |
391 | |
392 | /* This isn't a very exhaustive test. Unlike for normalization, there don't | |
393 | * seem to be any public test vectors for these algorithms. */ | |
e5a5a138 | 394 | |
460b9539 | 395 | for(c = 1; c < 256; ++c) { |
e5a5a138 | 396 | input = utf32_to_utf8(&c, 1, 0); |
56fd389c RK |
397 | canon_folded = utf8_casefold_canon(input, strlen(input), 0); |
398 | compat_folded = utf8_casefold_compat(input, strlen(input), 0); | |
460b9539 | 399 | switch(c) { |
400 | default: | |
401 | if((c >= 'A' && c <= 'Z') | |
402 | || (c >= 0xC0 && c <= 0xDE && c != 0xD7)) | |
403 | l = c ^ 0x20; | |
404 | else | |
405 | l = c; | |
406 | break; | |
407 | case 0xB5: /* MICRO SIGN */ | |
e5a5a138 | 408 | l = 0x3BC; /* GREEK SMALL LETTER MU */ |
460b9539 | 409 | break; |
410 | case 0xDF: /* LATIN SMALL LETTER SHARP S */ | |
56fd389c RK |
411 | insist(!strcmp(canon_folded, "ss")); |
412 | insist(!strcmp(compat_folded, "ss")); | |
460b9539 | 413 | l = 0; |
414 | break; | |
415 | } | |
416 | if(l) { | |
caecd4f4 | 417 | uint32_t *d; |
e5a5a138 | 418 | /* Case-folded data is now normalized */ |
caecd4f4 RK |
419 | d = utf32_decompose_canon(&l, 1, 0); |
420 | canon_expected = utf32_to_utf8(d, utf32_len(d), 0); | |
56fd389c RK |
421 | if(strcmp(canon_folded, canon_expected)) { |
422 | fprintf(stderr, "%s:%d: canon-casefolding %#lx got '%s', expected '%s'\n", | |
423 | __FILE__, __LINE__, (unsigned long)c, | |
424 | format(canon_folded), format(canon_expected)); | |
bb48024f | 425 | count_error(); |
56fd389c RK |
426 | } |
427 | ++tests; | |
caecd4f4 RK |
428 | d = utf32_decompose_compat(&l, 1, 0); |
429 | compat_expected = utf32_to_utf8(d, utf32_len(d), 0); | |
56fd389c RK |
430 | if(strcmp(compat_folded, compat_expected)) { |
431 | fprintf(stderr, "%s:%d: compat-casefolding %#lx got '%s', expected '%s'\n", | |
460b9539 | 432 | __FILE__, __LINE__, (unsigned long)c, |
56fd389c | 433 | format(compat_folded), format(compat_expected)); |
bb48024f | 434 | count_error(); |
460b9539 | 435 | } |
436 | ++tests; | |
437 | } | |
438 | } | |
c85b7022 | 439 | check_string(utf8_casefold_canon("", 0, 0), ""); |
460b9539 | 440 | } |
441 | ||
8818b7fc RK |
442 | struct { |
443 | const char *in; | |
444 | const char *expect[10]; | |
445 | } wtest[] = { | |
446 | /* Empty string */ | |
447 | { "", { 0 } }, | |
448 | /* Only whitespace and punctuation */ | |
449 | { " ", { 0 } }, | |
450 | { " ' ", { 0 } }, | |
451 | { " ! ", { 0 } }, | |
452 | { " \"\" ", { 0 } }, | |
453 | { " @ ", { 0 } }, | |
454 | /* Basics */ | |
455 | { "wibble", { "wibble", 0 } }, | |
456 | { " wibble", { "wibble", 0 } }, | |
457 | { " wibble ", { "wibble", 0 } }, | |
458 | { "wibble ", { "wibble", 0 } }, | |
459 | { "wibble spong", { "wibble", "spong", 0 } }, | |
460 | { " wibble spong", { "wibble", "spong", 0 } }, | |
461 | { " wibble spong ", { "wibble", "spong", 0 } }, | |
462 | { "wibble spong ", { "wibble", "spong", 0 } }, | |
463 | { "wibble spong splat foo zot ", { "wibble", "spong", "splat", "foo", "zot", 0 } }, | |
464 | /* Apostrophes */ | |
465 | { "wibble 'spong", { "wibble", "spong", 0 } }, | |
466 | { " wibble's", { "wibble's", 0 } }, | |
467 | { " wibblespong' ", { "wibblespong", 0 } }, | |
468 | { "wibble sp''ong ", { "wibble", "sp", "ong", 0 } }, | |
469 | }; | |
470 | #define NWTEST (sizeof wtest / sizeof *wtest) | |
471 | ||
472 | static void test_words(void) { | |
473 | size_t t, nexpect, ngot, i; | |
474 | int right; | |
475 | ||
476 | fprintf(stderr, "test_words\n"); | |
477 | for(t = 0; t < NWTEST; ++t) { | |
c85b7022 | 478 | char **got = utf8_word_split(wtest[t].in, strlen(wtest[t].in), &ngot, 0); |
8818b7fc RK |
479 | |
480 | for(nexpect = 0; wtest[t].expect[nexpect]; ++nexpect) | |
481 | ; | |
482 | if(nexpect == ngot) { | |
483 | for(i = 0; i < ngot; ++i) | |
484 | if(strcmp(wtest[t].expect[i], got[i])) | |
485 | break; | |
486 | right = i == ngot; | |
487 | } else | |
488 | right = 0; | |
489 | if(!right) { | |
490 | fprintf(stderr, "word split %zu failed\n", t); | |
491 | fprintf(stderr, "input: %s\n", wtest[t].in); | |
492 | fprintf(stderr, " | %-30s | %-30s\n", | |
493 | "expected", "got"); | |
494 | for(i = 0; i < nexpect || i < ngot; ++i) { | |
495 | const char *e = i < nexpect ? wtest[t].expect[i] : "<none>"; | |
496 | const char *g = i < ngot ? got[i] : "<none>"; | |
497 | fprintf(stderr, " %2zu | %-30s | %-30s\n", i, e, g); | |
498 | } | |
499 | count_error(); | |
500 | } | |
501 | ++tests; | |
502 | } | |
503 | } | |
504 | ||
033fd4e3 RK |
505 | /** @brief Less-than comparison function for integer heap */ |
506 | static inline int int_lt(int a, int b) { return a < b; } | |
507 | ||
dab22732 RK |
508 | /** @struct iheap |
509 | * @brief A heap with @c int elements */ | |
033fd4e3 | 510 | HEAP_TYPE(iheap, int, int_lt); |
8e3fe3d8 | 511 | HEAP_DEFINE(iheap, int, int_lt); |
033fd4e3 RK |
512 | |
513 | /** @brief Tests for @ref heap.h */ | |
514 | static void test_heap(void) { | |
515 | struct iheap h[1]; | |
516 | int n; | |
517 | int last = -1; | |
518 | ||
519 | fprintf(stderr, "test_heap\n"); | |
520 | ||
521 | iheap_init(h); | |
522 | for(n = 0; n < 1000; ++n) | |
523 | iheap_insert(h, random() % 100); | |
524 | for(n = 0; n < 1000; ++n) { | |
525 | const int latest = iheap_remove(h); | |
526 | if(last > latest) | |
527 | fprintf(stderr, "should have %d <= %d\n", last, latest); | |
528 | insist(last <= latest); | |
529 | last = latest; | |
530 | } | |
531 | putchar('\n'); | |
532 | } | |
533 | ||
e2452add RK |
534 | /** @brief Open a Unicode test file */ |
535 | static FILE *open_unicode_test(const char *path) { | |
536 | const char *base; | |
537 | FILE *fp; | |
538 | char buffer[1024]; | |
539 | int w; | |
540 | ||
541 | if((base = strrchr(path, '/'))) | |
542 | ++base; | |
543 | else | |
544 | base = path; | |
545 | if(!(fp = fopen(base, "r"))) { | |
546 | snprintf(buffer, sizeof buffer, | |
547 | "wget http://www.unicode.org/Public/5.0.0/ucd/%s", path); | |
548 | if((w = system(buffer))) | |
549 | fatal(0, "%s: %s", buffer, wstat(w)); | |
550 | if(chmod(base, 0444) < 0) | |
551 | fatal(errno, "chmod %s", base); | |
552 | if(!(fp = fopen(base, "r"))) | |
553 | fatal(errno, "%s", base); | |
554 | } | |
555 | return fp; | |
556 | } | |
557 | ||
1625e11a | 558 | /** @brief Run breaking tests for utf32_grapheme_boundary() etc */ |
bb48024f RK |
559 | static void breaktest(const char *path, |
560 | int (*breakfn)(const uint32_t *, size_t, size_t)) { | |
561 | FILE *fp = open_unicode_test(path); | |
562 | int lineno = 0; | |
563 | char *l, *lp; | |
564 | size_t bn, n; | |
565 | char break_allowed[1024]; | |
566 | uint32_t buffer[1024]; | |
567 | ||
568 | while(!inputline(path, fp, &l, '\n')) { | |
569 | ++lineno; | |
570 | if(l[0] == '#') continue; | |
571 | bn = 0; | |
572 | lp = l; | |
573 | while(*lp) { | |
574 | if(*lp == ' ' || *lp == '\t') { | |
575 | ++lp; | |
576 | continue; | |
577 | } | |
578 | if(*lp == '#') | |
579 | break; | |
580 | if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0xB7) { | |
581 | /* 00F7 DIVISION SIGN */ | |
582 | break_allowed[bn] = 1; | |
583 | lp += 2; | |
584 | continue; | |
585 | } | |
586 | if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0x97) { | |
587 | /* 00D7 MULTIPLICATION SIGN */ | |
588 | break_allowed[bn] = 0; | |
589 | lp += 2; | |
590 | continue; | |
591 | } | |
592 | if(isxdigit((unsigned char)*lp)) { | |
593 | buffer[bn++] = strtoul(lp, &lp, 16); | |
594 | continue; | |
595 | } | |
596 | fatal(0, "%s:%d: evil line: %s", path, lineno, l); | |
597 | } | |
598 | for(n = 0; n <= bn; ++n) { | |
599 | if(breakfn(buffer, bn, n) != break_allowed[n]) { | |
600 | fprintf(stderr, | |
b21a155c RK |
601 | "%s:%d: offset %zu: mismatch\n" |
602 | "%s\n" | |
603 | "\n", | |
604 | path, lineno, n, l); | |
bb48024f RK |
605 | count_error(); |
606 | } | |
607 | ++tests; | |
608 | } | |
609 | xfree(l); | |
610 | } | |
611 | fclose(fp); | |
612 | } | |
613 | ||
e5a5a138 RK |
614 | /** @brief Tests for @ref lib/unicode.h */ |
615 | static void test_unicode(void) { | |
616 | FILE *fp; | |
617 | int lineno = 0; | |
618 | char *l, *lp; | |
619 | uint32_t buffer[1024]; | |
16506c9d | 620 | uint32_t *c[6], *NFD_c[6], *NFKD_c[6], *NFC_c[6], *NFKC_c[6]; /* 1-indexed */ |
e5a5a138 RK |
621 | int cn, bn; |
622 | ||
623 | fprintf(stderr, "test_unicode\n"); | |
e2452add | 624 | fp = open_unicode_test("NormalizationTest.txt"); |
e5a5a138 RK |
625 | while(!inputline("NormalizationTest.txt", fp, &l, '\n')) { |
626 | ++lineno; | |
627 | if(*l == '#' || *l == '@') | |
628 | continue; | |
629 | bn = 0; | |
630 | cn = 1; | |
631 | lp = l; | |
632 | c[cn++] = &buffer[bn]; | |
633 | while(*lp && *lp != '#') { | |
634 | if(*lp == ' ') { | |
635 | ++lp; | |
636 | continue; | |
637 | } | |
638 | if(*lp == ';') { | |
639 | buffer[bn++] = 0; | |
640 | if(cn == 6) | |
641 | break; | |
642 | c[cn++] = &buffer[bn]; | |
643 | ++lp; | |
644 | continue; | |
645 | } | |
646 | buffer[bn++] = strtoul(lp, &lp, 16); | |
647 | } | |
648 | buffer[bn] = 0; | |
649 | assert(cn == 6); | |
650 | for(cn = 1; cn <= 5; ++cn) { | |
651 | NFD_c[cn] = utf32_decompose_canon(c[cn], utf32_len(c[cn]), 0); | |
652 | NFKD_c[cn] = utf32_decompose_compat(c[cn], utf32_len(c[cn]), 0); | |
16506c9d RK |
653 | NFC_c[cn] = utf32_compose_canon(c[cn], utf32_len(c[cn]), 0); |
654 | NFKC_c[cn] = utf32_compose_compat(c[cn], utf32_len(c[cn]), 0); | |
e5a5a138 RK |
655 | } |
656 | #define unt_check(T, A, B) do { \ | |
657 | ++tests; \ | |
658 | if(utf32_cmp(c[A], T##_c[B])) { \ | |
e2452add RK |
659 | fprintf(stderr, \ |
660 | "NormalizationTest.txt:%d: c%d != "#T"(c%d)\n", \ | |
661 | lineno, A, B); \ | |
16506c9d | 662 | fprintf(stderr, " c%d:%s\n", \ |
e5a5a138 | 663 | A, format_utf32(c[A])); \ |
16506c9d RK |
664 | fprintf(stderr, " c%d:%s\n", \ |
665 | B, format_utf32(c[B])); \ | |
666 | fprintf(stderr, "%4s(c%d):%s\n", \ | |
e5a5a138 | 667 | #T, B, format_utf32(T##_c[B])); \ |
bcf9ed7f | 668 | count_error(); \ |
e5a5a138 RK |
669 | } \ |
670 | } while(0) | |
671 | unt_check(NFD, 3, 1); | |
672 | unt_check(NFD, 3, 2); | |
673 | unt_check(NFD, 3, 3); | |
674 | unt_check(NFD, 5, 4); | |
675 | unt_check(NFD, 5, 5); | |
676 | unt_check(NFKD, 5, 1); | |
677 | unt_check(NFKD, 5, 2); | |
678 | unt_check(NFKD, 5, 3); | |
679 | unt_check(NFKD, 5, 4); | |
680 | unt_check(NFKD, 5, 5); | |
16506c9d RK |
681 | unt_check(NFC, 2, 1); |
682 | unt_check(NFC, 2, 2); | |
683 | unt_check(NFC, 2, 3); | |
684 | unt_check(NFC, 4, 4); | |
685 | unt_check(NFC, 4, 5); | |
686 | unt_check(NFKC, 4, 1); | |
687 | unt_check(NFKC, 4, 2); | |
688 | unt_check(NFKC, 4, 3); | |
689 | unt_check(NFKC, 4, 4); | |
690 | unt_check(NFKC, 4, 5); | |
e5a5a138 RK |
691 | for(cn = 1; cn <= 5; ++cn) { |
692 | xfree(NFD_c[cn]); | |
693 | xfree(NFKD_c[cn]); | |
694 | } | |
695 | xfree(l); | |
696 | } | |
e2452add | 697 | fclose(fp); |
1625e11a | 698 | breaktest("auxiliary/GraphemeBreakTest.txt", utf32_is_grapheme_boundary); |
bb48024f | 699 | breaktest("auxiliary/WordBreakTest.txt", utf32_is_word_boundary); |
c35e83d9 RK |
700 | insist(utf32_combining_class(0x40000) == 0); |
701 | insist(utf32_combining_class(0xE0000) == 0); | |
e5a5a138 RK |
702 | } |
703 | ||
ea387d53 RK |
704 | static void test_signame(void) { |
705 | fprintf(stderr, "test_signame\n"); | |
706 | insist(find_signal("SIGTERM") == SIGTERM); | |
707 | insist(find_signal("SIGHUP") == SIGHUP); | |
708 | insist(find_signal("SIGINT") == SIGINT); | |
709 | insist(find_signal("SIGQUIT") == SIGQUIT); | |
710 | insist(find_signal("SIGKILL") == SIGKILL); | |
711 | insist(find_signal("SIGYOURMUM") == -1); | |
712 | } | |
713 | ||
9f28e855 RK |
714 | static void test_cache(void) { |
715 | const struct cache_type t1 = { 1 }, t2 = { 10 }; | |
716 | const char v11[] = "spong", v12[] = "wibble", v2[] = "blat"; | |
717 | fprintf(stderr, "test_cache\n"); | |
718 | cache_put(&t1, "1_1", v11); | |
719 | cache_put(&t1, "1_2", v12); | |
720 | cache_put(&t2, "2", v2); | |
721 | insist(cache_count() == 3); | |
722 | insist(cache_get(&t2, "2") == v2); | |
723 | insist(cache_get(&t1, "1_1") == v11); | |
724 | insist(cache_get(&t1, "1_2") == v12); | |
725 | insist(cache_get(&t1, "2") == 0); | |
726 | insist(cache_get(&t2, "1_1") == 0); | |
727 | insist(cache_get(&t2, "1_2") == 0); | |
728 | insist(cache_get(&t1, "2") == 0); | |
729 | insist(cache_get(&t2, "1_1") == 0); | |
730 | insist(cache_get(&t2, "1_2") == 0); | |
731 | sleep(2); | |
732 | cache_expire(); | |
733 | insist(cache_count() == 1); | |
734 | insist(cache_get(&t1, "1_1") == 0); | |
735 | insist(cache_get(&t1, "1_2") == 0); | |
736 | insist(cache_get(&t2, "2") == v2); | |
737 | cache_clean(0); | |
738 | insist(cache_count() == 0); | |
739 | insist(cache_get(&t2, "2") == 0); | |
740 | } | |
741 | ||
00e36cd0 RK |
742 | static void test_filepart(void) { |
743 | fprintf(stderr, "test_filepart\n"); | |
744 | check_string(d_dirname("/"), "/"); | |
745 | check_string(d_dirname("/spong"), "/"); | |
746 | check_string(d_dirname("/foo/bar"), "/foo"); | |
747 | check_string(d_dirname("./bar"), "."); | |
748 | check_string(d_dirname("."), "."); | |
749 | check_string(d_dirname(".."), "."); | |
750 | check_string(d_dirname("../blat"), ".."); | |
751 | check_string(d_dirname("wibble"), "."); | |
752 | check_string(extension("foo.c"), ".c"); | |
753 | check_string(extension(".c"), ".c"); | |
754 | check_string(extension("."), "."); | |
755 | check_string(extension("foo"), ""); | |
756 | check_string(extension("./foo"), ""); | |
757 | check_string(extension("./foo.c"), ".c"); | |
758 | } | |
759 | ||
65bb0fff RK |
760 | static void test_selection(void) { |
761 | hash *h; | |
762 | fprintf(stderr, "test_selection\n"); | |
763 | insist((h = selection_new()) != 0); | |
764 | selection_set(h, "one", 1); | |
765 | selection_set(h, "two", 1); | |
766 | selection_set(h, "three", 0); | |
767 | selection_set(h, "four", 1); | |
768 | insist(selection_selected(h, "one") == 1); | |
769 | insist(selection_selected(h, "two") == 1); | |
770 | insist(selection_selected(h, "three") == 0); | |
771 | insist(selection_selected(h, "four") == 1); | |
772 | insist(selection_selected(h, "five") == 0); | |
773 | insist(hash_count(h) == 3); | |
774 | selection_flip(h, "one"); | |
775 | selection_flip(h, "three"); | |
776 | insist(selection_selected(h, "one") == 0); | |
777 | insist(selection_selected(h, "three") == 1); | |
778 | insist(hash_count(h) == 3); | |
779 | selection_live(h, "one"); | |
780 | selection_live(h, "two"); | |
781 | selection_live(h, "three"); | |
782 | selection_cleanup(h); | |
783 | insist(selection_selected(h, "one") == 0); | |
784 | insist(selection_selected(h, "two") == 1); | |
785 | insist(selection_selected(h, "three") == 1); | |
786 | insist(selection_selected(h, "four") == 0); | |
787 | insist(selection_selected(h, "five") == 0); | |
788 | insist(hash_count(h) == 2); | |
789 | selection_empty(h); | |
790 | insist(selection_selected(h, "one") == 0); | |
791 | insist(selection_selected(h, "two") == 0); | |
792 | insist(selection_selected(h, "three") == 0); | |
793 | insist(selection_selected(h, "four") == 0); | |
794 | insist(selection_selected(h, "five") == 0); | |
795 | insist(hash_count(h) == 0); | |
796 | } | |
797 | ||
71b90230 RK |
798 | static void test_wstat(void) { |
799 | pid_t pid; | |
800 | int w; | |
801 | ||
802 | fprintf(stderr, "test_wstat\n"); | |
803 | if(!(pid = xfork())) { | |
804 | _exit(1); | |
805 | } | |
806 | while(waitpid(pid, &w, 0) < 0 && errno == EINTR) | |
807 | ; | |
808 | check_string(wstat(w), "exited with status 1"); | |
809 | if(!(pid = xfork())) { | |
810 | kill(getpid(), SIGTERM); | |
811 | _exit(-1); | |
812 | } | |
813 | while(waitpid(pid, &w, 0) < 0 && errno == EINTR) | |
814 | ; | |
815 | check_string_prefix(wstat(w), "terminated by signal 15"); | |
816 | } | |
817 | ||
22f61603 RK |
818 | static void test_kvp(void) { |
819 | struct kvp *k; | |
820 | ||
821 | fprintf(stderr, "test_kvp\n"); | |
822 | #define KVP_URLDECODE(S) kvp_urldecode((S), strlen(S)) | |
823 | insist(KVP_URLDECODE("=%zz") == 0); | |
824 | insist(KVP_URLDECODE("=%0") == 0); | |
825 | insist(KVP_URLDECODE("=%0z") == 0); | |
826 | insist(KVP_URLDECODE("=%%") == 0); | |
827 | insist(KVP_URLDECODE("==%") == 0); | |
828 | insist(KVP_URLDECODE("wibble") == 0); | |
829 | insist(KVP_URLDECODE("") == 0); | |
830 | insist(KVP_URLDECODE("wibble&") == 0); | |
831 | insist((k = KVP_URLDECODE("one=bl%61t+foo")) != 0); | |
832 | check_string(kvp_get(k, "one"), "blat foo"); | |
833 | insist(kvp_get(k, "ONE") == 0); | |
834 | insist(k->next == 0); | |
835 | insist((k = KVP_URLDECODE("wibble=splat&bar=spong")) != 0); | |
836 | check_string(kvp_get(k, "wibble"), "splat"); | |
837 | check_string(kvp_get(k, "bar"), "spong"); | |
838 | insist(kvp_get(k, "ONE") == 0); | |
839 | insist(k->next->next == 0); | |
840 | /* TODO test encoding too */ | |
841 | } | |
842 | ||
460b9539 | 843 | int main(void) { |
bb48024f | 844 | fail_first = !!getenv("FAIL_FIRST"); |
460b9539 | 845 | insist('\n' == 0x0A); |
846 | insist('\r' == 0x0D); | |
847 | insist(' ' == 0x20); | |
848 | insist('0' == 0x30); | |
849 | insist('9' == 0x39); | |
850 | insist('A' == 0x41); | |
851 | insist('Z' == 0x5A); | |
852 | insist('a' == 0x61); | |
853 | insist('z' == 0x7A); | |
854 | /* addr.c */ | |
855 | /* asprintf.c */ | |
856 | /* authhash.c */ | |
857 | /* basen.c */ | |
858 | /* charset.c */ | |
859 | /* client.c */ | |
860 | /* configuration.c */ | |
861 | /* event.c */ | |
00e36cd0 RK |
862 | /* filepart.c */ |
863 | test_filepart(); | |
460b9539 | 864 | /* fprintf.c */ |
033fd4e3 RK |
865 | /* heap.c */ |
866 | test_heap(); | |
460b9539 | 867 | /* hex.c */ |
868 | test_hex(); | |
869 | /* inputline.c */ | |
870 | /* kvp.c */ | |
22f61603 | 871 | test_kvp(); |
460b9539 | 872 | /* log.c */ |
873 | /* mem.c */ | |
874 | /* mime.c */ | |
875 | test_mime(); | |
876 | /* mixer.c */ | |
877 | /* plugin.c */ | |
878 | /* printf.c */ | |
879 | /* queue.c */ | |
880 | /* sink.c */ | |
881 | /* snprintf.c */ | |
882 | /* split.c */ | |
883 | /* syscalls.c */ | |
884 | /* table.c */ | |
e5a5a138 RK |
885 | /* unicode.c */ |
886 | test_unicode(); | |
460b9539 | 887 | /* utf8.c */ |
888 | test_utf8(); | |
889 | /* vector.c */ | |
890 | /* words.c */ | |
891 | test_casefold(); | |
8818b7fc | 892 | test_words(); |
460b9539 | 893 | /* wstat.c */ |
71b90230 | 894 | test_wstat(); |
ea387d53 RK |
895 | /* signame.c */ |
896 | test_signame(); | |
9f28e855 RK |
897 | /* cache.c */ |
898 | test_cache(); | |
65bb0fff RK |
899 | /* selection.c */ |
900 | test_selection(); | |
460b9539 | 901 | fprintf(stderr, "%d errors out of %d tests\n", errors, tests); |
902 | return !!errors; | |
903 | } | |
904 | ||
905 | /* | |
906 | Local Variables: | |
907 | c-basic-offset:2 | |
908 | comment-column:40 | |
56fd389c RK |
909 | fill-column:79 |
910 | indent-tabs-mode:nil | |
460b9539 | 911 | End: |
912 | */ |