2 *******************************************************************************
\r
3 * Copyright (C) 2003-2009, 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
710 * test Unicode 3.2 normalization, before Public Review Issue #29
\r
711 * see cnormtst.c TestComposition()
\r
713 final String strings[]={
\r
714 "\u1100\u0300\u1161\u0327",
\r
715 "\u0b47\u0300\u0b3e\u0327"
\r
718 String ascii = null, unicode = null;
\r
721 for(i=0; i<strings.length; ++i) {
\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
729 if(unicode == null || unicode.compareTo(ascii) != 0) {
\r
730 String uc = unicode == null? "(null)" : unicode;
\r
732 errln("string " + i + " yields " + uc +" instead of " + ascii);
\r
736 public void TestJB4490(){
\r
737 String[] in = new String[]{
\r
738 "\u00F5\u00dE\u00dF\u00dD",
\r
741 for ( int i=0; i< in.length; i++){
\r
743 String ascii = IDNA.convertToASCII(in[i],IDNA.DEFAULT).toString();
\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
750 }catch(StringPrepParseException ex){
\r
751 errln("Unexpected exception for convertToASCII: " + ex.getMessage());
\r
755 public void TestJB4475(){
\r
756 String[] in = new String[]{
\r
760 for ( int i=0; i< in.length; i++){
\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
767 }catch(StringPrepParseException ex){
\r
768 errln("Unexpected exception: " + ex.getMessage());
\r
774 public void TestDebug(){
\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
781 }catch(StringPrepParseException ex){
\r
782 logln("Unexpected exception: " + ex.getMessage());
\r
785 String ascii = IDNA.convertToASCII("\u00AD",IDNA.DEFAULT).toString();
\r
787 errln("Did not get the expected exception");
\r
789 }catch(StringPrepParseException ex){
\r
790 logln("Got the expected exception: " + ex.getMessage());
\r
793 public void TestJB5273(){
\r
794 String INVALID_DOMAIN_NAME = "xn--m\u00FCller.de";
\r
796 IDNA.convertIDNToUnicode(INVALID_DOMAIN_NAME, IDNA.DEFAULT);
\r
797 IDNA.convertIDNToUnicode(INVALID_DOMAIN_NAME, IDNA.USE_STD3_RULES);
\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
805 String domain = "xn--m\u00FCller.de";
\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
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
821 IDNA.convertToUnicode("xn--m\u00FCller", IDNA.DEFAULT);
\r
822 }catch(Exception ex){
\r
823 errln("ToUnicode operation failed! "+ex.getMessage());
\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
831 IDNA.convertIDNToUnicode("xn--m\u1234ller", IDNA.USE_STD3_RULES);
\r
832 }catch(StringPrepParseException ex){
\r
833 errln("ToUnicode operation failed! "+ex.getMessage());
\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
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
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
864 logln("IDNA.convertToASCII(ul, IDNA.DEFAULT) Succeeded");
\r
868 IDNA.convertToASCII(ul1, IDNA.DEFAULT);
\r
869 }catch (StringPrepParseException ex){
\r
870 errln("IDNA.convertToASCII failed with error: "+ex.toString());
\r
873 IDNA.convertToUnicode(ul1, IDNA.DEFAULT);
\r
874 }catch (StringPrepParseException ex){
\r
875 errln("IDNA.convertToASCII failed with error: "+ex.toString());
\r
878 IDNA.convertToUnicode(ul, IDNA.DEFAULT);
\r
879 }catch (StringPrepParseException ex){
\r
880 errln("IDNA.convertToASCII failed with error: "+ex.toString());
\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
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
891 logln("IDNA.convertToASCII(idn, IDNA.DEFAULT) Succeeded");
\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
901 logln("IDNA.convertToUnicode(idn, IDNA.DEFAULT) Succeeded");
\r