Use uint32_t instead of unsigned long.
[libidn.git] / tst_ostringprep.c
blobbbb24bd9c76bb732228b226e8d11a5c2b85282f6
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"},
117 "Case folding ASCII U+0043 U+0041 U+0046 U+0045", "CAFE", "cafe"},
119 "Case folding 8bit U+00DF (german sharp s)", "\xC3\x9F", "ss"},
121 "Case folding U+0130 (turkish capital I with dot)",
122 "\xC4\xB0", "i\xcc\x87"},
124 "Case folding multibyte U+0143 U+037A",
125 "\xC5\x83\xCD\xBA", "\xC5\x84 \xCE\xB9"},
127 "Case folding U+2121 U+33C6 U+1D7BB",
128 "\xE2\x84\xA1\xE3\x8F\x86\xF0\x9D\x9E\xBB",
129 "telc\xE2\x88\x95" "kg\xCF\x83"},
131 "Normalization of U+006a U+030c U+00A0 U+00AA",
132 "\x6A\xCC\x8C\xC2\xA0\xC2\xAA", "\xC7\xB0 a"},
134 "Case folding U+1FB7 and normalization",
135 "\xE1\xBE\xB7", "\xE1\xBE\xB6\xCE\xB9"},
137 "Self-reverting case folding U+01F0 and normalization",
138 "\xC7\xB0", "\xC7\xB0"},
140 "Self-reverting case folding U+0390 and normalization",
141 "\xCE\x90", "\xCE\x90"},
143 "Self-reverting case folding U+03B0 and normalization",
144 "\xCE\xB0", "\xCE\xB0"},
146 "Self-reverting case folding U+1E96 and normalization",
147 "\xE1\xBA\x96", "\xE1\xBA\x96"},
149 "Self-reverting case folding U+1F56 and normalization",
150 "\xE1\xBD\x96", "\xE1\xBD\x96"},
152 "ASCII space character U+0020", "\x20", "\x20"},
154 "Non-ASCII 8bit space character U+00A0", "\xC2\xA0", "\x20"},
156 "Non-ASCII multibyte space character U+1680",
157 "\xE1\x9A\x80", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
159 "Non-ASCII multibyte space character U+2000", "\xE2\x80\x80", "\x20"},
161 "Zero Width Space U+200b", "\xE2\x80\x8b", ""},
163 "Non-ASCII multibyte space character U+3000", "\xE3\x80\x80", "\x20"},
165 "ASCII control characters U+0010 U+007F", "\x10\x7F", "\x10\x7F"},
167 "Non-ASCII 8bit control character U+0085",
168 "\xC2\x85", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
170 "Non-ASCII multibyte control character U+180E",
171 "\xE1\xA0\x8E", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
173 "Zero Width No-Break Space U+FEFF", "\xEF\xBB\xBF", ""},
175 "Non-ASCII control character U+1D175",
176 "\xF0\x9D\x85\xB5", NULL, "Nameprep", 0,
177 STRINGPREP_CONTAINS_PROHIBITED},
179 "Plane 0 private use character U+F123",
180 "\xEF\x84\xA3", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
182 "Plane 15 private use character U+F1234",
183 "\xF3\xB1\x88\xB4", NULL, "Nameprep", 0,
184 STRINGPREP_CONTAINS_PROHIBITED},
186 "Plane 16 private use character U+10F234",
187 "\xF4\x8F\x88\xB4", NULL, "Nameprep", 0,
188 STRINGPREP_CONTAINS_PROHIBITED},
190 "Non-character code point U+8FFFE",
191 "\xF2\x8F\xBF\xBE", NULL, "Nameprep", 0,
192 STRINGPREP_CONTAINS_PROHIBITED},
194 "Non-character code point U+10FFFF",
195 "\xF4\x8F\xBF\xBF", NULL, "Nameprep", 0,
196 STRINGPREP_CONTAINS_PROHIBITED},
198 "Surrogate code U+DF42",
199 "\xED\xBD\x82", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
201 "Non-plain text character U+FFFD",
202 "\xEF\xBF\xBD", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
204 "Ideographic description character U+2FF5",
205 "\xE2\xBF\xB5", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
207 "Display property character U+0341", "\xCD\x81", "\xCC\x81"},
209 "Left-to-right mark U+200E",
210 "\xE2\x80\x8E", "\xCC\x81", "Nameprep", 0,
211 STRINGPREP_CONTAINS_PROHIBITED},
213 "Deprecated U+202A",
214 "\xE2\x80\xAA", "\xCC\x81", "Nameprep", 0,
215 STRINGPREP_CONTAINS_PROHIBITED},
217 "Language tagging character U+E0001",
218 "\xF3\xA0\x80\x81", "\xCC\x81", "Nameprep", 0,
219 STRINGPREP_CONTAINS_PROHIBITED},
221 "Language tagging character U+E0042",
222 "\xF3\xA0\x81\x82", NULL, "Nameprep", 0,
223 STRINGPREP_CONTAINS_PROHIBITED},
225 "Bidi: RandALCat character U+05BE and LCat characters",
226 "foo\xD6\xBE" "bar", NULL, "Nameprep", 0,
227 STRINGPREP_BIDI_BOTH_L_AND_RAL},
229 "Bidi: RandALCat character U+FD50 and LCat characters",
230 "foo\xEF\xB5\x90" "bar", NULL, "Nameprep", 0,
231 STRINGPREP_BIDI_BOTH_L_AND_RAL},
233 "Bidi: RandALCat character U+FB38 and LCat characters",
234 "foo\xEF\xB9\xB6" "bar", "foo \xd9\x8e" "bar"},
236 "Bidi: RandALCat without trailing RandALCat U+0627 U+0031",
237 "\xD8\xA7\x31", NULL, "Nameprep", 0, STRINGPREP_BIDI_LEADTRAIL_NOT_RAL}
240 "Bidi: RandALCat character U+0627 U+0031 U+0628",
241 "\xD8\xA7\x31\xD8\xA8", "\xD8\xA7\x31\xD8\xA8"},
243 "Unassigned code point U+E0002",
244 "\xF3\xA0\x80\x82", NULL, "Nameprep", STRINGPREP_NO_UNASSIGNED,
245 STRINGPREP_CONTAINS_UNASSIGNED},
247 "Larger test (shrinking)",
248 "X\xC2\xAD\xC3\x9F\xC4\xB0\xE2\x84\xA1\x6a\xcc\x8c\xc2\xa0\xc2"
249 "\xaa\xce\xb0\xe2\x80\x80", "xssi\xcc\x87" "tel\xc7\xb0 a\xce\xb0 ",
250 "Nameprep"},
252 "Larger test (expanding)",
253 "X\xC3\x9F\xe3\x8c\x96\xC4\xB0\xE2\x84\xA1\xE2\x92\x9F\xE3\x8c\x80",
254 "xss\xe3\x82\xad\xe3\x83\xad\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\x88"
255 "\xe3\x83\xab" "i\xcc\x87" "tel\x28" "d\x29\xe3\x82\xa2\xe3\x83\x91"
256 "\xe3\x83\xbc\xe3\x83\x88"},
258 "Test of prohibited ASCII character U+0020",
259 "\x20", NULL, "generic", 0, STRINGPREP_CONTAINS_PROHIBITED},
261 "Test of NFKC U+00A0 and prohibited character U+0020",
262 "\xC2\xA0", NULL, "generic", 0, STRINGPREP_CONTAINS_PROHIBITED},
264 "Case map + normalization", "\xC2\xB5", "\xCE\xBC", "generic"},
265 /* The rest are rather non-interesting, but no point in removing
266 working test cases... */
268 "case_nonfkc", "\xC2\xB5", "\xCE\xBC", "generic", STRINGPREP_NO_NFKC}
271 "NFKC test", "\xC2\xAA", "\x61", "generic"},
273 "nameprep, exposed a bug in libstringprep 0.0.5",
274 "\xC2\xAA\x0A", "\x61\x0A"},
276 "unassigned code point U+0221", "\xC8\xA1", "\xC8\xA1", "generic"},
278 "Unassigned code point U+0221",
279 "\xC8\xA1", NULL, "generic", STRINGPREP_NO_UNASSIGNED,
280 STRINGPREP_CONTAINS_UNASSIGNED},
282 "Unassigned code point U+0236", "\xC8\xB6", "\xC8\xB6", "generic"},
284 "unassigned code point U+0236",
285 "\xC8\xB6", NULL, "generic", STRINGPREP_NO_UNASSIGNED,
286 STRINGPREP_CONTAINS_UNASSIGNED},
288 "bidi both RandALCat and LCat U+0627 U+00AA U+0628",
289 "\xD8\xA7\xC2\xAA\xD8\xA8", NULL, "generic", 0,
290 STRINGPREP_BIDI_BOTH_L_AND_RAL}
293 "XMPP node profile prohibited output",
294 "foo@bar", NULL, "Nodeprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
296 "XMPP resource profile on same string should work though",
297 "foo@bar", "foo@bar", "Resourceprep"},
299 "SASL ANONYMOUS plain mechanism",
300 "simon@josefsson.org", "simon@josefsson.org", "plain"},
302 "iSCSI profile", "Example-Name", "example-name", "ISCSIprep"},
304 "SASL profile", "Example\xC2\xA0" "Name", "Example Name", "SASLprep"}
308 main (int argc, char *argv[])
310 char *p;
311 int rc;
312 size_t i;
314 if (!stringprep_check_version (STRINGPREP_VERSION))
315 fail ("stringprep_check_version() failed\n");
318 if (strcmp (argv[argc - 1], "-v") == 0 ||
319 strcmp (argv[argc - 1], "--verbose") == 0)
320 debug = 1;
321 else if (strcmp (argv[argc - 1], "-b") == 0 ||
322 strcmp (argv[argc - 1], "--break-on-error") == 0)
323 break_on_error = 1;
324 else if (strcmp (argv[argc - 1], "-h") == 0 ||
325 strcmp (argv[argc - 1], "-?") == 0 ||
326 strcmp (argv[argc - 1], "--help") == 0)
328 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
329 argv[0]);
330 return 1;
332 while (argc-- > 1);
334 for (i = 0; i < sizeof (strprep) / sizeof (strprep[0]); i++)
336 if (debug)
337 printf ("STRINGPREP entry %d\n", i);
339 if (debug)
341 printf ("flags: %d\n", strprep[i].flags);
343 printf ("in: ");
344 escapeprint (strprep[i].in, strlen (strprep[i].in));
345 hexprint (strprep[i].in, strlen (strprep[i].in));
346 puts ("");
347 binprint (strprep[i].in, strlen (strprep[i].in));
348 puts ("");
352 unsigned long *l;
353 char *x;
354 l = stringprep_utf8_to_ucs4 (strprep[i].in, -1, NULL);
355 x = stringprep_ucs4_to_utf8 (l, -1, NULL, NULL);
357 if (strcmp (strprep[i].in, x) != 0)
359 fail ("bad UTF-8 in entry %d\n", i);
360 if (debug)
362 puts ("expected:");
363 escapeprint (strprep[i].in, strlen (strprep[i].in));
364 hexprint (strprep[i].in, strlen (strprep[i].in));
365 puts ("\ncomputed:");
366 escapeprint (x, strlen (x));
367 hexprint (x, strlen (x));
368 puts ("");
372 rc = stringprep_profile (strprep[i].in, &p,
373 strprep[i].profile ?
374 strprep[i].profile :
375 "Nameprep", strprep[i].flags);
376 if (rc != strprep[i].rc)
378 fail ("stringprep() entry %d failed: %d\n", i, rc);
379 if (debug)
380 printf ("FATAL\n");
381 if (rc == STRINGPREP_OK)
382 free (p);
383 continue;
386 if (debug && rc == STRINGPREP_OK)
388 printf ("out: ");
389 escapeprint (p, strlen (p));
390 hexprint (p, strlen (p));
391 puts ("");
392 binprint (p, strlen (p));
393 puts ("");
395 printf ("expected out: ");
396 escapeprint (strprep[i].out, strlen (strprep[i].out));
397 hexprint (strprep[i].out, strlen (strprep[i].out));
398 puts ("");
399 binprint (strprep[i].out, strlen (strprep[i].out));
400 puts ("");
402 else if (debug)
403 printf ("returned %d expected %d\n", rc, strprep[i].rc);
405 if (rc == STRINGPREP_OK)
407 if (strlen (strprep[i].out) != strlen (p) ||
408 memcmp (strprep[i].out, p, strlen (p)) != 0)
410 fail ("stringprep() entry %d failed\n", i);
411 if (debug)
412 printf ("ERROR\n");
414 else if (debug)
415 printf ("OK\n\n");
417 free (p);
419 else if (debug)
420 printf ("OK\n\n");
423 #if 0
425 char p[20];
426 memset (p, 0, 10);
427 stringprep_unichar_to_utf8 (0x00DF, p);
428 hexprint (p, strlen (p));
429 puts ("");
431 #endif
433 if (debug)
434 printf ("Stringprep self tests done with %d errors\n", error_count);
436 return error_count ? 1 : 0;