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