Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/CookieHttpsClientTest.java
41161 views
/*1* Copyright (c) 2012, 2019, 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* @library /test/lib32* @run main/othervm CookieHttpsClientTest33*/3435import java.net.CookieHandler;36import java.net.CookieManager;37import java.net.CookiePolicy;38import java.net.InetAddress;39import java.net.URL;40import java.io.InputStream;41import java.io.IOException;42import javax.net.ssl.HostnameVerifier;43import javax.net.ssl.HttpsURLConnection;44import javax.net.ssl.SSLHandshakeException;45import javax.net.ssl.SSLServerSocket;46import javax.net.ssl.SSLServerSocketFactory;47import javax.net.ssl.SSLSession;48import javax.net.ssl.SSLSocket;49import jdk.test.lib.net.URIBuilder;5051public class CookieHttpsClientTest {52static final int TIMEOUT = 10 * 1000;5354static final String replyString = "HTTP/1.1 200 OK\r\n" +55"Set-Cookie: name=test\r\n" +56"Content-Length: 10\r\n\r\n" +57"1234567890";5859/*60* =============================================================61* Set the various variables needed for the tests, then62* specify what tests to run on each side.63*/6465/*66* Should we run the client or server in a separate thread?67* Both sides can throw exceptions, but do you have a preference68* as to which side should be the main thread.69*/70static boolean separateServerThread = true;7172/*73* Where do we find the keystores?74*/75static String pathToStores = "../../../../../../javax/net/ssl/etc";76static String keyStoreFile = "keystore";77static String trustStoreFile = "truststore";78static String passwd = "passphrase";7980/*81* Is the server ready to serve?82*/83volatile static boolean serverReady = false;8485/*86* Turn on SSL debugging?87*/88static boolean debug = false;8990/*91* Define the server side of the test.92*93* If the server prematurely exits, serverReady will be set to true94* to avoid infinite hangs.95*/96void doServerSide() throws Exception {97InetAddress loopback = InetAddress.getLoopbackAddress();98SSLServerSocketFactory sslssf =99(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();100SSLServerSocket sslServerSocket =101(SSLServerSocket) sslssf.createServerSocket(serverPort, 0, loopback);102serverPort = sslServerSocket.getLocalPort();103104/*105* Signal Client, we're ready for his connect.106*/107serverReady = true;108SSLSocket sslSocket = null;109try {110sslSocket = (SSLSocket) sslServerSocket.accept();111sslSocket.setSoTimeout(TIMEOUT);112readOneRequest(sslSocket.getInputStream());113sslSocket.getOutputStream().write(replyString.getBytes());114115readOneRequest(sslSocket.getInputStream());116sslSocket.getOutputStream().write(replyString.getBytes());117} catch (Exception e) {118e.printStackTrace();119} finally {120try {121if (sslSocket != null) { sslSocket.close(); }122sslServerSocket.close();123} catch (IOException unused) { /* gulp!burp! */ }124}125}126127/*128* Define the client side of the test.129*130* If the server prematurely exits, serverReady will be set to true131* to avoid infinite hangs.132*/133void doClientSide() throws Exception {134// Wait for server to get started.135while (!serverReady) {136Thread.sleep(50);137}138139HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {140public boolean verify(String hostname, SSLSession session) {141return true;142}});143144URL url = URIBuilder.newBuilder()145.scheme("https")146.loopback()147.port(serverPort)148.path("/")149.toURL();150151System.out.println("Client ready to connect to: " + url);152153// Run without a CookieHandler first154InputStream in = url.openConnection(java.net.Proxy.NO_PROXY).getInputStream();155while (in.read() != -1); // read response body so connection can be reused156157// Set a CookeHandler and retest using the HttpClient from the KAC158CookieManager manager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);159CookieHandler.setDefault(manager);160161in = url.openConnection().getInputStream();162while (in.read() != -1);163164if (manager.getCookieStore().getCookies().isEmpty()) {165throw new RuntimeException("Failed: No cookies in the cookie Handler.");166}167}168169static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };170171// Read until the end of a HTTP request172static void readOneRequest(InputStream is) throws IOException {173int requestEndCount = 0, r;174while ((r = is.read()) != -1) {175if (r == requestEnd[requestEndCount]) {176requestEndCount++;177if (requestEndCount == 4) {178break;179}180} else {181requestEndCount = 0;182}183}184}185186/*187* =============================================================188* The remainder is just support stuff189*/190191// use any free port by default192volatile int serverPort = 0;193194volatile Exception serverException = null;195volatile Exception clientException = null;196197private boolean sslConnectionFailed() {198return clientException instanceof SSLHandshakeException;199}200201public static void main(String args[]) throws Exception {202String keyFilename =203System.getProperty("test.src", ".") + "/" + pathToStores +204"/" + keyStoreFile;205String trustFilename =206System.getProperty("test.src", ".") + "/" + pathToStores +207"/" + trustStoreFile;208209System.setProperty("javax.net.ssl.keyStore", keyFilename);210System.setProperty("javax.net.ssl.keyStorePassword", passwd);211System.setProperty("javax.net.ssl.trustStore", trustFilename);212System.setProperty("javax.net.ssl.trustStorePassword", passwd);213214if (debug)215System.setProperty("javax.net.debug", "all");216217new CookieHttpsClientTest();218}219220Thread clientThread = null;221Thread serverThread = null;222223/*224* Primary constructor, used to drive remainder of the test.225*226* Fork off the other side, then do your work.227*/228CookieHttpsClientTest() throws Exception {229Exception startException = null;230try {231if (separateServerThread) {232startServer(true);233startClient(false);234} else {235startClient(true);236startServer(false);237}238} catch (Exception e) {239startException = e;240}241242/*243* Wait for other side to close down.244*/245if (separateServerThread) {246if (serverThread != null) {247// don't join the server thread if the248// client failed to connect249if (!sslConnectionFailed()) {250serverThread.join();251}252}253} else {254if (clientThread != null) {255clientThread.join();256}257}258259/*260* When we get here, the test is pretty much over.261* Which side threw the error?262*/263Exception local;264Exception remote;265266if (separateServerThread) {267remote = serverException;268local = clientException;269} else {270remote = clientException;271local = serverException;272}273274Exception exception = null;275276/*277* Check various exception conditions.278*/279if ((local != null) && (remote != null)) {280// If both failed, return the curthread's exception.281local.addSuppressed(remote);282exception = local;283} else if (local != null) {284exception = local;285} else if (remote != null) {286exception = remote;287} else if (startException != null) {288exception = startException;289}290291/*292* If there was an exception *AND* a startException,293* output it.294*/295if (exception != null) {296if (exception != startException && startException != null) {297exception.addSuppressed(startException);298}299throw exception;300}301302// Fall-through: no exception to throw!303}304305void startServer(boolean newThread) throws Exception {306if (newThread) {307serverThread = new Thread() {308public void run() {309try {310doServerSide();311} catch (Exception e) {312/*313* Our server thread just died.314*315* Release the client, if not active already...316*/317System.err.println("Server died...");318serverReady = true;319serverException = e;320}321}322};323serverThread.start();324} else {325try {326doServerSide();327} catch (Exception e) {328serverException = e;329} finally {330serverReady = true;331}332}333}334335void startClient(boolean newThread) throws Exception {336if (newThread) {337clientThread = new Thread() {338public void run() {339try {340doClientSide();341} catch (Exception e) {342/*343* Our client thread just died.344*/345System.err.println("Client died: " + e);346clientException = e;347}348}349};350clientThread.start();351} else {352try {353doClientSide();354} catch (Exception e) {355System.err.println("Client died: " + e);356clientException = e;357}358}359}360}361362363