# HG changeset patch # User alanb # Date 1554564984 -3600 # Node ID f22d38b23756eff8fea48ddccf122036916eb2c2 # Parent b2c5d70eecf69a5243094849b23d4067911900cf Add -Djdk.net.usePlainSocketImpl to important tests diff -r b2c5d70eecf6 -r f22d38b23756 test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java --- a/test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java Fri Apr 05 12:07:38 2019 +0100 +++ b/test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java Sat Apr 06 16:36:24 2019 +0100 @@ -38,6 +38,7 @@ * jdk.test.lib.process.* * AcceptCauseFileDescriptorLeak * @run main/othervm AcceptCauseFileDescriptorLeak root + * @run main/othervm -Djdk.net.usePlainSocketImpl=true AcceptCauseFileDescriptorLeak root */ import java.io.IOException; diff -r b2c5d70eecf6 -r f22d38b23756 test/jdk/java/net/ServerSocket/UnreferencedSockets.java --- a/test/jdk/java/net/ServerSocket/UnreferencedSockets.java Fri Apr 05 12:07:38 2019 +0100 +++ b/test/jdk/java/net/ServerSocket/UnreferencedSockets.java Sat Apr 06 16:36:24 2019 +0100 @@ -26,6 +26,7 @@ * @modules java.management java.base/java.io:+open java.base/java.net:+open * @run main/othervm UnreferencedSockets * @run main/othervm -Djava.net.preferIPv4Stack=true UnreferencedSockets + * @run main/othervm -Djdk.net.usePlainSocketImpl UnreferencedSockets * @summary Check that unreferenced sockets are closed */ diff -r b2c5d70eecf6 -r f22d38b23756 test/jdk/java/net/Socket/ConnectionReset.java --- a/test/jdk/java/net/Socket/ConnectionReset.java Fri Apr 05 12:07:38 2019 +0100 +++ b/test/jdk/java/net/Socket/ConnectionReset.java Sat Apr 06 16:36:24 2019 +0100 @@ -25,6 +25,7 @@ * @test * @requires os.family != "solaris" * @run testng ConnectionReset + * @run testng/othervm -Djdk.net.usePlainSocketImpl ConnectionReset * @summary Test behavior of read and available when a connection is reset */ diff -r b2c5d70eecf6 -r f22d38b23756 test/jdk/java/net/Socket/Timeouts.java --- a/test/jdk/java/net/Socket/Timeouts.java Fri Apr 05 12:07:38 2019 +0100 +++ b/test/jdk/java/net/Socket/Timeouts.java Sat Apr 06 16:36:24 2019 +0100 @@ -25,7 +25,7 @@ * @test * @library /test/lib * @build jdk.test.lib.Utils - * @run testng Timeouts + * @run testng/timeout=180 Timeouts * @summary Test Socket timeouts */ @@ -131,10 +131,14 @@ public void testTimedRead3() throws IOException { withConnection((s1, s2) -> { s2.setSoTimeout(2000); + long start = System.currentTimeMillis(); try { s2.getInputStream().read(); assertTrue(false); - } catch (SocketTimeoutException expected) { } + } catch (SocketTimeoutException expected) { + int timeout = s2.getSoTimeout(); + checkDuration(start, timeout-100, timeout+2000); + } }); } @@ -312,11 +316,15 @@ public void testTimedAccept3() throws IOException { try (ServerSocket ss = new ServerSocket(0)) { ss.setSoTimeout(2000); + long start = System.currentTimeMillis(); try { Socket s = ss.accept(); s.close(); assertTrue(false); - } catch (SocketTimeoutException expected) { } + } catch (SocketTimeoutException expected) { + int timeout = ss.getSoTimeout(); + checkDuration(start, timeout-100, timeout+2000); + } } } @@ -386,11 +394,15 @@ public void testTimedAccept7() throws IOException { try (ServerSocket ss = new ServerSocket(0)) { ss.setSoTimeout(30*1000); - scheduleClose(ss, 2000); + long delay = 2000; + scheduleClose(ss, delay); + long start = System.currentTimeMillis(); try { ss.accept().close(); assertTrue(false); - } catch (SocketException expected) { } + } catch (SocketException expected) { + checkDuration(start, delay-100, delay+2000); + } } } @@ -407,8 +419,9 @@ s.close(); assertTrue(false); } catch (SocketTimeoutException expected) { - // accept should have blocked for 2000ms - assertTrue((System.currentTimeMillis() - start) > 1800); + // accept should have blocked for 2 seconds + int timeout = ss.getSoTimeout(); + checkDuration(start, timeout-100, timeout+2000); assertTrue(Thread.currentThread().isInterrupted()); } finally { Thread.interrupted(); // clear interrupt status @@ -422,16 +435,17 @@ public void testTimedAccept9() throws IOException { try (ServerSocket ss = new ServerSocket(0)) { ss.setSoTimeout(4000); - // interrupt thread after 1000ms + // interrupt thread after 1 second Future interrupter = scheduleInterrupt(Thread.currentThread(), 1000); long start = System.currentTimeMillis(); try { - Socket s = ss.accept(); // should block for 4000 + Socket s = ss.accept(); // should block for 4 seconds s.close(); assertTrue(false); } catch (SocketTimeoutException expected) { - // accept should have blocked for 4000ms - assertTrue((System.currentTimeMillis() - start) > 3800); + // accept should have blocked for 4 seconds + int timeout = ss.getSoTimeout(); + checkDuration(start, timeout-100, timeout+2000); assertTrue(Thread.currentThread().isInterrupted()); } finally { interrupter.cancel(true); @@ -459,8 +473,9 @@ e = expectThrows(ExecutionException.class, result2::get); assertTrue(e.getCause() instanceof SocketTimeoutException); - // both tasks should completed in a little over 4000ms - assertTrue((System.currentTimeMillis() - start) < 5000); + // should get here in 4 seconds, not 8 seconds + int timeout = ss.getSoTimeout(); + checkDuration(start, timeout-100, timeout+2000); } finally { pool.shutdown(); } @@ -479,7 +494,8 @@ Future result1 = pool.submit(ss::accept); Future result2 = pool.submit(ss::accept); - scheduleConnect(ss.getLocalSocketAddress(), 1500); + // establish connection after 3 seconds + scheduleConnect(ss.getLocalSocketAddress(), 3000); // one task should have accepted the connection, the other should // have completed with SocketTimeoutException @@ -499,9 +515,9 @@ } assertTrue((s1 != null) ^ (s2 != null)); - // both tasks should completed in a little over 4000ms - long duration = System.currentTimeMillis() - start; - assertTrue(duration > 3800 && duration < 5000); + // should get here in 4 seconds, not 7 seconds + int timeout = ss.getSoTimeout(); + checkDuration(start, timeout-100, timeout+2000); } finally { pool.shutdown(); } @@ -613,4 +629,17 @@ executor.shutdown(); } } + + /** + * Check the duration of a task + * @param start start time, in milliseconds + * @param min minimum expected duration, in milliseconds + * @param max maximum expected duration, in milliseconds + * @return the duration (now - start), in milliseconds + */ + private static long checkDuration(long start, long min, long max) { + long duration = System.currentTimeMillis() - start; + assertTrue(duration >= min && duration <= max); + return duration; + } } diff -r b2c5d70eecf6 -r f22d38b23756 test/jdk/java/net/Socket/UdpSocket.java --- a/test/jdk/java/net/Socket/UdpSocket.java Fri Apr 05 12:07:38 2019 +0100 +++ b/test/jdk/java/net/Socket/UdpSocket.java Sat Apr 06 16:36:24 2019 +0100 @@ -23,7 +23,7 @@ /** * @test - * @run main UdpSocket + * @run testng/othervm -Dsun.net.maxDatagramSockets=32 UdpSocket * @summary Basic test for a Socket to a UDP socket */ @@ -34,13 +34,23 @@ import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; +import java.security.Permission; import java.util.Arrays; +import java.util.ArrayList; +import java.util.List; +import org.testng.annotations.Test; +import static org.testng.Assert.*; + +@Test public class UdpSocket { - static final String MESSAGE = "hello"; + /** + * Test using the Socket API to send/receive datagrams + */ + public void testSendReceive() throws IOException { + final String MESSAGE = "hello"; - public static void main(String[] args) throws IOException { try (DatagramChannel dc = DatagramChannel.open()) { var loopback = InetAddress.getLoopbackAddress(); dc.bind(new InetSocketAddress(loopback, 0)); @@ -56,8 +66,7 @@ var buf = ByteBuffer.allocate(100); SocketAddress remote = dc.receive(buf); buf.flip(); - if (buf.remaining() != MESSAGE.length()) - throw new RuntimeException("Unexpected size"); + assertTrue(buf.remaining() == MESSAGE.length(), "Unexpected size"); // echo the datagram dc.send(buf, remote); @@ -65,11 +74,58 @@ // receive datagram with the socket input stream byte[] array2 = new byte[100]; int n = s.getInputStream().read(array2); - if (n != MESSAGE.length()) - throw new RuntimeException("Unexpected size"); - if (!Arrays.equals(array1, 0, n, array2, 0, n)) - throw new RuntimeException("Unexpected contents"); + assertTrue(n == MESSAGE.length(), "Unexpected size"); + assertTrue(Arrays.equals(array1, 0, n, array2, 0, n), "Unexpected contents"); } } } + + /** + * Test that the number of UDP sockets is limited when running with a + * security manager. + */ + public void testMaxSockets() throws IOException { + int limit = Integer.getInteger("sun.net.maxDatagramSockets"); + + // security manager grants all permissions + var securityManager = new SecurityManager() { + @Override public void checkPermission(Permission perm) { } + }; + + System.setSecurityManager(securityManager); + List sockets = new ArrayList<>(); + try { + // create the maximum number of sockets + for (int i=0; i sockets) throws IOException { + for (Socket s : sockets) { + s.close(); + } + } } diff -r b2c5d70eecf6 -r f22d38b23756 test/jdk/java/net/Socket/asyncClose/AsyncClose.java --- a/test/jdk/java/net/Socket/asyncClose/AsyncClose.java Fri Apr 05 12:07:38 2019 +0100 +++ b/test/jdk/java/net/Socket/asyncClose/AsyncClose.java Sat Apr 06 16:36:24 2019 +0100 @@ -33,6 +33,7 @@ * cause any thread blocked on the socket to throw a SocketException. * @run main AsyncClose * @run main/othervm -Djava.net.preferIPv4Stack=true AsyncClose + * @run main/othervm -Djdk.net.usePlainSocketImpl AsyncClose */ public class AsyncClose { diff -r b2c5d70eecf6 -r f22d38b23756 test/jdk/java/net/ipv6tests/TcpTest.java --- a/test/jdk/java/net/ipv6tests/TcpTest.java Fri Apr 05 12:07:38 2019 +0100 +++ b/test/jdk/java/net/ipv6tests/TcpTest.java Sat Apr 06 16:36:24 2019 +0100 @@ -29,7 +29,8 @@ * @library /test/lib * @build jdk.test.lib.NetworkConfiguration * jdk.test.lib.Platform - * @run main TcpTest -d + * @run main TcpTest + * @run main/othervm -Djdk.net.usePlainSocketImpl TcpTest */ import java.net.*;