test/jdk/java/net/Socket/UdpSocket.java
branchniosocketimpl-branch
changeset 57308 f22d38b23756
parent 57281 c08d024d6bf9
child 57309 dbd1beb994ab
equal deleted inserted replaced
57305:b2c5d70eecf6 57308:f22d38b23756
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @run main UdpSocket
    26  * @run testng/othervm -Dsun.net.maxDatagramSockets=32 UdpSocket
    27  * @summary Basic test for a Socket to a UDP socket
    27  * @summary Basic test for a Socket to a UDP socket
    28  */
    28  */
    29 
    29 
    30 import java.io.IOException;
    30 import java.io.IOException;
    31 import java.net.InetAddress;
    31 import java.net.InetAddress;
    32 import java.net.InetSocketAddress;
    32 import java.net.InetSocketAddress;
    33 import java.net.Socket;
    33 import java.net.Socket;
    34 import java.net.SocketAddress;
    34 import java.net.SocketAddress;
    35 import java.nio.ByteBuffer;
    35 import java.nio.ByteBuffer;
    36 import java.nio.channels.DatagramChannel;
    36 import java.nio.channels.DatagramChannel;
       
    37 import java.security.Permission;
    37 import java.util.Arrays;
    38 import java.util.Arrays;
       
    39 import java.util.ArrayList;
       
    40 import java.util.List;
    38 
    41 
       
    42 import org.testng.annotations.Test;
       
    43 import static org.testng.Assert.*;
       
    44 
       
    45 @Test
    39 public class UdpSocket {
    46 public class UdpSocket {
    40 
    47 
    41     static final String MESSAGE = "hello";
    48     /**
       
    49      * Test using the Socket API to send/receive datagrams
       
    50      */
       
    51     public void testSendReceive() throws IOException {
       
    52         final String MESSAGE = "hello";
    42 
    53 
    43     public static void main(String[] args) throws IOException {
       
    44         try (DatagramChannel dc = DatagramChannel.open()) {
    54         try (DatagramChannel dc = DatagramChannel.open()) {
    45             var loopback = InetAddress.getLoopbackAddress();
    55             var loopback = InetAddress.getLoopbackAddress();
    46             dc.bind(new InetSocketAddress(loopback, 0));
    56             dc.bind(new InetSocketAddress(loopback, 0));
    47 
    57 
    48             int port = ((InetSocketAddress) dc.getLocalAddress()).getPort();
    58             int port = ((InetSocketAddress) dc.getLocalAddress()).getPort();
    54 
    64 
    55                 // receive the datagram
    65                 // receive the datagram
    56                 var buf = ByteBuffer.allocate(100);
    66                 var buf = ByteBuffer.allocate(100);
    57                 SocketAddress remote = dc.receive(buf);
    67                 SocketAddress remote = dc.receive(buf);
    58                 buf.flip();
    68                 buf.flip();
    59                 if (buf.remaining() != MESSAGE.length())
    69                 assertTrue(buf.remaining() == MESSAGE.length(), "Unexpected size");
    60                     throw new RuntimeException("Unexpected size");
       
    61 
    70 
    62                 // echo the datagram
    71                 // echo the datagram
    63                 dc.send(buf, remote);
    72                 dc.send(buf, remote);
    64 
    73 
    65                 // receive datagram with the socket input stream
    74                 // receive datagram with the socket input stream
    66                 byte[] array2 = new byte[100];
    75                 byte[] array2 = new byte[100];
    67                 int n = s.getInputStream().read(array2);
    76                 int n = s.getInputStream().read(array2);
    68                 if (n != MESSAGE.length())
    77                 assertTrue(n == MESSAGE.length(), "Unexpected size");
    69                     throw new RuntimeException("Unexpected size");
    78                 assertTrue(Arrays.equals(array1, 0, n, array2, 0, n), "Unexpected contents");
    70                 if (!Arrays.equals(array1, 0, n, array2, 0, n))
       
    71                     throw new RuntimeException("Unexpected contents");
       
    72             }
    79             }
    73         }
    80         }
    74     }
    81     }
       
    82 
       
    83     /**
       
    84      * Test that the number of UDP sockets is limited when running with a
       
    85      * security manager.
       
    86      */
       
    87     public void testMaxSockets() throws IOException {
       
    88         int limit = Integer.getInteger("sun.net.maxDatagramSockets");
       
    89 
       
    90         // security manager grants all permissions
       
    91         var securityManager = new SecurityManager() {
       
    92             @Override public void checkPermission(Permission perm) { }
       
    93         };
       
    94 
       
    95         System.setSecurityManager(securityManager);
       
    96         List<Socket> sockets = new ArrayList<>();
       
    97         try {
       
    98             // create the maximum number of sockets
       
    99             for (int i=0; i<limit; i++) {
       
   100                 sockets.add(newUdpSocket());
       
   101             }
       
   102 
       
   103             // try to create another socket
       
   104             try {
       
   105                 Socket s = newUdpSocket();
       
   106                 s.close();
       
   107                 assertTrue(false);
       
   108             } catch (IOException expected) { }
       
   109 
       
   110             // close one socket
       
   111             sockets.get(0).close();
       
   112 
       
   113             // create another socket
       
   114             Socket s = newUdpSocket();
       
   115             s.close();
       
   116         } finally {
       
   117             closeAll(sockets);
       
   118             System.setSecurityManager(null);
       
   119         }
       
   120     }
       
   121 
       
   122     private Socket newUdpSocket() throws IOException {
       
   123         return new Socket(InetAddress.getLoopbackAddress(), 8000, false);
       
   124     }
       
   125 
       
   126     private void closeAll(List<Socket> sockets) throws IOException {
       
   127         for (Socket s : sockets) {
       
   128             s.close();
       
   129         }
       
   130     }
    75 }
   131 }