View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator.routines;
18  
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.core.Is.is;
21  import static org.hamcrest.core.IsEqual.equalTo;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.net.URI;
28  import java.net.URISyntaxException;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import org.apache.commons.validator.ResultPair;
33  import org.junit.Before;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Performs Validation Test for url validations.
38   */
39  public class UrlValidatorTest {
40  
41      static boolean incrementTestPartsIndex(final int[] testPartsIndex, final Object[] testParts) {
42          boolean carry = true; // add 1 to lowest order part.
43          boolean maxIndex = true;
44          for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) {
45              int index = testPartsIndex[testPartsIndexIndex];
46              final ResultPair[] part = (ResultPair[]) testParts[testPartsIndexIndex];
47              maxIndex &= index == part.length - 1;
48              if (carry) {
49                  if (index < part.length - 1) {
50                      index++;
51                      testPartsIndex[testPartsIndexIndex] = index;
52                      carry = false;
53                  } else {
54                      testPartsIndex[testPartsIndexIndex] = 0;
55                      carry = true;
56                  }
57              }
58          }
59  
60          return !maxIndex;
61      }
62  
63      /**
64       * Validator for checking URL parsing
65       *
66       * @param args - URLs to validate
67       */
68      public static void main(final String[] args) {
69          final UrlValidator uv = new UrlValidator();
70          for (final String arg : args) {
71              try {
72                  URI uri = new URI(arg);
73                  uri = uri.normalize();
74                  System.out.println(uri.toString());
75                  System.out.printf("URI scheme: %s%n", uri.getScheme());
76                  System.out.printf("URI scheme specific part: %s%n", uri.getSchemeSpecificPart());
77                  System.out.printf("URI raw scheme specific part: %s%n", uri.getRawSchemeSpecificPart());
78                  System.out.printf("URI auth: %s%n", uri.getAuthority());
79                  System.out.printf("URI raw auth: %s%n", uri.getRawAuthority());
80                  System.out.printf("URI userInfo: %s%n", uri.getUserInfo());
81                  System.out.printf("URI raw userInfo: %s%n", uri.getRawUserInfo());
82                  System.out.printf("URI host: %s%n", uri.getHost());
83                  System.out.printf("URI port: %s%n", uri.getPort());
84                  System.out.printf("URI path: %s%n", uri.getPath());
85                  System.out.printf("URI raw path: %s%n", uri.getRawPath());
86                  System.out.printf("URI query: %s%n", uri.getQuery());
87                  System.out.printf("URI raw query: %s%n", uri.getRawQuery());
88                  System.out.printf("URI fragment: %s%n", uri.getFragment());
89                  System.out.printf("URI raw fragment: %s%n", uri.getRawFragment());
90              } catch (final URISyntaxException e) {
91                  System.out.println(e.getMessage());
92              }
93              System.out.printf("isValid: %s%n", uv.isValid(arg));
94          }
95      }
96  
97      private final boolean printStatus = false;
98  
99      private final boolean printIndex = false;// print index that indicates current scheme,host,port,path, query test were using.
100 
101     // -------------------- Test data for creating a composite URL
102     /**
103      * The data given below approximates the 4 parts of a URL <scheme>://<authority><path>?<query> except that the port number is broken out of authority to
104      * increase the number of permutations. A complete URL is composed of a scheme+authority+port+path+query, all of which must be individually valid for the
105      * entire URL to be considered valid.
106      */
107     ResultPair[] testUrlScheme = { new ResultPair("http://", true), new ResultPair("ftp://", true), new ResultPair("h3t://", true),
108             new ResultPair("3ht://", false), new ResultPair("http:/", false), new ResultPair("http:", false), new ResultPair("http/", false),
109             new ResultPair("://", false) };
110 
111     ResultPair[] testUrlAuthority = { new ResultPair("www.google.com", true), new ResultPair("www.google.com.", true), new ResultPair("go.com", true),
112             new ResultPair("go.au", true), new ResultPair("0.0.0.0", true), new ResultPair("255.255.255.255", true), new ResultPair("256.256.256.256", false),
113             new ResultPair("255.com", true), new ResultPair("1.2.3.4.5", false), new ResultPair("1.2.3.4.", false), new ResultPair("1.2.3", false),
114             new ResultPair(".1.2.3.4", false), new ResultPair("go.a", false), new ResultPair("go.a1a", false), new ResultPair("go.cc", true),
115             new ResultPair("go.1aa", false), new ResultPair("aaa.", false), new ResultPair(".aaa", false), new ResultPair("aaa", false),
116             new ResultPair("", false) };
117 
118     ResultPair[] testUrlPort = { new ResultPair(":80", true), new ResultPair(":65535", true), // max possible
119             new ResultPair(":65536", false), // max possible +1
120             new ResultPair(":0", true), new ResultPair("", true), new ResultPair(":-1", false), new ResultPair(":65636", false),
121             new ResultPair(":999999999999999999", false), new ResultPair(":65a", false) };
122 
123     ResultPair[] testPath = { new ResultPair("/test1", true), new ResultPair("/t123", true), new ResultPair("/$23", true), new ResultPair("/..", false),
124             new ResultPair("/../", false), new ResultPair("/test1/", true), new ResultPair("", true), new ResultPair("/test1/file", true),
125             new ResultPair("/..//file", false), new ResultPair("/test1//file", false) };
126 
127     // Test allow2slash, noFragment
128     ResultPair[] testUrlPathOptions = { new ResultPair("/test1", true), new ResultPair("/t123", true), new ResultPair("/$23", true),
129             new ResultPair("/..", false), new ResultPair("/../", false), new ResultPair("/test1/", true), new ResultPair("/#", false), new ResultPair("", true),
130             new ResultPair("/test1/file", true), new ResultPair("/t123/file", true), new ResultPair("/$23/file", true), new ResultPair("/../file", false),
131             new ResultPair("/..//file", false), new ResultPair("/test1//file", true), new ResultPair("/#/file", false) };
132 
133     ResultPair[] testUrlQuery = { new ResultPair("?action=view", true), new ResultPair("?action=edit&mode=up", true), new ResultPair("", true) };
134 
135     Object[] testUrlParts = { testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery };
136 
137     Object[] testUrlPartsOptions = { testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery };
138 
139     int[] testPartsIndex = { 0, 0, 0, 0, 0 };
140 
141     // ---------------- Test data for individual url parts ----------------
142     private final String[] schemes = { "http", "gopher", "g0-To+.", "not_valid" // TODO this will need to be dropped if the ctor validates schemes
143     };
144 
145     ResultPair[] testScheme = { new ResultPair("http", true), new ResultPair("ftp", false), new ResultPair("httpd", false), new ResultPair("gopher", true),
146             new ResultPair("g0-to+.", true), new ResultPair("not_valid", false), // underscore not allowed
147             new ResultPair("HtTp", true), new ResultPair("telnet", false) };
148 
149     @Before
150     public void setUp() {
151         for (int index = 0; index < testPartsIndex.length - 1; index++) {
152             testPartsIndex[index] = 0;
153         }
154     }
155 
156     @Test
157     public void testFragments() {
158         final String[] schemes = { "http", "https" };
159         UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS);
160         assertFalse(urlValidator.isValid("http://apache.org/a/b/c#frag"));
161         urlValidator = new UrlValidator(schemes);
162         assertTrue(urlValidator.isValid("http://apache.org/a/b/c#frag"));
163     }
164 
165     @Test
166     public void testIsValid() {
167         testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
168         setUp();
169         final long options = UrlValidator.ALLOW_2_SLASHES + UrlValidator.ALLOW_ALL_SCHEMES + UrlValidator.NO_FRAGMENTS;
170 
171         testIsValid(testUrlPartsOptions, options);
172     }
173 
174     /**
175      * Create set of tests by taking the testUrlXXX arrays and running through all possible permutations of their combinations.
176      *
177      * @param testObjects Used to create a url.
178      */
179     public void testIsValid(final Object[] testObjects, final long options) {
180         final UrlValidator urlVal = new UrlValidator(null, null, options);
181         assertTrue(urlVal.isValid("http://www.google.com"));
182         assertTrue(urlVal.isValid("http://www.google.com/"));
183         int statusPerLine = 60;
184         int printed = 0;
185         if (printIndex) {
186             statusPerLine = 6;
187         }
188         do {
189             final StringBuilder testBuffer = new StringBuilder();
190             boolean expected = true;
191             for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
192                 final int index = testPartsIndex[testPartsIndexIndex];
193                 final ResultPair[] part = (ResultPair[]) testObjects[testPartsIndexIndex];
194                 testBuffer.append(part[index].item);
195                 expected &= part[index].valid;
196             }
197             final String url = testBuffer.toString();
198             final boolean result = urlVal.isValid(url);
199             assertEquals(expected, result, url);
200             if (printStatus) {
201                 if (printIndex) {
202                     System.out.print(testPartsIndextoString());
203                 } else if (result == expected) {
204                     System.out.print('.');
205                 } else {
206                     System.out.print('X');
207                 }
208                 printed++;
209                 if (printed == statusPerLine) {
210                     System.out.println();
211                     printed = 0;
212                 }
213             }
214         } while (incrementTestPartsIndex(testPartsIndex, testObjects));
215         if (printStatus) {
216             System.out.println();
217         }
218     }
219 
220     @Test
221     public void testIsValidScheme() {
222         if (printStatus) {
223             System.out.print("\n testIsValidScheme() ");
224         }
225         // UrlValidator urlVal = new UrlValidator(schemes,false,false,false);
226         final UrlValidator urlVal = new UrlValidator(schemes, 0);
227         for (final ResultPair testPair : testScheme) {
228             final boolean result = urlVal.isValidScheme(testPair.item);
229             assertEquals(testPair.valid, result, testPair.item);
230             if (printStatus) {
231                 if (result == testPair.valid) {
232                     System.out.print('.');
233                 } else {
234                     System.out.print('X');
235                 }
236             }
237         }
238         if (printStatus) {
239             System.out.println();
240         }
241 
242     }
243 
244     private String testPartsIndextoString() {
245         final StringBuilder carryMsg = new StringBuilder("{");
246         for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
247             carryMsg.append(testPartsIndex[testPartsIndexIndex]);
248             if (testPartsIndexIndex < testPartsIndex.length - 1) {
249                 carryMsg.append(',');
250             } else {
251                 carryMsg.append('}');
252             }
253         }
254         return carryMsg.toString();
255 
256     }
257 
258     @Test
259     public void testValidateUrl() {
260         assertTrue(true);
261     }
262 
263     @Test
264     public void testValidator202() {
265         final String[] schemes = { "http", "https" };
266         final UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS);
267         assertTrue(urlValidator
268                 .isValid("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"));
269     }
270 
271     @Test
272     public void testValidator204() {
273         final String[] schemes = { "http", "https" };
274         final UrlValidator urlValidator = new UrlValidator(schemes);
275         assertTrue(urlValidator.isValid("http://tech.yahoo.com/rc/desktops/102;_ylt=Ao8yevQHlZ4On0O3ZJGXLEQFLZA5"));
276     }
277 
278     @Test
279     public void testValidator218() {
280         final UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
281         assertTrue(validator.isValid("http://somewhere.com/pathxyz/file(1).html"), "parentheses should be valid in URLs");
282     }
283 
284     @Test
285     public void testValidator235() {
286         final String version = System.getProperty("java.version");
287         if (version.compareTo("1.6") < 0) {
288             System.out.println("Cannot run Unicode IDN tests");
289             return; // Cannot run the test
290         }
291         final UrlValidator validator = new UrlValidator();
292         assertTrue(validator.isValid("http://xn--d1abbgf6aiiy.xn--p1ai"), "xn--d1abbgf6aiiy.xn--p1ai should validate");
293         assertTrue(validator.isValid("http://президент.рф"), "президент.рф should validate");
294         assertTrue(validator.isValid("http://www.b\u00fccher.ch"), "www.b\u00fccher.ch should validate");
295         assertFalse(validator.isValid("http://www.\uFFFD.ch"), "www.\uFFFD.ch FFFD should fail");
296         assertTrue(validator.isValid("ftp://www.b\u00fccher.ch"), "www.b\u00fccher.ch should validate");
297         assertFalse(validator.isValid("ftp://www.\uFFFD.ch"), "www.\uFFFD.ch FFFD should fail");
298     }
299 
300     @Test
301     public void testValidator248() {
302         final RegexValidator regex = new RegexValidator("localhost", ".*\\.my-testing");
303         UrlValidator validator = new UrlValidator(regex, 0);
304 
305         assertTrue(validator.isValid("http://localhost/test/index.html"), "localhost URL should validate");
306         assertTrue(validator.isValid("http://first.my-testing/test/index.html"), "first.my-testing should validate");
307         assertTrue(validator.isValid("http://sup3r.my-testing/test/index.html"), "sup3r.my-testing should validate");
308 
309         assertFalse(validator.isValid("http://broke.my-test/test/index.html"), "broke.my-test should not validate");
310 
311         assertTrue(validator.isValid("http://www.apache.org/test/index.html"), "www.apache.org should still validate");
312 
313         // Now check using options
314         validator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
315 
316         assertTrue(validator.isValid("http://localhost/test/index.html"), "localhost URL should validate");
317 
318         assertTrue(validator.isValid("http://machinename/test/index.html"), "machinename URL should validate");
319 
320         assertTrue(validator.isValid("http://www.apache.org/test/index.html"), "www.apache.org should still validate");
321     }
322 
323     @Test
324     public void testValidator276() {
325         // file:// isn't allowed by default
326         UrlValidator validator = new UrlValidator();
327 
328         assertTrue(validator.isValid("http://www.apache.org/test/index.html"), "http://apache.org/ should be allowed by default");
329 
330         assertFalse(validator.isValid("file:///C:/some.file"), "file:///c:/ shouldn't be allowed by default");
331 
332         assertFalse(validator.isValid("file:///C:\\some.file"), "file:///c:\\ shouldn't be allowed by default");
333 
334         assertFalse(validator.isValid("file:///etc/hosts"), "file:///etc/ shouldn't be allowed by default");
335 
336         assertFalse(validator.isValid("file://localhost/etc/hosts"), "file://localhost/etc/ shouldn't be allowed by default");
337 
338         assertFalse(validator.isValid("file://localhost/c:/some.file"), "file://localhost/c:/ shouldn't be allowed by default");
339 
340         // Turn it on, and check
341         // Note - we need to enable local urls when working with file:
342         validator = new UrlValidator(new String[] { "http", "file" }, UrlValidator.ALLOW_LOCAL_URLS);
343 
344         assertTrue(validator.isValid("http://www.apache.org/test/index.html"), "http://apache.org/ should be allowed by default");
345 
346         assertTrue(validator.isValid("file:///C:/some.file"), "file:///c:/ should now be allowed");
347 
348         // Only allow forward slashes
349         assertFalse(validator.isValid("file:///C:\\some.file"), "file:///c:\\ should not be allowed");
350 
351         assertTrue(validator.isValid("file:///etc/hosts"), "file:///etc/ should now be allowed");
352 
353         assertTrue(validator.isValid("file://localhost/etc/hosts"), "file://localhost/etc/ should now be allowed");
354 
355         assertTrue(validator.isValid("file://localhost/c:/some.file"), "file://localhost/c:/ should now be allowed");
356 
357         // These are never valid
358         assertFalse(validator.isValid("file://C:/some.file"), "file://c:/ shouldn't ever be allowed, needs file:///c:/");
359 
360         assertFalse(validator.isValid("file://C:\\some.file"), "file://c:\\ shouldn't ever be allowed, needs file:///c:/");
361     }
362 
363     @Test
364     public void testValidator283() {
365         final UrlValidator validator = new UrlValidator();
366         assertFalse(validator.isValid("http://finance.yahoo.com/news/Owners-54B-NY-housing-apf-2493139299.html?x=0&ap=%fr"));
367         assertTrue(validator.isValid("http://finance.yahoo.com/news/Owners-54B-NY-housing-apf-2493139299.html?x=0&ap=%22"));
368     }
369 
370     @Test
371     public void testValidator288() {
372         UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
373 
374         assertTrue(validator.isValid("http://hostname"), "hostname should validate");
375 
376         assertTrue(validator.isValid("http://hostname/test/index.html"), "hostname with path should validate");
377 
378         assertTrue(validator.isValid("http://localhost/test/index.html"), "localhost URL should validate");
379 
380         assertFalse(validator.isValid("http://first.my-testing/test/index.html"), "first.my-testing should not validate");
381 
382         assertFalse(validator.isValid("http://broke.hostname/test/index.html"), "broke.hostname should not validate");
383 
384         assertTrue(validator.isValid("http://www.apache.org/test/index.html"), "www.apache.org should still validate");
385 
386         // Turn it off, and check
387         validator = new UrlValidator(0);
388 
389         assertFalse(validator.isValid("http://hostname"), "hostname should no longer validate");
390 
391         assertFalse(validator.isValid("http://localhost/test/index.html"), "localhost URL should no longer validate");
392 
393         assertTrue(validator.isValid("http://www.apache.org/test/index.html"), "www.apache.org should still validate");
394     }
395 
396     @Test
397     public void testValidator290() {
398         final UrlValidator validator = new UrlValidator();
399         assertTrue(validator.isValid("http://xn--h1acbxfam.idn.icann.org/"));
400 //        assertTrue(validator.isValid("http://xn--e1afmkfd.xn--80akhbyknj4f"));
401         // Internationalized country code top-level domains
402         assertTrue(validator.isValid("http://test.xn--lgbbat1ad8j")); // Algeria
403         assertTrue(validator.isValid("http://test.xn--fiqs8s")); // China
404         assertTrue(validator.isValid("http://test.xn--fiqz9s")); // China
405         assertTrue(validator.isValid("http://test.xn--wgbh1c")); // Egypt
406         assertTrue(validator.isValid("http://test.xn--j6w193g")); // Hong Kong
407         assertTrue(validator.isValid("http://test.xn--h2brj9c")); // India
408         assertTrue(validator.isValid("http://test.xn--mgbbh1a71e")); // India
409         assertTrue(validator.isValid("http://test.xn--fpcrj9c3d")); // India
410         assertTrue(validator.isValid("http://test.xn--gecrj9c")); // India
411         assertTrue(validator.isValid("http://test.xn--s9brj9c")); // India
412         assertTrue(validator.isValid("http://test.xn--xkc2dl3a5ee0h")); // India
413         assertTrue(validator.isValid("http://test.xn--45brj9c")); // India
414         assertTrue(validator.isValid("http://test.xn--mgba3a4f16a")); // Iran
415         assertTrue(validator.isValid("http://test.xn--mgbayh7gpa")); // Jordan
416         assertTrue(validator.isValid("http://test.xn--mgbc0a9azcg")); // Morocco
417         assertTrue(validator.isValid("http://test.xn--ygbi2ammx")); // Palestinian Territory
418         assertTrue(validator.isValid("http://test.xn--wgbl6a")); // Qatar
419         assertTrue(validator.isValid("http://test.xn--p1ai")); // Russia
420         assertTrue(validator.isValid("http://test.xn--mgberp4a5d4ar")); // Saudi Arabia
421         assertTrue(validator.isValid("http://test.xn--90a3ac")); // Serbia
422         assertTrue(validator.isValid("http://test.xn--yfro4i67o")); // Singapore
423         assertTrue(validator.isValid("http://test.xn--clchc0ea0b2g2a9gcd")); // Singapore
424         assertTrue(validator.isValid("http://test.xn--3e0b707e")); // South Korea
425         assertTrue(validator.isValid("http://test.xn--fzc2c9e2c")); // Sri Lanka
426         assertTrue(validator.isValid("http://test.xn--xkc2al3hye2a")); // Sri Lanka
427         assertTrue(validator.isValid("http://test.xn--ogbpf8fl")); // Syria
428         assertTrue(validator.isValid("http://test.xn--kprw13d")); // Taiwan
429         assertTrue(validator.isValid("http://test.xn--kpry57d")); // Taiwan
430         assertTrue(validator.isValid("http://test.xn--o3cw4h")); // Thailand
431         assertTrue(validator.isValid("http://test.xn--pgbs0dh")); // Tunisia
432         assertTrue(validator.isValid("http://test.xn--mgbaam7a8h")); // United Arab Emirates
433         // Proposed internationalized ccTLDs
434 //        assertTrue(validator.isValid("http://test.xn--54b7fta0cc")); // Bangladesh
435 //        assertTrue(validator.isValid("http://test.xn--90ae")); // Bulgaria
436 //        assertTrue(validator.isValid("http://test.xn--node")); // Georgia
437 //        assertTrue(validator.isValid("http://test.xn--4dbrk0ce")); // Israel
438 //        assertTrue(validator.isValid("http://test.xn--mgb9awbf")); // Oman
439 //        assertTrue(validator.isValid("http://test.xn--j1amh")); // Ukraine
440 //        assertTrue(validator.isValid("http://test.xn--mgb2ddes")); // Yemen
441         // Test TLDs
442 //        assertTrue(validator.isValid("http://test.xn--kgbechtv")); // Arabic
443 //        assertTrue(validator.isValid("http://test.xn--hgbk6aj7f53bba")); // Persian
444 //        assertTrue(validator.isValid("http://test.xn--0zwm56d")); // Chinese
445 //        assertTrue(validator.isValid("http://test.xn--g6w251d")); // Chinese
446 //        assertTrue(validator.isValid("http://test.xn--80akhbyknj4f")); // Russian
447 //        assertTrue(validator.isValid("http://test.xn--11b5bs3a9aj6g")); // Hindi
448 //        assertTrue(validator.isValid("http://test.xn--jxalpdlp")); // Greek
449 //        assertTrue(validator.isValid("http://test.xn--9t4b11yi5a")); // Korean
450 //        assertTrue(validator.isValid("http://test.xn--deba0ad")); // Yiddish
451 //        assertTrue(validator.isValid("http://test.xn--zckzah")); // Japanese
452 //        assertTrue(validator.isValid("http://test.xn--hlcj6aya9esc7a")); // Tamil
453     }
454 
455     @Test
456     public void testValidator309() {
457         UrlValidator urlValidator = new UrlValidator();
458         assertTrue(urlValidator.isValid("http://sample.ondemand.com/"));
459         assertTrue(urlValidator.isValid("hTtP://sample.ondemand.CoM/"));
460         assertTrue(urlValidator.isValid("httpS://SAMPLE.ONEMAND.COM/"));
461         urlValidator = new UrlValidator(new String[] { "HTTP", "HTTPS" });
462         assertTrue(urlValidator.isValid("http://sample.ondemand.com/"));
463         assertTrue(urlValidator.isValid("hTtP://sample.ondemand.CoM/"));
464         assertTrue(urlValidator.isValid("httpS://SAMPLE.ONEMAND.COM/"));
465     }
466 
467     @Test
468     public void testValidator339() {
469         final UrlValidator urlValidator = new UrlValidator();
470         assertTrue(urlValidator.isValid("http://www.cnn.com/WORLD/?hpt=sitenav")); // without
471         assertTrue(urlValidator.isValid("http://www.cnn.com./WORLD/?hpt=sitenav")); // with
472         assertFalse(urlValidator.isValid("http://www.cnn.com../")); // doubly dotty
473         assertFalse(urlValidator.isValid("http://www.cnn.invalid/"));
474         assertFalse(urlValidator.isValid("http://www.cnn.invalid./")); // check . does not affect invalid domains
475     }
476 
477     @Test
478     public void testValidator339IDN() {
479         final UrlValidator urlValidator = new UrlValidator();
480         assertTrue(urlValidator.isValid("http://президент.рф/WORLD/?hpt=sitenav")); // without
481         assertTrue(urlValidator.isValid("http://президент.рф./WORLD/?hpt=sitenav")); // with
482         assertFalse(urlValidator.isValid("http://президент.рф..../")); // very dotty
483         assertFalse(urlValidator.isValid("http://президент.рф.../")); // triply dotty
484         assertFalse(urlValidator.isValid("http://президент.рф../")); // doubly dotty
485     }
486 
487     @Test
488     public void testValidator342() {
489         final UrlValidator urlValidator = new UrlValidator();
490         assertTrue(urlValidator.isValid("http://example.rocks/"));
491         assertTrue(urlValidator.isValid("http://example.rocks"));
492     }
493 
494     @Test
495     public void testValidator353() { // userinfo
496         final 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://user@www.apache.org:80/path"));
501         assertTrue(validator.isValid("http://us%00er:-._~!$&'()*+,;=@www.apache.org:80/path"));
502         assertFalse(validator.isValid("http://:pass@www.apache.org:80/path"));
503         assertFalse(validator.isValid("http://:@www.apache.org:80/path"));
504         assertFalse(validator.isValid("http://user:pa:ss@www.apache.org/path"));
505         assertFalse(validator.isValid("http://user:pa@ss@www.apache.org/path"));
506     }
507 
508     @Test
509     public void testValidator361() {
510         final UrlValidator validator = new UrlValidator();
511         assertTrue(validator.isValid("http://hello.tokyo/"));
512     }
513 
514     @Test
515     public void testValidator363() {
516         final UrlValidator urlValidator = new UrlValidator();
517         assertTrue(urlValidator.isValid("http://www.example.org/a/b/hello..world"));
518         assertTrue(urlValidator.isValid("http://www.example.org/a/hello..world"));
519         assertTrue(urlValidator.isValid("http://www.example.org/hello.world/"));
520         assertTrue(urlValidator.isValid("http://www.example.org/hello..world/"));
521         assertTrue(urlValidator.isValid("http://www.example.org/hello.world"));
522         assertTrue(urlValidator.isValid("http://www.example.org/hello..world"));
523         assertTrue(urlValidator.isValid("http://www.example.org/..world"));
524         assertTrue(urlValidator.isValid("http://www.example.org/.../world"));
525         assertFalse(urlValidator.isValid("http://www.example.org/../world"));
526         assertFalse(urlValidator.isValid("http://www.example.org/.."));
527         assertFalse(urlValidator.isValid("http://www.example.org/../"));
528         assertFalse(urlValidator.isValid("http://www.example.org/./.."));
529         assertFalse(urlValidator.isValid("http://www.example.org/././.."));
530         assertTrue(urlValidator.isValid("http://www.example.org/..."));
531         assertTrue(urlValidator.isValid("http://www.example.org/.../"));
532         assertTrue(urlValidator.isValid("http://www.example.org/.../.."));
533     }
534 
535     @Test
536     public void testValidator375() {
537         final UrlValidator validator = new UrlValidator();
538         String url = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html";
539         assertTrue(validator.isValid(url), "IPv6 address URL should validate: " + url);
540         url = "http://[::1]:80/index.html";
541         assertTrue(validator.isValid(url), "IPv6 address URL should validate: " + url);
542         url = "http://FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:80/index.html";
543         assertFalse(validator.isValid(url), "IPv6 address without [] should not validate: " + url);
544     }
545 
546     @Test
547     public void testValidator380() {
548         final UrlValidator validator = new UrlValidator();
549         assertTrue(validator.isValid("http://www.apache.org:80/path"));
550         assertTrue(validator.isValid("http://www.apache.org:8/path"));
551         assertTrue(validator.isValid("http://www.apache.org:/path"));
552     }
553 
554     @Test
555     public void testValidator382() {
556         final UrlValidator validator = new UrlValidator();
557         assertTrue(validator.isValid("ftp://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose"));
558     }
559 
560     @Test
561     public void testValidator391FAILS() {
562         final String[] schemes = { "file" };
563         final UrlValidator urlValidator = new UrlValidator(schemes);
564         assertTrue(urlValidator.isValid("file:/C:/path/to/dir/"));
565     }
566 
567     @Test
568     public void testValidator391OK() {
569         final String[] schemes = { "file" };
570         final UrlValidator urlValidator = new UrlValidator(schemes);
571         assertTrue(urlValidator.isValid("file:///C:/path/to/dir/"));
572     }
573 
574     @Test
575     public void testValidator411() {
576         final UrlValidator urlValidator = new UrlValidator();
577         assertTrue(urlValidator.isValid("http://example.rocks:/"));
578         assertTrue(urlValidator.isValid("http://example.rocks:0/"));
579         assertTrue(urlValidator.isValid("http://example.rocks:65535/"));
580         assertFalse(urlValidator.isValid("http://example.rocks:65536/"));
581         assertFalse(urlValidator.isValid("http://example.rocks:100000/"));
582     }
583 
584     @Test
585     public void testValidator420() {
586         final UrlValidator validator = new UrlValidator();
587         assertFalse(validator.isValid("http://example.com/serach?address=Main Avenue"));
588         assertTrue(validator.isValid("http://example.com/serach?address=Main%20Avenue"));
589         assertTrue(validator.isValid("http://example.com/serach?address=Main+Avenue"));
590     }
591 
592     @Test
593     public void testValidator452() {
594         final UrlValidator urlValidator = new UrlValidator();
595         assertTrue(urlValidator.isValid("http://[::FFFF:129.144.52.38]:80/index.html"));
596     }
597 
598     @Test
599     public void testValidator464() {
600         final String[] schemes = { "file" };
601         final UrlValidator urlValidator = new UrlValidator(schemes);
602         final String fileNAK = "file://bad ^ domain.com/label/test";
603         assertFalse(urlValidator.isValid(fileNAK), fileNAK);
604     }
605 
606     @Test
607     public void testValidator467() {
608         final UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
609         assertTrue(validator.isValid("https://example.com/some_path/path/"));
610         assertTrue(validator.isValid("https://example.com//somepath/path/"));
611         assertTrue(validator.isValid("https://example.com//some_path/path/"));
612         assertTrue(validator.isValid("http://example.com//_test")); // VALIDATOR-429
613     }
614 
615     @Test
616     public void testValidator473_1() { // reject null DomainValidator
617         final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> new UrlValidator(new String[] {}, null, 0L, null));
618         assertThat(thrown.getMessage(), is(equalTo("DomainValidator must not be null")));
619     }
620 
621     @Test
622     public void testValidator473_2() { // reject null DomainValidator with mismatched allowLocal
623         final List<DomainValidator.Item> items = new ArrayList<>();
624         final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class,
625                 () -> new UrlValidator(new String[] {}, null, 0L, DomainValidator.getInstance(true, items)));
626         assertThat(thrown.getMessage(), is(equalTo("DomainValidator disagrees with ALLOW_LOCAL_URLS setting")));
627     }
628 
629     @Test
630     public void testValidator473_3() { // reject null DomainValidator with mismatched allowLocal
631         final List<DomainValidator.Item> items = new ArrayList<>();
632         final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class,
633                 () -> new UrlValidator(new String[] {}, null, UrlValidator.ALLOW_LOCAL_URLS, DomainValidator.getInstance(false, items)));
634         assertThat(thrown.getMessage(), is(equalTo("DomainValidator disagrees with ALLOW_LOCAL_URLS setting")));
635     }
636 
637 }