1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2109Spec.java,v 1.3 2004/06/05 16:49:20 olegk Exp $
3    * $Revision: 290666 $
4    * $Date: 2005-09-21 05:30:56 -0400 (Wed, 21 Sep 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   */
28  
29  package org.apache.commons.httpclient.cookie;
30  
31  import junit.framework.Test;
32  import junit.framework.TestSuite;
33  
34  import org.apache.commons.httpclient.Cookie;
35  import org.apache.commons.httpclient.Header;
36  import org.apache.commons.httpclient.NameValuePair;
37  
38  /***
39   * Test cases for RFC2109 cookie spec
40   *
41   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
42   * 
43   * @version $Revision: 290666 $
44   */
45  public class TestCookieRFC2109Spec extends TestCookieBase {
46  
47  
48      // ------------------------------------------------------------ Constructor
49  
50      public TestCookieRFC2109Spec(String name) {
51          super(name);
52      }
53  
54      // ------------------------------------------------------- TestCase Methods
55  
56      public static Test suite() {
57          return new TestSuite(TestCookieRFC2109Spec.class);
58      }
59  
60      public void testParseAttributeInvalidAttrib() throws Exception {
61          CookieSpec cookiespec = new RFC2109Spec();
62          try {
63              cookiespec.parseAttribute(null, null);
64              fail("IllegalArgumentException must have been thrown");
65          } catch (IllegalArgumentException expected) {
66          }
67      }
68  
69      public void testParseAttributeInvalidCookie() throws Exception {
70          CookieSpec cookiespec = new RFC2109Spec();
71          try {
72              cookiespec.parseAttribute(new NameValuePair("name", "value"), null);
73              fail("IllegalArgumentException must have been thrown");
74          } catch (IllegalArgumentException expected) {
75          }
76      }
77  
78      public void testParseAttributeNullPath() throws Exception {
79          CookieSpec cookiespec = new RFC2109Spec();
80          try {
81              Cookie cookie = new Cookie();
82              cookiespec.parseAttribute(new NameValuePair("path", null), cookie);
83              fail("MalformedCookieException must have been thrown");
84          } catch (MalformedCookieException expected) {
85          }
86      }
87  
88      public void testParseAttributeBlankPath() throws Exception {
89          CookieSpec cookiespec = new RFC2109Spec();
90          try {
91              Cookie cookie = new Cookie();
92              cookiespec.parseAttribute(new NameValuePair("path", "   "), cookie);
93              fail("MalformedCookieException must have been thrown");
94          } catch (MalformedCookieException expected) {
95          }
96      }
97  
98      public void testParseAttributeNullVersion() throws Exception {
99          CookieSpec cookiespec = new RFC2109Spec();
100         try {
101             Cookie cookie = new Cookie();
102             cookiespec.parseAttribute(new NameValuePair("version", null), cookie);
103             fail("MalformedCookieException must have been thrown");
104         } catch (MalformedCookieException expected) {
105         }
106     }
107 
108     public void testParseAttributeInvalidVersion() throws Exception {
109         CookieSpec cookiespec = new RFC2109Spec();
110         try {
111             Cookie cookie = new Cookie();
112             cookiespec.parseAttribute(new NameValuePair("version", "nonsense"), cookie);
113             fail("MalformedCookieException must have been thrown");
114         } catch (MalformedCookieException expected) {
115         }
116     }
117 
118     public void testParseVersion() throws Exception {
119         Header header = new Header("Set-Cookie","cookie-name=cookie-value; version=1");
120 
121         CookieSpec cookiespec = new RFC2109Spec();
122         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
123         assertEquals("Found 1 cookie.",1,parsed.length);
124         assertEquals("Name","cookie-name",parsed[0].getName());
125         assertEquals("Value","cookie-value",parsed[0].getValue());
126         assertEquals("Version",1,parsed[0].getVersion());
127     }
128 
129     /***
130      * Test domain equals host 
131      */
132     public void testParseDomainEqualsHost() throws Exception {
133         Header header = new Header("Set-Cookie",
134             "cookie-name=cookie-value; domain=www.b.com; version=1");
135 
136         CookieSpec cookiespec = new RFC2109Spec();
137         Cookie[] parsed = cookieParse(cookiespec, "www.b.com", 80, "/", false, header);
138         assertNotNull(parsed);
139         assertEquals(1, parsed.length);
140         assertEquals("www.b.com", parsed[0].getDomain());
141     }
142 
143     /***
144      * Domain does not start with a dot
145      */
146     public void testParseWithIllegalDomain1() throws Exception {
147         Header header = new Header("Set-Cookie",
148             "cookie-name=cookie-value; domain=a.b.com; version=1");
149 
150         CookieSpec cookiespec = new RFC2109Spec();
151         try {
152             Cookie[] parsed = cookieParse(cookiespec, "www.a.b.com", 80, "/", false, header);
153             fail("MalformedCookieException should have been thrown");
154         } catch (MalformedCookieException e) {
155             // expected
156         }
157     }
158 
159     /***
160      * Domain must have alt least one embedded dot
161      */
162     public void testParseWithIllegalDomain2() throws Exception {
163         Header header = new Header("Set-Cookie",
164             "cookie-name=cookie-value; domain=.com; version=1");
165 
166         CookieSpec cookiespec = new RFC2109Spec();
167         try {
168             Cookie[] parsed = cookieParse(cookiespec, "b.com", 80, "/", false, header);
169             fail("MalformedCookieException should have been thrown");
170         } catch (MalformedCookieException e) {
171             // expected
172         }
173     }
174     /***
175      * Domain must have alt least one embedded dot
176      */
177     public void testParseWithIllegalDomain3() throws Exception {
178         Header header = new Header("Set-Cookie",
179             "cookie-name=cookie-value; domain=.com.; version=1");
180 
181         CookieSpec cookiespec = new RFC2109Spec();
182         try {
183             Cookie[] parsed = cookieParse(cookiespec, "b.com", 80, "/", false, header);
184             fail("HttpException exception should have been thrown");
185         } catch (MalformedCookieException e) {
186             // expected
187         }
188     }
189 
190     /***
191      * Host minus domain may not contain any dots
192      */
193     public void testParseWithIllegalDomain4() throws Exception {
194         Header header = new Header("Set-Cookie",
195             "cookie-name=cookie-value; domain=.c.com; version=1");
196 
197         CookieSpec cookiespec = new RFC2109Spec();
198         try {
199             Cookie[] parsed = cookieParse(cookiespec, "a.b.c.com", 80, "/", false, header);
200             fail("MalformedCookieException should have been thrown");
201         } catch (MalformedCookieException e) {
202             // expected
203         }
204     }
205 
206     /***
207      * Tests if that invalid second domain level cookie gets 
208      * rejected in the strict mode, but gets accepted in the
209      * browser compatibility mode.
210      */
211     public void testSecondDomainLevelCookie() throws Exception {
212         Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false); 
213         cookie.setDomainAttributeSpecified(true);
214         cookie.setPathAttributeSpecified(true);
215 
216         CookieSpec cookiespec = new RFC2109Spec();
217         try {
218             cookiespec.validate("sourceforge.net", 80, "/", false, cookie);
219             fail("MalformedCookieException should have been thrown");
220         } catch (MalformedCookieException e) {
221             // Expected
222         }
223     }    
224 
225     public void testSecondDomainLevelCookieMatch() throws Exception {
226         Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false); 
227         cookie.setDomainAttributeSpecified(true);
228         cookie.setPathAttributeSpecified(true);
229 
230         CookieSpec cookiespec = new RFC2109Spec();
231         assertFalse(cookiespec.match("sourceforge.net", 80, "/", false, cookie));
232     }
233     
234     public void testParseWithWrongPath() throws Exception {
235         Header header = new Header("Set-Cookie",
236             "cookie-name=cookie-value; domain=127.0.0.1; path=/not/just/root");
237 
238         CookieSpec cookiespec = new RFC2109Spec();
239         try {
240             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
241             fail("HttpException exception should have been thrown");
242         } catch (MalformedCookieException e) {
243             // expected
244         }
245     }
246 
247     /***
248      * Tests if cookie constructor rejects cookie name containing blanks.
249      */
250     public void testCookieNameWithBlanks() throws Exception {
251         Header setcookie = new Header("Set-Cookie", "invalid name=");
252         CookieSpec cookiespec = new RFC2109Spec();
253         try {
254             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, setcookie);
255             fail("MalformedCookieException exception should have been thrown");
256         } catch (MalformedCookieException e) {
257             // expected
258         }
259     }
260 
261 
262     /***
263      * Tests if cookie constructor rejects cookie name starting with $.
264      */
265     public void testCookieNameStartingWithDollarSign() throws Exception {
266         Header setcookie = new Header("Set-Cookie", "$invalid_name=");
267         CookieSpec cookiespec = new RFC2109Spec();
268         try {
269             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, setcookie);
270             fail("MalformedCookieException exception should have been thrown");
271         } catch (MalformedCookieException e) {
272             // expected
273         }
274     }
275 
276     /***
277      * Tests if default cookie validator rejects cookies originating from a host without domain
278      * where domain attribute does not match the host of origin 
279      */
280     public void testInvalidDomainWithSimpleHostName() throws Exception {    
281         CookieSpec cookiespec = new RFC2109Spec();
282         Header header = new Header("Set-Cookie", 
283             "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
284         Cookie[]cookies = cookiespec.parse("host", 80, "/", false, header );
285         try {
286             cookiespec.validate("host", 80, "/", false, cookies[0]);
287             fail("MalformedCookieException must have thrown");
288         }
289         catch(MalformedCookieException expected) {
290         }
291         header = new Header("Set-Cookie", 
292             "name=\"value\"; version=\"1\"; path=\"/\"; domain=\"host1\"");
293         cookies = cookiespec.parse("host2", 80, "/", false, header );
294         try {
295             cookiespec.validate("host2", 80, "/", false, cookies[0]);
296             fail("MalformedCookieException must have thrown");
297         }
298         catch(MalformedCookieException expected) {
299         }
300     }
301 
302     /***
303      * Tests if cookie values with embedded comma are handled correctly.
304      */
305     public void testCookieWithComma() throws Exception {
306         Header header = new Header("Set-Cookie", "a=b,c");
307 
308         CookieSpec cookiespec = new RFC2109Spec();
309         Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
310         assertEquals("number of cookies", 2, cookies.length);
311         assertEquals("a", cookies[0].getName());
312         assertEquals("b", cookies[0].getValue());
313         assertEquals("c", cookies[1].getName());
314         assertEquals(null, cookies[1].getValue());
315     }
316 
317     public void testFormatInvalidCookies() throws Exception {
318         CookieSpec cookiespec = new RFC2109Spec();
319         try {
320             String s = cookiespec.formatCookie(null);
321             fail("IllegalArgumentException nust have been thrown");
322         } catch (IllegalArgumentException expected) {
323         }
324     }    
325 
326     /***
327      * Tests RFC 2109 compiant cookie formatting.
328      */
329     public void testRFC2109CookieFormatting() throws Exception {
330         CookieSpec cookiespec = new RFC2109Spec();
331         Header header = new Header("Set-Cookie", 
332             "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
333         Cookie[] cookies  = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
334         cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
335         String s1 = cookiespec.formatCookie(cookies[0]);
336         assertEquals(s1, "$Version=\"1\"; name=\"value\"; $Path=\"/\"; $Domain=\".mydomain.com\"");
337 
338         header = new Header( "Set-Cookie", 
339             "name=value; path=/; domain=.mydomain.com");
340         cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
341         cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
342         String s2 = cookiespec.formatCookie(cookies[0]);
343         assertEquals(s2, "$Version=0; name=value; $Path=/; $Domain=.mydomain.com");
344     }
345 
346     public void testRFC2109CookiesFormatting() throws Exception {
347         CookieSpec cookiespec = new RFC2109Spec();
348         Header header = new Header("Set-Cookie", 
349             "name1=value1; path=/; domain=.mydomain.com, " + 
350             "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
351         Cookie[] cookies = cookieParse(cookiespec, "myhost.mydomain.com", 80, "/", false, header);
352         assertNotNull(cookies);
353         assertEquals(2, cookies.length);
354         String s1 = cookiespec.formatCookies(cookies);
355         assertEquals(s1, 
356             "$Version=0; name1=value1; $Path=/; $Domain=.mydomain.com; " + 
357             "name2=value2; $Path=/; $Domain=.mydomain.com");
358 
359         header = new Header("Set-Cookie", 
360             "name1=value1; version=1; path=/; domain=.mydomain.com, " + 
361             "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
362         cookies = cookieParse(cookiespec, "myhost.mydomain.com", 80, "/", false, header);
363         assertNotNull(cookies);
364         assertEquals(2, cookies.length);
365         String s2 = cookiespec.formatCookies(cookies);
366         assertEquals(s2, 
367             "$Version=\"1\"; name1=\"value1\"; $Path=\"/\"; $Domain=\".mydomain.com\"; " + 
368             "name2=\"value2\"; $Path=\"/\"; $Domain=\".mydomain.com\"");
369     }
370     
371     /***
372      * Tests if null cookie values are handled correctly.
373      */
374     public void testNullCookieValueFormatting() {
375         Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false); 
376         cookie.setDomainAttributeSpecified(true);
377         cookie.setPathAttributeSpecified(true);
378 
379         CookieSpec cookiespec = new RFC2109Spec();
380         String s = cookiespec.formatCookie(cookie);
381         assertEquals("$Version=0; name=; $Path=/; $Domain=.whatever.com", s);
382 
383         cookie.setVersion(1);
384         s = cookiespec.formatCookie(cookie);
385         assertEquals("$Version=\"1\"; name=\"\"; $Path=\"/\"; $Domain=\".whatever.com\"", s);
386     }
387 
388     public void testCookieNullDomainNullPathFormatting() {
389         Cookie cookie = new Cookie(null, "name", null, "/", null, false); 
390         cookie.setDomainAttributeSpecified(true);
391         cookie.setPathAttributeSpecified(true);
392 
393         CookieSpec cookiespec = new RFC2109Spec();
394         String s = cookiespec.formatCookie(cookie);
395         assertEquals("$Version=0; name=; $Path=/", s);
396 
397         cookie.setDomainAttributeSpecified(false);
398         cookie.setPathAttributeSpecified(false);
399         s = cookiespec.formatCookie(cookie);
400         assertEquals("$Version=0; name=", s);
401     }
402 
403 }
404