Release 0.0.7 (version 0.0.6 was abandoned).
[libidn.git] / tst_stringprep.c
blobba1a7f7a91d25a3eec1d5de726d0afe2485fcebe
1 /* tst_stringprep.c libstringprep self tests for stringprep_stringprep()
2 * Copyright (C) 2002 Simon Josefsson
4 * This file is part of libstringprep.
6 * Libstringprep 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 * Libstringprep 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 libstringprep; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "internal.h"
23 #include <stringprep_nameprep.h>
25 static int debug = 0;
26 static int error_count = 0;
27 static int break_on_error = 0;
29 static void
30 fail (const char *format, ...)
32 va_list arg_ptr;
34 va_start (arg_ptr, format);
35 vfprintf (stderr, format, arg_ptr);
36 va_end (arg_ptr);
37 error_count++;
38 if (break_on_error)
39 exit (1);
42 static void
43 escapeprint (char *str, int len)
45 int i;
47 printf ("\t;; `");
48 for (i = 0; i < len; i++)
50 if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
51 ((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
52 ((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
53 || (str[i] & 0xFF) == '.')
54 printf ("%c", (str[i] & 0xFF));
55 else
56 printf ("\\x%02x", (str[i] & 0xFF));
58 printf ("' (length %d bytes)\n", len);
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 *in;
103 int flags;
104 char *out;
105 Stringprep_profile *profile;
106 int rc;
108 strprep[] =
110 /* map to nothing U+00AD */
112 "foo\xC2\xAD" "bar", 0, "foobar", stringprep_generic}
114 /* map case_nfkc + normalization: */
116 "\xC2\xB5", 0, "\xCE\xBC", stringprep_generic}
118 /* case_nonfkc: */
120 "\xC2\xB5", STRINGPREP_NO_NFKC, "\xCE\xBC", stringprep_generic}
123 "\xC2\xAA", 0, "\x61", stringprep_generic}
125 /* nameprep, exposed a bug in libstringprep 0.0.5 */
127 "\xC2\xAA\x0A", 0, "\x61\x0A", stringprep_nameprep}
129 /* unassigned code point U+0221: */
131 "\xC8\xA1", 0, "\xC8\xA1", stringprep_generic}
133 /* unassigned code point U+0221: */
135 "\xC8\xA1", STRINGPREP_NO_UNASSIGNED, NULL, stringprep_generic,
136 STRINGPREP_CONTAINS_UNASSIGNED}
138 /* unassigned code point U+0236: */
140 "\xC8\xB6", 0, "\xC8\xB6", stringprep_generic}
142 /* unassigned code point U+0236: */
144 "\xC8\xB6", STRINGPREP_NO_UNASSIGNED, NULL, stringprep_generic,
145 STRINGPREP_CONTAINS_UNASSIGNED},
146 /* prohibited ASCII character U+0020: */
148 "\x20", 0, NULL, stringprep_generic,
149 STRINGPREP_CONTAINS_PROHIBITED},
150 /* prohibited character U+00A0: */
152 "\xC2\xA0", 0, NULL, stringprep_generic,
153 STRINGPREP_CONTAINS_PROHIBITED},
154 /* prohibited non-character U+10FFFE: */
156 "\xF4\x8F\xBF\xBE", 0, NULL, stringprep_generic,
157 STRINGPREP_CONTAINS_PROHIBITED},
158 /* prohibited surrogate character U+D801: */
160 "\xED\xA0\x81", 0, NULL, stringprep_generic,
161 STRINGPREP_CONTAINS_PROHIBITED},
162 /* bidi RandALCat without trailing RandALCat <U+0627><U+0031>: */
164 "\xD8\xA7\x31", 0, NULL, stringprep_generic,
165 STRINGPREP_BIDI_LEADTRAIL_NOT_RAL},
166 /* bidi RandALCat correct <U+0627><U+0031><U+0628>: */
168 "\xD8\xA7\x31\xD8\xA8", 0, "\xD8\xA7\x31\xD8\xA8", stringprep_generic},
169 /* bidi both RandALCat and LCat <U+0627><U+00AA><U+0628>: */
171 "\xD8\xA7\xC2\xAA\xD8\xA8", 0, NULL, stringprep_generic,
172 STRINGPREP_BIDI_BOTH_L_AND_RAL},
173 /* case mapping (this triggered a bug in 0.0.5) */
175 "CAFE", 0, "cafe", stringprep_generic}
179 main (int argc, char *argv[])
181 char *p;
182 int rc, i;
185 if (strcmp (argv[argc - 1], "-v") == 0 ||
186 strcmp (argv[argc - 1], "--verbose") == 0)
187 debug = 1;
188 else if (strcmp (argv[argc - 1], "-b") == 0 ||
189 strcmp (argv[argc - 1], "--break-on-error") == 0)
190 break_on_error = 1;
191 else if (strcmp (argv[argc - 1], "-h") == 0 ||
192 strcmp (argv[argc - 1], "-?") == 0 ||
193 strcmp (argv[argc - 1], "--help") == 0)
195 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
196 argv[0]);
197 return 1;
199 while (argc-- > 1);
201 p = malloc (BUFSIZ);
202 if (p == NULL)
203 fail ("malloc() returned NULL\n");
205 for (i = 0; i < sizeof (strprep) / sizeof (strprep[0]); i++)
207 if (debug)
208 printf ("STRINGPREP entry %d\n", i);
210 strcpy (p, strprep[i].in);
212 if (debug)
214 printf ("flags: %d\n", strprep[i].flags);
216 printf ("in:\n");
217 escapeprint (strprep[i].in, strlen (strprep[i].in));
218 hexprint (strprep[i].in, strlen (strprep[i].in));
219 puts ("");
220 binprint (strprep[i].in, strlen (strprep[i].in));
221 puts ("");
224 rc = stringprep (p, BUFSIZ, strprep[i].flags, strprep[i].profile);
225 if (rc != strprep[i].rc)
227 fail ("stringprep() entry %d failed: %d\n", i, rc);
228 if (debug)
229 printf ("FATAL\n");
230 continue;
233 if (debug && rc == STRINGPREP_OK)
235 printf ("out:\n");
236 escapeprint (p, strlen (p));
237 hexprint (p, strlen (p));
238 puts ("");
239 binprint (p, strlen (p));
240 puts ("");
242 printf ("expected out:\n");
243 escapeprint (strprep[i].out, strlen (strprep[i].out));
244 hexprint (strprep[i].out, strlen (strprep[i].out));
245 puts ("");
246 binprint (strprep[i].out, strlen (strprep[i].out));
247 puts ("");
249 else if (debug)
250 printf("returned %d expected %d\n", rc, strprep[i].rc);
252 if (rc == STRINGPREP_OK)
254 if (strlen (strprep[i].out) != strlen (p) ||
255 memcmp (strprep[i].out, p, strlen (p)) != 0)
257 fail ("stringprep() entry %d failed\n", i);
258 if (debug)
259 printf ("ERROR\n");
261 else if (debug)
262 printf ("OK\n\n");
264 else if (debug)
265 printf ("OK\n\n");
268 free (p);
270 #if 0
271 memset(p, 0, 10);
272 stringprep_unichar_to_utf8 (0x0628, p);
273 hexprint (p, strlen (p)); puts("");
274 #endif
276 if (debug)
277 printf ("Stringprep self tests done with %d errors\n", error_count);
279 return error_count ? 1 : 0;