Changeset 17374 in josm for trunk/test/unit/org/openstreetmap/josm/data/validation
- Timestamp:
- 2020-11-29T10:33:58+01:00 (4 years ago)
- Location:
- trunk/test/unit/org/openstreetmap/josm/data/validation/routines
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/data/validation/routines/DomainValidatorTestIT.java
r17275 r17374 348 348 private static boolean isInIanaList(String name, String[] array, Set<String> ianaTlds) { 349 349 boolean ok = true; 350 for ( int i = 0; i < array.length; i++) {351 if (!ianaTlds.contains( array[i])) {352 Logging.error(name + " contains unexpected value: " + array[i]);350 for (String element : array) { 351 if (!ianaTlds.contains(element)) { 352 Logging.error(name + " contains unexpected value: " + element); 353 353 ok = false; 354 354 } -
trunk/test/unit/org/openstreetmap/josm/data/validation/routines/EmailValidatorTest.java
r17275 r17374 493 493 @Test 494 494 void testEmailFromPerl() { 495 for ( int index = 0; index <testEmailFromPerl.length; index++) {496 String item = testEmailFromPerl[index].item;497 if ( testEmailFromPerl[index].valid) {498 assertTrue("Should be OK: " +item, validator.isValid(item));495 for (ResultPair resultPair : testEmailFromPerl) { 496 String item = resultPair.item; 497 if (resultPair.valid) { 498 assertTrue("Should be OK: " + item, validator.isValid(item)); 499 499 } else { 500 assertFalse("Should fail: " +item, validator.isValid(item));500 assertFalse("Should fail: " + item, validator.isValid(item)); 501 501 } 502 502 } -
trunk/test/unit/org/openstreetmap/josm/data/validation/routines/UrlValidatorTest.java
r17275 r17374 31 31 class UrlValidatorTest { 32 32 33 private static final boolean printStatus = false; 34 private static final boolean printIndex = false; //print index that indicates current scheme,host,port,path, query test were using. 35 36 /** 37 * Setup 38 */ 39 @BeforeEach 40 public void setUp() { 41 for (int index = 0; index < testPartsIndex.length - 1; index++) { 42 testPartsIndex[index] = 0; 43 } 44 } 45 46 /** 47 * Test is valid 48 */ 49 @Test 50 public void testIsValid() { 33 private static final boolean printStatus = false; 34 private static final boolean printIndex = false; //print index that indicates current scheme,host,port,path, query test were using. 35 36 /** 37 * Setup 38 */ 39 @BeforeEach 40 public void setUp() { 41 for (int index = 0; index < testPartsIndex.length - 1; index++) { 42 testPartsIndex[index] = 0; 43 } 44 } 45 46 /** 47 * Test is valid 48 */ 49 @Test 50 public void testIsValid() { 51 51 testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES); 52 52 setUp(); 53 53 long options = 54 UrlValidator.ALLOW_2_SLASHES 55 + UrlValidator.ALLOW_ALL_SCHEMES 56 + UrlValidator.NO_FRAGMENTS; 54 UrlValidator.ALLOW_2_SLASHES 55 + UrlValidator.ALLOW_ALL_SCHEMES 56 + UrlValidator.NO_FRAGMENTS; 57 57 58 58 testIsValid(testUrlPartsOptions, options); 59 } 60 61 /** 62 * Test is valid scheme 63 */ 64 @Test 65 public void testIsValidScheme() { 66 if (printStatus) { 67 System.out.print("\n testIsValidScheme() "); 68 } 69 //UrlValidator urlVal = new UrlValidator(schemes,false,false,false); 70 UrlValidator urlVal = new UrlValidator(schemes, 0); 71 for (int sIndex = 0; sIndex <testScheme.length; sIndex++) {72 ResultPairtestPair= testScheme[sIndex];73 boolean result = urlVal.isValidScheme(testPair.item);74 assertEquals(testPair.item, testPair.valid, result);75 if (printStatus) {76 if (result == testPair.valid) {77 System.out.print('.');78 } else {79 System.out.print('X');59 } 60 61 /** 62 * Test is valid scheme 63 */ 64 @Test 65 public void testIsValidScheme() { 66 if (printStatus) { 67 System.out.print("\n testIsValidScheme() "); 68 } 69 //UrlValidator urlVal = new UrlValidator(schemes,false,false,false); 70 UrlValidator urlVal = new UrlValidator(schemes, 0); 71 for (ResultPair testPair : testScheme) { 72 boolean result = urlVal.isValidScheme(testPair.item); 73 assertEquals(testPair.item, testPair.valid, result); 74 if (printStatus) { 75 if (result == testPair.valid) { 76 System.out.print('.'); 77 } else { 78 System.out.print('X'); 79 } 80 80 } 81 } 82 } 83 if (printStatus) { 84 System.out.println(); 85 } 86 } 87 88 /** 89 * Create set of tests by taking the testUrlXXX arrays and 90 * running through all possible permutations of their combinations. 91 * 92 * @param testObjects Used to create a url. 93 * @param options options 94 */ 95 private void testIsValid(Object[] testObjects, long options) { 96 UrlValidator urlVal = new UrlValidator(null, null, options); 97 assertTrue(urlVal.isValid("http://www.google.com")); 98 assertTrue(urlVal.isValid("http://www.google.com/")); 99 int statusPerLine = 60; 100 int printed = 0; 101 if (printIndex) { 102 statusPerLine = 6; 103 } 104 do { 105 StringBuilder testBuffer = new StringBuilder(); 106 boolean expected = true; 107 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) { 108 int index = testPartsIndex[testPartsIndexIndex]; 109 ResultPair[] part = (ResultPair[]) testObjects[testPartsIndexIndex]; 110 testBuffer.append(part[index].item); 111 expected &= part[index].valid; 112 } 113 String url = testBuffer.toString(); 114 boolean result = urlVal.isValid(url); 115 assertEquals(url, expected, result); 116 if (printStatus) { 117 if (printIndex) { 118 System.out.print(testPartsIndextoString()); 119 } else { 120 if (result == expected) { 121 System.out.print('.'); 122 } else { 123 System.out.print('X'); 124 } 81 } 82 if (printStatus) { 83 System.out.println(); 84 } 85 } 86 87 /** 88 * Create set of tests by taking the testUrlXXX arrays and 89 * running through all possible permutations of their combinations. 90 * 91 * @param testObjects Used to create a url. 92 * @param options options 93 */ 94 private void testIsValid(Object[] testObjects, long options) { 95 UrlValidator urlVal = new UrlValidator(null, null, options); 96 assertTrue(urlVal.isValid("http://www.google.com")); 97 assertTrue(urlVal.isValid("http://www.google.com/")); 98 int statusPerLine = 60; 99 int printed = 0; 100 if (printIndex) { 101 statusPerLine = 6; 102 } 103 do { 104 StringBuilder testBuffer = new StringBuilder(); 105 boolean expected = true; 106 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) { 107 int index = testPartsIndex[testPartsIndexIndex]; 108 ResultPair[] part = (ResultPair[]) testObjects[testPartsIndexIndex]; 109 testBuffer.append(part[index].item); 110 expected &= part[index].valid; 125 111 } 126 printed++; 127 if (printed == statusPerLine) { 128 System.out.println(); 129 printed = 0; 112 String url = testBuffer.toString(); 113 boolean result = urlVal.isValid(url); 114 assertEquals(url, expected, result); 115 if (printStatus) { 116 if (printIndex) { 117 System.out.print(testPartsIndextoString()); 118 } else { 119 if (result == expected) { 120 System.out.print('.'); 121 } else { 122 System.out.print('X'); 123 } 124 } 125 printed++; 126 if (printed == statusPerLine) { 127 System.out.println(); 128 printed = 0; 129 } 130 130 } 131 } 132 } while (incrementTestPartsIndex(testPartsIndex, testObjects)); 133 if (printStatus) { 134 System.out.println(); 135 } 136 } 131 } while (incrementTestPartsIndex(testPartsIndex, testObjects)); 132 if (printStatus) { 133 System.out.println(); 134 } 135 } 137 136 138 137 /** … … 144 143 UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS); 145 144 assertTrue(urlValidator.isValid( 146 "http://l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.org")); 145 "http://l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.org")); 147 146 } 148 147 … … 164 163 UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES); 165 164 assertTrue("parentheses should be valid in URLs", 166 validator.isValid("http://somewhere.com/pathxyz/file(1).html")); 165 validator.isValid("http://somewhere.com/pathxyz/file(1).html")); 167 166 } 168 167 … … 191 190 @Test 192 191 void testValidator248() { 193 RegexValidator regex = new RegexValidator( new String[] {"localhost", ".*\\.my-testing"});192 RegexValidator regex = new RegexValidator("localhost", ".*\\.my-testing"); 194 193 UrlValidator validator = new UrlValidator(regex, 0); 195 194 … … 211 210 212 211 assertTrue("localhost URL should validate", 213 validator.isValid("http://localhost/test/index.html")); 212 validator.isValid("http://localhost/test/index.html")); 214 213 215 214 assertTrue("machinename URL should validate", 216 validator.isValid("http://machinename/test/index.html")); 215 validator.isValid("http://machinename/test/index.html")); 217 216 218 217 assertTrue("www.apache.org should still validate", 219 validator.isValid("http://www.apache.org/test/index.html")); 218 validator.isValid("http://www.apache.org/test/index.html")); 220 219 } 221 220 … … 267 266 268 267 assertTrue("http://apache.org/ should be allowed by default", 269 268 validator.isValid("http://www.apache.org/test/index.html")); 270 269 271 270 assertFalse("file:///c:/ shouldn't be allowed by default", 272 271 validator.isValid("file:///C:/some.file")); 273 272 274 273 assertFalse("file:///c:\\ shouldn't be allowed by default", 275 validator.isValid("file:///C:\\some.file")); 274 validator.isValid("file:///C:\\some.file")); 276 275 277 276 assertFalse("file:///etc/ shouldn't be allowed by default", 278 validator.isValid("file:///etc/hosts")); 277 validator.isValid("file:///etc/hosts")); 279 278 280 279 assertFalse("file://localhost/etc/ shouldn't be allowed by default", 281 validator.isValid("file://localhost/etc/hosts")); 280 validator.isValid("file://localhost/etc/hosts")); 282 281 283 282 assertFalse("file://localhost/c:/ shouldn't be allowed by default", 284 validator.isValid("file://localhost/c:/some.file")); 283 validator.isValid("file://localhost/c:/some.file")); 285 284 286 285 // Turn it on, and check 287 286 // Note - we need to enable local urls when working with file: 288 validator = new UrlValidator(new String[] 287 validator = new UrlValidator(new String[]{"http", "file"}, UrlValidator.ALLOW_LOCAL_URLS); 289 288 290 289 assertTrue("http://apache.org/ should be allowed by default", 291 290 validator.isValid("http://www.apache.org/test/index.html")); 292 291 293 292 assertTrue("file:///c:/ should now be allowed", 294 293 validator.isValid("file:///C:/some.file")); 295 294 296 295 // Currently, we don't support the c:\ form 297 296 assertFalse("file:///c:\\ shouldn't be allowed", 298 validator.isValid("file:///C:\\some.file")); 297 validator.isValid("file:///C:\\some.file")); 299 298 300 299 assertTrue("file:///etc/ should now be allowed", 301 validator.isValid("file:///etc/hosts")); 300 validator.isValid("file:///etc/hosts")); 302 301 303 302 assertTrue("file://localhost/etc/ should now be allowed", 304 validator.isValid("file://localhost/etc/hosts")); 303 validator.isValid("file://localhost/etc/hosts")); 305 304 306 305 assertTrue("file://localhost/c:/ should now be allowed", 307 validator.isValid("file://localhost/c:/some.file")); 306 validator.isValid("file://localhost/c:/some.file")); 308 307 309 308 // These are never valid 310 309 assertFalse("file://c:/ shouldn't ever be allowed, needs file:///c:/", 311 validator.isValid("file://C:/some.file")); 310 validator.isValid("file://C:/some.file")); 312 311 313 312 assertFalse("file://c:\\ shouldn't ever be allowed, needs file:///c:/", 314 validator.isValid("file://C:\\some.file")); 313 validator.isValid("file://C:\\some.file")); 315 314 } 316 315 … … 324 323 assertTrue(urlValidator.isValid("hTtP://sample.ondemand.CoM/")); 325 324 assertTrue(urlValidator.isValid("httpS://SAMPLE.ONEMAND.COM/")); 326 urlValidator = new UrlValidator( new String[] {"HTTP", "HTTPS"});325 urlValidator = new UrlValidator("HTTP", "HTTPS"); 327 326 assertTrue(urlValidator.isValid("http://sample.ondemand.com/")); 328 327 assertTrue(urlValidator.isValid("hTtP://sample.ondemand.CoM/")); … … 367 366 368 367 static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) { 369 boolean carry = true; //add 1 to lowest order part. 370 boolean maxIndex = true; 371 for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) { 372 int index = testPartsIndex[testPartsIndexIndex]; 373 ResultPair[] part = (ResultPair[]) testParts[testPartsIndexIndex]; 374 if (carry) { 375 if (index < part.length - 1) { 376 index++; 377 testPartsIndex[testPartsIndexIndex] = index; 378 carry = false; 368 boolean carry = true; //add 1 to lowest order part. 369 boolean maxIndex = true; 370 for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) { 371 int index = testPartsIndex[testPartsIndexIndex]; 372 ResultPair[] part = (ResultPair[]) testParts[testPartsIndexIndex]; 373 if (carry) { 374 if (index < part.length - 1) { 375 index++; 376 testPartsIndex[testPartsIndexIndex] = index; 377 carry = false; 378 } else { 379 testPartsIndex[testPartsIndexIndex] = 0; 380 carry = true; 381 } 382 } 383 maxIndex &= (index == (part.length - 1)); 384 } 385 386 return (!maxIndex); 387 } 388 389 private String testPartsIndextoString() { 390 StringBuilder carryMsg = new StringBuilder("{"); 391 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) { 392 carryMsg.append(testPartsIndex[testPartsIndexIndex]); 393 if (testPartsIndexIndex < testPartsIndex.length - 1) { 394 carryMsg.append(','); 379 395 } else { 380 testPartsIndex[testPartsIndexIndex] = 0; 381 carry = true; 396 carryMsg.append('}'); 382 397 } 383 } 384 maxIndex &= (index == (part.length - 1)); 385 } 386 387 return (!maxIndex); 388 } 389 390 private String testPartsIndextoString() { 391 StringBuilder carryMsg = new StringBuilder("{"); 392 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) { 393 carryMsg.append(testPartsIndex[testPartsIndexIndex]); 394 if (testPartsIndexIndex < testPartsIndex.length - 1) { 395 carryMsg.append(','); 396 } else { 397 carryMsg.append('}'); 398 } 399 } 400 return carryMsg.toString(); 401 } 402 403 /** 404 * Non-regression test for VALIDATOR-290 405 */ 406 @Test 407 public void testValidator290() { 398 } 399 return carryMsg.toString(); 400 } 401 402 /** 403 * Non-regression test for VALIDATOR-290 404 */ 405 @Test 406 public void testValidator290() { 408 407 UrlValidator validator = new UrlValidator(); 409 408 assertTrue(validator.isValid("http://xn--h1acbxfam.idn.icann.org/")); … … 442 441 } 443 442 444 /** 445 * Non-regression test for VALIDATOR-361 446 */ 447 @Test 448 public void testValidator361() { 449 UrlValidator validator = new UrlValidator(); 450 assertTrue(validator.isValid("http://hello.tokyo/")); 451 } 452 453 /** 454 * Non-regression test for VALIDATOR-363 455 */ 456 @Test 457 public void testValidator363() { 443 /** 444 * Non-regression test for VALIDATOR-361 445 */ 446 @Test 447 public void testValidator361() { 448 UrlValidator validator = new UrlValidator(); 449 assertTrue(validator.isValid("http://hello.tokyo/")); 450 } 451 452 /** 453 * Non-regression test for VALIDATOR-363 454 */ 455 @Test 456 public void testValidator363() { 458 457 UrlValidator urlValidator = new UrlValidator(); 459 458 assertTrue(urlValidator.isValid("http://www.example.org/a/b/hello..world")); … … 475 474 } 476 475 477 /** 478 * Non-regression test for VALIDATOR-375 479 */ 480 @Test 481 public void testValidator375() { 482 UrlValidator validator = new UrlValidator(); 483 String url = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html"; 484 assertTrue("IPv6 address URL should validate: " + url, validator.isValid(url)); 485 url = "http://[::1]:80/index.html"; 486 assertTrue("IPv6 address URL should validate: " + url, validator.isValid(url)); 487 url = "http://FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:80/index.html"; 488 assertFalse("IPv6 address without [] should not validate: " + url, validator.isValid(url)); 489 } 490 491 /** 492 * Non-regression test for VALIDATOR-353 493 */ 494 @Test 495 public void testValidator353() { // userinfo 496 UrlValidator validator = new UrlValidator(); 497 assertTrue(validator.isValid("http://www.apache.org:80/path")); 498 assertTrue(validator.isValid("http://user:pass@www.apache.org:80/path")); 499 assertTrue(validator.isValid("http://user:@www.apache.org:80/path")); 500 assertTrue(validator.isValid("http://us%00er:-._~!$&'()*+,;=@www.apache.org:80/path")); 501 assertFalse(validator.isValid("http://:pass@www.apache.org:80/path")); 502 assertFalse(validator.isValid("http://:@www.apache.org:80/path")); 503 assertFalse(validator.isValid("http://user:pa:ss@www.apache.org/path")); 504 assertFalse(validator.isValid("http://user:pa@ss@www.apache.org/path")); 505 } 506 507 /** 508 * Non-regression test for VALIDATOR-382 509 */ 510 @Test 511 public void testValidator382() { 512 UrlValidator validator = new UrlValidator(); 513 assertTrue(validator.isValid("ftp://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose")); 514 } 515 516 /** 517 * Non-regression test for VALIDATOR-380 518 */ 519 @Test 520 public void testValidator380() { 521 UrlValidator validator = new UrlValidator(); 522 assertTrue(validator.isValid("http://www.apache.org:80/path")); 523 assertTrue(validator.isValid("http://www.apache.org:8/path")); 524 assertTrue(validator.isValid("http://www.apache.org:/path")); 525 } 526 527 /** 528 * Unit test of {@link UrlValidator#getValidatorName}. 529 */ 530 @Test 531 public void testValidatorName() { 532 assertEquals("URL validator", UrlValidator.getInstance().getValidatorName()); 533 } 534 535 //-------------------- Test data for creating a composite URL 536 /** 537 * The data given below approximates the 4 parts of a URL 538 * {@code <scheme>://<authority><path>?<query>} except that the port number 539 * is broken out of authority to increase the number of permutations. 540 * A complete URL is composed of a scheme+authority+port+path+query, 541 * all of which must be individually valid for the entire URL to be considered 542 * valid. 543 */ 544 ResultPair[] testUrlScheme = {new ResultPair("http://", true), 545 new ResultPair("ftp://", true), 546 new ResultPair("h3t://", true), 547 new ResultPair("3ht://", false), 548 new ResultPair("http:/", false), 549 new ResultPair("http:", false), 550 new ResultPair("http/", false), 551 new ResultPair("://", false), 552 new ResultPair("", true)}; 553 554 ResultPair[] testUrlAuthority = {new ResultPair("www.google.com", true), 555 new ResultPair("go.com", true), 556 new ResultPair("go.au", true), 557 new ResultPair("0.0.0.0", true), 558 new ResultPair("255.255.255.255", true), 559 new ResultPair("256.256.256.256", false), 560 new ResultPair("255.com", true), 561 new ResultPair("1.2.3.4.5", false), 562 new ResultPair("1.2.3.4.", false), 563 new ResultPair("1.2.3", false), 564 new ResultPair(".1.2.3.4", false), 565 new ResultPair("go.a", false), 566 new ResultPair("go.a1a", false), 567 new ResultPair("go.cc", true), 568 new ResultPair("go.1aa", false), 569 new ResultPair("aaa.", false), 570 new ResultPair(".aaa", false), 571 new ResultPair("aaa", false), 572 new ResultPair("", false) 573 }; 574 ResultPair[] testUrlPort = {new ResultPair(":80", true), 575 new ResultPair(":65535", true), 576 new ResultPair(":0", true), 577 new ResultPair("", true), 578 new ResultPair(":-1", false), 579 new ResultPair(":65636", true), 580 new ResultPair(":65a", false) 581 }; 582 ResultPair[] testPath = {new ResultPair("/test1", true), 583 new ResultPair("/t123", true), 584 new ResultPair("/$23", true), 585 new ResultPair("/..", false), 586 new ResultPair("/../", false), 587 new ResultPair("/test1/", true), 588 new ResultPair("", true), 589 new ResultPair("/test1/file", true), 590 new ResultPair("/..//file", false), 591 new ResultPair("/test1//file", false) 592 }; 593 //Test allow2slash, noFragment 594 ResultPair[] testUrlPathOptions = {new ResultPair("/test1", true), 595 new ResultPair("/t123", true), 596 new ResultPair("/$23", true), 597 new ResultPair("/..", false), 598 new ResultPair("/../", false), 599 new ResultPair("/test1/", true), 600 new ResultPair("/#", false), 601 new ResultPair("", true), 602 new ResultPair("/test1/file", true), 603 new ResultPair("/t123/file", true), 604 new ResultPair("/$23/file", true), 605 new ResultPair("/../file", false), 606 new ResultPair("/..//file", false), 607 new ResultPair("/test1//file", true), 608 new ResultPair("/#/file", false) 609 }; 610 611 ResultPair[] testUrlQuery = {new ResultPair("?action=view", true), 612 new ResultPair("?action=edit&mode=up", true), 613 new ResultPair("", true) 614 }; 615 616 Object[] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery}; 617 Object[] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery}; 618 int[] testPartsIndex = {0, 0, 0, 0, 0}; 619 620 //---------------- Test data for individual url parts ---------------- 621 private final String[] schemes = {"http", "gopher", "g0-To+.", 622 "not_valid" // TODO this will need to be dropped if the ctor validates schemes 623 }; 624 625 ResultPair[] testScheme = {new ResultPair("http", true), 626 new ResultPair("ftp", false), 627 new ResultPair("httpd", false), 628 new ResultPair("gopher", true), 629 new ResultPair("g0-to+.", true), 630 new ResultPair("not_valid", false), // underscore not allowed 631 new ResultPair("HtTp", true), 632 new ResultPair("telnet", false)}; 476 /** 477 * Non-regression test for VALIDATOR-375 478 */ 479 @Test 480 public void testValidator375() { 481 UrlValidator validator = new UrlValidator(); 482 String url = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html"; 483 assertTrue("IPv6 address URL should validate: " + url, validator.isValid(url)); 484 url = "http://[::1]:80/index.html"; 485 assertTrue("IPv6 address URL should validate: " + url, validator.isValid(url)); 486 url = "http://FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:80/index.html"; 487 assertFalse("IPv6 address without [] should not validate: " + url, validator.isValid(url)); 488 } 489 490 /** 491 * Non-regression test for VALIDATOR-353 492 */ 493 @Test 494 public void testValidator353() { // userinfo 495 UrlValidator validator = new UrlValidator(); 496 assertTrue(validator.isValid("http://www.apache.org:80/path")); 497 assertTrue(validator.isValid("http://user:pass@www.apache.org:80/path")); 498 assertTrue(validator.isValid("http://user:@www.apache.org:80/path")); 499 assertTrue(validator.isValid("http://us%00er:-._~!$&'()*+,;=@www.apache.org:80/path")); 500 assertFalse(validator.isValid("http://:pass@www.apache.org:80/path")); 501 assertFalse(validator.isValid("http://:@www.apache.org:80/path")); 502 assertFalse(validator.isValid("http://user:pa:ss@www.apache.org/path")); 503 assertFalse(validator.isValid("http://user:pa@ss@www.apache.org/path")); 504 } 505 506 /** 507 * Non-regression test for VALIDATOR-382 508 */ 509 @Test 510 public void testValidator382() { 511 UrlValidator validator = new UrlValidator(); 512 assertTrue(validator.isValid("ftp://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose")); 513 } 514 515 /** 516 * Non-regression test for VALIDATOR-380 517 */ 518 @Test 519 public void testValidator380() { 520 UrlValidator validator = new UrlValidator(); 521 assertTrue(validator.isValid("http://www.apache.org:80/path")); 522 assertTrue(validator.isValid("http://www.apache.org:8/path")); 523 assertTrue(validator.isValid("http://www.apache.org:/path")); 524 } 525 526 /** 527 * Unit test of {@link UrlValidator#getValidatorName}. 528 */ 529 @Test 530 public void testValidatorName() { 531 assertEquals("URL validator", UrlValidator.getInstance().getValidatorName()); 532 } 533 534 //-------------------- Test data for creating a composite URL 535 /** 536 * The data given below approximates the 4 parts of a URL 537 * {@code <scheme>://<authority><path>?<query>} except that the port number 538 * is broken out of authority to increase the number of permutations. 539 * A complete URL is composed of a scheme+authority+port+path+query, 540 * all of which must be individually valid for the entire URL to be considered 541 * valid. 542 */ 543 ResultPair[] testUrlScheme = { 544 new ResultPair("http://", true), 545 new ResultPair("ftp://", true), 546 new ResultPair("h3t://", true), 547 new ResultPair("3ht://", false), 548 new ResultPair("http:/", false), 549 new ResultPair("http:", false), 550 new ResultPair("http/", false), 551 new ResultPair("://", false), 552 new ResultPair("", true) 553 }; 554 555 ResultPair[] testUrlAuthority = { 556 new ResultPair("www.google.com", true), 557 new ResultPair("go.com", true), 558 new ResultPair("go.au", true), 559 new ResultPair("0.0.0.0", true), 560 new ResultPair("255.255.255.255", true), 561 new ResultPair("256.256.256.256", false), 562 new ResultPair("255.com", true), 563 new ResultPair("1.2.3.4.5", false), 564 new ResultPair("1.2.3.4.", false), 565 new ResultPair("1.2.3", false), 566 new ResultPair(".1.2.3.4", false), 567 new ResultPair("go.a", false), 568 new ResultPair("go.a1a", false), 569 new ResultPair("go.cc", true), 570 new ResultPair("go.1aa", false), 571 new ResultPair("aaa.", false), 572 new ResultPair(".aaa", false), 573 new ResultPair("aaa", false), 574 new ResultPair("", false) 575 }; 576 577 ResultPair[] testUrlPort = { 578 new ResultPair(":80", true), 579 new ResultPair(":65535", true), 580 new ResultPair(":0", true), 581 new ResultPair("", true), 582 new ResultPair(":-1", false), 583 new ResultPair(":65636", true), 584 new ResultPair(":65a", false) 585 }; 586 587 ResultPair[] testPath = { 588 new ResultPair("/test1", true), 589 new ResultPair("/t123", true), 590 new ResultPair("/$23", true), 591 new ResultPair("/..", false), 592 new ResultPair("/../", false), 593 new ResultPair("/test1/", true), 594 new ResultPair("", true), 595 new ResultPair("/test1/file", true), 596 new ResultPair("/..//file", false), 597 new ResultPair("/test1//file", false) 598 }; 599 600 //Test allow2slash, noFragment 601 ResultPair[] testUrlPathOptions = { 602 new ResultPair("/test1", true), 603 new ResultPair("/t123", true), 604 new ResultPair("/$23", true), 605 new ResultPair("/..", false), 606 new ResultPair("/../", false), 607 new ResultPair("/test1/", true), 608 new ResultPair("/#", false), 609 new ResultPair("", true), 610 new ResultPair("/test1/file", true), 611 new ResultPair("/t123/file", true), 612 new ResultPair("/$23/file", true), 613 new ResultPair("/../file", false), 614 new ResultPair("/..//file", false), 615 new ResultPair("/test1//file", true), 616 new ResultPair("/#/file", false) 617 }; 618 619 ResultPair[] testUrlQuery = { 620 new ResultPair("?action=view", true), 621 new ResultPair("?action=edit&mode=up", true), 622 new ResultPair("", true) 623 }; 624 625 Object[] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery}; 626 Object[] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery}; 627 int[] testPartsIndex = {0, 0, 0, 0, 0}; 628 629 //---------------- Test data for individual url parts ---------------- 630 private final String[] schemes = { 631 "http", 632 "gopher", 633 "g0-To+.", 634 "not_valid" // TODO this will need to be dropped if the ctor validates schemes 635 }; 636 637 ResultPair[] testScheme = { 638 new ResultPair("http", true), 639 new ResultPair("ftp", false), 640 new ResultPair("httpd", false), 641 new ResultPair("gopher", true), 642 new ResultPair("g0-to+.", true), 643 new ResultPair("not_valid", false), // underscore not allowed 644 new ResultPair("HtTp", true), 645 new ResultPair("telnet", false) 646 }; 633 647 }
Note:
See TracChangeset
for help on using the changeset viewer.