8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake"
authorfyuan
Mon, 16 Jan 2017 15:16:10 +0800
changeset 43106 4a7670d316ee
parent 43105 b1d99aa9f29f
child 43107 b232ec246f2b
8167146: sun/security/pkcs11/sslecc/ClientJSSEServerJSSE.java failed with "Remote host terminated the handshake" Summary: The fix takes the server to accept request after the client threads start, and also deal with possible timeout issue. Reviewed-by: xuelei Contributed-by: John Jiang <sha.jiang@oracle.com>
jdk/test/sun/security/pkcs11/sslecc/CipherTest.java
jdk/test/sun/security/pkcs11/sslecc/JSSEClient.java
jdk/test/sun/security/pkcs11/sslecc/JSSEServer.java
--- a/jdk/test/sun/security/pkcs11/sslecc/CipherTest.java	Fri Jan 13 14:58:21 2017 -0800
+++ b/jdk/test/sun/security/pkcs11/sslecc/CipherTest.java	Mon Jan 16 15:16:10 2017 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2017, 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
@@ -28,7 +28,6 @@
 
 import java.security.*;
 import java.security.cert.*;
-import java.security.cert.Certificate;
 
 import javax.net.ssl.*;
 
@@ -61,6 +60,8 @@
 
     private static PeerFactory peerFactory;
 
+    static final CountDownLatch clientCondition = new CountDownLatch(1);
+
     static abstract class Server implements Runnable {
 
         final CipherTest cipherTest;
@@ -313,6 +314,10 @@
             }
             threads[i].start();
         }
+
+        // The client threads are ready.
+        clientCondition.countDown();
+
         try {
             for (int i = 0; i < THREADS; i++) {
                 threads[i].join();
@@ -367,6 +372,10 @@
                 try {
                     runTest(params);
                     System.out.println("Passed " + params);
+                } catch (SocketTimeoutException ste) {
+                    System.out.println("The client connects to the server timeout, "
+                            + "so ignore the test.");
+                    break;
                 } catch (Exception e) {
                     cipherTest.setFailed();
                     System.out.println("** Failed " + params + "**");
--- a/jdk/test/sun/security/pkcs11/sslecc/JSSEClient.java	Fri Jan 13 14:58:21 2017 -0800
+++ b/jdk/test/sun/security/pkcs11/sslecc/JSSEClient.java	Mon Jan 16 15:16:10 2017 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2017, 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
@@ -23,10 +23,7 @@
 
 import java.io.*;
 import java.net.*;
-import java.util.*;
 
-import java.security.*;
-import java.security.cert.*;
 import java.security.cert.Certificate;
 
 import javax.net.ssl.*;
@@ -46,10 +43,30 @@
         SSLSocket socket = null;
         try {
             keyManager.setAuthType(params.clientAuth);
-            sslContext.init(new KeyManager[] {keyManager}, new TrustManager[] {cipherTest.trustManager}, cipherTest.secureRandom);
-            SSLSocketFactory factory = (SSLSocketFactory)sslContext.getSocketFactory();
-            socket = (SSLSocket)factory.createSocket("127.0.0.1", cipherTest.serverPort);
-            socket.setSoTimeout(cipherTest.TIMEOUT);
+            sslContext.init(
+                    new KeyManager[] { keyManager },
+                    new TrustManager[] { CipherTest.trustManager },
+                    CipherTest.secureRandom);
+            SSLSocketFactory factory
+                    = (SSLSocketFactory) sslContext.getSocketFactory();
+
+            socket = (SSLSocket) factory.createSocket();
+            try {
+                socket.connect(new InetSocketAddress("127.0.0.1",
+                        CipherTest.serverPort), 15000);
+            } catch (IOException ioe) {
+                // The server side may be impacted by naughty test cases or
+                // third party routines, and cannot accept connections.
+                //
+                // Just ignore the test if the connection cannot be
+                // established.
+                System.out.println(
+                        "Cannot make a connection in 15 seconds. " +
+                        "Ignore in client side.");
+                return;
+            }
+
+            socket.setSoTimeout(CipherTest.TIMEOUT);
             socket.setEnabledCipherSuites(new String[] {params.cipherSuite});
             socket.setEnabledProtocols(new String[] {params.protocol});
             InputStream in = socket.getInputStream();
--- a/jdk/test/sun/security/pkcs11/sslecc/JSSEServer.java	Fri Jan 13 14:58:21 2017 -0800
+++ b/jdk/test/sun/security/pkcs11/sslecc/JSSEServer.java	Mon Jan 16 15:16:10 2017 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2017, 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
@@ -24,8 +24,11 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.SocketTimeoutException;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
 import javax.net.ssl.KeyManager;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLServerSocket;
@@ -40,24 +43,37 @@
     JSSEServer(CipherTest cipherTest) throws Exception {
         super(cipherTest);
         SSLContext serverContext = SSLContext.getInstance("TLS");
-        serverContext.init(new KeyManager[] {cipherTest.keyManager}, new TrustManager[] {cipherTest.trustManager}, cipherTest.secureRandom);
+        serverContext.init(
+                new KeyManager[] { CipherTest.keyManager },
+                new TrustManager[] { CipherTest.trustManager },
+                CipherTest.secureRandom);
 
         SSLServerSocketFactory factory = (SSLServerSocketFactory)serverContext.getServerSocketFactory();
         serverSocket = (SSLServerSocket)factory.createServerSocket(0);
-        cipherTest.serverPort = serverSocket.getLocalPort();
+        serverSocket.setSoTimeout(CipherTest.TIMEOUT);
+        CipherTest.serverPort = serverSocket.getLocalPort();
         serverSocket.setEnabledCipherSuites(factory.getSupportedCipherSuites());
         serverSocket.setWantClientAuth(true);
     }
 
     @Override
     public void run() {
-        System.out.println("JSSE Server listening on port " + cipherTest.serverPort);
+        System.out.println("JSSE Server listening on port " + CipherTest.serverPort);
         Executor exec = Executors.newFixedThreadPool
                             (CipherTest.THREADS, DaemonThreadFactory.INSTANCE);
+
         try {
+            if (!CipherTest.clientCondition.await(CipherTest.TIMEOUT,
+                    TimeUnit.MILLISECONDS)) {
+                System.out.println(
+                        "The client is not the expected one or timeout. "
+                                + "Ignore in server side.");
+                return;
+            }
+
             while (true) {
                 final SSLSocket socket = (SSLSocket)serverSocket.accept();
-                socket.setSoTimeout(cipherTest.TIMEOUT);
+                socket.setSoTimeout(CipherTest.TIMEOUT);
                 Runnable r = new Runnable() {
                     @Override
                     public void run() {
@@ -86,11 +102,12 @@
                 };
                 exec.execute(r);
             }
-        } catch (IOException e) {
+        } catch (SocketTimeoutException ste) {
+            System.out.println("The server got timeout for waiting for the connection, "
+                    + "so ignore the test.");
+        } catch (Exception e) {
             cipherTest.setFailed();
             e.printStackTrace();
-            //
         }
     }
-
 }