Change unsigned long to uint32_t.
[libidn.git] / tst_stringprep.c
blobbd0ced3535bec16ba36f414f096d1354107ed5ad
1 /* tst_stringprep.c Self tests for stringprep().
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNU Libidn is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Libidn; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "internal.h"
24 static int debug = 0;
25 static int error_count = 0;
26 static int break_on_error = 0;
28 static void
29 fail (const char *format, ...)
31 va_list arg_ptr;
33 va_start (arg_ptr, format);
34 vfprintf (stderr, format, arg_ptr);
35 va_end (arg_ptr);
36 error_count++;
37 if (break_on_error)
38 exit (1);
41 static void
42 escapeprint (char *str, int len)
44 int i;
46 printf (" (length %d bytes):\n\t", len);
47 for (i = 0; i < len; i++)
49 if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
50 ((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
51 ((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
52 || (str[i] & 0xFF) == ' ' || (str[i] & 0xFF) == '.')
53 printf ("%c", (str[i] & 0xFF));
54 else
55 printf ("\\x%02X", (str[i] & 0xFF));
56 if ((i+1)%16 == 0 && (i+1) < len)
57 printf("'\n\t'");
61 static void
62 hexprint (char *str, int len)
64 int i;
66 printf ("\t;; ");
67 for (i = 0; i < len; i++)
69 printf ("%02x ", (str[i] & 0xFF));
70 if ((i + 1) % 8 == 0)
71 printf (" ");
72 if ((i + 1) % 16 == 0 && i + 1 < len)
73 printf ("\n\t;; ");
77 static void
78 binprint (char *str, int len)
80 int i;
82 printf ("\t;; ");
83 for (i = 0; i < len; i++)
85 printf ("%d%d%d%d%d%d%d%d ",
86 (str[i] & 0xFF) & 0x80 ? 1 : 0,
87 (str[i] & 0xFF) & 0x40 ? 1 : 0,
88 (str[i] & 0xFF) & 0x20 ? 1 : 0,
89 (str[i] & 0xFF) & 0x10 ? 1 : 0,
90 (str[i] & 0xFF) & 0x08 ? 1 : 0,
91 (str[i] & 0xFF) & 0x04 ? 1 : 0,
92 (str[i] & 0xFF) & 0x02 ? 1 : 0, (str[i] & 0xFF) & 0x01 ? 1 : 0);
93 if ((i + 1) % 3 == 0)
94 printf (" ");
95 if ((i + 1) % 6 == 0 && i + 1 < len)
96 printf ("\n\t;; ");
100 struct stringprep
102 char *comment;
103 char *in;
104 char *out;
105 char *profile;
106 int flags;
107 int rc;
109 strprep[] =
112 "Map to nothing",
113 "foo\xC2\xAD\xCD\x8F\xE1\xA0\x86\xE1\xA0\x8B"
114 "bar""\xE2\x80\x8B\xE2\x81\xA0""baz\xEF\xB8\x80\xEF\xB8\x88"
115 "\xEF\xB8\x8F\xEF\xBB\xBF", "foobarbaz"
118 "Case folding ASCII U+0043 U+0041 U+0046 U+0045",
119 "CAFE", "cafe"
122 "Case folding 8bit U+00DF (german sharp s)",
123 "\xC3\x9F", "ss"
126 "Case folding U+0130 (turkish capital I with dot)",
127 "\xC4\xB0", "i\xcc\x87"
130 "Case folding multibyte U+0143 U+037A",
131 "\xC5\x83\xCD\xBA", "\xC5\x84 \xCE\xB9"
134 "Case folding U+2121 U+33C6 U+1D7BB",
135 "\xE2\x84\xA1\xE3\x8F\x86\xF0\x9D\x9E\xBB",
136 "telc\xE2\x88\x95""kg\xCF\x83"
139 "Normalization of U+006a U+030c U+00A0 U+00AA",
140 "\x6A\xCC\x8C\xC2\xA0\xC2\xAA", "\xC7\xB0 a"
143 "Case folding U+1FB7 and normalization",
144 "\xE1\xBE\xB7", "\xE1\xBE\xB6\xCE\xB9"
147 "Self-reverting case folding U+01F0 and normalization",
148 "\xC7\xB0", "\xC7\xB0"
151 "Self-reverting case folding U+0390 and normalization",
152 "\xCE\x90", "\xCE\x90"
155 "Self-reverting case folding U+03B0 and normalization",
156 "\xCE\xB0", "\xCE\xB0"
159 "Self-reverting case folding U+1E96 and normalization",
160 "\xE1\xBA\x96", "\xE1\xBA\x96"
163 "Self-reverting case folding U+1F56 and normalization",
164 "\xE1\xBD\x96", "\xE1\xBD\x96"
167 "ASCII space character U+0020",
168 "\x20", "\x20"
171 "Non-ASCII 8bit space character U+00A0",
172 "\xC2\xA0", "\x20"
175 "Non-ASCII multibyte space character U+1680",
176 "\xE1\x9A\x80", NULL, "Nameprep", 0,
177 STRINGPREP_CONTAINS_PROHIBITED
180 "Non-ASCII multibyte space character U+2000",
181 "\xE2\x80\x80", "\x20"
184 "Zero Width Space U+200b",
185 "\xE2\x80\x8b", ""
188 "Non-ASCII multibyte space character U+3000",
189 "\xE3\x80\x80", "\x20"
192 "ASCII control characters U+0010 U+007F",
193 "\x10\x7F", "\x10\x7F"
196 "Non-ASCII 8bit control character U+0085",
197 "\xC2\x85", NULL, "Nameprep", 0,
198 STRINGPREP_CONTAINS_PROHIBITED
201 "Non-ASCII multibyte control character U+180E",
202 "\xE1\xA0\x8E", NULL, "Nameprep", 0,
203 STRINGPREP_CONTAINS_PROHIBITED
206 "Zero Width No-Break Space U+FEFF",
207 "\xEF\xBB\xBF", ""
210 "Non-ASCII control character U+1D175",
211 "\xF0\x9D\x85\xB5", NULL, "Nameprep", 0,
212 STRINGPREP_CONTAINS_PROHIBITED
215 "Plane 0 private use character U+F123",
216 "\xEF\x84\xA3", NULL, "Nameprep", 0,
217 STRINGPREP_CONTAINS_PROHIBITED
220 "Plane 15 private use character U+F1234",
221 "\xF3\xB1\x88\xB4", NULL, "Nameprep", 0,
222 STRINGPREP_CONTAINS_PROHIBITED
225 "Plane 16 private use character U+10F234",
226 "\xF4\x8F\x88\xB4", NULL, "Nameprep", 0,
227 STRINGPREP_CONTAINS_PROHIBITED
230 "Non-character code point U+8FFFE",
231 "\xF2\x8F\xBF\xBE", NULL, "Nameprep", 0,
232 STRINGPREP_CONTAINS_PROHIBITED
235 "Non-character code point U+10FFFF",
236 "\xF4\x8F\xBF\xBF", NULL, "Nameprep", 0,
237 STRINGPREP_CONTAINS_PROHIBITED
240 "Surrogate code U+DF42",
241 "\xED\xBD\x82", NULL, "Nameprep", 0,
242 STRINGPREP_CONTAINS_PROHIBITED
245 "Non-plain text character U+FFFD",
246 "\xEF\xBF\xBD", NULL, "Nameprep", 0,
247 STRINGPREP_CONTAINS_PROHIBITED
250 "Ideographic description character U+2FF5",
251 "\xE2\xBF\xB5", NULL, "Nameprep", 0,
252 STRINGPREP_CONTAINS_PROHIBITED
255 "Display property character U+0341",
256 "\xCD\x81", "\xCC\x81"
259 "Left-to-right mark U+200E",
260 "\xE2\x80\x8E", "\xCC\x81", "Nameprep", 0,
261 STRINGPREP_CONTAINS_PROHIBITED
264 "Deprecated U+202A",
265 "\xE2\x80\xAA", "\xCC\x81", "Nameprep", 0,
266 STRINGPREP_CONTAINS_PROHIBITED
269 "Language tagging character U+E0001",
270 "\xF3\xA0\x80\x81", "\xCC\x81", "Nameprep", 0,
271 STRINGPREP_CONTAINS_PROHIBITED
274 "Language tagging character U+E0042",
275 "\xF3\xA0\x81\x82", NULL, "Nameprep", 0,
276 STRINGPREP_CONTAINS_PROHIBITED
279 "Bidi: RandALCat character U+05BE and LCat characters",
280 "foo\xD6\xBE""bar", NULL, "Nameprep", 0,
281 STRINGPREP_BIDI_BOTH_L_AND_RAL
284 "Bidi: RandALCat character U+FD50 and LCat characters",
285 "foo\xEF\xB5\x90""bar", NULL, "Nameprep", 0,
286 STRINGPREP_BIDI_BOTH_L_AND_RAL
289 "Bidi: RandALCat character U+FB38 and LCat characters",
290 "foo\xEF\xB9\xB6""bar", "foo \xd9\x8e""bar"
292 { "Bidi: RandALCat without trailing RandALCat U+0627 U+0031",
293 "\xD8\xA7\x31", NULL, "Nameprep", 0,
294 STRINGPREP_BIDI_LEADTRAIL_NOT_RAL}
297 "Bidi: RandALCat character U+0627 U+0031 U+0628",
298 "\xD8\xA7\x31\xD8\xA8", "\xD8\xA7\x31\xD8\xA8"
301 "Unassigned code point U+E0002",
302 "\xF3\xA0\x80\x82", NULL, "Nameprep", STRINGPREP_NO_UNASSIGNED,
303 STRINGPREP_CONTAINS_UNASSIGNED
306 "Larger test (shrinking)",
307 "X\xC2\xAD\xC3\x9F\xC4\xB0\xE2\x84\xA1\x6a\xcc\x8c\xc2\xa0\xc2"
308 "\xaa\xce\xb0\xe2\x80\x80", "xssi\xcc\x87""tel\xc7\xb0 a\xce\xb0 ",
309 "Nameprep"
312 "Larger test (expanding)",
313 "X\xC3\x9F\xe3\x8c\x96\xC4\xB0\xE2\x84\xA1\xE2\x92\x9F\xE3\x8c\x80",
314 "xss\xe3\x82\xad\xe3\x83\xad\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\x88"
315 "\xe3\x83\xab""i\xcc\x87""tel\x28""d\x29\xe3\x82\xa2\xe3\x83\x91"
316 "\xe3\x83\xbc\xe3\x83\x88"
318 { "Test of prohibited ASCII character U+0020",
319 "\x20", NULL, "generic", 0, STRINGPREP_CONTAINS_PROHIBITED
322 "Test of NFKC U+00A0 and prohibited character U+0020",
323 "\xC2\xA0", NULL, "generic", 0, STRINGPREP_CONTAINS_PROHIBITED
325 { "Case map + normalization",
326 "\xC2\xB5", "\xCE\xBC", "generic"},
327 /* The rest are rather non-interesting, but no point in removing
328 working test cases... */
329 { "case_nonfkc",
330 "\xC2\xB5", "\xCE\xBC", "generic", STRINGPREP_NO_NFKC}
332 { "NFKC test",
333 "\xC2\xAA", "\x61", "generic"},
334 { "nameprep, exposed a bug in libstringprep 0.0.5",
335 "\xC2\xAA\x0A", "\x61\x0A"},
336 { "unassigned code point U+0221",
337 "\xC8\xA1", "\xC8\xA1", "generic"},
338 { "Unassigned code point U+0221",
339 "\xC8\xA1", NULL, "generic", STRINGPREP_NO_UNASSIGNED,
340 STRINGPREP_CONTAINS_UNASSIGNED},
341 { "Unassigned code point U+0236",
342 "\xC8\xB6", "\xC8\xB6", "generic"},
343 { "unassigned code point U+0236",
344 "\xC8\xB6", NULL, "generic", STRINGPREP_NO_UNASSIGNED,
345 STRINGPREP_CONTAINS_UNASSIGNED},
346 { "bidi both RandALCat and LCat U+0627 U+00AA U+0628",
347 "\xD8\xA7\xC2\xAA\xD8\xA8", NULL, "generic", 0,
348 STRINGPREP_BIDI_BOTH_L_AND_RAL}
350 { "XMPP node profile prohibited output",
351 "foo@bar", NULL, "Nodeprep", 0,
352 STRINGPREP_CONTAINS_PROHIBITED},
353 { "XMPP resource profile on same string should work though",
354 "foo@bar", "foo@bar", "Resourceprep"},
355 { "SASL ANONYMOUS plain mechanism",
356 "simon@josefsson.org", "simon@josefsson.org", "plain"},
357 { "iSCSI profile",
358 "Example-Name", "example-name", "ISCSIprep"},
359 { "SASL profile",
360 "Example\xC2\xA0""Name", "Example Name", "SASLprep"}
364 main (int argc, char *argv[])
366 char *p;
367 int rc;
368 size_t i;
370 if (!stringprep_check_version (STRINGPREP_VERSION))
371 fail ("stringprep_check_version() failed\n");
374 if (strcmp (argv[argc - 1], "-v") == 0 ||
375 strcmp (argv[argc - 1], "--verbose") == 0)
376 debug = 1;
377 else if (strcmp (argv[argc - 1], "-b") == 0 ||
378 strcmp (argv[argc - 1], "--break-on-error") == 0)
379 break_on_error = 1;
380 else if (strcmp (argv[argc - 1], "-h") == 0 ||
381 strcmp (argv[argc - 1], "-?") == 0 ||
382 strcmp (argv[argc - 1], "--help") == 0)
384 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
385 argv[0]);
386 return 1;
388 while (argc-- > 1);
390 for (i = 0; i < sizeof (strprep) / sizeof (strprep[0]); i++)
392 if (debug)
393 printf ("STRINGPREP entry %d\n", i);
395 if (debug)
397 printf ("flags: %d\n", strprep[i].flags);
399 printf ("in: ");
400 escapeprint (strprep[i].in, strlen (strprep[i].in));
401 hexprint (strprep[i].in, strlen (strprep[i].in));
402 puts ("");
403 binprint (strprep[i].in, strlen (strprep[i].in));
404 puts ("");
408 unsigned long *l;
409 char *x;
410 l = stringprep_utf8_to_ucs4 (strprep[i].in, -1, NULL);
411 x = stringprep_ucs4_to_utf8 (l, -1, NULL, NULL);
413 if (strcmp(strprep[i].in, x) != 0)
415 fail ("bad UTF-8 in entry %d\n", i);
416 if (debug)
418 puts("expected:");
419 escapeprint (strprep[i].in, strlen (strprep[i].in));
420 hexprint (strprep[i].in, strlen (strprep[i].in));
421 puts("\ncomputed:");
422 escapeprint (x, strlen (x));
423 hexprint (x, strlen (x));
424 puts("");
428 rc = stringprep_profile (strprep[i].in, &p,
429 strprep[i].profile ?
430 strprep[i].profile :
431 "Nameprep",
432 strprep[i].flags);
433 if (rc != strprep[i].rc)
435 fail ("stringprep() entry %d failed: %d\n", i, rc);
436 if (debug)
437 printf ("FATAL\n");
438 if (rc == STRINGPREP_OK)
439 free(p);
440 continue;
443 if (debug && rc == STRINGPREP_OK)
445 printf ("out: ");
446 escapeprint (p, strlen (p));
447 hexprint (p, strlen (p));
448 puts ("");
449 binprint (p, strlen (p));
450 puts ("");
452 printf ("expected out: ");
453 escapeprint (strprep[i].out, strlen (strprep[i].out));
454 hexprint (strprep[i].out, strlen (strprep[i].out));
455 puts ("");
456 binprint (strprep[i].out, strlen (strprep[i].out));
457 puts ("");
459 else if (debug)
460 printf ("returned %d expected %d\n", rc, strprep[i].rc);
462 if (rc == STRINGPREP_OK)
464 if (strlen (strprep[i].out) != strlen (p) ||
465 memcmp (strprep[i].out, p, strlen (p)) != 0)
467 fail ("stringprep() entry %d failed\n", i);
468 if (debug)
469 printf ("ERROR\n");
471 else if (debug)
472 printf ("OK\n\n");
474 free (p);
476 else if (debug)
477 printf ("OK\n\n");
480 #if 0
481 { char p[20];
482 memset (p, 0, 10);
483 stringprep_unichar_to_utf8 (0x00DF, p);
484 hexprint (p, strlen (p));
485 puts ("");
487 #endif
489 if (debug)
490 printf ("Stringprep self tests done with %d errors\n", error_count);
492 return error_count ? 1 : 0;