blob: a59a0a3795d4c1e3187cb5cace4f665e98726f49 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
alanb0d058232012-11-02 15:50:11 +00002 * Copyright (c) 2005, 2012, 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.CookieManager
khazraa3d17742013-05-16 10:58:20 -070027 * @bug 6244040 7150552
duke6e45e102007-12-01 00:00:00 +000028 * @run main/othervm -ea CookieManagerTest
29 * @author Edward Wang
30 */
31
khazraa3d17742013-05-16 10:58:20 -070032import com.sun.net.httpserver.*;
33import java.io.IOException;
34import java.net.CookieHandler;
35import java.net.CookieManager;
36import java.net.CookiePolicy;
37import java.net.HttpURLConnection;
38import java.net.InetAddress;
39import java.net.InetSocketAddress;
40import java.net.URL;
duke6e45e102007-12-01 00:00:00 +000041
42public class CookieManagerTest {
khazraa3d17742013-05-16 10:58:20 -070043
44 static CookieTransactionHandler httpTrans;
45 static HttpServer server;
duke6e45e102007-12-01 00:00:00 +000046
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
khazraa3d17742013-05-16 10:58:20 -070056 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();
duke6e45e102007-12-01 00:00:00 +000061 }
62
khazraa3d17742013-05-16 10:58:20 -070063 public static void makeHttpCall() throws IOException {
duke6e45e102007-12-01 00:00:00 +000064 try {
khazraa3d17742013-05-16 10:58:20 -070065 System.out.println("http server listenining on: "
66 + server.getAddress().getPort());
duke6e45e102007-12-01 00:00:00 +000067
68 // install CookieManager to use
69 CookieHandler.setDefault(new CookieManager());
70
khazraa3d17742013-05-16 10:58:20 -070071 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);
duke6e45e102007-12-01 00:00:00 +000083 HttpURLConnection uc = (HttpURLConnection)url.openConnection();
84 uc.getResponseCode();
85 uc.disconnect();
86 }
duke6e45e102007-12-01 00:00:00 +000087 } finally {
khazraa3d17742013-05-16 10:58:20 -070088 server.stop(0);
duke6e45e102007-12-01 00:00:00 +000089 }
90 }
91}
92
khazraa3d17742013-05-16 10:58:20 -070093class CookieTransactionHandler implements HttpHandler {
94
95 private int testcaseDone = 0;
96 private int testDone = 0;
97
duke6e45e102007-12-01 00:00:00 +000098 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
khazraa3d17742013-05-16 10:58:20 -0700105 @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
duke6e45e102007-12-01 00:00:00 +0000146 // 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
khazraa3d17742013-05-16 10:58:20 -0700161 /*
162 * these two must match each other,
163 * i.e. testCases.length == testPolicies.length
164 */
duke6e45e102007-12-01 00:00:00 +0000165
khazraa3d17742013-05-16 10:58:20 -0700166 // 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() {
duke6e45e102007-12-01 00:00:00 +0000172 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",
khazraa3d17742013-05-16 10:58:20 -0700185 "CUSTOMER=WILE:BOB; " +
186 "path=/; expires=Sat, 09-Nov-2030 23:12:40 GMT;" + "domain=." +
187 localHostAddr,
duke6e45e102007-12-01 00:00:00 +0000188 "CUSTOMER=WILE:BOB",
189 "/"
190 ),
191 new CookieTestCase("Set-Cookie",
192 "PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,
jccollet11609272008-03-05 11:40:22 +0100193 "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",
duke6e45e102007-12-01 00:00:00 +0000194 "/"
195 ),
196 new CookieTestCase("Set-Cookie",
197 "SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,
jccollet11609272008-03-05 11:40:22 +0100198 "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",
duke6e45e102007-12-01 00:00:00 +0000199 "/"
200 ),
201 new CookieTestCase("Set-Cookie",
202 "SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,
jccollet11609272008-03-05 11:40:22 +0100203 "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX",
duke6e45e102007-12-01 00:00:00 +0000204 "/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,
jccollet11609272008-03-05 11:40:22 +0100218 "PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001",
duke6e45e102007-12-01 00:00:00 +0000219 "/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,
jccollet11609272008-03-05 11:40:22 +0100228 "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
duke6e45e102007-12-01 00:00:00 +0000229 "/acme/login"
230 ),
231 new CookieTestCase("Set-Cookie2",
232 "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\";" + "domain=." + localHostAddr,
khazraa3d17742013-05-16 10:58:20 -0700233 "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." +
234 localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";"
235 + "$Domain=\"." + localHostAddr + "\"",
duke6e45e102007-12-01 00:00:00 +0000236 "/acme/pickitem"
237 ),
238 new CookieTestCase("Set-Cookie2",
239 "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
khazraa3d17742013-05-16 10:58:20 -0700240 "$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 + "\"",
duke6e45e102007-12-01 00:00:00 +0000244 "/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,
jccollet11609272008-03-05 11:40:22 +0100253 "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
duke6e45e102007-12-01 00:00:00 +0000254 "/acme/ammo"
255 ),
256 new CookieTestCase("Set-Cookie2",
khazraa3d17742013-05-16 10:58:20 -0700257 "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 + "\"",
duke6e45e102007-12-01 00:00:00 +0000262 "/acme/ammo"
263 ),
264 new CookieTestCase("",
265 "",
jccollet11609272008-03-05 11:40:22 +0100266 "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
duke6e45e102007-12-01 00:00:00 +0000267 "/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,
jccollet11609272008-03-05 11:40:22 +0100276 "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
duke6e45e102007-12-01 00:00:00 +0000277 "/acme"
278 ),
279 new CookieTestCase("Set-Cookie2",
280 "Part_Number=\"Rocket_Launcher_2000\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
jccollet11609272008-03-05 11:40:22 +0100281 "$Version=\"1\"; Part_Number=\"Rocket_Launcher_2000\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
duke6e45e102007-12-01 00:00:00 +0000282 "/acme"
283 )
284 };
285
286 // cookies without domain attributes
jccollet9f453df2009-05-25 22:27:26 +0200287 // RFC 2965 states that domain should default to host
duke6e45e102007-12-01 00:00:00 +0000288 testPolicies[count] = CookiePolicy.ACCEPT_ALL;
289 testCases[count++] = new CookieTestCase[]{
290 new CookieTestCase("Set-Cookie2",
291 "Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"",
jccollet9f453df2009-05-25 22:27:26 +0200292 "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
duke6e45e102007-12-01 00:00:00 +0000293 "/acme/login"
294 ),
295 new CookieTestCase("Set-Cookie2",
296 "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"",
khazraa3d17742013-05-16 10:58:20 -0700297 "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +
298 "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
duke6e45e102007-12-01 00:00:00 +0000299 "/acme/pickitem"
300 ),
301 new CookieTestCase("Set-Cookie2",
302 "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"",
khazraa3d17742013-05-16 10:58:20 -0700303 "$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+"\"",
duke6e45e102007-12-01 00:00:00 +0000306 "/acme/shipping"
307 )
308 };
309
310 assert count == testCount;
311 }
duke6e45e102007-12-01 00:00:00 +0000312}