--- 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<Socket> sockets = new ArrayList<>();
+ try {
+ // create the maximum number of sockets
+ for (int i=0; i<limit; i++) {
+ sockets.add(newUdpSocket());
+ }
+
+ // try to create another socket
+ try {
+ Socket s = newUdpSocket();
+ s.close();
+ assertTrue(false);
+ } catch (IOException expected) { }
+
+ // close one socket
+ sockets.get(0).close();
+
+ // create another socket
+ Socket s = newUdpSocket();
+ s.close();
+ } finally {
+ closeAll(sockets);
+ System.setSecurityManager(null);
+ }
+ }
+
+ private Socket newUdpSocket() throws IOException {
+ return new Socket(InetAddress.getLoopbackAddress(), 8000, false);
+ }
+
+ private void closeAll(List<Socket> sockets) throws IOException {
+ for (Socket s : sockets) {
+ s.close();
+ }
+ }
}