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