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.text.DateFormat;
31  import java.text.SimpleDateFormat;
32  import java.util.Arrays;
33  import java.util.Date;
34  import java.util.Locale;
35  
36  import org.apache.hc.client5.http.cookie.Cookie;
37  import org.apache.hc.client5.http.cookie.CookieAttributeHandler;
38  import org.apache.hc.client5.http.cookie.CookieOrigin;
39  import org.apache.hc.client5.http.cookie.MalformedCookieException;
40  import org.apache.hc.client5.http.psl.DomainType;
41  import org.apache.hc.client5.http.psl.PublicSuffixMatcher;
42  import org.apache.hc.client5.http.utils.DateUtils;
43  import org.junit.Assert;
44  import org.junit.Test;
45  
46  public class TestBasicCookieAttribHandlers {
47  
48      @Test
49      public void testBasicDomainParse() throws Exception {
50          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
51          final CookieAttributeHandler h = new BasicDomainHandler();
52          h.parse(cookie, "www.somedomain.com");
53          Assert.assertEquals("www.somedomain.com", cookie.getDomain());
54      }
55  
56      @Test(expected=MalformedCookieException.class)
57      public void testBasicDomainParseInvalid1() throws Exception {
58          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
59          final CookieAttributeHandler h = new BasicDomainHandler();
60          h.parse(cookie, "");
61      }
62  
63      @Test(expected=MalformedCookieException.class)
64      public void testBasicDomainParseInvalid2() throws Exception {
65          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
66          final CookieAttributeHandler h = new BasicDomainHandler();
67          h.parse(cookie, null);
68      }
69  
70      @Test
71      public void testBasicDomainValidate1() throws Exception {
72          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
73          final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
74          final CookieAttributeHandler h = new BasicDomainHandler();
75  
76          cookie.setDomain(".somedomain.com");
77          h.validate(cookie, origin);
78  
79          cookie.setDomain(".otherdomain.com");
80          try {
81              h.validate(cookie, origin);
82              Assert.fail("MalformedCookieException should have been thrown");
83          } catch (final MalformedCookieException ex) {
84              // expected
85          }
86          cookie.setDomain("www.otherdomain.com");
87          try {
88              h.validate(cookie, origin);
89              Assert.fail("MalformedCookieException should have been thrown");
90          } catch (final MalformedCookieException ex) {
91              // expected
92          }
93      }
94  
95      @Test
96      public void testBasicDomainValidate2() throws Exception {
97          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
98          final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
99          final CookieAttributeHandler h = new BasicDomainHandler();
100 
101         cookie.setDomain("somehost");
102         h.validate(cookie, origin);
103 
104         cookie.setDomain("otherhost");
105         try {
106             h.validate(cookie, origin);
107             Assert.fail("MalformedCookieException should have been thrown");
108         } catch (final MalformedCookieException ex) {
109             // expected
110         }
111     }
112 
113     @Test
114     public void testBasicDomainValidate3() throws Exception {
115         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
116         final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
117         final CookieAttributeHandler h = new BasicDomainHandler();
118 
119         cookie.setDomain(".somedomain.com");
120         h.validate(cookie, origin);
121     }
122 
123     @Test
124     public void testBasicDomainValidate4() throws Exception {
125         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
126         final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
127         final CookieAttributeHandler h = new BasicDomainHandler();
128 
129         cookie.setDomain(null);
130         try {
131             h.validate(cookie, origin);
132             Assert.fail("MalformedCookieException should have been thrown");
133         } catch (final MalformedCookieException ex) {
134             // expected
135         }
136     }
137 
138     @Test
139     public void testBasicDomainMatch1() throws Exception {
140         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
141         final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
142         final CookieAttributeHandler h = new BasicDomainHandler();
143 
144         cookie.setDomain("somedomain.com");
145         cookie.setAttribute(Cookie.DOMAIN_ATTR, "somedomain.com");
146         Assert.assertTrue(h.match(cookie, origin));
147 
148         cookie.setDomain(".somedomain.com");
149         Assert.assertTrue(h.match(cookie, origin));
150     }
151 
152     @Test
153     public void testBasicDomainMatch2() throws Exception {
154         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
155         final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
156         final CookieAttributeHandler h = new BasicDomainHandler();
157 
158         cookie.setDomain("somedomain.com");
159         cookie.setAttribute(Cookie.DOMAIN_ATTR, "somedomain.com");
160         Assert.assertTrue(h.match(cookie, origin));
161 
162         cookie.setDomain(".somedomain.com");
163         Assert.assertTrue(h.match(cookie, origin));
164 
165         cookie.setDomain(null);
166         Assert.assertFalse(h.match(cookie, origin));
167     }
168 
169     @Test
170     public void testBasicDomainMatchOneLetterPrefix() throws Exception {
171         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
172         final CookieOrigin origin = new CookieOrigin("a.somedomain.com", 80, "/", false);
173         final CookieAttributeHandler h = new BasicDomainHandler();
174 
175         cookie.setDomain("somedomain.com");
176         cookie.setAttribute(Cookie.DOMAIN_ATTR, "somedomain.com");
177         Assert.assertTrue(h.match(cookie, origin));
178     }
179 
180     @Test
181     public void testBasicDomainMatchMixedCase() throws Exception {
182         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
183         final CookieOrigin origin = new CookieOrigin("a.SomeDomain.com", 80, "/", false);
184         final CookieAttributeHandler h = new BasicDomainHandler();
185 
186         cookie.setDomain("somedoMain.Com");
187         cookie.setAttribute(Cookie.DOMAIN_ATTR, "somedoMain.Com");
188         Assert.assertTrue(h.match(cookie, origin));
189     }
190 
191     @Test
192     public void testBasicDomainInvalidInput() throws Exception {
193         final CookieAttributeHandler h = new BasicDomainHandler();
194         try {
195             h.parse(null, null);
196             Assert.fail("NullPointerException must have been thrown");
197         } catch (final NullPointerException ex) {
198             // expected
199         }
200         try {
201             h.validate(null, null);
202             Assert.fail("NullPointerException must have been thrown");
203         } catch (final NullPointerException ex) {
204             // expected
205         }
206         try {
207             h.validate(new BasicClientCookie("name", "value"), null);
208             Assert.fail("NullPointerException must have been thrown");
209         } catch (final NullPointerException ex) {
210             // expected
211         }
212         try {
213             h.match(null, null);
214             Assert.fail("NullPointerException must have been thrown");
215         } catch (final NullPointerException ex) {
216             // expected
217         }
218         try {
219             h.match(new BasicClientCookie("name", "value"), null);
220             Assert.fail("NullPointerException must have been thrown");
221         } catch (final NullPointerException ex) {
222             // expected
223         }
224     }
225 
226     @Test
227     public void testBasicPathParse() throws Exception {
228         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
229         final CookieAttributeHandler h = new BasicPathHandler();
230         h.parse(cookie, "stuff");
231         Assert.assertEquals("stuff", cookie.getPath());
232         h.parse(cookie, "");
233         Assert.assertEquals("/", cookie.getPath());
234         h.parse(cookie, null);
235         Assert.assertEquals("/", cookie.getPath());
236     }
237 
238     @Test
239     public void testBasicPathMatch1() throws Exception {
240         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
241         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
242         final CookieAttributeHandler h = new BasicPathHandler();
243         cookie.setPath("/stuff");
244         Assert.assertTrue(h.match(cookie, origin));
245     }
246 
247     @Test
248     public void testBasicPathMatch2() throws Exception {
249         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
250         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff/", false);
251         final CookieAttributeHandler h = new BasicPathHandler();
252         cookie.setPath("/stuff");
253         Assert.assertTrue(h.match(cookie, origin));
254     }
255 
256     @Test
257     public void testBasicPathMatch3() throws Exception {
258         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
259         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff/more-stuff", false);
260         final CookieAttributeHandler h = new BasicPathHandler();
261         cookie.setPath("/stuff");
262         Assert.assertTrue(h.match(cookie, origin));
263     }
264 
265     @Test
266     public void testBasicPathMatch4() throws Exception {
267         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
268         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuffed", false);
269         final CookieAttributeHandler h = new BasicPathHandler();
270         cookie.setPath("/stuff");
271         Assert.assertFalse(h.match(cookie, origin));
272     }
273 
274     @Test
275     public void testBasicPathMatch5() throws Exception {
276         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
277         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/otherstuff", false);
278         final CookieAttributeHandler h = new BasicPathHandler();
279         cookie.setPath("/stuff");
280         Assert.assertFalse(h.match(cookie, origin));
281     }
282 
283     @Test
284     public void testBasicPathMatch6() throws Exception {
285         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
286         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
287         final CookieAttributeHandler h = new BasicPathHandler();
288         cookie.setPath("/stuff/");
289         Assert.assertTrue(h.match(cookie, origin));
290     }
291 
292     @Test
293     public void testBasicPathMatch7() throws Exception {
294         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
295         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
296         final CookieAttributeHandler h = new BasicPathHandler();
297         Assert.assertTrue(h.match(cookie, origin));
298     }
299 
300     @Test
301     public void testBasicPathInvalidInput() throws Exception {
302         final CookieAttributeHandler h = new BasicPathHandler();
303         try {
304             h.parse(null, null);
305             Assert.fail("NullPointerException must have been thrown");
306         } catch (final NullPointerException ex) {
307             // expected
308         }
309         try {
310             h.match(null, null);
311             Assert.fail("NullPointerException must have been thrown");
312         } catch (final NullPointerException ex) {
313             // expected
314         }
315         try {
316             h.match(new BasicClientCookie("name", "value"), null);
317             Assert.fail("NullPointerException must have been thrown");
318         } catch (final NullPointerException ex) {
319             // expected
320         }
321     }
322 
323     @Test
324     public void testBasicMaxAgeParse() throws Exception {
325         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
326         final CookieAttributeHandler h = new BasicMaxAgeHandler();
327         h.parse(cookie, "2000");
328         Assert.assertNotNull(cookie.getExpiryDate());
329     }
330 
331     @Test
332     public void testBasicMaxAgeParseInvalid() throws Exception {
333         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
334         final CookieAttributeHandler h = new BasicMaxAgeHandler();
335         try {
336             h.parse(cookie, "garbage");
337             Assert.fail("MalformedCookieException must have been thrown");
338         } catch (final MalformedCookieException ex) {
339             // expected
340         }
341         try {
342             h.parse(cookie, null);
343             Assert.fail("MalformedCookieException must have been thrown");
344         } catch (final MalformedCookieException ex) {
345             // expected
346         }
347     }
348 
349     @Test
350     public void testBasicMaxAgeInvalidInput() throws Exception {
351         final CookieAttributeHandler h = new BasicMaxAgeHandler();
352         try {
353             h.parse(null, null);
354             Assert.fail("NullPointerException must have been thrown");
355         } catch (final NullPointerException ex) {
356             // expected
357         }
358     }
359 
360     @Test
361     public void testBasicSecureParse() throws Exception {
362         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
363         final CookieAttributeHandler h = new BasicSecureHandler();
364         h.parse(cookie, "whatever");
365         Assert.assertTrue(cookie.isSecure());
366         h.parse(cookie, null);
367         Assert.assertTrue(cookie.isSecure());
368     }
369 
370     @Test
371     public void testBasicSecureMatch() throws Exception {
372         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
373         final CookieAttributeHandler h = new BasicSecureHandler();
374 
375         final CookieOrigin origin1 = new CookieOrigin("somehost", 80, "/stuff", false);
376         cookie.setSecure(false);
377         Assert.assertTrue(h.match(cookie, origin1));
378         cookie.setSecure(true);
379         Assert.assertFalse(h.match(cookie, origin1));
380 
381         final CookieOrigin origin2 = new CookieOrigin("somehost", 80, "/stuff", true);
382         cookie.setSecure(false);
383         Assert.assertTrue(h.match(cookie, origin2));
384         cookie.setSecure(true);
385         Assert.assertTrue(h.match(cookie, origin2));
386     }
387 
388     @Test
389     public void testBasicSecureInvalidInput() throws Exception {
390         final CookieAttributeHandler h = new BasicSecureHandler();
391         try {
392             h.parse(null, null);
393             Assert.fail("NullPointerException must have been thrown");
394         } catch (final NullPointerException ex) {
395             // expected
396         }
397         try {
398             h.match(null, null);
399             Assert.fail("NullPointerException must have been thrown");
400         } catch (final NullPointerException ex) {
401             // expected
402         }
403         try {
404             h.match(new BasicClientCookie("name", "value"), null);
405             Assert.fail("NullPointerException must have been thrown");
406         } catch (final NullPointerException ex) {
407             // expected
408         }
409     }
410 
411     @Test
412     public void testBasicExpiresParse() throws Exception {
413         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
414         final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
415 
416         final DateFormat dateformat = new SimpleDateFormat(DateUtils.PATTERN_RFC1123, Locale.US);
417         dateformat.setTimeZone(DateUtils.GMT);
418 
419         final Date now = new Date();
420 
421         h.parse(cookie, dateformat.format(now));
422         Assert.assertNotNull(cookie.getExpiryDate());
423     }
424 
425     @Test
426     public void testBasicExpiresParseInvalid() throws Exception {
427         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
428         final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
429         try {
430             h.parse(cookie, "garbage");
431             Assert.fail("MalformedCookieException must have been thrown");
432         } catch (final MalformedCookieException ex) {
433             // expected
434         }
435         try {
436             h.parse(cookie, null);
437             Assert.fail("MalformedCookieException must have been thrown");
438         } catch (final MalformedCookieException ex) {
439             // expected
440         }
441     }
442 
443     @SuppressWarnings("unused")
444     @Test
445     public void testBasicExpiresInvalidInput() throws Exception {
446         try {
447             new BasicExpiresHandler(null);
448             Assert.fail("NullPointerException must have been thrown");
449         } catch (final NullPointerException ex) {
450             // expected
451         }
452         final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
453         try {
454             h.parse(null, null);
455             Assert.fail("NullPointerException must have been thrown");
456         } catch (final NullPointerException ex) {
457             // expected
458         }
459     }
460 
461     @Test
462     public void testPublicSuffixFilter() throws Exception {
463         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
464 
465         final PublicSuffixMatcher matcher = new PublicSuffixMatcher(DomainType.ICANN, Arrays.asList("co.uk", "com"), null);
466         final PublicSuffixDomainFilter h = new PublicSuffixDomainFilter(new BasicDomainHandler(), matcher);
467 
468         cookie.setDomain(".co.uk");
469         cookie.setAttribute(Cookie.DOMAIN_ATTR, ".co.uk");
470         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.co.uk", 80, "/stuff", false)));
471 
472         cookie.setDomain("co.uk");
473         cookie.setAttribute(Cookie.DOMAIN_ATTR, "co.uk");
474         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.co.uk", 80, "/stuff", false)));
475 
476         cookie.setDomain(".co.com");
477         cookie.setAttribute(Cookie.DOMAIN_ATTR, ".co.com");
478         Assert.assertTrue(h.match(cookie, new CookieOrigin("apache.co.com", 80, "/stuff", false)));
479 
480         cookie.setDomain("co.com");
481         cookie.setAttribute(Cookie.DOMAIN_ATTR, "co.com");
482         Assert.assertTrue(h.match(cookie, new CookieOrigin("apache.co.com", 80, "/stuff", false)));
483 
484         cookie.setDomain(".com");
485         cookie.setAttribute(Cookie.DOMAIN_ATTR, ".com");
486         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.com", 80, "/stuff", false)));
487 
488         cookie.setDomain("com");
489         cookie.setAttribute(Cookie.DOMAIN_ATTR, "com");
490         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.com", 80, "/stuff", false)));
491 
492         cookie.setDomain("apache.com");
493         cookie.setAttribute(Cookie.DOMAIN_ATTR, "apache.com");
494         Assert.assertTrue(h.match(cookie, new CookieOrigin("apache.com", 80, "/stuff", false)));
495 
496         cookie.setDomain(".apache.com");
497         cookie.setAttribute(Cookie.DOMAIN_ATTR, ".apache.com");
498         Assert.assertTrue(h.match(cookie, new CookieOrigin("www.apache.com", 80, "/stuff", false)));
499 
500         cookie.setDomain("localhost");
501         cookie.setAttribute(Cookie.DOMAIN_ATTR, "localhost");
502         Assert.assertTrue(h.match(cookie, new CookieOrigin("localhost", 80, "/stuff", false)));
503     }
504 
505 }