test/jdk/java/net/Socket/UdpSocket.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 57355 ceb5c3fd71d2
parent 55102 59567035d279
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     1
/*
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     4
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     8
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    13
 * accompanied this code).
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    14
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    18
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    21
 * questions.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    22
 */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    23
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    24
/**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    25
 * @test
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    26
 * @run testng/othervm -Dsun.net.maxDatagramSockets=32 UdpSocket
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    27
 * @summary Basic test for a Socket to a UDP socket
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    28
 */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    29
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    30
import java.io.IOException;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    31
import java.lang.ref.WeakReference;
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    32
import java.net.InetAddress;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    33
import java.net.InetSocketAddress;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    34
import java.net.Socket;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    35
import java.net.SocketAddress;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    36
import java.nio.ByteBuffer;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    37
import java.nio.channels.DatagramChannel;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    38
import java.security.Permission;
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    39
import java.util.Arrays;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    40
import java.util.ArrayList;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    41
import java.util.ArrayDeque;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    42
import java.util.Deque;
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    43
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    44
import org.testng.annotations.Test;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    45
import static org.testng.Assert.*;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    46
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    47
@Test
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    48
public class UdpSocket {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    49
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    50
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    51
     * Test using the Socket API to send/receive datagrams
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    52
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    53
    public void testSendReceive() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    54
        final String MESSAGE = "hello";
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    55
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    56
        try (DatagramChannel dc = DatagramChannel.open()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    57
            var loopback = InetAddress.getLoopbackAddress();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    58
            dc.bind(new InetSocketAddress(loopback, 0));
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    59
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    60
            int port = ((InetSocketAddress) dc.getLocalAddress()).getPort();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    61
            try (Socket s = new Socket(loopback, port, false)) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    62
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    63
                // send datagram with socket output stream
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    64
                byte[] array1 = MESSAGE.getBytes("UTF-8");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    65
                s.getOutputStream().write(array1);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    66
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    67
                // receive the datagram
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    68
                var buf = ByteBuffer.allocate(100);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    69
                SocketAddress remote = dc.receive(buf);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    70
                buf.flip();
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    71
                assertTrue(buf.remaining() == MESSAGE.length(), "Unexpected size");
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    72
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    73
                // echo the datagram
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    74
                dc.send(buf, remote);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    75
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    76
                // receive datagram with the socket input stream
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    77
                byte[] array2 = new byte[100];
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    78
                int n = s.getInputStream().read(array2);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    79
                assertTrue(n == MESSAGE.length(), "Unexpected size");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    80
                assertEquals(Arrays.copyOf(array1, n), Arrays.copyOf(array2, n),
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    81
                            "Unexpected contents");
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    82
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    83
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    84
    }
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    85
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    86
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    87
     * Test that the number of UDP sockets is limited when running with a
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    88
     * security manager.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    89
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    90
    public void testMaxSockets() throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    91
        int limit = Integer.getInteger("sun.net.maxDatagramSockets");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    92
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    93
        // security manager grants all permissions
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    94
        var securityManager = new SecurityManager() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    95
            @Override public void checkPermission(Permission perm) { }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    96
        };
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    97
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    98
        System.setSecurityManager(securityManager);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
    99
        Deque<Socket> sockets = new ArrayDeque<>();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   100
        try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   101
            // create the maximum number of sockets
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   102
            for (int i=0; i<limit; i++) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   103
                sockets.offer(newUdpSocket());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   104
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   105
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   106
            // try to create another socket - should fail
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   107
            try {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   108
                Socket s = newUdpSocket();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   109
                s.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   110
                assertTrue(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   111
            } catch (IOException expected) { }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   112
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   113
            // close one socket
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   114
            sockets.pop().close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   115
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   116
            // try to create another socket - should succeed
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   117
            Socket s = newUdpSocket();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   118
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   119
            // unreference the socket and wait for it to be closed by the cleaner
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   120
            var ref = new WeakReference<>(s);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   121
            s = null;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   122
            while (ref.get() != null) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   123
                System.gc();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   124
                Thread.sleep(100);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   125
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   126
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   127
            // try to create another socket - should succeed
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   128
            s = newUdpSocket();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   129
            s.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   130
        } finally {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   131
            closeAll(sockets);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   132
            System.setSecurityManager(null);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   133
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   134
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   135
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   136
    private Socket newUdpSocket() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   137
        return new Socket(InetAddress.getLoopbackAddress(), 8000, false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   138
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   139
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   140
    private void closeAll(Deque<Socket> sockets) throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   141
        Socket s;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   142
        while ((s = sockets.poll()) != null) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   143
            s.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   144
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 54216
diff changeset
   145
    }
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   146
}