1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.jupiter.api.Assertions;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42
43 public class TestPublicSuffixListParser {
44
45 private static final String SOURCE_FILE = "suffixlist.txt";
46
47 private PublicSuffixDomainFilter filter;
48
49 @BeforeEach
50 public void setUp() throws Exception {
51 final ClassLoader classLoader = getClass().getClassLoader();
52 final InputStream in = classLoader.getResourceAsStream(SOURCE_FILE);
53 Assertions.assertNotNull(in);
54 final PublicSuffixList suffixList;
55 try {
56 final PublicSuffixListParser parser = PublicSuffixListParser.INSTANCE;
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(BasicDomainHandler.INSTANCE, 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 Assertions.assertFalse(filter.match(cookie, new CookieOrigin("apache.jp", 80, "/stuff", false)));
72
73 cookie.setDomain(".ac.jp");
74 Assertions.assertFalse(filter.match(cookie, new CookieOrigin("apache.ac.jp", 80, "/stuff", false)));
75
76 cookie.setDomain(".any.tokyo.jp");
77 Assertions.assertFalse(filter.match(cookie, new CookieOrigin("apache.any.tokyo.jp", 80, "/stuff", false)));
78
79
80 cookie.setDomain(".metro.tokyo.jp");
81 Assertions.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 Assertions.assertTrue(filter.match(cookie, new CookieOrigin("localhost", 80, "/stuff", false)));
91
92 cookie.setDomain("somehost");
93 cookie.setAttribute(Cookie.DOMAIN_ATTR, "somehost");
94 Assertions.assertTrue(filter.match(cookie, new CookieOrigin("somehost", 80, "/stuff", false)));
95
96 cookie.setDomain(".localdomain");
97 cookie.setAttribute(Cookie.DOMAIN_ATTR, ".localdomain");
98 Assertions.assertTrue(filter.match(cookie, new CookieOrigin("somehost.localdomain", 80, "/stuff", false)));
99
100 cookie.setDomain(".local.");
101 cookie.setAttribute(Cookie.DOMAIN_ATTR, ".local.");
102 Assertions.assertTrue(filter.match(cookie, new CookieOrigin("somehost.local.", 80, "/stuff", false)));
103
104 cookie.setDomain(".localhost.");
105 cookie.setAttribute(Cookie.DOMAIN_ATTR, ".localhost.");
106 Assertions.assertTrue(filter.match(cookie, new CookieOrigin("somehost.localhost.", 80, "/stuff", false)));
107
108 cookie.setDomain(".local");
109 cookie.setAttribute(Cookie.DOMAIN_ATTR, ".local");
110 Assertions.assertTrue(filter.match(cookie, new CookieOrigin("somehost.local", 80, "/stuff", false)));
111
112 cookie.setDomain(".blah");
113 cookie.setAttribute(Cookie.DOMAIN_ATTR, ".blah");
114 Assertions.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");
122 Assertions.assertFalse(filter.match(cookie, new CookieOrigin("apache.h\u00E5.no", 80, "/stuff", false)));
123
124 cookie.setDomain(".xn--h-2fa.no");
125 Assertions.assertFalse(filter.match(cookie, new CookieOrigin("apache.xn--h-2fa.no", 80, "/stuff", false)));
126
127 cookie.setDomain(".h\u00E5.no");
128 Assertions.assertFalse(filter.match(cookie, new CookieOrigin("apache.xn--h-2fa.no", 80, "/stuff", false)));
129
130 cookie.setDomain(".xn--h-2fa.no");
131 Assertions.assertFalse(filter.match(cookie, new CookieOrigin("apache.h\u00E5.no", 80, "/stuff", false)));
132 }
133
134 }