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.hc.client5.http.psl;
29  
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  import java.nio.charset.StandardCharsets;
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      @Before
46      public void setUp() throws Exception {
47          final ClassLoader classLoader = getClass().getClassLoader();
48          final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE);
49          Assert.assertNotNull(in);
50          final List<PublicSuffixList> lists = new PublicSuffixListParser().parseByType(
51                  new InputStreamReader(in, StandardCharsets.UTF_8));
52          matcher = new PublicSuffixMatcher(lists);
53      }
54  
55      @Test
56      public void testGetDomainRootAnyType() {
57          // Private
58          Assert.assertEquals("xx", matcher.getDomainRoot("example.XX"));
59          Assert.assertEquals("xx", matcher.getDomainRoot("www.example.XX"));
60          Assert.assertEquals("xx", matcher.getDomainRoot("www.blah.blah.example.XX"));
61          Assert.assertEquals("appspot.com", matcher.getDomainRoot("example.appspot.com"));
62          // Too short
63          Assert.assertEquals(null, matcher.getDomainRoot("jp"));
64          Assert.assertEquals(null, matcher.getDomainRoot("ac.jp"));
65          Assert.assertEquals(null, matcher.getDomainRoot("any.tokyo.jp"));
66          // ICANN
67          Assert.assertEquals("metro.tokyo.jp", matcher.getDomainRoot("metro.tokyo.jp"));
68          Assert.assertEquals("blah.blah.tokyo.jp", matcher.getDomainRoot("blah.blah.tokyo.jp"));
69          Assert.assertEquals("blah.ac.jp", matcher.getDomainRoot("blah.blah.ac.jp"));
70          // Unknown
71          Assert.assertEquals("garbage", matcher.getDomainRoot("garbage"));
72          Assert.assertEquals("garbage", matcher.getDomainRoot("garbage.garbage"));
73          Assert.assertEquals("garbage", matcher.getDomainRoot("*.garbage.garbage"));
74          Assert.assertEquals("garbage", matcher.getDomainRoot("*.garbage.garbage.garbage"));
75      }
76  
77      @Test
78      public void testGetDomainRootOnlyPRIVATE() {
79          // Private
80          Assert.assertEquals("xx", matcher.getDomainRoot("example.XX", DomainType.PRIVATE));
81          Assert.assertEquals("xx", matcher.getDomainRoot("www.example.XX", DomainType.PRIVATE));
82          Assert.assertEquals("xx", matcher.getDomainRoot("www.blah.blah.example.XX", DomainType.PRIVATE));
83          Assert.assertEquals("appspot.com", matcher.getDomainRoot("example.appspot.com"));
84          // Too short
85          Assert.assertEquals(null, matcher.getDomainRoot("jp", DomainType.PRIVATE));
86          Assert.assertEquals(null, matcher.getDomainRoot("ac.jp", DomainType.PRIVATE));
87          Assert.assertEquals(null, matcher.getDomainRoot("any.tokyo.jp", DomainType.PRIVATE));
88          // ICANN
89          Assert.assertEquals(null, matcher.getDomainRoot("metro.tokyo.jp", DomainType.PRIVATE));
90          Assert.assertEquals(null, matcher.getDomainRoot("blah.blah.tokyo.jp", DomainType.PRIVATE));
91          Assert.assertEquals(null, matcher.getDomainRoot("blah.blah.ac.jp", DomainType.PRIVATE));
92          // Unknown
93          Assert.assertEquals(null, matcher.getDomainRoot("garbage", DomainType.PRIVATE));
94          Assert.assertEquals(null, matcher.getDomainRoot("garbage.garbage", DomainType.PRIVATE));
95          Assert.assertEquals(null, matcher.getDomainRoot("*.garbage.garbage", DomainType.PRIVATE));
96          Assert.assertEquals(null, matcher.getDomainRoot("*.garbage.garbage.garbage", DomainType.PRIVATE));
97      }
98  
99      @Test
100     public void testGetDomainRootOnlyICANN() {
101         // Private
102         Assert.assertEquals(null, matcher.getDomainRoot("example.XX", DomainType.ICANN));
103         Assert.assertEquals(null, matcher.getDomainRoot("www.example.XX", DomainType.ICANN));
104         Assert.assertEquals(null, matcher.getDomainRoot("www.blah.blah.example.XX", DomainType.ICANN));
105         // Too short
106         Assert.assertEquals(null, matcher.getDomainRoot("xx", DomainType.ICANN));
107         Assert.assertEquals(null, matcher.getDomainRoot("jp", DomainType.ICANN));
108         Assert.assertEquals(null, matcher.getDomainRoot("ac.jp", DomainType.ICANN));
109         Assert.assertEquals(null, matcher.getDomainRoot("any.tokyo.jp", DomainType.ICANN));
110         // ICANN
111         Assert.assertEquals("metro.tokyo.jp", matcher.getDomainRoot("metro.tokyo.jp", DomainType.ICANN));
112         Assert.assertEquals("blah.blah.tokyo.jp", matcher.getDomainRoot("blah.blah.tokyo.jp", DomainType.ICANN));
113         Assert.assertEquals("blah.ac.jp", matcher.getDomainRoot("blah.blah.ac.jp", DomainType.ICANN));
114         // Unknown
115         Assert.assertEquals(null, matcher.getDomainRoot("garbage", DomainType.ICANN));
116         Assert.assertEquals(null, matcher.getDomainRoot("garbage.garbage", DomainType.ICANN));
117         Assert.assertEquals(null, matcher.getDomainRoot("*.garbage.garbage", DomainType.ICANN));
118         Assert.assertEquals(null, matcher.getDomainRoot("*.garbage.garbage.garbage", DomainType.ICANN));
119     }
120 
121 
122     @Test
123     public void testMatch() {
124         Assert.assertTrue(matcher.matches(".jp"));
125         Assert.assertTrue(matcher.matches(".ac.jp"));
126         Assert.assertTrue(matcher.matches(".any.tokyo.jp"));
127         // exception
128         Assert.assertFalse(matcher.matches(".metro.tokyo.jp"));
129         Assert.assertFalse(matcher.matches(".xx"));
130         Assert.assertFalse(matcher.matches(".appspot.com"));
131     }
132 
133     @Test
134     public void testMatchUnicode() {
135         Assert.assertTrue(matcher.matches(".h\u00E5.no")); // \u00E5 is <aring>
136         Assert.assertTrue(matcher.matches(".xn--h-2fa.no"));
137         Assert.assertTrue(matcher.matches(".h\u00E5.no"));
138         Assert.assertTrue(matcher.matches(".xn--h-2fa.no"));
139     }
140 
141 }