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.DomainType;
38  import org.apache.http.conn.util.PublicSuffixMatcher;
39  import org.apache.http.cookie.ClientCookie;
40  import org.apache.http.cookie.CookieAttributeHandler;
41  import org.apache.http.cookie.CookieOrigin;
42  import org.apache.http.cookie.MalformedCookieException;
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(ClientCookie.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(ClientCookie.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(ClientCookie.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(ClientCookie.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("IllegalArgumentException must have been thrown");
197         } catch (final IllegalArgumentException ex) {
198             // expected
199         }
200         try {
201             h.validate(null, null);
202             Assert.fail("IllegalArgumentException must have been thrown");
203         } catch (final IllegalArgumentException ex) {
204             // expected
205         }
206         try {
207             h.validate(new BasicClientCookie("name", "value"), null);
208             Assert.fail("IllegalArgumentException must have been thrown");
209         } catch (final IllegalArgumentException ex) {
210             // expected
211         }
212         try {
213             h.match(null, null);
214             Assert.fail("IllegalArgumentException must have been thrown");
215         } catch (final IllegalArgumentException ex) {
216             // expected
217         }
218         try {
219             h.match(new BasicClientCookie("name", "value"), null);
220             Assert.fail("IllegalArgumentException must have been thrown");
221         } catch (final IllegalArgumentException 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 testBasicPathValidate() throws Exception {
302         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
303         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
304         final CookieAttributeHandler h = new BasicPathHandler();
305         cookie.setPath("/stuff");
306         h.validate(cookie, origin);
307         cookie.setPath("/stuffed");
308         try {
309             h.validate(cookie, origin);
310             Assert.fail("MalformedCookieException must have been thrown");
311         } catch (final MalformedCookieException ex) {
312             // expected
313         }
314     }
315 
316     @Test
317     public void testBasicPathInvalidInput() throws Exception {
318         final CookieAttributeHandler h = new BasicPathHandler();
319         try {
320             h.parse(null, null);
321             Assert.fail("IllegalArgumentException must have been thrown");
322         } catch (final IllegalArgumentException ex) {
323             // expected
324         }
325         try {
326             h.match(null, null);
327             Assert.fail("IllegalArgumentException must have been thrown");
328         } catch (final IllegalArgumentException ex) {
329             // expected
330         }
331         try {
332             h.match(new BasicClientCookie("name", "value"), null);
333             Assert.fail("IllegalArgumentException must have been thrown");
334         } catch (final IllegalArgumentException ex) {
335             // expected
336         }
337     }
338 
339     @Test
340     public void testBasicMaxAgeParse() throws Exception {
341         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
342         final CookieAttributeHandler h = new BasicMaxAgeHandler();
343         h.parse(cookie, "2000");
344         Assert.assertNotNull(cookie.getExpiryDate());
345     }
346 
347     @Test
348     public void testBasicMaxAgeParseInvalid() throws Exception {
349         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
350         final CookieAttributeHandler h = new BasicMaxAgeHandler();
351         try {
352             h.parse(cookie, "garbage");
353             Assert.fail("MalformedCookieException must have been thrown");
354         } catch (final MalformedCookieException ex) {
355             // expected
356         }
357         try {
358             h.parse(cookie, null);
359             Assert.fail("MalformedCookieException must have been thrown");
360         } catch (final MalformedCookieException ex) {
361             // expected
362         }
363     }
364 
365     @Test
366     public void testBasicMaxAgeInvalidInput() throws Exception {
367         final CookieAttributeHandler h = new BasicMaxAgeHandler();
368         try {
369             h.parse(null, null);
370             Assert.fail("IllegalArgumentException must have been thrown");
371         } catch (final IllegalArgumentException ex) {
372             // expected
373         }
374     }
375 
376     @Test
377     public void testBasicCommentParse() throws Exception {
378         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
379         final CookieAttributeHandler h = new BasicCommentHandler();
380         h.parse(cookie, "whatever");
381         Assert.assertEquals("whatever", cookie.getComment());
382         h.parse(cookie, null);
383         Assert.assertEquals(null, cookie.getComment());
384     }
385 
386     @Test
387     public void testBasicCommentInvalidInput() throws Exception {
388         final CookieAttributeHandler h = new BasicCommentHandler();
389         try {
390             h.parse(null, null);
391             Assert.fail("IllegalArgumentException must have been thrown");
392         } catch (final IllegalArgumentException ex) {
393             // expected
394         }
395     }
396 
397     @Test
398     public void testBasicSecureParse() throws Exception {
399         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
400         final CookieAttributeHandler h = new BasicSecureHandler();
401         h.parse(cookie, "whatever");
402         Assert.assertTrue(cookie.isSecure());
403         h.parse(cookie, null);
404         Assert.assertTrue(cookie.isSecure());
405     }
406 
407     @Test
408     public void testBasicSecureMatch() throws Exception {
409         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
410         final CookieAttributeHandler h = new BasicSecureHandler();
411 
412         final CookieOrigin origin1 = new CookieOrigin("somehost", 80, "/stuff", false);
413         cookie.setSecure(false);
414         Assert.assertTrue(h.match(cookie, origin1));
415         cookie.setSecure(true);
416         Assert.assertFalse(h.match(cookie, origin1));
417 
418         final CookieOrigin origin2 = new CookieOrigin("somehost", 80, "/stuff", true);
419         cookie.setSecure(false);
420         Assert.assertTrue(h.match(cookie, origin2));
421         cookie.setSecure(true);
422         Assert.assertTrue(h.match(cookie, origin2));
423     }
424 
425     @Test
426     public void testBasicSecureInvalidInput() throws Exception {
427         final CookieAttributeHandler h = new BasicSecureHandler();
428         try {
429             h.parse(null, null);
430             Assert.fail("IllegalArgumentException must have been thrown");
431         } catch (final IllegalArgumentException ex) {
432             // expected
433         }
434         try {
435             h.match(null, null);
436             Assert.fail("IllegalArgumentException must have been thrown");
437         } catch (final IllegalArgumentException ex) {
438             // expected
439         }
440         try {
441             h.match(new BasicClientCookie("name", "value"), null);
442             Assert.fail("IllegalArgumentException must have been thrown");
443         } catch (final IllegalArgumentException ex) {
444             // expected
445         }
446     }
447 
448     @Test
449     public void testBasicExpiresParse() throws Exception {
450         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
451         final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
452 
453         final DateFormat dateformat = new SimpleDateFormat(DateUtils.PATTERN_RFC1123, Locale.US);
454         dateformat.setTimeZone(DateUtils.GMT);
455 
456         final Date now = new Date();
457 
458         h.parse(cookie, dateformat.format(now));
459         Assert.assertNotNull(cookie.getExpiryDate());
460     }
461 
462     @Test
463     public void testBasicExpiresParseInvalid() throws Exception {
464         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
465         final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
466         try {
467             h.parse(cookie, "garbage");
468             Assert.fail("MalformedCookieException must have been thrown");
469         } catch (final MalformedCookieException ex) {
470             // expected
471         }
472         try {
473             h.parse(cookie, null);
474             Assert.fail("MalformedCookieException must have been thrown");
475         } catch (final MalformedCookieException ex) {
476             // expected
477         }
478     }
479 
480     @SuppressWarnings("unused")
481     @Test
482     public void testBasicExpiresInvalidInput() throws Exception {
483         try {
484             new BasicExpiresHandler(null);
485             Assert.fail("IllegalArgumentException must have been thrown");
486         } catch (final IllegalArgumentException ex) {
487             // expected
488         }
489         final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
490         try {
491             h.parse(null, null);
492             Assert.fail("IllegalArgumentException must have been thrown");
493         } catch (final IllegalArgumentException ex) {
494             // expected
495         }
496     }
497 
498     @Test
499     public void testPublicSuffixFilter() throws Exception {
500         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
501 
502         final PublicSuffixMatcher matcher = new PublicSuffixMatcher(DomainType.ICANN, Arrays.asList("co.uk", "com"), null);
503         final PublicSuffixDomainFilter h = new PublicSuffixDomainFilter(new RFC2109DomainHandler(), matcher);
504 
505         cookie.setDomain(".co.uk");
506         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.co.uk", 80, "/stuff", false)));
507 
508         cookie.setDomain("co.uk");
509         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.co.uk", 80, "/stuff", false)));
510 
511         cookie.setDomain(".co.com");
512         Assert.assertTrue(h.match(cookie, new CookieOrigin("apache.co.com", 80, "/stuff", false)));
513 
514         cookie.setDomain("co.com");
515         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.co.com", 80, "/stuff", false)));
516 
517         cookie.setDomain(".com");
518         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.com", 80, "/stuff", false)));
519 
520         cookie.setDomain("com");
521         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.com", 80, "/stuff", false)));
522 
523         cookie.setDomain("apache.com");
524         Assert.assertTrue(h.match(cookie, new CookieOrigin("apache.com", 80, "/stuff", false)));
525 
526         cookie.setDomain(".apache.com");
527         Assert.assertTrue(h.match(cookie, new CookieOrigin("www.apache.com", 80, "/stuff", false)));
528 
529         cookie.setDomain("localhost");
530         Assert.assertTrue(h.match(cookie, new CookieOrigin("localhost", 80, "/stuff", false)));
531     }
532 
533 }