2 *******************************************************************************
\r
3 * Copyright (C) 2003-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.stringprep;
\r
9 import java.util.Random;
\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
22 public class TestIDNA extends TestFmwk {
\r
23 public static void main(String[] args) throws Exception {
\r
24 new TestIDNA().run(args);
\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
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
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
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
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
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
75 private void doTestToUnicode(String src, String expected, int options, Object expectedException)
\r
77 StringBuffer inBuf = new StringBuffer(src);
\r
78 UCharacterIterator inIter = UCharacterIterator.getInstance(src);
\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
86 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
87 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
\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
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
101 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
102 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
\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
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
116 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
117 errln("Did not get the expected exception. The operation succeeded!");
\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
126 private void doTestIDNToUnicode(String src, String expected, int options, Object expectedException)
\r
128 StringBuffer inBuf = new StringBuffer(src);
\r
129 UCharacterIterator inIter = UCharacterIterator.getInstance(src);
\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
137 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
138 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
\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
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
151 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
152 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
\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
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
166 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
167 errln("Did not get the expected exception. The operation succeeded!");
\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
175 private void doTestToASCII(String src, String expected, int options, Object expectedException)
\r
177 StringBuffer inBuf = new StringBuffer(src);
\r
178 UCharacterIterator inIter = UCharacterIterator.getInstance(src);
\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
186 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
187 errln("convertToASCII did not get the expected exception. The operation succeeded!");
\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
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
201 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
202 errln("convertToASCII did not get the expected exception. The operation succeeded!");
\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
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
216 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
217 errln("convertToASCII did not get the expected exception. The operation succeeded!");
\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
225 private void doTestIDNToASCII(String src, String expected, int options, Object expectedException)
\r
227 StringBuffer inBuf = new StringBuffer(src);
\r
228 UCharacterIterator inIter = UCharacterIterator.getInstance(src);
\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
236 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
237 errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
\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
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
250 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
251 errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
\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
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
266 if(expectedException!=null && !unassignedException.equals(expectedException)){
\r
267 errln("convertIDNToASCII did not get the expected exception. The operation succeeded!");
\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
275 public void TestConformance()throws Exception{
\r
276 for(int i=0; i<TestData.conformanceTestCases.length;i++){
\r
278 TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
\r
279 if(testCase.expected != null){
\r
281 doTestToASCII(testCase.input,testCase.output,IDNA.DEFAULT,testCase.expected);
\r
282 doTestToASCII(testCase.input,testCase.output,IDNA.ALLOW_UNASSIGNED,testCase.expected);
\r
285 //doTestToUnicode(testCase.input,testCase.output,IDNA.DEFAULT,testCase.expected);
\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
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
299 if(testCase.expected!=null && !unassignedException.equals(testCase.expected)){
\r
300 errln("Did not get the expected exception. The operation succeeded!");
\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
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
315 if(testCase.expected!=null && !unassignedException.equals(testCase.expected)){
\r
316 errln("Did not get the expected exception. The operation succeeded!");
\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
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
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
337 if(errCase.useSTD3ASCIIRules!=true){
\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
344 doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNA.USE_STD3_RULES,errCase.expected);
\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
355 doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNA.USE_STD3_RULES,errCase.expected);
\r
360 private void doTestCompare(String s1, String s2, boolean isEqual){
\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
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
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
377 }catch(Exception e){
\r
378 e.printStackTrace();
\r
379 errln("Unexpected exception thrown by IDNA.compare");
\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
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
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
398 }catch(Exception e){
\r
399 errln("Unexpected exception thrown by IDNA.compare");
\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
411 uni0.append(TestData.unicodeIn[0]);
\r
414 uni1.append(TestData.unicodeIn[1]);
\r
417 ascii0.append(TestData.asciiIn[0]);
\r
418 ascii0.append(com);
\r
420 ascii1.append(TestData.asciiIn[1]);
\r
421 ascii1.append(com);
\r
423 for(int i=0;i< TestData.unicodeIn.length; i++){
\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
431 // a) compare it with itself
\r
432 doTestCompare(source.toString(),source.toString(),true);
\r
434 // b) compare it with asciiIn equivalent
\r
435 doTestCompare(source.toString(),www+TestData.asciiIn[i]+com,true);
\r
437 // c) compare it with unicodeIn not equivalent
\r
439 doTestCompare(source.toString(), uni1.toString(), false);
\r
441 doTestCompare(source.toString(),uni0.toString(), false);
\r
443 // d) compare it with asciiIn not equivalent
\r
445 doTestCompare(source.toString(),ascii1.toString(), false);
\r
447 doTestCompare(source.toString(),ascii0.toString(), false);
\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
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
465 if(!expected.toString().equals(chained.toString())){
\r
466 errln("Chaining test failed for convertIDNToASCII");
\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
474 if(!expected.toString().equals(chained.toString())){
\r
475 errln("Chaining test failed for convertToASCII");
\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
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
490 if(!expected.toString().equals(chained.toString())){
\r
491 errln("Chaining test failed for convertIDNToUnicode");
\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
499 if(!expected.toString().equals(chained.toString())){
\r
500 errln("Chaining test failed for convertToUnicode");
\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
507 for(int i=0; i< TestData.unicodeIn.length; i++){
\r
508 doTestChainingToASCII(new String(TestData.unicodeIn[i]));
\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
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
530 uni0.append(TestData.unicodeIn[0]);
\r
533 uni1.append(TestData.unicodeIn[1]);
\r
536 ascii0.append(TestData.asciiIn[0]);
\r
537 ascii0.append(com);
\r
539 ascii1.append(TestData.asciiIn[1]);
\r
540 ascii1.append(com);
\r
542 for(int i=0;i< TestData.unicodeIn.length; i++){
\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
550 // a) compare it with itself
\r
551 doTestCompare(source.toString(),source.toString(),true);
\r
553 // b) compare it with asciiIn equivalent
\r
554 doTestCompare(source.toString(),www+TestData.asciiIn[i]+com,true);
\r
556 // c) compare it with unicodeIn not equivalent
\r
558 doTestCompare(source.toString(), uni1.toString(), false);
\r
560 doTestCompare(source.toString(),uni0.toString(), false);
\r
562 // d) compare it with asciiIn not equivalent
\r
564 doTestCompare(source.toString(),ascii1.toString(), false);
\r
566 doTestCompare(source.toString(),ascii0.toString(), false);
\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
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
583 private int rand_uni()
\r
585 int retVal = (int)(random.nextLong()& 0x3FFFF);
\r
586 if(retVal >= 0x30000){
\r
592 private int randi(int n){
\r
593 return (random.nextInt(0x7fff) % (n+1));
\r
596 private StringBuffer getTestSource(StringBuffer fillIn) {
\r
597 // use uniform seed value from the framework
\r
599 random = createRandom();
\r
602 int charCount = (randi(maxCharCount) + 1);
\r
603 while (i <charCount ) {
\r
604 int codepoint = rand_uni();
\r
605 if(codepoint == 0x0000){
\r
608 UTF16.append(fillIn, codepoint);
\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
623 // test string with embedded null
\r
624 source.append( "\\u0000\\u2109\\u3E1B\\U000E65CA\\U0001CAC5" );
\r
626 source = new StringBuffer(Utility.unescape(source.toString()));
\r
627 doTestCompareReferenceImpl(source);
\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
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
638 private void doTestCompareReferenceImpl(StringBuffer src) throws Exception{
\r
640 StringBuffer label = src;
\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
647 ascii = IDNAReference.convertToASCII(label, options);
\r
648 }catch( StringPrepParseException e){
\r
650 if(e.equals(unassignedException)){
\r
651 options = IDNA.ALLOW_UNASSIGNED;
\r
654 ascii = IDNAReference.convertToASCII(label, options);
\r
655 }catch( StringPrepParseException ex){
\r
661 doTestToASCII(label.toString(),
\r
662 (ascii == null) ? null : ascii.toString(),
\r
666 logln("Comparing idnaref_toUnicode with uidna_toUnicode for input: " + prettify(label));
\r
667 StringBuffer uni =null;
\r
669 if(expected == null){
\r
670 options = IDNA.DEFAULT;
\r
672 uni = IDNAReference.convertToUnicode(ascii, options);
\r
673 }catch( StringPrepParseException e ){
\r
675 if(expected.equals(unassignedException)){
\r
676 options = IDNA.ALLOW_UNASSIGNED;
\r
679 uni = IDNAReference.convertToUnicode(ascii, options);
\r
680 }catch(StringPrepParseException ex){
\r
685 doTestToUnicode(ascii.toString(),
\r
686 (uni==null)? null : uni.toString(),
\r
692 public void TestCompareRefImpl() throws Exception{
\r
694 for(int i = 0x40000 ; i< 0x10ffff; i++){
\r
695 StringBuffer src = new StringBuffer();
\r
697 if(isQuick()==true && i> 0x1FFFF){
\r
700 if(i >= 0x30000 && i<=0xf0000){
\r
703 UTF16.append(src,i);
\r
704 doTestCompareReferenceImpl(src);
\r
708 public void TestUnicode32Norm() {
\r
709 // This test fails - is this because of the new Noramlizer implementation?
\r
710 // Revisit this after 4.4.
\r
711 if (skipIfBeforeICU(4,5,1)) {
\r
712 logln("Skipping TestUnicode32Norm");
\r
717 * test Unicode 3.2 normalization, before Public Review Issue #29
\r
718 * see cnormtst.c TestComposition()
\r
720 final String strings[]={
\r
721 "\u1100\u0300\u1161\u0327",
\r
722 "\u0b47\u0300\u0b3e\u0327"
\r
725 String ascii = null, unicode = null;
\r
728 for(i=0; i<strings.length; ++i) {
\r
730 ascii=IDNA.convertToASCII(strings[i], 0).toString();
\r
731 unicode=IDNA.convertToUnicode(ascii, 0).toString();
\r
732 } catch(StringPrepParseException ex) {
\r
733 errln("string " + i + " gets exception " + ex.toString());
\r
736 if(unicode == null || unicode.compareTo(ascii) != 0) {
\r
737 String uc = unicode == null? "(null)" : unicode;
\r
739 errln("string " + i + " yields " + uc +" instead of " + ascii);
\r
743 public void TestJB4490(){
\r
744 String[] in = new String[]{
\r
745 "\u00F5\u00dE\u00dF\u00dD",
\r
748 for ( int i=0; i< in.length; i++){
\r
750 String ascii = IDNA.convertToASCII(in[i],IDNA.DEFAULT).toString();
\r
752 String unicode = IDNA.convertToUnicode(ascii,IDNA.DEFAULT).toString();
\r
753 logln("result " + unicode);
\r
754 }catch(StringPrepParseException ex){
\r
755 errln("Unexpected exception for convertToUnicode: " + ex.getMessage());
\r
757 }catch(StringPrepParseException ex){
\r
758 errln("Unexpected exception for convertToASCII: " + ex.getMessage());
\r
762 public void TestJB4475(){
\r
763 String[] in = new String[]{
\r
767 for ( int i=0; i< in.length; i++){
\r
770 String ascii = IDNA.convertToASCII(in[i],IDNA.DEFAULT).toString();
\r
771 if(!ascii.equals(in[i])){
\r
772 errln("Did not get the expected string for convertToASCII. Expected: "+ in[i] +" Got: " + ascii);
\r
774 }catch(StringPrepParseException ex){
\r
775 errln("Unexpected exception: " + ex.getMessage());
\r
781 public void TestDebug(){
\r
783 String src = "\u00ED4dn";
\r
784 String uni = IDNA.convertToUnicode(src,IDNA.DEFAULT).toString();
\r
785 if(!uni.equals(src)){
\r
786 errln("Did not get the expected result. Expected: "+ prettify(src) +" Got: " +uni);
\r
788 }catch(StringPrepParseException ex){
\r
789 logln("Unexpected exception: " + ex.getMessage());
\r
792 String ascii = IDNA.convertToASCII("\u00AD",IDNA.DEFAULT).toString();
\r
794 errln("Did not get the expected exception");
\r
796 }catch(StringPrepParseException ex){
\r
797 logln("Got the expected exception: " + ex.getMessage());
\r
800 public void TestJB5273(){
\r
801 String INVALID_DOMAIN_NAME = "xn--m\u00FCller.de";
\r
803 IDNA.convertIDNToUnicode(INVALID_DOMAIN_NAME, IDNA.DEFAULT);
\r
804 IDNA.convertIDNToUnicode(INVALID_DOMAIN_NAME, IDNA.USE_STD3_RULES);
\r
806 } catch (StringPrepParseException ex) {
\r
807 errln("Unexpected exception: " + ex.getMessage());
\r
808 } catch (ArrayIndexOutOfBoundsException ex) {
\r
809 errln("Got an ArrayIndexOutOfBoundsException calling convertIDNToUnicode(\"" + INVALID_DOMAIN_NAME + "\")");
\r
812 String domain = "xn--m\u00FCller.de";
\r
814 IDNA.convertIDNToUnicode(domain, IDNA.DEFAULT);
\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
821 IDNA.convertIDNToUnicode(domain, IDNA.USE_STD3_RULES);
\r
822 }catch(StringPrepParseException ex){
\r
823 logln("Got the expected exception. "+ex.getMessage());
\r
824 }catch (Exception ex){
\r
825 errln("Unexpected exception: " + ex.getMessage());
\r
828 IDNA.convertToUnicode("xn--m\u00FCller", IDNA.DEFAULT);
\r
829 }catch(Exception ex){
\r
830 errln("ToUnicode operation failed! "+ex.getMessage());
\r
833 IDNA.convertToUnicode("xn--m\u00FCller", IDNA.USE_STD3_RULES);
\r
834 }catch(Exception ex){
\r
835 errln("ToUnicode operation failed! "+ex.getMessage());
\r
838 IDNA.convertIDNToUnicode("xn--m\u1234ller", IDNA.USE_STD3_RULES);
\r
839 }catch(StringPrepParseException ex){
\r
840 errln("ToUnicode operation failed! "+ex.getMessage());
\r
844 public void TestLength(){
\r
845 String ul = "my_very_very_very_very_very_very_very_very_very_very_very_very_very_long_and_incredibly_uncreative_domain_label";
\r
847 /* this unicode string is longer than MAX_LABEL_BUFFER_SIZE and produces an
\r
848 IDNA prepared string (including xn--)that is exactly 63 bytes long */
\r
849 String ul1 ="\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774"+
\r
850 "\uD55C\uAD6D\uC5B4\uB97C\uC774\u00AD\u034F\u1806\u180B"+
\r
851 "\u180C\u180D\u200B\u200C\u200D\u2060\uFE00\uFE01\uFE02"+
\r
852 "\uFE03\uFE04\uFE05\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B"+
\r
853 "\uFE0C\uFE0D\uFE0E\uFE0F\uFEFF\uD574\uD55C\uB2E4\uBA74"+
\r
854 "\uC138\u0041\u00AD\u034F\u1806\u180B\u180C\u180D\u200B"+
\r
855 "\u200C\u200D\u2060\uFE00\uFE01\uFE02\uFE03\uFE04\uFE05"+
\r
856 "\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B\uFE0C\uFE0D\uFE0E"+
\r
857 "\uFE0F\uFEFF\u00AD\u034F\u1806\u180B\u180C\u180D\u200B"+
\r
858 "\u200C\u200D\u2060\uFE00\uFE01\uFE02\uFE03\uFE04\uFE05"+
\r
859 "\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B\uFE0C\uFE0D\uFE0E"+
\r
860 "\uFE0F\uFEFF\u00AD\u034F\u1806\u180B\u180C\u180D\u200B"+
\r
861 "\u200C\u200D\u2060\uFE00\uFE01\uFE02\uFE03\uFE04\uFE05"+
\r
862 "\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B\uFE0C\uFE0D\uFE0E"+
\r
865 IDNA.convertToASCII(ul, IDNA.DEFAULT);
\r
866 errln("IDNA.convertToUnicode did not fail!");
\r
867 }catch (StringPrepParseException ex){
\r
868 if(ex.getError()!= StringPrepParseException.LABEL_TOO_LONG_ERROR){
\r
869 errln("IDNA.convertToASCII failed with error: "+ex.toString());
\r
871 logln("IDNA.convertToASCII(ul, IDNA.DEFAULT) Succeeded");
\r
875 IDNA.convertToASCII(ul1, IDNA.DEFAULT);
\r
876 }catch (StringPrepParseException ex){
\r
877 errln("IDNA.convertToASCII failed with error: "+ex.toString());
\r
880 IDNA.convertToUnicode(ul1, IDNA.DEFAULT);
\r
881 }catch (StringPrepParseException ex){
\r
882 errln("IDNA.convertToASCII failed with error: "+ex.toString());
\r
885 IDNA.convertToUnicode(ul, IDNA.DEFAULT);
\r
886 }catch (StringPrepParseException ex){
\r
887 errln("IDNA.convertToASCII failed with error: "+ex.toString());
\r
890 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
892 IDNA.convertIDNToASCII(idn, IDNA.DEFAULT);
\r
893 errln("IDNA.convertToUnicode did not fail!");
\r
894 }catch (StringPrepParseException ex){
\r
895 if(ex.getError()!= StringPrepParseException.DOMAIN_NAME_TOO_LONG_ERROR){
\r
896 errln("IDNA.convertToASCII failed with error: "+ex.toString());
\r
898 logln("IDNA.convertToASCII(idn, IDNA.DEFAULT) Succeeded");
\r
902 IDNA.convertIDNToUnicode(idn, IDNA.DEFAULT);
\r
903 errln("IDNA.convertToUnicode did not fail!");
\r
904 }catch (StringPrepParseException ex){
\r
905 if(ex.getError()!= StringPrepParseException.DOMAIN_NAME_TOO_LONG_ERROR){
\r
906 errln("IDNA.convertToUnicode failed with error: "+ex.toString());
\r
908 logln("IDNA.convertToUnicode(idn, IDNA.DEFAULT) Succeeded");
\r
913 /* Tests the method public static StringBuffer convertToASCII(String src, int options) */
\r
914 public void TestConvertToASCII() {
\r
916 if (!IDNA.convertToASCII("dummy", 0).toString().equals("dummy")) {
\r
917 errln("IDNA.convertToASCII(String,int) was suppose to return the same string passed.");
\r
919 } catch (Exception e) {
\r
920 errln("IDNA.convertToASCII(String,int) was not suppose to return an exception.");
\r
925 * Tests the method public static StringBuffer convertIDNToASCII(UCharacterIterator src, int options), method public
\r
926 * static StringBuffer public static StringBuffer convertIDNToASCII(StringBuffer src, int options), public static
\r
927 * StringBuffer convertIDNToASCII(UCharacterIterator src, int options)
\r
929 public void TestConvertIDNToASCII() {
\r
931 UCharacterIterator uci = UCharacterIterator.getInstance("dummy");
\r
932 if (!IDNA.convertIDNToASCII(uci, 0).toString().equals("dummy")) {
\r
933 errln("IDNA.convertIDNToASCII(UCharacterIterator, int) was suppose to "
\r
934 + "return the same string passed.");
\r
936 if (!IDNA.convertIDNToASCII(new StringBuffer("dummy"), 0).toString().equals("dummy")) {
\r
937 errln("IDNA.convertIDNToASCII(StringBuffer, int) was suppose to " + "return the same string passed.");
\r
939 } catch (Exception e) {
\r
940 errln("IDNA.convertIDNToASCII was not suppose to return an exception.");
\r
945 * Tests the method public static StringBuffer convertToUnicode(String src, int options), public static StringBuffer
\r
946 * convertToUnicode(StringBuffer src, int options)
\r
948 public void TestConvertToUnicode() {
\r
950 if (!IDNA.convertToUnicode("dummy", 0).toString().equals("dummy")) {
\r
951 errln("IDNA.convertToUnicode(String, int) was suppose to " + "return the same string passed.");
\r
953 if (!IDNA.convertToUnicode(new StringBuffer("dummy"), 0).toString().equals("dummy")) {
\r
954 errln("IDNA.convertToUnicode(StringBuffer, int) was suppose to " + "return the same string passed.");
\r
956 } catch (Exception e) {
\r
957 errln("IDNA.convertToUnicode was not suppose to return an exception.");
\r
961 /* Tests the method public static StringBuffer convertIDNToUnicode(UCharacterIterator src, int options) */
\r
962 public void TestConvertIDNToUnicode() {
\r
964 UCharacterIterator uci = UCharacterIterator.getInstance("dummy");
\r
965 if (!IDNA.convertIDNToUnicode(uci, 0).toString().equals("dummy")) {
\r
966 errln("IDNA.convertIDNToUnicode(UCharacterIterator, int) was suppose to "
\r
967 + "return the same string passed.");
\r
969 if (!IDNA.convertIDNToUnicode(new StringBuffer("dummy"), 0).toString().equals("dummy")) {
\r
970 errln("IDNA.convertIDNToUnicode(StringBuffer, int) was suppose to " + "return the same string passed.");
\r
972 } catch (Exception e) {
\r
973 errln("IDNA.convertIDNToUnicode was not suppose to return an exception.");
\r
977 /* Tests the method public static int compare */
\r
978 public void TestIDNACompare() {
\r
979 // Testing the method public static int compare(String s1, String s2, int options)
\r
981 IDNA.compare((String) null, (String) null, 0);
\r
982 errln("IDNA.compare((String)null,(String)null) was suppose to return an exception.");
\r
983 } catch (Exception e) {
\r
987 IDNA.compare((String) null, "dummy", 0);
\r
988 errln("IDNA.compare((String)null,'dummy') was suppose to return an exception.");
\r
989 } catch (Exception e) {
\r
993 IDNA.compare("dummy", (String) null, 0);
\r
994 errln("IDNA.compare('dummy',(String)null) was suppose to return an exception.");
\r
995 } catch (Exception e) {
\r
999 if (IDNA.compare("dummy", "dummy", 0) != 0) {
\r
1000 errln("IDNA.compare('dummy','dummy') was suppose to return a 0.");
\r
1002 } catch (Exception e) {
\r
1003 errln("IDNA.compare('dummy','dummy') was not suppose to return an exception.");
\r
1006 // Testing the method public static int compare(StringBuffer s1, StringBuffer s2, int options)
\r
1008 IDNA.compare((StringBuffer) null, (StringBuffer) null, 0);
\r
1009 errln("IDNA.compare((StringBuffer)null,(StringBuffer)null) was suppose to return an exception.");
\r
1010 } catch (Exception e) {
\r
1014 IDNA.compare((StringBuffer) null, new StringBuffer("dummy"), 0);
\r
1015 errln("IDNA.compare((StringBuffer)null,'dummy') was suppose to return an exception.");
\r
1016 } catch (Exception e) {
\r
1020 IDNA.compare(new StringBuffer("dummy"), (StringBuffer) null, 0);
\r
1021 errln("IDNA.compare('dummy',(StringBuffer)null) was suppose to return an exception.");
\r
1022 } catch (Exception e) {
\r
1026 if (IDNA.compare(new StringBuffer("dummy"), new StringBuffer("dummy"), 0) != 0) {
\r
1027 errln("IDNA.compare(new StringBuffer('dummy'),new StringBuffer('dummy')) was suppose to return a 0.");
\r
1029 } catch (Exception e) {
\r
1030 errln("IDNA.compare(new StringBuffer('dummy'),new StringBuffer('dummy')) was not suppose to return an exception.");
\r
1033 // Testing the method public static int compare(UCharacterIterator s1, UCharacterIterator s2, int options)
\r
1034 UCharacterIterator uci = UCharacterIterator.getInstance("dummy");
\r
1036 IDNA.compare((UCharacterIterator) null, (UCharacterIterator) null, 0);
\r
1037 errln("IDNA.compare((UCharacterIterator)null,(UCharacterIterator)null) was suppose to return an exception.");
\r
1038 } catch (Exception e) {
\r
1042 IDNA.compare((UCharacterIterator) null, uci, 0);
\r
1043 errln("IDNA.compare((UCharacterIterator)null,UCharacterIterator) was suppose to return an exception.");
\r
1044 } catch (Exception e) {
\r
1048 IDNA.compare(uci, (UCharacterIterator) null, 0);
\r
1049 errln("IDNA.compare(UCharacterIterator,(UCharacterIterator)null) was suppose to return an exception.");
\r
1050 } catch (Exception e) {
\r
1054 if (IDNA.compare(uci, uci, 0) != 0) {
\r
1055 errln("IDNA.compare(UCharacterIterator('dummy'),UCharacterIterator('dummy')) was suppose to return a 0.");
\r
1057 } catch (Exception e) {
\r
1058 errln("IDNA.compare(UCharacterIterator('dummy'),UCharacterIterator('dummy')) was not suppose to return an exception.");
\r