changeset 48705:2d250a0174a6

8189760: sun/security/ssl/CertPathRestrictions/TLSRestrictions.java failed with unexpected Exception intermittently Summary: Adds synchronization to make sure the server exception is available Reviewed-by: xuelei
author jjiang
date Thu, 04 Jan 2018 19:58:45 -0800
parents 04d8d293e458
children 20fe8cd3179d 9e524244b67d
files test/jdk/sun/security/ssl/CertPathRestrictions/JSSEServer.java test/jdk/sun/security/ssl/CertPathRestrictions/TLSRestrictions.java
diffstat 2 files changed, 72 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/sun/security/ssl/CertPathRestrictions/JSSEServer.java	Thu Jan 04 10:14:50 2018 -0800
+++ b/test/jdk/sun/security/ssl/CertPathRestrictions/JSSEServer.java	Thu Jan 04 19:58:45 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, 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
@@ -36,8 +36,6 @@
 
     private SSLServerSocket server = null;
 
-    private Exception exception = null;
-
     public JSSEServer(SSLContext context, String constraint,
             boolean needClientAuth) throws Exception {
         TLSRestrictions.setConstraint("Server", constraint);
@@ -49,35 +47,28 @@
         System.out.println("Server: port=" + getPort());
     }
 
-    public void start() {
-        new Thread(new Runnable() {
+    public Exception start() {
+        System.out.println("Server: started");
+        Exception exception = null;
+        try (SSLSocket socket = (SSLSocket) server.accept()) {
+            System.out.println("Server: accepted connection");
+            socket.setSoTimeout(TLSRestrictions.TIMEOUT);
+            InputStream sslIS = socket.getInputStream();
+            OutputStream sslOS = socket.getOutputStream();
+            sslIS.read();
+            sslOS.write('S');
+            sslOS.flush();
+            System.out.println("Server: finished");
+        } catch (Exception e) {
+            exception = e;
+            e.printStackTrace(System.out);
+            System.out.println("Server: failed");
+        }
 
-            @Override
-            public void run() {
-                try {
-                    System.out.println("Server: started");
-                    try (SSLSocket socket = (SSLSocket) server.accept()) {
-                        socket.setSoTimeout(TLSRestrictions.TIMEOUT);
-                        InputStream sslIS = socket.getInputStream();
-                        OutputStream sslOS = socket.getOutputStream();
-                        sslIS.read();
-                        sslOS.write('S');
-                        sslOS.flush();
-                        System.out.println("Server: finished");
-                    }
-                } catch (Exception e) {
-                    e.printStackTrace(System.out);
-                    exception = e;
-                }
-            }
-        }).start();
+        return exception;
     }
 
     public int getPort() {
         return server.getLocalPort();
     }
-
-    public Exception getException() {
-        return exception;
-    }
 }
--- a/test/jdk/sun/security/ssl/CertPathRestrictions/TLSRestrictions.java	Thu Jan 04 10:14:50 2018 -0800
+++ b/test/jdk/sun/security/ssl/CertPathRestrictions/TLSRestrictions.java	Thu Jan 04 19:58:45 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, 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
@@ -36,6 +36,10 @@
 import java.security.cert.CertificateFactory;
 import java.security.spec.PKCS8EncodedKeySpec;
 import java.util.Base64;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 import javax.net.ssl.KeyManagerFactory;
@@ -216,59 +220,59 @@
                 needClientAuth,
                 pass);
 
-        JSSEServer server = new JSSEServer(
-                createSSLContext(trustNames, certNames),
-                serverConstraint,
-                needClientAuth);
-        int port = server.getPort();
-        server.start();
+        ExecutorService executor = Executors.newFixedThreadPool(1);
+        try {
+            JSSEServer server = new JSSEServer(
+                    createSSLContext(trustNames, certNames),
+                    serverConstraint,
+                    needClientAuth);
+            int port = server.getPort();
+            Future<Exception> serverFuture = executor.submit(() -> server.start());
 
-        // Run client on another JVM so that its properties cannot be in conflict
-        // with server's.
-        OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
-                "-Dcert.dir=" + CERT_DIR,
-                "-Djava.security.debug=certpath",
-                "-classpath",
-                TEST_CLASSES,
-                "JSSEClient",
-                port + "",
-                trustNameStr,
-                certNameStr,
-                clientConstraint);
-        int exitValue = outputAnalyzer.getExitValue();
-        String clientOut = outputAnalyzer.getOutput();
+            // Run client on another JVM so that its properties cannot be in conflict
+            // with server's.
+            OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
+                    "-Dcert.dir=" + CERT_DIR,
+                    "-Djava.security.debug=certpath",
+                    "-classpath",
+                    TEST_CLASSES,
+                    "JSSEClient",
+                    port + "",
+                    trustNameStr,
+                    certNameStr,
+                    clientConstraint);
+            int clientExitValue = outputAnalyzer.getExitValue();
+            String clientOut = outputAnalyzer.getOutput();
+            System.out.println("---------- Client output start ----------");
+            System.out.println(clientOut);
+            System.out.println("---------- Client output end ----------");
 
-        Exception serverException = server.getException();
-        if (serverException != null) {
-            System.out.println("Server: failed");
-        }
-
-        System.out.println("---------- Client output start ----------");
-        System.out.println(clientOut);
-        System.out.println("---------- Client output end ----------");
-
-        if (serverException instanceof SocketTimeoutException
-                || clientOut.contains("SocketTimeoutException")) {
-            System.out.println("The communication gets timeout and skips the test.");
-            return;
-        }
-
-        if (pass) {
-            if (serverException != null || exitValue != 0) {
-                throw new RuntimeException(
-                        "Unexpected failure. Operation was blocked.");
-            }
-        } else {
-            if (serverException == null && exitValue == 0) {
-                throw new RuntimeException(
-                        "Unexpected pass. Operation was allowed.");
+            Exception serverException = serverFuture.get(TIMEOUT, TimeUnit.MILLISECONDS);
+            if (serverException instanceof SocketTimeoutException
+                    || clientOut.contains("SocketTimeoutException")) {
+                System.out.println("The communication gets timeout and skips the test.");
+                return;
             }
 
-            // The test may encounter non-SSL issues, like network problem.
-            if (!(serverException instanceof SSLHandshakeException
-                    || clientOut.contains("SSLHandshakeException"))) {
-                throw new RuntimeException("Failure with unexpected exception.");
+            if (pass) {
+                if (serverException != null || clientExitValue != 0) {
+                    throw new RuntimeException(
+                            "Unexpected failure. Operation was blocked.");
+                }
+            } else {
+                if (serverException == null && clientExitValue == 0) {
+                    throw new RuntimeException(
+                            "Unexpected pass. Operation was allowed.");
+                }
+
+                // The test may encounter non-SSL issues, like network problem.
+                if (!(serverException instanceof SSLHandshakeException
+                        || clientOut.contains("SSLHandshakeException"))) {
+                    throw new RuntimeException("Failure with unexpected exception.");
+                }
             }
+        } finally {
+            executor.shutdown();
         }
     }
 
@@ -520,7 +524,6 @@
                     true);
             break;
         }
-
         System.out.println("Case passed");
         System.out.println("========================================");
     }