提交 b879cbad 编写于 作者: I igerasim

8189760: sun/security/ssl/CertPathRestrictions/TLSRestrictions.java failed...

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
上级 fb29e369
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -36,8 +36,6 @@ public class JSSEServer { ...@@ -36,8 +36,6 @@ public class JSSEServer {
private SSLServerSocket server = null; private SSLServerSocket server = null;
private Exception exception = null;
public JSSEServer(SSLContext context, public JSSEServer(SSLContext context,
boolean needClientAuth) throws Exception { boolean needClientAuth) throws Exception {
SSLServerSocketFactory serverFactory = context.getServerSocketFactory(); SSLServerSocketFactory serverFactory = context.getServerSocketFactory();
...@@ -47,35 +45,28 @@ public class JSSEServer { ...@@ -47,35 +45,28 @@ public class JSSEServer {
System.out.println("Server: port=" + getPort()); System.out.println("Server: port=" + getPort());
} }
public void start() { public Exception start() {
new Thread(new Runnable() { 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 return exception;
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();
} }
public int getPort() { public int getPort() {
return server.getLocalPort(); return server.getLocalPort();
} }
public Exception getException() {
return exception;
}
} }
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -36,6 +36,10 @@ import java.security.cert.Certificate; ...@@ -36,6 +36,10 @@ import java.security.cert.Certificate;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64; 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 java.util.stream.Collectors;
import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.KeyManagerFactory;
...@@ -210,58 +214,58 @@ public class TLSRestrictions { ...@@ -210,58 +214,58 @@ public class TLSRestrictions {
needClientAuth, needClientAuth,
pass); pass);
setConstraint("Server", serverConstraint); setConstraint("Server", serverConstraint);
JSSEServer server = new JSSEServer( ExecutorService executor = Executors.newFixedThreadPool(1);
createSSLContext(trustNames, certNames), try {
needClientAuth); JSSEServer server = new JSSEServer(
int port = server.getPort(); createSSLContext(trustNames, certNames),
server.start(); needClientAuth);
int port = server.getPort();
// Run client on another JVM so that its properties cannot be in conflict Future<Exception> serverFuture = executor.submit(() -> server.start());
// with server's.
OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm( // Run client on another JVM so that its properties cannot be in conflict
"-Dcert.dir=" + CERT_DIR, // with server's.
"-Djava.security.debug=certpath", OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
"-classpath", "-Dcert.dir=" + CERT_DIR,
TEST_CLASSES, "-Djava.security.debug=certpath",
"JSSEClient", "-classpath",
port + "", TEST_CLASSES,
trustNameStr, "JSSEClient",
certNameStr, port + "",
clientConstraint); trustNameStr,
int exitValue = outputAnalyzer.getExitValue(); certNameStr,
String clientOut = outputAnalyzer.getOutput(); clientConstraint);
int clientExitValue = outputAnalyzer.getExitValue();
Exception serverException = server.getException(); String clientOut = outputAnalyzer.getOutput();
if (serverException != null) { System.out.println("---------- Client output start ----------");
System.out.println("Server: failed"); System.out.println(clientOut);
} System.out.println("---------- Client output end ----------");
System.out.println("---------- Client output start ----------"); Exception serverException = serverFuture.get(TIMEOUT, TimeUnit.MILLISECONDS);
System.out.println(clientOut); if (serverException instanceof SocketTimeoutException
System.out.println("---------- Client output end ----------"); || clientOut.contains("SocketTimeoutException")) {
System.out.println("The communication gets timeout and skips the test.");
if (serverException instanceof SocketTimeoutException return;
|| 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.");
} }
// The test may encounter non-SSL issues, like network problem. if (pass) {
if (!(serverException instanceof SSLHandshakeException if (serverException != null || clientExitValue != 0) {
|| clientOut.contains("SSLHandshakeException"))) { throw new RuntimeException(
throw new RuntimeException("Failure with unexpected exception."); "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();
} }
} }
...@@ -513,7 +517,6 @@ public class TLSRestrictions { ...@@ -513,7 +517,6 @@ public class TLSRestrictions {
true); true);
break; break;
} }
System.out.println("Case passed"); System.out.println("Case passed");
System.out.println("========================================"); System.out.println("========================================");
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册