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