8160624: sun/security/tools/keytool/printssl.sh failed with "Socket closed"
Reviewed-by: weijun
--- a/jdk/test/sun/security/tools/keytool/PrintSSL.java Tue Jul 05 12:44:28 2016 +0200
+++ b/jdk/test/sun/security/tools/keytool/PrintSSL.java Tue Jul 05 10:04:16 2016 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 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,36 +21,74 @@
* questions.
*/
-// Read printssl.sh, this Java program starts an SSL server
+/*
+ * @test
+ * @bug 6480981 8160624
+ * @modules java.base/sun.security.tools.keytool
+ * @summary keytool should be able to import certificates from remote SSL server
+ * @run main/othervm PrintSSL
+ */
+import java.io.IOException;
import java.net.ServerSocket;
+import java.util.concurrent.CountDownLatch;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
public class PrintSSL {
+
public static void main(String[] args) throws Exception {
- System.setProperty("javax.net.ssl.keyStorePassword", "passphrase");
- System.setProperty("javax.net.ssl.keyStore",
- System.getProperty("test.src", "./")
- + "/../../../../javax/net/ssl/etc/keystore");
- SSLServerSocketFactory sslssf =
- (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
- final ServerSocket server = sslssf.createServerSocket(0);
- System.out.println(server.getLocalPort());
- System.out.flush();
- Thread t = new Thread() {
- public void run() {
- try {
- Thread.sleep(30000);
- server.close();
- } catch (Exception e) {
- ;
- }
- throw new RuntimeException("Timeout");
+
+ int port = new Server().start();
+ if(port == -1) {
+ throw new RuntimeException("Unable start ssl server.");
+ }
+ String vmOpt = System.getProperty("TESTTOOLVMOPTS");
+ String cmd = String.format(
+ "-debug %s -printcert -sslserver localhost:%s",
+ ((vmOpt == null) ? "" : vmOpt ), port);
+ sun.security.tools.keytool.Main.main(cmd.split("\\s+"));
+ }
+
+ private static class Server implements Runnable {
+
+ private volatile int serverPort = -1;
+ private final CountDownLatch untilServerReady = new CountDownLatch(1);
+
+ public int start() throws InterruptedException {
+
+ Thread server = new Thread(this);
+ server.setDaemon(true);
+ server.start();
+ untilServerReady.await();
+ return this.getServerPort();
+ }
+
+ @Override
+ public void run() {
+
+ System.setProperty("javax.net.ssl.keyStorePassword", "passphrase");
+ System.setProperty("javax.net.ssl.keyStore",
+ System.getProperty("test.src", "./")
+ + "/../../../../javax/net/ssl/etc/keystore");
+ SSLServerSocketFactory sslssf =
+ (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
+ try (ServerSocket server = sslssf.createServerSocket(0)) {
+ this.serverPort = server.getLocalPort();
+ System.out.printf("%nServer started on: %s%n", getServerPort());
+ untilServerReady.countDown();
+ ((SSLSocket)server.accept()).startHandshake();
+ } catch (Throwable e) {
+ e.printStackTrace(System.out);
+ untilServerReady.countDown();
}
- };
- t.setDaemon(true);
- t.start();
- ((SSLSocket)server.accept()).startHandshake();
+
+ }
+
+ public int getServerPort() {
+ return this.serverPort;
+ }
+
}
+
}
--- a/jdk/test/sun/security/tools/keytool/printssl.sh Tue Jul 05 12:44:28 2016 +0200
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,72 +0,0 @@
-#
-# Copyright (c) 2008, 2014, 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 6480981
-# @summary keytool should be able to import certificates from remote SSL servers
-
-if [ "${TESTSRC}" = "" ] ; then
- TESTSRC="."
-fi
-if [ "${TESTJAVA}" = "" ] ; then
- echo "TESTJAVA not set. Test cannot execute."
- echo "FAILED!!!"
- exit 1
-fi
-if [ "${COMPILEJAVA}" = "" ]; then
- COMPILEJAVA="${TESTJAVA}"
-fi
-
-# set platform-dependent variables
-OS=`uname -s`
-case "$OS" in
- SunOS | Linux | Darwin | AIX )
- FS="/"
- ;;
- CYGWIN* )
- FS="/"
- ;;
- Windows_* )
- FS="\\"
- ;;
- * )
- echo "Unrecognized operating system!"
- exit 1;
- ;;
-esac
-
-${COMPILEJAVA}${FS}bin${FS}javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d . ${TESTSRC}${FS}PrintSSL.java || exit 10
-${TESTJAVA}${FS}bin${FS}java ${TESTVMOPTS} -Dtest.src=$TESTSRC PrintSSL | (
- read PORT
- if [ "$PORT" = "" ]; then
- echo "Server not started"
- exit 2
- else
- ${TESTJAVA}${FS}bin${FS}keytool ${TESTTOOLVMOPTS} -printcert -sslserver localhost:$PORT
- fi
-)
-status=$?
-
-rm PrintSSL*.class
-
-exit $status