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