--- a/jdk/test/javax/net/ssl/templates/SSLSocketSSLEngineTemplate.java Fri Mar 18 13:14:53 2016 +0000
+++ b/jdk/test/javax/net/ssl/templates/SSLSocketSSLEngineTemplate.java Sun Mar 20 00:03:43 2016 +0000
@@ -101,9 +101,6 @@
private static final boolean debug = false;
private final SSLContext sslc;
private SSLEngine serverEngine; // server-side SSLEngine
- private SSLSocket sslSocket; // client-side socket
- private ServerSocket serverSocket; // server-side Socket, generates the...
- private Socket socket; // server-side socket that will read
private final byte[] serverMsg =
"Hi there Client, I'm a Server.".getBytes();
@@ -217,132 +214,128 @@
private void runTest(boolean direct) throws Exception {
boolean serverClose = direct;
- serverSocket = new ServerSocket();
- serverSocket.setReuseAddress(false);
- serverSocket.bind(null);
- int port = serverSocket.getLocalPort();
- Thread thread = createClientThread(port, serverClose);
+ // generates the server-side Socket
+ try (ServerSocket serverSocket = new ServerSocket()) {
+ serverSocket.setReuseAddress(false);
+ serverSocket.bind(null);
+ int port = serverSocket.getLocalPort();
+ Thread thread = createClientThread(port, serverClose);
- socket = serverSocket.accept();
- socket.setSoTimeout(500);
- serverSocket.close();
-
- createSSLEngine();
- createBuffers(direct);
-
- try {
- boolean closed = false;
- // will try to read one more time in case client message
- // is fragmented to multiple pieces
- boolean retry = true;
+ createSSLEngine();
+ createBuffers(direct);
- InputStream is = socket.getInputStream();
- OutputStream os = socket.getOutputStream();
-
- SSLEngineResult serverResult; // results from last operation
+ // server-side socket that will read
+ try (Socket socket = serverSocket.accept()) {
+ socket.setSoTimeout(500);
- /*
- * Examining the SSLEngineResults could be much more involved,
- * and may alter the overall flow of the application.
- *
- * For example, if we received a BUFFER_OVERFLOW when trying
- * to write to the output pipe, we could reallocate a larger
- * pipe, but instead we wait for the peer to drain it.
- */
- byte[] inbound = new byte[8192];
- byte[] outbound = new byte[8192];
+ boolean closed = false;
+ // will try to read one more time in case client message
+ // is fragmented to multiple pieces
+ boolean retry = true;
- while (!isEngineClosed(serverEngine)) {
- int len;
+ InputStream is = socket.getInputStream();
+ OutputStream os = socket.getOutputStream();
- // Inbound data
- log("================");
+ SSLEngineResult serverResult; // results from last operation
- // Read from the Client side.
- try {
- len = is.read(inbound);
- if (len == -1) {
- throw new Exception("Unexpected EOF");
- }
- cTOs.put(inbound, 0, len);
- } catch (SocketTimeoutException ste) {
- // swallow. Nothing yet, probably waiting on us.
- }
-
- cTOs.flip();
-
- serverResult = serverEngine.unwrap(cTOs, serverIn);
- log("server unwrap: ", serverResult);
- runDelegatedTasks(serverResult, serverEngine);
- cTOs.compact();
-
- // Outbound data
- log("----");
-
- serverResult = serverEngine.wrap(serverOut, sTOc);
- log("server wrap: ", serverResult);
- runDelegatedTasks(serverResult, serverEngine);
-
- sTOc.flip();
+ /*
+ * Examining the SSLEngineResults could be much more involved,
+ * and may alter the overall flow of the application.
+ *
+ * For example, if we received a BUFFER_OVERFLOW when trying
+ * to write to the output pipe, we could reallocate a larger
+ * pipe, but instead we wait for the peer to drain it.
+ */
+ byte[] inbound = new byte[8192];
+ byte[] outbound = new byte[8192];
- if ((len = sTOc.remaining()) != 0) {
- sTOc.get(outbound, 0, len);
- os.write(outbound, 0, len);
- // Give the other side a chance to process
- }
+ while (!isEngineClosed(serverEngine)) {
+ int len;
- sTOc.compact();
-
- if (!closed && (serverOut.remaining() == 0)) {
- closed = true;
+ // Inbound data
+ log("================");
- /*
- * We'll alternate initiatating the shutdown.
- * When the server initiates, it will take one more
- * loop, but tests the orderly shutdown.
- */
- if (serverClose) {
- serverEngine.closeOutbound();
- }
- serverIn.flip();
-
- /*
- * A sanity check to ensure we got what was sent.
- */
- if (serverIn.remaining() != clientMsg.length) {
- if (retry && serverIn.remaining() < clientMsg.length) {
- log("Need to read more from client");
- retry = false;
- continue;
- } else {
- throw new Exception("Client: Data length error");
+ // Read from the Client side.
+ try {
+ len = is.read(inbound);
+ if (len == -1) {
+ throw new Exception("Unexpected EOF");
}
+ cTOs.put(inbound, 0, len);
+ } catch (SocketTimeoutException ste) {
+ // swallow. Nothing yet, probably waiting on us.
}
- for (int i = 0; i < clientMsg.length; i++) {
- if (clientMsg[i] != serverIn.get()) {
- throw new Exception("Client: Data content error");
+ cTOs.flip();
+
+ serverResult = serverEngine.unwrap(cTOs, serverIn);
+ log("server unwrap: ", serverResult);
+ runDelegatedTasks(serverResult, serverEngine);
+ cTOs.compact();
+
+ // Outbound data
+ log("----");
+
+ serverResult = serverEngine.wrap(serverOut, sTOc);
+ log("server wrap: ", serverResult);
+ runDelegatedTasks(serverResult, serverEngine);
+
+ sTOc.flip();
+
+ if ((len = sTOc.remaining()) != 0) {
+ sTOc.get(outbound, 0, len);
+ os.write(outbound, 0, len);
+ // Give the other side a chance to process
+ }
+
+ sTOc.compact();
+
+ if (!closed && (serverOut.remaining() == 0)) {
+ closed = true;
+
+ /*
+ * We'll alternate initiatating the shutdown.
+ * When the server initiates, it will take one more
+ * loop, but tests the orderly shutdown.
+ */
+ if (serverClose) {
+ serverEngine.closeOutbound();
}
+ serverIn.flip();
+
+ /*
+ * A sanity check to ensure we got what was sent.
+ */
+ if (serverIn.remaining() != clientMsg.length) {
+ if (retry &&
+ serverIn.remaining() < clientMsg.length) {
+ log("Need to read more from client");
+ retry = false;
+ continue;
+ } else {
+ throw new Exception(
+ "Client: Data length error");
+ }
+ }
+
+ for (int i = 0; i < clientMsg.length; i++) {
+ if (clientMsg[i] != serverIn.get()) {
+ throw new Exception(
+ "Client: Data content error");
+ }
+ }
+ serverIn.compact();
}
- serverIn.compact();
+ }
+ } catch (Exception e) {
+ serverException = e;
+ } finally {
+ // Wait for the client to join up with us.
+ if (thread != null) {
+ thread.join();
}
}
- } catch (Exception e) {
- serverException = e;
} finally {
- if (socket != null) {
- socket.close();
- }
-
- // Wait for the client to join up with us.
- if (thread != null) {
- thread.join();
- }
-
- if (sslSocket != null) {
- sslSocket.close();
- }
-
if (serverException != null) {
if (clientException != null) {
serverException.initCause(clientException);
@@ -369,11 +362,9 @@
@Override
public void run() {
- try {
- Thread.sleep(1000); // Give server time to finish setup.
-
- sslSocket = (SSLSocket) sslc.getSocketFactory().
- createSocket("localhost", port);
+ // client-side socket
+ try (SSLSocket sslSocket = (SSLSocket)sslc.getSocketFactory().
+ createSocket("localhost", port)) {
OutputStream os = sslSocket.getOutputStream();
InputStream is = sslSocket.getInputStream();
--- a/jdk/test/javax/net/ssl/templates/SSLSocketTemplate.java Fri Mar 18 13:14:53 2016 +0000
+++ b/jdk/test/javax/net/ssl/templates/SSLSocketTemplate.java Sun Mar 20 00:03:43 2016 +0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 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
@@ -85,26 +85,26 @@
*/
void doServerSide() throws Exception {
SSLServerSocketFactory sslssf =
- (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
- SSLServerSocket sslServerSocket =
- (SSLServerSocket) sslssf.createServerSocket(serverPort);
+ (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
+ try (SSLServerSocket sslServerSocket =
+ (SSLServerSocket)sslssf.createServerSocket(serverPort)) {
- serverPort = sslServerSocket.getLocalPort();
+ serverPort = sslServerSocket.getLocalPort();
- /*
- * Signal Client, we're ready for his connect.
- */
- serverReady = true;
+ /*
+ * Signal Client, we're ready for his connect.
+ */
+ serverReady = true;
- SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
- InputStream sslIS = sslSocket.getInputStream();
- OutputStream sslOS = sslSocket.getOutputStream();
+ try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {
+ InputStream sslIS = sslSocket.getInputStream();
+ OutputStream sslOS = sslSocket.getOutputStream();
- sslIS.read();
- sslOS.write(85);
- sslOS.flush();
-
- sslSocket.close();
+ sslIS.read();
+ sslOS.write(85);
+ sslOS.flush();
+ }
+ }
}
/*
@@ -123,18 +123,17 @@
}
SSLSocketFactory sslsf =
- (SSLSocketFactory) SSLSocketFactory.getDefault();
- SSLSocket sslSocket = (SSLSocket)
- sslsf.createSocket("localhost", serverPort);
+ (SSLSocketFactory)SSLSocketFactory.getDefault();
+ try (SSLSocket sslSocket =
+ (SSLSocket)sslsf.createSocket("localhost", serverPort)) {
- InputStream sslIS = sslSocket.getInputStream();
- OutputStream sslOS = sslSocket.getOutputStream();
+ InputStream sslIS = sslSocket.getInputStream();
+ OutputStream sslOS = sslSocket.getOutputStream();
- sslOS.write(280);
- sslOS.flush();
- sslIS.read();
-
- sslSocket.close();
+ sslOS.write(280);
+ sslOS.flush();
+ sslIS.read();
+ }
}
/*