1/*
2 *******************************************************************************
3 * Copyright (C) 2003-2011, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6*/
7package com.ibm.icu.dev.test.stringprep;
8
9import java.util.Random;
10
11import com.ibm.icu.dev.test.TestFmwk;
12import com.ibm.icu.impl.Utility;
13import com.ibm.icu.text.IDNA;
14import com.ibm.icu.text.StringPrep;
15import com.ibm.icu.text.StringPrepParseException;
16import com.ibm.icu.text.UCharacterIterator;
17import com.ibm.icu.text.UTF16;
18
19/**
20 * @author ram
21 */
22public class TestIDNA extends TestFmwk {
23    public static void main(String[] args) throws Exception {
24        new TestIDNA().run(args);
25    }
26    private StringPrepParseException unassignedException = new StringPrepParseException("",StringPrepParseException.UNASSIGNED_ERROR);
27    public void TestToUnicode() throws Exception{
28        for(int i=0; i<TestData.asciiIn.length; i++){
29            // test StringBuffer toUnicode
30            doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.DEFAULT, null);
31            doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.ALLOW_UNASSIGNED, null);
32            doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.USE_STD3_RULES, null);
33            doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.USE_STD3_RULES|IDNA.ALLOW_UNASSIGNED, null);
34
35        }
36    }
37
38    public void TestToASCII() throws Exception{
39        for(int i=0; i<TestData.asciiIn.length; i++){
40            // test StringBuffer toUnicode
41            doTestToASCII(new String(TestData.unicodeIn[i]),TestData.asciiIn[i],IDNA.DEFAULT, null);
42            doTestToASCII(new String(TestData.unicodeIn[i]),TestData.asciiIn[i],IDNA.ALLOW_UNASSIGNED, null);
43            doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.USE_STD3_RULES, null);
44            doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.USE_STD3_RULES|IDNA.ALLOW_UNASSIGNED, null);
45
46        }
47    }
48
49    public void TestIDNToASCII() throws Exception{
50        for(int i=0; i<TestData.domainNames.length; i++){
51            doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNA.DEFAULT, null);
52            doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNA.ALLOW_UNASSIGNED, null);
53            doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNA.USE_STD3_RULES, null);
54            doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNA.ALLOW_UNASSIGNED|IDNA.USE_STD3_RULES, null);
55        }
56
57        for(int i=0; i<TestData.domainNames1Uni.length; i++){
58            doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNA.DEFAULT, null);
59            doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNA.ALLOW_UNASSIGNED, null);
60        }
61    }
62    public void TestIDNToUnicode() throws Exception{
63        for(int i=0; i<TestData.domainNames.length; i++){
64            doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNA.DEFAULT, null);
65            doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNA.ALLOW_UNASSIGNED, null);
66            doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNA.USE_STD3_RULES, null);
67            doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNA.ALLOW_UNASSIGNED|IDNA.USE_STD3_RULES, null);
68        }
69        for(int i=0; i<TestData.domainNamesToASCIIOut.length; i++){
70            doTestIDNToUnicode(TestData.domainNamesToASCIIOut[i],TestData.domainNamesToUnicodeOut[i],IDNA.DEFAULT, null);
71            doTestIDNToUnicode(TestData.domainNamesToASCIIOut[i],TestData.domainNamesToUnicodeOut[i],IDNA.ALLOW_UNASSIGNED, null);
72        }
73    }
74
75    private void doTestToUnicode(String src, String expected, int options, Object expectedException)
76                throws Exception{
77        StringBuffer inBuf = new StringBuffer(src);
78        UCharacterIterator inIter = UCharacterIterator.getInstance(src);
79        try{
80
81            StringBuffer out = IDNA.convertToUnicode(src,options);
82            if(expected!=null && out != null && !out.toString().equals(expected)){
83                errln("convertToUnicode did not return expected result with options : "+ options +
84                      " Expected: " + prettify(expected)+" Got: "+prettify(out));
85            }
86            if(expectedException!=null && !unassignedException.equals(expectedException)){
87                errln("convertToUnicode did not get the expected exception. The operation succeeded!");
88            }
89        }catch(StringPrepParseException ex){
90            if(expectedException == null || !ex.equals(expectedException)){
91                errln("convertToUnicode did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());
92            }
93        }
94        try{
95
96            StringBuffer out = IDNA.convertToUnicode(inBuf,options);
97            if(expected!=null && out != null && !out.toString().equals(expected)){
98               errln("convertToUnicode did not return expected result with options : "+ options +
99                     " Expected: " + prettify(expected)+" Got: "+out);
100            }
101            if(expectedException!=null && !unassignedException.equals(expectedException)){
102                errln("convertToUnicode did not get the expected exception. The operation succeeded!");
103            }
104        }catch(StringPrepParseException ex){
105            if(expectedException == null || !ex.equals(expectedException)){
106                errln("convertToUnicode did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());
107            }
108        }
109
110        try{
111            StringBuffer out = IDNA.convertToUnicode(inIter,options);
112            if(expected!=null && out != null && !out.toString().equals(expected)){
113               errln("convertToUnicode did not return expected result with options : "+ options +
114                     " Expected: " + prettify(expected)+" Got: "+prettify(out));
115            }
116            if(expectedException!=null && !unassignedException.equals(expectedException)){
117                errln("Did not get the expected exception. The operation succeeded!");
118            }
119        }catch(StringPrepParseException ex){
120            if(expectedException == null || !ex.equals(expectedException)){
121                errln("Did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());
122            }
123        }
124    }
125
126    private void doTestIDNToUnicode(String src, String expected, int options, Object expectedException)
127                throws Exception{
128        StringBuffer inBuf = new StringBuffer(src);
129        UCharacterIterator inIter = UCharacterIterator.getInstance(src);
130        try{
131
132            StringBuffer out = IDNA.convertIDNToUnicode(src,options);
133            if(expected!=null && out != null && !out.toString().equals(expected)){
134                errln("convertToUnicode did not return expected result with options : "+ options +
135                      " Expected: " + prettify(expected)+" Got: "+prettify(out));
136            }
137            if(expectedException!=null && !unassignedException.equals(expectedException)){
138                errln("convertToUnicode did not get the expected exception. The operation succeeded!");
139            }
140        }catch(StringPrepParseException ex){
141            if(expectedException == null || !expectedException.equals(ex)){
142                errln("convertToUnicode did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
143            }
144        }
145        try{
146            StringBuffer out = IDNA.convertIDNToUnicode(inBuf,options);
147            if(expected!=null && out != null && !out.toString().equals(expected)){
148               errln("convertToUnicode did not return expected result with options : "+ options +
149                     " Expected: " + prettify(expected)+" Got: "+out);
150            }
151            if(expectedException!=null && !unassignedException.equals(expectedException)){
152                errln("convertToUnicode did not get the expected exception. The operation succeeded!");
153            }
154        }catch(StringPrepParseException ex){
155            if(expectedException == null || !expectedException.equals(ex)){
156                errln("convertToUnicode did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
157            }
158        }
159
160        try{
161            StringBuffer out = IDNA.convertIDNToUnicode(inIter,options);
162            if(expected!=null && out != null && !out.toString().equals(expected)){
163               errln("convertToUnicode did not return expected result with options : "+ options +
164                     " Expected: " + prettify(expected)+" Got: "+prettify(out));
165            }
166            if(expectedException!=null && !unassignedException.equals(expectedException)){
167                errln("Did not get the expected exception. The operation succeeded!");
168            }
169        }catch(StringPrepParseException ex){
170            if(expectedException == null || !expectedException.equals(ex)){
171                errln("Did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
172            }
173        }
174    }
175    private void doTestToASCII(String src, String expected, int options, Object expectedException)
176                throws Exception{
177        StringBuffer inBuf = new StringBuffer(src);
178        UCharacterIterator inIter = UCharacterIterator.getInstance(src);
179        try{
180
181            StringBuffer out = IDNA.convertToASCII(src,options);
182            if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){
183                errln("convertToASCII did not return expected result with options : "+ options +
184                      " Expected: " + expected+" Got: "+out);
185            }
186            if(expectedException!=null && !unassignedException.equals(expectedException)){
187                errln("convertToASCII did not get the expected exception. The operation succeeded!");
188            }
189        }catch(StringPrepParseException ex){
190            if(expectedException == null || !expectedException.equals(ex)){
191                errln("convertToASCII did not get the expected exception for source: " +src +"\n Got:  "+ ex.toString() +"\n Expected: " +ex.toString());
192            }
193        }
194
195        try{
196            StringBuffer out = IDNA.convertToASCII(inBuf,options);
197            if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){
198               errln("convertToASCII did not return expected result with options : "+ options +
199                     " Expected: " + expected+" Got: "+out);
200            }
201            if(expectedException!=null && !unassignedException.equals(expectedException)){
202                errln("convertToASCII did not get the expected exception. The operation succeeded!");
203            }
204        }catch(StringPrepParseException ex){
205            if(expectedException == null || !expectedException.equals(ex)){
206                errln("convertToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
207            }
208        }
209
210        try{
211            StringBuffer out = IDNA.convertToASCII(inIter,options);
212            if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){
213               errln("convertToASCII did not return expected result with options : "+ options +
214                     " Expected: " + expected+" Got: "+ out);
215            }
216            if(expectedException!=null && !unassignedException.equals(expectedException)){
217                errln("convertToASCII did not get the expected exception. The operation succeeded!");
218            }
219        }catch(StringPrepParseException ex){
220            if(expectedException == null || !expectedException.equals(ex)){
221                errln("convertToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
222            }
223        }
224    }
225    private void doTestIDNToASCII(String src, String expected, int options, Object expectedException)
226                throws Exception{
227        StringBuffer inBuf = new StringBuffer(src);
228        UCharacterIterator inIter = UCharacterIterator.getInstance(src);
229        try{
230
231            StringBuffer out = IDNA.convertIDNToASCII(src,options);
232            if(expected!=null && out != null && !out.toString().equals(expected)){
233                errln("convertToIDNASCII did not return expected result with options : "+ options +
234                      " Expected: " + expected+" Got: "+out);
235            }
236            if(expectedException!=null && !unassignedException.equals(expectedException)){
237                errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
238            }
239        }catch(StringPrepParseException ex){
240            if(expectedException == null || !ex.equals(expectedException)){
241                errln("convertToIDNASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
242            }
243        }
244        try{
245            StringBuffer out = IDNA.convertIDNToASCII(inBuf,options);
246            if(expected!=null && out != null && !out.toString().equals(expected)){
247               errln("convertToIDNASCII did not return expected result with options : "+ options +
248                     " Expected: " + expected+" Got: "+out);
249            }
250            if(expectedException!=null && !unassignedException.equals(expectedException)){
251                errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
252            }
253        }catch(StringPrepParseException ex){
254            if(expectedException == null || !ex.equals(expectedException)){
255                errln("convertToIDNASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
256            }
257        }
258
259        try{
260            StringBuffer out = IDNA.convertIDNToASCII(inIter,options);
261            if(expected!=null && out != null && !out.toString().equals(expected)){
262               errln("convertIDNToASCII did not return expected result with options : "+ options +
263                     " Expected: " + expected+" Got: "+ out);
264            }
265
266            if(expectedException!=null && !unassignedException.equals(expectedException)){
267                errln("convertIDNToASCII did not get the expected exception. The operation succeeded!");
268            }
269        }catch(StringPrepParseException ex){
270            if(expectedException == null || !ex.equals(expectedException)){
271                errln("convertIDNToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
272            }
273        }
274    }
275    public void TestConformance()throws Exception{
276        for(int i=0; i<TestData.conformanceTestCases.length;i++){
277
278            TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
279            if(testCase.expected != null){
280                //Test toASCII
281                doTestToASCII(testCase.input,testCase.output,IDNA.DEFAULT,testCase.expected);
282                doTestToASCII(testCase.input,testCase.output,IDNA.ALLOW_UNASSIGNED,testCase.expected);
283            }
284            //Test toUnicode
285            //doTestToUnicode(testCase.input,testCase.output,IDNA.DEFAULT,testCase.expected);
286        }
287    }
288    public void TestNamePrepConformance() throws Exception{
289        StringPrep namePrep = StringPrep.getInstance(StringPrep.RFC3491_NAMEPREP);
290        for(int i=0; i<TestData.conformanceTestCases.length;i++){
291            TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
292            UCharacterIterator iter = UCharacterIterator.getInstance(testCase.input);
293            try{
294                StringBuffer output = namePrep.prepare(iter,StringPrep.DEFAULT);
295                if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){
296                    errln("Did not get the expected output. Expected: " + prettify(testCase.output)+
297                          " Got: "+ prettify(output) );
298                }
299                if(testCase.expected!=null && !unassignedException.equals(testCase.expected)){
300                    errln("Did not get the expected exception. The operation succeeded!");
301                }
302            }catch(StringPrepParseException ex){
303                if(testCase.expected == null || !ex.equals(testCase.expected)){
304                    errln("Did not get the expected exception for source: " +testCase.input +" Got:  "+ ex.toString());
305                }
306            }
307
308            try{
309                iter.setToStart();
310                StringBuffer output = namePrep.prepare(iter,StringPrep.ALLOW_UNASSIGNED);
311                if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){
312                    errln("Did not get the expected output. Expected: " + prettify(testCase.output)+
313                          " Got: "+ prettify(output) );
314                }
315                if(testCase.expected!=null && !unassignedException.equals(testCase.expected)){
316                    errln("Did not get the expected exception. The operation succeeded!");
317                }
318            }catch(StringPrepParseException ex){
319                if(testCase.expected == null || !ex.equals(testCase.expected)){
320                    errln("Did not get the expected exception for source: " +testCase.input +" Got:  "+ ex.toString());
321                }
322            }
323        }
324
325    }
326    public void TestErrorCases() throws Exception{
327        for(int i=0; i < TestData.errorCases.length; i++){
328            TestData.ErrorCase errCase = TestData.errorCases[i];
329            if(errCase.testLabel==true){
330                // Test ToASCII
331                doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNA.DEFAULT,errCase.expected);
332                doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNA.ALLOW_UNASSIGNED,errCase.expected);
333                if(errCase.useSTD3ASCIIRules){
334                    doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNA.USE_STD3_RULES,errCase.expected);
335                }
336            }
337            if(errCase.useSTD3ASCIIRules!=true){
338
339                // Test IDNToASCII
340                doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNA.DEFAULT,errCase.expected);
341                doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNA.ALLOW_UNASSIGNED,errCase.expected);
342
343            }else{
344                doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNA.USE_STD3_RULES,errCase.expected);
345            }
346            //TestToUnicode
347            if(errCase.testToUnicode==true){
348                if(errCase.useSTD3ASCIIRules!=true){
349                    // Test IDNToUnicode
350                    doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNA.DEFAULT,errCase.expected);
351                    doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNA.ALLOW_UNASSIGNED,errCase.expected);
352
353                }else{
354                    doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNA.USE_STD3_RULES,errCase.expected);
355                }
356            }
357        }
358    }
359    private void doTestCompare(String s1, String s2, boolean isEqual){
360        try{
361            int retVal = IDNA.compare(s1,s2,IDNA.DEFAULT);
362            if(isEqual==true && retVal != 0){
363                errln("Did not get the expected result for s1: "+ prettify(s1)+
364                      " s2: "+prettify(s2));
365            }
366            retVal = IDNA.compare(new StringBuffer(s1), new StringBuffer(s2), IDNA.DEFAULT);
367            if(isEqual==true && retVal != 0){
368                errln("Did not get the expected result for s1: "+ prettify(s1)+
369                     " s2: "+prettify(s2));
370            }
371            retVal = IDNA.compare(UCharacterIterator.getInstance(s1), UCharacterIterator.getInstance(s2), IDNA.DEFAULT);
372            if(isEqual==true && retVal != 0){
373                errln("Did not get the expected result for s1: "+ prettify(s1)+
374                     " s2: "+prettify(s2));
375            }
376        }catch(Exception e){
377            e.printStackTrace();
378            errln("Unexpected exception thrown by IDNA.compare");
379        }
380
381        try{
382            int retVal = IDNA.compare(s1,s2,IDNA.ALLOW_UNASSIGNED);
383            if(isEqual==true && retVal != 0){
384                errln("Did not get the expected result for s1: "+ prettify(s1)+
385                      " s2: "+prettify(s2));
386            }
387            retVal = IDNA.compare(new StringBuffer(s1), new StringBuffer(s2), IDNA.ALLOW_UNASSIGNED);
388            if(isEqual==true && retVal != 0){
389                errln("Did not get the expected result for s1: "+ prettify(s1)+
390                     " s2: "+prettify(s2));
391            }
392            retVal = IDNA.compare(UCharacterIterator.getInstance(s1), UCharacterIterator.getInstance(s2), IDNA.ALLOW_UNASSIGNED);
393            if(isEqual==true && retVal != 0){
394                errln("Did not get the expected result for s1: "+ prettify(s1)+
395                     " s2: "+prettify(s2));
396            }
397        }catch(Exception e){
398            errln("Unexpected exception thrown by IDNA.compare");
399        }
400    }
401    public void TestCompare() throws Exception{
402        String www = "www.";
403        String com = ".com";
404        StringBuffer source = new StringBuffer(www);
405        StringBuffer uni0   = new StringBuffer(www);
406        StringBuffer uni1   = new StringBuffer(www);
407        StringBuffer ascii0 = new StringBuffer(www);
408        StringBuffer ascii1 = new StringBuffer(www);
409
410        uni0.append(TestData.unicodeIn[0]);
411        uni0.append(com);
412
413        uni1.append(TestData.unicodeIn[1]);
414        uni1.append(com);
415
416        ascii0.append(TestData.asciiIn[0]);
417        ascii0.append(com);
418
419        ascii1.append(TestData.asciiIn[1]);
420        ascii1.append(com);
421
422        for(int i=0;i< TestData.unicodeIn.length; i++){
423
424            // for every entry in unicodeIn array
425            // prepend www. and append .com
426            source.setLength(4);
427            source.append(TestData.unicodeIn[i]);
428            source.append(com);
429
430            // a) compare it with itself
431            doTestCompare(source.toString(),source.toString(),true);
432
433            // b) compare it with asciiIn equivalent
434            doTestCompare(source.toString(),www+TestData.asciiIn[i]+com,true);
435
436            // c) compare it with unicodeIn not equivalent
437            if(i==0){
438                doTestCompare(source.toString(), uni1.toString(), false);
439            }else{
440                doTestCompare(source.toString(),uni0.toString(), false);
441            }
442            // d) compare it with asciiIn not equivalent
443            if(i==0){
444                doTestCompare(source.toString(),ascii1.toString(), false);
445            }else{
446                doTestCompare(source.toString(),ascii0.toString(), false);
447            }
448
449        }
450    }
451
452    //  test and ascertain
453    //  func(func(func(src))) == func(src)
454    public void doTestChainingToASCII(String source)throws Exception{
455        StringBuffer expected;
456        StringBuffer chained;
457
458        // test convertIDNToASCII
459        expected = IDNA.convertIDNToASCII(source,IDNA.DEFAULT);
460        chained = expected;
461        for(int i=0; i< 4; i++){
462            chained = IDNA.convertIDNToASCII(chained,IDNA.DEFAULT);
463        }
464        if(!expected.toString().equals(chained.toString())){
465            errln("Chaining test failed for convertIDNToASCII");
466        }
467        // test convertIDNToA
468        expected = IDNA.convertToASCII(source,IDNA.DEFAULT);
469        chained = expected;
470        for(int i=0; i< 4; i++){
471            chained = IDNA.convertToASCII(chained,IDNA.DEFAULT);
472        }
473        if(!expected.toString().equals(chained.toString())){
474            errln("Chaining test failed for convertToASCII");
475        }
476    }
477    //  test and ascertain
478    //  func(func(func(src))) == func(src)
479    public void doTestChainingToUnicode(String source)throws Exception{
480        StringBuffer expected;
481        StringBuffer chained;
482
483        // test convertIDNToUnicode
484        expected = IDNA.convertIDNToUnicode(source,IDNA.DEFAULT);
485        chained = expected;
486        for(int i=0; i< 4; i++){
487            chained = IDNA.convertIDNToUnicode(chained,IDNA.DEFAULT);
488        }
489        if(!expected.toString().equals(chained.toString())){
490            errln("Chaining test failed for convertIDNToUnicode");
491        }
492        // test convertIDNToA
493        expected = IDNA.convertToUnicode(source,IDNA.DEFAULT);
494        chained = expected;
495        for(int i=0; i< 4; i++){
496            chained = IDNA.convertToUnicode(chained,IDNA.DEFAULT);
497        }
498        if(!expected.toString().equals(chained.toString())){
499            errln("Chaining test failed for convertToUnicode");
500        }
501    }
502    public void TestChaining() throws Exception{
503        for(int i=0; i< TestData.asciiIn.length; i++){
504            doTestChainingToUnicode(TestData.asciiIn[i]);
505        }
506        for(int i=0; i< TestData.unicodeIn.length; i++){
507            doTestChainingToASCII(new String(TestData.unicodeIn[i]));
508        }
509    }
510
511
512    /* IDNA RFC Says:
513    A label is an individual part of a domain name.  Labels are usually
514    shown separated by dots; for example, the domain name
515    "www.example.com" is composed of three labels: "www", "example", and
516    "com".  (The zero-length root label described in [STD13], which can
517    be explicit as in "www.example.com." or implicit as in
518    "www.example.com", is not considered a label in this specification.)
519    */
520    public void TestRootLabelSeparator() throws Exception{
521        String www = "www.";
522        String com = ".com."; //root label separator
523        StringBuffer source = new StringBuffer(www);
524        StringBuffer uni0   = new StringBuffer(www);
525        StringBuffer uni1   = new StringBuffer(www);
526        StringBuffer ascii0 = new StringBuffer(www);
527        StringBuffer ascii1 = new StringBuffer(www);
528
529        uni0.append(TestData.unicodeIn[0]);
530        uni0.append(com);
531
532        uni1.append(TestData.unicodeIn[1]);
533        uni1.append(com);
534
535        ascii0.append(TestData.asciiIn[0]);
536        ascii0.append(com);
537
538        ascii1.append(TestData.asciiIn[1]);
539        ascii1.append(com);
540
541        for(int i=0;i< TestData.unicodeIn.length; i++){
542
543            // for every entry in unicodeIn array
544            // prepend www. and append .com
545            source.setLength(4);
546            source.append(TestData.unicodeIn[i]);
547            source.append(com);
548
549            // a) compare it with itself
550            doTestCompare(source.toString(),source.toString(),true);
551
552            // b) compare it with asciiIn equivalent
553            doTestCompare(source.toString(),www+TestData.asciiIn[i]+com,true);
554
555            // c) compare it with unicodeIn not equivalent
556            if(i==0){
557                doTestCompare(source.toString(), uni1.toString(), false);
558            }else{
559                doTestCompare(source.toString(),uni0.toString(), false);
560            }
561            // d) compare it with asciiIn not equivalent
562            if(i==0){
563                doTestCompare(source.toString(),ascii1.toString(), false);
564            }else{
565                doTestCompare(source.toString(),ascii0.toString(), false);
566            }
567
568        }
569
570    }
571
572
573    private static final int loopCount = 100;
574    private static final int maxCharCount = 15;
575   // private static final int maxCodePoint = 0x10ffff;
576    private Random random = null;
577
578    /**
579     * Return a random integer i where 0 <= i < n.
580     * A special function that gets random codepoints from planes 0,1,2 and 14
581     */
582    private int rand_uni()
583    {
584       int retVal = (int)(random.nextLong()& 0x3FFFF);
585       if(retVal >= 0x30000){
586           retVal+=0xB0000;
587       }
588       return retVal;
589    }
590
591    private int randi(int n){
592        return (random.nextInt(0x7fff) % (n+1));
593    }
594
595    private StringBuffer getTestSource(StringBuffer fillIn) {
596        // use uniform seed value from the framework
597        if(random==null){
598            random = createRandom();
599        }
600        int i = 0;
601        int charCount = (randi(maxCharCount) + 1);
602        while (i <charCount ) {
603            int codepoint = rand_uni();
604            if(codepoint == 0x0000){
605                continue;
606            }
607            UTF16.append(fillIn, codepoint);
608            i++;
609        }
610        return fillIn;
611
612    }
613    public void MonkeyTest() throws Exception{
614         StringBuffer source = new StringBuffer();
615         /* do the monkey test   */
616         for(int i=0; i<loopCount; i++){
617             source.setLength(0);
618             getTestSource(source);
619             doTestCompareReferenceImpl(source);
620         }
621
622         // test string with embedded null
623         source.append( "\\u0000\\u2109\\u3E1B\\U000E65CA\\U0001CAC5" );
624
625         source = new StringBuffer(Utility.unescape(source.toString()));
626         doTestCompareReferenceImpl(source);
627
628         //StringBuffer src = new StringBuffer(Utility.unescape("\\uDEE8\\U000E228C\\U0002EE8E\\U000E6350\\U00024DD9\u4049\\U000E0DE4\\U000E448C\\U0001869B\\U000E3380\\U00016A8E\\U000172D5\\U0001C408\\U000E9FB5"));
629         //doTestCompareReferenceImpl(src);
630
631         //test deletion of code points
632         source = new StringBuffer(Utility.unescape("\\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"));
633         StringBuffer expected = new StringBuffer("xn--b1abfaaepdrnnbgefbadotcwatmq2g4l");
634         doTestCompareReferenceImpl(source);
635         doTestToASCII(source.toString(),expected.toString(), IDNA.DEFAULT, null);
636    }
637
638    private StringBuffer _doTestCompareReferenceImpl(StringBuffer src, boolean toASCII, int options) {
639        String refIDNAName = toASCII ? "IDNAReference.convertToASCII" : "IDNAReference.convertToUnicode";
640        String uIDNAName = toASCII ? "IDNA.convertToASCII" : "IDNA.convertToUnicode";
641
642        logln("Comparing " + refIDNAName + " with " + uIDNAName + " for input: "
643                + prettify(src) + " with options: " + options);
644
645        StringBuffer exp = null;
646        int expStatus = -1;
647        try {
648            exp = toASCII ? IDNAReference.convertToASCII(src, options) : IDNAReference.convertToUnicode(src, options);
649        } catch (StringPrepParseException e) {
650            expStatus = e.getError();
651        }
652
653        StringBuffer got = null;
654        int gotStatus = -1;
655        try {
656            got = toASCII ? IDNA.convertToASCII(src, options) : IDNA.convertToUnicode(src, options);
657        } catch (StringPrepParseException e) {
658            gotStatus = e.getError();
659        }
660
661        if (expStatus != gotStatus) {
662            errln("Did not get the expected status while comparing " + refIDNAName + " with " + uIDNAName
663                    + " Expected: " + expStatus
664                    + " Got: " + gotStatus
665                    + " for Source: "+ prettify(src)
666                    + " Options: " + options);
667        } else {
668            // now we know that both implementation yielded same status
669            if (gotStatus == -1) {
670                // compare the outputs
671                if (!got.toString().equals(exp.toString())) {
672                    errln("Did not get the expected output while comparing " + refIDNAName + " with " + uIDNAName
673                            + " Expected: " + exp
674                            + " Got: " + got
675                            + " for Source: "+ prettify(src)
676                            + " Options: " + options);
677                }
678            } else {
679                logln("Got the same error while comparing " + refIDNAName + " with " + uIDNAName
680                        +" for input: " + prettify(src) + " with options: " + options);
681            }
682        }
683
684        return exp;
685    }
686
687    private void doTestCompareReferenceImpl(StringBuffer src) throws Exception{
688        // test toASCII
689        src.setLength(0);
690        src.append("[");
691        StringBuffer asciiLabel = _doTestCompareReferenceImpl(src, true, IDNA.ALLOW_UNASSIGNED);
692        _doTestCompareReferenceImpl(src, true, IDNA.DEFAULT);
693        _doTestCompareReferenceImpl(src, true, IDNA.USE_STD3_RULES);
694        _doTestCompareReferenceImpl(src, true, IDNA.USE_STD3_RULES | IDNA.ALLOW_UNASSIGNED);
695
696        if (asciiLabel != null) {
697            // test toUnicode
698            _doTestCompareReferenceImpl(src, false, IDNA.ALLOW_UNASSIGNED);
699            _doTestCompareReferenceImpl(src, false, IDNA.DEFAULT);
700            _doTestCompareReferenceImpl(src, false, IDNA.USE_STD3_RULES);
701            _doTestCompareReferenceImpl(src, false, IDNA.USE_STD3_RULES | IDNA.ALLOW_UNASSIGNED);
702        }
703    }
704
705    public void TestCompareRefImpl() throws Exception {
706        for (int i = 65; i < 0x10FFFF; i++) {
707            StringBuffer src = new StringBuffer();
708            if (isQuick() == true && i > 0x0FFF) {
709                return;
710            }
711            if (i == 0x30000) {
712                // jump to E0000, no characters assigned in plain 3 to plain 13 as of Unicode 6.0
713                i = 0xE0000;
714            }
715            UTF16.append(src, i);
716            doTestCompareReferenceImpl(src);
717        }
718    }
719
720    public void TestJB4490(){
721        String[] in = new String[]{
722                "\u00F5\u00dE\u00dF\u00dD",
723                "\uFB00\uFB01"
724               };
725        for ( int i=0; i< in.length; i++){
726            try{
727                String ascii = IDNA.convertToASCII(in[i],IDNA.DEFAULT).toString();
728                try{
729                    String unicode = IDNA.convertToUnicode(ascii,IDNA.DEFAULT).toString();
730                    logln("result " + unicode);
731                }catch(StringPrepParseException ex){
732                    errln("Unexpected exception for convertToUnicode: " + ex.getMessage());
733                }
734            }catch(StringPrepParseException ex){
735                errln("Unexpected exception for convertToASCII: " + ex.getMessage());
736            }
737        }
738    }
739    public void TestJB4475(){
740        String[] in = new String[]{
741                        "TEST",
742                        "test"
743                       };
744        for ( int i=0; i< in.length; i++){
745
746            try{
747                String ascii = IDNA.convertToASCII(in[i],IDNA.DEFAULT).toString();
748                if(!ascii.equals(in[i])){
749                    errln("Did not get the expected string for convertToASCII. Expected: "+ in[i] +" Got: " + ascii);
750                }
751            }catch(StringPrepParseException ex){
752                errln("Unexpected exception: " + ex.getMessage());
753            }
754        }
755
756    }
757
758    public void TestDebug(){
759        try{
760            String src = "\u00ED4dn";
761            String uni = IDNA.convertToUnicode(src,IDNA.DEFAULT).toString();
762            if(!uni.equals(src)){
763                errln("Did not get the expected result. Expected: "+ prettify(src) +" Got: " +uni);
764            }
765        }catch(StringPrepParseException ex){
766            logln("Unexpected exception: " + ex.getMessage());
767        }
768        try{
769            String ascii = IDNA.convertToASCII("\u00AD",IDNA.DEFAULT).toString();
770            if(ascii!=null){
771                errln("Did not get the expected exception");
772            }
773        }catch(StringPrepParseException ex){
774            logln("Got the expected exception: " + ex.getMessage());
775        }
776    }
777    public void TestJB5273(){
778        String INVALID_DOMAIN_NAME = "xn--m\u00FCller.de";
779        try {
780            IDNA.convertIDNToUnicode(INVALID_DOMAIN_NAME, IDNA.DEFAULT);
781            IDNA.convertIDNToUnicode(INVALID_DOMAIN_NAME, IDNA.USE_STD3_RULES);
782
783        } catch (StringPrepParseException ex) {
784            errln("Unexpected exception: " + ex.getMessage());
785        } catch (ArrayIndexOutOfBoundsException ex) {
786            errln("Got an ArrayIndexOutOfBoundsException calling convertIDNToUnicode(\"" + INVALID_DOMAIN_NAME + "\")");
787        }
788
789        String domain = "xn--m\u00FCller.de";
790        try{
791            IDNA.convertIDNToUnicode(domain, IDNA.DEFAULT);
792        }catch(StringPrepParseException ex){
793            logln("Got the expected exception. "+ex.getMessage());
794        }catch (Exception ex){
795            errln("Unexpected exception: " + ex.getMessage());
796        }
797        try{
798            IDNA.convertIDNToUnicode(domain, IDNA.USE_STD3_RULES);
799        }catch(StringPrepParseException ex){
800            logln("Got the expected exception. "+ex.getMessage());
801        }catch (Exception ex){
802            errln("Unexpected exception: " + ex.getMessage());
803        }
804        try{
805            IDNA.convertToUnicode("xn--m\u00FCller", IDNA.DEFAULT);
806        }catch(Exception ex){
807            errln("ToUnicode operation failed! "+ex.getMessage());
808        }
809        try{
810            IDNA.convertToUnicode("xn--m\u00FCller", IDNA.USE_STD3_RULES);
811        }catch(Exception ex){
812            errln("ToUnicode operation failed! "+ex.getMessage());
813        }
814        try{
815            IDNA.convertIDNToUnicode("xn--m\u1234ller", IDNA.USE_STD3_RULES);
816        }catch(StringPrepParseException ex){
817            errln("ToUnicode operation failed! "+ex.getMessage());
818        }
819    }
820
821    public void TestLength(){
822        String ul = "my_very_very_very_very_very_very_very_very_very_very_very_very_very_long_and_incredibly_uncreative_domain_label";
823
824        /* this unicode string is longer than MAX_LABEL_BUFFER_SIZE and produces an
825           IDNA prepared string (including xn--)that is exactly 63 bytes long */
826        String ul1 ="\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774"+
827                    "\uD55C\uAD6D\uC5B4\uB97C\uC774\u00AD\u034F\u1806\u180B"+
828                    "\u180C\u180D\u200B\u200C\u200D\u2060\uFE00\uFE01\uFE02"+
829                    "\uFE03\uFE04\uFE05\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B"+
830                    "\uFE0C\uFE0D\uFE0E\uFE0F\uFEFF\uD574\uD55C\uB2E4\uBA74"+
831                    "\uC138\u0041\u00AD\u034F\u1806\u180B\u180C\u180D\u200B"+
832                    "\u200C\u200D\u2060\uFE00\uFE01\uFE02\uFE03\uFE04\uFE05"+
833                    "\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B\uFE0C\uFE0D\uFE0E"+
834                    "\uFE0F\uFEFF\u00AD\u034F\u1806\u180B\u180C\u180D\u200B"+
835                    "\u200C\u200D\u2060\uFE00\uFE01\uFE02\uFE03\uFE04\uFE05"+
836                    "\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B\uFE0C\uFE0D\uFE0E"+
837                    "\uFE0F\uFEFF\u00AD\u034F\u1806\u180B\u180C\u180D\u200B"+
838                    "\u200C\u200D\u2060\uFE00\uFE01\uFE02\uFE03\uFE04\uFE05"+
839                    "\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B\uFE0C\uFE0D\uFE0E"+
840                    "\uFE0F\uFEFF";
841        try{
842            IDNA.convertToASCII(ul, IDNA.DEFAULT);
843            errln("IDNA.convertToUnicode did not fail!");
844        }catch (StringPrepParseException ex){
845            if(ex.getError()!= StringPrepParseException.LABEL_TOO_LONG_ERROR){
846                errln("IDNA.convertToASCII failed with error: "+ex.toString());
847            }else{
848                logln("IDNA.convertToASCII(ul, IDNA.DEFAULT) Succeeded");
849            }
850        }
851        try{
852            IDNA.convertToASCII(ul1, IDNA.DEFAULT);
853        }catch (StringPrepParseException ex){
854            errln("IDNA.convertToASCII failed with error: "+ex.toString());
855        }
856        try{
857            IDNA.convertToUnicode(ul1, IDNA.DEFAULT);
858        }catch (StringPrepParseException ex){
859            errln("IDNA.convertToASCII failed with error: "+ex.toString());
860        }
861        try{
862            IDNA.convertToUnicode(ul, IDNA.DEFAULT);
863        }catch (StringPrepParseException ex){
864            errln("IDNA.convertToASCII failed with error: "+ex.toString());
865        }
866
867        String idn = "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";
868        try{
869            IDNA.convertIDNToASCII(idn, IDNA.DEFAULT);
870            errln("IDNA.convertToUnicode did not fail!");
871        }catch (StringPrepParseException ex){
872            if(ex.getError()!= StringPrepParseException.DOMAIN_NAME_TOO_LONG_ERROR){
873                errln("IDNA.convertToASCII failed with error: "+ex.toString());
874            }else{
875                logln("IDNA.convertToASCII(idn, IDNA.DEFAULT) Succeeded");
876            }
877        }
878        try{
879            IDNA.convertIDNToUnicode(idn, IDNA.DEFAULT);
880            errln("IDNA.convertToUnicode did not fail!");
881        }catch (StringPrepParseException ex){
882            if(ex.getError()!= StringPrepParseException.DOMAIN_NAME_TOO_LONG_ERROR){
883                errln("IDNA.convertToUnicode failed with error: "+ex.toString());
884            }else{
885                logln("IDNA.convertToUnicode(idn, IDNA.DEFAULT) Succeeded");
886            }
887        }
888    }
889
890    /* Tests the method public static StringBuffer convertToASCII(String src, int options) */
891    public void TestConvertToASCII() {
892        try {
893            if (!IDNA.convertToASCII("dummy", 0).toString().equals("dummy")) {
894                errln("IDNA.convertToASCII(String,int) was suppose to return the same string passed.");
895            }
896        } catch (Exception e) {
897            errln("IDNA.convertToASCII(String,int) was not suppose to return an exception.");
898        }
899    }
900
901    /*
902     * Tests the method public static StringBuffer convertIDNToASCII(UCharacterIterator src, int options), method public
903     * static StringBuffer public static StringBuffer convertIDNToASCII(StringBuffer src, int options), public static
904     * StringBuffer convertIDNToASCII(UCharacterIterator src, int options)
905     */
906    public void TestConvertIDNToASCII() {
907        try {
908            UCharacterIterator uci = UCharacterIterator.getInstance("dummy");
909            if (!IDNA.convertIDNToASCII(uci, 0).toString().equals("dummy")) {
910                errln("IDNA.convertIDNToASCII(UCharacterIterator, int) was suppose to "
911                        + "return the same string passed.");
912            }
913            if (!IDNA.convertIDNToASCII(new StringBuffer("dummy"), 0).toString().equals("dummy")) {
914                errln("IDNA.convertIDNToASCII(StringBuffer, int) was suppose to " + "return the same string passed.");
915            }
916        } catch (Exception e) {
917            errln("IDNA.convertIDNToASCII was not suppose to return an exception.");
918        }
919    }
920
921    /*
922     * Tests the method public static StringBuffer convertToUnicode(String src, int options), public static StringBuffer
923     * convertToUnicode(StringBuffer src, int options)
924     */
925    public void TestConvertToUnicode() {
926        try {
927            if (!IDNA.convertToUnicode("dummy", 0).toString().equals("dummy")) {
928                errln("IDNA.convertToUnicode(String, int) was suppose to " + "return the same string passed.");
929            }
930            if (!IDNA.convertToUnicode(new StringBuffer("dummy"), 0).toString().equals("dummy")) {
931                errln("IDNA.convertToUnicode(StringBuffer, int) was suppose to " + "return the same string passed.");
932            }
933        } catch (Exception e) {
934            errln("IDNA.convertToUnicode was not suppose to return an exception.");
935        }
936    }
937
938    /* Tests the method public static StringBuffer convertIDNToUnicode(UCharacterIterator src, int options) */
939    public void TestConvertIDNToUnicode() {
940        try {
941            UCharacterIterator uci = UCharacterIterator.getInstance("dummy");
942            if (!IDNA.convertIDNToUnicode(uci, 0).toString().equals("dummy")) {
943                errln("IDNA.convertIDNToUnicode(UCharacterIterator, int) was suppose to "
944                        + "return the same string passed.");
945            }
946            if (!IDNA.convertIDNToUnicode(new StringBuffer("dummy"), 0).toString().equals("dummy")) {
947                errln("IDNA.convertIDNToUnicode(StringBuffer, int) was suppose to " + "return the same string passed.");
948            }
949        } catch (Exception e) {
950            errln("IDNA.convertIDNToUnicode was not suppose to return an exception.");
951        }
952    }
953
954    /* Tests the method public static int compare */
955    public void TestIDNACompare() {
956        // Testing the method public static int compare(String s1, String s2, int options)
957        try {
958            IDNA.compare((String) null, (String) null, 0);
959            errln("IDNA.compare((String)null,(String)null) was suppose to return an exception.");
960        } catch (Exception e) {
961        }
962
963        try {
964            IDNA.compare((String) null, "dummy", 0);
965            errln("IDNA.compare((String)null,'dummy') was suppose to return an exception.");
966        } catch (Exception e) {
967        }
968
969        try {
970            IDNA.compare("dummy", (String) null, 0);
971            errln("IDNA.compare('dummy',(String)null) was suppose to return an exception.");
972        } catch (Exception e) {
973        }
974
975        try {
976            if (IDNA.compare("dummy", "dummy", 0) != 0) {
977                errln("IDNA.compare('dummy','dummy') was suppose to return a 0.");
978            }
979        } catch (Exception e) {
980            errln("IDNA.compare('dummy','dummy') was not suppose to return an exception.");
981        }
982
983        // Testing the method public static int compare(StringBuffer s1, StringBuffer s2, int options)
984        try {
985            IDNA.compare((StringBuffer) null, (StringBuffer) null, 0);
986            errln("IDNA.compare((StringBuffer)null,(StringBuffer)null) was suppose to return an exception.");
987        } catch (Exception e) {
988        }
989
990        try {
991            IDNA.compare((StringBuffer) null, new StringBuffer("dummy"), 0);
992            errln("IDNA.compare((StringBuffer)null,'dummy') was suppose to return an exception.");
993        } catch (Exception e) {
994        }
995
996        try {
997            IDNA.compare(new StringBuffer("dummy"), (StringBuffer) null, 0);
998            errln("IDNA.compare('dummy',(StringBuffer)null) was suppose to return an exception.");
999        } catch (Exception e) {
1000        }
1001
1002        try {
1003            if (IDNA.compare(new StringBuffer("dummy"), new StringBuffer("dummy"), 0) != 0) {
1004                errln("IDNA.compare(new StringBuffer('dummy'),new StringBuffer('dummy')) was suppose to return a 0.");
1005            }
1006        } catch (Exception e) {
1007            errln("IDNA.compare(new StringBuffer('dummy'),new StringBuffer('dummy')) was not suppose to return an exception.");
1008        }
1009
1010        // Testing the method public static int compare(UCharacterIterator s1, UCharacterIterator s2, int options)
1011        UCharacterIterator uci = UCharacterIterator.getInstance("dummy");
1012        try {
1013            IDNA.compare((UCharacterIterator) null, (UCharacterIterator) null, 0);
1014            errln("IDNA.compare((UCharacterIterator)null,(UCharacterIterator)null) was suppose to return an exception.");
1015        } catch (Exception e) {
1016        }
1017
1018        try {
1019            IDNA.compare((UCharacterIterator) null, uci, 0);
1020            errln("IDNA.compare((UCharacterIterator)null,UCharacterIterator) was suppose to return an exception.");
1021        } catch (Exception e) {
1022        }
1023
1024        try {
1025            IDNA.compare(uci, (UCharacterIterator) null, 0);
1026            errln("IDNA.compare(UCharacterIterator,(UCharacterIterator)null) was suppose to return an exception.");
1027        } catch (Exception e) {
1028        }
1029
1030        try {
1031            if (IDNA.compare(uci, uci, 0) != 0) {
1032                errln("IDNA.compare(UCharacterIterator('dummy'),UCharacterIterator('dummy')) was suppose to return a 0.");
1033            }
1034        } catch (Exception e) {
1035            errln("IDNA.compare(UCharacterIterator('dummy'),UCharacterIterator('dummy')) was not suppose to return an exception.");
1036        }
1037    }
1038}
1039