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