1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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: 1.3 $
44 */
45 public class TestCookieRFC2109Spec extends TestCookieBase {
46
47
48
49
50 public TestCookieRFC2109Spec(String name) {
51 super(name);
52 }
53
54
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
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
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
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
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
222 }
223 }
224
225 public void testParseWithWrongPath() throws Exception {
226 Header header = new Header("Set-Cookie",
227 "cookie-name=cookie-value; domain=127.0.0.1; path=/not/just/root");
228
229 CookieSpec cookiespec = new RFC2109Spec();
230 try {
231 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
232 fail("HttpException exception should have been thrown");
233 } catch (MalformedCookieException e) {
234
235 }
236 }
237
238 /***
239 * Tests if cookie constructor rejects cookie name containing blanks.
240 */
241 public void testCookieNameWithBlanks() throws Exception {
242 Header setcookie = new Header("Set-Cookie", "invalid name=");
243 CookieSpec cookiespec = new RFC2109Spec();
244 try {
245 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, setcookie);
246 fail("MalformedCookieException exception should have been thrown");
247 } catch (MalformedCookieException e) {
248
249 }
250 }
251
252
253 /***
254 * Tests if cookie constructor rejects cookie name starting with $.
255 */
256 public void testCookieNameStartingWithDollarSign() throws Exception {
257 Header setcookie = new Header("Set-Cookie", "$invalid_name=");
258 CookieSpec cookiespec = new RFC2109Spec();
259 try {
260 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, setcookie);
261 fail("MalformedCookieException exception should have been thrown");
262 } catch (MalformedCookieException e) {
263
264 }
265 }
266
267 /***
268 * Tests if default cookie validator rejects cookies originating from a host without domain
269 * where domain attribute does not match the host of origin
270 */
271 public void testInvalidDomainWithSimpleHostName() throws Exception {
272 CookieSpec cookiespec = new RFC2109Spec();
273 Header header = new Header("Set-Cookie",
274 "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
275 Cookie[]cookies = cookiespec.parse("host", 80, "/", false, header );
276 try {
277 cookiespec.validate("host", 80, "/", false, cookies[0]);
278 fail("MalformedCookieException must have thrown");
279 }
280 catch(MalformedCookieException expected) {
281 }
282 header = new Header("Set-Cookie",
283 "name=\"value\"; version=\"1\"; path=\"/\"; domain=\"host1\"");
284 cookies = cookiespec.parse("host2", 80, "/", false, header );
285 try {
286 cookiespec.validate("host2", 80, "/", false, cookies[0]);
287 fail("MalformedCookieException must have thrown");
288 }
289 catch(MalformedCookieException expected) {
290 }
291 }
292
293 /***
294 * Tests if cookie values with embedded comma are handled correctly.
295 */
296 public void testCookieWithComma() throws Exception {
297 Header header = new Header("Set-Cookie", "a=b,c");
298
299 CookieSpec cookiespec = new RFC2109Spec();
300 Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
301 assertEquals("number of cookies", 2, cookies.length);
302 assertEquals("a", cookies[0].getName());
303 assertEquals("b", cookies[0].getValue());
304 assertEquals("c", cookies[1].getName());
305 assertEquals(null, cookies[1].getValue());
306 }
307
308 public void testFormatInvalidCookies() throws Exception {
309 CookieSpec cookiespec = new RFC2109Spec();
310 try {
311 String s = cookiespec.formatCookie(null);
312 fail("IllegalArgumentException nust have been thrown");
313 } catch (IllegalArgumentException expected) {
314 }
315 }
316
317 /***
318 * Tests RFC 2109 compiant cookie formatting.
319 */
320 public void testRFC2109CookieFormatting() throws Exception {
321 CookieSpec cookiespec = new RFC2109Spec();
322 Header header = new Header("Set-Cookie",
323 "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
324 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
325 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
326 String s1 = cookiespec.formatCookie(cookies[0]);
327 assertEquals(s1, "$Version=\"1\"; name=\"value\"; $Domain=\".mydomain.com\"; $Path=\"/\"");
328
329 header = new Header( "Set-Cookie",
330 "name=value; path=/; domain=.mydomain.com");
331 cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
332 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
333 String s2 = cookiespec.formatCookie(cookies[0]);
334 assertEquals(s2, "$Version=0; name=value; $Domain=.mydomain.com; $Path=/");
335 }
336
337 public void testRFC2109CookiesFormatting() throws Exception {
338 CookieSpec cookiespec = new RFC2109Spec();
339 Header header = new Header("Set-Cookie",
340 "name1=value1; path=/; domain=.mydomain.com, " +
341 "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
342 Cookie[] cookies = cookieParse(cookiespec, "myhost.mydomain.com", 80, "/", false, header);
343 assertNotNull(cookies);
344 assertEquals(2, cookies.length);
345 String s1 = cookiespec.formatCookies(cookies);
346 assertEquals(s1,
347 "$Version=0; name1=value1; $Domain=.mydomain.com; $Path=/; " +
348 "name2=value2; $Domain=.mydomain.com; $Path=/");
349
350 header = new Header("Set-Cookie",
351 "name1=value1; version=1; path=/; domain=.mydomain.com, " +
352 "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
353 cookies = cookieParse(cookiespec, "myhost.mydomain.com", 80, "/", false, header);
354 assertNotNull(cookies);
355 assertEquals(2, cookies.length);
356 String s2 = cookiespec.formatCookies(cookies);
357 assertEquals(s2,
358 "$Version=\"1\"; name1=\"value1\"; $Domain=\".mydomain.com\"; $Path=\"/\"; " +
359 "name2=\"value2\"; $Domain=\".mydomain.com\"; $Path=\"/\"");
360 }
361
362 /***
363 * Tests if null cookie values are handled correctly.
364 */
365 public void testNullCookieValueFormatting() {
366 Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false);
367 cookie.setDomainAttributeSpecified(true);
368 cookie.setPathAttributeSpecified(true);
369
370 CookieSpec cookiespec = new RFC2109Spec();
371 String s = cookiespec.formatCookie(cookie);
372 assertEquals("$Version=0; name=; $Domain=.whatever.com; $Path=/", s);
373
374 cookie.setVersion(1);
375 s = cookiespec.formatCookie(cookie);
376 assertEquals("$Version=\"1\"; name=\"\"; $Domain=\".whatever.com\"; $Path=\"/\"", s);
377 }
378
379 public void testCookieNullDomainNullPathFormatting() {
380 Cookie cookie = new Cookie(null, "name", null, "/", null, false);
381 cookie.setDomainAttributeSpecified(true);
382 cookie.setPathAttributeSpecified(true);
383
384 CookieSpec cookiespec = new RFC2109Spec();
385 String s = cookiespec.formatCookie(cookie);
386 assertEquals("$Version=0; name=; $Path=/", s);
387
388 cookie.setDomainAttributeSpecified(false);
389 cookie.setPathAttributeSpecified(false);
390 s = cookiespec.formatCookie(cookie);
391 assertEquals("$Version=0; name=", s);
392 }
393
394 }
395