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