Add.
[libidn.git] / tst_stringprep.c
blob9d828ada32c1a7fc482dedaa59aae68a761d6c67
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 #if !defined(DRAFT)
319 { "Test of prohibited ASCII character U+0020",
320 "\x20", NULL, "generic", 0, STRINGPREP_CONTAINS_PROHIBITED
323 "Test of NFKC U+00A0 and prohibited character U+0020",
324 "\xC2\xA0", NULL, "generic", 0, STRINGPREP_CONTAINS_PROHIBITED
326 { "Case map + normalization",
327 "\xC2\xB5", "\xCE\xBC", "generic"},
328 /* The rest are rather non-interesting, but no point in removing
329 working test cases... */
330 { "case_nonfkc",
331 "\xC2\xB5", "\xCE\xBC", "generic", STRINGPREP_NO_NFKC}
333 { "NFKC test",
334 "\xC2\xAA", "\x61", "generic"},
335 { "nameprep, exposed a bug in libstringprep 0.0.5",
336 "\xC2\xAA\x0A", "\x61\x0A"},
337 { "unassigned code point U+0221",
338 "\xC8\xA1", "\xC8\xA1", "generic"},
339 { "Unassigned code point U+0221",
340 "\xC8\xA1", NULL, "generic", STRINGPREP_NO_UNASSIGNED,
341 STRINGPREP_CONTAINS_UNASSIGNED},
342 { "Unassigned code point U+0236",
343 "\xC8\xB6", "\xC8\xB6", "generic"},
344 { "unassigned code point U+0236",
345 "\xC8\xB6", NULL, "generic", STRINGPREP_NO_UNASSIGNED,
346 STRINGPREP_CONTAINS_UNASSIGNED},
347 { "bidi both RandALCat and LCat U+0627 U+00AA U+0628",
348 "\xD8\xA7\xC2\xAA\xD8\xA8", NULL, "generic", 0,
349 STRINGPREP_BIDI_BOTH_L_AND_RAL}
351 { "XMPP node profile prohibited output",
352 "foo@bar", NULL, "Nodeprep", 0,
353 STRINGPREP_CONTAINS_PROHIBITED},
354 { "XMPP resource profile on same string should work though",
355 "foo@bar", "foo@bar", "Resourceprep"},
356 { "SASL ANONYMOUS plain mechanism",
357 "simon@josefsson.org", "simon@josefsson.org", "plain"},
358 { "iSCSI profile",
359 "Example-Name", "example-name", "ISCSIprep"},
360 { "SASL profile",
361 "Example\xC2\xA0""Name", "Example Name", "SASLprep"}
362 #endif
366 main (int argc, char *argv[])
368 char *p;
369 int rc;
370 size_t i;
372 if (!stringprep_check_version (STRINGPREP_VERSION))
373 fail ("stringprep_check_version() failed\n");
376 if (strcmp (argv[argc - 1], "-v") == 0 ||
377 strcmp (argv[argc - 1], "--verbose") == 0)
378 debug = 1;
379 else if (strcmp (argv[argc - 1], "-b") == 0 ||
380 strcmp (argv[argc - 1], "--break-on-error") == 0)
381 break_on_error = 1;
382 else if (strcmp (argv[argc - 1], "-h") == 0 ||
383 strcmp (argv[argc - 1], "-?") == 0 ||
384 strcmp (argv[argc - 1], "--help") == 0)
386 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
387 argv[0]);
388 return 1;
390 while (argc-- > 1);
392 for (i = 0; i < sizeof (strprep) / sizeof (strprep[0]); i++)
394 #ifdef DRAFT
395 printf ("<section title=\"%s\">\n", strprep[i].comment);
396 printf ("\n");
397 printf ("<figure>\n");
398 printf ("<artwork>\n");
399 printf ("in");
400 escapeprint (strprep[i].in, strlen (strprep[i].in));
401 printf ("\n");
402 #endif
403 if (debug)
404 printf ("STRINGPREP entry %d\n", i);
406 if (debug)
408 printf ("flags: %d\n", strprep[i].flags);
410 printf ("in: ");
411 escapeprint (strprep[i].in, strlen (strprep[i].in));
412 hexprint (strprep[i].in, strlen (strprep[i].in));
413 puts ("");
414 binprint (strprep[i].in, strlen (strprep[i].in));
415 puts ("");
419 unsigned long *l;
420 char *x;
421 l = stringprep_utf8_to_ucs4 (strprep[i].in, -1, NULL);
422 x = stringprep_ucs4_to_utf8 (l, -1, NULL, NULL);
424 if (strcmp(strprep[i].in, x) != 0)
426 fail ("bad UTF-8 in entry %d\n", i);
427 if (debug)
429 puts("expected:");
430 escapeprint (strprep[i].in, strlen (strprep[i].in));
431 hexprint (strprep[i].in, strlen (strprep[i].in));
432 puts("\ncomputed:");
433 escapeprint (x, strlen (x));
434 hexprint (x, strlen (x));
435 puts("");
439 rc = stringprep_profile (strprep[i].in, &p,
440 strprep[i].profile ?
441 strprep[i].profile :
442 "Nameprep",
443 strprep[i].flags);
444 if (rc != strprep[i].rc)
446 fail ("stringprep() entry %d failed: %d\n", i, rc);
447 if (debug)
448 printf ("FATAL\n");
449 if (rc == STRINGPREP_OK)
450 free(p);
451 continue;
454 #ifdef DRAFT
455 if (rc == STRINGPREP_OK)
457 printf ("out");
458 escapeprint (p, strlen (p));
460 #endif
462 if (debug && rc == STRINGPREP_OK)
464 printf ("out: ");
465 escapeprint (p, strlen (p));
466 hexprint (p, strlen (p));
467 puts ("");
468 binprint (p, strlen (p));
469 puts ("");
471 printf ("expected out: ");
472 escapeprint (strprep[i].out, strlen (strprep[i].out));
473 hexprint (strprep[i].out, strlen (strprep[i].out));
474 puts ("");
475 binprint (strprep[i].out, strlen (strprep[i].out));
476 puts ("");
478 else if (debug)
479 printf ("returned %d expected %d\n", rc, strprep[i].rc);
481 if (rc == STRINGPREP_OK)
483 if (strlen (strprep[i].out) != strlen (p) ||
484 memcmp (strprep[i].out, p, strlen (p)) != 0)
486 fail ("stringprep() entry %d failed\n", i);
487 if (debug)
488 printf ("ERROR\n");
490 else if (debug)
491 printf ("OK\n\n");
493 free (p);
495 else if (debug)
496 printf ("OK\n\n");
498 #ifdef DRAFT
499 printf("</artwork>\n");
500 printf("</figure>\n");
501 printf("\n");
502 printf("</section>\n");
503 #endif
506 #if 0
507 { char p[20];
508 memset (p, 0, 10);
509 stringprep_unichar_to_utf8 (0x00DF, p);
510 hexprint (p, strlen (p));
511 puts ("");
513 #endif
515 if (debug)
516 printf ("Stringprep self tests done with %d errors\n", error_count);
518 return error_count ? 1 : 0;