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