1/*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2003-2009, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 *******************************************************************************
8 *   file name:  idnatest.c
9 *   encoding:   US-ASCII
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 2003jul11
14 *   created by: Ram Viswanadha
15 */
16#include <stdlib.h>
17#include <string.h>
18#include "unicode/utypes.h"
19
20#if !UCONFIG_NO_IDNA
21
22#include "unicode/ustring.h"
23#include "unicode/uidna.h"
24#include "cintltst.h"
25
26
27
28#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
29#define MAX_DEST_SIZE 1000
30
31static void TestToUnicode(void);
32static void TestToASCII(void);
33static void TestIDNToUnicode(void);
34static void TestIDNToASCII(void);
35static void TestCompare(void);
36static void TestJB4490(void);
37static void TestJB4475(void);
38static void TestLength(void);
39static void TestJB5273(void);
40void addIDNATest(TestNode** root);
41
42
43typedef int32_t
44(U_EXPORT2 *TestFunc) (   const UChar *src, int32_t srcLength,
45                UChar *dest, int32_t destCapacity,
46                int32_t options, UParseError *parseError,
47                UErrorCode *status);
48typedef int32_t
49(U_EXPORT2 *CompareFunc) (const UChar *s1, int32_t s1Len,
50                const UChar *s2, int32_t s2Len,
51                int32_t options,
52                UErrorCode *status);
53
54
55void
56addIDNATest(TestNode** root)
57{
58   addTest(root, &TestToUnicode,    "idna/TestToUnicode");
59   addTest(root, &TestToASCII,      "idna/TestToASCII");
60   addTest(root, &TestIDNToUnicode, "idna/TestIDNToUnicode");
61   addTest(root, &TestIDNToASCII,   "idna/TestIDNToASCII");
62   addTest(root, &TestCompare,      "idna/TestCompare");
63   addTest(root, &TestJB4490,       "idna/TestJB4490");
64   addTest(root, &TestJB4475,       "idna/TestJB4475");
65   addTest(root, &TestLength,       "idna/TestLength");
66   addTest(root, &TestJB5273,       "idna/TestJB5273");
67}
68
69static void
70testAPI(const UChar* src, const UChar* expected, const char* testName,
71            UBool useSTD3ASCIIRules,UErrorCode expectedStatus,
72            UBool doCompare, UBool testUnassigned,  TestFunc func){
73
74    UErrorCode status = U_ZERO_ERROR;
75    UChar destStack[MAX_DEST_SIZE];
76    int32_t destLen = 0;
77    UChar* dest = NULL;
78    int32_t expectedLen = (expected != NULL) ? u_strlen(expected) : 0;
79    int32_t options = (useSTD3ASCIIRules == TRUE) ? UIDNA_USE_STD3_RULES : UIDNA_DEFAULT;
80    UParseError parseError;
81    int32_t tSrcLen = 0;
82    UChar* tSrc = NULL;
83
84    if(src != NULL){
85        tSrcLen = u_strlen(src);
86        tSrc  =(UChar*) malloc( U_SIZEOF_UCHAR * tSrcLen );
87        memcpy(tSrc,src,tSrcLen * U_SIZEOF_UCHAR);
88    }
89
90    /* test null-terminated source and return value of number of UChars required */
91
92    destLen = func(src,-1,NULL,0,options, &parseError , &status);
93    if(status == U_BUFFER_OVERFLOW_ERROR){
94        status = U_ZERO_ERROR; /* reset error code */
95        if(destLen+1 < MAX_DEST_SIZE){
96            dest = destStack;
97            destLen = func(src,-1,dest,destLen+1,options, &parseError, &status);
98            /* TODO : compare output with expected */
99            if(U_SUCCESS(status) && expectedStatus != U_IDNA_STD3_ASCII_RULES_ERROR&& (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
100                log_err("Did not get the expected result for  null terminated source.\n" );
101            }
102        }else{
103            log_err( "%s null terminated source failed. Requires destCapacity > 300\n",testName);
104        }
105    }
106
107    if(status != expectedStatus){
108        log_err_status(status,  "Did not get the expected error for %s null terminated source failed. Expected: %s Got: %s\n",testName, u_errorName(expectedStatus), u_errorName(status));
109        free(tSrc);
110        return;
111    }
112    if(testUnassigned ){
113        status = U_ZERO_ERROR;
114        destLen = func(src,-1,NULL,0,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
115        if(status == U_BUFFER_OVERFLOW_ERROR){
116            status = U_ZERO_ERROR; /* reset error code */
117            if(destLen+1 < MAX_DEST_SIZE){
118                dest = destStack;
119                destLen = func(src,-1,dest,destLen+1,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
120                /* TODO : compare output with expected */
121                if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
122                    log_err("Did not get the expected result for %s null terminated source with both options set.\n",testName);
123
124                }
125            }else{
126                log_err( "%s null terminated source failed. Requires destCapacity > 300\n",testName);
127            }
128        }
129        /*testing query string*/
130        if(status != expectedStatus && expectedStatus != U_IDNA_UNASSIGNED_ERROR){
131            log_err( "Did not get the expected error for %s null terminated source with options set. Expected: %s Got: %s\n",testName, u_errorName(expectedStatus), u_errorName(status));
132        }
133    }
134
135    status = U_ZERO_ERROR;
136
137    /* test source with lengthand return value of number of UChars required*/
138    destLen = func(tSrc, tSrcLen, NULL,0,options, &parseError, &status);
139    if(status == U_BUFFER_OVERFLOW_ERROR){
140        status = U_ZERO_ERROR; /* reset error code */
141        if(destLen+1 < MAX_DEST_SIZE){
142            dest = destStack;
143            destLen = func(src,u_strlen(src),dest,destLen+1,options, &parseError, &status);
144            /* TODO : compare output with expected */
145            if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
146                log_err("Did not get the expected result for %s with source length.\n",testName);
147            }
148        }else{
149            log_err( "%s with source length  failed. Requires destCapacity > 300\n",testName);
150        }
151    }
152
153    if(status != expectedStatus){
154        log_err( "Did not get the expected error for %s with source length. Expected: %s Got: %s\n",testName, u_errorName(expectedStatus), u_errorName(status));
155    }
156    if(testUnassigned){
157        status = U_ZERO_ERROR;
158
159        destLen = func(tSrc,tSrcLen,NULL,0,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
160
161        if(status == U_BUFFER_OVERFLOW_ERROR){
162            status = U_ZERO_ERROR; /* reset error code */
163            if(destLen+1 < MAX_DEST_SIZE){
164                dest = destStack;
165                destLen = func(src,u_strlen(src),dest,destLen+1,options | UIDNA_ALLOW_UNASSIGNED, &parseError, &status);
166                /* TODO : compare output with expected */
167                if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
168                    log_err("Did not get the expected result for %s with source length and both options set.\n",testName);
169                }
170            }else{
171                log_err( "%s with source length  failed. Requires destCapacity > 300\n",testName);
172            }
173        }
174        /*testing query string*/
175        if(status != expectedStatus && expectedStatus != U_IDNA_UNASSIGNED_ERROR){
176            log_err( "Did not get the expected error for %s with source length and options set. Expected: %s Got: %s\n",testName, u_errorName(expectedStatus), u_errorName(status));
177        }
178    }
179
180    status = U_ZERO_ERROR;
181    destLen = func(src,-1,NULL,0,options | UIDNA_USE_STD3_RULES, &parseError, &status);
182    if(status == U_BUFFER_OVERFLOW_ERROR){
183        status = U_ZERO_ERROR; /* reset error code*/
184        if(destLen+1 < MAX_DEST_SIZE){
185            dest = destStack;
186            destLen = func(src,-1,dest,destLen+1,options | UIDNA_USE_STD3_RULES, &parseError, &status);
187            /* TODO : compare output with expected*/
188            if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
189                log_err("Did not get the expected result for %s null terminated source with both options set.\n",testName);
190
191            }
192        }else{
193            log_err( "%s null terminated source failed. Requires destCapacity > 300\n",testName);
194        }
195    }
196    /*testing query string*/
197    if(status != expectedStatus){
198        log_err( "Did not get the expected error for %s null terminated source with options set. Expected: %s Got: %s\n",testName, u_errorName(expectedStatus), u_errorName(status));
199    }
200
201    status = U_ZERO_ERROR;
202
203    destLen = func(tSrc,tSrcLen,NULL,0,options | UIDNA_USE_STD3_RULES, &parseError, &status);
204
205    if(status == U_BUFFER_OVERFLOW_ERROR){
206        status = U_ZERO_ERROR; /* reset error code*/
207        if(destLen+1 < MAX_DEST_SIZE){
208            dest = destStack;
209            destLen = func(src,u_strlen(src),dest,destLen+1,options | UIDNA_USE_STD3_RULES, &parseError, &status);
210            /* TODO : compare output with expected*/
211            if(U_SUCCESS(status) && (doCompare==TRUE) && u_strCaseCompare(dest,destLen, expected,expectedLen,0,&status)!=0){
212                log_err("Did not get the expected result for %s with source length and both options set.\n",testName);
213            }
214        }else{
215            log_err( "%s with source length  failed. Requires destCapacity > 300\n",testName);
216        }
217    }
218    /*testing query string*/
219    if(status != expectedStatus && expectedStatus != U_IDNA_UNASSIGNED_ERROR){
220        log_err( "Did not get the expected error for %s with source length and options set. Expected: %s Got: %s\n",testName, u_errorName(expectedStatus), u_errorName(status));
221    }
222    free(tSrc);
223}
224
225static const UChar unicodeIn[][41] ={
226    {
227        0x0644, 0x064A, 0x0647, 0x0645, 0x0627, 0x0628, 0x062A, 0x0643, 0x0644,
228        0x0645, 0x0648, 0x0634, 0x0639, 0x0631, 0x0628, 0x064A, 0x061F, 0x0000
229    },
230    {
231        0x4ED6, 0x4EEC, 0x4E3A, 0x4EC0, 0x4E48, 0x4E0D, 0x8BF4, 0x4E2D, 0x6587,
232        0x0000
233    },
234    {
235        0x0050, 0x0072, 0x006F, 0x010D, 0x0070, 0x0072, 0x006F, 0x0073, 0x0074,
236        0x011B, 0x006E, 0x0065, 0x006D, 0x006C, 0x0075, 0x0076, 0x00ED, 0x010D,
237        0x0065, 0x0073, 0x006B, 0x0079, 0x0000
238    },
239    {
240        0x05DC, 0x05DE, 0x05D4, 0x05D4, 0x05DD, 0x05E4, 0x05E9, 0x05D5, 0x05D8,
241        0x05DC, 0x05D0, 0x05DE, 0x05D3, 0x05D1, 0x05E8, 0x05D9, 0x05DD, 0x05E2,
242        0x05D1, 0x05E8, 0x05D9, 0x05EA, 0x0000
243    },
244    {
245        0x092F, 0x0939, 0x0932, 0x094B, 0x0917, 0x0939, 0x093F, 0x0928, 0x094D,
246        0x0926, 0x0940, 0x0915, 0x094D, 0x092F, 0x094B, 0x0902, 0x0928, 0x0939,
247        0x0940, 0x0902, 0x092C, 0x094B, 0x0932, 0x0938, 0x0915, 0x0924, 0x0947,
248        0x0939, 0x0948, 0x0902, 0x0000
249    },
250    {
251        0x306A, 0x305C, 0x307F, 0x3093, 0x306A, 0x65E5, 0x672C, 0x8A9E, 0x3092,
252        0x8A71, 0x3057, 0x3066, 0x304F, 0x308C, 0x306A, 0x3044, 0x306E, 0x304B,
253        0x0000
254    },
255/*
256    {
257        0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
258        0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
259        0xC5BC, 0xB9C8, 0xB098, 0xC88B, 0xC744, 0xAE4C, 0x0000
260    },
261*/
262    {
263        0x043F, 0x043E, 0x0447, 0x0435, 0x043C, 0x0443, 0x0436, 0x0435, 0x043E,
264        0x043D, 0x0438, 0x043D, 0x0435, 0x0433, 0x043E, 0x0432, 0x043E, 0x0440,
265        0x044F, 0x0442, 0x043F, 0x043E, 0x0440, 0x0443, 0x0441, 0x0441, 0x043A,
266        0x0438, 0x0000
267    },
268    {
269        0x0050, 0x006F, 0x0072, 0x0071, 0x0075, 0x00E9, 0x006E, 0x006F, 0x0070,
270        0x0075, 0x0065, 0x0064, 0x0065, 0x006E, 0x0073, 0x0069, 0x006D, 0x0070,
271        0x006C, 0x0065, 0x006D, 0x0065, 0x006E, 0x0074, 0x0065, 0x0068, 0x0061,
272        0x0062, 0x006C, 0x0061, 0x0072, 0x0065, 0x006E, 0x0045, 0x0073, 0x0070,
273        0x0061, 0x00F1, 0x006F, 0x006C, 0x0000
274    },
275    {
276        0x4ED6, 0x5011, 0x7232, 0x4EC0, 0x9EBD, 0x4E0D, 0x8AAA, 0x4E2D, 0x6587,
277        0x0000
278    },
279    {
280        0x0054, 0x1EA1, 0x0069, 0x0073, 0x0061, 0x006F, 0x0068, 0x1ECD, 0x006B,
281        0x0068, 0x00F4, 0x006E, 0x0067, 0x0074, 0x0068, 0x1EC3, 0x0063, 0x0068,
282        0x1EC9, 0x006E, 0x00F3, 0x0069, 0x0074, 0x0069, 0x1EBF, 0x006E, 0x0067,
283        0x0056, 0x0069, 0x1EC7, 0x0074, 0x0000
284    },
285    {
286        0x0033, 0x5E74, 0x0042, 0x7D44, 0x91D1, 0x516B, 0x5148, 0x751F, 0x0000
287    },
288    {
289        0x5B89, 0x5BA4, 0x5948, 0x7F8E, 0x6075, 0x002D, 0x0077, 0x0069, 0x0074,
290        0x0068, 0x002D, 0x0053, 0x0055, 0x0050, 0x0045, 0x0052, 0x002D, 0x004D,
291        0x004F, 0x004E, 0x004B, 0x0045, 0x0059, 0x0053, 0x0000
292    },
293    {
294        0x0048, 0x0065, 0x006C, 0x006C, 0x006F, 0x002D, 0x0041, 0x006E, 0x006F,
295        0x0074, 0x0068, 0x0065, 0x0072, 0x002D, 0x0057, 0x0061, 0x0079, 0x002D,
296        0x305D, 0x308C, 0x305E, 0x308C, 0x306E, 0x5834, 0x6240, 0x0000
297    },
298    {
299        0x3072, 0x3068, 0x3064, 0x5C4B, 0x6839, 0x306E, 0x4E0B, 0x0032, 0x0000
300    },
301    {
302        0x004D, 0x0061, 0x006A, 0x0069, 0x3067, 0x004B, 0x006F, 0x0069, 0x3059,
303        0x308B, 0x0035, 0x79D2, 0x524D, 0x0000
304    },
305    {
306        0x30D1, 0x30D5, 0x30A3, 0x30FC, 0x0064, 0x0065, 0x30EB, 0x30F3, 0x30D0,
307        0x0000
308    },
309    {
310        0x305D, 0x306E, 0x30B9, 0x30D4, 0x30FC, 0x30C9, 0x3067, 0x0000
311    },
312    /* test non-BMP code points */
313    {
314        0xD800, 0xDF00, 0xD800, 0xDF01, 0xD800, 0xDF02, 0xD800, 0xDF03, 0xD800, 0xDF05,
315        0xD800, 0xDF06, 0xD800, 0xDF07, 0xD800, 0xDF09, 0xD800, 0xDF0A, 0xD800, 0xDF0B,
316        0x0000
317    },
318    {
319        0xD800, 0xDF0D, 0xD800, 0xDF0C, 0xD800, 0xDF1E, 0xD800, 0xDF0F, 0xD800, 0xDF16,
320        0xD800, 0xDF15, 0xD800, 0xDF14, 0xD800, 0xDF12, 0xD800, 0xDF10, 0xD800, 0xDF20,
321        0xD800, 0xDF21,
322        0x0000
323    },
324    /* Greek  */
325    {
326        0x03b5, 0x03bb, 0x03bb, 0x03b7, 0x03bd, 0x03b9, 0x03ba, 0x03ac
327    },
328    /* Maltese */
329    {
330        0x0062, 0x006f, 0x006e, 0x0121, 0x0075, 0x0073, 0x0061, 0x0127,
331        0x0127, 0x0061
332    },
333    /* Russian */
334    {
335        0x043f, 0x043e, 0x0447, 0x0435, 0x043c, 0x0443, 0x0436, 0x0435,
336        0x043e, 0x043d, 0x0438, 0x043d, 0x0435, 0x0433, 0x043e, 0x0432,
337        0x043e, 0x0440, 0x044f, 0x0442, 0x043f, 0x043e, 0x0440, 0x0443,
338        0x0441, 0x0441, 0x043a, 0x0438
339    },
340    {
341        0x0054,0x0045,0x0053,0x0054
342    }
343};
344
345static const char * const asciiIn[] = {
346    "xn--egbpdaj6bu4bxfgehfvwxn",
347    "xn--ihqwcrb4cv8a8dqg056pqjye",
348    "xn--Proprostnemluvesky-uyb24dma41a",
349    "xn--4dbcagdahymbxekheh6e0a7fei0b",
350    "xn--i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd",
351    "xn--n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa",
352/*  "xn--989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879ccm6fea98c",*/
353    "xn--b1abfaaepdrnnbgefbaDotcwatmq2g4l",
354    "xn--PorqunopuedensimplementehablarenEspaol-fmd56a",
355    "xn--ihqwctvzc91f659drss3x8bo0yb",
356    "xn--TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g",
357    "xn--3B-ww4c5e180e575a65lsy2b",
358    "xn---with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n",
359    "xn--Hello-Another-Way--fc4qua05auwb3674vfr0b",
360    "xn--2-u9tlzr9756bt3uc0v",
361    "xn--MajiKoi5-783gue6qz075azm5e",
362    "xn--de-jg4avhby1noc0d",
363    "xn--d9juau41awczczp",
364    "XN--097CCDEKGHQJK",
365    "XN--db8CBHEJLGH4E0AL",
366    "xn--hxargifdar",                       /* Greek */
367    "xn--bonusaa-5bb1da",                   /* Maltese */
368    "xn--b1abfaaepdrnnbgefbadotcwatmq2g4l",  /* Russian (Cyrillic)*/
369    "TEST"
370
371};
372
373static const char * const domainNames[] = {
374    "slip129-37-118-146.nc.us.ibm.net",
375    "saratoga.pe.utexas.edu",
376    "dial-120-45.ots.utexas.edu",
377    "woo-085.dorms.waller.net",
378    "hd30-049.hil.compuserve.com",
379    "pem203-31.pe.ttu.edu",
380    "56K-227.MaxTNT3.pdq.net",
381    "dial-36-2.ots.utexas.edu",
382    "slip129-37-23-152.ga.us.ibm.net",
383    "ts45ip119.cadvision.com",
384    "sdn-ts-004txaustP05.dialsprint.net",
385    "bar-tnt1s66.erols.com",
386    "101.st-louis-15.mo.dial-access.att.net",
387    "h92-245.Arco.COM",
388    "dial-13-2.ots.utexas.edu",
389    "net-redynet29.datamarkets.com.ar",
390    "ccs-shiva28.reacciun.net.ve",
391    "7.houston-11.tx.dial-access.att.net",
392    "ingw129-37-120-26.mo.us.ibm.net",
393    "dialup6.austintx.com",
394    "dns2.tpao.gov.tr",
395    "slip129-37-119-194.nc.us.ibm.net",
396    "cs7.dillons.co.uk.203.119.193.in-addr.arpa",
397    "swprd1.innovplace.saskatoon.sk.ca",
398    "bikini.bologna.maraut.it",
399    "node91.subnet159-198-79.baxter.com",
400    "cust19.max5.new-york.ny.ms.uu.net",
401    "balexander.slip.andrew.cmu.edu",
402    "pool029.max2.denver.co.dynip.alter.net",
403    "cust49.max9.new-york.ny.ms.uu.net",
404    "s61.abq-dialin2.hollyberry.com",
405    "\\u0917\\u0928\\u0947\\u0936.sanjose.ibm.com", /*':'(0x003a) produces U_IDNA_STD3_ASCII_RULES_ERROR*/
406    "www.xn--vea.com",
407    /* "www.\\u00E0\\u00B3\\u00AF.com",//' ' (0x0020) produces U_IDNA_STD3_ASCII_RULES_ERROR*/
408    "www.\\u00C2\\u00A4.com",
409    "www.\\u00C2\\u00A3.com",
410    /* "\\u0025", //'%' (0x0025) produces U_IDNA_STD3_ASCII_RULES_ERROR*/
411    /* "\\u005C\\u005C", //'\' (0x005C) produces U_IDNA_STD3_ASCII_RULES_ERROR*/
412    /*"@",*/
413    /*"\\u002F",*/
414    /*"www.\\u0021.com",*/
415    /*"www.\\u0024.com",*/
416    /*"\\u003f",*/
417    /* These yeild U_IDNA_PROHIBITED_ERROR*/
418    /*"\\u00CF\\u0082.com",*/
419    /*"\\u00CE\\u00B2\\u00C3\\u009Fss.com",*/
420    /*"\\u00E2\\u0098\\u00BA.com",*/
421    "\\u00C3\\u00BC.com"
422
423};
424
425static void
426TestToASCII(){
427
428    int32_t i;
429    UChar buf[MAX_DEST_SIZE];
430    const char* testName = "uidna_toASCII";
431    TestFunc func = uidna_toASCII;
432    for(i=0;i< (int32_t)(sizeof(unicodeIn)/sizeof(unicodeIn[0])); i++){
433        u_charsToUChars(asciiIn[i],buf, (int32_t)strlen(asciiIn[i])+1);
434        testAPI(unicodeIn[i], buf,testName, FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
435
436    }
437}
438
439static void
440TestToUnicode(){
441
442    int32_t i;
443    UChar buf[MAX_DEST_SIZE];
444    const char* testName = "uidna_toUnicode";
445    TestFunc func = uidna_toUnicode;
446    for(i=0;i< (int32_t)(sizeof(asciiIn)/sizeof(asciiIn[0])); i++){
447        u_charsToUChars(asciiIn[i],buf, (int32_t)strlen(asciiIn[i])+1);
448        testAPI(buf,unicodeIn[i],testName,FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
449    }
450}
451
452
453static void
454TestIDNToUnicode(){
455    int32_t i;
456    UChar buf[MAX_DEST_SIZE];
457    UChar expected[MAX_DEST_SIZE];
458    UErrorCode status = U_ZERO_ERROR;
459    int32_t bufLen = 0;
460    UParseError parseError;
461    const char* testName="uidna_IDNToUnicode";
462    TestFunc func = uidna_IDNToUnicode;
463    for(i=0;i< (int32_t)(sizeof(domainNames)/sizeof(domainNames[0])); i++){
464        bufLen = (int32_t)strlen(domainNames[i]);
465        bufLen = u_unescape(domainNames[i],buf, bufLen+1);
466        func(buf,bufLen,expected,MAX_DEST_SIZE, UIDNA_ALLOW_UNASSIGNED, &parseError,&status);
467        if(U_FAILURE(status)){
468            log_err_status(status,  "%s failed to convert domainNames[%i].Error: %s \n",testName, i, u_errorName(status));
469            break;
470        }
471        testAPI(buf,expected,testName,FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
472         /*test toUnicode with all labels in the string*/
473        testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
474        if(U_FAILURE(status)){
475            log_err( "%s failed to convert domainNames[%i].Error: %s \n",testName,i, u_errorName(status));
476            break;
477        }
478    }
479
480}
481
482static void
483TestIDNToASCII(){
484    int32_t i;
485    UChar buf[MAX_DEST_SIZE];
486    UChar expected[MAX_DEST_SIZE];
487    UErrorCode status = U_ZERO_ERROR;
488    int32_t bufLen = 0;
489    UParseError parseError;
490    const char* testName="udina_IDNToASCII";
491    TestFunc func=uidna_IDNToASCII;
492
493    for(i=0;i< (int32_t)(sizeof(domainNames)/sizeof(domainNames[0])); i++){
494        bufLen = (int32_t)strlen(domainNames[i]);
495        bufLen = u_unescape(domainNames[i],buf, bufLen+1);
496        func(buf,bufLen,expected,MAX_DEST_SIZE, UIDNA_ALLOW_UNASSIGNED, &parseError,&status);
497        if(U_FAILURE(status)){
498            log_err_status(status,  "%s failed to convert domainNames[%i].Error: %s \n",testName,i, u_errorName(status));
499            break;
500        }
501        testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, TRUE, TRUE, func);
502        /*test toASCII with all labels in the string*/
503        testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, FALSE, TRUE, func);
504        if(U_FAILURE(status)){
505            log_err( "%s failed to convert domainNames[%i].Error: %s \n",testName,i, u_errorName(status));
506            break;
507        }
508    }
509
510
511}
512
513
514static void
515testCompareWithSrc(const UChar* s1, int32_t s1Len,
516            const UChar* s2, int32_t s2Len,
517            const char* testName, CompareFunc func,
518            UBool isEqual){
519
520    UErrorCode status = U_ZERO_ERROR;
521    int32_t retVal = func(s1,-1,s2,-1,UIDNA_DEFAULT,&status);
522
523    if(isEqual==TRUE &&  retVal !=0){
524        log_err("Did not get the expected result for %s with null termniated strings.\n",testName);
525    }
526    if(U_FAILURE(status)){
527        log_err_status(status, "%s null terminated source failed. Error: %s\n", testName,u_errorName(status));
528    }
529
530    status = U_ZERO_ERROR;
531    retVal = func(s1,-1,s2,-1,UIDNA_ALLOW_UNASSIGNED,&status);
532
533    if(isEqual==TRUE &&  retVal !=0){
534        log_err("Did not get the expected result for %s with null termniated strings with options set.\n", testName);
535    }
536    if(U_FAILURE(status)){
537        log_err_status(status, "%s null terminated source and options set failed. Error: %s\n",testName, u_errorName(status));
538    }
539
540    status = U_ZERO_ERROR;
541    retVal = func(s1,s1Len,s2,s2Len,UIDNA_DEFAULT,&status);
542
543    if(isEqual==TRUE &&  retVal !=0){
544        log_err("Did not get the expected result for %s with string length.\n",testName);
545    }
546    if(U_FAILURE(status)){
547        log_err_status(status,  "%s with string length. Error: %s\n",testName, u_errorName(status));
548    }
549
550    status = U_ZERO_ERROR;
551    retVal = func(s1,s1Len,s2,s2Len,UIDNA_ALLOW_UNASSIGNED,&status);
552
553    if(isEqual==TRUE &&  retVal !=0){
554        log_err("Did not get the expected result for %s with string length and options set.\n",testName);
555    }
556    if(U_FAILURE(status)){
557        log_err_status(status,  "%s with string length and options set. Error: %s\n", u_errorName(status), testName);
558    }
559}
560
561
562static void
563TestCompare(){
564    int32_t i;
565
566    const char* testName ="uidna_compare";
567    CompareFunc func = uidna_compare;
568
569    UChar www[] = {0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
570    UChar com[] = {0x002E, 0x0043, 0x004F, 0x004D, 0x0000};
571    UChar buf[MAX_DEST_SIZE]={0x0057, 0x0057, 0x0057, 0x002E, 0x0000};
572    UChar source[MAX_DEST_SIZE]={0},
573          uni0[MAX_DEST_SIZE]={0},
574          uni1[MAX_DEST_SIZE]={0},
575          ascii0[MAX_DEST_SIZE]={0},
576          ascii1[MAX_DEST_SIZE]={0},
577          temp[MAX_DEST_SIZE] ={0};
578
579
580    u_strcat(uni0,unicodeIn[0]);
581    u_strcat(uni0,com);
582
583    u_strcat(uni1,unicodeIn[1]);
584    u_strcat(uni1,com);
585
586    u_charsToUChars(asciiIn[0], temp, (int32_t)strlen(asciiIn[0]));
587    u_strcat(ascii0,temp);
588    u_strcat(ascii0,com);
589
590    memset(temp, 0, U_SIZEOF_UCHAR * MAX_DEST_SIZE);
591
592    u_charsToUChars(asciiIn[1], temp, (int32_t)strlen(asciiIn[1]));
593    u_strcat(ascii1,temp);
594    u_strcat(ascii1,com);
595
596    /* prepend www. */
597    u_strcat(source, www);
598
599    for(i=0;i< (int32_t)(sizeof(unicodeIn)/sizeof(unicodeIn[0])); i++){
600        UChar* src;
601        int32_t srcLen;
602
603        memset(buf+4, 0, (MAX_DEST_SIZE-4) * U_SIZEOF_UCHAR);
604
605        u_charsToUChars(asciiIn[i],buf+4, (int32_t)strlen(asciiIn[i]));
606        u_strcat(buf,com);
607
608
609        /* for every entry in unicodeIn array
610           prepend www. and append .com*/
611        source[4]=0;
612        u_strncat(source,unicodeIn[i], u_strlen(unicodeIn[i]));
613        u_strcat(source,com);
614
615        /* a) compare it with itself*/
616        src = source;
617        srcLen = u_strlen(src);
618
619        testCompareWithSrc(src,srcLen,src,srcLen,testName, func, TRUE);
620
621        /* b) compare it with asciiIn equivalent */
622        testCompareWithSrc(src,srcLen,buf,u_strlen(buf),testName, func,TRUE);
623
624        /* c) compare it with unicodeIn not equivalent*/
625        if(i==0){
626            testCompareWithSrc(src,srcLen,uni1,u_strlen(uni1),testName, func,FALSE);
627        }else{
628            testCompareWithSrc(src,srcLen,uni0,u_strlen(uni0),testName, func,FALSE);
629        }
630        /* d) compare it with asciiIn not equivalent */
631        if(i==0){
632            testCompareWithSrc(src,srcLen,ascii1,u_strlen(ascii1),testName, func,FALSE);
633        }else{
634            testCompareWithSrc(src,srcLen,ascii0,u_strlen(ascii0),testName, func,FALSE);
635        }
636
637    }
638}
639
640static void TestJB4490(){
641    static const UChar data[][50]= {
642        {0x00F5,0x00dE,0x00dF,0x00dD, 0x0000},
643        {0xFB00,0xFB01}
644    };
645    UChar output1[40] = {0};
646    UChar output2[40] = {0};
647    int32_t i;
648    for(i=0; i< sizeof(data)/sizeof(data[0]); i++){
649        const UChar* src1 = data[i];
650        int32_t src1Len = u_strlen(src1);
651        UChar* dest1 = output1;
652        int32_t dest1Len = 40;
653        UErrorCode status = U_ZERO_ERROR;
654        UParseError ps;
655        UChar* src2 = NULL;
656        int32_t src2Len = 0;
657        UChar* dest2 = output2;
658        int32_t dest2Len = 40;
659        dest1Len = uidna_toASCII(src1, src1Len, dest1, dest1Len,UIDNA_DEFAULT, &ps, &status);
660        if(U_FAILURE(status)){
661            log_err_status(status, "uidna_toUnicode failed with error %s.\n", u_errorName(status));
662        }
663        src2 = dest1;
664        src2Len = dest1Len;
665        dest2Len = uidna_toUnicode(src2, src2Len, dest2, dest2Len, UIDNA_DEFAULT, &ps, &status);
666        if(U_FAILURE(status)){
667            log_err_status(status, "uidna_toUnicode failed with error %s.\n", u_errorName(status));
668        }
669    }
670}
671
672static void TestJB4475(){
673
674    static const UChar input[][10] = {
675        {0x0054,0x0045,0x0053,0x0054,0x0000},/* TEST */
676        {0x0074,0x0065,0x0073,0x0074,0x0000} /* test */
677    };
678    int i;
679    UChar output[40] = {0};
680    for(i=0; i< sizeof(input)/sizeof(input[0]); i++){
681        const UChar* src = input[i];
682        int32_t srcLen = u_strlen(src);
683        UChar* dest = output;
684        int32_t destLen = 40;
685        UErrorCode status = U_ZERO_ERROR;
686        UParseError ps;
687
688        destLen = uidna_toASCII(src, srcLen, dest, destLen,UIDNA_DEFAULT, &ps, &status);
689        if(U_FAILURE(status)){
690            log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status));
691            continue;
692        }
693        if(u_strncmp(input[i], dest, srcLen)!=0){
694            log_err("uidna_toASCII did not return the expected output.\n");
695        }
696    }
697}
698
699static void TestLength(){
700    {
701        static const char* cl = "my_very_very_very_very_very_very_very_very_very_very_very_very_very_long_and_incredibly_uncreative_domain_label";
702        UChar ul[128] = {'\0'};
703        UChar dest[256] = {'\0'};
704        /* this unicode string is longer than MAX_LABEL_BUFFER_SIZE and produces an
705           IDNA prepared string (including xn--)that is exactly 63 bytes long */
706        UChar ul1[] = { 0xC138, 0xACC4, 0xC758, 0xBAA8, 0xB4E0, 0xC0AC, 0xB78C, 0xB4E4, 0xC774,
707                        0xD55C, 0xAD6D, 0xC5B4, 0xB97C, 0xC774, 0x00AD, 0x034F, 0x1806, 0x180B,
708                        0x180C, 0x180D, 0x200B, 0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02,
709                        0xFE03, 0xFE04, 0xFE05, 0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B,
710                        0xFE0C, 0xFE0D, 0xFE0E, 0xFE0F, 0xFEFF, 0xD574, 0xD55C, 0xB2E4, 0xBA74,
711                        0xC138, 0x0041, 0x00AD, 0x034F, 0x1806, 0x180B, 0x180C, 0x180D, 0x200B,
712                        0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05,
713                        0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E,
714                        0xFE0F, 0xFEFF, 0x00AD, 0x034F, 0x1806, 0x180B, 0x180C, 0x180D, 0x200B,
715                        0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05,
716                        0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E,
717                        0xFE0F, 0xFEFF, 0x00AD, 0x034F, 0x1806, 0x180B, 0x180C, 0x180D, 0x200B,
718                        0x200C, 0x200D, 0x2060, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05,
719                        0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E,
720                        0xFE0F, 0xFEFF, 0x0000
721                      };
722
723        int32_t len1 = LENGTHOF(ul1)-1/*remove the null termination*/;
724        int32_t destLen = LENGTHOF(dest);
725        UErrorCode status = U_ZERO_ERROR;
726        UParseError ps;
727        int32_t len = (int32_t)strlen(cl);
728        u_charsToUChars(cl, ul, len+1);
729        destLen = uidna_toUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
730        if(status != U_ZERO_ERROR){
731            log_err_status(status, "uidna_toUnicode failed with error %s.\n", u_errorName(status));
732        }
733
734        status = U_ZERO_ERROR;
735        destLen = LENGTHOF(dest);
736        len = -1;
737        destLen = uidna_toUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
738        if(status != U_ZERO_ERROR){
739            log_err_status(status, "uidna_toUnicode failed with error %s.\n", u_errorName(status));
740        }
741        status = U_ZERO_ERROR;
742        destLen = LENGTHOF(dest);
743        len = (int32_t)strlen(cl);
744        destLen = uidna_toASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
745        if(status != U_IDNA_LABEL_TOO_LONG_ERROR){
746            log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status));
747        }
748
749        status = U_ZERO_ERROR;
750        destLen = LENGTHOF(dest);
751        len = -1;
752        destLen = uidna_toASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
753        if(status != U_IDNA_LABEL_TOO_LONG_ERROR){
754            log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status));
755        }
756
757        status = U_ZERO_ERROR;
758        destLen = LENGTHOF(dest);
759        destLen = uidna_toASCII(ul1, len1, dest, destLen, UIDNA_DEFAULT, &ps, &status);
760        if(status != U_ZERO_ERROR){
761            log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status));
762        }
763
764        status = U_ZERO_ERROR;
765        destLen = LENGTHOF(dest);
766        len1 = -1;
767        destLen = uidna_toASCII(ul1, len1, dest, destLen, UIDNA_DEFAULT, &ps, &status);
768        if(status != U_ZERO_ERROR){
769            log_err_status(status, "uidna_toASCII failed with error %s.\n", u_errorName(status));
770        }
771    }
772    {
773        static const char* cl = "my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.ibm.com";
774        UChar ul[400] = {'\0'};
775        UChar dest[400] = {'\0'};
776        int32_t destLen = LENGTHOF(dest);
777        UErrorCode status = U_ZERO_ERROR;
778        UParseError ps;
779        int32_t len = (int32_t)strlen(cl);
780        u_charsToUChars(cl, ul, len+1);
781
782        destLen = uidna_IDNToUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
783        if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
784            log_err_status(status, "uidna_IDNToUnicode failed with error %s.\n", u_errorName(status));
785        }
786
787        status = U_ZERO_ERROR;
788        destLen = LENGTHOF(dest);
789        len = -1;
790        destLen = uidna_IDNToUnicode(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
791        if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
792            log_err_status(status, "uidna_IDNToUnicode failed with error %s.\n", u_errorName(status));
793        }
794
795        status = U_ZERO_ERROR;
796        destLen = LENGTHOF(dest);
797        len = (int32_t)strlen(cl);
798        destLen = uidna_IDNToASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
799        if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
800            log_err_status(status, "uidna_IDNToASCII failed with error %s.\n", u_errorName(status));
801        }
802
803        status = U_ZERO_ERROR;
804        destLen = LENGTHOF(dest);
805        len = -1;
806        destLen = uidna_IDNToASCII(ul, len, dest, destLen, UIDNA_DEFAULT, &ps, &status);
807        if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
808            log_err_status(status, "uidna_IDNToASCII failed with error %s.\n", u_errorName(status));
809        }
810
811        status = U_ZERO_ERROR;
812        uidna_compare(ul, len, ul, len, UIDNA_DEFAULT, &status);
813        if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
814            log_err_status(status, "uidna_compare failed with error %s.\n", u_errorName(status));
815        }
816        uidna_compare(ul, -1, ul, -1, UIDNA_DEFAULT, &status);
817        if(status != U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR){
818            log_err_status(status, "uidna_compare failed with error %s.\n", u_errorName(status));
819        }
820    }
821}
822static void TestJB5273(){
823    static const char INVALID_DOMAIN_NAME[] = "xn--m\\u00FCller.de";
824    UChar invalid_idn[25] = {'\0'};
825    int32_t len = u_unescape(INVALID_DOMAIN_NAME, invalid_idn, strlen(INVALID_DOMAIN_NAME));
826    UChar output[50] = {'\0'};
827    UErrorCode status = U_ZERO_ERROR;
828    UParseError prsError;
829    int32_t outLen = uidna_toUnicode(invalid_idn, len, output, 50, UIDNA_DEFAULT, &prsError, &status);
830    if(U_FAILURE(status)){
831        log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status));
832    }
833    status = U_ZERO_ERROR;
834    outLen = uidna_toUnicode(invalid_idn, len, output, 50, UIDNA_USE_STD3_RULES, &prsError, &status);
835    if(U_FAILURE(status)){
836        log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status));
837    }
838
839    status = U_ZERO_ERROR;
840    outLen = uidna_IDNToUnicode(invalid_idn, len, output, 50, UIDNA_DEFAULT, &prsError, &status);
841    if(U_FAILURE(status)){
842        log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status));
843    }
844    status = U_ZERO_ERROR;
845    outLen = uidna_IDNToUnicode(invalid_idn, len, output, 50, UIDNA_USE_STD3_RULES, &prsError, &status);
846    if(U_FAILURE(status)){
847        log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status));
848    }
849}
850#endif
851
852/*
853 * Hey, Emacs, please set the following:
854 *
855 * Local Variables:
856 * indent-tabs-mode: nil
857 * End:
858 *
859 */
860
861