test/jdk/java/net/Socket/UdpSocket.java
author alanb
Thu, 09 May 2019 17:49:28 +0100
branchniosocketimpl-branch
changeset 57355 ceb5c3fd71d2
parent 57341 733e9746d615
child 58679 9c3209ff7550
permissions -rw-r--r--
Address review comments
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
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
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;
57309
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
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;
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
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;
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    40
import java.util.ArrayList;
57309
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
    41
import java.util.ArrayDeque;
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
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
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    44
import org.testng.annotations.Test;
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    45
import static org.testng.Assert.*;
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    46
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
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
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    50
    /**
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    51
     * Test using the Socket API to send/receive datagrams
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    52
     */
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    53
    public void testSendReceive() throws IOException {
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
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();
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
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);
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    79
                assertTrue(n == MESSAGE.length(), "Unexpected size");
57355
ceb5c3fd71d2 Address review comments
alanb
parents: 57341
diff changeset
    80
                assertEquals(Arrays.copyOf(array1, n), Arrays.copyOf(array2, n),
ceb5c3fd71d2 Address review comments
alanb
parents: 57341
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
    }
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    85
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    86
    /**
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    87
     * Test that the number of UDP sockets is limited when running with a
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    88
     * security manager.
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    89
     */
57309
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
    90
    public void testMaxSockets() throws Exception {
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    91
        int limit = Integer.getInteger("sun.net.maxDatagramSockets");
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    92
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    93
        // security manager grants all permissions
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    94
        var securityManager = new SecurityManager() {
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    95
            @Override public void checkPermission(Permission perm) { }
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    96
        };
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    97
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
    98
        System.setSecurityManager(securityManager);
57309
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
    99
        Deque<Socket> sockets = new ArrayDeque<>();
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   100
        try {
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   101
            // create the maximum number of sockets
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   102
            for (int i=0; i<limit; i++) {
57309
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   103
                sockets.offer(newUdpSocket());
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   104
            }
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   105
57309
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   106
            // try to create another socket - should fail
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   107
            try {
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   108
                Socket s = newUdpSocket();
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   109
                s.close();
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   110
                assertTrue(false);
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   111
            } catch (IOException expected) { }
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   112
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   113
            // close one socket
57309
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   114
            sockets.pop().close();
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   115
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   116
            // try to create another socket - should succeed
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   117
            Socket s = newUdpSocket();
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   118
57309
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   119
            // unreference the socket and wait for it to be closed by the cleaner
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   120
            var ref = new WeakReference<>(s);
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   121
            s = null;
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   122
            while (ref.get() != null) {
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   123
                System.gc();
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   124
                Thread.sleep(100);
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   125
            }
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   126
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   127
            // try to create another socket - should succeed
dbd1beb994ab Extend test coverage of UDP sockets
alanb
parents: 57308
diff changeset
   128
            s = newUdpSocket();
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   129
            s.close();
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   130
        } finally {
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   131
            closeAll(sockets);
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   132
            System.setSecurityManager(null);
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   133
        }
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   134
    }
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   135
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   136
    private Socket newUdpSocket() throws IOException {
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   137
        return new Socket(InetAddress.getLoopbackAddress(), 8000, false);
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   138
    }
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   139
57341
733e9746d615 More cleanup and test improvements
alanb
parents: 57313
diff changeset
   140
    private void closeAll(Deque<Socket> sockets) throws IOException {
57313
14b02c7b27b8 More test updates
alanb
parents: 57309
diff changeset
   141
        Socket s;
14b02c7b27b8 More test updates
alanb
parents: 57309
diff changeset
   142
        while ((s = sockets.poll()) != null) {
14b02c7b27b8 More test updates
alanb
parents: 57309
diff changeset
   143
            s.close();
14b02c7b27b8 More test updates
alanb
parents: 57309
diff changeset
   144
        }
57308
f22d38b23756 Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents: 57281
diff changeset
   145
    }
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   146
}