8134267: javax/net/ssl/TLS/TestJSSE.java fails intermittently with BindException: Address already in use
authorasmotrak
Thu, 19 May 2016 09:16:59 -0700
changeset 38430 e8d1995c38fa
parent 38429 0fd11baf20c3
child 38431 ed73e79a0eb4
8134267: javax/net/ssl/TLS/TestJSSE.java fails intermittently with BindException: Address already in use Reviewed-by: chegar
jdk/test/javax/net/ssl/TLS/CipherTestUtils.java
jdk/test/javax/net/ssl/TLS/JSSEServer.java
jdk/test/javax/net/ssl/TLS/TestJSSE.java
--- a/jdk/test/javax/net/ssl/TLS/CipherTestUtils.java	Thu May 19 16:08:40 2016 +0200
+++ b/jdk/test/javax/net/ssl/TLS/CipherTestUtils.java	Thu May 19 09:16:59 2016 -0700
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 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 under
@@ -202,6 +202,8 @@
         @Override
         public abstract void run();
 
+        abstract int getPort();
+
         void handleRequest(InputStream in, OutputStream out)
                 throws IOException {
             boolean newline = false;
@@ -528,9 +530,9 @@
         return ks;
     }
 
-    public static void main(PeerFactory peerFactory, String mode,
-            String expectedException)
-            throws Exception {
+    public static int mainServer(PeerFactory peerFactory,
+            String expectedException) throws Exception {
+
         long time = System.currentTimeMillis();
         setTestedArguments(peerFactory.getTestedProtocol(),
                 peerFactory.getTestedCipher());
@@ -540,33 +542,49 @@
         secureRandom.nextInt();
 
         CipherTestUtils cipherTest = CipherTestUtils.getInstance();
-        if (mode.equalsIgnoreCase("Server")) {  // server mode
-            Thread serverThread = new Thread(peerFactory.newServer(cipherTest),
-                    "Server");
-            serverThread.start();
-        } else if (mode.equalsIgnoreCase("Client")) {
-            peerFactory.newClient(cipherTest).run();
-            cipherTest.checkResult(expectedException);
-            JSSEServer.closeServer = true;
-        } else {
-            throw new RuntimeException("unsupported mode");
-        }
+        Server server = peerFactory.newServer(cipherTest, PeerFactory.FREE_PORT);
+        Thread serverThread = new Thread(server, "Server");
+        serverThread.start();
+
         time = System.currentTimeMillis() - time;
         System.out.println("Elapsed time " + time);
 
+        return server.getPort();
+    }
+
+    public static void mainClient(PeerFactory peerFactory, int port,
+            String expectedException) throws Exception {
+
+        long time = System.currentTimeMillis();
+        setTestedArguments(peerFactory.getTestedProtocol(),
+                peerFactory.getTestedCipher());
+
+        System.out.print(
+                " Initializing test '" + peerFactory.getName() + "'...");
+        secureRandom.nextInt();
+
+        CipherTestUtils cipherTest = CipherTestUtils.getInstance();
+        peerFactory.newClient(cipherTest, port).run();
+        cipherTest.checkResult(expectedException);
+        JSSEServer.closeServer = true;
+
+        time = System.currentTimeMillis() - time;
+        System.out.println("Elapsed time " + time);
     }
 
     public static abstract class PeerFactory {
 
+        public static final int FREE_PORT = 0;
+
         abstract String getName();
 
         abstract String getTestedProtocol();
 
         abstract String getTestedCipher();
 
-        abstract Client newClient(CipherTestUtils cipherTest) throws Exception;
+        abstract Client newClient(CipherTestUtils cipherTest, int testPort) throws Exception;
 
-        abstract Server newServer(CipherTestUtils cipherTest) throws Exception;
+        abstract Server newServer(CipherTestUtils cipherTest, int testPort) throws Exception;
 
         boolean isSupported(String cipherSuite) {
             return true;
--- a/jdk/test/javax/net/ssl/TLS/JSSEServer.java	Thu May 19 16:08:40 2016 +0200
+++ b/jdk/test/javax/net/ssl/TLS/JSSEServer.java	Thu May 19 09:16:59 2016 -0700
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 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 under
@@ -77,4 +77,8 @@
             }
         }
     }
+
+    int getPort() {
+        return serverSocket.getLocalPort();
+    }
 }
--- a/jdk/test/javax/net/ssl/TLS/TestJSSE.java	Thu May 19 16:08:40 2016 +0200
+++ b/jdk/test/javax/net/ssl/TLS/TestJSSE.java	Thu May 19 09:16:59 2016 -0700
@@ -27,10 +27,8 @@
 /**
  * @test
  * @bug 8049429
- * @library ../../../../lib/testlibrary/
  * @modules java.management
  *          jdk.crypto.ec/sun.security.ec
- * @build jdk.testlibrary.Utils
  * @compile CipherTestUtils.java JSSEClient.java JSSEServer.java
  * @summary Test that all cipher suites work in all versions and all client
  * authentication types. The way this is setup the server is stateless and
@@ -86,7 +84,6 @@
 
         String serverProtocol = System.getProperty("SERVER_PROTOCOL");
         String clientProtocol = System.getProperty("CLIENT_PROTOCOL");
-        int port = jdk.testlibrary.Utils.getFreePort();
         String cipher = System.getProperty("CIPHER");
         if (serverProtocol == null
                 || clientProtocol == null
@@ -97,7 +94,7 @@
         out.println("ServerProtocol =" + serverProtocol);
         out.println("ClientProtocol =" + clientProtocol);
         out.println("Cipher         =" + cipher);
-        server(serverProtocol, cipher, port, args);
+        int port = server(serverProtocol, cipher, args);
         client(port, clientProtocol, cipher, args);
 
     }
@@ -112,28 +109,30 @@
         out.println(" Testing - Protocol : " + testProtocols);
         out.println(" Testing - Cipher : " + testCipher);
         try {
-            CipherTestUtils.main(new JSSEFactory(LOCAL_IP,
-                    testPort, testProtocols,
-                    testCipher, "client JSSE"),
-                    "client", expectedException);
+            CipherTestUtils.mainClient(new JSSEFactory(LOCAL_IP, testProtocols,
+                        testCipher, "Client JSSE"),
+                    testPort, expectedException);
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
 
-    public static void server(String testProtocol, String testCipher,
-            int testPort,
+    public static int server(String testProtocol, String testCipher,
             String... exception) throws Exception {
+
         String expectedException = exception.length >= 1
                 ? exception[0] : null;
         out.println(" This is Server");
         out.println(" Testing Protocol: " + testProtocol);
         out.println(" Testing Cipher: " + testCipher);
-        out.println(" Testing Port: " + testPort);
+
         try {
-            CipherTestUtils.main(new JSSEFactory(null, testPort,
-                    testProtocol, testCipher, "Server JSSE"),
-                    "Server", expectedException);
+            int port = CipherTestUtils.mainServer(new JSSEFactory(
+                        null, testProtocol, testCipher, "Server JSSE"),
+                    expectedException);
+
+            out.println(" Testing Port: " + port);
+            return port;
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
@@ -142,15 +141,13 @@
     private static class JSSEFactory extends CipherTestUtils.PeerFactory {
 
         final String testedCipherSuite, testedProtocol, testHost;
-        final int testPort;
         final String name;
 
-        JSSEFactory(String testHost, int testPort, String testedProtocol,
+        JSSEFactory(String testHost, String testedProtocol,
                 String testedCipherSuite, String name) {
             this.testedCipherSuite = testedCipherSuite;
             this.testedProtocol = testedProtocol;
             this.testHost = testHost;
-            this.testPort = testPort;
             this.name = name;
         }
 
@@ -170,14 +167,14 @@
         }
 
         @Override
-        CipherTestUtils.Client newClient(CipherTestUtils cipherTest)
+        CipherTestUtils.Client newClient(CipherTestUtils cipherTest, int testPort)
                 throws Exception {
             return new JSSEClient(cipherTest, testHost, testPort,
                     testedProtocol, testedCipherSuite);
         }
 
         @Override
-        CipherTestUtils.Server newServer(CipherTestUtils cipherTest)
+        CipherTestUtils.Server newServer(CipherTestUtils cipherTest, int testPort)
                 throws Exception {
             return new JSSEServer(cipherTest, testPort,
                     testedProtocol, testedCipherSuite);