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.impl.cookie;
29  
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  import java.nio.charset.StandardCharsets;
33  
34  import org.apache.hc.client5.http.cookie.Cookie;
35  import org.apache.hc.client5.http.cookie.CookieOrigin;
36  import org.apache.hc.client5.http.psl.PublicSuffixList;
37  import org.apache.hc.client5.http.psl.PublicSuffixListParser;
38  import org.apache.hc.client5.http.psl.PublicSuffixMatcher;
39  import org.junit.Assert;
40  import org.junit.Before;
41  import org.junit.Test;
42  
43  public class TestPublicSuffixListParser {
44  
45      private static final String SOURCE_FILE = "suffixlist.txt";
46  
47      private PublicSuffixDomainFilter filter;
48  
49      @Before
50      public void setUp() throws Exception {
51          final ClassLoader classLoader = getClass().getClassLoader();
52          final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE);
53          Assert.assertNotNull(in);
54          final PublicSuffixList suffixList;
55          try {
56              final PublicSuffixListParser parser = new PublicSuffixListParser();
57              suffixList = parser.parse(new InputStreamReader(in, StandardCharsets.UTF_8));
58          } finally {
59              in.close();
60          }
61          final PublicSuffixMatcher matcher = new PublicSuffixMatcher(suffixList.getRules(), suffixList.getExceptions());
62          this.filter = new PublicSuffixDomainFilter(new BasicDomainHandler(), matcher);
63      }
64  
65      @Test
66      public void testParse() throws Exception {
67          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
68  
69          cookie.setAttribute(Cookie.DOMAIN_ATTR, ".jp");
70          cookie.setDomain(".jp");
71          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.jp", 80, "/stuff", false)));
72  
73          cookie.setDomain(".ac.jp");
74          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.ac.jp", 80, "/stuff", false)));
75  
76          cookie.setDomain(".any.tokyo.jp");
77          Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.any.tokyo.jp", 80, "/stuff", false)));
78  
79          // exception
80          cookie.setDomain(".metro.tokyo.jp");
81          Assert.assertTrue(filter.match(cookie, new CookieOrigin("apache.metro.tokyo.jp", 80, "/stuff", false)));
82      }
83  
84      @Test
85      public void testParseLocal() throws Exception {
86          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
87  
88          cookie.setDomain("localhost");
89          cookie.setAttribute(Cookie.DOMAIN_ATTR, "localhost");
90          Assert.assertTrue(filter.match(cookie, new CookieOrigin("localhost", 80, "/stuff", false)));
91  
92          cookie.setDomain("somehost");
93          cookie.setAttribute(Cookie.DOMAIN_ATTR, "somehost");
94          Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost", 80, "/stuff", false)));
95  
96          cookie.setDomain(".localdomain");
97          cookie.setAttribute(Cookie.DOMAIN_ATTR, ".localdomain");
98          Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.localdomain", 80, "/stuff", false)));
99  
100         cookie.setDomain(".local.");
101         cookie.setAttribute(Cookie.DOMAIN_ATTR, ".local.");
102         Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.local.", 80, "/stuff", false)));
103 
104         cookie.setDomain(".localhost.");
105         cookie.setAttribute(Cookie.DOMAIN_ATTR, ".localhost.");
106         Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.localhost.", 80, "/stuff", false)));
107 
108         cookie.setDomain(".local");
109         cookie.setAttribute(Cookie.DOMAIN_ATTR, ".local");
110         Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.local", 80, "/stuff", false)));
111 
112         cookie.setDomain(".blah");
113         cookie.setAttribute(Cookie.DOMAIN_ATTR, ".blah");
114         Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.blah", 80, "/stuff", false)));
115     }
116 
117     @Test
118     public void testUnicode() throws Exception {
119         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
120 
121         cookie.setDomain(".h\u00E5.no"); // \u00E5 is <aring>
122         Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.h\u00E5.no", 80, "/stuff", false)));
123 
124         cookie.setDomain(".xn--h-2fa.no");
125         Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.xn--h-2fa.no", 80, "/stuff", false)));
126 
127         cookie.setDomain(".h\u00E5.no");
128         Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.xn--h-2fa.no", 80, "/stuff", false)));
129 
130         cookie.setDomain(".xn--h-2fa.no");
131         Assert.assertFalse(filter.match(cookie, new CookieOrigin("apache.h\u00E5.no", 80, "/stuff", false)));
132     }
133 
134 }