]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/testidna.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / test / intltest / testidna.cpp
CommitLineData
b75a7d8f
A
1/*
2 *******************************************************************************
3 *
73c04bcf 4 * Copyright (C) 2003-2006, International Business Machines
b75a7d8f
A
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: testidna.cpp
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2003feb1
14 * created by: Ram Viswanadha
15 */
16
17#include "unicode/utypes.h"
18
19#if !UCONFIG_NO_IDNA && !UCONFIG_NO_TRANSLITERATION
20
b75a7d8f
A
21#include <time.h>
22#include <limits.h>
374ca955
A
23#include <stdlib.h>
24#include <string.h>
b75a7d8f 25#include "unicode/ustring.h"
374ca955 26#include "unicode/usprep.h"
b75a7d8f 27#include "unicode/uniset.h"
b75a7d8f
A
28#include "testidna.h"
29#include "idnaref.h"
30#include "nptrans.h"
374ca955 31#include "unicode/putil.h"
73c04bcf 32#include "idnaconf.h"
b75a7d8f
A
33
34static UChar unicodeIn[][41] ={
35 {
36 0x0644, 0x064A, 0x0647, 0x0645, 0x0627, 0x0628, 0x062A, 0x0643, 0x0644,
37 0x0645, 0x0648, 0x0634, 0x0639, 0x0631, 0x0628, 0x064A, 0x061F, 0x0000
38 },
39 {
40 0x4ED6, 0x4EEC, 0x4E3A, 0x4EC0, 0x4E48, 0x4E0D, 0x8BF4, 0x4E2D, 0x6587,
41 0x0000
42 },
43 {
44 0x0050, 0x0072, 0x006F, 0x010D, 0x0070, 0x0072, 0x006F, 0x0073, 0x0074,
45 0x011B, 0x006E, 0x0065, 0x006D, 0x006C, 0x0075, 0x0076, 0x00ED, 0x010D,
46 0x0065, 0x0073, 0x006B, 0x0079, 0x0000
47 },
48 {
49 0x05DC, 0x05DE, 0x05D4, 0x05D4, 0x05DD, 0x05E4, 0x05E9, 0x05D5, 0x05D8,
50 0x05DC, 0x05D0, 0x05DE, 0x05D3, 0x05D1, 0x05E8, 0x05D9, 0x05DD, 0x05E2,
51 0x05D1, 0x05E8, 0x05D9, 0x05EA, 0x0000
52 },
53 {
54 0x092F, 0x0939, 0x0932, 0x094B, 0x0917, 0x0939, 0x093F, 0x0928, 0x094D,
55 0x0926, 0x0940, 0x0915, 0x094D, 0x092F, 0x094B, 0x0902, 0x0928, 0x0939,
56 0x0940, 0x0902, 0x092C, 0x094B, 0x0932, 0x0938, 0x0915, 0x0924, 0x0947,
57 0x0939, 0x0948, 0x0902, 0x0000
58 },
59 {
60 0x306A, 0x305C, 0x307F, 0x3093, 0x306A, 0x65E5, 0x672C, 0x8A9E, 0x3092,
61 0x8A71, 0x3057, 0x3066, 0x304F, 0x308C, 0x306A, 0x3044, 0x306E, 0x304B,
62 0x0000
63 },
64/*
65 {
66 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
67 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
68 0xC5BC, 0xB9C8, 0xB098, 0xC88B, 0xC744, 0xAE4C, 0x0000
69 },
70*/
71 {
72 0x043F, 0x043E, 0x0447, 0x0435, 0x043C, 0x0443, 0x0436, 0x0435, 0x043E,
73 0x043D, 0x0438, 0x043D, 0x0435, 0x0433, 0x043E, 0x0432, 0x043E, 0x0440,
74 0x044F, 0x0442, 0x043F, 0x043E, 0x0440, 0x0443, 0x0441, 0x0441, 0x043A,
75 0x0438, 0x0000
76 },
77 {
78 0x0050, 0x006F, 0x0072, 0x0071, 0x0075, 0x00E9, 0x006E, 0x006F, 0x0070,
79 0x0075, 0x0065, 0x0064, 0x0065, 0x006E, 0x0073, 0x0069, 0x006D, 0x0070,
80 0x006C, 0x0065, 0x006D, 0x0065, 0x006E, 0x0074, 0x0065, 0x0068, 0x0061,
81 0x0062, 0x006C, 0x0061, 0x0072, 0x0065, 0x006E, 0x0045, 0x0073, 0x0070,
82 0x0061, 0x00F1, 0x006F, 0x006C, 0x0000
83 },
84 {
85 0x4ED6, 0x5011, 0x7232, 0x4EC0, 0x9EBD, 0x4E0D, 0x8AAA, 0x4E2D, 0x6587,
86 0x0000
87 },
88 {
89 0x0054, 0x1EA1, 0x0069, 0x0073, 0x0061, 0x006F, 0x0068, 0x1ECD, 0x006B,
90 0x0068, 0x00F4, 0x006E, 0x0067, 0x0074, 0x0068, 0x1EC3, 0x0063, 0x0068,
91 0x1EC9, 0x006E, 0x00F3, 0x0069, 0x0074, 0x0069, 0x1EBF, 0x006E, 0x0067,
92 0x0056, 0x0069, 0x1EC7, 0x0074, 0x0000
93 },
94 {
95 0x0033, 0x5E74, 0x0042, 0x7D44, 0x91D1, 0x516B, 0x5148, 0x751F, 0x0000
96 },
97 {
98 0x5B89, 0x5BA4, 0x5948, 0x7F8E, 0x6075, 0x002D, 0x0077, 0x0069, 0x0074,
99 0x0068, 0x002D, 0x0053, 0x0055, 0x0050, 0x0045, 0x0052, 0x002D, 0x004D,
100 0x004F, 0x004E, 0x004B, 0x0045, 0x0059, 0x0053, 0x0000
101 },
102 {
103 0x0048, 0x0065, 0x006C, 0x006C, 0x006F, 0x002D, 0x0041, 0x006E, 0x006F,
104 0x0074, 0x0068, 0x0065, 0x0072, 0x002D, 0x0057, 0x0061, 0x0079, 0x002D,
105 0x305D, 0x308C, 0x305E, 0x308C, 0x306E, 0x5834, 0x6240, 0x0000
106 },
107 {
108 0x3072, 0x3068, 0x3064, 0x5C4B, 0x6839, 0x306E, 0x4E0B, 0x0032, 0x0000
109 },
110 {
111 0x004D, 0x0061, 0x006A, 0x0069, 0x3067, 0x004B, 0x006F, 0x0069, 0x3059,
112 0x308B, 0x0035, 0x79D2, 0x524D, 0x0000
113 },
114 {
115 0x30D1, 0x30D5, 0x30A3, 0x30FC, 0x0064, 0x0065, 0x30EB, 0x30F3, 0x30D0,
116 0x0000
117 },
118 {
119 0x305D, 0x306E, 0x30B9, 0x30D4, 0x30FC, 0x30C9, 0x3067, 0x0000
120 },
121 // test non-BMP code points
122 {
123 0xD800, 0xDF00, 0xD800, 0xDF01, 0xD800, 0xDF02, 0xD800, 0xDF03, 0xD800, 0xDF05,
124 0xD800, 0xDF06, 0xD800, 0xDF07, 0xD800, 0xDF09, 0xD800, 0xDF0A, 0xD800, 0xDF0B,
125 0x0000
126 },
127 {
128 0xD800, 0xDF0D, 0xD800, 0xDF0C, 0xD800, 0xDF1E, 0xD800, 0xDF0F, 0xD800, 0xDF16,
129 0xD800, 0xDF15, 0xD800, 0xDF14, 0xD800, 0xDF12, 0xD800, 0xDF10, 0xD800, 0xDF20,
130 0xD800, 0xDF21,
131 0x0000
132 },
133 // Greek
134 {
135 0x03b5, 0x03bb, 0x03bb, 0x03b7, 0x03bd, 0x03b9, 0x03ba, 0x03ac
136 },
137 // Maltese
138 {
139 0x0062, 0x006f, 0x006e, 0x0121, 0x0075, 0x0073, 0x0061, 0x0127,
140 0x0127, 0x0061
141 },
142 // Russian
143 {
144 0x043f, 0x043e, 0x0447, 0x0435, 0x043c, 0x0443, 0x0436, 0x0435,
145 0x043e, 0x043d, 0x0438, 0x043d, 0x0435, 0x0433, 0x043e, 0x0432,
146 0x043e, 0x0440, 0x044f, 0x0442, 0x043f, 0x043e, 0x0440, 0x0443,
147 0x0441, 0x0441, 0x043a, 0x0438
374ca955 148 },
73c04bcf
A
149 {
150 0xFB00, 0xFB01
151 }
374ca955 152
b75a7d8f
A
153};
154
155static const char *asciiIn[] = {
156 "xn--egbpdaj6bu4bxfgehfvwxn",
157 "xn--ihqwcrb4cv8a8dqg056pqjye",
158 "xn--Proprostnemluvesky-uyb24dma41a",
159 "xn--4dbcagdahymbxekheh6e0a7fei0b",
160 "xn--i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd",
161 "xn--n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa",
162/* "xn--989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879ccm6fea98c",*/
163 "xn--b1abfaaepdrnnbgefbaDotcwatmq2g4l",
164 "xn--PorqunopuedensimplementehablarenEspaol-fmd56a",
165 "xn--ihqwctvzc91f659drss3x8bo0yb",
166 "xn--TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g",
167 "xn--3B-ww4c5e180e575a65lsy2b",
168 "xn---with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n",
169 "xn--Hello-Another-Way--fc4qua05auwb3674vfr0b",
170 "xn--2-u9tlzr9756bt3uc0v",
171 "xn--MajiKoi5-783gue6qz075azm5e",
172 "xn--de-jg4avhby1noc0d",
173 "xn--d9juau41awczczp",
174 "XN--097CCDEKGHQJK",
175 "XN--db8CBHEJLGH4E0AL",
176 "xn--hxargifdar", // Greek
177 "xn--bonusaa-5bb1da", // Maltese
178 "xn--b1abfaaepdrnnbgefbadotcwatmq2g4l", // Russian (Cyrillic)
73c04bcf 179 "fffi"
b75a7d8f
A
180};
181
182static const char *domainNames[] = {
183 "slip129-37-118-146.nc.us.ibm.net",
184 "saratoga.pe.utexas.edu",
185 "dial-120-45.ots.utexas.edu",
186 "woo-085.dorms.waller.net",
187 "hd30-049.hil.compuserve.com",
188 "pem203-31.pe.ttu.edu",
189 "56K-227.MaxTNT3.pdq.net",
190 "dial-36-2.ots.utexas.edu",
191 "slip129-37-23-152.ga.us.ibm.net",
192 "ts45ip119.cadvision.com",
193 "sdn-ts-004txaustP05.dialsprint.net",
194 "bar-tnt1s66.erols.com",
195 "101.st-louis-15.mo.dial-access.att.net",
196 "h92-245.Arco.COM",
197 "dial-13-2.ots.utexas.edu",
198 "net-redynet29.datamarkets.com.ar",
199 "ccs-shiva28.reacciun.net.ve",
200 "7.houston-11.tx.dial-access.att.net",
201 "ingw129-37-120-26.mo.us.ibm.net",
202 "dialup6.austintx.com",
203 "dns2.tpao.gov.tr",
204 "slip129-37-119-194.nc.us.ibm.net",
205 "cs7.dillons.co.uk.203.119.193.in-addr.arpa",
206 "swprd1.innovplace.saskatoon.sk.ca",
207 "bikini.bologna.maraut.it",
208 "node91.subnet159-198-79.baxter.com",
209 "cust19.max5.new-york.ny.ms.uu.net",
210 "balexander.slip.andrew.cmu.edu",
211 "pool029.max2.denver.co.dynip.alter.net",
212 "cust49.max9.new-york.ny.ms.uu.net",
213 "s61.abq-dialin2.hollyberry.com",
374ca955 214 "\\u0917\\u0928\\u0947\\u0936.sanjose.ibm.com", //':'(0x003a) produces U_IDNA_STD3_ASCII_RULES_ERROR
b75a7d8f 215 "www.xn--vea.com",
374ca955 216 // "www.\\u00E0\\u00B3\\u00AF.com",//' ' (0x0020) produces U_IDNA_STD3_ASCII_RULES_ERROR
b75a7d8f
A
217 "www.\\u00C2\\u00A4.com",
218 "www.\\u00C2\\u00A3.com",
374ca955
A
219 // "\\u0025", //'%' (0x0025) produces U_IDNA_STD3_ASCII_RULES_ERROR
220 // "\\u005C\\u005C", //'\' (0x005C) produces U_IDNA_STD3_ASCII_RULES_ERROR
221 //"@",
222 //"\\u002F",
223 //"www.\\u0021.com",
224 //"www.\\u0024.com",
225 //"\\u003f",
226 // These yeild U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
227 //"\\u00CF\\u0082.com",
228 //"\\u00CE\\u00B2\\u00C3\\u009Fss.com",
229 //"\\u00E2\\u0098\\u00BA.com",
230 "\\u00C3\\u00BC.com",
231
232};
233
234typedef struct ErrorCases ErrorCases;
235
236static struct ErrorCases{
237
238 UChar unicode[100];
239 const char *ascii;
240 UErrorCode expected;
241 UBool useSTD3ASCIIRules;
242 UBool testToUnicode;
243 UBool testLabel;
244} errorCases[] = {
245 {
246
247 {
248 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
249 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
374ca955 250 0x070F,/*prohibited*/
b75a7d8f
A
251 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
252 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
253 0x0000
254 },
374ca955
A
255 "www.XN--8mb5595fsoa28orucya378bqre2tcwop06c5qbw82a1rffmae0361dea96b.com",
256 U_IDNA_PROHIBITED_ERROR,
b75a7d8f
A
257 FALSE, TRUE, TRUE
258 },
259
260 {
261 {
262 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
263 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
264 0x0221, 0x0234/*Unassigned code points*/,
265 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
266 0x0000
267 },
268 "www.XN--6lA2Bz548Fj1GuA391Bf1Gb1N59Ab29A7iA.com",
269
374ca955 270 U_IDNA_UNASSIGNED_ERROR,
b75a7d8f
A
271 FALSE, TRUE, TRUE
272 },
273 {
274 {
275 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
276 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
277 0x0644, 0x064A, 0x0647,/*Arabic code points. Cannot mix RTL with LTR*/
278 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
279 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
280 0x0000
281 },
282 "www.xn--ghBGI4851OiyA33VqrD6Az86C4qF83CtRv93D5xBk15AzfG0nAgA0578DeA71C.com",
283 U_IDNA_CHECK_BIDI_ERROR,
284 FALSE, TRUE, TRUE
285 },
286 {
287 {
288 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
289 /* labels cannot begin with an HYPHEN */
290 0x002D, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
291 0x002E,
292 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
293 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
294 0x0000
295
296 },
297 "www.xn----b95Ew8SqA315Ao5FbuMlnNmhA.com",
298 U_IDNA_STD3_ASCII_RULES_ERROR,
299 TRUE, TRUE, FALSE
300 },
301 {
302 {
303 /* correct ACE-prefix followed by unicode */
304 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
305 0x0078, 0x006e, 0x002d,0x002d, /* ACE Prefix */
306 0x002D, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
307 0x002D,
308 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
309 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
310 0x0000
311
312 },
313 /* wrong ACE-prefix followed by valid ACE-encoded ASCII */
314 "www.XY-----b91I0V65S96C2A355Cw1E5yCeQr19CsnP1mFfmAE0361DeA96B.com",
315 U_IDNA_ACE_PREFIX_ERROR,
316 FALSE, FALSE, FALSE
317 },
318 /* cannot verify U_IDNA_VERIFICATION_ERROR */
319
320 {
321 {
322 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
323 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
324 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
325 0xC5BC, 0xB9C8, 0xB098, 0xC88B, 0xC744, 0xAE4C,
326 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
327 0x0000
328 },
329 "www.xn--989AoMsVi5E83Db1D2A355Cv1E0vAk1DwRv93D5xBh15A0Dt30A5JpSD879Ccm6FeA98C.com",
330 U_IDNA_LABEL_TOO_LONG_ERROR,
331 FALSE, TRUE, TRUE
332 },
333
334 {
335 {
336 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
337 0x0030, 0x0644, 0x064A, 0x0647, 0x0031, /* Arabic code points squashed between EN codepoints */
338 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
339 0x0000
340 },
341 "www.xn--01-tvdmo.com",
342 U_IDNA_CHECK_BIDI_ERROR,
343 FALSE, TRUE, TRUE
344 },
345
346 {
347 {
348 0x0077, 0x0077, 0x0077, 0x002e, // www.
349 0x206C, 0x0644, 0x064A, 0x0647, 0x206D, // Arabic code points squashed between BN codepoints
350 0x002e, 0x0063, 0x006f, 0x006d, // com.
351 0x0000
352 },
353 "www.XN--ghbgi278xia.com",
374ca955 354 U_IDNA_PROHIBITED_ERROR,
b75a7d8f
A
355 FALSE, TRUE, TRUE
356 },
357 {
358 {
359 0x0077, 0x0077, 0x0077, 0x002e, // www.
360 0x002D, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, // HYPHEN at the start of label
361 0x002e, 0x0063, 0x006f, 0x006d, // com.
362 0x0000
363 },
364 "www.-abcde.com",
365 U_IDNA_STD3_ASCII_RULES_ERROR,
366 TRUE, TRUE, FALSE
367 },
368 {
369 {
370 0x0077, 0x0077, 0x0077, 0x002e, // www.
371 0x0041, 0x0042, 0x0043, 0x0044, 0x0045,0x002D, // HYPHEN at the end of the label
372 0x002e, 0x0063, 0x006f, 0x006d, // com.
373 0x0000
374 },
375 "www.abcde-.com",
376 U_IDNA_STD3_ASCII_RULES_ERROR,
377 TRUE, TRUE, FALSE
378 },
379 {
380 {
381 0x0077, 0x0077, 0x0077, 0x002e, // www.
382 0x0041, 0x0042, 0x0043, 0x0044, 0x0045,0x0040, // Containing non LDH code point
383 0x002e, 0x0063, 0x006f, 0x006d, // com.
384 0x0000
385 },
386 "www.abcde@.com",
387 U_IDNA_STD3_ASCII_RULES_ERROR,
388 TRUE, TRUE, FALSE
389 },
73c04bcf
A
390 {
391 {
392 0x0077, 0x0077, 0x0077, 0x002e, // www.
393 // zero length label
394 0x002e, 0x0063, 0x006f, 0x006d, // com.
395 0x0000
396 },
397 "www..com",
398 U_IDNA_ZERO_LENGTH_LABEL_ERROR,
399 TRUE, TRUE, FALSE
400 },
b75a7d8f
A
401 {
402 {0},
403 NULL,
404 U_ILLEGAL_ARGUMENT_ERROR,
405 TRUE, TRUE, FALSE
406 }
407};
408
409
410static struct ConformanceTestCases
411 {
412 const char *comment;
413 const char *in;
414 const char *out;
415 const char *profile;
416 int32_t flags;
417 UErrorCode expectedStatus;
418 }
419 conformanceTestCases[] =
420 {
421
422 {
423 "Case folding ASCII U+0043 U+0041 U+0046 U+0045",
424 "\x43\x41\x46\x45", "\x63\x61\x66\x65",
425 "Nameprep", UIDNA_DEFAULT,
426 U_ZERO_ERROR
427
428 },
429 {
430 "Case folding 8bit U+00DF (german sharp s)",
431 "\xC3\x9F", "\x73\x73",
432 "Nameprep", UIDNA_DEFAULT,
433 U_ZERO_ERROR
434 },
435 {
436 "Non-ASCII multibyte space character U+1680",
437 "\xE1\x9A\x80", NULL,
438 "Nameprep", UIDNA_DEFAULT,
374ca955 439 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
440 },
441 {
442 "Non-ASCII 8bit control character U+0085",
443 "\xC2\x85", NULL,
444 "Nameprep", UIDNA_DEFAULT,
374ca955 445 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
446 },
447 {
448 "Non-ASCII multibyte control character U+180E",
449 "\xE1\xA0\x8E", NULL,
450 "Nameprep", UIDNA_DEFAULT,
374ca955 451 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
452 },
453 {
454 "Non-ASCII control character U+1D175",
455 "\xF0\x9D\x85\xB5", NULL,
456 "Nameprep", UIDNA_DEFAULT,
374ca955 457 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
458 },
459 {
460 "Plane 0 private use character U+F123",
461 "\xEF\x84\xA3", NULL,
462 "Nameprep", UIDNA_DEFAULT,
374ca955 463 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
464 },
465 {
466 "Plane 15 private use character U+F1234",
467 "\xF3\xB1\x88\xB4", NULL,
468 "Nameprep", UIDNA_DEFAULT,
374ca955 469 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
470 },
471 {
472 "Plane 16 private use character U+10F234",
473 "\xF4\x8F\x88\xB4", NULL,
474 "Nameprep", UIDNA_DEFAULT,
374ca955 475 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
476 },
477 {
478 "Non-character code point U+8FFFE",
479 "\xF2\x8F\xBF\xBE", NULL,
480 "Nameprep", UIDNA_DEFAULT,
374ca955 481 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
482 },
483 {
484 "Non-character code point U+10FFFF",
485 "\xF4\x8F\xBF\xBF", NULL,
486 "Nameprep", UIDNA_DEFAULT,
374ca955 487 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
488 },
489 /*
490 {
491 "Surrogate code U+DF42",
492 "\xED\xBD\x82", NULL, "Nameprep", UIDNA_DEFAULT,
374ca955 493 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
494 },
495*/
496 {
497 "Non-plain text character U+FFFD",
498 "\xEF\xBF\xBD", NULL,
499 "Nameprep", UIDNA_DEFAULT,
374ca955 500 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
501 },
502 {
503 "Ideographic description character U+2FF5",
504 "\xE2\xBF\xB5", NULL,
505 "Nameprep", UIDNA_DEFAULT,
374ca955 506 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
507 },
508 {
509 "Display property character U+0341",
374ca955 510 "\xCD\x81", "\xCC\x81",
b75a7d8f
A
511 "Nameprep", UIDNA_DEFAULT, U_ZERO_ERROR
512
513 },
514
515 {
516 "Left-to-right mark U+200E",
517 "\xE2\x80\x8E", "\xCC\x81",
518 "Nameprep", UIDNA_DEFAULT,
374ca955 519 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
520 },
521 {
522
523 "Deprecated U+202A",
524 "\xE2\x80\xAA", "\xCC\x81",
525 "Nameprep", UIDNA_DEFAULT,
374ca955 526 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
527 },
528 {
529 "Language tagging character U+E0001",
530 "\xF3\xA0\x80\x81", "\xCC\x81",
531 "Nameprep", UIDNA_DEFAULT,
374ca955 532 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
533 },
534 {
535 "Language tagging character U+E0042",
536 "\xF3\xA0\x81\x82", NULL,
537 "Nameprep", UIDNA_DEFAULT,
374ca955 538 U_IDNA_PROHIBITED_ERROR
b75a7d8f
A
539 },
540 {
541 "Bidi: RandALCat character U+05BE and LCat characters",
542 "\x66\x6F\x6F\xD6\xBE\x62\x61\x72", NULL,
543 "Nameprep", UIDNA_DEFAULT,
544 U_IDNA_CHECK_BIDI_ERROR
545 },
546 {
547 "Bidi: RandALCat character U+FD50 and LCat characters",
548 "\x66\x6F\x6F\xEF\xB5\x90\x62\x61\x72", NULL,
549 "Nameprep",UIDNA_DEFAULT ,
550 U_IDNA_CHECK_BIDI_ERROR
551 },
552 {
553 "Bidi: RandALCat character U+FB38 and LCat characters",
374ca955 554 "\x66\x6F\x6F\xEF\xB9\xB6\x62\x61\x72", "\x66\x6F\x6F\x20\xd9\x8e\x62\x61\x72",
b75a7d8f
A
555 "Nameprep", UIDNA_DEFAULT,
556 U_ZERO_ERROR
557 },
558 { "Bidi: RandALCat without trailing RandALCat U+0627 U+0031",
559 "\xD8\xA7\x31", NULL,
560 "Nameprep", UIDNA_DEFAULT,
561 U_IDNA_CHECK_BIDI_ERROR
562 },
563 {
564 "Bidi: RandALCat character U+0627 U+0031 U+0628",
565 "\xD8\xA7\x31\xD8\xA8", "\xD8\xA7\x31\xD8\xA8",
566 "Nameprep", UIDNA_DEFAULT,
567 U_ZERO_ERROR
568 },
569 {
570 "Unassigned code point U+E0002",
571 "\xF3\xA0\x80\x82", NULL,
572 "Nameprep", UIDNA_DEFAULT,
374ca955 573 U_IDNA_UNASSIGNED_ERROR
b75a7d8f
A
574 },
575
576/* // Invalid UTF-8
577 {
578 "Larger test (shrinking)",
579 "X\xC2\xAD\xC3\xDF\xC4\xB0\xE2\x84\xA1\x6a\xcc\x8c\xc2\xa0\xc2"
580 "\xaa\xce\xb0\xe2\x80\x80", "xssi\xcc\x87""tel\xc7\xb0 a\xce\xb0 ",
581 "Nameprep",
582 UIDNA_DEFAULT, U_ZERO_ERROR
583 },
584 {
585
586 "Larger test (expanding)",
587 "X\xC3\xDF\xe3\x8c\x96\xC4\xB0\xE2\x84\xA1\xE2\x92\x9F\xE3\x8c\x80",
588 "xss\xe3\x82\xad\xe3\x83\xad\xe3\x83\xa1\xe3\x83\xbc\xe3\x83\x88"
589 "\xe3\x83\xab""i\xcc\x87""tel\x28""d\x29\xe3\x82\xa2\xe3\x83\x91"
590 "\xe3\x83\xbc\xe3\x83\x88"
591 "Nameprep",
592 UIDNA_DEFAULT, U_ZERO_ERROR
593 },
594 */
595};
596
597
598
599#define MAX_DEST_SIZE 300
600
374ca955
A
601void TestIDNA::debug(const UChar* src, int32_t srcLength, int32_t options){
602 UParseError parseError;
603 UErrorCode transStatus = U_ZERO_ERROR;
604 UErrorCode prepStatus = U_ZERO_ERROR;
605 NamePrepTransform* trans = NamePrepTransform::createInstance(parseError,transStatus);
606 int32_t prepOptions = (((options & UIDNA_ALLOW_UNASSIGNED) != 0) ? USPREP_ALLOW_UNASSIGNED: 0);
607 UStringPrepProfile* prep = usprep_open(NULL,"uidna",&prepStatus);
608 UChar *transOut=NULL, *prepOut=NULL;
609 int32_t transOutLength=0, prepOutLength=0;
610
611
612 transOutLength = trans->process(src,srcLength,transOut, 0, prepOptions>0, &parseError, transStatus);
613 if( transStatus == U_BUFFER_OVERFLOW_ERROR){
614 transStatus = U_ZERO_ERROR;
615 transOut = (UChar*) malloc(U_SIZEOF_UCHAR * transOutLength);
616 transOutLength = trans->process(src,srcLength,transOut, transOutLength, prepOptions>0, &parseError, transStatus);
617 }
618
619 prepOutLength = usprep_prepare(prep, src, srcLength, prepOut, 0, prepOptions, &parseError, &prepStatus);
b75a7d8f 620
374ca955
A
621 if( prepStatus == U_BUFFER_OVERFLOW_ERROR){
622 prepStatus = U_ZERO_ERROR;
623 prepOut = (UChar*) malloc(U_SIZEOF_UCHAR * prepOutLength);
624 prepOutLength = usprep_prepare(prep, src, srcLength, prepOut, prepOutLength, prepOptions, &parseError, &prepStatus);
625 }
626
627 if(UnicodeString(transOut,transOutLength)!= UnicodeString(prepOut, prepOutLength)){
628 errln("Failed. Expected: " + prettify(UnicodeString(transOut, transOutLength))
629 + " Got: " + prettify(UnicodeString(prepOut,prepOutLength)));
630 }
631 free(transOut);
632 free(prepOut);
633 delete trans;
634
635}
b75a7d8f
A
636
637void TestIDNA::testAPI(const UChar* src, const UChar* expected, const char* testName,
638 UBool useSTD3ASCIIRules,UErrorCode expectedStatus,
374ca955 639 UBool doCompare, UBool testUnassigned, TestFunc func, UBool testSTD3ASCIIRules){
b75a7d8f
A
640
641 UErrorCode status = U_ZERO_ERROR;
642 UChar destStack[MAX_DEST_SIZE];
643 int32_t destLen = 0;
644 UChar* dest = NULL;
645 int32_t expectedLen = (expected != NULL) ? u_strlen(expected) : 0;
646 int32_t options = (useSTD3ASCIIRules == TRUE) ? UIDNA_USE_STD3_RULES : UIDNA_DEFAULT;
647 UParseError parseError;
648 int32_t tSrcLen = 0;
649 UChar* tSrc = NULL;
650
651 if(src != NULL){
652 tSrcLen = u_strlen(src);
374ca955
A
653 tSrc =(UChar*) malloc( U_SIZEOF_UCHAR * tSrcLen );
654 memcpy(tSrc,src,tSrcLen * U_SIZEOF_UCHAR);
b75a7d8f
A
655 }
656
657 // test null-terminated source and return value of number of UChars required
374ca955
A
658 destLen = func(src,-1,NULL,0,options, &parseError , &status);
659 if(status == U_BUFFER_OVERFLOW_ERROR){
660 status = U_ZERO_ERROR; // reset error code
661 if(destLen+1 < MAX_DEST_SIZE){
662 dest = destStack;
663 destLen = func(src,-1,dest,destLen+1,options, &parseError, &status);
664 // TODO : compare output with expected
665 if(U_SUCCESS(status) && expectedStatus != U_IDNA_STD3_ASCII_RULES_ERROR&& (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
666 errln("Did not get the expected result for "+UnicodeString(testName) +" null terminated source. Expected : "
667 + prettify(UnicodeString(expected,expectedLen))
668 + " Got: " + prettify(UnicodeString(dest,destLen))
669 );
670 }
671 }else{
672 errln( "%s null terminated source failed. Requires destCapacity > 300\n",testName);
673 }
674 }
675
676 if(status != expectedStatus){
73c04bcf 677 errln( "Did not get the expected error for "+
374ca955 678 UnicodeString(testName)+
73c04bcf 679 " null terminated source. Expected: " +UnicodeString(u_errorName(expectedStatus))
374ca955
A
680 + " Got: "+ UnicodeString(u_errorName(status))
681 + " Source: " + prettify(UnicodeString(src))
682 );
683 free(tSrc);
684 return;
685 }
686 if(testUnassigned ){
687 status = U_ZERO_ERROR;
688 destLen = func(src,-1,NULL,0,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
b75a7d8f
A
689 if(status == U_BUFFER_OVERFLOW_ERROR){
690 status = U_ZERO_ERROR; // reset error code
691 if(destLen+1 < MAX_DEST_SIZE){
692 dest = destStack;
374ca955 693 destLen = func(src,-1,dest,destLen+1,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
b75a7d8f 694 // TODO : compare output with expected
374ca955
A
695 if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
696 //errln("Did not get the expected result for %s null terminated source with both options set.\n",testName);
697 errln("Did not get the expected result for "+UnicodeString(testName) +
698 " null terminated source "+ prettify(src) +
699 " with both options set. Expected: "+ prettify(UnicodeString(expected,expectedLen))+
700 "Got: " + prettify(UnicodeString(dest,destLen)));
701
702 debug(src,-1,options | UIDNA_ALLOW_UNASSIGNED);
703
b75a7d8f
A
704 }
705 }else{
706 errln( "%s null terminated source failed. Requires destCapacity > 300\n",testName);
707 }
708 }
374ca955
A
709 //testing query string
710 if(status != expectedStatus && expectedStatus != U_IDNA_UNASSIGNED_ERROR){
73c04bcf 711 errln( "Did not get the expected error for "+
374ca955 712 UnicodeString(testName)+
73c04bcf 713 " null terminated source with options set. Expected: " +UnicodeString(u_errorName(expectedStatus))
374ca955
A
714 + " Got: "+ UnicodeString(u_errorName(status))
715 + " Source: " + prettify(UnicodeString(src))
716 );
717 }
718 }
b75a7d8f 719
374ca955
A
720 status = U_ZERO_ERROR;
721
722 // test source with lengthand return value of number of UChars required
723 destLen = func(tSrc, tSrcLen, NULL,0,options, &parseError, &status);
724 if(status == U_BUFFER_OVERFLOW_ERROR){
725 status = U_ZERO_ERROR; // reset error code
726 if(destLen+1 < MAX_DEST_SIZE){
727 dest = destStack;
728 destLen = func(src,u_strlen(src),dest,destLen+1,options, &parseError, &status);
729 // TODO : compare output with expected
730 if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
731 errln("Did not get the expected result for %s with source length.\n",testName);
b75a7d8f 732 }
374ca955
A
733 }else{
734 errln( "%s with source length failed. Requires destCapacity > 300\n",testName);
b75a7d8f 735 }
374ca955 736 }
b75a7d8f 737
374ca955 738 if(status != expectedStatus){
73c04bcf 739 errln( "Did not get the expected error for "+
374ca955 740 UnicodeString(testName)+
73c04bcf 741 " with source length. Expected: " +UnicodeString(u_errorName(expectedStatus))
374ca955
A
742 + " Got: "+ UnicodeString(u_errorName(status))
743 + " Source: " + prettify(UnicodeString(src))
744 );
745 }
746 if(testUnassigned){
b75a7d8f
A
747 status = U_ZERO_ERROR;
748
374ca955
A
749 destLen = func(tSrc,tSrcLen,NULL,0,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
750
b75a7d8f
A
751 if(status == U_BUFFER_OVERFLOW_ERROR){
752 status = U_ZERO_ERROR; // reset error code
753 if(destLen+1 < MAX_DEST_SIZE){
754 dest = destStack;
374ca955 755 destLen = func(src,u_strlen(src),dest,destLen+1,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
b75a7d8f
A
756 // TODO : compare output with expected
757 if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
374ca955 758 errln("Did not get the expected result for %s with source length and both options set.\n",testName);
b75a7d8f
A
759 }
760 }else{
761 errln( "%s with source length failed. Requires destCapacity > 300\n",testName);
762 }
763 }
374ca955
A
764 //testing query string
765 if(status != expectedStatus && expectedStatus != U_IDNA_UNASSIGNED_ERROR){
73c04bcf 766 errln( "Did not get the expected error for "+
374ca955 767 UnicodeString(testName)+
73c04bcf 768 " with source length and options set. Expected: " +UnicodeString(u_errorName(expectedStatus))
374ca955
A
769 + " Got: "+ UnicodeString(u_errorName(status))
770 + " Source: " + prettify(UnicodeString(src))
771 );
b75a7d8f 772 }
374ca955 773 }
b75a7d8f 774
374ca955
A
775 status = U_ZERO_ERROR;
776 if(testSTD3ASCIIRules==TRUE){
777 destLen = func(src,-1,NULL,0,options | UIDNA_USE_STD3_RULES, &parseError, &status);
b75a7d8f
A
778 if(status == U_BUFFER_OVERFLOW_ERROR){
779 status = U_ZERO_ERROR; // reset error code
780 if(destLen+1 < MAX_DEST_SIZE){
781 dest = destStack;
782 destLen = func(src,-1,dest,destLen+1,options | UIDNA_USE_STD3_RULES, &parseError, &status);
783 // TODO : compare output with expected
784 if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
785 //errln("Did not get the expected result for %s null terminated source with both options set.\n",testName);
786 errln("Did not get the expected result for "+UnicodeString(testName) +" null terminated source with both options set. Expected: "+ prettify(UnicodeString(expected,expectedLen)));
374ca955 787
b75a7d8f
A
788 }
789 }else{
790 errln( "%s null terminated source failed. Requires destCapacity > 300\n",testName);
791 }
792 }
793 //testing query string
794 if(status != expectedStatus){
73c04bcf 795 errln( "Did not get the expected error for "+
374ca955 796 UnicodeString(testName)+
73c04bcf 797 " null terminated source with options set. Expected: " +UnicodeString(u_errorName(expectedStatus))
374ca955
A
798 + " Got: "+ UnicodeString(u_errorName(status))
799 + " Source: " + prettify(UnicodeString(src))
800 );
b75a7d8f
A
801 }
802
803 status = U_ZERO_ERROR;
804
374ca955 805 destLen = func(tSrc,tSrcLen,NULL,0,options | UIDNA_USE_STD3_RULES, &parseError, &status);
b75a7d8f
A
806
807 if(status == U_BUFFER_OVERFLOW_ERROR){
808 status = U_ZERO_ERROR; // reset error code
809 if(destLen+1 < MAX_DEST_SIZE){
810 dest = destStack;
811 destLen = func(src,u_strlen(src),dest,destLen+1,options | UIDNA_USE_STD3_RULES, &parseError, &status);
812 // TODO : compare output with expected
813 if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
814 errln("Did not get the expected result for %s with source length and both options set.\n",testName);
815 }
816 }else{
817 errln( "%s with source length failed. Requires destCapacity > 300\n",testName);
818 }
819 }
820 //testing query string
374ca955 821 if(status != expectedStatus && expectedStatus != U_IDNA_UNASSIGNED_ERROR){
73c04bcf 822 errln( "Did not get the expected error for "+
374ca955 823 UnicodeString(testName)+
73c04bcf 824 " with source length and options set. Expected: " +UnicodeString(u_errorName(expectedStatus))
374ca955
A
825 + " Got: "+ UnicodeString(u_errorName(status))
826 + " Source: " + prettify(UnicodeString(src))
827 );
b75a7d8f
A
828 }
829 }
374ca955 830 free(tSrc);
b75a7d8f
A
831}
832
833void TestIDNA::testCompare(const UChar* s1, int32_t s1Len,
834 const UChar* s2, int32_t s2Len,
835 const char* testName, CompareFunc func,
836 UBool isEqual){
837
838 UErrorCode status = U_ZERO_ERROR;
839 int32_t retVal = func(s1,-1,s2,-1,UIDNA_DEFAULT,&status);
840
841 if(isEqual==TRUE && retVal !=0){
842 errln("Did not get the expected result for %s with null termniated strings.\n",testName);
843 }
844 if(U_FAILURE(status)){
845 errln( "%s null terminated source failed. Error: %s\n", testName,u_errorName(status));
846 }
847
848 status = U_ZERO_ERROR;
849 retVal = func(s1,-1,s2,-1,UIDNA_ALLOW_UNASSIGNED,&status);
850
851 if(isEqual==TRUE && retVal !=0){
852 errln("Did not get the expected result for %s with null termniated strings with options set.\n", testName);
853 }
854 if(U_FAILURE(status)){
855 errln( "%s null terminated source and options set failed. Error: %s\n",testName, u_errorName(status));
856 }
857
858 status = U_ZERO_ERROR;
859 retVal = func(s1,s1Len,s2,s2Len,UIDNA_DEFAULT,&status);
860
861 if(isEqual==TRUE && retVal !=0){
862 errln("Did not get the expected result for %s with string length.\n",testName);
863 }
864 if(U_FAILURE(status)){
865 errln( "%s with string length. Error: %s\n",testName, u_errorName(status));
866 }
867
868 status = U_ZERO_ERROR;
869 retVal = func(s1,s1Len,s2,s2Len,UIDNA_ALLOW_UNASSIGNED,&status);
870
871 if(isEqual==TRUE && retVal !=0){
872 errln("Did not get the expected result for %s with string length and options set.\n",testName);
873 }
874 if(U_FAILURE(status)){
875 errln( "%s with string length and options set. Error: %s\n", u_errorName(status), testName);
876 }
877}
878
879void TestIDNA::testToASCII(const char* testName, TestFunc func){
880
881 int32_t i;
882 UChar buf[MAX_DEST_SIZE];
883
884 for(i=0;i< (int32_t)(sizeof(unicodeIn)/sizeof(unicodeIn[0])); i++){
374ca955 885 u_charsToUChars(asciiIn[i],buf, (int32_t)(strlen(asciiIn[i])+1));
b75a7d8f
A
886 testAPI(unicodeIn[i], buf,testName, FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
887
888 }
889}
890
891void TestIDNA::testToUnicode(const char* testName, TestFunc func){
892
893 int32_t i;
894 UChar buf[MAX_DEST_SIZE];
895
896 for(i=0;i< (int32_t)(sizeof(asciiIn)/sizeof(asciiIn[0])); i++){
374ca955 897 u_charsToUChars(asciiIn[i],buf, (int32_t)(strlen(asciiIn[i])+1));
b75a7d8f
A
898 testAPI(buf,unicodeIn[i],testName,FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
899 }
900}
901
902
903void TestIDNA::testIDNToUnicode(const char* testName, TestFunc func){
904 int32_t i;
905 UChar buf[MAX_DEST_SIZE];
906 UChar expected[MAX_DEST_SIZE];
907 UErrorCode status = U_ZERO_ERROR;
908 int32_t bufLen = 0;
909 UParseError parseError;
910 for(i=0;i< (int32_t)(sizeof(domainNames)/sizeof(domainNames[0])); i++){
374ca955 911 bufLen = (int32_t)strlen(domainNames[i]);
b75a7d8f
A
912 bufLen = u_unescape(domainNames[i],buf, bufLen+1);
913 func(buf,bufLen,expected,MAX_DEST_SIZE, UIDNA_ALLOW_UNASSIGNED, &parseError,&status);
914 if(U_FAILURE(status)){
915 errln( "%s failed to convert domainNames[%i].Error: %s \n",testName, i, u_errorName(status));
916 break;
917 }
918 testAPI(buf,expected,testName,FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
919 //test toUnicode with all labels in the string
920 testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
921 if(U_FAILURE(status)){
922 errln( "%s failed to convert domainNames[%i].Error: %s \n",testName,i, u_errorName(status));
923 break;
924 }
925 }
926
927}
928
929void TestIDNA::testIDNToASCII(const char* testName, TestFunc func){
930 int32_t i;
931 UChar buf[MAX_DEST_SIZE];
932 UChar expected[MAX_DEST_SIZE];
933 UErrorCode status = U_ZERO_ERROR;
934 int32_t bufLen = 0;
935 UParseError parseError;
936 for(i=0;i< (int32_t)(sizeof(domainNames)/sizeof(domainNames[0])); i++){
374ca955 937 bufLen = (int32_t)strlen(domainNames[i]);
b75a7d8f
A
938 bufLen = u_unescape(domainNames[i],buf, bufLen+1);
939 func(buf,bufLen,expected,MAX_DEST_SIZE, UIDNA_ALLOW_UNASSIGNED, &parseError,&status);
940 if(U_FAILURE(status)){
941 errln( "%s failed to convert domainNames[%i].Error: %s \n",testName,i, u_errorName(status));
942 break;
943 }
944 testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
945 //test toASCII with all labels in the string
946 testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, FALSE, TRUE, func);
947 if(U_FAILURE(status)){
948 errln( "%s failed to convert domainNames[%i].Error: %s \n",testName,i, u_errorName(status));
949 break;
950 }
951 }
952
953}
954
955void TestIDNA::testCompare(const char* testName, CompareFunc func){
956 int32_t i;
957
958
959 UChar www[] = {0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
960 UChar com[] = {0x002E, 0x0043, 0x004F, 0x004D, 0x0000};
961 UChar buf[MAX_DEST_SIZE]={0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
962
963 UnicodeString source(www), uni0(www),uni1(www), ascii0(www), ascii1(www);
964
965 uni0.append(unicodeIn[0]);
966 uni0.append(com);
967 uni0.append((UChar)0x0000);
968
969 uni1.append(unicodeIn[1]);
970 uni1.append(com);
971 uni1.append((UChar)0x0000);
972
973 ascii0.append(asciiIn[0]);
974 ascii0.append(com);
975 ascii0.append((UChar)0x0000);
976
977 ascii1.append(asciiIn[1]);
978 ascii1.append(com);
979 ascii1.append((UChar)0x0000);
980
981 for(i=0;i< (int32_t)(sizeof(unicodeIn)/sizeof(unicodeIn[0])); i++){
982
374ca955 983 u_charsToUChars(asciiIn[i],buf+4, (int32_t)(strlen(asciiIn[i])+1));
b75a7d8f
A
984 u_strcat(buf,com);
985
986 // for every entry in unicodeIn array
987 // prepend www. and append .com
988 source.truncate(4);
989 source.append(unicodeIn[i]);
990 source.append(com);
991 source.append((UChar)0x0000);
992 // a) compare it with itself
993 const UChar* src = source.getBuffer();
994 int32_t srcLen = u_strlen(src); //subtract null
995
996 testCompare(src,srcLen,src,srcLen,testName, func, TRUE);
997
998 // b) compare it with asciiIn equivalent
999 testCompare(src,srcLen,buf,u_strlen(buf),testName, func,TRUE);
1000
1001 // c) compare it with unicodeIn not equivalent
1002 if(i==0){
1003 testCompare(src,srcLen,uni1.getBuffer(),uni1.length()-1,testName, func,FALSE);
b75a7d8f
A
1004 }else{
1005 testCompare(src,srcLen,uni0.getBuffer(),uni0.length()-1,testName, func,FALSE);
b75a7d8f
A
1006 }
1007 // d) compare it with asciiIn not equivalent
1008 if(i==0){
1009 testCompare(src,srcLen,ascii1.getBuffer(),ascii1.length()-1,testName, func,FALSE);
b75a7d8f
A
1010 }else{
1011 testCompare(src,srcLen,ascii0.getBuffer(),ascii0.length()-1,testName, func,FALSE);
b75a7d8f
A
1012 }
1013
1014 }
1015}
1016
1017#if 0
1018
1019static int32_t
1020getNextSeperator(UChar *src,int32_t srcLength,
1021 UChar **limit){
1022 if(srcLength == -1){
1023 int32_t i;
1024 for(i=0 ; ;i++){
1025 if(src[i] == 0){
1026 *limit = src + i; // point to null
1027 return i;
1028 }
1029 if(src[i]==0x002e){
1030 *limit = src + (i+1); // go past the delimiter
1031 return i;
1032 }
1033 }
1034 // we have not found the delimiter
1035 if(i==srcLength){
1036 *limit = src+srcLength;
1037 }
1038 return i;
1039 }else{
1040 int32_t i;
1041 for(i=0;i<srcLength;i++){
1042 if(src[i]==0x002e){
1043 *limit = src + (i+1); // go past the delimiter
1044 return i;
1045 }
1046 }
1047 // we have not found the delimiter
1048 if(i==srcLength){
1049 *limit = src+srcLength;
1050 }
1051 return i;
1052 }
1053}
1054
1055void printPunycodeOutput(){
1056
1057 UChar dest[MAX_DEST_SIZE];
1058 int32_t destCapacity=MAX_DEST_SIZE;
1059 UChar* start;
1060 UChar* limit;
1061 int32_t labelLen=0;
1062 UBool caseFlags[MAX_DEST_SIZE];
1063
1064 for(int32_t i=0;i< sizeof(errorCases)/sizeof(errorCases[0]);i++){
1065 ErrorCases errorCase = errorCases[i];
1066 UErrorCode status = U_ZERO_ERROR;
1067 start = errorCase.unicode;
1068 int32_t srcLen = u_strlen(start);
1069 labelLen = getNextSeperator(start,srcLen,&limit);
1070 start = limit;
1071 labelLen=getNextSeperator(start,srcLen-labelLen,&limit);
1072 int32_t destLen = u_strToPunycode(dest,destCapacity,start,labelLen,caseFlags, &status);
1073 if(U_FAILURE(status)){
1074 printf("u_strToPunycode failed for index %i\n",i);
1075 continue;
1076 }
1077 for(int32_t j=0; j<destLen; j++){
1078 printf("%c",(char)dest[j]);
1079 }
1080 printf("\n");
1081 }
1082}
1083#endif
1084
374ca955
A
1085void TestIDNA::testErrorCases(const char* IDNToASCIIName, TestFunc IDNToASCII,
1086 const char* IDNToUnicodeName, TestFunc IDNToUnicode){
b75a7d8f
A
1087 UChar buf[MAX_DEST_SIZE];
1088 int32_t bufLen=0;
1089
1090 for(int32_t i=0;i< (int32_t)(sizeof(errorCases)/sizeof(errorCases[0]));i++){
1091 ErrorCases errorCase = errorCases[i];
1092 UChar* src =NULL;
1093 if(errorCase.ascii != NULL){
374ca955 1094 bufLen = (int32_t)strlen(errorCase.ascii);
b75a7d8f
A
1095 u_charsToUChars(errorCase.ascii,buf, bufLen+1);
1096 }else{
1097 bufLen = 1 ;
1098 memset(buf,0,U_SIZEOF_UCHAR*MAX_DEST_SIZE);
1099 }
1100
1101 if(errorCase.unicode[0]!=0){
1102 src = errorCase.unicode;
1103 }
1104 // test toASCII
1105 testAPI(src,buf,
1106 IDNToASCIIName, errorCase.useSTD3ASCIIRules,
1107 errorCase.expected, TRUE, TRUE, IDNToASCII);
1108 if(errorCase.testLabel ==TRUE){
1109 testAPI(src,buf,
374ca955
A
1110 IDNToASCIIName, errorCase.useSTD3ASCIIRules,
1111 errorCase.expected, FALSE,TRUE, IDNToASCII);
b75a7d8f
A
1112 }
1113 if(errorCase.testToUnicode ==TRUE){
1114 testAPI((src==NULL)? NULL : buf,src,
1115 IDNToUnicodeName, errorCase.useSTD3ASCIIRules,
1116 errorCase.expected, TRUE, TRUE, IDNToUnicode);
1117 }
1118
1119 }
1120
1121}
1122
1123void TestIDNA::testConformance(const char* toASCIIName, TestFunc toASCII,
1124 const char* IDNToASCIIName, TestFunc IDNToASCII,
1125 const char* IDNToUnicodeName, TestFunc IDNToUnicode,
1126 const char* toUnicodeName, TestFunc toUnicode){
1127 UChar src[MAX_DEST_SIZE];
1128 int32_t srcLen=0;
1129 UChar expected[MAX_DEST_SIZE];
1130 int32_t expectedLen = 0;
1131 for(int32_t i=0;i< (int32_t)(sizeof(conformanceTestCases)/sizeof(conformanceTestCases[0]));i++){
1132 const char* utf8Chars1 = conformanceTestCases[i].in;
374ca955 1133 int32_t utf8Chars1Len = (int32_t)strlen(utf8Chars1);
b75a7d8f 1134 const char* utf8Chars2 = conformanceTestCases[i].out;
374ca955 1135 int32_t utf8Chars2Len = (utf8Chars2 == NULL) ? 0 : (int32_t)strlen(utf8Chars2);
b75a7d8f
A
1136
1137 UErrorCode status = U_ZERO_ERROR;
1138 u_strFromUTF8(src,MAX_DEST_SIZE,&srcLen,utf8Chars1,utf8Chars1Len,&status);
1139 if(U_FAILURE(status)){
1140 errln(UnicodeString("Conversion of UTF8 source in conformanceTestCases[") + i +UnicodeString( "].in ( ")+prettify(utf8Chars1) +UnicodeString(" ) failed. Error: ")+ UnicodeString(u_errorName(status)));
1141 continue;
1142 }
1143 if(utf8Chars2 != NULL){
1144 u_strFromUTF8(expected,MAX_DEST_SIZE,&expectedLen,utf8Chars2,utf8Chars2Len, &status);
1145 if(U_FAILURE(status)){
1146 errln(UnicodeString("Conversion of UTF8 source in conformanceTestCases[") + i +UnicodeString( "].in ( ")+prettify(utf8Chars1) +UnicodeString(" ) failed. Error: ")+ UnicodeString(u_errorName(status)));
1147 continue;
1148 }
1149 }
1150
1151 if(conformanceTestCases[i].expectedStatus != U_ZERO_ERROR){
1152 // test toASCII
1153 testAPI(src,expected,
1154 IDNToASCIIName, FALSE,
1155 conformanceTestCases[i].expectedStatus,
1156 TRUE,
374ca955 1157 (conformanceTestCases[i].expectedStatus != U_IDNA_UNASSIGNED_ERROR),
b75a7d8f
A
1158 IDNToASCII);
1159
1160 testAPI(src,expected,
1161 toASCIIName, FALSE,
1162 conformanceTestCases[i].expectedStatus, TRUE,
374ca955 1163 (conformanceTestCases[i].expectedStatus != U_IDNA_UNASSIGNED_ERROR),
b75a7d8f
A
1164 toASCII);
1165 }
1166
1167 testAPI(src,src,
1168 IDNToUnicodeName, FALSE,
1169 conformanceTestCases[i].expectedStatus, TRUE, TRUE, IDNToUnicode);
1170 testAPI(src,src,
1171 toUnicodeName, FALSE,
1172 conformanceTestCases[i].expectedStatus, TRUE, TRUE, toUnicode);
1173
1174 }
1175
1176}
1177
1178// test and ascertain
1179// func(func(func(src))) == func(src)
1180void TestIDNA::testChaining(UChar* src,int32_t numIterations,const char* testName,
1181 UBool useSTD3ASCIIRules, UBool caseInsensitive, TestFunc func){
1182 UChar even[MAX_DEST_SIZE];
1183 UChar odd[MAX_DEST_SIZE];
1184 UChar expected[MAX_DEST_SIZE];
1185 int32_t i=0,evenLen=0,oddLen=0,expectedLen=0;
1186 UErrorCode status = U_ZERO_ERROR;
1187 int32_t srcLen = u_strlen(src);
1188 int32_t options = (useSTD3ASCIIRules == TRUE) ? UIDNA_USE_STD3_RULES : UIDNA_DEFAULT;
1189 UParseError parseError;
1190
1191 // test null-terminated source
1192 expectedLen = func(src,-1,expected,MAX_DEST_SIZE, options, &parseError, &status);
1193 if(U_FAILURE(status)){
1194 errln("%s null terminated source failed. Error: %s\n",testName, u_errorName(status));
1195 }
374ca955
A
1196 memcpy(odd,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
1197 memcpy(even,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
b75a7d8f
A
1198 for(;i<=numIterations; i++){
1199 if((i%2) ==0){
1200 evenLen = func(odd,-1,even,MAX_DEST_SIZE,options, &parseError, &status);
1201 if(U_FAILURE(status)){
1202 errln("%s null terminated source failed\n",testName);
1203 break;
1204 }
1205 }else{
1206 oddLen = func(even,-1,odd,MAX_DEST_SIZE,options, &parseError, &status);
1207 if(U_FAILURE(status)){
1208 errln("%s null terminated source failed\n",testName);
1209 break;
1210 }
1211 }
1212 }
1213 if(caseInsensitive ==TRUE){
1214 if( u_strCaseCompare(even,evenLen, expected,expectedLen, 0, &status) !=0 ||
1215 u_strCaseCompare(odd,oddLen, expected,expectedLen, 0, &status) !=0 ){
1216
1217 errln("Chaining for %s null terminated source failed\n",testName);
1218 }
1219 }else{
1220 if( u_strncmp(even,expected,expectedLen) != 0 ||
1221 u_strncmp(odd,expected,expectedLen) !=0 ){
1222
1223 errln("Chaining for %s null terminated source failed\n",testName);
1224 }
1225 }
1226
1227 // test null-terminated source
1228 status = U_ZERO_ERROR;
1229 expectedLen = func(src,-1,expected,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
1230 if(U_FAILURE(status)){
1231 errln("%s null terminated source with options set failed. Error: %s\n",testName, u_errorName(status));
1232 }
374ca955
A
1233 memcpy(odd,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
1234 memcpy(even,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
b75a7d8f
A
1235 for(;i<=numIterations; i++){
1236 if((i%2) ==0){
1237 evenLen = func(odd,-1,even,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
1238 if(U_FAILURE(status)){
1239 errln("%s null terminated source with options set failed\n",testName);
1240 break;
1241 }
1242 }else{
1243 oddLen = func(even,-1,odd,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
1244 if(U_FAILURE(status)){
1245 errln("%s null terminated source with options set failed\n",testName);
1246 break;
1247 }
1248 }
1249 }
1250 if(caseInsensitive ==TRUE){
1251 if( u_strCaseCompare(even,evenLen, expected,expectedLen, 0, &status) !=0 ||
1252 u_strCaseCompare(odd,oddLen, expected,expectedLen, 0, &status) !=0 ){
1253
1254 errln("Chaining for %s null terminated source with options set failed\n",testName);
1255 }
1256 }else{
1257 if( u_strncmp(even,expected,expectedLen) != 0 ||
1258 u_strncmp(odd,expected,expectedLen) !=0 ){
1259
1260 errln("Chaining for %s null terminated source with options set failed\n",testName);
1261 }
1262 }
1263
1264
1265 // test source with length
1266 status = U_ZERO_ERROR;
1267 expectedLen = func(src,srcLen,expected,MAX_DEST_SIZE,options, &parseError, &status);
1268 if(U_FAILURE(status)){
1269 errln("%s null terminated source failed. Error: %s\n",testName, u_errorName(status));
1270 }
374ca955
A
1271 memcpy(odd,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
1272 memcpy(even,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
b75a7d8f
A
1273 for(;i<=numIterations; i++){
1274 if((i%2) ==0){
1275 evenLen = func(odd,oddLen,even,MAX_DEST_SIZE,options, &parseError, &status);
1276 if(U_FAILURE(status)){
1277 errln("%s source with source length failed\n",testName);
1278 break;
1279 }
1280 }else{
1281 oddLen = func(even,evenLen,odd,MAX_DEST_SIZE,options, &parseError, &status);
1282 if(U_FAILURE(status)){
1283 errln("%s source with source length failed\n",testName);
1284 break;
1285 }
1286 }
1287 }
1288 if(caseInsensitive ==TRUE){
1289 if( u_strCaseCompare(even,evenLen, expected,expectedLen, 0, &status) !=0 ||
1290 u_strCaseCompare(odd,oddLen, expected,expectedLen, 0, &status) !=0 ){
1291
1292 errln("Chaining for %s source with source length failed\n",testName);
1293 }
1294 }else{
1295 if( u_strncmp(even,expected,expectedLen) != 0 ||
1296 u_strncmp(odd,expected,expectedLen) !=0 ){
1297
1298 errln("Chaining for %s source with source length failed\n",testName);
1299 }
1300 }
1301 status = U_ZERO_ERROR;
1302 expectedLen = func(src,srcLen,expected,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
1303 if(U_FAILURE(status)){
1304 errln("%s null terminated source with options set failed. Error: %s\n",testName, u_errorName(status));
1305 }
374ca955
A
1306 memcpy(odd,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
1307 memcpy(even,expected,(expectedLen+1) * U_SIZEOF_UCHAR);
b75a7d8f
A
1308 for(;i<=numIterations; i++){
1309 if((i%2) ==0){
1310 evenLen = func(odd,oddLen,even,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
1311 if(U_FAILURE(status)){
1312 errln("%s source with source length and options set failed\n",testName);
1313 break;
1314 }
1315 }else{
1316 oddLen = func(even,evenLen,odd,MAX_DEST_SIZE,options|UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
1317 if(U_FAILURE(status)){
1318 errln("%s source with source length and options set failed\n",testName);
1319 break;
1320 }
1321 }
1322 }
1323 if(caseInsensitive ==TRUE){
1324 if( u_strCaseCompare(even,evenLen, expected,expectedLen, 0, &status) !=0 ||
1325 u_strCaseCompare(odd,oddLen, expected,expectedLen, 0, &status) !=0 ){
1326
1327 errln("Chaining for %s source with source length and options set failed\n",testName);
1328 }
1329 }else{
1330 if( u_strncmp(even,expected,expectedLen) != 0 ||
1331 u_strncmp(odd,expected,expectedLen) !=0 ){
1332
1333 errln("Chaining for %s source with source length and options set failed\n",testName);
1334 }
1335 }
1336}
1337void TestIDNA::testChaining(const char* toASCIIName, TestFunc toASCII,
1338 const char* toUnicodeName, TestFunc toUnicode){
1339 int32_t i;
1340 UChar buf[MAX_DEST_SIZE];
1341
1342 for(i=0;i< (int32_t)(sizeof(asciiIn)/sizeof(asciiIn[0])); i++){
374ca955 1343 u_charsToUChars(asciiIn[i],buf, (int32_t)(strlen(asciiIn[i])+1));
b75a7d8f
A
1344 testChaining(buf,5,toUnicodeName, FALSE, FALSE, toUnicode);
1345 }
1346 for(i=0;i< (int32_t)(sizeof(unicodeIn)/sizeof(unicodeIn[0])); i++){
1347 testChaining(unicodeIn[i], 5,toASCIIName, FALSE, TRUE, toASCII);
1348 }
1349}
1350
1351
1352void TestIDNA::testRootLabelSeparator(const char* testName, CompareFunc func,
1353 const char* IDNToASCIIName, TestFunc IDNToASCII,
1354 const char* IDNToUnicodeName, TestFunc IDNToUnicode){
1355 int32_t i;
1356
1357
1358 UChar www[] = {0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
1359 UChar com[] = {0x002E, 0x0043, 0x004F, 0x004D, 0x002E, /* root label separator */0x0000};
1360 UChar buf[MAX_DEST_SIZE]={0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
1361
1362 UnicodeString source(www), uni0(www),uni1(www), ascii0(www), ascii1(www);
1363
1364 uni0.append(unicodeIn[0]);
1365 uni0.append(com);
1366 uni0.append((UChar)0x0000);
1367
1368 uni1.append(unicodeIn[1]);
1369 uni1.append(com);
1370 uni1.append((UChar)0x0000);
1371
1372 ascii0.append(asciiIn[0]);
1373 ascii0.append(com);
1374 ascii0.append((UChar)0x0000);
1375
1376 ascii1.append(asciiIn[1]);
1377 ascii1.append(com);
1378 ascii1.append((UChar)0x0000);
1379
1380 for(i=0;i< (int32_t)(sizeof(unicodeIn)/sizeof(unicodeIn[0])); i++){
1381
374ca955 1382 u_charsToUChars(asciiIn[i],buf+4, (int32_t)(strlen(asciiIn[i])+1));
b75a7d8f
A
1383 u_strcat(buf,com);
1384
1385 // for every entry in unicodeIn array
1386 // prepend www. and append .com
1387 source.truncate(4);
1388 source.append(unicodeIn[i]);
1389 source.append(com);
1390 source.append((UChar)0x0000);
374ca955 1391
b75a7d8f
A
1392 const UChar* src = source.getBuffer();
1393 int32_t srcLen = u_strlen(src); //subtract null
b75a7d8f
A
1394
1395 // b) compare it with asciiIn equivalent
1396 testCompare(src,srcLen,buf,u_strlen(buf),testName, func,TRUE);
1397
374ca955
A
1398 // a) compare it with itself
1399 testCompare(src,srcLen,src,srcLen,testName, func,TRUE);
1400
1401
b75a7d8f
A
1402 // IDNToASCII comparison
1403 testAPI(src,buf,IDNToASCIIName,FALSE,U_ZERO_ERROR,TRUE, TRUE, IDNToASCII);
1404 // IDNToUnicode comparison
1405 testAPI(buf,src,IDNToUnicodeName, FALSE,U_ZERO_ERROR, TRUE, TRUE, IDNToUnicode);
1406
1407 // c) compare it with unicodeIn not equivalent
1408 if(i==0){
1409 testCompare(src,srcLen,uni1.getBuffer(),uni1.length()-1,testName, func,FALSE);
b75a7d8f
A
1410 }else{
1411 testCompare(src,srcLen,uni0.getBuffer(),uni0.length()-1,testName, func,FALSE);
b75a7d8f
A
1412 }
1413 // d) compare it with asciiIn not equivalent
1414 if(i==0){
1415 testCompare(src,srcLen,ascii1.getBuffer(),ascii1.length()-1,testName, func,FALSE);
b75a7d8f
A
1416 }else{
1417 testCompare(src,srcLen,ascii0.getBuffer(),ascii0.length()-1,testName, func,FALSE);
b75a7d8f
A
1418 }
1419 }
1420}
1421
1422//---------------------------------------------
1423// runIndexedTest
1424//---------------------------------------------
1425
73c04bcf 1426void TestIDNA::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par)
b75a7d8f
A
1427{
1428 if (exec) logln((UnicodeString)"TestSuite IDNA API ");
1429 switch (index) {
1430
1431 case 0: name = "TestToASCII"; if (exec) TestToASCII(); break;
1432 case 1: name = "TestToUnicode"; if (exec) TestToUnicode(); break;
1433 case 2: name = "TestIDNToASCII"; if (exec) TestIDNToASCII(); break;
1434 case 3: name = "TestIDNToUnicode"; if (exec) TestIDNToUnicode(); break;
1435 case 4: name = "TestCompare"; if (exec) TestCompare(); break;
1436 case 5: name = "TestErrorCases"; if (exec) TestErrorCases(); break;
1437 case 6: name = "TestChaining"; if (exec) TestChaining(); break;
1438 case 7: name = "TestRootLabelSeparator"; if(exec) TestRootLabelSeparator(); break;
1439 case 8: name = "TestCompareReferenceImpl"; if(exec) TestCompareReferenceImpl(); break;
1440 case 9: name = "TestDataFile"; if(exec) TestDataFile(); break;
1441 case 10: name = "TestRefIDNA"; if(exec) TestRefIDNA(); break;
1442 case 11: name = "TestIDNAMonkeyTest"; if(exec) TestIDNAMonkeyTest(); break;
1443 case 12: name = "TestConformance"; if(exec) TestConformance();break;
73c04bcf
A
1444 case 13:
1445 {
1446 name = "TestConformanceTestVectors";
1447 if(exec){
1448 logln("TestSuite IDNA conf----"); logln();
1449 IdnaConfTest test;
1450 callTest(test, par);
1451 }
1452 break;
1453 }
b75a7d8f
A
1454 default: name = ""; break; /*needed to end loop*/
1455 }
1456}
1457void TestIDNA::TestToASCII(){
1458 testToASCII("uidna_toASCII", uidna_toASCII);
1459}
1460void TestIDNA::TestToUnicode(){
1461 testToUnicode("uidna_toUnicode", uidna_toUnicode);
1462}
1463void TestIDNA::TestIDNToASCII(){
1464 testIDNToASCII("uidna_IDNToASCII", uidna_IDNToASCII);
1465}
1466void TestIDNA::TestIDNToUnicode(){
1467 testIDNToUnicode("uidna_IDNToUnicode", uidna_IDNToUnicode);
1468}
1469void TestIDNA::TestCompare(){
1470 testCompare("uidna_compare",uidna_compare);
1471}
1472void TestIDNA::TestErrorCases(){
374ca955 1473 testErrorCases( "uidna_IDNToASCII",uidna_IDNToASCII,
b75a7d8f
A
1474 "uidna_IDNToUnicode",uidna_IDNToUnicode);
1475}
1476void TestIDNA::TestRootLabelSeparator(){
1477 testRootLabelSeparator( "uidna_compare",uidna_compare,
1478 "uidna_IDNToASCII", uidna_IDNToASCII,
1479 "uidna_IDNToUnicode",uidna_IDNToUnicode
1480 );
1481}
1482void TestIDNA::TestChaining(){
1483 testChaining("uidna_toASCII",uidna_toASCII, "uidna_toUnicode", uidna_toUnicode);
1484}
1485void TestIDNA::TestConformance(){
1486 testConformance("uidna_toASCII",uidna_toASCII,"uidna_IDNToASCII",uidna_IDNToASCII,
1487 "uidna_IDNToUnicode",uidna_IDNToUnicode, "uidna_toUnicode", uidna_toUnicode);
1488}
1489
1490static const int loopCount = 100;
1491static const int maxCharCount = 20;
1492static const int maxCodePoint = 0x10ffff;
1493static uint32_t
1494randul()
1495{
1496 static UBool initialized = FALSE;
1497 if (!initialized)
1498 {
1499 srand((unsigned)time(NULL));
1500 initialized = TRUE;
1501 }
1502 // Assume rand has at least 12 bits of precision
1503 uint32_t l = 0;
1504 for (uint32_t i=0; i<sizeof(l); ++i)
1505 ((char*)&l)[i] = (char)((rand() & 0x0FF0) >> 4);
1506 return l;
1507}
1508
1509/**
1510 * Return a random integer i where 0 <= i < n.
1511 * A special function that gets random codepoints from planes 0,1,2 and 14
1512 */
1513static int32_t rand_uni()
1514{
1515 int32_t retVal = (int32_t)(randul()& 0x3FFFF);
1516 if(retVal >= 0x30000){
1517 retVal+=0xB0000;
1518 }
1519 return retVal;
1520}
1521
1522static int32_t randi(int32_t n){
1523 return (int32_t) (randul() % (n+1));
1524}
1525
1526void getTestSource(UnicodeString& fillIn) {
1527 int32_t i = 0;
1528 int32_t charCount = (randi(maxCharCount) + 1);
1529 while (i <charCount ) {
1530 int32_t codepoint = rand_uni();
1531 if(codepoint == 0x0000){
1532 continue;
1533 }
1534 fillIn.append((UChar32)codepoint);
1535 i++;
1536 }
1537
1538}
374ca955
A
1539
1540UnicodeString TestIDNA::testCompareReferenceImpl(UnicodeString& src,
1541 TestFunc refIDNA, const char* refIDNAName,
1542 TestFunc uIDNA, const char* uIDNAName,
1543 int32_t options){
1544
1545 const UChar* srcUChars = src.getBuffer();
1546 UChar exp[MAX_DEST_SIZE]={0};
1547 int32_t expCap = MAX_DEST_SIZE, expLen=0;
1548 UErrorCode expStatus = U_ZERO_ERROR;
1549 UParseError parseError;
1550
1551 logln("Comparing "+ UnicodeString(refIDNAName)
1552 + " with "+ UnicodeString(uIDNAName)
1553 +" for input: " + prettify(srcUChars));
b75a7d8f 1554
374ca955
A
1555 expLen = refIDNA(srcUChars, src.length()-1, exp, expCap,
1556 options, &parseError, &expStatus);
1557
1558 UChar got[MAX_DEST_SIZE]={0};
1559 int32_t gotCap = MAX_DEST_SIZE, gotLen=0;
1560 UErrorCode gotStatus = U_ZERO_ERROR;
1561
1562 gotLen = uIDNA(srcUChars, src.length()-1, got, gotCap,
1563 options, &parseError, &gotStatus);
1564
1565 if(expStatus != gotStatus){
1566 errln("Did not get the expected status while comparing " + UnicodeString(refIDNAName)
1567 + " with " + UnicodeString(uIDNAName)
1568 + " Expected: " + UnicodeString(u_errorName(expStatus))
1569 + " Got: " + UnicodeString(u_errorName(gotStatus))
1570 + " for Source: "+ prettify(srcUChars)
1571 + " Options: " + options);
374ca955
A
1572 return UnicodeString("");
1573 }
b75a7d8f 1574
374ca955
A
1575 // now we know that both implementations yielded same error
1576 if(U_SUCCESS(expStatus)){
1577 // compare the outputs if status == U_ZERO_ERROR
1578 if(u_strCompare(exp, expLen, got, gotLen, TRUE) != 0){
1579 errln("Did not get the expected output while comparing " + UnicodeString(refIDNAName)
1580 + " with " + UnicodeString(uIDNAName)
1581 + " Expected: " + prettify(UnicodeString(exp, expLen))
1582 + " Got: " + prettify(UnicodeString(got, gotLen))
1583 + " for Source: "+ prettify(srcUChars)
1584 + " Options: " + options);
b75a7d8f 1585 }
374ca955 1586 return UnicodeString(exp, expLen);
b75a7d8f 1587
374ca955
A
1588 }else{
1589 logln("Got the same error while comparing "
1590 + UnicodeString(refIDNAName)
1591 + " with "+ UnicodeString(uIDNAName)
1592 +" for input: " + prettify(srcUChars));
1593 }
374ca955
A
1594 return UnicodeString("");
1595}
1596
1597void TestIDNA::testCompareReferenceImpl(const UChar* src, int32_t srcLen){
1598 UnicodeString label(src,srcLen);
1599 label.append((UChar)0x0000);
1600
1601 //test idnaref_toASCII and idnare
1602 UnicodeString asciiLabel = testCompareReferenceImpl(label,
1603 idnaref_toASCII, "idnaref_toASCII",
1604 uidna_toASCII, "uidna_toASCII",
1605 UIDNA_ALLOW_UNASSIGNED);
1606 testCompareReferenceImpl(label,
1607 idnaref_toASCII, "idnaref_toASCII",
1608 uidna_toASCII, "uidna_toASCII",
1609 UIDNA_DEFAULT);
1610 testCompareReferenceImpl(label,
1611 idnaref_toASCII, "idnaref_toASCII",
1612 uidna_toASCII, "uidna_toASCII",
1613 UIDNA_USE_STD3_RULES);
1614 testCompareReferenceImpl(label,
1615 idnaref_toASCII, "idnaref_toASCII",
1616 uidna_toASCII, "uidna_toASCII",
1617 UIDNA_USE_STD3_RULES | UIDNA_ALLOW_UNASSIGNED);
1618
1619 if(asciiLabel.length()!=0){
1620 asciiLabel.append((UChar)0x0000);
1621
1622 // test toUnciode
1623 testCompareReferenceImpl(asciiLabel,
1624 idnaref_toUnicode, "idnaref_toUnicode",
1625 uidna_toUnicode, "uidna_toUnicode",
1626 UIDNA_ALLOW_UNASSIGNED);
1627 testCompareReferenceImpl(asciiLabel,
1628 idnaref_toUnicode, "idnaref_toUnicode",
1629 uidna_toUnicode, "uidna_toUnicode",
1630 UIDNA_DEFAULT);
1631 testCompareReferenceImpl(asciiLabel,
1632 idnaref_toUnicode, "idnaref_toUnicode",
1633 uidna_toUnicode, "uidna_toUnicode",
1634 UIDNA_USE_STD3_RULES);
1635 testCompareReferenceImpl(asciiLabel,
1636 idnaref_toUnicode, "idnaref_toUnicode",
1637 uidna_toUnicode, "uidna_toUnicode",
1638 UIDNA_USE_STD3_RULES | UIDNA_ALLOW_UNASSIGNED);
b75a7d8f
A
1639 }
1640
1641}
374ca955
A
1642const char* failures[] ={
1643 "\\uAA42\\U0001F8DD\\U00019D01\\U000149A3\\uD385\\U000EE0F5\\U00018B92\\U000179D1\\U00018624\\U0002227F\\U000E83C0\\U000E8DCD\\u5460\\U00017F34\\U0001570B\\u43D1\\U0002C9C9\\U000281EC\\u2105\\U000180AE\\uC5D4",
1644 "\\U0002F5A6\\uD638\\u0D0A\\u9E9C\\uFE5B\\U0001FCCB\\u66C4",
1645};
1646
b75a7d8f
A
1647void TestIDNA::TestIDNAMonkeyTest(){
1648 UnicodeString source;
1649 UErrorCode status = U_ZERO_ERROR;
374ca955 1650 int i;
b75a7d8f
A
1651
1652 getInstance(status); // Init prep
73c04bcf
A
1653 if (U_FAILURE(status)) {
1654 errln("Test could not initialize. Got %s", u_errorName(status));
1655 return;
1656 }
1657
374ca955 1658 for(i=0; i<loopCount; i++){
b75a7d8f
A
1659 source.truncate(0);
1660 getTestSource(source);
1661 source.append((UChar)0x0000);
374ca955
A
1662 const UChar* src = source.getBuffer();
1663 testCompareReferenceImpl(src,source.length()-1);
1664 testCompareReferenceImpl(src,source.length()-1);
374ca955
A
1665 }
1666
1667 /* for debugging */
1668 for (i=0; i<(int)(sizeof(failures)/sizeof(failures[0])); i++){
1669 source.truncate(0);
1670 source.append( failures[i] );
1671 source = source.unescape();
1672 source.append((UChar)0x0000);
1673 const UChar *src = source.getBuffer();
1674 testCompareReferenceImpl(src,source.length()-1);
1675 //debug(source.getBuffer(),source.length(),UIDNA_ALLOW_UNASSIGNED);
b75a7d8f 1676 }
374ca955
A
1677
1678
1679 source.truncate(0);
1680 source.append("\\uCF18\\U00021161\\U000EEF11\\U0002BB82\\U0001D63C");
1681 debug(source.getBuffer(),source.length(),UIDNA_ALLOW_UNASSIGNED);
374ca955
A
1682
1683 { // test deletion of code points
1684 UnicodeString source("\\u043f\\u00AD\\u034f\\u043e\\u0447\\u0435\\u043c\\u0443\\u0436\\u0435\\u043e\\u043d\\u0438\\u043d\\u0435\\u0433\\u043e\\u0432\\u043e\\u0440\\u044f\\u0442\\u043f\\u043e\\u0440\\u0443\\u0441\\u0441\\u043a\\u0438\\u0000");
1685 source = source.unescape();
1686 UnicodeString expected("\\u043f\\u043e\\u0447\\u0435\\u043c\\u0443\\u0436\\u0435\\u043e\\u043d\\u0438\\u043d\\u0435\\u0433\\u043e\\u0432\\u043e\\u0440\\u044f\\u0442\\u043f\\u043e\\u0440\\u0443\\u0441\\u0441\\u043a\\u0438\\u0000");
1687 expected = expected.unescape();
1688 UnicodeString ascii("xn--b1abfaaepdrnnbgefbadotcwatmq2g4l");
1689 ascii.append((UChar)0x0000);
1690 testAPI(source.getBuffer(),ascii.getBuffer(), "uidna_toASCII", FALSE, U_ZERO_ERROR, TRUE, TRUE, uidna_toASCII);
374ca955
A
1691
1692 testAPI(source.getBuffer(),ascii.getBuffer(), "idnaref_toASCII", FALSE, U_ZERO_ERROR, TRUE, TRUE, idnaref_toASCII);
374ca955
A
1693
1694 testCompareReferenceImpl(source.getBuffer(), source.length()-1);
374ca955 1695 }
b75a7d8f 1696
b75a7d8f
A
1697}
1698
1699void TestIDNA::TestCompareReferenceImpl(){
1700
1701 UChar src [2] = {0,0};
1702 int32_t srcLen = 0;
1703
1704
1705 for(int32_t i = 0x40000 ; i< 0x10ffff; i++){
1706 if(quick==TRUE && i> 0x1FFFF){
1707 return;
1708 }
374ca955 1709 if(i >= 0x30000 && i <= 0xF0000){
b75a7d8f
A
1710 i+=0xB0000;
1711 }
1712 if(i>0xFFFF){
1713 src[0] = U16_LEAD(i);
1714 src[1] = U16_TRAIL(i);
1715 srcLen =2;
1716 }else{
1717 src[0] = (UChar)i;
1718 src[1] = 0;
1719 srcLen = 1;
1720 }
1721 testCompareReferenceImpl(src, srcLen);
1722 }
1723}
1724
1725void TestIDNA::TestRefIDNA(){
73c04bcf
A
1726 UErrorCode status = U_ZERO_ERROR;
1727 getInstance(status); // Init prep
1728 if (U_FAILURE(status)) {
1729 errln("Test could not initialize. Got %s", u_errorName(status));
1730 return;
1731 }
1732
b75a7d8f
A
1733 testToASCII("idnaref_toASCII", idnaref_toASCII);
1734 testToUnicode("idnaref_toUnicode", idnaref_toUnicode);
1735 testIDNToASCII("idnaref_IDNToASCII", idnaref_IDNToASCII);
1736 testIDNToUnicode("idnaref_IDNToUnicode", idnaref_IDNToUnicode);
1737 testCompare("idnaref_compare",idnaref_compare);
374ca955 1738 testErrorCases( "idnaref_IDNToASCII",idnaref_IDNToASCII,
b75a7d8f
A
1739 "idnaref_IDNToUnicode",idnaref_IDNToUnicode);
1740 testChaining("idnaref_toASCII",idnaref_toASCII, "idnaref_toUnicode", idnaref_toUnicode);
1741
1742 testRootLabelSeparator( "idnaref_compare",idnaref_compare,
1743 "idnaref_IDNToASCII", idnaref_IDNToASCII,
1744 "idnaref_IDNToUnicode",idnaref_IDNToUnicode
1745 );
1746 testChaining("idnaref_toASCII",idnaref_toASCII, "idnaref_toUnicode", idnaref_toUnicode);
b75a7d8f
A
1747}
1748
73c04bcf 1749
b75a7d8f
A
1750void TestIDNA::TestDataFile(){
1751 testData(*this);
1752}
374ca955
A
1753TestIDNA::~TestIDNA(){
1754 if(gPrep!=NULL){
1755 delete gPrep;
1756 gPrep = NULL;
1757 }
1758}
b75a7d8f 1759
374ca955
A
1760NamePrepTransform* TestIDNA::gPrep = NULL;
1761
1762NamePrepTransform* TestIDNA::getInstance(UErrorCode& status){
1763 if(TestIDNA::gPrep == NULL){
1764 UParseError parseError;
1765 TestIDNA::gPrep = NamePrepTransform::createInstance(parseError, status);
1766 if(TestIDNA::gPrep ==NULL){
1767 //status = U_MEMORY_ALLOCATION_ERROR;
1768 return NULL;
1769 }
1770 }
1771 return TestIDNA::gPrep;
1772
1773}
b75a7d8f 1774#endif /* #if !UCONFIG_NO_IDNA */