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.util.Calendar;
31  import java.util.Date;
32  
33  import org.apache.http.cookie.CookieAttributeHandler;
34  import org.apache.http.cookie.MalformedCookieException;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  public class TestLaxCookieAttribHandlers {
39  
40      @Test
41      public void testParseMaxAge() throws Exception {
42          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
43          final CookieAttributeHandler h = new LaxMaxAgeHandler();
44          h.parse(cookie, "2000");
45          Assert.assertNotNull(cookie.getExpiryDate());
46      }
47  
48      @Test
49      public void testParseMaxNegative() throws Exception {
50          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
51          final CookieAttributeHandler h = new LaxMaxAgeHandler();
52          h.parse(cookie, "-2000");
53          Assert.assertNotNull(cookie.getExpiryDate());
54      }
55  
56      @Test
57      public void testParseMaxZero() throws Exception {
58          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
59          final CookieAttributeHandler h = new LaxMaxAgeHandler();
60          h.parse(cookie, "0000");
61          Assert.assertNotNull(cookie.getExpiryDate());
62      }
63  
64      @Test
65      public void testBasicMaxAgeParseEmpty() throws Exception {
66          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
67          final CookieAttributeHandler h = new LaxMaxAgeHandler();
68          h.parse(cookie, "  ");
69          Assert.assertNull(cookie.getExpiryDate());
70      }
71  
72      @Test
73      public void testBasicMaxAgeParseInvalid() throws Exception {
74          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
75          final CookieAttributeHandler h = new LaxMaxAgeHandler();
76          h.parse(cookie, "garbage");
77          Assert.assertNull(cookie.getExpiryDate());
78      }
79  
80      @Test
81      public void testBasicMaxAgeInvalidInput() throws Exception {
82          final CookieAttributeHandler h = new LaxMaxAgeHandler();
83          try {
84              h.parse(null, "stuff");
85              Assert.fail("IllegalArgumentException must have been thrown");
86          } catch (final IllegalArgumentException ex) {
87              // expected
88          }
89      }
90  
91      @Test(expected = MalformedCookieException.class)
92      public void testExpiryGarbage() throws Exception {
93          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
94          final CookieAttributeHandler h = new LaxExpiresHandler();
95          h.parse(cookie, ";;blah,blah;yada  ");
96      }
97  
98      @Test
99      public void testParseExpiry() throws Exception {
100         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
101         final CookieAttributeHandler h = new LaxExpiresHandler();
102         h.parse(cookie, "1:0:12 8-jan-2012");
103 
104         final Date expiryDate = cookie.getExpiryDate();
105         Assert.assertNotNull(expiryDate);
106         final Calendar c = Calendar.getInstance();
107         c.setTimeZone(LaxExpiresHandler.UTC);
108         c.setTime(expiryDate);
109         Assert.assertEquals(2012, c.get(Calendar.YEAR));
110         Assert.assertEquals(Calendar.JANUARY, c.get(Calendar.MONTH));
111         Assert.assertEquals(8, c.get(Calendar.DAY_OF_MONTH));
112         Assert.assertEquals(1, c.get(Calendar.HOUR_OF_DAY));
113         Assert.assertEquals(0, c.get(Calendar.MINUTE));
114         Assert.assertEquals(12, c.get(Calendar.SECOND));
115         Assert.assertEquals(0, c.get(Calendar.MILLISECOND));
116     }
117 
118     @Test(expected = MalformedCookieException.class)
119     public void testParseExpiryInvalidTime1() throws Exception {
120         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
121         final CookieAttributeHandler h = new LaxExpiresHandler();
122         h.parse(cookie, "1:0:122 8 dec 1980");
123     }
124 
125     @Test(expected = MalformedCookieException.class)
126     public void testParseExpiryInvalidTime2() throws Exception {
127         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
128         final CookieAttributeHandler h = new LaxExpiresHandler();
129         h.parse(cookie, "24:00:00 8 dec 1980");
130     }
131 
132     @Test(expected = MalformedCookieException.class)
133     public void testParseExpiryInvalidTime3() throws Exception {
134         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
135         final CookieAttributeHandler h = new LaxExpiresHandler();
136         h.parse(cookie, "23:60:00 8 dec 1980");
137     }
138 
139     @Test(expected = MalformedCookieException.class)
140     public void testParseExpiryInvalidTime4() throws Exception {
141         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
142         final CookieAttributeHandler h = new LaxExpiresHandler();
143         h.parse(cookie, "23:00:60 8 dec 1980");
144     }
145 
146     @Test
147     public void testParseExpiryFunnyTime() throws Exception {
148         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
149         final CookieAttributeHandler h = new LaxExpiresHandler();
150         h.parse(cookie, "1:59:00blah; 8-feb-2000");
151 
152         final Date expiryDate = cookie.getExpiryDate();
153         Assert.assertNotNull(expiryDate);
154         final Calendar c = Calendar.getInstance();
155         c.setTimeZone(LaxExpiresHandler.UTC);
156         c.setTime(expiryDate);
157         Assert.assertEquals(2000, c.get(Calendar.YEAR));
158         Assert.assertEquals(Calendar.FEBRUARY, c.get(Calendar.MONTH));
159         Assert.assertEquals(8, c.get(Calendar.DAY_OF_MONTH));
160         Assert.assertEquals(1, c.get(Calendar.HOUR_OF_DAY));
161         Assert.assertEquals(59, c.get(Calendar.MINUTE));
162         Assert.assertEquals(0, c.get(Calendar.SECOND));
163         Assert.assertEquals(0, c.get(Calendar.MILLISECOND));
164     }
165 
166     @Test(expected = MalformedCookieException.class)
167     public void testParseExpiryInvalidDayOfMonth1() throws Exception {
168         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
169         final CookieAttributeHandler h = new LaxExpiresHandler();
170         h.parse(cookie, "12:00:00 888 mar 1880");
171     }
172 
173     @Test(expected = MalformedCookieException.class)
174     public void testParseExpiryInvalidDayOfMonth2() throws Exception {
175         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
176         final CookieAttributeHandler h = new LaxExpiresHandler();
177         h.parse(cookie, "12:00:00 0 mar 1880");
178     }
179 
180     @Test(expected = MalformedCookieException.class)
181     public void testParseExpiryInvalidDayOfMonth3() throws Exception {
182         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
183         final CookieAttributeHandler h = new LaxExpiresHandler();
184         h.parse(cookie, "12:00:00 32 mar 1880");
185     }
186 
187     @Test
188     public void testParseExpiryFunnyDayOfMonth() throws Exception {
189         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
190         final CookieAttributeHandler h = new LaxExpiresHandler();
191         h.parse(cookie, "12:00:00 8blah;mar;1880");
192 
193         final Date expiryDate = cookie.getExpiryDate();
194         Assert.assertNotNull(expiryDate);
195         final Calendar c = Calendar.getInstance();
196         c.setTimeZone(LaxExpiresHandler.UTC);
197         c.setTime(expiryDate);
198         Assert.assertEquals(1880, c.get(Calendar.YEAR));
199         Assert.assertEquals(Calendar.MARCH, c.get(Calendar.MONTH));
200         Assert.assertEquals(8, c.get(Calendar.DAY_OF_MONTH));
201         Assert.assertEquals(12, c.get(Calendar.HOUR_OF_DAY));
202         Assert.assertEquals(0, c.get(Calendar.MINUTE));
203         Assert.assertEquals(0, c.get(Calendar.SECOND));
204         Assert.assertEquals(0, c.get(Calendar.MILLISECOND));
205     }
206 
207     @Test(expected = MalformedCookieException.class)
208     public void testParseExpiryInvalidMonth() throws Exception {
209         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
210         final CookieAttributeHandler h = new LaxExpiresHandler();
211         h.parse(cookie, "1:00:00 8 dek 80");
212     }
213 
214     @Test
215     public void testParseExpiryFunnyMonth() throws Exception {
216         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
217         final CookieAttributeHandler h = new LaxExpiresHandler();
218         h.parse(cookie, "23:59:59; 1-ApriLLLLL-2008");
219 
220         final Date expiryDate = cookie.getExpiryDate();
221         Assert.assertNotNull(expiryDate);
222         final Calendar c = Calendar.getInstance();
223         c.setTimeZone(LaxExpiresHandler.UTC);
224         c.setTime(expiryDate);
225         Assert.assertEquals(2008, c.get(Calendar.YEAR));
226         Assert.assertEquals(Calendar.APRIL, c.get(Calendar.MONTH));
227         Assert.assertEquals(1, c.get(Calendar.DAY_OF_MONTH));
228         Assert.assertEquals(23, c.get(Calendar.HOUR_OF_DAY));
229         Assert.assertEquals(59, c.get(Calendar.MINUTE));
230         Assert.assertEquals(59, c.get(Calendar.SECOND));
231         Assert.assertEquals(0, c.get(Calendar.MILLISECOND));
232     }
233 
234     @Test(expected = MalformedCookieException.class)
235     public void testParseExpiryInvalidYearTooShort() throws Exception {
236         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
237         final CookieAttributeHandler h = new LaxExpiresHandler();
238         h.parse(cookie, "1:00:00 8 dec 8");
239     }
240 
241     @Test(expected = MalformedCookieException.class)
242     public void testParseExpiryInvalidYearTooLong() throws Exception {
243         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
244         final CookieAttributeHandler h = new LaxExpiresHandler();
245         h.parse(cookie, "1:00:00 8 dec 88888");
246     }
247 
248     @Test(expected = MalformedCookieException.class)
249     public void testParseExpiryInvalidYearTooLongAgo() throws Exception {
250         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
251         final CookieAttributeHandler h = new LaxExpiresHandler();
252         h.parse(cookie, "1:00:00 8 dec 1600");
253     }
254 
255     @Test
256     public void testParseExpiryFunnyYear() throws Exception {
257         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
258         final CookieAttributeHandler h = new LaxExpiresHandler();
259         h.parse(cookie, "23:59:59; 1-Apr-2008blah");
260 
261         final Date expiryDate = cookie.getExpiryDate();
262         Assert.assertNotNull(expiryDate);
263         final Calendar c = Calendar.getInstance();
264         c.setTimeZone(LaxExpiresHandler.UTC);
265         c.setTime(expiryDate);
266         Assert.assertEquals(2008, c.get(Calendar.YEAR));
267         Assert.assertEquals(Calendar.APRIL, c.get(Calendar.MONTH));
268         Assert.assertEquals(1, c.get(Calendar.DAY_OF_MONTH));
269         Assert.assertEquals(23, c.get(Calendar.HOUR_OF_DAY));
270         Assert.assertEquals(59, c.get(Calendar.MINUTE));
271         Assert.assertEquals(59, c.get(Calendar.SECOND));
272         Assert.assertEquals(0, c.get(Calendar.MILLISECOND));
273     }
274 
275     @Test
276     public void testParseExpiryYearTwoDigit1() throws Exception {
277         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
278         final CookieAttributeHandler h = new LaxExpiresHandler();
279         h.parse(cookie, "23:59:59; 1-Apr-70");
280 
281         final Date expiryDate = cookie.getExpiryDate();
282         Assert.assertNotNull(expiryDate);
283         final Calendar c = Calendar.getInstance();
284         c.setTimeZone(LaxExpiresHandler.UTC);
285         c.setTime(expiryDate);
286         Assert.assertEquals(1970, c.get(Calendar.YEAR));
287     }
288 
289     @Test
290     public void testParseExpiryYearTwoDigit2() throws Exception {
291         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
292         final CookieAttributeHandler h = new LaxExpiresHandler();
293         h.parse(cookie, "23:59:59; 1-Apr-99");
294 
295         final Date expiryDate = cookie.getExpiryDate();
296         Assert.assertNotNull(expiryDate);
297         final Calendar c = Calendar.getInstance();
298         c.setTimeZone(LaxExpiresHandler.UTC);
299         c.setTime(expiryDate);
300         Assert.assertEquals(1999, c.get(Calendar.YEAR));
301     }
302 
303     @Test
304     public void testParseExpiryYearTwoDigit3() throws Exception {
305         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
306         final CookieAttributeHandler h = new LaxExpiresHandler();
307         h.parse(cookie, "23:59:59; 1-Apr-00");
308 
309         final Date expiryDate = cookie.getExpiryDate();
310         Assert.assertNotNull(expiryDate);
311         final Calendar c = Calendar.getInstance();
312         c.setTimeZone(LaxExpiresHandler.UTC);
313         c.setTime(expiryDate);
314         Assert.assertEquals(2000, c.get(Calendar.YEAR));
315     }
316 
317 }