From cb8aa405a6f62b28cbe1636fa350fb23c9298cbe Mon Sep 17 00:00:00 2001 From: Xue-Lei Andrew Fan Date: Sun, 20 Mar 2016 00:03:43 +0000 Subject: [PATCH 1/4] 8152221: Use try-with-resource in test templates Reviewed-by: weijun --- .../templates/SSLSocketSSLEngineTemplate.java | 227 +++++++++--------- .../net/ssl/templates/SSLSocketTemplate.java | 53 ++-- 2 files changed, 135 insertions(+), 145 deletions(-) diff --git a/jdk/test/javax/net/ssl/templates/SSLSocketSSLEngineTemplate.java b/jdk/test/javax/net/ssl/templates/SSLSocketSSLEngineTemplate.java index d7f0d38cdee..fd5ec86e4ef 100644 --- a/jdk/test/javax/net/ssl/templates/SSLSocketSSLEngineTemplate.java +++ b/jdk/test/javax/net/ssl/templates/SSLSocketSSLEngineTemplate.java @@ -101,9 +101,6 @@ public class SSLSocketSSLEngineTemplate { 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 @@ public class SSLSocketSSLEngineTemplate { 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); - createSSLEngine(); - createBuffers(direct); + // server-side socket that will read + try (Socket socket = serverSocket.accept()) { + socket.setSoTimeout(500); - try { - boolean closed = false; - // will try to read one more time in case client message - // is fragmented to multiple pieces - boolean retry = true; + boolean closed = false; + // will try to read one more time in case client message + // is fragmented to multiple pieces + boolean retry = true; - InputStream is = socket.getInputStream(); - OutputStream os = socket.getOutputStream(); + InputStream is = socket.getInputStream(); + OutputStream os = socket.getOutputStream(); - SSLEngineResult serverResult; // results from last operation + SSLEngineResult serverResult; // results from last operation - /* - * 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]; + /* + * 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]; - while (!isEngineClosed(serverEngine)) { - int len; + while (!isEngineClosed(serverEngine)) { + int len; - // Inbound data - log("================"); + // Inbound data + log("================"); - // 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(); - - 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"); + // 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 } - serverIn.compact(); + + 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(); + } + } + } 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 @@ public class SSLSocketSSLEngineTemplate { @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(); diff --git a/jdk/test/javax/net/ssl/templates/SSLSocketTemplate.java b/jdk/test/javax/net/ssl/templates/SSLSocketTemplate.java index 920f4056267..12dfcaacae5 100644 --- a/jdk/test/javax/net/ssl/templates/SSLSocketTemplate.java +++ b/jdk/test/javax/net/ssl/templates/SSLSocketTemplate.java @@ -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 @@ public class SSLSocketTemplate { */ 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 @@ public class SSLSocketTemplate { } 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(); + } } /* From b69f398c06adc467694aa870bfa2ed318cb7587b Mon Sep 17 00:00:00 2001 From: Huaming Li Date: Sun, 20 Mar 2016 07:35:20 +0000 Subject: [PATCH 2/4] 8151582: (ch) test java/nio/channels/AsyncCloseAndInterrupt.java failing due to "Connection succeeded" Reviewed-by: bpb, rriggs, alanb --- .../nio/channels/AsyncCloseAndInterrupt.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/jdk/test/java/nio/channels/AsyncCloseAndInterrupt.java b/jdk/test/java/nio/channels/AsyncCloseAndInterrupt.java index 4172efe0daf..d897fa0ea55 100644 --- a/jdk/test/java/nio/channels/AsyncCloseAndInterrupt.java +++ b/jdk/test/java/nio/channels/AsyncCloseAndInterrupt.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 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 @@ -23,6 +23,7 @@ /* @test * @bug 4460583 4470470 4840199 6419424 6710579 6596323 6824135 6395224 7142919 + * 8151582 * @run main/othervm AsyncCloseAndInterrupt * @summary Comprehensive test of asynchronous closing and interruption * @author Mark Reinhold @@ -89,7 +90,7 @@ public class AsyncCloseAndInterrupt { private static void initRefuser() throws IOException { refuser = ServerSocketChannel.open(); - refuser.socket().bind(wildcardAddress); + refuser.bind(wildcardAddress, 1); // use minimum backlog } // Dead pipe source and sink @@ -349,7 +350,7 @@ public class AsyncCloseAndInterrupt { static final Op CONNECT = new Op("connect") { void setup() { - waitPump("connect wait for pumping refuser ..."); + waitPump("connect waiting for pumping refuser ..."); } void doIO(InterruptibleChannel ich) throws IOException { SocketChannel sc = (SocketChannel)ich; @@ -361,7 +362,7 @@ public class AsyncCloseAndInterrupt { static final Op FINISH_CONNECT = new Op("finishConnect") { void setup() { - waitPump("finishConnect wait for pumping refuser ..."); + waitPump("finishConnect waiting for pumping refuser ..."); } void doIO(InterruptibleChannel ich) throws IOException { SocketChannel sc = (SocketChannel)ich; @@ -498,12 +499,11 @@ public class AsyncCloseAndInterrupt { private static volatile boolean pumpReady = false; private static void waitPump(String msg){ - pumpReady = false; log.println(msg); - while (!pumpReady){ sleep(200); } + log.println(msg + " done"); } // Create a pump thread dedicated to saturate refuser's connection backlog @@ -520,28 +520,34 @@ public class AsyncCloseAndInterrupt { // Saturate the refuser's connection backlog so that further connection // attempts will be blocked + pumpReady = false; while (!pumpDone) { SocketChannel sc = SocketChannel.open(); sc.configureBlocking(false); boolean connected = sc.connect(refuser.socket().getLocalSocketAddress()); // Assume that the connection backlog is saturated if a - // client cannot connect to the refuser within 50 miliseconds + // client cannot connect to the refuser within 50 milliseconds long start = System.currentTimeMillis(); - while (!connected && (System.currentTimeMillis() - start < 50)) { + while (!pumpReady && !connected + && (System.currentTimeMillis() - start < 50)) { connected = sc.finishConnect(); } if (connected) { // Retain so that finalizer doesn't close refuserClients.add(sc); - pumpReady = false; } else { sc.close(); pumpReady = true; } } + for (SocketChannel sc : refuserClients) { + sc.close(); + } + refuser.close(); + log.println("Stop pumping refuser ..."); return refuserClients.size(); } @@ -565,8 +571,6 @@ public class AsyncCloseAndInterrupt { sleep(50); } while (!t.ready); - sleep(100); - switch (test) { case TEST_INTR: From 4b4f61a64affbf3afd42cdefbee801301b9c99a7 Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Mon, 21 Mar 2016 10:27:50 +0100 Subject: [PATCH 3/4] 8131913: jdk/internal/jline/console/StripAnsiTest.java can't run in the background Avoid using a real terminal in tests. Reviewed-by: rfield --- jdk/test/jdk/internal/jline/console/StripAnsiTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/jdk/test/jdk/internal/jline/console/StripAnsiTest.java b/jdk/test/jdk/internal/jline/console/StripAnsiTest.java index f9b03cecc23..46f919e0a12 100644 --- a/jdk/test/jdk/internal/jline/console/StripAnsiTest.java +++ b/jdk/test/jdk/internal/jline/console/StripAnsiTest.java @@ -23,14 +23,16 @@ /** * @test - * @bug 8080679 - * @modules jdk.internal.le/jdk.internal.jline.console + * @bug 8080679 8131913 + * @modules jdk.internal.le/jdk.internal.jline + * jdk.internal.le/jdk.internal.jline.console * @summary Verify ConsoleReader.stripAnsi strips escape sequences from its input correctly. */ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.lang.reflect.Method; +import jdk.internal.jline.UnsupportedTerminal; import jdk.internal.jline.console.ConsoleReader; public class StripAnsiTest { @@ -41,7 +43,7 @@ public class StripAnsiTest { void run() throws Exception { ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]); ByteArrayOutputStream out = new ByteArrayOutputStream(); - ConsoleReader reader = new ConsoleReader(in, out); + ConsoleReader reader = new ConsoleReader(in, out, new UnsupportedTerminal()); String withAnsi = "0\033[s1\033[2J2\033[37;4m3"; String expected = "0123"; From 6b7a41ca2bf049f3447ddc3a987fcef058631595 Mon Sep 17 00:00:00 2001 From: Naoto Sato Date: Mon, 21 Mar 2016 08:48:34 -0700 Subject: [PATCH 4/4] 8060097: sun/net/idn/TestStringPrep.java failed Reviewed-by: michaelm --- jdk/test/sun/net/idn/TestStringPrep.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/jdk/test/sun/net/idn/TestStringPrep.java b/jdk/test/sun/net/idn/TestStringPrep.java index 20b4f5d3659..984ecb8a785 100644 --- a/jdk/test/sun/net/idn/TestStringPrep.java +++ b/jdk/test/sun/net/idn/TestStringPrep.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 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 @@ -24,7 +24,7 @@ /* * @test * @summary Unit test for sun.net.idn.Punycode - * @bug 4737170 + * @bug 4737170 8060097 * @modules java.base/sun.net.idn java.base/sun.text.normalizer * @library . * @compile -XDignore.symbol.file TestStringPrep.java NFS4StringPrep.java @@ -41,6 +41,7 @@ import java.text.ParseException; import java.io.InputStream; +import java.util.Locale; import sun.net.idn.StringPrep; import sun.text.normalizer.UCharacterIterator; @@ -209,7 +210,7 @@ public class TestStringPrep { src = "THISISATEST"; byte[] dest = NFS4StringPrep.cs_prepare(src.getBytes("UTF-8"), false); String destStr = new String(dest, "UTF-8"); - if(!src.toLowerCase().equals(destStr)){ + if(!src.toLowerCase(Locale.ROOT).equals(destStr)){ fail("Did not get expected output. Expected: "+ prettify(src)+ " Got: " + prettify(destStr)); } @@ -274,7 +275,7 @@ public class TestStringPrep { private static String hex(char ch) { StringBuffer result = new StringBuffer(); - String foo = Integer.toString(ch,16).toUpperCase(); + String foo = Integer.toString(ch,16).toUpperCase(Locale.ROOT); for (int i = foo.length(); i < 4; ++i) { result.append('0'); }