2 *******************************************************************************
4 * Copyright (C) 2003-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: testidna.cpp
10 * tab size: 8 (not used)
13 * created on: 2003feb1
14 * created by: Ram Viswanadha
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_IDNA && !UCONFIG_NO_TRANSLITERATION
25 #include "unicode/localpointer.h"
26 #include "unicode/ustring.h"
27 #include "unicode/usprep.h"
28 #include "unicode/uniset.h"
32 #include "unicode/putil.h"
35 static const UChar unicodeIn
[][41] ={
37 0x0644, 0x064A, 0x0647, 0x0645, 0x0627, 0x0628, 0x062A, 0x0643, 0x0644,
38 0x0645, 0x0648, 0x0634, 0x0639, 0x0631, 0x0628, 0x064A, 0x061F, 0x0000
41 0x4ED6, 0x4EEC, 0x4E3A, 0x4EC0, 0x4E48, 0x4E0D, 0x8BF4, 0x4E2D, 0x6587,
45 0x0050, 0x0072, 0x006F, 0x010D, 0x0070, 0x0072, 0x006F, 0x0073, 0x0074,
46 0x011B, 0x006E, 0x0065, 0x006D, 0x006C, 0x0075, 0x0076, 0x00ED, 0x010D,
47 0x0065, 0x0073, 0x006B, 0x0079, 0x0000
50 0x05DC, 0x05DE, 0x05D4, 0x05D4, 0x05DD, 0x05E4, 0x05E9, 0x05D5, 0x05D8,
51 0x05DC, 0x05D0, 0x05DE, 0x05D3, 0x05D1, 0x05E8, 0x05D9, 0x05DD, 0x05E2,
52 0x05D1, 0x05E8, 0x05D9, 0x05EA, 0x0000
55 0x092F, 0x0939, 0x0932, 0x094B, 0x0917, 0x0939, 0x093F, 0x0928, 0x094D,
56 0x0926, 0x0940, 0x0915, 0x094D, 0x092F, 0x094B, 0x0902, 0x0928, 0x0939,
57 0x0940, 0x0902, 0x092C, 0x094B, 0x0932, 0x0938, 0x0915, 0x0924, 0x0947,
58 0x0939, 0x0948, 0x0902, 0x0000
61 0x306A, 0x305C, 0x307F, 0x3093, 0x306A, 0x65E5, 0x672C, 0x8A9E, 0x3092,
62 0x8A71, 0x3057, 0x3066, 0x304F, 0x308C, 0x306A, 0x3044, 0x306E, 0x304B,
67 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
68 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
69 0xC5BC, 0xB9C8, 0xB098, 0xC88B, 0xC744, 0xAE4C, 0x0000
73 0x043F, 0x043E, 0x0447, 0x0435, 0x043C, 0x0443, 0x0436, 0x0435, 0x043E,
74 0x043D, 0x0438, 0x043D, 0x0435, 0x0433, 0x043E, 0x0432, 0x043E, 0x0440,
75 0x044F, 0x0442, 0x043F, 0x043E, 0x0440, 0x0443, 0x0441, 0x0441, 0x043A,
79 0x0050, 0x006F, 0x0072, 0x0071, 0x0075, 0x00E9, 0x006E, 0x006F, 0x0070,
80 0x0075, 0x0065, 0x0064, 0x0065, 0x006E, 0x0073, 0x0069, 0x006D, 0x0070,
81 0x006C, 0x0065, 0x006D, 0x0065, 0x006E, 0x0074, 0x0065, 0x0068, 0x0061,
82 0x0062, 0x006C, 0x0061, 0x0072, 0x0065, 0x006E, 0x0045, 0x0073, 0x0070,
83 0x0061, 0x00F1, 0x006F, 0x006C, 0x0000
86 0x4ED6, 0x5011, 0x7232, 0x4EC0, 0x9EBD, 0x4E0D, 0x8AAA, 0x4E2D, 0x6587,
90 0x0054, 0x1EA1, 0x0069, 0x0073, 0x0061, 0x006F, 0x0068, 0x1ECD, 0x006B,
91 0x0068, 0x00F4, 0x006E, 0x0067, 0x0074, 0x0068, 0x1EC3, 0x0063, 0x0068,
92 0x1EC9, 0x006E, 0x00F3, 0x0069, 0x0074, 0x0069, 0x1EBF, 0x006E, 0x0067,
93 0x0056, 0x0069, 0x1EC7, 0x0074, 0x0000
96 0x0033, 0x5E74, 0x0042, 0x7D44, 0x91D1, 0x516B, 0x5148, 0x751F, 0x0000
99 0x5B89, 0x5BA4, 0x5948, 0x7F8E, 0x6075, 0x002D, 0x0077, 0x0069, 0x0074,
100 0x0068, 0x002D, 0x0053, 0x0055, 0x0050, 0x0045, 0x0052, 0x002D, 0x004D,
101 0x004F, 0x004E, 0x004B, 0x0045, 0x0059, 0x0053, 0x0000
104 0x0048, 0x0065, 0x006C, 0x006C, 0x006F, 0x002D, 0x0041, 0x006E, 0x006F,
105 0x0074, 0x0068, 0x0065, 0x0072, 0x002D, 0x0057, 0x0061, 0x0079, 0x002D,
106 0x305D, 0x308C, 0x305E, 0x308C, 0x306E, 0x5834, 0x6240, 0x0000
109 0x3072, 0x3068, 0x3064, 0x5C4B, 0x6839, 0x306E, 0x4E0B, 0x0032, 0x0000
112 0x004D, 0x0061, 0x006A, 0x0069, 0x3067, 0x004B, 0x006F, 0x0069, 0x3059,
113 0x308B, 0x0035, 0x79D2, 0x524D, 0x0000
116 0x30D1, 0x30D5, 0x30A3, 0x30FC, 0x0064, 0x0065, 0x30EB, 0x30F3, 0x30D0,
120 0x305D, 0x306E, 0x30B9, 0x30D4, 0x30FC, 0x30C9, 0x3067, 0x0000
122 // test non-BMP code points
124 0xD800, 0xDF00, 0xD800, 0xDF01, 0xD800, 0xDF02, 0xD800, 0xDF03, 0xD800, 0xDF05,
125 0xD800, 0xDF06, 0xD800, 0xDF07, 0xD800, 0xDF09, 0xD800, 0xDF0A, 0xD800, 0xDF0B,
129 0xD800, 0xDF0D, 0xD800, 0xDF0C, 0xD800, 0xDF1E, 0xD800, 0xDF0F, 0xD800, 0xDF16,
130 0xD800, 0xDF15, 0xD800, 0xDF14, 0xD800, 0xDF12, 0xD800, 0xDF10, 0xD800, 0xDF20,
136 0x03b5, 0x03bb, 0x03bb, 0x03b7, 0x03bd, 0x03b9, 0x03ba, 0x03ac
140 0x0062, 0x006f, 0x006e, 0x0121, 0x0075, 0x0073, 0x0061, 0x0127,
145 0x043f, 0x043e, 0x0447, 0x0435, 0x043c, 0x0443, 0x0436, 0x0435,
146 0x043e, 0x043d, 0x0438, 0x043d, 0x0435, 0x0433, 0x043e, 0x0432,
147 0x043e, 0x0440, 0x044f, 0x0442, 0x043f, 0x043e, 0x0440, 0x0443,
148 0x0441, 0x0441, 0x043a, 0x0438
156 static const char *asciiIn
[] = {
157 "xn--egbpdaj6bu4bxfgehfvwxn",
158 "xn--ihqwcrb4cv8a8dqg056pqjye",
159 "xn--Proprostnemluvesky-uyb24dma41a",
160 "xn--4dbcagdahymbxekheh6e0a7fei0b",
161 "xn--i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd",
162 "xn--n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa",
163 /* "xn--989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879ccm6fea98c",*/
164 "xn--b1abfaaepdrnnbgefbaDotcwatmq2g4l",
165 "xn--PorqunopuedensimplementehablarenEspaol-fmd56a",
166 "xn--ihqwctvzc91f659drss3x8bo0yb",
167 "xn--TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g",
168 "xn--3B-ww4c5e180e575a65lsy2b",
169 "xn---with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n",
170 "xn--Hello-Another-Way--fc4qua05auwb3674vfr0b",
171 "xn--2-u9tlzr9756bt3uc0v",
172 "xn--MajiKoi5-783gue6qz075azm5e",
173 "xn--de-jg4avhby1noc0d",
174 "xn--d9juau41awczczp",
176 "XN--db8CBHEJLGH4E0AL",
177 "xn--hxargifdar", // Greek
178 "xn--bonusaa-5bb1da", // Maltese
179 "xn--b1abfaaepdrnnbgefbadotcwatmq2g4l", // Russian (Cyrillic)
183 static const char *domainNames
[] = {
184 "slip129-37-118-146.nc.us.ibm.net",
185 "saratoga.pe.utexas.edu",
186 "dial-120-45.ots.utexas.edu",
187 "woo-085.dorms.waller.net",
188 "hd30-049.hil.compuserve.com",
189 "pem203-31.pe.ttu.edu",
190 "56K-227.MaxTNT3.pdq.net",
191 "dial-36-2.ots.utexas.edu",
192 "slip129-37-23-152.ga.us.ibm.net",
193 "ts45ip119.cadvision.com",
194 "sdn-ts-004txaustP05.dialsprint.net",
195 "bar-tnt1s66.erols.com",
196 "101.st-louis-15.mo.dial-access.att.net",
198 "dial-13-2.ots.utexas.edu",
199 "net-redynet29.datamarkets.com.ar",
200 "ccs-shiva28.reacciun.net.ve",
201 "7.houston-11.tx.dial-access.att.net",
202 "ingw129-37-120-26.mo.us.ibm.net",
203 "dialup6.austintx.com",
205 "slip129-37-119-194.nc.us.ibm.net",
206 "cs7.dillons.co.uk.203.119.193.in-addr.arpa",
207 "swprd1.innovplace.saskatoon.sk.ca",
208 "bikini.bologna.maraut.it",
209 "node91.subnet159-198-79.baxter.com",
210 "cust19.max5.new-york.ny.ms.uu.net",
211 "balexander.slip.andrew.cmu.edu",
212 "pool029.max2.denver.co.dynip.alter.net",
213 "cust49.max9.new-york.ny.ms.uu.net",
214 "s61.abq-dialin2.hollyberry.com",
215 "\\u0917\\u0928\\u0947\\u0936.sanjose.ibm.com", //':'(0x003a) produces U_IDNA_STD3_ASCII_RULES_ERROR
217 // "www.\\u00E0\\u00B3\\u00AF.com",//' ' (0x0020) produces U_IDNA_STD3_ASCII_RULES_ERROR
218 "www.\\u00C2\\u00A4.com",
219 "www.\\u00C2\\u00A3.com",
220 // "\\u0025", //'%' (0x0025) produces U_IDNA_STD3_ASCII_RULES_ERROR
221 // "\\u005C\\u005C", //'\' (0x005C) produces U_IDNA_STD3_ASCII_RULES_ERROR
227 // These yeild U_IDNA_PROHIBITED_ERROR
228 //"\\u00CF\\u0082.com",
229 //"\\u00CE\\u00B2\\u00C3\\u009Fss.com",
230 //"\\u00E2\\u0098\\u00BA.com",
231 "\\u00C3\\u00BC.com",
235 typedef struct ErrorCases ErrorCases
;
237 static const struct ErrorCases
{
242 UBool useSTD3ASCIIRules
;
249 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
250 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
251 0x070F,/*prohibited*/
252 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
253 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
256 "www.XN--8mb5595fsoa28orucya378bqre2tcwop06c5qbw82a1rffmae0361dea96b.com",
257 U_IDNA_PROHIBITED_ERROR
,
263 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
264 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
265 0x0221, 0x0234/*Unassigned code points*/,
266 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
269 "www.XN--6lA2Bz548Fj1GuA391Bf1Gb1N59Ab29A7iA.com",
271 U_IDNA_UNASSIGNED_ERROR
,
276 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
277 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
278 0x0644, 0x064A, 0x0647,/*Arabic code points. Cannot mix RTL with LTR*/
279 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
280 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
283 "www.xn--ghBGI4851OiyA33VqrD6Az86C4qF83CtRv93D5xBk15AzfG0nAgA0578DeA71C.com",
284 U_IDNA_CHECK_BIDI_ERROR
,
289 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
290 /* labels cannot begin with an HYPHEN */
291 0x002D, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
293 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
294 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
298 "www.xn----b95Ew8SqA315Ao5FbuMlnNmhA.com",
299 U_IDNA_STD3_ASCII_RULES_ERROR
,
304 /* correct ACE-prefix followed by unicode */
305 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
306 0x0078, 0x006e, 0x002d,0x002d, /* ACE Prefix */
307 0x002D, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
309 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
310 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
314 /* wrong ACE-prefix followed by valid ACE-encoded ASCII */
315 "www.XY-----b91I0V65S96C2A355Cw1E5yCeQr19CsnP1mFfmAE0361DeA96B.com",
316 U_IDNA_ACE_PREFIX_ERROR
,
319 /* cannot verify U_IDNA_VERIFICATION_ERROR */
323 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
324 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
325 0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
326 0xC5BC, 0xB9C8, 0xB098, 0xC88B, 0xC744, 0xAE4C,
327 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
330 "www.xn--989AoMsVi5E83Db1D2A355Cv1E0vAk1DwRv93D5xBh15A0Dt30A5JpSD879Ccm6FeA98C.com",
331 U_IDNA_LABEL_TOO_LONG_ERROR
,
337 0x0077, 0x0077, 0x0077, 0x002e, /* www. */
338 0x0030, 0x0644, 0x064A, 0x0647, 0x0031, /* Arabic code points squashed between EN codepoints */
339 0x002e, 0x0063, 0x006f, 0x006d, /* com. */
342 "www.xn--01-tvdmo.com",
343 U_IDNA_CHECK_BIDI_ERROR
,
349 0x0077, 0x0077, 0x0077, 0x002e, // www.
350 0x206C, 0x0644, 0x064A, 0x0647, 0x206D, // Arabic code points squashed between BN codepoints
351 0x002e, 0x0063, 0x006f, 0x006d, // com.
354 "www.XN--ghbgi278xia.com",
355 U_IDNA_PROHIBITED_ERROR
,
360 0x0077, 0x0077, 0x0077, 0x002e, // www.
361 0x002D, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, // HYPHEN at the start of label
362 0x002e, 0x0063, 0x006f, 0x006d, // com.
366 U_IDNA_STD3_ASCII_RULES_ERROR
,
371 0x0077, 0x0077, 0x0077, 0x002e, // www.
372 0x0041, 0x0042, 0x0043, 0x0044, 0x0045,0x002D, // HYPHEN at the end of the label
373 0x002e, 0x0063, 0x006f, 0x006d, // com.
377 U_IDNA_STD3_ASCII_RULES_ERROR
,
382 0x0077, 0x0077, 0x0077, 0x002e, // www.
383 0x0041, 0x0042, 0x0043, 0x0044, 0x0045,0x0040, // Containing non LDH code point
384 0x002e, 0x0063, 0x006f, 0x006d, // com.
388 U_IDNA_STD3_ASCII_RULES_ERROR
,
393 0x0077, 0x0077, 0x0077, 0x002e, // www.
395 0x002e, 0x0063, 0x006f, 0x006d, // com.
399 U_IDNA_ZERO_LENGTH_LABEL_ERROR
,
405 U_ILLEGAL_ARGUMENT_ERROR
,
413 #define MAX_DEST_SIZE 300
415 void TestIDNA::debug(const UChar
* src
, int32_t srcLength
, int32_t options
){
416 UParseError parseError
;
417 UErrorCode transStatus
= U_ZERO_ERROR
;
418 UErrorCode prepStatus
= U_ZERO_ERROR
;
419 NamePrepTransform
* trans
= NamePrepTransform::createInstance(parseError
,transStatus
);
420 int32_t prepOptions
= (((options
& UIDNA_ALLOW_UNASSIGNED
) != 0) ? USPREP_ALLOW_UNASSIGNED
: 0);
421 LocalUStringPrepProfilePointer
prep(usprep_openByType(USPREP_RFC3491_NAMEPREP
,&prepStatus
));
422 UChar
*transOut
=NULL
, *prepOut
=NULL
;
423 int32_t transOutLength
=0, prepOutLength
=0;
426 transOutLength
= trans
->process(src
,srcLength
,transOut
, 0, prepOptions
>0, &parseError
, transStatus
);
427 if( transStatus
== U_BUFFER_OVERFLOW_ERROR
){
428 transStatus
= U_ZERO_ERROR
;
429 transOut
= (UChar
*) malloc(U_SIZEOF_UCHAR
* transOutLength
);
430 transOutLength
= trans
->process(src
,srcLength
,transOut
, transOutLength
, prepOptions
>0, &parseError
, transStatus
);
433 prepOutLength
= usprep_prepare(prep
.getAlias(), src
, srcLength
, prepOut
, 0, prepOptions
, &parseError
, &prepStatus
);
435 if( prepStatus
== U_BUFFER_OVERFLOW_ERROR
){
436 prepStatus
= U_ZERO_ERROR
;
437 prepOut
= (UChar
*) malloc(U_SIZEOF_UCHAR
* prepOutLength
);
438 prepOutLength
= usprep_prepare(prep
.getAlias(), src
, srcLength
, prepOut
, prepOutLength
, prepOptions
, &parseError
, &prepStatus
);
441 if(UnicodeString(transOut
,transOutLength
)!= UnicodeString(prepOut
, prepOutLength
)){
442 errln("Failed. Expected: " + prettify(UnicodeString(transOut
, transOutLength
))
443 + " Got: " + prettify(UnicodeString(prepOut
,prepOutLength
)));
450 void TestIDNA::testAPI(const UChar
* src
, const UChar
* expected
, const char* testName
,
451 UBool useSTD3ASCIIRules
,UErrorCode expectedStatus
,
452 UBool doCompare
, UBool testUnassigned
, TestFunc func
, UBool testSTD3ASCIIRules
){
454 UErrorCode status
= U_ZERO_ERROR
;
455 UChar destStack
[MAX_DEST_SIZE
];
458 int32_t expectedLen
= (expected
!= NULL
) ? u_strlen(expected
) : 0;
459 int32_t options
= (useSTD3ASCIIRules
== TRUE
) ? UIDNA_USE_STD3_RULES
: UIDNA_DEFAULT
;
460 UParseError parseError
;
465 tSrcLen
= u_strlen(src
);
466 tSrc
=(UChar
*) malloc( U_SIZEOF_UCHAR
* tSrcLen
);
467 memcpy(tSrc
,src
,tSrcLen
* U_SIZEOF_UCHAR
);
470 // test null-terminated source and return value of number of UChars required
471 destLen
= func(src
,-1,NULL
,0,options
, &parseError
, &status
);
472 if(status
== U_BUFFER_OVERFLOW_ERROR
){
473 status
= U_ZERO_ERROR
; // reset error code
474 if(destLen
+1 < MAX_DEST_SIZE
){
476 destLen
= func(src
,-1,dest
,destLen
+1,options
, &parseError
, &status
);
477 // TODO : compare output with expected
478 if(U_SUCCESS(status
) && expectedStatus
!= U_IDNA_STD3_ASCII_RULES_ERROR
&& (doCompare
==TRUE
) && u_strCaseCompare(dest
,destLen
, expected
,expectedLen
,0,&status
)!=0){
479 errln("Did not get the expected result for "+UnicodeString(testName
) +" null terminated source. Expected : "
480 + prettify(UnicodeString(expected
,expectedLen
))
481 + " Got: " + prettify(UnicodeString(dest
,destLen
))
485 errln( "%s null terminated source failed. Requires destCapacity > 300\n",testName
);
489 if(status
!= expectedStatus
){
490 errcheckln(status
, "Did not get the expected error for "+
491 UnicodeString(testName
)+
492 " null terminated source. Expected: " +UnicodeString(u_errorName(expectedStatus
))
493 + " Got: "+ UnicodeString(u_errorName(status
))
494 + " Source: " + prettify(UnicodeString(src
))
500 status
= U_ZERO_ERROR
;
501 destLen
= func(src
,-1,NULL
,0,options
| UIDNA_ALLOW_UNASSIGNED
, &parseError
, &status
);
502 if(status
== U_BUFFER_OVERFLOW_ERROR
){
503 status
= U_ZERO_ERROR
; // reset error code
504 if(destLen
+1 < MAX_DEST_SIZE
){
506 destLen
= func(src
,-1,dest
,destLen
+1,options
| UIDNA_ALLOW_UNASSIGNED
, &parseError
, &status
);
507 // TODO : compare output with expected
508 if(U_SUCCESS(status
) && (doCompare
==TRUE
) && u_strCaseCompare(dest
,destLen
, expected
,expectedLen
,0,&status
)!=0){
509 //errln("Did not get the expected result for %s null terminated source with both options set.\n",testName);
510 errln("Did not get the expected result for "+UnicodeString(testName
) +
511 " null terminated source "+ prettify(src
) +
512 " with both options set. Expected: "+ prettify(UnicodeString(expected
,expectedLen
))+
513 "Got: " + prettify(UnicodeString(dest
,destLen
)));
515 debug(src
,-1,options
| UIDNA_ALLOW_UNASSIGNED
);
519 errln( "%s null terminated source failed. Requires destCapacity > 300\n",testName
);
522 //testing query string
523 if(status
!= expectedStatus
&& expectedStatus
!= U_IDNA_UNASSIGNED_ERROR
){
524 errln( "Did not get the expected error for "+
525 UnicodeString(testName
)+
526 " null terminated source with options set. Expected: " +UnicodeString(u_errorName(expectedStatus
))
527 + " Got: "+ UnicodeString(u_errorName(status
))
528 + " Source: " + prettify(UnicodeString(src
))
533 status
= U_ZERO_ERROR
;
535 // test source with lengthand return value of number of UChars required
536 destLen
= func(tSrc
, tSrcLen
, NULL
,0,options
, &parseError
, &status
);
537 if(status
== U_BUFFER_OVERFLOW_ERROR
){
538 status
= U_ZERO_ERROR
; // reset error code
539 if(destLen
+1 < MAX_DEST_SIZE
){
541 destLen
= func(src
,u_strlen(src
),dest
,destLen
+1,options
, &parseError
, &status
);
542 // TODO : compare output with expected
543 if(U_SUCCESS(status
) && (doCompare
==TRUE
) && u_strCaseCompare(dest
,destLen
, expected
,expectedLen
,0,&status
)!=0){
544 errln("Did not get the expected result for %s with source length.\n",testName
);
547 errln( "%s with source length failed. Requires destCapacity > 300\n",testName
);
551 if(status
!= expectedStatus
){
552 errln( "Did not get the expected error for "+
553 UnicodeString(testName
)+
554 " with source length. Expected: " +UnicodeString(u_errorName(expectedStatus
))
555 + " Got: "+ UnicodeString(u_errorName(status
))
556 + " Source: " + prettify(UnicodeString(src
))
560 status
= U_ZERO_ERROR
;
562 destLen
= func(tSrc
,tSrcLen
,NULL
,0,options
| UIDNA_ALLOW_UNASSIGNED
, &parseError
, &status
);
564 if(status
== U_BUFFER_OVERFLOW_ERROR
){
565 status
= U_ZERO_ERROR
; // reset error code
566 if(destLen
+1 < MAX_DEST_SIZE
){
568 destLen
= func(src
,u_strlen(src
),dest
,destLen
+1,options
| UIDNA_ALLOW_UNASSIGNED
, &parseError
, &status
);
569 // TODO : compare output with expected
570 if(U_SUCCESS(status
) && (doCompare
==TRUE
) && u_strCaseCompare(dest
,destLen
, expected
,expectedLen
,0,&status
)!=0){
571 errln("Did not get the expected result for %s with source length and both options set.\n",testName
);
574 errln( "%s with source length failed. Requires destCapacity > 300\n",testName
);
577 //testing query string
578 if(status
!= expectedStatus
&& expectedStatus
!= U_IDNA_UNASSIGNED_ERROR
){
579 errln( "Did not get the expected error for "+
580 UnicodeString(testName
)+
581 " with source length and options set. Expected: " +UnicodeString(u_errorName(expectedStatus
))
582 + " Got: "+ UnicodeString(u_errorName(status
))
583 + " Source: " + prettify(UnicodeString(src
))
588 status
= U_ZERO_ERROR
;
589 if(testSTD3ASCIIRules
==TRUE
){
590 destLen
= func(src
,-1,NULL
,0,options
| UIDNA_USE_STD3_RULES
, &parseError
, &status
);
591 if(status
== U_BUFFER_OVERFLOW_ERROR
){
592 status
= U_ZERO_ERROR
; // reset error code
593 if(destLen
+1 < MAX_DEST_SIZE
){
595 destLen
= func(src
,-1,dest
,destLen
+1,options
| UIDNA_USE_STD3_RULES
, &parseError
, &status
);
596 // TODO : compare output with expected
597 if(U_SUCCESS(status
) && (doCompare
==TRUE
) && u_strCaseCompare(dest
,destLen
, expected
,expectedLen
,0,&status
)!=0){
598 //errln("Did not get the expected result for %s null terminated source with both options set.\n",testName);
599 errln("Did not get the expected result for "+UnicodeString(testName
) +" null terminated source with both options set. Expected: "+ prettify(UnicodeString(expected
,expectedLen
)));
603 errln( "%s null terminated source failed. Requires destCapacity > 300\n",testName
);
606 //testing query string
607 if(status
!= expectedStatus
){
608 errln( "Did not get the expected error for "+
609 UnicodeString(testName
)+
610 " null terminated source with options set. Expected: " +UnicodeString(u_errorName(expectedStatus
))
611 + " Got: "+ UnicodeString(u_errorName(status
))
612 + " Source: " + prettify(UnicodeString(src
))
616 status
= U_ZERO_ERROR
;
618 destLen
= func(tSrc
,tSrcLen
,NULL
,0,options
| UIDNA_USE_STD3_RULES
, &parseError
, &status
);
620 if(status
== U_BUFFER_OVERFLOW_ERROR
){
621 status
= U_ZERO_ERROR
; // reset error code
622 if(destLen
+1 < MAX_DEST_SIZE
){
624 destLen
= func(src
,u_strlen(src
),dest
,destLen
+1,options
| UIDNA_USE_STD3_RULES
, &parseError
, &status
);
625 // TODO : compare output with expected
626 if(U_SUCCESS(status
) && (doCompare
==TRUE
) && u_strCaseCompare(dest
,destLen
, expected
,expectedLen
,0,&status
)!=0){
627 errln("Did not get the expected result for %s with source length and both options set.\n",testName
);
630 errln( "%s with source length failed. Requires destCapacity > 300\n",testName
);
633 //testing query string
634 if(status
!= expectedStatus
&& expectedStatus
!= U_IDNA_UNASSIGNED_ERROR
){
635 errln( "Did not get the expected error for "+
636 UnicodeString(testName
)+
637 " with source length and options set. Expected: " +UnicodeString(u_errorName(expectedStatus
))
638 + " Got: "+ UnicodeString(u_errorName(status
))
639 + " Source: " + prettify(UnicodeString(src
))
646 void TestIDNA::testCompare(const UChar
* s1
, int32_t s1Len
,
647 const UChar
* s2
, int32_t s2Len
,
648 const char* testName
, CompareFunc func
,
651 UErrorCode status
= U_ZERO_ERROR
;
652 int32_t retVal
= func(s1
,-1,s2
,-1,UIDNA_DEFAULT
,&status
);
654 if(isEqual
==TRUE
&& retVal
!=0){
655 errln("Did not get the expected result for %s with null termniated strings.\n",testName
);
657 if(U_FAILURE(status
)){
658 errcheckln(status
, "%s null terminated source failed. Error: %s", testName
,u_errorName(status
));
661 status
= U_ZERO_ERROR
;
662 retVal
= func(s1
,-1,s2
,-1,UIDNA_ALLOW_UNASSIGNED
,&status
);
664 if(isEqual
==TRUE
&& retVal
!=0){
665 errln("Did not get the expected result for %s with null termniated strings with options set.\n", testName
);
667 if(U_FAILURE(status
)){
668 errcheckln(status
, "%s null terminated source and options set failed. Error: %s",testName
, u_errorName(status
));
671 status
= U_ZERO_ERROR
;
672 retVal
= func(s1
,s1Len
,s2
,s2Len
,UIDNA_DEFAULT
,&status
);
674 if(isEqual
==TRUE
&& retVal
!=0){
675 errln("Did not get the expected result for %s with string length.\n",testName
);
677 if(U_FAILURE(status
)){
678 errcheckln(status
, "%s with string length. Error: %s",testName
, u_errorName(status
));
681 status
= U_ZERO_ERROR
;
682 retVal
= func(s1
,s1Len
,s2
,s2Len
,UIDNA_ALLOW_UNASSIGNED
,&status
);
684 if(isEqual
==TRUE
&& retVal
!=0){
685 errln("Did not get the expected result for %s with string length and options set.\n",testName
);
687 if(U_FAILURE(status
)){
688 errcheckln(status
, "%s with string length and options set. Error: %s", u_errorName(status
), testName
);
692 void TestIDNA::testToASCII(const char* testName
, TestFunc func
){
695 UChar buf
[MAX_DEST_SIZE
];
697 for(i
=0;i
< (int32_t)(sizeof(unicodeIn
)/sizeof(unicodeIn
[0])); i
++){
698 u_charsToUChars(asciiIn
[i
],buf
, (int32_t)(strlen(asciiIn
[i
])+1));
699 testAPI(unicodeIn
[i
], buf
,testName
, FALSE
,U_ZERO_ERROR
, TRUE
, TRUE
, func
);
704 void TestIDNA::testToUnicode(const char* testName
, TestFunc func
){
707 UChar buf
[MAX_DEST_SIZE
];
709 for(i
=0;i
< (int32_t)(sizeof(asciiIn
)/sizeof(asciiIn
[0])); i
++){
710 u_charsToUChars(asciiIn
[i
],buf
, (int32_t)(strlen(asciiIn
[i
])+1));
711 testAPI(buf
,unicodeIn
[i
],testName
,FALSE
,U_ZERO_ERROR
, TRUE
, TRUE
, func
);
716 void TestIDNA::testIDNToUnicode(const char* testName
, TestFunc func
){
718 UChar buf
[MAX_DEST_SIZE
];
719 UChar expected
[MAX_DEST_SIZE
];
720 UErrorCode status
= U_ZERO_ERROR
;
722 UParseError parseError
;
723 for(i
=0;i
< (int32_t)(sizeof(domainNames
)/sizeof(domainNames
[0])); i
++){
724 bufLen
= (int32_t)strlen(domainNames
[i
]);
725 bufLen
= u_unescape(domainNames
[i
],buf
, bufLen
+1);
726 func(buf
,bufLen
,expected
,MAX_DEST_SIZE
, UIDNA_ALLOW_UNASSIGNED
, &parseError
,&status
);
727 if(U_FAILURE(status
)){
728 errcheckln(status
, "%s failed to convert domainNames[%i].Error: %s",testName
, i
, u_errorName(status
));
731 testAPI(buf
,expected
,testName
,FALSE
,U_ZERO_ERROR
, TRUE
, TRUE
, func
);
732 //test toUnicode with all labels in the string
733 testAPI(buf
,expected
,testName
, FALSE
,U_ZERO_ERROR
, TRUE
, TRUE
, func
);
734 if(U_FAILURE(status
)){
735 errln( "%s failed to convert domainNames[%i].Error: %s \n",testName
,i
, u_errorName(status
));
742 void TestIDNA::testIDNToASCII(const char* testName
, TestFunc func
){
744 UChar buf
[MAX_DEST_SIZE
];
745 UChar expected
[MAX_DEST_SIZE
];
746 UErrorCode status
= U_ZERO_ERROR
;
748 UParseError parseError
;
749 for(i
=0;i
< (int32_t)(sizeof(domainNames
)/sizeof(domainNames
[0])); i
++){
750 bufLen
= (int32_t)strlen(domainNames
[i
]);
751 bufLen
= u_unescape(domainNames
[i
],buf
, bufLen
+1);
752 func(buf
,bufLen
,expected
,MAX_DEST_SIZE
, UIDNA_ALLOW_UNASSIGNED
, &parseError
,&status
);
753 if(U_FAILURE(status
)){
754 errcheckln(status
, "%s failed to convert domainNames[%i].Error: %s",testName
,i
, u_errorName(status
));
757 testAPI(buf
,expected
,testName
, FALSE
,U_ZERO_ERROR
, TRUE
, TRUE
, func
);
758 //test toASCII with all labels in the string
759 testAPI(buf
,expected
,testName
, FALSE
,U_ZERO_ERROR
, FALSE
, TRUE
, func
);
760 if(U_FAILURE(status
)){
761 errln( "%s failed to convert domainNames[%i].Error: %s \n",testName
,i
, u_errorName(status
));
768 void TestIDNA::testCompare(const char* testName
, CompareFunc func
){
772 UChar www
[] = {0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
773 UChar com
[] = {0x002E, 0x0043, 0x004F, 0x004D, 0x0000};
774 UChar buf
[MAX_DEST_SIZE
]={0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
776 UnicodeString
source(www
), uni0(www
),uni1(www
), ascii0(www
), ascii1(www
);
778 uni0
.append(unicodeIn
[0]);
780 uni0
.append((UChar
)0x0000);
782 uni1
.append(unicodeIn
[1]);
784 uni1
.append((UChar
)0x0000);
786 ascii0
.append(asciiIn
[0]);
788 ascii0
.append((UChar
)0x0000);
790 ascii1
.append(asciiIn
[1]);
792 ascii1
.append((UChar
)0x0000);
794 for(i
=0;i
< (int32_t)(sizeof(unicodeIn
)/sizeof(unicodeIn
[0])); i
++){
796 u_charsToUChars(asciiIn
[i
],buf
+4, (int32_t)(strlen(asciiIn
[i
])+1));
799 // for every entry in unicodeIn array
800 // prepend www. and append .com
802 source
.append(unicodeIn
[i
]);
804 source
.append((UChar
)0x0000);
805 // a) compare it with itself
806 const UChar
* src
= source
.getBuffer();
807 int32_t srcLen
= u_strlen(src
); //subtract null
809 testCompare(src
,srcLen
,src
,srcLen
,testName
, func
, TRUE
);
811 // b) compare it with asciiIn equivalent
812 testCompare(src
,srcLen
,buf
,u_strlen(buf
),testName
, func
,TRUE
);
814 // c) compare it with unicodeIn not equivalent
816 testCompare(src
,srcLen
,uni1
.getBuffer(),uni1
.length()-1,testName
, func
,FALSE
);
818 testCompare(src
,srcLen
,uni0
.getBuffer(),uni0
.length()-1,testName
, func
,FALSE
);
820 // d) compare it with asciiIn not equivalent
822 testCompare(src
,srcLen
,ascii1
.getBuffer(),ascii1
.length()-1,testName
, func
,FALSE
);
824 testCompare(src
,srcLen
,ascii0
.getBuffer(),ascii0
.length()-1,testName
, func
,FALSE
);
833 getNextSeperator(UChar
*src
,int32_t srcLength
,
839 *limit
= src
+ i
; // point to null
843 *limit
= src
+ (i
+1); // go past the delimiter
847 // we have not found the delimiter
849 *limit
= src
+srcLength
;
854 for(i
=0;i
<srcLength
;i
++){
856 *limit
= src
+ (i
+1); // go past the delimiter
860 // we have not found the delimiter
862 *limit
= src
+srcLength
;
868 void printPunycodeOutput(){
870 UChar dest
[MAX_DEST_SIZE
];
871 int32_t destCapacity
=MAX_DEST_SIZE
;
875 UBool caseFlags
[MAX_DEST_SIZE
];
877 for(int32_t i
=0;i
< sizeof(errorCases
)/sizeof(errorCases
[0]);i
++){
878 ErrorCases errorCase
= errorCases
[i
];
879 UErrorCode status
= U_ZERO_ERROR
;
880 start
= errorCase
.unicode
;
881 int32_t srcLen
= u_strlen(start
);
882 labelLen
= getNextSeperator(start
,srcLen
,&limit
);
884 labelLen
=getNextSeperator(start
,srcLen
-labelLen
,&limit
);
885 int32_t destLen
= u_strToPunycode(dest
,destCapacity
,start
,labelLen
,caseFlags
, &status
);
886 if(U_FAILURE(status
)){
887 printf("u_strToPunycode failed for index %i\n",i
);
890 for(int32_t j
=0; j
<destLen
; j
++){
891 printf("%c",(char)dest
[j
]);
898 void TestIDNA::testErrorCases(const char* IDNToASCIIName
, TestFunc IDNToASCII
,
899 const char* IDNToUnicodeName
, TestFunc IDNToUnicode
){
900 UChar buf
[MAX_DEST_SIZE
];
903 for(int32_t i
=0;i
< (int32_t)(sizeof(errorCases
)/sizeof(errorCases
[0]));i
++){
904 ErrorCases errorCase
= errorCases
[i
];
906 if(errorCase
.ascii
!= NULL
){
907 bufLen
= (int32_t)strlen(errorCase
.ascii
);
908 u_charsToUChars(errorCase
.ascii
,buf
, bufLen
+1);
911 memset(buf
,0,U_SIZEOF_UCHAR
*MAX_DEST_SIZE
);
914 if(errorCase
.unicode
[0]!=0){
915 src
= errorCase
.unicode
;
919 IDNToASCIIName
, errorCase
.useSTD3ASCIIRules
,
920 errorCase
.expected
, TRUE
, TRUE
, IDNToASCII
);
921 if(errorCase
.testLabel
==TRUE
){
923 IDNToASCIIName
, errorCase
.useSTD3ASCIIRules
,
924 errorCase
.expected
, FALSE
,TRUE
, IDNToASCII
);
926 if(errorCase
.testToUnicode
==TRUE
){
927 testAPI((src
==NULL
)? NULL
: buf
,src
,
928 IDNToUnicodeName
, errorCase
.useSTD3ASCIIRules
,
929 errorCase
.expected
, TRUE
, TRUE
, IDNToUnicode
);
936 void TestIDNA::testConformance(const char* toASCIIName, TestFunc toASCII,
937 const char* IDNToASCIIName, TestFunc IDNToASCII,
938 const char* IDNToUnicodeName, TestFunc IDNToUnicode,
939 const char* toUnicodeName, TestFunc toUnicode){
940 UChar src[MAX_DEST_SIZE];
942 UChar expected[MAX_DEST_SIZE];
943 int32_t expectedLen = 0;
944 for(int32_t i=0;i< (int32_t)(sizeof(conformanceTestCases)/sizeof(conformanceTestCases[0]));i++){
945 const char* utf8Chars1 = conformanceTestCases[i].in;
946 int32_t utf8Chars1Len = (int32_t)strlen(utf8Chars1);
947 const char* utf8Chars2 = conformanceTestCases[i].out;
948 int32_t utf8Chars2Len = (utf8Chars2 == NULL) ? 0 : (int32_t)strlen(utf8Chars2);
950 UErrorCode status = U_ZERO_ERROR;
951 u_strFromUTF8(src,MAX_DEST_SIZE,&srcLen,utf8Chars1,utf8Chars1Len,&status);
952 if(U_FAILURE(status)){
953 errln(UnicodeString("Conversion of UTF8 source in conformanceTestCases[") + i +UnicodeString( "].in ( ")+prettify(utf8Chars1) +UnicodeString(" ) failed. Error: ")+ UnicodeString(u_errorName(status)));
956 if(utf8Chars2 != NULL){
957 u_strFromUTF8(expected,MAX_DEST_SIZE,&expectedLen,utf8Chars2,utf8Chars2Len, &status);
958 if(U_FAILURE(status)){
959 errln(UnicodeString("Conversion of UTF8 source in conformanceTestCases[") + i +UnicodeString( "].in ( ")+prettify(utf8Chars1) +UnicodeString(" ) failed. Error: ")+ UnicodeString(u_errorName(status)));
964 if(conformanceTestCases[i].expectedStatus != U_ZERO_ERROR){
966 testAPI(src,expected,
967 IDNToASCIIName, FALSE,
968 conformanceTestCases[i].expectedStatus,
970 (conformanceTestCases[i].expectedStatus != U_IDNA_UNASSIGNED_ERROR),
973 testAPI(src,expected,
975 conformanceTestCases[i].expectedStatus, TRUE,
976 (conformanceTestCases[i].expectedStatus != U_IDNA_UNASSIGNED_ERROR),
981 IDNToUnicodeName, FALSE,
982 conformanceTestCases[i].expectedStatus, TRUE, TRUE, IDNToUnicode);
984 toUnicodeName, FALSE,
985 conformanceTestCases[i].expectedStatus, TRUE, TRUE, toUnicode);
991 // test and ascertain
992 // func(func(func(src))) == func(src)
993 void TestIDNA::testChaining(const UChar
* src
,int32_t numIterations
,const char* testName
,
994 UBool useSTD3ASCIIRules
, UBool caseInsensitive
, TestFunc func
){
995 UChar even
[MAX_DEST_SIZE
];
996 UChar odd
[MAX_DEST_SIZE
];
997 UChar expected
[MAX_DEST_SIZE
];
998 int32_t i
=0,evenLen
=0,oddLen
=0,expectedLen
=0;
999 UErrorCode status
= U_ZERO_ERROR
;
1000 int32_t srcLen
= u_strlen(src
);
1001 int32_t options
= (useSTD3ASCIIRules
== TRUE
) ? UIDNA_USE_STD3_RULES
: UIDNA_DEFAULT
;
1002 UParseError parseError
;
1004 // test null-terminated source
1005 expectedLen
= func(src
,-1,expected
,MAX_DEST_SIZE
, options
, &parseError
, &status
);
1006 if(U_FAILURE(status
)){
1007 errcheckln(status
, "%s null terminated source failed. Error: %s",testName
, u_errorName(status
));
1009 memcpy(odd
,expected
,(expectedLen
+1) * U_SIZEOF_UCHAR
);
1010 memcpy(even
,expected
,(expectedLen
+1) * U_SIZEOF_UCHAR
);
1011 for(;i
<=numIterations
; i
++){
1013 evenLen
= func(odd
,-1,even
,MAX_DEST_SIZE
,options
, &parseError
, &status
);
1014 if(U_FAILURE(status
)){
1015 errcheckln(status
, "%s null terminated source failed - %s",testName
, u_errorName(status
));
1019 oddLen
= func(even
,-1,odd
,MAX_DEST_SIZE
,options
, &parseError
, &status
);
1020 if(U_FAILURE(status
)){
1021 errln("%s null terminated source failed\n",testName
);
1026 if(caseInsensitive
==TRUE
){
1027 if( u_strCaseCompare(even
,evenLen
, expected
,expectedLen
, 0, &status
) !=0 ||
1028 u_strCaseCompare(odd
,oddLen
, expected
,expectedLen
, 0, &status
) !=0 ){
1030 errln("Chaining for %s null terminated source failed\n",testName
);
1033 if( u_strncmp(even
,expected
,expectedLen
) != 0 ||
1034 u_strncmp(odd
,expected
,expectedLen
) !=0 ){
1036 errln("Chaining for %s null terminated source failed\n",testName
);
1040 // test null-terminated source
1041 status
= U_ZERO_ERROR
;
1042 expectedLen
= func(src
,-1,expected
,MAX_DEST_SIZE
,options
|UIDNA_ALLOW_UNASSIGNED
, &parseError
, &status
);
1043 if(U_FAILURE(status
)){
1044 errcheckln(status
, "%s null terminated source with options set failed. Error: %s",testName
, u_errorName(status
));
1046 memcpy(odd
,expected
,(expectedLen
+1) * U_SIZEOF_UCHAR
);
1047 memcpy(even
,expected
,(expectedLen
+1) * U_SIZEOF_UCHAR
);
1048 for(;i
<=numIterations
; i
++){
1050 evenLen
= func(odd
,-1,even
,MAX_DEST_SIZE
,options
|UIDNA_ALLOW_UNASSIGNED
, &parseError
, &status
);
1051 if(U_FAILURE(status
)){
1052 errcheckln(status
, "%s null terminated source with options set failed - %s",testName
, u_errorName(status
));
1056 oddLen
= func(even
,-1,odd
,MAX_DEST_SIZE
,options
|UIDNA_ALLOW_UNASSIGNED
, &parseError
, &status
);
1057 if(U_FAILURE(status
)){
1058 errln("%s null terminated source with options set failed\n",testName
);
1063 if(caseInsensitive
==TRUE
){
1064 if( u_strCaseCompare(even
,evenLen
, expected
,expectedLen
, 0, &status
) !=0 ||
1065 u_strCaseCompare(odd
,oddLen
, expected
,expectedLen
, 0, &status
) !=0 ){
1067 errln("Chaining for %s null terminated source with options set failed\n",testName
);
1070 if( u_strncmp(even
,expected
,expectedLen
) != 0 ||
1071 u_strncmp(odd
,expected
,expectedLen
) !=0 ){
1073 errln("Chaining for %s null terminated source with options set failed\n",testName
);
1078 // test source with length
1079 status
= U_ZERO_ERROR
;
1080 expectedLen
= func(src
,srcLen
,expected
,MAX_DEST_SIZE
,options
, &parseError
, &status
);
1081 if(U_FAILURE(status
)){
1082 errcheckln(status
, "%s null terminated source failed. Error: %s",testName
, u_errorName(status
));
1084 memcpy(odd
,expected
,(expectedLen
+1) * U_SIZEOF_UCHAR
);
1085 memcpy(even
,expected
,(expectedLen
+1) * U_SIZEOF_UCHAR
);
1086 for(;i
<=numIterations
; i
++){
1088 evenLen
= func(odd
,oddLen
,even
,MAX_DEST_SIZE
,options
, &parseError
, &status
);
1089 if(U_FAILURE(status
)){
1090 errcheckln(status
, "%s source with source length failed - %s",testName
, u_errorName(status
));
1094 oddLen
= func(even
,evenLen
,odd
,MAX_DEST_SIZE
,options
, &parseError
, &status
);
1095 if(U_FAILURE(status
)){
1096 errcheckln(status
, "%s source with source length failed - %s",testName
, u_errorName(status
));
1101 if(caseInsensitive
==TRUE
){
1102 if( u_strCaseCompare(even
,evenLen
, expected
,expectedLen
, 0, &status
) !=0 ||
1103 u_strCaseCompare(odd
,oddLen
, expected
,expectedLen
, 0, &status
) !=0 ){
1105 errln("Chaining for %s source with source length failed\n",testName
);
1108 if( u_strncmp(even
,expected
,expectedLen
) != 0 ||
1109 u_strncmp(odd
,expected
,expectedLen
) !=0 ){
1111 errln("Chaining for %s source with source length failed\n",testName
);
1114 status
= U_ZERO_ERROR
;
1115 expectedLen
= func(src
,srcLen
,expected
,MAX_DEST_SIZE
,options
|UIDNA_ALLOW_UNASSIGNED
, &parseError
, &status
);
1116 if(U_FAILURE(status
)){
1117 errcheckln(status
, "%s null terminated source with options set failed. Error: %s",testName
, u_errorName(status
));
1119 memcpy(odd
,expected
,(expectedLen
+1) * U_SIZEOF_UCHAR
);
1120 memcpy(even
,expected
,(expectedLen
+1) * U_SIZEOF_UCHAR
);
1121 for(;i
<=numIterations
; i
++){
1123 evenLen
= func(odd
,oddLen
,even
,MAX_DEST_SIZE
,options
|UIDNA_ALLOW_UNASSIGNED
, &parseError
, &status
);
1124 if(U_FAILURE(status
)){
1125 errcheckln(status
, "%s source with source length and options set failed - %s",testName
, u_errorName(status
));
1129 oddLen
= func(even
,evenLen
,odd
,MAX_DEST_SIZE
,options
|UIDNA_ALLOW_UNASSIGNED
, &parseError
, &status
);
1130 if(U_FAILURE(status
)){
1131 errcheckln(status
, "%s source with source length and options set failed - %s",testName
, u_errorName(status
));
1136 if(caseInsensitive
==TRUE
){
1137 if( u_strCaseCompare(even
,evenLen
, expected
,expectedLen
, 0, &status
) !=0 ||
1138 u_strCaseCompare(odd
,oddLen
, expected
,expectedLen
, 0, &status
) !=0 ){
1140 errln("Chaining for %s source with source length and options set failed\n",testName
);
1143 if( u_strncmp(even
,expected
,expectedLen
) != 0 ||
1144 u_strncmp(odd
,expected
,expectedLen
) !=0 ){
1146 errln("Chaining for %s source with source length and options set failed\n",testName
);
1150 void TestIDNA::testChaining(const char* toASCIIName
, TestFunc toASCII
,
1151 const char* toUnicodeName
, TestFunc toUnicode
){
1153 UChar buf
[MAX_DEST_SIZE
];
1155 for(i
=0;i
< (int32_t)(sizeof(asciiIn
)/sizeof(asciiIn
[0])); i
++){
1156 u_charsToUChars(asciiIn
[i
],buf
, (int32_t)(strlen(asciiIn
[i
])+1));
1157 testChaining(buf
,5,toUnicodeName
, FALSE
, FALSE
, toUnicode
);
1159 for(i
=0;i
< (int32_t)(sizeof(unicodeIn
)/sizeof(unicodeIn
[0])); i
++){
1160 testChaining(unicodeIn
[i
], 5,toASCIIName
, FALSE
, TRUE
, toASCII
);
1165 void TestIDNA::testRootLabelSeparator(const char* testName
, CompareFunc func
,
1166 const char* IDNToASCIIName
, TestFunc IDNToASCII
,
1167 const char* IDNToUnicodeName
, TestFunc IDNToUnicode
){
1171 UChar www
[] = {0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
1172 UChar com
[] = {0x002E, 0x0043, 0x004F, 0x004D, 0x002E, /* root label separator */0x0000};
1173 UChar buf
[MAX_DEST_SIZE
]={0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
1175 UnicodeString
source(www
), uni0(www
),uni1(www
), ascii0(www
), ascii1(www
);
1177 uni0
.append(unicodeIn
[0]);
1179 uni0
.append((UChar
)0x0000);
1181 uni1
.append(unicodeIn
[1]);
1183 uni1
.append((UChar
)0x0000);
1185 ascii0
.append(asciiIn
[0]);
1187 ascii0
.append((UChar
)0x0000);
1189 ascii1
.append(asciiIn
[1]);
1191 ascii1
.append((UChar
)0x0000);
1193 for(i
=0;i
< (int32_t)(sizeof(unicodeIn
)/sizeof(unicodeIn
[0])); i
++){
1195 u_charsToUChars(asciiIn
[i
],buf
+4, (int32_t)(strlen(asciiIn
[i
])+1));
1198 // for every entry in unicodeIn array
1199 // prepend www. and append .com
1201 source
.append(unicodeIn
[i
]);
1203 source
.append((UChar
)0x0000);
1205 const UChar
* src
= source
.getBuffer();
1206 int32_t srcLen
= u_strlen(src
); //subtract null
1208 // b) compare it with asciiIn equivalent
1209 testCompare(src
,srcLen
,buf
,u_strlen(buf
),testName
, func
,TRUE
);
1211 // a) compare it with itself
1212 testCompare(src
,srcLen
,src
,srcLen
,testName
, func
,TRUE
);
1215 // IDNToASCII comparison
1216 testAPI(src
,buf
,IDNToASCIIName
,FALSE
,U_ZERO_ERROR
,TRUE
, TRUE
, IDNToASCII
);
1217 // IDNToUnicode comparison
1218 testAPI(buf
,src
,IDNToUnicodeName
, FALSE
,U_ZERO_ERROR
, TRUE
, TRUE
, IDNToUnicode
);
1220 // c) compare it with unicodeIn not equivalent
1222 testCompare(src
,srcLen
,uni1
.getBuffer(),uni1
.length()-1,testName
, func
,FALSE
);
1224 testCompare(src
,srcLen
,uni0
.getBuffer(),uni0
.length()-1,testName
, func
,FALSE
);
1226 // d) compare it with asciiIn not equivalent
1228 testCompare(src
,srcLen
,ascii1
.getBuffer(),ascii1
.length()-1,testName
, func
,FALSE
);
1230 testCompare(src
,srcLen
,ascii0
.getBuffer(),ascii0
.length()-1,testName
, func
,FALSE
);
1235 //---------------------------------------------
1237 //---------------------------------------------
1239 extern IntlTest
*createUTS46Test();
1241 void TestIDNA::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
1243 if (exec
) logln((UnicodeString
)"TestSuite IDNA API ");
1246 case 0: name
= "TestToASCII"; if (exec
) TestToASCII(); break;
1247 case 1: name
= "TestToUnicode"; if (exec
) TestToUnicode(); break;
1248 case 2: name
= "TestIDNToASCII"; if (exec
) TestIDNToASCII(); break;
1249 case 3: name
= "TestIDNToUnicode"; if (exec
) TestIDNToUnicode(); break;
1250 case 4: name
= "TestCompare"; if (exec
) TestCompare(); break;
1251 case 5: name
= "TestErrorCases"; if (exec
) TestErrorCases(); break;
1252 case 6: name
= "TestChaining"; if (exec
) TestChaining(); break;
1253 case 7: name
= "TestRootLabelSeparator"; if(exec
) TestRootLabelSeparator(); break;
1254 case 8: name
= "TestCompareReferenceImpl"; if(exec
) TestCompareReferenceImpl(); break;
1255 case 9: name
= "TestDataFile"; if(exec
) TestDataFile(); break;
1256 #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
1257 case 10: name
= "TestRefIDNA"; if(exec
) TestRefIDNA(); break;
1258 case 11: name
= "TestIDNAMonkeyTest"; if(exec
) TestIDNAMonkeyTest(); break;
1260 case 10: case 11: name
= "skip"; break;
1264 name
= "TestConformanceTestVectors";
1266 logln("TestSuite IDNA conf----"); logln();
1268 callTest(test
, par
);
1275 logln("TestSuite UTS46Test---"); logln();
1276 LocalPointer
<IntlTest
> test(createUTS46Test());
1277 callTest(*test
, par
);
1280 default: name
= ""; break; /*needed to end loop*/
1283 void TestIDNA::TestToASCII(){
1284 testToASCII("uidna_toASCII", uidna_toASCII
);
1286 void TestIDNA::TestToUnicode(){
1287 testToUnicode("uidna_toUnicode", uidna_toUnicode
);
1289 void TestIDNA::TestIDNToASCII(){
1290 testIDNToASCII("uidna_IDNToASCII", uidna_IDNToASCII
);
1292 void TestIDNA::TestIDNToUnicode(){
1293 testIDNToUnicode("uidna_IDNToUnicode", uidna_IDNToUnicode
);
1295 void TestIDNA::TestCompare(){
1296 testCompare("uidna_compare",uidna_compare
);
1298 void TestIDNA::TestErrorCases(){
1299 testErrorCases( "uidna_IDNToASCII",uidna_IDNToASCII
,
1300 "uidna_IDNToUnicode",uidna_IDNToUnicode
);
1302 void TestIDNA::TestRootLabelSeparator(){
1303 testRootLabelSeparator( "uidna_compare",uidna_compare
,
1304 "uidna_IDNToASCII", uidna_IDNToASCII
,
1305 "uidna_IDNToUnicode",uidna_IDNToUnicode
1308 void TestIDNA::TestChaining(){
1309 testChaining("uidna_toASCII",uidna_toASCII
, "uidna_toUnicode", uidna_toUnicode
);
1313 static const int loopCount
= 100;
1314 static const int maxCharCount
= 20;
1315 static const int maxCodePoint
= 0x10ffff;
1319 static UBool initialized
= FALSE
;
1322 srand((unsigned)time(NULL
));
1325 // Assume rand has at least 12 bits of precision
1327 for (uint32_t i
=0; i
<sizeof(l
); ++i
)
1328 ((char*)&l
)[i
] = (char)((rand() & 0x0FF0) >> 4);
1333 * Return a random integer i where 0 <= i < n.
1334 * A special function that gets random codepoints from planes 0,1,2 and 14
1336 static int32_t rand_uni()
1338 int32_t retVal
= (int32_t)(randul()& 0x3FFFF);
1339 if(retVal
>= 0x30000){
1345 static int32_t randi(int32_t n
){
1346 return (int32_t) (randul() % (n
+1));
1349 void getTestSource(UnicodeString
& fillIn
) {
1351 int32_t charCount
= (randi(maxCharCount
) + 1);
1352 while (i
<charCount
) {
1353 int32_t codepoint
= rand_uni();
1354 if(codepoint
== 0x0000){
1357 fillIn
.append((UChar32
)codepoint
);
1363 UnicodeString
TestIDNA::testCompareReferenceImpl(UnicodeString
& src
,
1364 TestFunc refIDNA
, const char* refIDNAName
,
1365 TestFunc uIDNA
, const char* uIDNAName
,
1368 const UChar
* srcUChars
= src
.getBuffer();
1369 UChar exp
[MAX_DEST_SIZE
]={0};
1370 int32_t expCap
= MAX_DEST_SIZE
, expLen
=0;
1371 UErrorCode expStatus
= U_ZERO_ERROR
;
1372 UParseError parseError
;
1374 logln("Comparing "+ UnicodeString(refIDNAName
)
1375 + " with "+ UnicodeString(uIDNAName
)
1376 +" for input: " + prettify(srcUChars
));
1378 expLen
= refIDNA(srcUChars
, src
.length()-1, exp
, expCap
,
1379 options
, &parseError
, &expStatus
);
1381 UChar got
[MAX_DEST_SIZE
]={0};
1382 int32_t gotCap
= MAX_DEST_SIZE
, gotLen
=0;
1383 UErrorCode gotStatus
= U_ZERO_ERROR
;
1385 gotLen
= uIDNA(srcUChars
, src
.length()-1, got
, gotCap
,
1386 options
, &parseError
, &gotStatus
);
1388 if(expStatus
!= gotStatus
){
1389 errln("Did not get the expected status while comparing " + UnicodeString(refIDNAName
)
1390 + " with " + UnicodeString(uIDNAName
)
1391 + " Expected: " + UnicodeString(u_errorName(expStatus
))
1392 + " Got: " + UnicodeString(u_errorName(gotStatus
))
1393 + " for Source: "+ prettify(srcUChars
)
1394 + " Options: " + options
);
1395 return UnicodeString("");
1398 // now we know that both implementations yielded same error
1399 if(U_SUCCESS(expStatus
)){
1400 // compare the outputs if status == U_ZERO_ERROR
1401 if(u_strCompare(exp
, expLen
, got
, gotLen
, TRUE
) != 0){
1402 errln("Did not get the expected output while comparing " + UnicodeString(refIDNAName
)
1403 + " with " + UnicodeString(uIDNAName
)
1404 + " Expected: " + prettify(UnicodeString(exp
, expLen
))
1405 + " Got: " + prettify(UnicodeString(got
, gotLen
))
1406 + " for Source: "+ prettify(srcUChars
)
1407 + " Options: " + options
);
1409 return UnicodeString(exp
, expLen
);
1412 logln("Got the same error while comparing "
1413 + UnicodeString(refIDNAName
)
1414 + " with "+ UnicodeString(uIDNAName
)
1415 +" for input: " + prettify(srcUChars
));
1417 return UnicodeString("");
1420 void TestIDNA::testCompareReferenceImpl(const UChar
* src
, int32_t srcLen
){
1421 UnicodeString
label(src
,srcLen
);
1422 label
.append((UChar
)0x0000);
1424 //test idnaref_toASCII and idnare
1425 UnicodeString asciiLabel
= testCompareReferenceImpl(label
,
1426 idnaref_toASCII
, "idnaref_toASCII",
1427 uidna_toASCII
, "uidna_toASCII",
1428 UIDNA_ALLOW_UNASSIGNED
);
1429 testCompareReferenceImpl(label
,
1430 idnaref_toASCII
, "idnaref_toASCII",
1431 uidna_toASCII
, "uidna_toASCII",
1433 testCompareReferenceImpl(label
,
1434 idnaref_toASCII
, "idnaref_toASCII",
1435 uidna_toASCII
, "uidna_toASCII",
1436 UIDNA_USE_STD3_RULES
);
1437 testCompareReferenceImpl(label
,
1438 idnaref_toASCII
, "idnaref_toASCII",
1439 uidna_toASCII
, "uidna_toASCII",
1440 UIDNA_USE_STD3_RULES
| UIDNA_ALLOW_UNASSIGNED
);
1442 if(asciiLabel
.length()!=0){
1443 asciiLabel
.append((UChar
)0x0000);
1446 testCompareReferenceImpl(asciiLabel
,
1447 idnaref_toUnicode
, "idnaref_toUnicode",
1448 uidna_toUnicode
, "uidna_toUnicode",
1449 UIDNA_ALLOW_UNASSIGNED
);
1450 testCompareReferenceImpl(asciiLabel
,
1451 idnaref_toUnicode
, "idnaref_toUnicode",
1452 uidna_toUnicode
, "uidna_toUnicode",
1454 testCompareReferenceImpl(asciiLabel
,
1455 idnaref_toUnicode
, "idnaref_toUnicode",
1456 uidna_toUnicode
, "uidna_toUnicode",
1457 UIDNA_USE_STD3_RULES
);
1458 testCompareReferenceImpl(asciiLabel
,
1459 idnaref_toUnicode
, "idnaref_toUnicode",
1460 uidna_toUnicode
, "uidna_toUnicode",
1461 UIDNA_USE_STD3_RULES
| UIDNA_ALLOW_UNASSIGNED
);
1465 const char* failures
[] ={
1466 "\\uAA42\\U0001F8DD\\U00019D01\\U000149A3\\uD385\\U000EE0F5\\U00018B92\\U000179D1\\U00018624\\U0002227F\\U000E83C0\\U000E8DCD\\u5460\\U00017F34\\U0001570B\\u43D1\\U0002C9C9\\U000281EC\\u2105\\U000180AE\\uC5D4",
1467 "\\U0002F5A6\\uD638\\u0D0A\\u9E9C\\uFE5B\\U0001FCCB\\u66C4",
1470 void TestIDNA::TestIDNAMonkeyTest(){
1471 UnicodeString source
;
1472 UErrorCode status
= U_ZERO_ERROR
;
1475 getInstance(status
); // Init prep
1476 if (U_FAILURE(status
)) {
1477 dataerrln("Test could not initialize. Got %s", u_errorName(status
));
1481 for(i
=0; i
<loopCount
; i
++){
1483 getTestSource(source
);
1484 source
.append((UChar
)0x0000);
1485 const UChar
* src
= source
.getBuffer();
1486 testCompareReferenceImpl(src
,source
.length()-1);
1487 testCompareReferenceImpl(src
,source
.length()-1);
1491 for (i
=0; i
<(int)(sizeof(failures
)/sizeof(failures
[0])); i
++){
1493 source
.append( UnicodeString(failures
[i
], -1, US_INV
) );
1494 source
= source
.unescape();
1495 source
.append((UChar
)0x0000);
1496 const UChar
*src
= source
.getBuffer();
1497 testCompareReferenceImpl(src
,source
.length()-1);
1498 //debug(source.getBuffer(),source.length(),UIDNA_ALLOW_UNASSIGNED);
1503 source
.append(UNICODE_STRING_SIMPLE("\\uCF18\\U00021161\\U000EEF11\\U0002BB82\\U0001D63C"));
1504 debug(source
.getBuffer(),source
.length(),UIDNA_ALLOW_UNASSIGNED
);
1506 { // test deletion of code points
1507 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", -1, US_INV
);
1508 source
= source
.unescape();
1509 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", -1, US_INV
);
1510 expected
= expected
.unescape();
1511 UnicodeString
ascii("xn--b1abfaaepdrnnbgefbadotcwatmq2g4l");
1512 ascii
.append((UChar
)0x0000);
1513 testAPI(source
.getBuffer(),ascii
.getBuffer(), "uidna_toASCII", FALSE
, U_ZERO_ERROR
, TRUE
, TRUE
, uidna_toASCII
);
1515 testAPI(source
.getBuffer(),ascii
.getBuffer(), "idnaref_toASCII", FALSE
, U_ZERO_ERROR
, TRUE
, TRUE
, idnaref_toASCII
);
1517 testCompareReferenceImpl(source
.getBuffer(), source
.length()-1);
1522 void TestIDNA::TestCompareReferenceImpl(){
1524 UChar src
[2] = {0,0};
1529 UErrorCode dataStatus
= U_ZERO_ERROR
;
1530 loadTestData(dataStatus
);
1531 if(U_FAILURE(dataStatus
)) {
1532 dataerrln("Couldn't load test data: %s\n", u_errorName(dataStatus
)); // save us from thousands and thousands of errors
1537 for (int32_t i
= 0; i
<= 0x10FFFF; i
++){
1538 if (quick
== TRUE
&& i
> 0x0FFF){
1542 // jump to E0000, no characters assigned in plain 3 to plain 13 as of Unicode 6.0
1546 src
[0] = U16_LEAD(i
);
1547 src
[1] = U16_TRAIL(i
);
1554 testCompareReferenceImpl(src
, srcLen
);
1558 void TestIDNA::TestRefIDNA(){
1559 UErrorCode status
= U_ZERO_ERROR
;
1560 getInstance(status
); // Init prep
1561 if (U_FAILURE(status
)) {
1562 if (status
== U_FILE_ACCESS_ERROR
) {
1563 dataerrln("Test could not initialize. Got %s", u_errorName(status
));
1568 testToASCII("idnaref_toASCII", idnaref_toASCII
);
1569 testToUnicode("idnaref_toUnicode", idnaref_toUnicode
);
1570 testIDNToASCII("idnaref_IDNToASCII", idnaref_IDNToASCII
);
1571 testIDNToUnicode("idnaref_IDNToUnicode", idnaref_IDNToUnicode
);
1572 testCompare("idnaref_compare",idnaref_compare
);
1573 testErrorCases( "idnaref_IDNToASCII",idnaref_IDNToASCII
,
1574 "idnaref_IDNToUnicode",idnaref_IDNToUnicode
);
1575 testChaining("idnaref_toASCII",idnaref_toASCII
, "idnaref_toUnicode", idnaref_toUnicode
);
1577 testRootLabelSeparator( "idnaref_compare",idnaref_compare
,
1578 "idnaref_IDNToASCII", idnaref_IDNToASCII
,
1579 "idnaref_IDNToUnicode",idnaref_IDNToUnicode
1581 testChaining("idnaref_toASCII",idnaref_toASCII
, "idnaref_toUnicode", idnaref_toUnicode
);
1585 void TestIDNA::TestDataFile(){
1588 TestIDNA::~TestIDNA(){
1595 NamePrepTransform
* TestIDNA::gPrep
= NULL
;
1597 NamePrepTransform
* TestIDNA::getInstance(UErrorCode
& status
){
1598 if(TestIDNA::gPrep
== NULL
){
1599 UParseError parseError
;
1600 TestIDNA::gPrep
= NamePrepTransform::createInstance(parseError
, status
);
1601 if(TestIDNA::gPrep
==NULL
){
1602 //status = U_MEMORY_ALLOCATION_ERROR;
1606 return TestIDNA::gPrep
;
1609 #endif /* #if !UCONFIG_NO_IDNA */