Line data Source code
1 : /* tst_stringprep.c --- Self tests for stringprep().
2 : * Copyright (C) 2002-2020 Simon Josefsson
3 : *
4 : * This file is part of GNU Libidn.
5 : *
6 : * This program 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 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program 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.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : *
19 : */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : # include "config.h"
23 : #endif
24 :
25 : #include <stdio.h>
26 : #include <stdlib.h>
27 : #include <stdarg.h>
28 : #include <string.h>
29 :
30 : #include <stringprep.h>
31 :
32 : #include "utils.h"
33 :
34 : struct stringprep
35 : {
36 : const char *comment;
37 : const char *in;
38 : const char *out;
39 : const char *profile;
40 : int flags;
41 : int rc;
42 : };
43 :
44 : const struct stringprep strprep[] = {
45 : {"Map to nothing",
46 : "foo\xC2\xAD\xCD\x8F\xE1\xA0\x86\xE1\xA0\x8B"
47 : "bar" "\xE2\x80\x8B\xE2\x81\xA0" "baz\xEF\xB8\x80\xEF\xB8\x88"
48 : "\xEF\xB8\x8F\xEF\xBB\xBF", "foobarbaz"},
49 : {"Case folding ASCII U+0043 U+0041 U+0046 U+0045", "CAFE", "cafe"},
50 : {"Case folding 8bit U+00DF (german sharp s)", "\xC3\x9F", "ss"},
51 : {"Case folding U+0130 (turkish capital I with dot)",
52 : "\xC4\xB0", "i\xcc\x87"},
53 : {"Case folding multibyte U+0143 U+037A",
54 : "\xC5\x83\xCD\xBA", "\xC5\x84 \xCE\xB9"},
55 : {"Case folding U+2121 U+33C6 U+1D7BB",
56 : "\xE2\x84\xA1\xE3\x8F\x86\xF0\x9D\x9E\xBB",
57 : "telc\xE2\x88\x95" "kg\xCF\x83"},
58 : {"Normalization of U+006a U+030c U+00A0 U+00AA",
59 : "\x6A\xCC\x8C\xC2\xA0\xC2\xAA", "\xC7\xB0 a"},
60 : {"Case folding U+1FB7 and normalization",
61 : "\xE1\xBE\xB7", "\xE1\xBE\xB6\xCE\xB9"},
62 : {"Self-reverting case folding U+01F0 and normalization",
63 : "\xC7\xB0", "\xC7\xB0"},
64 : {"Self-reverting case folding U+0390 and normalization",
65 : "\xCE\x90", "\xCE\x90"},
66 : {"Self-reverting case folding U+03B0 and normalization",
67 : "\xCE\xB0", "\xCE\xB0"},
68 : {"Self-reverting case folding U+1E96 and normalization",
69 : "\xE1\xBA\x96", "\xE1\xBA\x96"},
70 : {"Self-reverting case folding U+1F56 and normalization",
71 : "\xE1\xBD\x96", "\xE1\xBD\x96"},
72 : {"ASCII space character U+0020", "\x20", "\x20"},
73 : {"Non-ASCII 8bit space character U+00A0", "\xC2\xA0", "\x20"},
74 : {"Non-ASCII multibyte space character U+1680",
75 : "\xE1\x9A\x80", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
76 : {"Non-ASCII multibyte space character U+2000", "\xE2\x80\x80", "\x20"},
77 : {"Zero Width Space U+200b", "\xE2\x80\x8b", ""},
78 : {"Non-ASCII multibyte space character U+3000", "\xE3\x80\x80", "\x20"},
79 : {"ASCII control characters U+0010 U+007F", "\x10\x7F", "\x10\x7F"},
80 : {"Non-ASCII 8bit control character U+0085",
81 : "\xC2\x85", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
82 : {"Non-ASCII multibyte control character U+180E",
83 : "\xE1\xA0\x8E", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
84 : {"Zero Width No-Break Space U+FEFF", "\xEF\xBB\xBF", ""},
85 : {"Non-ASCII control character U+1D175",
86 : "\xF0\x9D\x85\xB5", NULL, "Nameprep", 0,
87 : STRINGPREP_CONTAINS_PROHIBITED},
88 : {"Plane 0 private use character U+F123",
89 : "\xEF\x84\xA3", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
90 : {"Plane 15 private use character U+F1234",
91 : "\xF3\xB1\x88\xB4", NULL, "Nameprep", 0,
92 : STRINGPREP_CONTAINS_PROHIBITED},
93 : {"Plane 16 private use character U+10F234",
94 : "\xF4\x8F\x88\xB4", NULL, "Nameprep", 0,
95 : STRINGPREP_CONTAINS_PROHIBITED},
96 : {"Non-character code point U+8FFFE",
97 : "\xF2\x8F\xBF\xBE", NULL, "Nameprep", 0,
98 : STRINGPREP_CONTAINS_PROHIBITED},
99 : {"Non-character code point U+10FFFF",
100 : "\xF4\x8F\xBF\xBF", NULL, "Nameprep", 0,
101 : STRINGPREP_CONTAINS_PROHIBITED},
102 : {"Surrogate code U+DF42",
103 : "\xED\xBD\x82", NULL, "Nameprep", 0, STRINGPREP_ICONV_ERROR
104 : /* was STRINGPREP_CONTAINS_PROHIBITED */ },
105 : {"Non-plain text character U+FFFD",
106 : "\xEF\xBF\xBD", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
107 : {"Ideographic description character U+2FF5",
108 : "\xE2\xBF\xB5", NULL, "Nameprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
109 : {"Display property character U+0341", "\xCD\x81", "\xCC\x81"},
110 : {"Left-to-right mark U+200E",
111 : "\xE2\x80\x8E", "\xCC\x81", "Nameprep", 0,
112 : STRINGPREP_CONTAINS_PROHIBITED},
113 : {"Deprecated U+202A", "\xE2\x80\xAA", "\xCC\x81", "Nameprep", 0,
114 : STRINGPREP_CONTAINS_PROHIBITED},
115 : {"Language tagging character U+E0001",
116 : "\xF3\xA0\x80\x81", "\xCC\x81", "Nameprep", 0,
117 : STRINGPREP_CONTAINS_PROHIBITED},
118 : {"Language tagging character U+E0042",
119 : "\xF3\xA0\x81\x82", NULL, "Nameprep", 0,
120 : STRINGPREP_CONTAINS_PROHIBITED},
121 : {"Bidi: RandALCat character U+05BE and LCat characters",
122 : "foo\xD6\xBE" "bar", NULL, "Nameprep", 0,
123 : STRINGPREP_BIDI_BOTH_L_AND_RAL},
124 : {"Bidi: RandALCat character U+FD50 and LCat characters",
125 : "foo\xEF\xB5\x90" "bar", NULL, "Nameprep", 0,
126 : STRINGPREP_BIDI_BOTH_L_AND_RAL},
127 : {"Bidi: RandALCat character U+FB38 and LCat characters",
128 : "foo\xEF\xB9\xB6" "bar", "foo \xd9\x8e" "bar"},
129 : {"Bidi: RandALCat without trailing RandALCat U+0627 U+0031",
130 : "\xD8\xA7\x31", NULL, "Nameprep", 0, STRINGPREP_BIDI_LEADTRAIL_NOT_RAL},
131 : {"Bidi: RandALCat character U+0627 U+0031 U+0628",
132 : "\xD8\xA7\x31\xD8\xA8", "\xD8\xA7\x31\xD8\xA8"},
133 : {"Unassigned code point U+E0002",
134 : "\xF3\xA0\x80\x82", NULL, "Nameprep", STRINGPREP_NO_UNASSIGNED,
135 : STRINGPREP_CONTAINS_UNASSIGNED},
136 : {"Larger test (shrinking)",
137 : "X\xC2\xAD\xC3\x9F\xC4\xB0\xE2\x84\xA1\x6a\xcc\x8c\xc2\xa0\xc2"
138 : "\xaa\xce\xb0\xe2\x80\x80", "xssi\xcc\x87" "tel\xc7\xb0 a\xce\xb0 ",
139 : "Nameprep"},
140 : {"Larger test (expanding)",
141 : "X\xC3\x9F\xe3\x8c\x96\xC4\xB0\xE2\x84\xA1\xE2\x92\x9F\xE3\x8c\x80",
142 : "xss\xe3\x82\xad\xe3\x83\xad\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\x88"
143 : "\xe3\x83\xab" "i\xcc\x87" "tel\x28" "d\x29\xe3\x82\xa2\xe3\x83\x91"
144 : "\xe3\x83\xbc\xe3\x83\x88"},
145 : {"Test of prohibited ASCII character U+0020",
146 : "\x20", NULL, "Nodeprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
147 : {"Test of NFKC U+00A0 and prohibited character U+0020",
148 : "\xC2\xA0", NULL, "Nodeprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
149 : {"Case map + normalization", "\xC2\xB5", "\xCE\xBC", "Nameprep"},
150 : /* The rest are rather non-interesting, but no point in removing
151 : working test cases... */
152 : {"case_nonfkc", "\xC2\xB5", "\xCE\xBC", "Nameprep", STRINGPREP_NO_NFKC,
153 : STRINGPREP_FLAG_ERROR},
154 : {"NFKC test", "\xC2\xAA", "\x61", "Nameprep"},
155 : {"nameprep, exposed a bug in libstringprep 0.0.5",
156 : "\xC2\xAA\x0A", "\x61\x0A"},
157 : {"unassigned code point U+0221", "\xC8\xA1", "\xC8\xA1", "Nameprep"},
158 : {"Unassigned code point U+0221",
159 : "\xC8\xA1", NULL, "Nameprep", STRINGPREP_NO_UNASSIGNED,
160 : STRINGPREP_CONTAINS_UNASSIGNED},
161 : {"Unassigned code point U+0236", "\xC8\xB6", "\xC8\xB6", "Nameprep"},
162 : {"unassigned code point U+0236",
163 : "\xC8\xB6", NULL, "Nameprep", STRINGPREP_NO_UNASSIGNED,
164 : STRINGPREP_CONTAINS_UNASSIGNED},
165 : {"bidi both RandALCat and LCat U+0627 U+00AA U+0628",
166 : "\xD8\xA7\xC2\xAA\xD8\xA8", NULL, "Nameprep", 0,
167 : STRINGPREP_BIDI_BOTH_L_AND_RAL},
168 : /* XMPP */
169 : {"XMPP node profile prohibited output",
170 : "foo@bar", NULL, "Nodeprep", 0, STRINGPREP_CONTAINS_PROHIBITED},
171 : {"XMPP resource profile on same string should work though",
172 : "foo@bar", "foo@bar", "Resourceprep"},
173 : /* iSCSI */
174 : {"iSCSI 1", "Example-Name", "example-name", "iSCSI"},
175 : {"iSCSI 2", "O+o", NULL, "iSCSI", 0, STRINGPREP_CONTAINS_PROHIBITED},
176 : {"iSCSI 3", "\x01", NULL, "iSCSI", 0, STRINGPREP_CONTAINS_PROHIBITED},
177 : {"iSCSI 4", "\xE3\x80\x82", NULL, "iSCSI", 0,
178 : STRINGPREP_CONTAINS_PROHIBITED},
179 : {"iSCSI 5", "\xE2\xBF\xB5", NULL, "iSCSI", 0,
180 : STRINGPREP_CONTAINS_PROHIBITED},
181 : {"SASL profile", "Example\xC2\xA0" "Name", "Example Name", "SASLprep"},
182 : /* SASL trace */
183 : {"SASL ANONYMOUS plain mechanism", "simon@josefsson.org",
184 : "simon@josefsson.org", "plain"},
185 : {"SASLprep 1 old", "x\xC2\xADy", "xy", "SASLprep"},
186 : {"SASLprep 4 old", "\xE2\x85\xA3", "IV", "SASLprep"},
187 : /* SASLprep test vectors. */
188 : {"SASLprep 1 SOFT HYPHEN mapped to nothing", "I\xC2\xADX", "IX",
189 : "SASLprep"},
190 : {"SASLprep 2 no transformation", "user", "user", "SASLprep"},
191 : {"SASLprep 3 case preserved, will not match #2", "USER", "USER",
192 : "SASLprep"},
193 : {"SASLprep 4 output is NFKC, input in ISO 8859-1", "\xC2\xAA", "a",
194 : "SASLprep"},
195 : {"SASLprep 5 output is NFKC, will match #1", "\xE2\x85\xA8", "IX",
196 : "SASLprep"},
197 : {"SASLprep 6 Error - prohibited character", "\x07", NULL, "SASLprep",
198 : 0, STRINGPREP_CONTAINS_PROHIBITED},
199 : {"SASLprep 7 Error - bidirectional check", "\xD8\xA7" "1", NULL, "SASLprep",
200 : 0, STRINGPREP_BIDI_LEADTRAIL_NOT_RAL}
201 : };
202 :
203 : void
204 1 : doit (void)
205 : {
206 : char *p;
207 : int rc;
208 : unsigned i;
209 :
210 1 : if (!stringprep_check_version (STRINGPREP_VERSION))
211 0 : fail ("stringprep_check_version failed (header %s runtime %s)\n",
212 : STRINGPREP_VERSION, stringprep_check_version (NULL));
213 :
214 1 : if (!stringprep_check_version (NULL))
215 0 : fail ("stringprep_check_version(NULL) failed\n");
216 :
217 1 : if (strcmp (stringprep_check_version (NULL), STRINGPREP_VERSION) != 0)
218 0 : fail ("stringprep_check_version failure (header %s runtime %s)\n",
219 : STRINGPREP_VERSION, stringprep_check_version (NULL));
220 :
221 1 : if (stringprep_check_version ("100.100"))
222 0 : fail ("stringprep_check_version(\"100.100\") failed\n");
223 :
224 75 : for (i = 0; i < sizeof (strprep) / sizeof (strprep[0]); i++)
225 : {
226 74 : if (debug)
227 0 : printf ("STRINGPREP entry %u\n", i);
228 :
229 74 : if (debug)
230 : {
231 0 : printf ("flags: %d\n", strprep[i].flags);
232 :
233 0 : printf ("in: ");
234 0 : escapeprint (strprep[i].in, strlen (strprep[i].in));
235 0 : hexprint (strprep[i].in, strlen (strprep[i].in));
236 0 : binprint (strprep[i].in, strlen (strprep[i].in));
237 : }
238 : {
239 : uint32_t *l;
240 74 : char *x = NULL;
241 74 : l = stringprep_utf8_to_ucs4 (strprep[i].in, -1, NULL);
242 74 : if (l)
243 73 : x = stringprep_ucs4_to_utf8 (l, -1, NULL, NULL);
244 74 : free (l);
245 74 : if (i == 29)
246 : /* Ignoring known bad UTF-8 in entry 29 */
247 1 : continue;
248 73 : else if (l == NULL)
249 : {
250 0 : fail ("bad UTF-8 in entry %u\n", i);
251 0 : continue;
252 : }
253 73 : else if (strcmp (strprep[i].in, x) != 0)
254 : {
255 0 : fail ("bad UTF-8 in entry %u\n", i);
256 0 : if (debug)
257 : {
258 0 : puts ("expected:");
259 0 : escapeprint (strprep[i].in, strlen (strprep[i].in));
260 0 : hexprint (strprep[i].in, strlen (strprep[i].in));
261 0 : puts ("computed:");
262 0 : escapeprint (x, strlen (x));
263 0 : hexprint (x, strlen (x));
264 : }
265 0 : continue;
266 : }
267 :
268 73 : free (x);
269 : }
270 :
271 73 : rc = stringprep_profile (strprep[i].in, &p,
272 73 : strprep[i].profile ?
273 : strprep[i].profile :
274 73 : "Nameprep", strprep[i].flags);
275 73 : if (rc != strprep[i].rc)
276 : {
277 0 : fail ("stringprep() entry %u failed: %d\n", i, rc);
278 0 : if (debug)
279 0 : printf ("FATAL\n");
280 0 : if (rc == STRINGPREP_OK)
281 0 : free (p);
282 0 : continue;
283 : }
284 :
285 73 : if (debug && rc == STRINGPREP_OK)
286 : {
287 0 : printf ("out: ");
288 0 : escapeprint (p, strlen (p));
289 0 : hexprint (p, strlen (p));
290 0 : binprint (p, strlen (p));
291 :
292 0 : printf ("expected out: ");
293 0 : escapeprint (strprep[i].out, strlen (strprep[i].out));
294 0 : hexprint (strprep[i].out, strlen (strprep[i].out));
295 0 : binprint (strprep[i].out, strlen (strprep[i].out));
296 : }
297 73 : else if (debug)
298 0 : printf ("returned %d expected %d\n", rc, strprep[i].rc);
299 :
300 73 : if (rc == STRINGPREP_OK)
301 : {
302 41 : if (strlen (strprep[i].out) != strlen (p) ||
303 41 : memcmp (strprep[i].out, p, strlen (p)) != 0)
304 : {
305 0 : fail ("stringprep() entry %ld failed\n", (long) i);
306 0 : if (debug)
307 0 : printf ("ERROR\n");
308 : }
309 41 : else if (debug)
310 0 : printf ("OK\n\n");
311 :
312 41 : free (p);
313 : }
314 32 : else if (debug)
315 0 : printf ("OK\n\n");
316 : }
317 :
318 : #if 0
319 : {
320 : char p[20];
321 : memset (p, 0, 10);
322 : stringprep_unichar_to_utf8 (0x00DF, p);
323 : hexprint (p, strlen (p));
324 : puts ("");
325 : }
326 : #endif
327 1 : }
|