Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/CookieHttpsClientTest.java
38889 views
/*1* Copyright (c) 2012, 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// SunJSSE does not support dynamic system properties, no way to re-use24// system properties in samevm/agentvm mode.2526/*27* @test28* @bug 712908329* @summary Cookiemanager does not store cookies if url is read30* before setting cookiemanager31* @run main/othervm CookieHttpsClientTest32*/3334import java.net.CookieHandler;35import java.net.CookieManager;36import java.net.CookiePolicy;37import java.net.URL;38import java.io.InputStream;39import java.io.IOException;40import javax.net.ssl.HostnameVerifier;41import javax.net.ssl.HttpsURLConnection;42import javax.net.ssl.SSLServerSocket;43import javax.net.ssl.SSLServerSocketFactory;44import javax.net.ssl.SSLSession;45import javax.net.ssl.SSLSocket;4647public class CookieHttpsClientTest {48static final int TIMEOUT = 10 * 1000;4950static final String replyString = "HTTP/1.1 200 OK\r\n" +51"Set-Cookie: name=test\r\n" +52"Content-Length: 10\r\n\r\n" +53"1234567890";5455/*56* =============================================================57* Set the various variables needed for the tests, then58* specify what tests to run on each side.59*/6061/*62* Should we run the client or server in a separate thread?63* Both sides can throw exceptions, but do you have a preference64* as to which side should be the main thread.65*/66static boolean separateServerThread = true;6768/*69* Where do we find the keystores?70*/71static String pathToStores = "../../../../../../javax/net/ssl/etc";72static String keyStoreFile = "keystore";73static String trustStoreFile = "truststore";74static String passwd = "passphrase";7576/*77* Is the server ready to serve?78*/79volatile static boolean serverReady = false;8081/*82* Turn on SSL debugging?83*/84static boolean debug = false;8586/*87* Define the server side of the test.88*89* If the server prematurely exits, serverReady will be set to true90* to avoid infinite hangs.91*/92void doServerSide() throws Exception {93SSLServerSocketFactory sslssf =94(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();95SSLServerSocket sslServerSocket =96(SSLServerSocket) sslssf.createServerSocket(serverPort);97serverPort = sslServerSocket.getLocalPort();9899/*100* Signal Client, we're ready for his connect.101*/102serverReady = true;103SSLSocket sslSocket = null;104try {105sslSocket = (SSLSocket) sslServerSocket.accept();106sslSocket.setSoTimeout(TIMEOUT);107readOneRequest(sslSocket.getInputStream());108sslSocket.getOutputStream().write(replyString.getBytes());109110readOneRequest(sslSocket.getInputStream());111sslSocket.getOutputStream().write(replyString.getBytes());112} catch (Exception e) {113e.printStackTrace();114} finally {115try {116if (sslSocket != null) { sslSocket.close(); }117sslServerSocket.close();118} catch (IOException unused) { /* gulp!burp! */ }119}120}121122/*123* Define the client side of the test.124*125* If the server prematurely exits, serverReady will be set to true126* to avoid infinite hangs.127*/128void doClientSide() throws Exception {129// Wait for server to get started.130while (!serverReady) {131Thread.sleep(50);132}133134HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {135public boolean verify(String hostname, SSLSession session) {136return true;137}});138139URL url = new URL("https://localhost:" + serverPort +"/");140141// Run without a CookieHandler first142InputStream in = url.openConnection().getInputStream();143while (in.read() != -1); // read response body so connection can be reused144145// Set a CookeHandler and retest using the HttpClient from the KAC146CookieManager manager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);147CookieHandler.setDefault(manager);148149in = url.openConnection().getInputStream();150while (in.read() != -1);151152if (manager.getCookieStore().getCookies().isEmpty()) {153throw new RuntimeException("Failed: No cookies in the cookie Handler.");154}155}156157static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };158159// Read until the end of a HTTP request160static void readOneRequest(InputStream is) throws IOException {161int requestEndCount = 0, r;162while ((r = is.read()) != -1) {163if (r == requestEnd[requestEndCount]) {164requestEndCount++;165if (requestEndCount == 4) {166break;167}168} else {169requestEndCount = 0;170}171}172}173174/*175* =============================================================176* The remainder is just support stuff177*/178179// use any free port by default180volatile int serverPort = 0;181182volatile Exception serverException = null;183volatile Exception clientException = null;184185public static void main(String args[]) throws Exception {186String keyFilename =187System.getProperty("test.src", ".") + "/" + pathToStores +188"/" + keyStoreFile;189String trustFilename =190System.getProperty("test.src", ".") + "/" + pathToStores +191"/" + trustStoreFile;192193System.setProperty("javax.net.ssl.keyStore", keyFilename);194System.setProperty("javax.net.ssl.keyStorePassword", passwd);195System.setProperty("javax.net.ssl.trustStore", trustFilename);196System.setProperty("javax.net.ssl.trustStorePassword", passwd);197198if (debug)199System.setProperty("javax.net.debug", "all");200201new CookieHttpsClientTest();202}203204Thread clientThread = null;205Thread serverThread = null;206207/*208* Primary constructor, used to drive remainder of the test.209*210* Fork off the other side, then do your work.211*/212CookieHttpsClientTest() throws Exception {213Exception startException = null;214try {215if (separateServerThread) {216startServer(true);217startClient(false);218} else {219startClient(true);220startServer(false);221}222} catch (Exception e) {223startException = e;224}225226/*227* Wait for other side to close down.228*/229if (separateServerThread) {230if (serverThread != null) {231serverThread.join();232}233} else {234if (clientThread != null) {235clientThread.join();236}237}238239/*240* When we get here, the test is pretty much over.241* Which side threw the error?242*/243Exception local;244Exception remote;245246if (separateServerThread) {247remote = serverException;248local = clientException;249} else {250remote = clientException;251local = serverException;252}253254Exception exception = null;255256/*257* Check various exception conditions.258*/259if ((local != null) && (remote != null)) {260// If both failed, return the curthread's exception.261local.initCause(remote);262exception = local;263} else if (local != null) {264exception = local;265} else if (remote != null) {266exception = remote;267} else if (startException != null) {268exception = startException;269}270271/*272* If there was an exception *AND* a startException,273* output it.274*/275if (exception != null) {276if (exception != startException) {277exception.addSuppressed(startException);278}279throw exception;280}281282// Fall-through: no exception to throw!283}284285void startServer(boolean newThread) throws Exception {286if (newThread) {287serverThread = new Thread() {288public void run() {289try {290doServerSide();291} catch (Exception e) {292/*293* Our server thread just died.294*295* Release the client, if not active already...296*/297System.err.println("Server died...");298serverReady = true;299serverException = e;300}301}302};303serverThread.start();304} else {305try {306doServerSide();307} catch (Exception e) {308serverException = e;309} finally {310serverReady = true;311}312}313}314315void startClient(boolean newThread) throws Exception {316if (newThread) {317clientThread = new Thread() {318public void run() {319try {320doClientSide();321} catch (Exception e) {322/*323* Our client thread just died.324*/325System.err.println("Client died...");326clientException = e;327}328}329};330clientThread.start();331} else {332try {333doClientSide();334} catch (Exception e) {335clientException = e;336}337}338}339}340341342