blob: cf93df935e9508b559466cf88edbb760f1928080 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohairf5857212010-12-28 15:53:50 -08002 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
ohair2283b9d2010-05-25 15:58:33 -070019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
duke6e45e102007-12-01 00:00:00 +000022 */
23
24/**
25 * @test
26 * @summary Unit test for java.net.HttpCookie
jccolletc6e315b2009-11-20 14:50:55 +010027 * @bug 6244040 6277796 6277801 6277808 6294071 6692802 6790677 6901170
duke6e45e102007-12-01 00:00:00 +000028 * @author Edward Wang
29 */
30
31import java.net.HttpCookie;
32import java.util.List;
33
34public class TestHttpCookie {
35 private static int testCount = 0;
36
37 private String cHeader = null;
38 private List<HttpCookie> cookies = null;
39
40 // test case here expressed as a string, which represents
41 // the header string to be parsed into HttpCookie instance.
42 // A TestHttpCookie instance will be created to hold such a HttpCookie
43 // object, and TestHttpCookie class has utility methods to check equality
44 // between HttpCookie's real property and expected property.
45 static TestHttpCookie test(String cookieHeader) {
46 testCount++;
47 return new TestHttpCookie(cookieHeader);
48 }
49
50 TestHttpCookie(String cHeader) {
duke6e45e102007-12-01 00:00:00 +000051 this.cHeader = cHeader;
52
53 try {
54 List<HttpCookie> cookies = HttpCookie.parse(cHeader);
55 this.cookies = cookies;
56 } catch (IllegalArgumentException ignored) {
57 cookies = null;
58 }
59 }
60
61 // check name
62 TestHttpCookie n(int index, String n) {
63 HttpCookie cookie = cookies.get(index);
64 if (cookie == null || !n.equalsIgnoreCase(cookie.getName())) {
65 raiseError("name", cookie.getName(), n);
66 }
67
68 return this;
69 }
70 TestHttpCookie n(String n) { return n(0, n); }
71
72 // check value
73 TestHttpCookie v(int index, String v) {
74 HttpCookie cookie = cookies.get(index);
75 if (cookie == null || !v.equals(cookie.getValue())) {
76 raiseError("value", cookie.getValue(), v);
77 }
78
79 return this;
80 }
81 TestHttpCookie v(String v) { return v(0, v); }
82
83 // check version
84 TestHttpCookie ver(int index, int ver) {
85 HttpCookie cookie = cookies.get(index);
86 if (cookie == null || (ver != cookie.getVersion())) {
87 raiseError("version", Integer.toString(cookie.getVersion()), Integer.toString(ver));
88 }
89
90 return this;
91 }
92 TestHttpCookie ver(int ver) { return ver(0, ver); }
93
94 // check path
95 TestHttpCookie p(int index, String p) {
96 HttpCookie cookie = cookies.get(index);
97 if (cookie == null || !p.equals(cookie.getPath())) {
98 raiseError("path", cookie.getPath(), p);
99 }
100
101 return this;
102 }
103 TestHttpCookie p(String p) { return p(0, p); }
104
105 // check null-ability
106 TestHttpCookie nil() {
107 if (cookies != null) {
108 raiseError("Check null-ability fail");
109 }
110
111 return this;
112 }
113
114 // check comment
115 TestHttpCookie c(int index, String c) {
116 HttpCookie cookie = cookies.get(index);
117 if (cookie == null || !c.equals(cookie.getComment())) {
118 raiseError("comment", cookie.getComment(), c);
119 }
120
121 return this;
122 }
123 TestHttpCookie c(String c) { return c(0, c); }
124
125 // check comment url
126 TestHttpCookie cu(int index, String cu) {
127 HttpCookie cookie = cookies.get(index);
128 if (cookie == null || !cu.equals(cookie.getCommentURL())) {
129 raiseError("comment url", cookie.getCommentURL(), cu);
130 }
131
132 return this;
133 }
134 TestHttpCookie cu(String cu) { return cu(0, cu); }
135
136 // check discard
137 TestHttpCookie dsc(int index, boolean dsc) {
138 HttpCookie cookie = cookies.get(index);
139 if (cookie == null || (dsc != cookie.getDiscard())) {
140 raiseError("discard", Boolean.toString(cookie.getDiscard()), Boolean.toString(dsc));
141 }
142
143 return this;
144 }
145 TestHttpCookie dsc(boolean dsc) { return dsc(0, dsc); }
146
147 // check domain
148 TestHttpCookie d(int index, String d) {
149 HttpCookie cookie = cookies.get(index);
150 if (cookie == null || !d.equalsIgnoreCase(cookie.getDomain())) {
151 raiseError("domain", cookie.getDomain(), d);
152 }
153
154 return this;
155 }
156 TestHttpCookie d(String d) { return d(0, d); }
157
158 // check max-age
159 TestHttpCookie a(int index, long a) {
160 HttpCookie cookie = cookies.get(index);
161 if (cookie == null || (a != cookie.getMaxAge())) {
162 raiseError("max-age", Long.toString(cookie.getMaxAge()), Long.toString(a));
163 }
164
165 return this;
166 }
167 TestHttpCookie a(long a) { return a(0, a); }
168
169 // check port list
170 TestHttpCookie port(int index, String p) {
171 HttpCookie cookie = cookies.get(index);
172 if (cookie == null || !p.equals(cookie.getPortlist())) {
173 raiseError("portlist", cookie.getPortlist(), p);
174 }
175
176 return this;
177 }
178 TestHttpCookie port(String p) { return port(0, p); }
179
jccollet10646082008-09-04 15:26:53 +0200180 // check http only
181 TestHttpCookie httpOnly(int index, boolean b) {
182 HttpCookie cookie = cookies.get(index);
183 if (cookie == null || b != cookie.isHttpOnly()) {
184 raiseError("HttpOnly", String.valueOf(cookie.isHttpOnly()), String.valueOf(b));
185 }
186 return this;
187 }
188
189 TestHttpCookie httpOnly(boolean b) {
190 return httpOnly(0, b);
191 }
192
duke6e45e102007-12-01 00:00:00 +0000193 // check equality
194 static void eq(HttpCookie ck1, HttpCookie ck2, boolean same) {
195 testCount++;
196 if (ck1.equals(ck2) != same) {
197 raiseError("Comparison inconsistent: " + ck1 + " " + ck2
198 + " should " + (same ? "equal" : "not equal"));
199 }
200
201 int h1 = ck1.hashCode();
202 int h2 = ck2.hashCode();
203 if ((h1 == h2) != same) {
204 raiseError("Comparison inconsistent: hashCode for " + ck1 + " " + ck2
205 + " should " + (same ? "equal" : "not equal"));
206 }
207 }
208
209 // check domainMatches()
210 static void dm(String domain, String host, boolean matches) {
211 testCount++;
212 if (HttpCookie.domainMatches(domain, host) != matches) {
213 raiseError("Host " + host + (matches?" should ":" should not ") +
214 "domain-match with domain " + domain);
215 }
216 }
217
218 void raiseError(String attr, String realValue, String expectedValue) {
219 StringBuilder sb = new StringBuilder();
220 sb.append("Cookie ").append(attr).append(" is ").append(realValue).
221 append(", should be ").append(expectedValue).
222 append(" (").append(cHeader).append(")");
223 throw new RuntimeException(sb.toString());
224 }
225
226 static void raiseError(String prompt) {
227 throw new RuntimeException(prompt);
228 }
229
230 static void runTests() {
231 rfc2965();
232 netscape();
233 misc();
234 }
235
236 static void rfc2965() {
237 header("Test using rfc 2965 syntax");
238
239 test("set-cookie2: Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"")
240 .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme");
241
242 // whitespace between attr and = sign
243 test("set-cookie2: Customer = \"WILE_E_COYOTE\"; Version = \"1\"; Path = \"/acme\"")
244 .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme");
245
246 // $NAME is reserved; result should be null
247 test("set-cookie2: $Customer = \"WILE_E_COYOTE\"; Version = \"1\"; Path = \"/acme\"")
248 .nil();
249
250 // a 'full' cookie
251 test("set-cookie2: Customer=\"WILE_E_COYOTE\"" +
252 ";Version=\"1\"" +
253 ";Path=\"/acme\"" +
254 ";Comment=\"this is a coyote\"" +
255 ";CommentURL=\"http://www.coyote.org\"" +
256 ";Discard" +
257 ";Domain=\".coyote.org\"" +
258 ";Max-Age=\"3600\"" +
259 ";Port=\"80\"" +
260 ";Secure")
261 .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme")
262 .c("this is a coyote").cu("http://www.coyote.org").dsc(true)
263 .d(".coyote.org").a(3600).port("80");
264
265 // a 'full' cookie, without leading set-cookie2 token
266 test("Customer=\"WILE_E_COYOTE\"" +
267 ";Version=\"1\"" +
268 ";Path=\"/acme\"" +
269 ";Comment=\"this is a coyote\"" +
270 ";CommentURL=\"http://www.coyote.org\"" +
271 ";Discard" +
272 ";Domain=\".coyote.org\"" +
273 ";Max-Age=\"3600\"" +
274 ";Port=\"80\"" +
275 ";Secure")
276 .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme")
277 .c("this is a coyote").cu("http://www.coyote.org").dsc(true)
278 .d(".coyote.org").a(3600).port("80");
279
duke6e45e102007-12-01 00:00:00 +0000280 // empty set-cookie string
281 test("").nil();
282
283 // NullPointerException expected
284 try {
285 test(null);
286 } catch (NullPointerException ignored) {
287 // no-op
288 }
289
290 // bug 6277796
291 test("Set-Cookie2:Customer=\"dtftest\"; Discard; Secure; Domain=\".sun.com\"; Max-Age=\"100\"; Version=\"1\"; path=\"/www\"; Port=\"80\"")
292 .n("Customer").v("dtftest").ver(1).d(".sun.com").p("/www").port("80").dsc(true).a(100);
293
294 // bug 6277801
295 test("Set-Cookie2:Customer=\"dtftest\"; Discard; Secure; Domain=\".sun.com\"; Max-Age=\"100\"; Version=\"1\"; path=\"/www\"; Port=\"80\"" +
296 ";Domain=\".java.sun.com\"; Max-Age=\"200\"; path=\"/javadoc\"; Port=\"8080\"")
297 .n("Customer").v("dtftest").ver(1).d(".sun.com").p("/www").port("80").dsc(true).a(100);
298
299 // bug 6294071
300 test("Set-Cookie2:Customer=\"dtftest\";Discard; Secure; Domain=\"sun.com\"; Max-Age=\"100\";Version=\"1\"; Path=\"/www\"; Port=\"80,8080\"")
301 .n("Customer").v("dtftest").ver(1).d("sun.com").p("/www").port("80,8080").dsc(true).a(100);
302 test("Set-Cookie2:Customer=\"developer\";Domain=\"sun.com\";Max-Age=\"100\";Path=\"/www\";Port=\"80,8080\";CommentURL=\"http://www.sun.com/java1,000,000.html\"")
303 .n("Customer").v("developer").d("sun.com").p("/www").port("80,8080").a(100).cu("http://www.sun.com/java1,000,000.html");
304
305 // a header string contains 2 cookies
306 test("Set-Cookie2:C1=\"V1\";Domain=\".sun1.com\";path=\"/www1\";Max-Age=\"100\",C2=\"V2\";Domain=\".sun2.com\";path=\"/www2\";Max-Age=\"200\"")
307 .n(0, "C1").v(0, "V1").p(0, "/www1").a(0, 100).d(0, ".sun1.com")
308 .n(1, "C2").v(1, "V2").p(1, "/www2").a(1, 200).d(1, ".sun2.com");
jccollet5408fca2009-01-27 11:36:28 +0100309
310 // Bug 6790677: Should ignore bogus attributes
311 test("Set-Cookie2:C1=\"V1\";foobar").n(0, "C1").v(0, "V1");
duke6e45e102007-12-01 00:00:00 +0000312 }
313
314 static void netscape() {
315 header("Test using netscape cookie syntax");
316
317 test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT")
318 .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
319
320 // a Netscape cookie, without set-cookie leading token
321 test("CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT")
322 .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
323
324 // a 'google' cookie
325 test("Set-Cookie: PREF=ID=1eda537de48ac25d:CR=1:TM=1112868587:LM=1112868587:S=t3FPA-mT9lTR3bxU;" +
326 "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com")
327 .n("PREF").v("ID=1eda537de48ac25d:CR=1:TM=1112868587:LM=1112868587:S=t3FPA-mT9lTR3bxU")
328 .p("/").d(".google.com").ver(0);
329
330 // bug 6277796
331 test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; Secure")
332 .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
333
334 // bug 6277801
335 test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; path=\"/acme\"")
336 .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
jccolletc6e315b2009-11-20 14:50:55 +0100337
338 // bug 6901170
339 test("set-cookie: CUSTOMER=WILE_E_COYOTE; version='1'").ver(1);
duke6e45e102007-12-01 00:00:00 +0000340 }
341
342 static void misc() {
343 header("Test equals()");
344
345 // test equals()
346 HttpCookie c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
347 c1.setDomain(".coyote.org");
348 c1.setPath("/acme");
349 HttpCookie c2 = (HttpCookie)c1.clone();
350 eq(c1, c2, true);
351
352 // test equals() when domain and path are null
353 c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
354 c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");
355 eq(c1, c2, true);
356
357 // path is case-sensitive
358 c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
359 c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");
360 c1.setPath("/acme");
361 c2.setPath("/ACME");
362 eq(c1, c2, false);
363
364 header("Test domainMatches()");
chegard2015d42011-08-22 14:09:28 +0100365 dm(".foo.com", "y.x.foo.com", false);
366 dm(".foo.com", "x.foo.com", true);
367 dm(".com", "whatever.com", false);
368 dm(".com.", "whatever.com", false);
369 dm(".ajax.com", "ajax.com", true);
370 dm(".local", "example.local", true);
371 dm("example.local", "example", true);
duke6e45e102007-12-01 00:00:00 +0000372
373 // bug 6277808
374 testCount++;
375 try {
376 c1 = new HttpCookie("", "whatever");
377 } catch (IllegalArgumentException ignored) {
378 // expected exception; no-op
379 }
jccollet10646082008-09-04 15:26:53 +0200380
381 // CR 6692802: HttpOnly flag
382 test("set-cookie: CUSTOMER=WILE_E_COYOTE;HttpOnly").httpOnly(true);
383 test("set-cookie: CUSTOMER=WILE_E_COYOTE").httpOnly(false);
duke6e45e102007-12-01 00:00:00 +0000384 }
385
386 static void header(String prompt) {
387 System.out.println("== " + prompt + " ==");
388 }
389
390 public static void main(String[] args) {
391 runTests();
392
393 System.out.println("Succeeded in running " + testCount + " tests.");
394 }
395}