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