test/jdk/jdk/net/RdmaSockets/rsocket/NullBind.java
author bpb
Thu, 07 Feb 2019 11:09:09 -0800
branchrsocket-branch
changeset 57160 c502c299d41e
parent 57156 81e4a12fd1a4
permissions -rw-r--r--
rsocket-branch: correct copyright year
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
57160
c502c299d41e rsocket-branch: correct copyright year
bpb
parents: 57156
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
 * @test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
 * @bug 8195160
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
 * @summary Tests binding with null ( automatically assigned address )
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
 * @requires (os.family == "linux")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
 * @library /test/lib
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    30
 * @build RsocketTest
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    31
 * @run testng/othervm NullBind
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
 * @run testng/othervm -Djava.net.preferIPv6Addresses=true NullBind
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    33
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
import java.io.IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
import java.net.Inet4Address;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
import java.net.Inet6Address;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
import java.net.InetAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
import java.net.InetSocketAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
import java.net.ProtocolFamily;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
import java.net.ServerSocket;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
import java.net.Socket;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
import java.nio.channels.ServerSocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
import java.nio.channels.SocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
import org.testng.SkipException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
import org.testng.annotations.BeforeTest;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
import org.testng.annotations.DataProvider;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
import org.testng.annotations.Test;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
import static java.lang.String.format;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
import static java.net.StandardProtocolFamily.INET;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
import static java.net.StandardProtocolFamily.INET6;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
import static jdk.net.RdmaSockets.*;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
import static org.testng.Assert.assertTrue;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
public class NullBind {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
    @BeforeTest
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
    public void setup() throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
        if (!RsocketTest.isRsocketAvailable())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
            throw new SkipException("rsocket is not available");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
    @DataProvider(name = "families")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
    public Object[][] families() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
        return new Object[][] {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
            { INET,  Inet4Address.class },
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
            { INET6, Inet6Address.class }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
        };
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
    @Test(dataProvider = "families")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
    public void testSocket(ProtocolFamily family,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
                           Class<? extends InetAddress> expectedClass)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
        throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
        try (Socket s = openSocket(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
            s.bind(null);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
            InetAddress addr = s.getLocalAddress();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
            //TODO: Argh! still a family issue in Socket.getLocalAddress
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
            //assertTrue(expectedClass.isAssignableFrom(addr.getClass()),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
            //           format("Excepted %s got: %s", expectedClass, addr));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
            assertTrue(addr.isAnyLocalAddress(),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
                       format("Expected any local address, got: %s", addr));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
    @Test(dataProvider = "families")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
    public void testServerSocket(ProtocolFamily family,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
                                 Class<? extends InetAddress> expectedClass)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
        throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
        try (ServerSocket ss = openServerSocket(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
            ss.bind(null);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    94
            InetAddress addr = ss.getInetAddress();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
            assertTrue(expectedClass.isAssignableFrom(addr.getClass()),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
                       format("Excepted %s got: %s", expectedClass, addr));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
            assertTrue(addr.isAnyLocalAddress(),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
                       format("Expected any local address, got: %s", addr));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
    @Test(dataProvider = "families")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
    public void testSocketChannel(ProtocolFamily family,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
                                  Class<? extends InetAddress> expectedClass)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
        throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
        try (SocketChannel sc = openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
            sc.bind(null);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
            InetAddress addr = ((InetSocketAddress)sc.getLocalAddress()).getAddress();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   110
            assertTrue(expectedClass.isAssignableFrom(addr.getClass()),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   111
                      format("Excepted %s got: %s", expectedClass, addr));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   112
            assertTrue(addr.isAnyLocalAddress(),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   113
                       format("Expected any local address, got: %s", addr));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   114
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   115
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   116
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   117
    @Test(dataProvider = "families")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   118
    public void testServerSocketChannel(ProtocolFamily family,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   119
                                        Class<? extends InetAddress> expectedClass)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   120
        throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   121
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   122
        try (ServerSocketChannel ssc = openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   123
            ssc.bind(null);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   124
            InetAddress addr = ((InetSocketAddress)ssc.getLocalAddress()).getAddress();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   125
            assertTrue(expectedClass.isAssignableFrom(addr.getClass()),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   126
                       format("Excepted %s got: %s", expectedClass, addr));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   127
            assertTrue(addr.isAnyLocalAddress(),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   128
                       format("Expected any local address, got: %s", addr));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   129
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   130
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   131
}