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
25 static int error_count
= 0;
26 static int break_on_error
= 0;
29 fail (const char *format
, ...)
33 va_start (arg_ptr
, format
);
34 vfprintf (stderr
, format
, arg_ptr
);
42 escapeprint (char *str
, int len
)
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));
55 printf ("\\x%02X", (str
[i
] & 0xFF));
56 if ((i
+1)%16 == 0 && (i
+1) < len
)
62 hexprint (char *str
, int len
)
67 for (i
= 0; i
< len
; i
++)
69 printf ("%02x ", (str
[i
] & 0xFF));
72 if ((i
+ 1) % 16 == 0 && i
+ 1 < len
)
78 binprint (char *str
, int len
)
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);
95 if ((i
+ 1) % 6 == 0 && i
+ 1 < len
)
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",
122 "Case folding 8bit U+00DF (german sharp s)",
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",
171 "Non-ASCII 8bit space character U+00A0",
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",
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",
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
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 ",
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... */
330 "\xC2\xB5", "\xCE\xBC", "generic", STRINGPREP_NO_NFKC
}
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"},
358 "Example-Name", "example-name", "ISCSIprep"},
360 "Example\xC2\xA0""Name", "Example Name", "SASLprep"}
364 main (int argc
, char *argv
[])
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)
377 else if (strcmp (argv
[argc
- 1], "-b") == 0 ||
378 strcmp (argv
[argc
- 1], "--break-on-error") == 0)
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",
390 for (i
= 0; i
< sizeof (strprep
) / sizeof (strprep
[0]); i
++)
393 printf ("STRINGPREP entry %d\n", i
);
397 printf ("flags: %d\n", strprep
[i
].flags
);
400 escapeprint (strprep
[i
].in
, strlen (strprep
[i
].in
));
401 hexprint (strprep
[i
].in
, strlen (strprep
[i
].in
));
403 binprint (strprep
[i
].in
, strlen (strprep
[i
].in
));
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
);
419 escapeprint (strprep
[i
].in
, strlen (strprep
[i
].in
));
420 hexprint (strprep
[i
].in
, strlen (strprep
[i
].in
));
422 escapeprint (x
, strlen (x
));
423 hexprint (x
, strlen (x
));
428 rc
= stringprep_profile (strprep
[i
].in
, &p
,
433 if (rc
!= strprep
[i
].rc
)
435 fail ("stringprep() entry %d failed: %d\n", i
, rc
);
438 if (rc
== STRINGPREP_OK
)
443 if (debug
&& rc
== STRINGPREP_OK
)
446 escapeprint (p
, strlen (p
));
447 hexprint (p
, strlen (p
));
449 binprint (p
, strlen (p
));
452 printf ("expected out: ");
453 escapeprint (strprep
[i
].out
, strlen (strprep
[i
].out
));
454 hexprint (strprep
[i
].out
, strlen (strprep
[i
].out
));
456 binprint (strprep
[i
].out
, strlen (strprep
[i
].out
));
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
);
483 stringprep_unichar_to_utf8 (0x00DF, p
);
484 hexprint (p
, strlen (p
));
490 printf ("Stringprep self tests done with %d errors\n", error_count
);
492 return error_count
? 1 : 0;