chiark / gitweb /
doxygen
[disorder] / lib / test.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2005 Richard Kettlewell
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 */
20
21#include <config.h>
22#include "types.h"
23
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <ctype.h>
29
30#include "utf8.h"
31#include "mem.h"
32#include "log.h"
33#include "vector.h"
34#include "charset.h"
35#include "mime.h"
36#include "hex.h"
37#include "words.h"
38
39static int tests, errors;
40
41#define insist(expr) do { \
42 if(!expr) { \
43 ++errors; \
44 fprintf(stderr, "%s:%d: error checking %s\n", \
45 __FILE__, __LINE__, #expr); \
46 } \
47 ++tests; \
48} while(0)
49
50static const char *format(const char *s) {
51 struct dynstr d;
52 int c;
53 char buf[10];
54
55 dynstr_init(&d);
56 while((c = (unsigned char)*s++)) {
57 if(c >= ' ' && c <= '~')
58 dynstr_append(&d, c);
59 else {
60 sprintf(buf, "\\x%02X", (unsigned)c);
61 dynstr_append_string(&d, buf);
62 }
63 }
64 dynstr_terminate(&d);
65 return d.vec;
66}
67
68#define check_string(GOT, WANT) do { \
69 const char *g = GOT; \
70 const char *w = WANT; \
71 \
72 if(w == 0) { \
73 fprintf(stderr, "%s:%d: %s returned 0\n", \
74 __FILE__, __LINE__, #GOT); \
75 ++errors; \
76 } else if(strcmp(w, g)) { \
77 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \
78 __FILE__, __LINE__, #GOT, format(g), format(w)); \
79 ++errors; \
80 } \
81 ++tests; \
82 } while(0)
83
84static uint32_t *ucs4parse(const char *s) {
85 struct dynstr_ucs4 d;
86 char *e;
87
88 dynstr_ucs4_init(&d);
89 while(*s) {
90 errno = 0;
91 dynstr_ucs4_append(&d, strtoul(s, &e, 0));
92 if(errno) fatal(errno, "strtoul (%s)", s);
93 s = e;
94 }
95 dynstr_ucs4_terminate(&d);
96 return d.vec;
97}
98
99static void test_utf8(void) {
100 /* Test validutf8, convert to UCS-4, check the answer is right,
101 * convert back to UTF-8, check we got to where we started */
102#define U8(CHARS, WORDS) do { \
103 uint32_t *w = ucs4parse(WORDS); \
104 uint32_t *ucs; \
105 char *u8; \
106 \
107 insist(validutf8(CHARS)); \
108 ucs = utf82ucs4(CHARS); \
109 insist(ucs != 0); \
110 insist(!ucs4cmp(w, ucs)); \
111 u8 = ucs42utf8(ucs); \
112 insist(u8 != 0); \
113 insist(!strcmp(u8, CHARS)); \
114} while(0)
115
116 /* empty string */
117
118 U8("", "");
119
120 /* ASCII characters */
121
122 U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
123 "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d "
124 "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a "
125 "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 "
126 "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 "
127 "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 "
128 "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e "
129 "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b "
130 "0x7c 0x7d 0x7e");
131 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",
132 "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 "
133 "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d "
134 "0x1e 0x1f 0x7f");
135
136 /* from RFC3629 */
137
138 /* UTF8-2 = %xC2-DF UTF8-tail */
139 insist(!validutf8("\xC0\x80"));
140 insist(!validutf8("\xC1\x80"));
141 insist(!validutf8("\xC2\x7F"));
142 U8("\xC2\x80", "0x80");
143 U8("\xDF\xBF", "0x7FF");
144 insist(!validutf8("\xDF\xC0"));
145
146 /* UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
147 * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
148 */
149 insist(!validutf8("\xE0\x9F\x80"));
150 U8("\xE0\xA0\x80", "0x800");
151 U8("\xE0\xBF\xBF", "0xFFF");
152 insist(!validutf8("\xE0\xC0\xBF"));
153
154 insist(!validutf8("\xE1\x80\x7F"));
155 U8("\xE1\x80\x80", "0x1000");
156 U8("\xEC\xBF\xBF", "0xCFFF");
157 insist(!validutf8("\xEC\xC0\xBF"));
158
159 U8("\xED\x80\x80", "0xD000");
160 U8("\xED\x9F\xBF", "0xD7FF");
161 insist(!validutf8("\xED\xA0\xBF"));
162
163 insist(!validutf8("\xEE\x7f\x80"));
164 U8("\xEE\x80\x80", "0xE000");
165 U8("\xEF\xBF\xBF", "0xFFFF");
166 insist(!validutf8("\xEF\xC0\xBF"));
167
168 /* UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
169 * %xF4 %x80-8F 2( UTF8-tail )
170 */
171 insist(!validutf8("\xF0\x8F\x80\x80"));
172 U8("\xF0\x90\x80\x80", "0x10000");
173 U8("\xF0\xBF\xBF\xBF", "0x3FFFF");
174 insist(!validutf8("\xF0\xC0\x80\x80"));
175
176 insist(!validutf8("\xF1\x80\x80\x7F"));
177 U8("\xF1\x80\x80\x80", "0x40000");
178 U8("\xF3\xBF\xBF\xBF", "0xFFFFF");
179 insist(!validutf8("\xF3\xC0\x80\x80"));
180
181 insist(!validutf8("\xF4\x80\x80\x7F"));
182 U8("\xF4\x80\x80\x80", "0x100000");
183 U8("\xF4\x8F\xBF\xBF", "0x10FFFF");
184 insist(!validutf8("\xF4\x90\x80\x80"));
185
186 /* miscellaneous non-UTF-8 rubbish */
187 insist(!validutf8("\x80"));
188 insist(!validutf8("\xBF"));
189 insist(!validutf8("\xC0"));
190 insist(!validutf8("\xC0\x7F"));
191 insist(!validutf8("\xC0\xC0"));
192 insist(!validutf8("\xE0"));
193 insist(!validutf8("\xE0\x7F"));
194 insist(!validutf8("\xE0\xC0"));
195 insist(!validutf8("\xE0\x80"));
196 insist(!validutf8("\xE0\x80\x7f"));
197 insist(!validutf8("\xE0\x80\xC0"));
198 insist(!validutf8("\xF0"));
199 insist(!validutf8("\xF0\x7F"));
200 insist(!validutf8("\xF0\xC0"));
201 insist(!validutf8("\xF0\x80"));
202 insist(!validutf8("\xF0\x80\x7f"));
203 insist(!validutf8("\xF0\x80\xC0"));
204 insist(!validutf8("\xF0\x80\x80\x7f"));
205 insist(!validutf8("\xF0\x80\x80\xC0"));
206 insist(!validutf8("\xF5\x80\x80\x80"));
207 insist(!validutf8("\xF8"));
208}
209
210static void test_mime(void) {
211 char *t, *n, *v;
212
213 t = n = v = 0;
214 insist(!mime_content_type("text/plain", &t, &n, &v));
215 insist(!strcmp(t, "text/plain"));
216 insist(n == 0);
217 insist(v == 0);
218
219 t = n = v = 0;
220 insist(!mime_content_type("TEXT ((nested) comment) /plain", &t, &n, &v));
221 insist(!strcmp(t, "text/plain"));
222 insist(n == 0);
223 insist(v == 0);
224
225 t = n = v = 0;
226 insist(!mime_content_type(" text/plain ; Charset=utf-8", &t, &n, &v));
227 insist(!strcmp(t, "text/plain"));
228 insist(!strcmp(n, "charset"));
229 insist(!strcmp(v, "utf-8"));
230
231 t = n = v = 0;
232 insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t, &n, &v));
233 insist(!strcmp(t, "text/plain"));
234 insist(!strcmp(n, "charset"));
235 insist(!strcmp(v, "ISO-8859-1"));
236
237 /* XXX mime_parse */
238 /* XXX mime_multipart */
239 /* XXX mime_rfc2388_content_disposition */
240
241 check_string(mime_qp(""), "");
242 check_string(mime_qp("foobar"), "foobar");
243 check_string(mime_qp("foo=20bar"), "foo bar");
244 check_string(mime_qp("x \r\ny"), "x\r\ny");
245 check_string(mime_qp("x=\r\ny"), "xy");
246 check_string(mime_qp("x= \r\ny"), "xy");
247 check_string(mime_qp("x =\r\ny"), "x y");
248 check_string(mime_qp("x = \r\ny"), "x y");
249
250 /* from RFC2045 */
251 check_string(mime_qp("Now's the time =\r\n"
252"for all folk to come=\r\n"
253" to the aid of their country."),
254 "Now's the time for all folk to come to the aid of their country.");
255
256 check_string(mime_base64(""), "");
257 check_string(mime_base64("BBBB"), "\x04\x10\x41");
258 check_string(mime_base64("////"), "\xFF\xFF\xFF");
259 check_string(mime_base64("//BB"), "\xFF\xF0\x41");
260 check_string(mime_base64("BBBB//BB////"),
261 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
262 check_string(mime_base64("B B B B / / B B / / / /"),
263 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
264 check_string(mime_base64("B\r\nBBB.// B-B//~//"),
265 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
266 check_string(mime_base64("BBBB="),
267 "\x04\x10\x41");
268 check_string(mime_base64("BBBBx="), /* not actually valid base64 */
269 "\x04\x10\x41");
270 check_string(mime_base64("BBBB BB=="),
271 "\x04\x10\x41" "\x04");
272 check_string(mime_base64("BBBB BBB="),
273 "\x04\x10\x41" "\x04\x10");
274}
275
276static void test_hex(void) {
277 unsigned n;
278 static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F };
279 uint8_t *u;
280 size_t ul;
281
282 for(n = 0; n <= UCHAR_MAX; ++n) {
283 if(!isxdigit(n))
284 insist(unhexdigitq(n) == -1);
285 }
286 insist(unhexdigitq('0') == 0);
287 insist(unhexdigitq('1') == 1);
288 insist(unhexdigitq('2') == 2);
289 insist(unhexdigitq('3') == 3);
290 insist(unhexdigitq('4') == 4);
291 insist(unhexdigitq('5') == 5);
292 insist(unhexdigitq('6') == 6);
293 insist(unhexdigitq('7') == 7);
294 insist(unhexdigitq('8') == 8);
295 insist(unhexdigitq('9') == 9);
296 insist(unhexdigitq('a') == 10);
297 insist(unhexdigitq('b') == 11);
298 insist(unhexdigitq('c') == 12);
299 insist(unhexdigitq('d') == 13);
300 insist(unhexdigitq('e') == 14);
301 insist(unhexdigitq('f') == 15);
302 insist(unhexdigitq('A') == 10);
303 insist(unhexdigitq('B') == 11);
304 insist(unhexdigitq('C') == 12);
305 insist(unhexdigitq('D') == 13);
306 insist(unhexdigitq('E') == 14);
307 insist(unhexdigitq('F') == 15);
308 check_string(hex(h, sizeof h), "00ff807f");
309 check_string(hex(0, 0), "");
310 u = unhex("00ff807f", &ul);
311 insist(ul == 4);
312 insist(memcmp(u, h, 4) == 0);
313 u = unhex("00FF807F", &ul);
314 insist(ul == 4);
315 insist(memcmp(u, h, 4) == 0);
316 u = unhex("", &ul);
317 insist(ul == 0);
318 fprintf(stderr, "2 ERROR reports expected:\n");
319 insist(unhex("F", 0) == 0);
320 insist(unhex("az", 0) == 0);
321}
322
323static void test_casefold(void) {
324 uint32_t c, l, u[2];
325 const char *s, *ls;
326
327 for(c = 1; c < 256; ++c) {
328 u[0] = c;
329 u[1] = 0;
330 s = ucs42utf8(u);
331 ls = casefold(s);
332 switch(c) {
333 default:
334 if((c >= 'A' && c <= 'Z')
335 || (c >= 0xC0 && c <= 0xDE && c != 0xD7))
336 l = c ^ 0x20;
337 else
338 l = c;
339 break;
340 case 0xB5: /* MICRO SIGN */
341 l = 0x3BC; /* GREEK SMALL LETTER MU */
342 break;
343 case 0xDF: /* LATIN SMALL LETTER SHARP S */
344 insist(!strcmp(ls, "ss"));
345 l = 0;
346 break;
347 }
348 if(l) {
349 u[0] = l;
350 u[1] = 0;
351 s = ucs42utf8(u);
352 if(strcmp(s, ls)) {
353 fprintf(stderr, "%s:%d: casefolding %#lx got '%s', expected '%s'\n",
354 __FILE__, __LINE__, (unsigned long)c,
355 format(ls), format(s));
356 ++errors;
357 }
358 ++tests;
359 }
360 }
361 check_string(casefold(""), "");
362}
363
364int main(void) {
365 insist('\n' == 0x0A);
366 insist('\r' == 0x0D);
367 insist(' ' == 0x20);
368 insist('0' == 0x30);
369 insist('9' == 0x39);
370 insist('A' == 0x41);
371 insist('Z' == 0x5A);
372 insist('a' == 0x61);
373 insist('z' == 0x7A);
374 /* addr.c */
375 /* asprintf.c */
376 /* authhash.c */
377 /* basen.c */
378 /* charset.c */
379 /* client.c */
380 /* configuration.c */
381 /* event.c */
382 /* fprintf.c */
383 /* hex.c */
384 test_hex();
385 /* inputline.c */
386 /* kvp.c */
387 /* log.c */
388 /* mem.c */
389 /* mime.c */
390 test_mime();
391 /* mixer.c */
392 /* plugin.c */
393 /* printf.c */
394 /* queue.c */
395 /* sink.c */
396 /* snprintf.c */
397 /* split.c */
398 /* syscalls.c */
399 /* table.c */
400 /* utf8.c */
401 test_utf8();
402 /* vector.c */
403 /* words.c */
404 test_casefold();
405 /* XXX words() */
406 /* wstat.c */
407 fprintf(stderr, "%d errors out of %d tests\n", errors, tests);
408 return !!errors;
409}
410
411/*
412Local Variables:
413c-basic-offset:2
414comment-column:40
415End:
416*/
417