Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/CookieHandler/CookieManagerTest.java
38812 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Unit test for java.net.CookieManager26* @bug 6244040 7150552 705186227* @modules jdk.httpserver28* @run main/othervm -ea CookieManagerTest29* @author Edward Wang30*/3132import com.sun.net.httpserver.*;33import java.io.IOException;34import java.net.*;35import static java.net.Proxy.NO_PROXY;3637public class CookieManagerTest {3839static CookieTransactionHandler httpTrans;40static HttpServer server;4142static final String hostAddress = getAddr();4344/** Returns an IP literal suitable for use by the test. */45static String getAddr() {46try {47InetAddress lh = InetAddress.getLocalHost();48System.out.println("Trying: " + lh);49if (lh.isReachable(5_000)) {50System.out.println("Using: " + lh);51return lh.getHostAddress();52}53} catch (IOException x) {54System.out.println("Debug: caught:" + x);55}56System.out.println("Using: \"127.0.0.1\"");57return "127.0.0.1";58}5960public static void main(String[] args) throws Exception {61startHttpServer();62makeHttpCall();6364if (httpTrans.badRequest) {65throw new RuntimeException("Test failed : bad cookie header");66}67checkCookiePolicy();68}6970public static void startHttpServer() throws IOException {71httpTrans = new CookieTransactionHandler();72server = HttpServer.create(new InetSocketAddress(0), 0);73server.createContext("/", httpTrans);74server.start();75}7677/*78* Checks if CookiePolicy.ACCEPT_ORIGINAL_SERVER#shouldAccept()79* returns false for null arguments80*/81private static void checkCookiePolicy() throws Exception {82CookiePolicy cp = CookiePolicy.ACCEPT_ORIGINAL_SERVER;83boolean retVal;84retVal = cp.shouldAccept(null, null);85checkValue(retVal);86retVal = cp.shouldAccept(null, new HttpCookie("CookieName", "CookieVal"));87checkValue(retVal);88retVal = cp.shouldAccept((new URL("http", "localhost", 2345, "/")).toURI(),89null);90checkValue(retVal);91}9293private static void checkValue(boolean val) {94if (val)95throw new RuntimeException("Return value is not false!");96}9798public static void makeHttpCall() throws IOException {99try {100int port = server.getAddress().getPort();101System.out.println("http server listenining on: " + port);102103// install CookieManager to use104CookieHandler.setDefault(new CookieManager());105106for (int i = 0; i < CookieTransactionHandler.testCount; i++) {107System.out.println("====== CookieManager test " + (i+1)108+ " ======");109((CookieManager)CookieHandler.getDefault())110.setCookiePolicy(CookieTransactionHandler.testPolicies[i]);111((CookieManager)CookieHandler.getDefault())112.getCookieStore().removeAll();113URL url = new URL("http" ,114hostAddress,115server.getAddress().getPort(),116CookieTransactionHandler.testCases[i][0]117.serverPath);118System.out.println("Requesting " + url);119HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);120uc.getResponseCode();121uc.disconnect();122}123} finally {124server.stop(0);125}126}127}128129class CookieTransactionHandler implements HttpHandler {130131private int testcaseDone = 0;132private int testDone = 0;133134public static boolean badRequest = false;135// the main test control logic will also loop exactly this number136// to send http request137public static final int testCount = 6;138139@Override140public void handle(HttpExchange exchange) throws IOException {141if (testDone < testCases[testcaseDone].length) {142// still have other tests to run,143// check the Cookie header and then redirect it144if (testDone > 0) checkRequest(exchange.getRequestHeaders());145exchange.getResponseHeaders().add("Location",146testCases[testcaseDone][testDone].serverPath);147exchange.getResponseHeaders()148.add(testCases[testcaseDone][testDone].headerToken,149testCases[testcaseDone][testDone].cookieToSend);150exchange.sendResponseHeaders(302, -1);151testDone++;152} else {153// the last test of this test case154if (testDone > 0) checkRequest(exchange.getRequestHeaders());155testcaseDone++;156testDone = 0;157exchange.sendResponseHeaders(200, -1);158}159exchange.close();160}161162private void checkRequest(Headers hdrs) {163164assert testDone > 0;165String cookieHeader = hdrs.getFirst("Cookie");166if (cookieHeader != null &&167cookieHeader168.equalsIgnoreCase(testCases[testcaseDone][testDone-1]169.cookieToRecv))170{171System.out.printf("%15s %s\n", "PASSED:", cookieHeader);172} else {173System.out.printf("%15s %s\n", "FAILED:", cookieHeader);174System.out.printf("%15s %s\n\n", "should be:",175testCases[testcaseDone][testDone-1].cookieToRecv);176badRequest = true;177}178}179180// test cases181public static class CookieTestCase {182public String headerToken;183public String cookieToSend;184public String cookieToRecv;185public String serverPath;186187public CookieTestCase(String h, String cts, String ctr, String sp) {188headerToken = h;189cookieToSend = cts;190cookieToRecv = ctr;191serverPath = sp;192}193};194195/*196* these two must match each other,197* i.e. testCases.length == testPolicies.length198*/199200// the test cases to run; each test case may contain multiple roundtrips201public static CookieTestCase[][] testCases = null;202// indicates what CookiePolicy to use with each test cases203public static CookiePolicy[] testPolicies = null;204205CookieTransactionHandler() {206testCases = new CookieTestCase[testCount][];207testPolicies = new CookiePolicy[testCount];208209String localHostAddr = CookieManagerTest.hostAddress;210211int count = 0;212213// an http session with Netscape cookies exchanged214testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;215testCases[count++] = new CookieTestCase[]{216new CookieTestCase("Set-Cookie",217"CUSTOMER=WILE:BOB; " +218"path=/; expires=Sat, 09-Nov-2030 23:12:40 GMT;" + "domain=." +219localHostAddr,220"CUSTOMER=WILE:BOB",221"/"222),223new CookieTestCase("Set-Cookie",224"PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,225"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",226"/"227),228new CookieTestCase("Set-Cookie",229"SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,230"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",231"/"232),233new CookieTestCase("Set-Cookie",234"SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,235"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX",236"/foo"237)238};239240// check whether or not path rule is applied241testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;242testCases[count++] = new CookieTestCase[]{243new CookieTestCase("Set-Cookie",244"PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,245"PART_NUMBER=ROCKET_LAUNCHER_0001",246"/"247),248new CookieTestCase("Set-Cookie",249"PART_NUMBER=RIDING_ROCKET_0023; path=/ammo;" + "domain=." + localHostAddr,250"PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001",251"/ammo"252)253};254255// an http session with rfc2965 cookies exchanged256testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;257testCases[count++] = new CookieTestCase[]{258new CookieTestCase("Set-Cookie2",259"Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,260"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",261"/acme/login"262),263new CookieTestCase("Set-Cookie2",264"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\";" + "domain=." + localHostAddr,265"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." +266localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";"267+ "$Domain=\"." + localHostAddr + "\"",268"/acme/pickitem"269),270new CookieTestCase("Set-Cookie2",271"Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,272"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr +273"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"."274+ localHostAddr + "\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";" +275"$Domain=\"." + localHostAddr + "\"",276"/acme/shipping"277)278};279280// check whether or not the path rule is applied281testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;282testCases[count++] = new CookieTestCase[]{283new CookieTestCase("Set-Cookie2",284"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,285"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",286"/acme/ammo"287),288new CookieTestCase("Set-Cookie2",289"Part_Number=\"Riding_Rocket_0023\"; Version=\"1\"; Path=\"/acme/ammo\";" + "domain=."290+ localHostAddr,291"$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\";$Path=\"/acme/ammo\";$Domain=\"."292+ localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";"293+ "$Domain=\"." + localHostAddr + "\"",294"/acme/ammo"295),296new CookieTestCase("",297"",298"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",299"/acme/parts"300)301};302303// new cookie should overwrite old cookie304testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;305testCases[count++] = new CookieTestCase[]{306new CookieTestCase("Set-Cookie2",307"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,308"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",309"/acme"310),311new CookieTestCase("Set-Cookie2",312"Part_Number=\"Rocket_Launcher_2000\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,313"$Version=\"1\"; Part_Number=\"Rocket_Launcher_2000\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",314"/acme"315)316};317318// cookies without domain attributes319// RFC 2965 states that domain should default to host320testPolicies[count] = CookiePolicy.ACCEPT_ALL;321testCases[count++] = new CookieTestCase[]{322new CookieTestCase("Set-Cookie2",323"Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"",324"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",325"/acme/login"326),327new CookieTestCase("Set-Cookie2",328"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"",329"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +330"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",331"/acme/pickitem"332),333new CookieTestCase("Set-Cookie2",334"Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"",335"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +336"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +337"; Shipping=\"FedEx\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",338"/acme/shipping"339)340};341342assert count == testCount;343}344}345346347