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;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Performs Validation Test for url validations.
27   *
28   * @deprecated to be removed when org.apache.commons.validator.UrlValidator is removed
29   */
30  @Deprecated
31  public class UrlTest {
32  
33      static boolean incrementTestPartsIndex(final int[] testPartsIndex, final Object[] testParts) {
34          boolean carry = true; // add 1 to lowest order part.
35          boolean maxIndex = true;
36          for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) {
37              int index = testPartsIndex[testPartsIndexIndex];
38              final ResultPair[] part = (ResultPair[]) testParts[testPartsIndexIndex];
39              if (carry) {
40                  if (index < part.length - 1) {
41                      index++;
42                      testPartsIndex[testPartsIndexIndex] = index;
43                      carry = false;
44                  } else {
45                      testPartsIndex[testPartsIndexIndex] = 0;
46                      carry = true;
47                  }
48              }
49              maxIndex &= index == part.length - 1;
50          }
51  
52          return !maxIndex;
53      }
54  
55      /**
56       * Only used to debug the unit tests.
57       *
58       * @param argv
59       */
60      public static void main(final String[] argv) {
61  
62          final UrlTest fct = new UrlTest();
63          fct.setUp();
64          fct.testIsValid();
65          fct.testIsValidScheme();
66      }
67  
68      private final boolean printStatus = false;
69  
70      private final boolean printIndex = false; // print index that indicates current scheme,host,port,path, query test were using.
71  
72      /**
73       * 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
74       * 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
75       * entire URL to be considered valid.
76       */
77      ResultPair[] testUrlScheme = { new ResultPair("http://", true), new ResultPair("ftp://", true), new ResultPair("h3t://", true),
78              new ResultPair("3ht://", false), new ResultPair("http:/", false), new ResultPair("http:", false), new ResultPair("http/", false),
79              new ResultPair("://", false), new ResultPair("", true) };
80  
81      ResultPair[] testUrlAuthority = { new ResultPair("www.google.com", true), new ResultPair("go.com", true), new ResultPair("go.au", true),
82              new ResultPair("0.0.0.0", true), new ResultPair("255.255.255.255", true), new ResultPair("256.256.256.256", false), new ResultPair("255.com", true),
83              new ResultPair("1.2.3.4.5", false), new ResultPair("1.2.3.4.", false), new ResultPair("1.2.3", false), new ResultPair(".1.2.3.4", false),
84              new ResultPair("go.a", false), new ResultPair("go.a1a", true), new ResultPair("go.1aa", false), new ResultPair("aaa.", false),
85              new ResultPair(".aaa", false), new ResultPair("aaa", false), new ResultPair("", false) };
86  
87      ResultPair[] testUrlPort = { new ResultPair(":80", true), new ResultPair(":65535", true), new ResultPair(":0", true), new ResultPair("", true),
88              new ResultPair(":-1", false), new ResultPair(":65636", true), new ResultPair(":65a", false) };
89  
90      ResultPair[] testPath = { new ResultPair("/test1", true), new ResultPair("/t123", true), new ResultPair("/$23", true), new ResultPair("/..", false),
91              new ResultPair("/../", false), new ResultPair("/test1/", true), new ResultPair("", true), new ResultPair("/test1/file", true),
92              new ResultPair("/..//file", false), new ResultPair("/test1//file", false) };
93  
94      // Test allow2slash, noFragment
95      ResultPair[] testUrlPathOptions = { new ResultPair("/test1", true), new ResultPair("/t123", true), new ResultPair("/$23", true),
96              new ResultPair("/..", false), new ResultPair("/../", false), new ResultPair("/test1/", true), new ResultPair("/#", false), new ResultPair("", true),
97              new ResultPair("/test1/file", true), new ResultPair("/t123/file", true), new ResultPair("/$23/file", true), new ResultPair("/../file", false),
98              new ResultPair("/..//file", false), new ResultPair("/test1//file", true), new ResultPair("/#/file", false) };
99  
100     ResultPair[] testUrlQuery = { new ResultPair("?action=view", true), new ResultPair("?action=edit&mode=up", true), new ResultPair("", true) };
101 
102     Object[] testUrlParts = { testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery };
103 
104     Object[] testUrlPartsOptions = { testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery };
105 
106     int[] testPartsIndex = { 0, 0, 0, 0, 0 };
107     // Test data for individual url parts
108     ResultPair[] testScheme = { new ResultPair("http", true), new ResultPair("ftp", false), new ResultPair("httpd", false), new ResultPair("telnet", false) };
109 
110     @BeforeEach
111     protected void setUp() {
112         for (int index = 0; index < testPartsIndex.length - 1; index++) {
113             testPartsIndex[index] = 0;
114         }
115     }
116 
117     @Test
118     public void testIsValid() {
119         testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
120         setUp();
121         final int options = UrlValidator.ALLOW_2_SLASHES + UrlValidator.ALLOW_ALL_SCHEMES + UrlValidator.NO_FRAGMENTS;
122 
123         testIsValid(testUrlPartsOptions, options);
124     }
125 
126     /**
127      * Create set of tests by taking the testUrlXXX arrays and running through all possible permutations of their combinations.
128      *
129      * @param testObjects Used to create a url.
130      */
131     void testIsValid(final Object[] testObjects, final int options) {
132         final UrlValidator urlVal = new UrlValidator(null, options);
133         assertTrue(urlVal.isValid("http://www.google.com"));
134         assertTrue(urlVal.isValid("http://www.google.com/"));
135         int statusPerLine = 60;
136         int printed = 0;
137         if (printIndex) {
138             statusPerLine = 6;
139         }
140         do {
141             final StringBuilder testBuffer = new StringBuilder();
142             boolean expected = true;
143             for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
144                 final int index = testPartsIndex[testPartsIndexIndex];
145                 final ResultPair[] part = (ResultPair[]) testObjects[testPartsIndexIndex];
146                 testBuffer.append(part[index].item);
147                 expected &= part[index].valid;
148             }
149             final String url = testBuffer.toString();
150             final boolean result = urlVal.isValid(url);
151             assertEquals(expected, result, url);
152             if (printStatus) {
153                 if (printIndex) {
154                     System.out.print(testPartsIndextoString());
155                 } else if (result == expected) {
156                     System.out.print('.');
157                 } else {
158                     System.out.print('X');
159                 }
160                 printed++;
161                 if (printed == statusPerLine) {
162                     System.out.println();
163                     printed = 0;
164                 }
165             }
166         } while (incrementTestPartsIndex(testPartsIndex, testObjects));
167         if (printStatus) {
168             System.out.println();
169         }
170     }
171 
172     @Test
173     public void testIsValidScheme() {
174         if (printStatus) {
175             System.out.print("\n testIsValidScheme() ");
176         }
177         final String[] schemes = { "http", "gopher" };
178         // UrlValidator urlVal = new UrlValidator(schemes,false,false,false);
179         final UrlValidator urlVal = new UrlValidator(schemes, 0);
180         for (final ResultPair testPair : testScheme) {
181             final boolean result = urlVal.isValidScheme(testPair.item);
182             assertEquals(testPair.valid, result, testPair.item);
183             if (printStatus) {
184                 if (result == testPair.valid) {
185                     System.out.print('.');
186                 } else {
187                     System.out.print('X');
188                 }
189             }
190         }
191         if (printStatus) {
192             System.out.println();
193         }
194 
195     }
196 
197     private String testPartsIndextoString() {
198         final StringBuilder carryMsg = new StringBuilder("{");
199         for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
200             carryMsg.append(testPartsIndex[testPartsIndexIndex]);
201             if (testPartsIndexIndex < testPartsIndex.length - 1) {
202                 carryMsg.append(',');
203             } else {
204                 carryMsg.append('}');
205             }
206         }
207         return carryMsg.toString();
208 
209     }
210 
211     @Test
212     public void testValidateUrl() {
213         assertTrue(true);
214     }
215 
216     @Test
217     public void testValidator202() {
218         final String[] schemes = { "http", "https" };
219         final UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS);
220         urlValidator.isValid(
221                 "http://www.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.log");
222     }
223 
224     @Test
225     public void testValidator204() {
226         final String[] schemes = { "http", "https" };
227         final UrlValidator urlValidator = new UrlValidator(schemes);
228         assertTrue(urlValidator.isValid("http://tech.yahoo.com/rc/desktops/102;_ylt=Ao8yevQHlZ4On0O3ZJGXLEQFLZA5"));
229     }
230 
231 }