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.2 $
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 default cookie validator rejects cookies originating from a host without domain
240 * where domain attribute does not match the host of origin
241 */
242 public void testInvalidDomainWithSimpleHostName() throws Exception {
243 CookieSpec cookiespec = new RFC2109Spec();
244 Header header = new Header("Set-Cookie",
245 "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
246 Cookie[]cookies = cookiespec.parse("host", 80, "/", false, header );
247 try {
248 cookiespec.validate("host", 80, "/", false, cookies[0]);
249 fail("MalformedCookieException must have thrown");
250 }
251 catch(MalformedCookieException expected) {
252 }
253 header = new Header("Set-Cookie",
254 "name=\"value\"; version=\"1\"; path=\"/\"; domain=\"host1\"");
255 cookies = cookiespec.parse("host2", 80, "/", false, header );
256 try {
257 cookiespec.validate("host2", 80, "/", false, cookies[0]);
258 fail("MalformedCookieException must have thrown");
259 }
260 catch(MalformedCookieException expected) {
261 }
262 }
263
264 /***
265 * Tests if cookie values with embedded comma are handled correctly.
266 */
267 public void testCookieWithComma() throws Exception {
268 Header header = new Header("Set-Cookie", "a=b,c");
269
270 CookieSpec cookiespec = new RFC2109Spec();
271 Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
272 assertEquals("number of cookies", 2, cookies.length);
273 assertEquals("a", cookies[0].getName());
274 assertEquals("b", cookies[0].getValue());
275 assertEquals("c", cookies[1].getName());
276 assertEquals(null, cookies[1].getValue());
277 }
278
279 public void testFormatInvalidCookies() throws Exception {
280 CookieSpec cookiespec = new RFC2109Spec();
281 try {
282 String s = cookiespec.formatCookie(null);
283 fail("IllegalArgumentException nust have been thrown");
284 } catch (IllegalArgumentException expected) {
285 }
286 }
287
288 /***
289 * Tests RFC 2109 compiant cookie formatting.
290 */
291 public void testRFC2109CookieFormatting() throws Exception {
292 CookieSpec cookiespec = new RFC2109Spec();
293 Header header = new Header("Set-Cookie",
294 "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
295 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
296 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
297 String s1 = cookiespec.formatCookie(cookies[0]);
298 assertEquals(s1, "$Version=\"1\"; name=\"value\"; $Domain=\".mydomain.com\"; $Path=\"/\"");
299
300 header = new Header( "Set-Cookie",
301 "name=value; path=/; domain=.mydomain.com");
302 cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
303 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
304 String s2 = cookiespec.formatCookie(cookies[0]);
305 assertEquals(s2, "$Version=0; name=value; $Domain=.mydomain.com; $Path=/");
306 }
307
308 public void testRFC2109CookiesFormatting() throws Exception {
309 CookieSpec cookiespec = new RFC2109Spec();
310 Header header = new Header("Set-Cookie",
311 "name1=value1; path=/; domain=.mydomain.com, " +
312 "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
313 Cookie[] cookies = cookieParse(cookiespec, "myhost.mydomain.com", 80, "/", false, header);
314 assertNotNull(cookies);
315 assertEquals(2, cookies.length);
316 String s1 = cookiespec.formatCookies(cookies);
317 assertEquals(s1,
318 "$Version=0; name1=value1; $Domain=.mydomain.com; $Path=/; " +
319 "name2=value2; $Domain=.mydomain.com; $Path=/");
320
321 header = new Header("Set-Cookie",
322 "name1=value1; version=1; path=/; domain=.mydomain.com, " +
323 "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
324 cookies = cookieParse(cookiespec, "myhost.mydomain.com", 80, "/", false, header);
325 assertNotNull(cookies);
326 assertEquals(2, cookies.length);
327 String s2 = cookiespec.formatCookies(cookies);
328 assertEquals(s2,
329 "$Version=\"1\"; name1=\"value1\"; $Domain=\".mydomain.com\"; $Path=\"/\"; " +
330 "name2=\"value2\"; $Domain=\".mydomain.com\"; $Path=\"/\"");
331 }
332
333 /***
334 * Tests if null cookie values are handled correctly.
335 */
336 public void testNullCookieValueFormatting() {
337 Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false);
338 cookie.setDomainAttributeSpecified(true);
339 cookie.setPathAttributeSpecified(true);
340
341 CookieSpec cookiespec = new RFC2109Spec();
342 String s = cookiespec.formatCookie(cookie);
343 assertEquals("$Version=0; name=; $Domain=.whatever.com; $Path=/", s);
344
345 cookie.setVersion(1);
346 s = cookiespec.formatCookie(cookie);
347 assertEquals("$Version=\"1\"; name=\"\"; $Domain=\".whatever.com\"; $Path=\"/\"", s);
348 }
349
350 public void testCookieNullDomainNullPathFormatting() {
351 Cookie cookie = new Cookie(null, "name", null, "/", null, false);
352 cookie.setDomainAttributeSpecified(true);
353 cookie.setPathAttributeSpecified(true);
354
355 CookieSpec cookiespec = new RFC2109Spec();
356 String s = cookiespec.formatCookie(cookie);
357 assertEquals("$Version=0; name=; $Path=/", s);
358
359 cookie.setDomainAttributeSpecified(false);
360 cookie.setPathAttributeSpecified(false);
361 s = cookiespec.formatCookie(cookie);
362 assertEquals("$Version=0; name=", s);
363 }
364
365 }
366