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 org.apache.http.cookie.CookieAttributeHandler;
31  import org.apache.http.cookie.CookieOrigin;
32  import org.apache.http.cookie.MalformedCookieException;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  public class TestRFC2109CookieAttribHandlers {
37  
38      @Test
39      public void testRFC2109DomainParse() throws Exception {
40          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
41          final CookieAttributeHandler h = new RFC2109DomainHandler();
42  
43          h.parse(cookie, "somehost");
44          Assert.assertEquals("somehost", cookie.getDomain());
45  
46          try {
47              h.parse(cookie, null);
48              Assert.fail("MalformedCookieException should have been thrown");
49          } catch (final MalformedCookieException ex) {
50              // expected
51          }
52          try {
53              h.parse(cookie, "  ");
54              Assert.fail("MalformedCookieException should have been thrown");
55          } catch (final MalformedCookieException ex) {
56              // expected
57          }
58      }
59  
60      @Test
61      public void testRFC2109DomainValidate1() throws Exception {
62          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
63          final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
64          final CookieAttributeHandler h = new RFC2109DomainHandler();
65  
66          cookie.setDomain("somehost");
67          h.validate(cookie, origin);
68  
69          cookie.setDomain("otherhost");
70          try {
71              h.validate(cookie, origin);
72              Assert.fail("MalformedCookieException should have been thrown");
73          } catch (final MalformedCookieException ex) {
74              // expected
75          }
76          cookie.setDomain(null);
77          try {
78              h.validate(cookie, origin);
79              Assert.fail("MalformedCookieException should have been thrown");
80          } catch (final MalformedCookieException ex) {
81              // expected
82          }
83      }
84  
85      @Test
86      public void testRFC2109DomainValidate2() throws Exception {
87          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
88          final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
89          final CookieAttributeHandler h = new RFC2109DomainHandler();
90  
91          cookie.setDomain(".somedomain.com");
92          h.validate(cookie, origin);
93  
94          cookie.setDomain(".otherdomain.com");
95          try {
96              h.validate(cookie, origin);
97              Assert.fail("MalformedCookieException should have been thrown");
98          } catch (final MalformedCookieException ex) {
99              // expected
100         }
101         cookie.setDomain("www.otherdomain.com");
102         try {
103             h.validate(cookie, origin);
104             Assert.fail("MalformedCookieException should have been thrown");
105         } catch (final MalformedCookieException ex) {
106             // expected
107         }
108     }
109 
110     @Test
111     public void testRFC2109DomainValidate3() throws Exception {
112         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
113         final CookieOrigin origin = new CookieOrigin("www.a.com", 80, "/", false);
114         final CookieAttributeHandler h = new RFC2109DomainHandler();
115 
116         cookie.setDomain(".a.com");
117         h.validate(cookie, origin);
118 
119         cookie.setDomain(".com");
120         try {
121             h.validate(cookie, origin);
122             Assert.fail("MalformedCookieException should have been thrown");
123         } catch (final MalformedCookieException ex) {
124             // expected
125         }
126     }
127 
128     @Test
129     public void testRFC2109DomainValidate4() throws Exception {
130         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
131         final CookieOrigin origin = new CookieOrigin("www.a.b.c", 80, "/", false);
132         final CookieAttributeHandler h = new RFC2109DomainHandler();
133 
134         cookie.setDomain(".a.b.c");
135         h.validate(cookie, origin);
136 
137         cookie.setDomain(".b.c");
138         try {
139             h.validate(cookie, origin);
140             Assert.fail("MalformedCookieException should have been thrown");
141         } catch (final MalformedCookieException ex) {
142             // expected
143         }
144         cookie.setDomain(".a.a.b.c");
145         try {
146             h.validate(cookie, origin);
147             Assert.fail("MalformedCookieException should have been thrown");
148         } catch (final MalformedCookieException ex) {
149             // expected
150         }
151     }
152 
153     @Test
154     public void testRFC2109DomainMatch1() throws Exception {
155         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
156         final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
157         final CookieAttributeHandler h = new RFC2109DomainHandler();
158 
159         cookie.setDomain(null);
160         Assert.assertFalse(h.match(cookie, origin));
161 
162         cookie.setDomain(".somedomain.com");
163         Assert.assertTrue(h.match(cookie, origin));
164     }
165 
166     @Test
167     public void testRFC2109DomainMatch2() throws Exception {
168         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
169         final CookieOrigin origin = new CookieOrigin("www.whatever.somedomain.com", 80, "/", false);
170         final CookieAttributeHandler h = new RFC2109DomainHandler();
171 
172         cookie.setDomain(".somedomain.com");
173         Assert.assertTrue(h.match(cookie, origin));
174     }
175 
176     @Test
177     public void testRFC2109DomainMatch3() throws Exception {
178         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
179         final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
180         final CookieAttributeHandler h = new RFC2109DomainHandler();
181 
182         cookie.setDomain("somedomain.com");
183         Assert.assertTrue(h.match(cookie, origin));
184     }
185 
186     @Test
187     public void testRFC2109DomainMatch4() throws Exception {
188         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
189         final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
190         final CookieAttributeHandler h = new RFC2109DomainHandler();
191 
192         cookie.setDomain("somedomain.com");
193         Assert.assertFalse(h.match(cookie, origin));
194     }
195 
196     @Test
197     public void testRFC2109DomainInvalidInput() throws Exception {
198         final CookieAttributeHandler h = new RFC2109DomainHandler();
199         try {
200             h.parse(null, null);
201             Assert.fail("IllegalArgumentException must have been thrown");
202         } catch (final IllegalArgumentException ex) {
203             // expected
204         }
205         try {
206             h.validate(null, null);
207             Assert.fail("IllegalArgumentException must have been thrown");
208         } catch (final IllegalArgumentException ex) {
209             // expected
210         }
211         try {
212             h.validate(new BasicClientCookie("name", "value"), null);
213             Assert.fail("IllegalArgumentException must have been thrown");
214         } catch (final IllegalArgumentException ex) {
215             // expected
216         }
217         try {
218             h.match(null, null);
219             Assert.fail("IllegalArgumentException must have been thrown");
220         } catch (final IllegalArgumentException ex) {
221             // expected
222         }
223         try {
224             h.match(new BasicClientCookie("name", "value"), null);
225             Assert.fail("IllegalArgumentException must have been thrown");
226         } catch (final IllegalArgumentException ex) {
227             // expected
228         }
229     }
230 
231     @Test
232     public void testRFC2109VersionParse() throws Exception {
233         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
234         final CookieAttributeHandler h = new RFC2109VersionHandler();
235         h.parse(cookie, "12");
236         Assert.assertEquals(12, cookie.getVersion());
237     }
238 
239     @Test
240     public void testRFC2109VersionParseInvalid() throws Exception {
241         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
242         final CookieAttributeHandler h = new RFC2109VersionHandler();
243         try {
244             h.parse(cookie, "garbage");
245             Assert.fail("MalformedCookieException must have been thrown");
246         } catch (final MalformedCookieException ex) {
247             // expected
248         }
249         try {
250             h.parse(cookie, null);
251             Assert.fail("MalformedCookieException must have been thrown");
252         } catch (final MalformedCookieException ex) {
253             // expected
254         }
255         try {
256             h.parse(cookie, "  ");
257             Assert.fail("MalformedCookieException must have been thrown");
258         } catch (final MalformedCookieException ex) {
259             // expected
260         }
261     }
262 
263     @Test
264     public void testRFC2109VersionValidate() throws Exception {
265         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
266         final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
267         final CookieAttributeHandler h = new RFC2109VersionHandler();
268 
269         cookie.setVersion(12);
270         h.validate(cookie, origin);
271 
272         cookie.setVersion(-12);
273         try {
274             h.validate(cookie, origin);
275             Assert.fail("MalformedCookieException must have been thrown");
276         } catch (final MalformedCookieException ex) {
277             // expected
278         }
279     }
280 
281     @Test
282     public void testRFC2109VersionInvalidInput() throws Exception {
283         final CookieAttributeHandler h = new RFC2109VersionHandler();
284         try {
285             h.parse(null, null);
286             Assert.fail("IllegalArgumentException must have been thrown");
287         } catch (final IllegalArgumentException ex) {
288             // expected
289         }
290         try {
291             h.validate(null, null);
292             Assert.fail("IllegalArgumentException must have been thrown");
293         } catch (final IllegalArgumentException ex) {
294             // expected
295         }
296     }
297 
298 }