blob: 43d5484e27cd954a965822225c9c4b03df67caca [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
xdono53d0f662008-10-02 19:58:32 -07002 * Copyright 2005-2008 Sun Microsystems, Inc. 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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @summary Unit test for java.net.HttpCookie
jccollet10646082008-09-04 15:26:53 +020027 * @bug 6244040 6277796 6277801 6277808 6294071 6692802
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) {
51 assert cHeader != null;
52 this.cHeader = cHeader;
53
54 try {
55 List<HttpCookie> cookies = HttpCookie.parse(cHeader);
56 this.cookies = cookies;
57 } catch (IllegalArgumentException ignored) {
58 cookies = null;
59 }
60 }
61
62 // check name
63 TestHttpCookie n(int index, String n) {
64 HttpCookie cookie = cookies.get(index);
65 if (cookie == null || !n.equalsIgnoreCase(cookie.getName())) {
66 raiseError("name", cookie.getName(), n);
67 }
68
69 return this;
70 }
71 TestHttpCookie n(String n) { return n(0, n); }
72
73 // check value
74 TestHttpCookie v(int index, String v) {
75 HttpCookie cookie = cookies.get(index);
76 if (cookie == null || !v.equals(cookie.getValue())) {
77 raiseError("value", cookie.getValue(), v);
78 }
79
80 return this;
81 }
82 TestHttpCookie v(String v) { return v(0, v); }
83
84 // check version
85 TestHttpCookie ver(int index, int ver) {
86 HttpCookie cookie = cookies.get(index);
87 if (cookie == null || (ver != cookie.getVersion())) {
88 raiseError("version", Integer.toString(cookie.getVersion()), Integer.toString(ver));
89 }
90
91 return this;
92 }
93 TestHttpCookie ver(int ver) { return ver(0, ver); }
94
95 // check path
96 TestHttpCookie p(int index, String p) {
97 HttpCookie cookie = cookies.get(index);
98 if (cookie == null || !p.equals(cookie.getPath())) {
99 raiseError("path", cookie.getPath(), p);
100 }
101
102 return this;
103 }
104 TestHttpCookie p(String p) { return p(0, p); }
105
106 // check null-ability
107 TestHttpCookie nil() {
108 if (cookies != null) {
109 raiseError("Check null-ability fail");
110 }
111
112 return this;
113 }
114
115 // check comment
116 TestHttpCookie c(int index, String c) {
117 HttpCookie cookie = cookies.get(index);
118 if (cookie == null || !c.equals(cookie.getComment())) {
119 raiseError("comment", cookie.getComment(), c);
120 }
121
122 return this;
123 }
124 TestHttpCookie c(String c) { return c(0, c); }
125
126 // check comment url
127 TestHttpCookie cu(int index, String cu) {
128 HttpCookie cookie = cookies.get(index);
129 if (cookie == null || !cu.equals(cookie.getCommentURL())) {
130 raiseError("comment url", cookie.getCommentURL(), cu);
131 }
132
133 return this;
134 }
135 TestHttpCookie cu(String cu) { return cu(0, cu); }
136
137 // check discard
138 TestHttpCookie dsc(int index, boolean dsc) {
139 HttpCookie cookie = cookies.get(index);
140 if (cookie == null || (dsc != cookie.getDiscard())) {
141 raiseError("discard", Boolean.toString(cookie.getDiscard()), Boolean.toString(dsc));
142 }
143
144 return this;
145 }
146 TestHttpCookie dsc(boolean dsc) { return dsc(0, dsc); }
147
148 // check domain
149 TestHttpCookie d(int index, String d) {
150 HttpCookie cookie = cookies.get(index);
151 if (cookie == null || !d.equalsIgnoreCase(cookie.getDomain())) {
152 raiseError("domain", cookie.getDomain(), d);
153 }
154
155 return this;
156 }
157 TestHttpCookie d(String d) { return d(0, d); }
158
159 // check max-age
160 TestHttpCookie a(int index, long a) {
161 HttpCookie cookie = cookies.get(index);
162 if (cookie == null || (a != cookie.getMaxAge())) {
163 raiseError("max-age", Long.toString(cookie.getMaxAge()), Long.toString(a));
164 }
165
166 return this;
167 }
168 TestHttpCookie a(long a) { return a(0, a); }
169
170 // check port list
171 TestHttpCookie port(int index, String p) {
172 HttpCookie cookie = cookies.get(index);
173 if (cookie == null || !p.equals(cookie.getPortlist())) {
174 raiseError("portlist", cookie.getPortlist(), p);
175 }
176
177 return this;
178 }
179 TestHttpCookie port(String p) { return port(0, p); }
180
jccollet10646082008-09-04 15:26:53 +0200181 // check http only
182 TestHttpCookie httpOnly(int index, boolean b) {
183 HttpCookie cookie = cookies.get(index);
184 if (cookie == null || b != cookie.isHttpOnly()) {
185 raiseError("HttpOnly", String.valueOf(cookie.isHttpOnly()), String.valueOf(b));
186 }
187 return this;
188 }
189
190 TestHttpCookie httpOnly(boolean b) {
191 return httpOnly(0, b);
192 }
193
duke6e45e102007-12-01 00:00:00 +0000194 // check equality
195 static void eq(HttpCookie ck1, HttpCookie ck2, boolean same) {
196 testCount++;
197 if (ck1.equals(ck2) != same) {
198 raiseError("Comparison inconsistent: " + ck1 + " " + ck2
199 + " should " + (same ? "equal" : "not equal"));
200 }
201
202 int h1 = ck1.hashCode();
203 int h2 = ck2.hashCode();
204 if ((h1 == h2) != same) {
205 raiseError("Comparison inconsistent: hashCode for " + ck1 + " " + ck2
206 + " should " + (same ? "equal" : "not equal"));
207 }
208 }
209
210 // check domainMatches()
211 static void dm(String domain, String host, boolean matches) {
212 testCount++;
213 if (HttpCookie.domainMatches(domain, host) != matches) {
214 raiseError("Host " + host + (matches?" should ":" should not ") +
215 "domain-match with domain " + domain);
216 }
217 }
218
219 void raiseError(String attr, String realValue, String expectedValue) {
220 StringBuilder sb = new StringBuilder();
221 sb.append("Cookie ").append(attr).append(" is ").append(realValue).
222 append(", should be ").append(expectedValue).
223 append(" (").append(cHeader).append(")");
224 throw new RuntimeException(sb.toString());
225 }
226
227 static void raiseError(String prompt) {
228 throw new RuntimeException(prompt);
229 }
230
231 static void runTests() {
232 rfc2965();
233 netscape();
234 misc();
235 }
236
237 static void rfc2965() {
238 header("Test using rfc 2965 syntax");
239
240 test("set-cookie2: Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"")
241 .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme");
242
243 // whitespace between attr and = sign
244 test("set-cookie2: Customer = \"WILE_E_COYOTE\"; Version = \"1\"; Path = \"/acme\"")
245 .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme");
246
247 // $NAME is reserved; result should be null
248 test("set-cookie2: $Customer = \"WILE_E_COYOTE\"; Version = \"1\"; Path = \"/acme\"")
249 .nil();
250
251 // a 'full' cookie
252 test("set-cookie2: Customer=\"WILE_E_COYOTE\"" +
253 ";Version=\"1\"" +
254 ";Path=\"/acme\"" +
255 ";Comment=\"this is a coyote\"" +
256 ";CommentURL=\"http://www.coyote.org\"" +
257 ";Discard" +
258 ";Domain=\".coyote.org\"" +
259 ";Max-Age=\"3600\"" +
260 ";Port=\"80\"" +
261 ";Secure")
262 .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme")
263 .c("this is a coyote").cu("http://www.coyote.org").dsc(true)
264 .d(".coyote.org").a(3600).port("80");
265
266 // a 'full' cookie, without leading set-cookie2 token
267 test("Customer=\"WILE_E_COYOTE\"" +
268 ";Version=\"1\"" +
269 ";Path=\"/acme\"" +
270 ";Comment=\"this is a coyote\"" +
271 ";CommentURL=\"http://www.coyote.org\"" +
272 ";Discard" +
273 ";Domain=\".coyote.org\"" +
274 ";Max-Age=\"3600\"" +
275 ";Port=\"80\"" +
276 ";Secure")
277 .n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme")
278 .c("this is a coyote").cu("http://www.coyote.org").dsc(true)
279 .d(".coyote.org").a(3600).port("80");
280
281 // illegal characters in set-cookie header
282 test("Set-Cookie2:Customer=;Version#=\"1\";Path=&\"/acme\"")
283 .nil();
284
285 // empty set-cookie string
286 test("").nil();
287
288 // NullPointerException expected
289 try {
290 test(null);
291 } catch (NullPointerException ignored) {
292 // no-op
293 }
294
295 // bug 6277796
296 test("Set-Cookie2:Customer=\"dtftest\"; Discard; Secure; Domain=\".sun.com\"; Max-Age=\"100\"; Version=\"1\"; path=\"/www\"; Port=\"80\"")
297 .n("Customer").v("dtftest").ver(1).d(".sun.com").p("/www").port("80").dsc(true).a(100);
298
299 // bug 6277801
300 test("Set-Cookie2:Customer=\"dtftest\"; Discard; Secure; Domain=\".sun.com\"; Max-Age=\"100\"; Version=\"1\"; path=\"/www\"; Port=\"80\"" +
301 ";Domain=\".java.sun.com\"; Max-Age=\"200\"; path=\"/javadoc\"; Port=\"8080\"")
302 .n("Customer").v("dtftest").ver(1).d(".sun.com").p("/www").port("80").dsc(true).a(100);
303
304 // bug 6294071
305 test("Set-Cookie2:Customer=\"dtftest\";Discard; Secure; Domain=\"sun.com\"; Max-Age=\"100\";Version=\"1\"; Path=\"/www\"; Port=\"80,8080\"")
306 .n("Customer").v("dtftest").ver(1).d("sun.com").p("/www").port("80,8080").dsc(true).a(100);
307 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\"")
308 .n("Customer").v("developer").d("sun.com").p("/www").port("80,8080").a(100).cu("http://www.sun.com/java1,000,000.html");
309
310 // a header string contains 2 cookies
311 test("Set-Cookie2:C1=\"V1\";Domain=\".sun1.com\";path=\"/www1\";Max-Age=\"100\",C2=\"V2\";Domain=\".sun2.com\";path=\"/www2\";Max-Age=\"200\"")
312 .n(0, "C1").v(0, "V1").p(0, "/www1").a(0, 100).d(0, ".sun1.com")
313 .n(1, "C2").v(1, "V2").p(1, "/www2").a(1, 200).d(1, ".sun2.com");
314 }
315
316 static void netscape() {
317 header("Test using netscape cookie syntax");
318
319 test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT")
320 .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
321
322 // a Netscape cookie, without set-cookie leading token
323 test("CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT")
324 .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
325
326 // a 'google' cookie
327 test("Set-Cookie: PREF=ID=1eda537de48ac25d:CR=1:TM=1112868587:LM=1112868587:S=t3FPA-mT9lTR3bxU;" +
328 "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com")
329 .n("PREF").v("ID=1eda537de48ac25d:CR=1:TM=1112868587:LM=1112868587:S=t3FPA-mT9lTR3bxU")
330 .p("/").d(".google.com").ver(0);
331
332 // bug 6277796
333 test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; Secure")
334 .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
335
336 // bug 6277801
337 test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; path=\"/acme\"")
338 .n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
339 }
340
341 static void misc() {
342 header("Test equals()");
343
344 // test equals()
345 HttpCookie c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
346 c1.setDomain(".coyote.org");
347 c1.setPath("/acme");
348 HttpCookie c2 = (HttpCookie)c1.clone();
349 eq(c1, c2, true);
350
351 // test equals() when domain and path are null
352 c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
353 c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");
354 eq(c1, c2, true);
355
356 // path is case-sensitive
357 c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
358 c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");
359 c1.setPath("/acme");
360 c2.setPath("/ACME");
361 eq(c1, c2, false);
362
363 header("Test domainMatches()");
364 dm(".foo.com", "y.x.foo.com", false);
365 dm(".foo.com", "x.foo.com", true);
366 dm(".com", "whatever.com", false);
367 dm(".com.", "whatever.com", false);
368 dm(".ajax.com", "ajax.com", true);
369 dm(".local", "example.local", true);
370
371 // bug 6277808
372 testCount++;
373 try {
374 c1 = new HttpCookie("", "whatever");
375 } catch (IllegalArgumentException ignored) {
376 // expected exception; no-op
377 }
jccollet10646082008-09-04 15:26:53 +0200378
379 // CR 6692802: HttpOnly flag
380 test("set-cookie: CUSTOMER=WILE_E_COYOTE;HttpOnly").httpOnly(true);
381 test("set-cookie: CUSTOMER=WILE_E_COYOTE").httpOnly(false);
duke6e45e102007-12-01 00:00:00 +0000382 }
383
384 static void header(String prompt) {
385 System.out.println("== " + prompt + " ==");
386 }
387
388 public static void main(String[] args) {
389 runTests();
390
391 System.out.println("Succeeded in running " + testCount + " tests.");
392 }
393}