2
|
1 |
/*
|
|
2 |
* Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* 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
|
|
27 |
* @bug 6244040 6277796 6277801 6277808 6294071
|
|
28 |
* @author Edward Wang
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.net.HttpCookie;
|
|
32 |
import java.util.List;
|
|
33 |
|
|
34 |
public 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 |
|
|
181 |
// check equality
|
|
182 |
static void eq(HttpCookie ck1, HttpCookie ck2, boolean same) {
|
|
183 |
testCount++;
|
|
184 |
if (ck1.equals(ck2) != same) {
|
|
185 |
raiseError("Comparison inconsistent: " + ck1 + " " + ck2
|
|
186 |
+ " should " + (same ? "equal" : "not equal"));
|
|
187 |
}
|
|
188 |
|
|
189 |
int h1 = ck1.hashCode();
|
|
190 |
int h2 = ck2.hashCode();
|
|
191 |
if ((h1 == h2) != same) {
|
|
192 |
raiseError("Comparison inconsistent: hashCode for " + ck1 + " " + ck2
|
|
193 |
+ " should " + (same ? "equal" : "not equal"));
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
// check domainMatches()
|
|
198 |
static void dm(String domain, String host, boolean matches) {
|
|
199 |
testCount++;
|
|
200 |
if (HttpCookie.domainMatches(domain, host) != matches) {
|
|
201 |
raiseError("Host " + host + (matches?" should ":" should not ") +
|
|
202 |
"domain-match with domain " + domain);
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
|
206 |
void raiseError(String attr, String realValue, String expectedValue) {
|
|
207 |
StringBuilder sb = new StringBuilder();
|
|
208 |
sb.append("Cookie ").append(attr).append(" is ").append(realValue).
|
|
209 |
append(", should be ").append(expectedValue).
|
|
210 |
append(" (").append(cHeader).append(")");
|
|
211 |
throw new RuntimeException(sb.toString());
|
|
212 |
}
|
|
213 |
|
|
214 |
static void raiseError(String prompt) {
|
|
215 |
throw new RuntimeException(prompt);
|
|
216 |
}
|
|
217 |
|
|
218 |
static void runTests() {
|
|
219 |
rfc2965();
|
|
220 |
netscape();
|
|
221 |
misc();
|
|
222 |
}
|
|
223 |
|
|
224 |
static void rfc2965() {
|
|
225 |
header("Test using rfc 2965 syntax");
|
|
226 |
|
|
227 |
test("set-cookie2: Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"")
|
|
228 |
.n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme");
|
|
229 |
|
|
230 |
// whitespace between attr and = sign
|
|
231 |
test("set-cookie2: Customer = \"WILE_E_COYOTE\"; Version = \"1\"; Path = \"/acme\"")
|
|
232 |
.n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme");
|
|
233 |
|
|
234 |
// $NAME is reserved; result should be null
|
|
235 |
test("set-cookie2: $Customer = \"WILE_E_COYOTE\"; Version = \"1\"; Path = \"/acme\"")
|
|
236 |
.nil();
|
|
237 |
|
|
238 |
// a 'full' cookie
|
|
239 |
test("set-cookie2: Customer=\"WILE_E_COYOTE\"" +
|
|
240 |
";Version=\"1\"" +
|
|
241 |
";Path=\"/acme\"" +
|
|
242 |
";Comment=\"this is a coyote\"" +
|
|
243 |
";CommentURL=\"http://www.coyote.org\"" +
|
|
244 |
";Discard" +
|
|
245 |
";Domain=\".coyote.org\"" +
|
|
246 |
";Max-Age=\"3600\"" +
|
|
247 |
";Port=\"80\"" +
|
|
248 |
";Secure")
|
|
249 |
.n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme")
|
|
250 |
.c("this is a coyote").cu("http://www.coyote.org").dsc(true)
|
|
251 |
.d(".coyote.org").a(3600).port("80");
|
|
252 |
|
|
253 |
// a 'full' cookie, without leading set-cookie2 token
|
|
254 |
test("Customer=\"WILE_E_COYOTE\"" +
|
|
255 |
";Version=\"1\"" +
|
|
256 |
";Path=\"/acme\"" +
|
|
257 |
";Comment=\"this is a coyote\"" +
|
|
258 |
";CommentURL=\"http://www.coyote.org\"" +
|
|
259 |
";Discard" +
|
|
260 |
";Domain=\".coyote.org\"" +
|
|
261 |
";Max-Age=\"3600\"" +
|
|
262 |
";Port=\"80\"" +
|
|
263 |
";Secure")
|
|
264 |
.n("Customer").v("WILE_E_COYOTE").ver(1).p("/acme")
|
|
265 |
.c("this is a coyote").cu("http://www.coyote.org").dsc(true)
|
|
266 |
.d(".coyote.org").a(3600).port("80");
|
|
267 |
|
|
268 |
// illegal characters in set-cookie header
|
|
269 |
test("Set-Cookie2:Customer=;Version#=\"1\";Path=&\"/acme\"")
|
|
270 |
.nil();
|
|
271 |
|
|
272 |
// empty set-cookie string
|
|
273 |
test("").nil();
|
|
274 |
|
|
275 |
// NullPointerException expected
|
|
276 |
try {
|
|
277 |
test(null);
|
|
278 |
} catch (NullPointerException ignored) {
|
|
279 |
// no-op
|
|
280 |
}
|
|
281 |
|
|
282 |
// bug 6277796
|
|
283 |
test("Set-Cookie2:Customer=\"dtftest\"; Discard; Secure; Domain=\".sun.com\"; Max-Age=\"100\"; Version=\"1\"; path=\"/www\"; Port=\"80\"")
|
|
284 |
.n("Customer").v("dtftest").ver(1).d(".sun.com").p("/www").port("80").dsc(true).a(100);
|
|
285 |
|
|
286 |
// bug 6277801
|
|
287 |
test("Set-Cookie2:Customer=\"dtftest\"; Discard; Secure; Domain=\".sun.com\"; Max-Age=\"100\"; Version=\"1\"; path=\"/www\"; Port=\"80\"" +
|
|
288 |
";Domain=\".java.sun.com\"; Max-Age=\"200\"; path=\"/javadoc\"; Port=\"8080\"")
|
|
289 |
.n("Customer").v("dtftest").ver(1).d(".sun.com").p("/www").port("80").dsc(true).a(100);
|
|
290 |
|
|
291 |
// bug 6294071
|
|
292 |
test("Set-Cookie2:Customer=\"dtftest\";Discard; Secure; Domain=\"sun.com\"; Max-Age=\"100\";Version=\"1\"; Path=\"/www\"; Port=\"80,8080\"")
|
|
293 |
.n("Customer").v("dtftest").ver(1).d("sun.com").p("/www").port("80,8080").dsc(true).a(100);
|
|
294 |
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\"")
|
|
295 |
.n("Customer").v("developer").d("sun.com").p("/www").port("80,8080").a(100).cu("http://www.sun.com/java1,000,000.html");
|
|
296 |
|
|
297 |
// a header string contains 2 cookies
|
|
298 |
test("Set-Cookie2:C1=\"V1\";Domain=\".sun1.com\";path=\"/www1\";Max-Age=\"100\",C2=\"V2\";Domain=\".sun2.com\";path=\"/www2\";Max-Age=\"200\"")
|
|
299 |
.n(0, "C1").v(0, "V1").p(0, "/www1").a(0, 100).d(0, ".sun1.com")
|
|
300 |
.n(1, "C2").v(1, "V2").p(1, "/www2").a(1, 200).d(1, ".sun2.com");
|
|
301 |
}
|
|
302 |
|
|
303 |
static void netscape() {
|
|
304 |
header("Test using netscape cookie syntax");
|
|
305 |
|
|
306 |
test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT")
|
|
307 |
.n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
|
|
308 |
|
|
309 |
// a Netscape cookie, without set-cookie leading token
|
|
310 |
test("CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT")
|
|
311 |
.n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
|
|
312 |
|
|
313 |
// a 'google' cookie
|
|
314 |
test("Set-Cookie: PREF=ID=1eda537de48ac25d:CR=1:TM=1112868587:LM=1112868587:S=t3FPA-mT9lTR3bxU;" +
|
|
315 |
"expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com")
|
|
316 |
.n("PREF").v("ID=1eda537de48ac25d:CR=1:TM=1112868587:LM=1112868587:S=t3FPA-mT9lTR3bxU")
|
|
317 |
.p("/").d(".google.com").ver(0);
|
|
318 |
|
|
319 |
// bug 6277796
|
|
320 |
test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; Secure")
|
|
321 |
.n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
|
|
322 |
|
|
323 |
// bug 6277801
|
|
324 |
test("set-cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT; path=\"/acme\"")
|
|
325 |
.n("CUSTOMER").v("WILE_E_COYOTE").p("/").ver(0);
|
|
326 |
}
|
|
327 |
|
|
328 |
static void misc() {
|
|
329 |
header("Test equals()");
|
|
330 |
|
|
331 |
// test equals()
|
|
332 |
HttpCookie c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
|
|
333 |
c1.setDomain(".coyote.org");
|
|
334 |
c1.setPath("/acme");
|
|
335 |
HttpCookie c2 = (HttpCookie)c1.clone();
|
|
336 |
eq(c1, c2, true);
|
|
337 |
|
|
338 |
// test equals() when domain and path are null
|
|
339 |
c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
|
|
340 |
c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");
|
|
341 |
eq(c1, c2, true);
|
|
342 |
|
|
343 |
// path is case-sensitive
|
|
344 |
c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
|
|
345 |
c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");
|
|
346 |
c1.setPath("/acme");
|
|
347 |
c2.setPath("/ACME");
|
|
348 |
eq(c1, c2, false);
|
|
349 |
|
|
350 |
header("Test domainMatches()");
|
|
351 |
dm(".foo.com", "y.x.foo.com", false);
|
|
352 |
dm(".foo.com", "x.foo.com", true);
|
|
353 |
dm(".com", "whatever.com", false);
|
|
354 |
dm(".com.", "whatever.com", false);
|
|
355 |
dm(".ajax.com", "ajax.com", true);
|
|
356 |
dm(".local", "example.local", true);
|
|
357 |
|
|
358 |
// bug 6277808
|
|
359 |
testCount++;
|
|
360 |
try {
|
|
361 |
c1 = new HttpCookie("", "whatever");
|
|
362 |
} catch (IllegalArgumentException ignored) {
|
|
363 |
// expected exception; no-op
|
|
364 |
}
|
|
365 |
}
|
|
366 |
|
|
367 |
static void header(String prompt) {
|
|
368 |
System.out.println("== " + prompt + " ==");
|
|
369 |
}
|
|
370 |
|
|
371 |
public static void main(String[] args) {
|
|
372 |
runTests();
|
|
373 |
|
|
374 |
System.out.println("Succeeded in running " + testCount + " tests.");
|
|
375 |
}
|
|
376 |
}
|