View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.conn.util;
29  
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  import java.nio.charset.Charset;
33  import java.util.List;
34  
35  import org.junit.Assert;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  public class TestPublicSuffixMatcher {
40  
41      private static final String SOURCE_FILE = "suffixlistmatcher.txt";
42  
43      private PublicSuffixMatcher matcher;
44  
45      private static final Charset UTF_8 = Charset.forName("UTF-8");
46  
47      @Before
48      public void setUp() throws Exception {
49          final ClassLoader classLoader = getClass().getClassLoader();
50          final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE);
51          Assert.assertNotNull(in);
52          final List<PublicSuffixList> lists = new PublicSuffixListParser().parseByType(
53                  new InputStreamReader(in, UTF_8));
54          matcher = new PublicSuffixMatcher(lists);
55      }
56  
57      @Test
58      public void testGetDomainRootAnyType() {
59          // Private
60          Assert.assertEquals("xx", matcher.getDomainRoot("example.XX"));
61          Assert.assertEquals("xx", matcher.getDomainRoot("www.example.XX"));
62          Assert.assertEquals("xx", matcher.getDomainRoot("www.blah.blah.example.XX"));
63          Assert.assertEquals("appspot.com", matcher.getDomainRoot("example.appspot.com"));
64          // Too short
65          Assert.assertEquals(null, matcher.getDomainRoot("jp"));
66          Assert.assertEquals(null, matcher.getDomainRoot("ac.jp"));
67          Assert.assertEquals(null, matcher.getDomainRoot("any.tokyo.jp"));
68          // ICANN
69          Assert.assertEquals("metro.tokyo.jp", matcher.getDomainRoot("metro.tokyo.jp"));
70          Assert.assertEquals("blah.blah.tokyo.jp", matcher.getDomainRoot("blah.blah.tokyo.jp"));
71          Assert.assertEquals("blah.ac.jp", matcher.getDomainRoot("blah.blah.ac.jp"));
72          // Unknown
73          Assert.assertEquals("garbage", matcher.getDomainRoot("garbage"));
74          Assert.assertEquals("garbage", matcher.getDomainRoot("garbage.garbage"));
75          Assert.assertEquals("garbage", matcher.getDomainRoot("*.garbage.garbage"));
76          Assert.assertEquals("garbage", matcher.getDomainRoot("*.garbage.garbage.garbage"));
77      }
78  
79      @Test
80      public void testGetDomainRootOnlyPRIVATE() {
81          // Private
82          Assert.assertEquals("xx", matcher.getDomainRoot("example.XX", DomainType.PRIVATE));
83          Assert.assertEquals("xx", matcher.getDomainRoot("www.example.XX", DomainType.PRIVATE));
84          Assert.assertEquals("xx", matcher.getDomainRoot("www.blah.blah.example.XX", DomainType.PRIVATE));
85          Assert.assertEquals("appspot.com", matcher.getDomainRoot("example.appspot.com"));
86          // Too short
87          Assert.assertEquals(null, matcher.getDomainRoot("jp", DomainType.PRIVATE));
88          Assert.assertEquals(null, matcher.getDomainRoot("ac.jp", DomainType.PRIVATE));
89          Assert.assertEquals(null, matcher.getDomainRoot("any.tokyo.jp", DomainType.PRIVATE));
90          // ICANN
91          Assert.assertEquals(null, matcher.getDomainRoot("metro.tokyo.jp", DomainType.PRIVATE));
92          Assert.assertEquals(null, matcher.getDomainRoot("blah.blah.tokyo.jp", DomainType.PRIVATE));
93          Assert.assertEquals(null, matcher.getDomainRoot("blah.blah.ac.jp", DomainType.PRIVATE));
94          // Unknown
95          Assert.assertEquals(null, matcher.getDomainRoot("garbage", DomainType.PRIVATE));
96          Assert.assertEquals(null, matcher.getDomainRoot("garbage.garbage", DomainType.PRIVATE));
97          Assert.assertEquals(null, matcher.getDomainRoot("*.garbage.garbage", DomainType.PRIVATE));
98          Assert.assertEquals(null, matcher.getDomainRoot("*.garbage.garbage.garbage", DomainType.PRIVATE));
99      }
100 
101     @Test
102     public void testGetDomainRootOnlyICANN() {
103         // Private
104         Assert.assertEquals(null, matcher.getDomainRoot("example.XX", DomainType.ICANN));
105         Assert.assertEquals(null, matcher.getDomainRoot("www.example.XX", DomainType.ICANN));
106         Assert.assertEquals(null, matcher.getDomainRoot("www.blah.blah.example.XX", DomainType.ICANN));
107         // Too short
108         Assert.assertEquals(null, matcher.getDomainRoot("xx", DomainType.ICANN));
109         Assert.assertEquals(null, matcher.getDomainRoot("jp", DomainType.ICANN));
110         Assert.assertEquals(null, matcher.getDomainRoot("ac.jp", DomainType.ICANN));
111         Assert.assertEquals(null, matcher.getDomainRoot("any.tokyo.jp", DomainType.ICANN));
112         // ICANN
113         Assert.assertEquals("metro.tokyo.jp", matcher.getDomainRoot("metro.tokyo.jp", DomainType.ICANN));
114         Assert.assertEquals("blah.blah.tokyo.jp", matcher.getDomainRoot("blah.blah.tokyo.jp", DomainType.ICANN));
115         Assert.assertEquals("blah.ac.jp", matcher.getDomainRoot("blah.blah.ac.jp", DomainType.ICANN));
116         // Unknown
117         Assert.assertEquals(null, matcher.getDomainRoot("garbage", DomainType.ICANN));
118         Assert.assertEquals(null, matcher.getDomainRoot("garbage.garbage", DomainType.ICANN));
119         Assert.assertEquals(null, matcher.getDomainRoot("*.garbage.garbage", DomainType.ICANN));
120         Assert.assertEquals(null, matcher.getDomainRoot("*.garbage.garbage.garbage", DomainType.ICANN));
121     }
122 
123 
124     @Test
125     public void testMatch() {
126         Assert.assertTrue(matcher.matches(".jp"));
127         Assert.assertTrue(matcher.matches(".ac.jp"));
128         Assert.assertTrue(matcher.matches(".any.tokyo.jp"));
129         // exception
130         Assert.assertFalse(matcher.matches(".metro.tokyo.jp"));
131         Assert.assertFalse(matcher.matches(".xx"));
132         Assert.assertFalse(matcher.matches(".appspot.com"));
133     }
134 
135     @Test
136     public void testMatchUnicode() {
137         Assert.assertTrue(matcher.matches(".h\u00E5.no")); // \u00E5 is <aring>
138         Assert.assertTrue(matcher.matches(".xn--h-2fa.no"));
139         Assert.assertTrue(matcher.matches(".h\u00E5.no"));
140         Assert.assertTrue(matcher.matches(".xn--h-2fa.no"));
141     }
142 
143 }