0N/A/*
5556N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @summary Unit test for java.net.CookieManager
0N/A * @bug 6244040
0N/A * @library ../../../sun/net/www/httptest/
0N/A * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
0N/A * @run main/othervm -ea CookieManagerTest
0N/A * @author Edward Wang
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/Aimport sun.net.www.MessageHeader;
0N/A
0N/Apublic class CookieManagerTest {
0N/A static CookieHttpTransaction httpTrans;
0N/A static HttpServer server;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A startHttpServer();
0N/A makeHttpCall();
0N/A
0N/A if (httpTrans.badRequest) {
0N/A throw new RuntimeException("Test failed : bad cookie header");
0N/A }
0N/A }
0N/A
0N/A public static void startHttpServer() {
0N/A try {
0N/A httpTrans = new CookieHttpTransaction();
0N/A server = new HttpServer(httpTrans, 1, 1, 0);
0N/A } catch (IOException e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A public static void makeHttpCall() {
0N/A try {
0N/A System.out.println("http server listen on: " + server.getLocalPort());
0N/A
0N/A // install CookieManager to use
0N/A CookieHandler.setDefault(new CookieManager());
0N/A
0N/A for (int i = 0; i < CookieHttpTransaction.testCount; i++) {
0N/A System.out.println("====== CookieManager test " + (i+1) + " ======");
0N/A ((CookieManager)CookieHandler.getDefault()).setCookiePolicy(CookieHttpTransaction.testPolicies[i]);
0N/A ((CookieManager)CookieHandler.getDefault()).getCookieStore().removeAll();
0N/A URL url = new URL("http" , InetAddress.getLocalHost().getHostAddress(),
0N/A server.getLocalPort(), CookieHttpTransaction.testCases[i][0].serverPath);
0N/A HttpURLConnection uc = (HttpURLConnection)url.openConnection();
0N/A uc.getResponseCode();
0N/A uc.disconnect();
0N/A }
0N/A } catch (IOException e) {
0N/A e.printStackTrace();
0N/A } finally {
0N/A server.terminate();
0N/A }
0N/A }
0N/A}
0N/A
0N/Aclass CookieHttpTransaction implements HttpCallback {
0N/A public static boolean badRequest = false;
0N/A // the main test control logic will also loop exactly this number
0N/A // to send http request
0N/A public static final int testCount = 6;
0N/A
0N/A private String localHostAddr = "127.0.0.1";
0N/A
0N/A // test cases
0N/A public static class CookieTestCase {
0N/A public String headerToken;
0N/A public String cookieToSend;
0N/A public String cookieToRecv;
0N/A public String serverPath;
0N/A
0N/A public CookieTestCase(String h, String cts, String ctr, String sp) {
0N/A headerToken = h;
0N/A cookieToSend = cts;
0N/A cookieToRecv = ctr;
0N/A serverPath = sp;
0N/A }
0N/A };
0N/A
0N/A //
0N/A // these two must match each other, i.e. testCases.length == testPolicies.length
0N/A //
0N/A public static CookieTestCase[][] testCases = null; // the test cases to run; each test case may contain multiple roundtrips
0N/A public static CookiePolicy[] testPolicies = null; // indicates what CookiePolicy to use with each test cases
0N/A
0N/A CookieHttpTransaction() {
0N/A testCases = new CookieTestCase[testCount][];
0N/A testPolicies = new CookiePolicy[testCount];
0N/A
0N/A try {
0N/A localHostAddr = InetAddress.getLocalHost().getHostAddress();
0N/A } catch (Exception ignored) {
0N/A };
0N/A int count = 0;
0N/A
0N/A // an http session with Netscape cookies exchanged
0N/A testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
0N/A testCases[count++] = new CookieTestCase[]{
0N/A new CookieTestCase("Set-Cookie",
5556N/A "CUSTOMER=WILE:BOB; path=/; expires=Sat, 09-Nov-2030 23:12:40 GMT;" + "domain=." + localHostAddr,
0N/A "CUSTOMER=WILE:BOB",
0N/A "/"
0N/A ),
0N/A new CookieTestCase("Set-Cookie",
0N/A "PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,
51N/A "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",
0N/A "/"
0N/A ),
0N/A new CookieTestCase("Set-Cookie",
0N/A "SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,
51N/A "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",
0N/A "/"
0N/A ),
0N/A new CookieTestCase("Set-Cookie",
0N/A "SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,
51N/A "CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX",
0N/A "/foo"
0N/A )
0N/A };
0N/A
0N/A // check whether or not path rule is applied
0N/A testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
0N/A testCases[count++] = new CookieTestCase[]{
0N/A new CookieTestCase("Set-Cookie",
0N/A "PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,
0N/A "PART_NUMBER=ROCKET_LAUNCHER_0001",
0N/A "/"
0N/A ),
0N/A new CookieTestCase("Set-Cookie",
0N/A "PART_NUMBER=RIDING_ROCKET_0023; path=/ammo;" + "domain=." + localHostAddr,
51N/A "PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001",
0N/A "/ammo"
0N/A )
0N/A };
0N/A
0N/A // an http session with rfc2965 cookies exchanged
0N/A testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
0N/A testCases[count++] = new CookieTestCase[]{
0N/A new CookieTestCase("Set-Cookie2",
0N/A "Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
51N/A "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
0N/A "/acme/login"
0N/A ),
0N/A new CookieTestCase("Set-Cookie2",
0N/A "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\";" + "domain=." + localHostAddr,
51N/A "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
0N/A "/acme/pickitem"
0N/A ),
0N/A new CookieTestCase("Set-Cookie2",
0N/A "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
51N/A "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
0N/A "/acme/shipping"
0N/A )
0N/A };
0N/A
0N/A // check whether or not the path rule is applied
0N/A testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
0N/A testCases[count++] = new CookieTestCase[]{
0N/A new CookieTestCase("Set-Cookie2",
0N/A "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
51N/A "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
0N/A "/acme/ammo"
0N/A ),
0N/A new CookieTestCase("Set-Cookie2",
0N/A "Part_Number=\"Riding_Rocket_0023\"; Version=\"1\"; Path=\"/acme/ammo\";" + "domain=." + localHostAddr,
51N/A "$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\";$Path=\"/acme/ammo\";$Domain=\"." + localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
0N/A "/acme/ammo"
0N/A ),
0N/A new CookieTestCase("",
0N/A "",
51N/A "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
0N/A "/acme/parts"
0N/A )
0N/A };
0N/A
0N/A // new cookie should overwrite old cookie
0N/A testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
0N/A testCases[count++] = new CookieTestCase[]{
0N/A new CookieTestCase("Set-Cookie2",
0N/A "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
51N/A "$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
0N/A "/acme"
0N/A ),
0N/A new CookieTestCase("Set-Cookie2",
0N/A "Part_Number=\"Rocket_Launcher_2000\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
51N/A "$Version=\"1\"; Part_Number=\"Rocket_Launcher_2000\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
0N/A "/acme"
0N/A )
0N/A };
0N/A
0N/A // cookies without domain attributes
1248N/A // RFC 2965 states that domain should default to host
0N/A testPolicies[count] = CookiePolicy.ACCEPT_ALL;
0N/A testCases[count++] = new CookieTestCase[]{
0N/A new CookieTestCase("Set-Cookie2",
0N/A "Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"",
1248N/A "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
0N/A "/acme/login"
0N/A ),
0N/A new CookieTestCase("Set-Cookie2",
0N/A "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"",
1248N/A "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
0N/A "/acme/pickitem"
0N/A ),
0N/A new CookieTestCase("Set-Cookie2",
0N/A "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"",
1248N/A "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
0N/A "/acme/shipping"
0N/A )
0N/A };
0N/A
0N/A assert count == testCount;
0N/A }
0N/A
0N/A private int testcaseDone = 0;
0N/A private int testDone = 0;
0N/A /*
0N/A * Our http server which is conducted by testCases array
0N/A */
0N/A public void request(HttpTransaction trans) {
0N/A try {
0N/A if (testDone < testCases[testcaseDone].length) {
0N/A // still have other tests to run,
0N/A // check the Cookie header and then redirect it
0N/A if (testDone > 0) checkResquest(trans);
0N/A trans.addResponseHeader("Location", testCases[testcaseDone][testDone].serverPath);
0N/A trans.addResponseHeader(testCases[testcaseDone][testDone].headerToken,
0N/A testCases[testcaseDone][testDone].cookieToSend);
0N/A testDone++;
0N/A trans.sendResponse(302, "Moved Temporarily");
0N/A } else {
0N/A // the last test of this test case
0N/A if (testDone > 0) checkResquest(trans);
0N/A testcaseDone++;
0N/A testDone = 0;
0N/A trans.sendResponse(200, "OK");
0N/A }
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A private void checkResquest(HttpTransaction trans) {
0N/A String cookieHeader = null;
0N/A
0N/A assert testDone > 0;
0N/A cookieHeader = trans.getRequestHeader("Cookie");
0N/A if (cookieHeader != null &&
0N/A cookieHeader.equalsIgnoreCase(testCases[testcaseDone][testDone-1].cookieToRecv))
0N/A {
0N/A System.out.printf("%15s %s\n", "PASSED:", cookieHeader);
0N/A } else {
0N/A System.out.printf("%15s %s\n", "FAILED:", cookieHeader);
0N/A System.out.printf("%15s %s\n\n", "should be:", testCases[testcaseDone][testDone-1].cookieToRecv);
0N/A badRequest = true;
0N/A }
0N/A }
0N/A}