# HG changeset patch # User mli # Date 1483506346 28800 # Node ID 224e91ad76c4bb6c9ab8abfc3396f064ae64bb4e # Parent faf815b53545afbf3be24bb07f510b805c9a887d 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout Summary: The fix sets timeout for the server and the client, and ignore SocketTimeoutException. Reviewed-by: chegar Contributed-by: John Jiang diff -r faf815b53545 -r 224e91ad76c4 jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.java --- a/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.java Wed Jan 04 00:08:40 2017 +0000 +++ b/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.java Tue Jan 03 21:05:46 2017 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -21,27 +21,32 @@ * questions. */ -/* - * This test is run using PostThruProxy.sh - */ - import java.io.*; import java.net.*; import java.security.KeyStore; import javax.net.*; import javax.net.ssl.*; -import java.security.cert.*; + +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.process.ProcessTools; /* - * This test case is written to test the https POST through a proxy. - * There is no proxy authentication done. - * - * PostThruProxy.java -- includes a simple server that serves - * http POST method requests in secure channel, and a client - * that makes https POST request through a proxy. + * @test + * @bug 4423074 + * @modules java.base/sun.net.www + * @summary This test case is written to test the https POST through a proxy. + * There is no proxy authentication done. It includes a simple server + * that serves http POST method requests in secure channel, and a client + * that makes https POST request through a proxy. + * @library /test/lib + * @compile OriginServer.java ProxyTunnelServer.java + * @run main/othervm PostThruProxy */ +public class PostThruProxy { -public class PostThruProxy { + private static final String TEST_SRC = System.getProperty("test.src", "."); + private static final int TIMEOUT = 30000; + /* * Where do we find the keystores? */ @@ -76,14 +81,10 @@ /* * Main method to create the server and client */ - public static void main(String args[]) throws Exception - { - String keyFilename = - args[1] + "/" + pathToStores + - "/" + keyStoreFile; - String trustFilename = - args[1] + "/" + pathToStores + - "/" + trustStoreFile; + public static void main(String args[]) throws Exception { + String keyFilename = TEST_SRC + "/" + pathToStores + "/" + keyStoreFile; + String trustFilename = TEST_SRC + "/" + pathToStores + "/" + + trustStoreFile; System.setProperty("javax.net.ssl.keyStore", keyFilename); System.setProperty("javax.net.ssl.keyStorePassword", passwd); @@ -95,10 +96,9 @@ * setup the server */ try { - ServerSocketFactory ssf = - PostThruProxy.getServerSocketFactory(useSSL); + ServerSocketFactory ssf = getServerSocketFactory(useSSL); ServerSocket ss = ssf.createServerSocket(serverPort); - ss.setSoTimeout(30000); // 30 seconds + ss.setSoTimeout(TIMEOUT); // 30 seconds serverPort = ss.getLocalPort(); new TestServer(ss); } catch (Exception e) { @@ -108,35 +108,29 @@ } // trigger the client try { - doClientSide(args[0]); + doClientSide(); } catch (Exception e) { System.out.println("Client side failed: " + e.getMessage()); throw e; - } + } } private static ServerSocketFactory getServerSocketFactory (boolean useSSL) throws Exception { if (useSSL) { - SSLServerSocketFactory ssf = null; // set up key manager to do server authentication - SSLContext ctx; - KeyManagerFactory kmf; - KeyStore ks; + SSLContext ctx = SSLContext.getInstance("TLS"); + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + KeyStore ks = KeyStore.getInstance("JKS"); char[] passphrase = passwd.toCharArray(); - ctx = SSLContext.getInstance("TLS"); - kmf = KeyManagerFactory.getInstance("SunX509"); - ks = KeyStore.getInstance("JKS"); - ks.load(new FileInputStream(System.getProperty( "javax.net.ssl.keyStore")), passphrase); kmf.init(ks, passphrase); ctx.init(kmf.getKeyManagers(), null, null); - ssf = ctx.getServerSocketFactory(); - return ssf; + return ctx.getServerSocketFactory(); } else { return ServerSocketFactory.getDefault(); } @@ -147,7 +141,7 @@ */ static String postMsg = "Testing HTTP post on a https server"; - static void doClientSide(String hostname) throws Exception { + static void doClientSide() throws Exception { HostnameVerifier reservedHV = HttpsURLConnection.getDefaultHostnameVerifier(); try { @@ -162,10 +156,12 @@ */ HttpsURLConnection.setDefaultHostnameVerifier( new NameVerifier()); - URL url = new URL("https://" + hostname+ ":" + serverPort); + URL url = new URL("https://" + getHostname() +":" + serverPort); Proxy p = new Proxy(Proxy.Type.HTTP, pAddr); HttpsURLConnection https = (HttpsURLConnection)url.openConnection(p); + https.setConnectTimeout(TIMEOUT); + https.setReadTimeout(TIMEOUT); https.setDoOutput(true); https.setRequestMethod("POST"); PrintStream ps = null; @@ -190,6 +186,9 @@ if (ps != null) ps.close(); throw e; + } catch (SocketTimeoutException e) { + System.out.println("Client can not get response in time: " + + e.getMessage()); } } finally { HttpsURLConnection.setDefaultHostnameVerifier(reservedHV); @@ -210,4 +209,13 @@ pserver.start(); return new InetSocketAddress("localhost", pserver.getPort()); } + + private static String getHostname() { + try { + OutputAnalyzer oa = ProcessTools.executeCommand("hostname"); + return oa.getOutput().trim(); + } catch (Throwable e) { + throw new RuntimeException("Get hostname failed.", e); + } + } } diff -r faf815b53545 -r 224e91ad76c4 jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh --- a/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh Wed Jan 04 00:08:40 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -#!/bin/sh - -# -# Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. -# -# This code is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 2 only, as -# published by the Free Software Foundation. -# -# This code is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -# version 2 for more details (a copy is included in the LICENSE file that -# accompanied this code). -# -# You should have received a copy of the GNU General Public License version -# 2 along with this work; if not, write to the Free Software Foundation, -# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. -# -# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA -# or visit www.oracle.com if you need additional information or have any -# questions. -# - - -# -# @test -# @bug 4423074 -# @summary Need to rebase all the duplicated classes from Merlin -# @modules java.base/sun.net.www - -HOSTNAME=`uname -n` -OS=`uname -s` -case "$OS" in - SunOS | Linux | Darwin | AIX ) - PS=":" - FS="/" - ;; - CYGWIN* ) - PS=";" - FS="/" - ;; - Windows* ) - PS=";" - FS="\\" - ;; - * ) - echo "Unrecognized system!" - exit 1; - ;; -esac - -EXTRAOPTS="--add-exports java.base/sun.net.www=ALL-UNNAMED" - -${COMPILEJAVA}${FS}bin${FS}javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} ${EXTRAOPTS} -d . \ - ${TESTSRC}${FS}OriginServer.java \ - ${TESTSRC}${FS}ProxyTunnelServer.java \ - ${TESTSRC}${FS}PostThruProxy.java -${TESTJAVA}${FS}bin${FS}java ${TESTVMOPTS} ${EXTRAOPTS} \ - PostThruProxy ${HOSTNAME} ${TESTSRC} -exit diff -r faf815b53545 -r 224e91ad76c4 jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxyWithAuth.java --- a/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxyWithAuth.java Wed Jan 04 00:08:40 2017 +0000 +++ b/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxyWithAuth.java Tue Jan 03 21:05:46 2017 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -21,27 +21,31 @@ * questions. */ -/* - * This test is run through PostThruProxyWithAuth.sh - */ - import java.io.*; import java.net.*; import java.security.KeyStore; import javax.net.*; import javax.net.ssl.*; -import java.security.cert.*; + +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.process.ProcessTools; /* - * This test case is written to test the https POST through a proxy - * with proxy authentication. - * - * PostThruProxyWithAuth.java -- includes a simple server that serves - * http POST method requests in secure channel, and a client - * that makes https POST request through a proxy. + * @test + * @bug 4423074 + * @modules java.base/sun.net.www + * @summary This test case is written to test the https POST through a proxy + * with proxy authentication. It includes a simple server that serves + * http POST method requests in secure channel, and a client that + * makes https POST request through a proxy. + * @library /test/lib + * @compile OriginServer.java ProxyTunnelServer.java + * @run main/othervm -Djdk.http.auth.tunneling.disabledSchemes= PostThruProxyWithAuth */ +public class PostThruProxyWithAuth { -public class PostThruProxyWithAuth { + private static final String TEST_SRC = System.getProperty("test.src", "."); + private static final int TIMEOUT = 30000; /* * Where do we find the keystores? @@ -78,14 +82,10 @@ /* * Main method to create the server and client */ - public static void main(String args[]) throws Exception - { - String keyFilename = - args[1] + "/" + pathToStores + - "/" + keyStoreFile; - String trustFilename = - args[1] + "/" + pathToStores + - "/" + trustStoreFile; + public static void main(String args[]) throws Exception { + String keyFilename = TEST_SRC + "/" + pathToStores + "/" + keyStoreFile; + String trustFilename = TEST_SRC + "/" + pathToStores + "/" + + trustStoreFile; System.setProperty("javax.net.ssl.keyStore", keyFilename); System.setProperty("javax.net.ssl.keyStorePassword", passwd); @@ -97,10 +97,9 @@ * setup the server */ try { - ServerSocketFactory ssf = - PostThruProxyWithAuth.getServerSocketFactory(useSSL); + ServerSocketFactory ssf = getServerSocketFactory(useSSL); ServerSocket ss = ssf.createServerSocket(serverPort); - ss.setSoTimeout(30000); // 30 seconds + ss.setSoTimeout(TIMEOUT); // 30 seconds serverPort = ss.getLocalPort(); new TestServer(ss); } catch (Exception e) { @@ -110,7 +109,7 @@ } // trigger the client try { - doClientSide(args[0]); + doClientSide(); } catch (Exception e) { System.out.println("Client side failed: " + e.getMessage()); @@ -121,24 +120,18 @@ private static ServerSocketFactory getServerSocketFactory (boolean useSSL) throws Exception { if (useSSL) { - SSLServerSocketFactory ssf = null; // set up key manager to do server authentication - SSLContext ctx; - KeyManagerFactory kmf; - KeyStore ks; + SSLContext ctx = SSLContext.getInstance("TLS"); + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + KeyStore ks = KeyStore.getInstance("JKS"); char[] passphrase = passwd.toCharArray(); - ctx = SSLContext.getInstance("TLS"); - kmf = KeyManagerFactory.getInstance("SunX509"); - ks = KeyStore.getInstance("JKS"); - ks.load(new FileInputStream(System.getProperty( "javax.net.ssl.keyStore")), passphrase); kmf.init(ks, passphrase); ctx.init(kmf.getKeyManagers(), null, null); - ssf = ctx.getServerSocketFactory(); - return ssf; + return ctx.getServerSocketFactory(); } else { return ServerSocketFactory.getDefault(); } @@ -149,7 +142,7 @@ */ static String postMsg = "Testing HTTP post on a https server"; - static void doClientSide(String hostname) throws Exception { + static void doClientSide() throws Exception { /* * setup up a proxy */ @@ -161,10 +154,12 @@ */ HttpsURLConnection.setDefaultHostnameVerifier( new NameVerifier()); - URL url = new URL("https://" + hostname + ":" + serverPort); + URL url = new URL("https://" + getHostname() + ":" + serverPort); Proxy p = new Proxy(Proxy.Type.HTTP, pAddr); HttpsURLConnection https = (HttpsURLConnection)url.openConnection(p); + https.setConnectTimeout(TIMEOUT); + https.setReadTimeout(TIMEOUT); https.setDoOutput(true); https.setRequestMethod("POST"); PrintStream ps = null; @@ -188,6 +183,9 @@ if (ps != null) ps.close(); throw e; + } catch (SocketTimeoutException e) { + System.out.println("Client can not get response in time: " + + e.getMessage()); } } @@ -221,4 +219,13 @@ "test123".toCharArray()); } } + + private static String getHostname() { + try { + OutputAnalyzer oa = ProcessTools.executeCommand("hostname"); + return oa.getOutput().trim(); + } catch (Throwable e) { + throw new RuntimeException("Get hostname failed.", e); + } + } } diff -r faf815b53545 -r 224e91ad76c4 jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxyWithAuth.sh --- a/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxyWithAuth.sh Wed Jan 04 00:08:40 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -#!/bin/sh - -# -# Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. -# -# This code is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 2 only, as -# published by the Free Software Foundation. -# -# This code is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -# version 2 for more details (a copy is included in the LICENSE file that -# accompanied this code). -# -# You should have received a copy of the GNU General Public License version -# 2 along with this work; if not, write to the Free Software Foundation, -# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. -# -# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA -# or visit www.oracle.com if you need additional information or have any -# questions. -# - - -# -# @test -# @bug 4423074 -# @summary Need to rebase all the duplicated classes from Merlin -# @modules java.base/sun.net.www - -HOSTNAME=`uname -n` -OS=`uname -s` -case "$OS" in - SunOS | Linux | Darwin | AIX ) - PS=":" - FS="/" - ;; - CYGWIN* ) - PS=";" - FS="/" - ;; - Windows* ) - PS=";" - FS="\\" - ;; - * ) - echo "Unrecognized system!" - exit 1; - ;; -esac - -EXTRAOPTS="--add-exports java.base/sun.net.www=ALL-UNNAMED" -${COMPILEJAVA}${FS}bin${FS}javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} ${EXTRAOPTS} \ - -d . ${TESTSRC}${FS}OriginServer.java \ - ${TESTSRC}${FS}ProxyTunnelServer.java \ - ${TESTSRC}${FS}PostThruProxyWithAuth.java -${TESTJAVA}${FS}bin${FS}java ${TESTVMOPTS} ${EXTRAOPTS} \ - -Djdk.http.auth.tunneling.disabledSchemes= \ - PostThruProxyWithAuth ${HOSTNAME} ${TESTSRC} -exit diff -r faf815b53545 -r 224e91ad76c4 jdk/test/sun/net/www/protocol/https/HttpsURLConnection/ProxyTunnelServer.java --- a/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/ProxyTunnelServer.java Wed Jan 04 00:08:40 2017 +0000 +++ b/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/ProxyTunnelServer.java Tue Jan 03 21:05:46 2017 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,13 +32,14 @@ import java.io.*; import java.net.*; -import javax.net.ssl.*; import javax.net.ServerSocketFactory; import sun.net.www.*; import java.util.Base64; public class ProxyTunnelServer extends Thread { + private static final int TIMEOUT = 30000; + private static ServerSocket ss = null; /* * holds the registered user's username and password @@ -64,8 +65,9 @@ public ProxyTunnelServer() throws IOException { if (ss == null) { - ss = (ServerSocket) ServerSocketFactory.getDefault(). - createServerSocket(0); + ss = (ServerSocket) ServerSocketFactory.getDefault() + .createServerSocket(0); + ss.setSoTimeout(TIMEOUT); } } @@ -86,6 +88,9 @@ try { clientSocket = ss.accept(); processRequests(); + } catch (SocketTimeoutException e) { + System.out.println( + "Proxy can not get response in time: " + e.getMessage()); } catch (Exception e) { System.out.println("Proxy Failed: " + e); e.printStackTrace(); @@ -188,8 +193,8 @@ serverToClient.start(); System.out.println("Proxy: Started tunneling......."); - clientToServer.join(); - serverToClient.join(); + clientToServer.join(TIMEOUT); + serverToClient.join(TIMEOUT); System.out.println("Proxy: Finished tunneling........"); clientToServer.close(); @@ -219,13 +224,11 @@ int BUFFER_SIZE = 400; byte[] buf = new byte[BUFFER_SIZE]; int bytesRead = 0; - int count = 0; // keep track of the amount of data transfer try { while ((bytesRead = input.read(buf)) >= 0) { output.write(buf, 0, bytesRead); output.flush(); - count += bytesRead; } } catch (IOException e) { /* @@ -236,7 +239,7 @@ } } - public void close() { + private void close() { try { if (!sockIn.isClosed()) sockIn.close(); @@ -275,7 +278,7 @@ serverPort = Integer.parseInt(connectInfo.substring(endi+1)); } catch (Exception e) { throw new IOException("Proxy recieved a request: " - + connectStr); + + connectStr, e); } serverInetAddr = InetAddress.getByName(serverName); }