author | khazra |
Thu, 16 May 2013 10:58:20 -0700 | |
changeset 17489 | a74a13b9aa93 |
parent 14787 | 54eb3f090934 |
child 18148 | d641a1b31205 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13788
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 19 |
* 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary Unit test for java.net.CookieManager |
|
17489 | 27 |
* @bug 6244040 7150552 |
2 | 28 |
* @run main/othervm -ea CookieManagerTest |
29 |
* @author Edward Wang |
|
30 |
*/ |
|
31 |
||
17489 | 32 |
import com.sun.net.httpserver.*; |
33 |
import java.io.IOException; |
|
34 |
import java.net.CookieHandler; |
|
35 |
import java.net.CookieManager; |
|
36 |
import java.net.CookiePolicy; |
|
37 |
import java.net.HttpURLConnection; |
|
38 |
import java.net.InetAddress; |
|
39 |
import java.net.InetSocketAddress; |
|
40 |
import java.net.URL; |
|
2 | 41 |
|
42 |
public class CookieManagerTest { |
|
17489 | 43 |
|
44 |
static CookieTransactionHandler httpTrans; |
|
45 |
static HttpServer server; |
|
2 | 46 |
|
47 |
public static void main(String[] args) throws Exception { |
|
48 |
startHttpServer(); |
|
49 |
makeHttpCall(); |
|
50 |
||
51 |
if (httpTrans.badRequest) { |
|
52 |
throw new RuntimeException("Test failed : bad cookie header"); |
|
53 |
} |
|
54 |
} |
|
55 |
||
17489 | 56 |
public static void startHttpServer() throws IOException { |
57 |
httpTrans = new CookieTransactionHandler(); |
|
58 |
server = HttpServer.create(new InetSocketAddress(0), 0); |
|
59 |
server.createContext("/", httpTrans); |
|
60 |
server.start(); |
|
2 | 61 |
} |
62 |
||
17489 | 63 |
public static void makeHttpCall() throws IOException { |
2 | 64 |
try { |
17489 | 65 |
System.out.println("http server listenining on: " |
66 |
+ server.getAddress().getPort()); |
|
2 | 67 |
|
68 |
// install CookieManager to use |
|
69 |
CookieHandler.setDefault(new CookieManager()); |
|
70 |
||
17489 | 71 |
for (int i = 0; i < CookieTransactionHandler.testCount; i++) { |
72 |
System.out.println("====== CookieManager test " + (i+1) |
|
73 |
+ " ======"); |
|
74 |
((CookieManager)CookieHandler.getDefault()) |
|
75 |
.setCookiePolicy(CookieTransactionHandler.testPolicies[i]); |
|
76 |
((CookieManager)CookieHandler.getDefault()) |
|
77 |
.getCookieStore().removeAll(); |
|
78 |
URL url = new URL("http" , |
|
79 |
InetAddress.getLocalHost().getHostAddress(), |
|
80 |
server.getAddress().getPort(), |
|
81 |
CookieTransactionHandler.testCases[i][0] |
|
82 |
.serverPath); |
|
2 | 83 |
HttpURLConnection uc = (HttpURLConnection)url.openConnection(); |
84 |
uc.getResponseCode(); |
|
85 |
uc.disconnect(); |
|
86 |
} |
|
87 |
} finally { |
|
17489 | 88 |
server.stop(0); |
2 | 89 |
} |
90 |
} |
|
91 |
} |
|
92 |
||
17489 | 93 |
class CookieTransactionHandler implements HttpHandler { |
94 |
||
95 |
private int testcaseDone = 0; |
|
96 |
private int testDone = 0; |
|
97 |
||
2 | 98 |
public static boolean badRequest = false; |
99 |
// the main test control logic will also loop exactly this number |
|
100 |
// to send http request |
|
101 |
public static final int testCount = 6; |
|
102 |
||
103 |
private String localHostAddr = "127.0.0.1"; |
|
104 |
||
17489 | 105 |
@Override |
106 |
public void handle(HttpExchange exchange) throws IOException { |
|
107 |
if (testDone < testCases[testcaseDone].length) { |
|
108 |
// still have other tests to run, |
|
109 |
// check the Cookie header and then redirect it |
|
110 |
if (testDone > 0) checkRequest(exchange.getRequestHeaders()); |
|
111 |
exchange.getResponseHeaders().add("Location", |
|
112 |
testCases[testcaseDone][testDone].serverPath); |
|
113 |
exchange.getResponseHeaders() |
|
114 |
.add(testCases[testcaseDone][testDone].headerToken, |
|
115 |
testCases[testcaseDone][testDone].cookieToSend); |
|
116 |
exchange.sendResponseHeaders(302, -1); |
|
117 |
testDone++; |
|
118 |
} else { |
|
119 |
// the last test of this test case |
|
120 |
if (testDone > 0) checkRequest(exchange.getRequestHeaders()); |
|
121 |
testcaseDone++; |
|
122 |
testDone = 0; |
|
123 |
exchange.sendResponseHeaders(200, -1); |
|
124 |
} |
|
125 |
exchange.close(); |
|
126 |
} |
|
127 |
||
128 |
private void checkRequest(Headers hdrs) { |
|
129 |
||
130 |
assert testDone > 0; |
|
131 |
String cookieHeader = hdrs.getFirst("Cookie"); |
|
132 |
if (cookieHeader != null && |
|
133 |
cookieHeader |
|
134 |
.equalsIgnoreCase(testCases[testcaseDone][testDone-1] |
|
135 |
.cookieToRecv)) |
|
136 |
{ |
|
137 |
System.out.printf("%15s %s\n", "PASSED:", cookieHeader); |
|
138 |
} else { |
|
139 |
System.out.printf("%15s %s\n", "FAILED:", cookieHeader); |
|
140 |
System.out.printf("%15s %s\n\n", "should be:", |
|
141 |
testCases[testcaseDone][testDone-1].cookieToRecv); |
|
142 |
badRequest = true; |
|
143 |
} |
|
144 |
} |
|
145 |
||
2 | 146 |
// test cases |
147 |
public static class CookieTestCase { |
|
148 |
public String headerToken; |
|
149 |
public String cookieToSend; |
|
150 |
public String cookieToRecv; |
|
151 |
public String serverPath; |
|
152 |
||
153 |
public CookieTestCase(String h, String cts, String ctr, String sp) { |
|
154 |
headerToken = h; |
|
155 |
cookieToSend = cts; |
|
156 |
cookieToRecv = ctr; |
|
157 |
serverPath = sp; |
|
158 |
} |
|
159 |
}; |
|
160 |
||
17489 | 161 |
/* |
162 |
* these two must match each other, |
|
163 |
* i.e. testCases.length == testPolicies.length |
|
164 |
*/ |
|
2 | 165 |
|
17489 | 166 |
// the test cases to run; each test case may contain multiple roundtrips |
167 |
public static CookieTestCase[][] testCases = null; |
|
168 |
// indicates what CookiePolicy to use with each test cases |
|
169 |
public static CookiePolicy[] testPolicies = null; |
|
170 |
||
171 |
CookieTransactionHandler() { |
|
2 | 172 |
testCases = new CookieTestCase[testCount][]; |
173 |
testPolicies = new CookiePolicy[testCount]; |
|
174 |
||
175 |
try { |
|
176 |
localHostAddr = InetAddress.getLocalHost().getHostAddress(); |
|
177 |
} catch (Exception ignored) { |
|
178 |
}; |
|
179 |
int count = 0; |
|
180 |
||
181 |
// an http session with Netscape cookies exchanged |
|
182 |
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
|
183 |
testCases[count++] = new CookieTestCase[]{ |
|
184 |
new CookieTestCase("Set-Cookie", |
|
17489 | 185 |
"CUSTOMER=WILE:BOB; " + |
186 |
"path=/; expires=Sat, 09-Nov-2030 23:12:40 GMT;" + "domain=." + |
|
187 |
localHostAddr, |
|
2 | 188 |
"CUSTOMER=WILE:BOB", |
189 |
"/" |
|
190 |
), |
|
191 |
new CookieTestCase("Set-Cookie", |
|
192 |
"PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
193 |
"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001", |
2 | 194 |
"/" |
195 |
), |
|
196 |
new CookieTestCase("Set-Cookie", |
|
197 |
"SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
198 |
"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001", |
2 | 199 |
"/" |
200 |
), |
|
201 |
new CookieTestCase("Set-Cookie", |
|
202 |
"SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
203 |
"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX", |
2 | 204 |
"/foo" |
205 |
) |
|
206 |
}; |
|
207 |
||
208 |
// check whether or not path rule is applied |
|
209 |
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
|
210 |
testCases[count++] = new CookieTestCase[]{ |
|
211 |
new CookieTestCase("Set-Cookie", |
|
212 |
"PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr, |
|
213 |
"PART_NUMBER=ROCKET_LAUNCHER_0001", |
|
214 |
"/" |
|
215 |
), |
|
216 |
new CookieTestCase("Set-Cookie", |
|
217 |
"PART_NUMBER=RIDING_ROCKET_0023; path=/ammo;" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
218 |
"PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001", |
2 | 219 |
"/ammo" |
220 |
) |
|
221 |
}; |
|
222 |
||
223 |
// an http session with rfc2965 cookies exchanged |
|
224 |
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
|
225 |
testCases[count++] = new CookieTestCase[]{ |
|
226 |
new CookieTestCase("Set-Cookie2", |
|
227 |
"Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
228 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
2 | 229 |
"/acme/login" |
230 |
), |
|
231 |
new CookieTestCase("Set-Cookie2", |
|
232 |
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\";" + "domain=." + localHostAddr, |
|
17489 | 233 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + |
234 |
localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" |
|
235 |
+ "$Domain=\"." + localHostAddr + "\"", |
|
2 | 236 |
"/acme/pickitem" |
237 |
), |
|
238 |
new CookieTestCase("Set-Cookie2", |
|
239 |
"Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
|
17489 | 240 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + |
241 |
"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." |
|
242 |
+ localHostAddr + "\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";" + |
|
243 |
"$Domain=\"." + localHostAddr + "\"", |
|
2 | 244 |
"/acme/shipping" |
245 |
) |
|
246 |
}; |
|
247 |
||
248 |
// check whether or not the path rule is applied |
|
249 |
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
|
250 |
testCases[count++] = new CookieTestCase[]{ |
|
251 |
new CookieTestCase("Set-Cookie2", |
|
252 |
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
253 |
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
2 | 254 |
"/acme/ammo" |
255 |
), |
|
256 |
new CookieTestCase("Set-Cookie2", |
|
17489 | 257 |
"Part_Number=\"Riding_Rocket_0023\"; Version=\"1\"; Path=\"/acme/ammo\";" + "domain=." |
258 |
+ localHostAddr, |
|
259 |
"$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\";$Path=\"/acme/ammo\";$Domain=\"." |
|
260 |
+ localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" |
|
261 |
+ "$Domain=\"." + localHostAddr + "\"", |
|
2 | 262 |
"/acme/ammo" |
263 |
), |
|
264 |
new CookieTestCase("", |
|
265 |
"", |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
266 |
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"", |
2 | 267 |
"/acme/parts" |
268 |
) |
|
269 |
}; |
|
270 |
||
271 |
// new cookie should overwrite old cookie |
|
272 |
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER; |
|
273 |
testCases[count++] = new CookieTestCase[]{ |
|
274 |
new CookieTestCase("Set-Cookie2", |
|
275 |
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
276 |
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
2 | 277 |
"/acme" |
278 |
), |
|
279 |
new CookieTestCase("Set-Cookie2", |
|
280 |
"Part_Number=\"Rocket_Launcher_2000\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
281 |
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_2000\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
2 | 282 |
"/acme" |
283 |
) |
|
284 |
}; |
|
285 |
||
286 |
// cookies without domain attributes |
|
2924
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
715
diff
changeset
|
287 |
// RFC 2965 states that domain should default to host |
2 | 288 |
testPolicies[count] = CookiePolicy.ACCEPT_ALL; |
289 |
testCases[count++] = new CookieTestCase[]{ |
|
290 |
new CookieTestCase("Set-Cookie2", |
|
291 |
"Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"", |
|
2924
a67f12d92bdd
6349566: java.net.CookieManager doesn't set default domain
jccollet
parents:
715
diff
changeset
|
292 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"", |
2 | 293 |
"/acme/login" |
294 |
), |
|
295 |
new CookieTestCase("Set-Cookie2", |
|
296 |
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"", |
|
17489 | 297 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + |
298 |
"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"", |
|
2 | 299 |
"/acme/pickitem" |
300 |
), |
|
301 |
new CookieTestCase("Set-Cookie2", |
|
302 |
"Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"", |
|
17489 | 303 |
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + |
304 |
"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + |
|
305 |
"; Shipping=\"FedEx\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"", |
|
2 | 306 |
"/acme/shipping" |
307 |
) |
|
308 |
}; |
|
309 |
||
310 |
assert count == testCount; |
|
311 |
} |
|
312 |
} |