duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
alanb | 0d05823 | 2012-11-02 15:50:11 +0000 | [diff] [blame] | 2 | * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @summary Unit test for java.net.CookieManager |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 27 | * @bug 6244040 7150552 |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 28 | * @run main/othervm -ea CookieManagerTest |
| 29 | * @author Edward Wang |
| 30 | */ |
| 31 | |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 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; |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 41 | |
| 42 | public class CookieManagerTest { |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 43 | |
| 44 | static CookieTransactionHandler httpTrans; |
| 45 | static HttpServer server; |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 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(); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 61 | } |
| 62 | |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 63 | public static void makeHttpCall() throws IOException { |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 64 | try { |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 65 | System.out.println("http server listenining on: " |
| 66 | + server.getAddress().getPort()); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 67 | |
| 68 | // install CookieManager to use |
| 69 | CookieHandler.setDefault(new CookieManager()); |
| 70 | |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 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); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 83 | HttpURLConnection uc = (HttpURLConnection)url.openConnection(); |
| 84 | uc.getResponseCode(); |
| 85 | uc.disconnect(); |
| 86 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 87 | } finally { |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 88 | server.stop(0); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 93 | class CookieTransactionHandler implements HttpHandler { |
| 94 | |
| 95 | private int testcaseDone = 0; |
| 96 | private int testDone = 0; |
| 97 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 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 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 161 | /* |
| 162 | * these two must match each other, |
| 163 | * i.e. testCases.length == testPolicies.length |
| 164 | */ |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 165 | |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 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() { |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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", |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 185 | "CUSTOMER=WILE:BOB; " + |
| 186 | "path=/; expires=Sat, 09-Nov-2030 23:12:40 GMT;" + "domain=." + |
| 187 | localHostAddr, |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 188 | "CUSTOMER=WILE:BOB", |
| 189 | "/" |
| 190 | ), |
| 191 | new CookieTestCase("Set-Cookie", |
| 192 | "PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr, |
jccollet | 1160927 | 2008-03-05 11:40:22 +0100 | [diff] [blame] | 193 | "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 194 | "/" |
| 195 | ), |
| 196 | new CookieTestCase("Set-Cookie", |
| 197 | "SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr, |
jccollet | 1160927 | 2008-03-05 11:40:22 +0100 | [diff] [blame] | 198 | "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 199 | "/" |
| 200 | ), |
| 201 | new CookieTestCase("Set-Cookie", |
| 202 | "SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr, |
jccollet | 1160927 | 2008-03-05 11:40:22 +0100 | [diff] [blame] | 203 | "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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, |
jccollet | 1160927 | 2008-03-05 11:40:22 +0100 | [diff] [blame] | 218 | "PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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, |
jccollet | 1160927 | 2008-03-05 11:40:22 +0100 | [diff] [blame] | 228 | "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 229 | "/acme/login" |
| 230 | ), |
| 231 | new CookieTestCase("Set-Cookie2", |
| 232 | "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\";" + "domain=." + localHostAddr, |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 233 | "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + |
| 234 | localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" |
| 235 | + "$Domain=\"." + localHostAddr + "\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 236 | "/acme/pickitem" |
| 237 | ), |
| 238 | new CookieTestCase("Set-Cookie2", |
| 239 | "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 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 + "\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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, |
jccollet | 1160927 | 2008-03-05 11:40:22 +0100 | [diff] [blame] | 253 | "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 254 | "/acme/ammo" |
| 255 | ), |
| 256 | new CookieTestCase("Set-Cookie2", |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 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 + "\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 262 | "/acme/ammo" |
| 263 | ), |
| 264 | new CookieTestCase("", |
| 265 | "", |
jccollet | 1160927 | 2008-03-05 11:40:22 +0100 | [diff] [blame] | 266 | "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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, |
jccollet | 1160927 | 2008-03-05 11:40:22 +0100 | [diff] [blame] | 276 | "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 277 | "/acme" |
| 278 | ), |
| 279 | new CookieTestCase("Set-Cookie2", |
| 280 | "Part_Number=\"Rocket_Launcher_2000\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr, |
jccollet | 1160927 | 2008-03-05 11:40:22 +0100 | [diff] [blame] | 281 | "$Version=\"1\"; Part_Number=\"Rocket_Launcher_2000\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 282 | "/acme" |
| 283 | ) |
| 284 | }; |
| 285 | |
| 286 | // cookies without domain attributes |
jccollet | 9f453df | 2009-05-25 22:27:26 +0200 | [diff] [blame] | 287 | // RFC 2965 states that domain should default to host |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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\"", |
jccollet | 9f453df | 2009-05-25 22:27:26 +0200 | [diff] [blame] | 292 | "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 293 | "/acme/login" |
| 294 | ), |
| 295 | new CookieTestCase("Set-Cookie2", |
| 296 | "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"", |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 297 | "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + |
| 298 | "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 299 | "/acme/pickitem" |
| 300 | ), |
| 301 | new CookieTestCase("Set-Cookie2", |
| 302 | "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"", |
khazra | a3d1774 | 2013-05-16 10:58:20 -0700 | [diff] [blame^] | 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+"\"", |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 306 | "/acme/shipping" |
| 307 | ) |
| 308 | }; |
| 309 | |
| 310 | assert count == testCount; |
| 311 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 312 | } |