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 junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  /***
24   * Performs Validation Test for url validations.
25   *
26   * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
27   */
28  public class UrlTest extends TestCase {
29  
30     private boolean printStatus = false;
31     private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.
32  
33     public UrlTest(String testName) {
34        super(testName);
35     }
36  
37     public static Test suite() {
38        return new TestSuite(UrlTest.class);
39     }
40  
41     protected void setUp() {
42        for (int index = 0; index < testPartsIndex.length - 1; index++) {
43           testPartsIndex[index] = 0;
44        }
45     }
46  
47     protected void tearDown() {
48     }
49  
50     public void testIsValid() {
51      	testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
52      	setUp();
53      	int options =
54      		UrlValidator.ALLOW_2_SLASHES
55      			+ UrlValidator.ALLOW_ALL_SCHEMES
56      			+ UrlValidator.NO_FRAGMENTS;
57      
58      	testIsValid(testUrlPartsOptions, options);
59     }
60  
61     public void testIsValidScheme() {
62        if (printStatus) {
63           System.out.print("\n testIsValidScheme() ");
64        }
65        String[] schemes = {"http", "gopher"};
66        //UrlValidator urlVal = new UrlValidator(schemes,false,false,false);
67        UrlValidator urlVal = new UrlValidator(schemes, 0);
68        for (int sIndex = 0; sIndex < testScheme.length; sIndex++) {
69           TestPair testPair = testScheme[sIndex];
70           boolean result = urlVal.isValidScheme(testPair.item);
71           assertEquals(testPair.item, testPair.valid, result);
72           if (printStatus) {
73              if (result == testPair.valid) {
74                 System.out.print('.');
75              } else {
76                 System.out.print('X');
77              }
78           }
79        }
80        if (printStatus) {
81           System.out.println();
82        }
83  
84     }
85  
86     /***
87      * Create set of tests by taking the testUrlXXX arrays and
88      * running through all possible permutations of their combinations.
89      *
90      * @param testObjects Used to create a url.
91      */
92     public void testIsValid(Object[] testObjects, int options) {
93        UrlValidator urlVal = new UrlValidator(null, options);
94        assertTrue(urlVal.isValid("http://www.google.com"));
95        assertTrue(urlVal.isValid("http://www.google.com/"));
96        int statusPerLine = 60;
97        int printed = 0;
98        if (printIndex)  {
99           statusPerLine = 6;
100       }
101       do {
102          StringBuffer testBuffer = new StringBuffer();
103          boolean expected = true;
104          for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
105             int index = testPartsIndex[testPartsIndexIndex];
106             TestPair[] part = (TestPair[]) testObjects[testPartsIndexIndex];
107             testBuffer.append(part[index].item);
108             expected &= part[index].valid;
109          }
110          String url = testBuffer.toString();
111          boolean result = urlVal.isValid(url);
112          assertEquals(url, expected, result);
113          if (printStatus) {
114             if (printIndex) {
115                System.out.print(testPartsIndextoString());
116             } else {
117                if (result == expected) {
118                   System.out.print('.');
119                } else {
120                   System.out.print('X');
121                }
122             }
123             printed++;
124             if (printed == statusPerLine) {
125                System.out.println();
126                printed = 0;
127             }
128          }
129       } while (incrementTestPartsIndex(testPartsIndex, testObjects));
130       if (printStatus) {
131          System.out.println();
132       }
133    }
134 
135    public void testValidator202() {
136        String[] schemes = {"http","https"};
137        UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS);
138        urlValidator.isValid("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");
139    }
140 
141    public void testValidator204() {
142        String[] schemes = {"http","https"};
143        UrlValidator urlValidator = new UrlValidator(schemes);
144        assertTrue(urlValidator.isValid("http://tech.yahoo.com/rc/desktops/102;_ylt=Ao8yevQHlZ4On0O3ZJGXLEQFLZA5"));
145    }
146 
147    static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) {
148       boolean carry = true;  //add 1 to lowest order part.
149       boolean maxIndex = true;
150       for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) {
151          int index = testPartsIndex[testPartsIndexIndex];
152          TestPair[] part = (TestPair[]) testParts[testPartsIndexIndex];
153          if (carry) {
154             if (index < part.length - 1) {
155                index++;
156                testPartsIndex[testPartsIndexIndex] = index;
157                carry = false;
158             } else {
159                testPartsIndex[testPartsIndexIndex] = 0;
160                carry = true;
161             }
162          }
163          maxIndex &= (index == (part.length - 1));
164       }
165 
166 
167       return (!maxIndex);
168    }
169 
170    private String testPartsIndextoString() {
171       StringBuffer carryMsg = new StringBuffer("{");
172       for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
173          carryMsg.append(testPartsIndex[testPartsIndexIndex]);
174          if (testPartsIndexIndex < testPartsIndex.length - 1) {
175             carryMsg.append(',');
176          } else {
177             carryMsg.append('}');
178          }
179       }
180       return carryMsg.toString();
181 
182    }
183 
184    public void testValidateUrl() {
185       assertTrue(true);
186    }
187 
188    /***
189     * Only used to debug the unit tests.
190     * @param argv
191     */
192    public static void main(String[] argv) {
193 
194       UrlTest fct = new UrlTest("url test");
195       fct.setUp();
196       fct.testIsValid();
197       fct.testIsValidScheme();
198    }
199    //-------------------- Test data for creating a composite URL
200    /***
201     * The data given below approximates the 4 parts of a URL
202     * <scheme>://<authority><path>?<query> except that the port number
203     * is broken out of authority to increase the number of permutations.
204     * A complete URL is composed of a scheme+authority+port+path+query,
205     * all of which must be individually valid for the entire URL to be considered
206     * valid.
207     */
208    TestPair[] testUrlScheme = {new TestPair("http://", true),
209                                new TestPair("ftp://", true),
210                                new TestPair("h3t://", true),
211                                new TestPair("3ht://", false),
212                                new TestPair("http:/", false),
213                                new TestPair("http:", false),
214                                new TestPair("http/", false),
215                                new TestPair("://", false),
216                                new TestPair("", true)};
217 
218    TestPair[] testUrlAuthority = {new TestPair("www.google.com", true),
219                                   new TestPair("go.com", true),
220                                   new TestPair("go.au", true),
221                                   new TestPair("0.0.0.0", true),
222                                   new TestPair("255.255.255.255", true),
223                                   new TestPair("256.256.256.256", false),
224                                   new TestPair("255.com", true),
225                                   new TestPair("1.2.3.4.5", false),
226                                   new TestPair("1.2.3.4.", false),
227                                   new TestPair("1.2.3", false),
228                                   new TestPair(".1.2.3.4", false),
229                                   new TestPair("go.a", false),
230                                   new TestPair("go.a1a", true),
231                                   new TestPair("go.1aa", false),
232                                   new TestPair("aaa.", false),
233                                   new TestPair(".aaa", false),
234                                   new TestPair("aaa", false),
235                                   new TestPair("", false)
236    };
237    TestPair[] testUrlPort = {new TestPair(":80", true),
238                              new TestPair(":65535", true),
239                              new TestPair(":0", true),
240                              new TestPair("", true),
241                              new TestPair(":-1", false),
242                              new TestPair(":65636", true),
243                              new TestPair(":65a", false)
244    };
245    TestPair[] testPath = {new TestPair("/test1", true),
246                           new TestPair("/t123", true),
247                           new TestPair("/$23", true),
248                           new TestPair("/..", false),
249                           new TestPair("/../", false),
250                           new TestPair("/test1/", true),
251                           new TestPair("", true),
252                           new TestPair("/test1/file", true),
253                           new TestPair("/..//file", false),
254                           new TestPair("/test1//file", false)
255    };
256    //Test allow2slash, noFragment
257    TestPair[] testUrlPathOptions = {new TestPair("/test1", true),
258                                     new TestPair("/t123", true),
259                                     new TestPair("/$23", true),
260                                     new TestPair("/..", false),
261                                     new TestPair("/../", false),
262                                     new TestPair("/test1/", true),
263                                     new TestPair("/#", false),
264                                     new TestPair("", true),
265                                     new TestPair("/test1/file", true),
266                                     new TestPair("/t123/file", true),
267                                     new TestPair("/$23/file", true),
268                                     new TestPair("/../file", false),
269                                     new TestPair("/..//file", false),
270                                     new TestPair("/test1//file", true),
271                                     new TestPair("/#/file", false)
272    };
273 
274    TestPair[] testUrlQuery = {new TestPair("?action=view", true),
275                               new TestPair("?action=edit&mode=up", true),
276                               new TestPair("", true)
277    };
278 
279    Object[] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery};
280    Object[] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery};
281    int[] testPartsIndex = {0, 0, 0, 0, 0};
282 
283    //---------------- Test data for individual url parts ----------------
284    TestPair[] testScheme = {new TestPair("http", true),
285                             new TestPair("ftp", false),
286                             new TestPair("httpd", false),
287                             new TestPair("telnet", false)};
288 
289 
290 }