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