test/jdk/java/nio/channels/DatagramChannel/AddressesAfterDisconnect.java
author dfuchs
Wed, 09 Oct 2019 17:38:58 +0100
changeset 58518 705c3f88a409
permissions -rw-r--r--
8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx) Summary: DatagramChannel::disconnect will attempt to rebind to the original port if the local port switches back to 0 after the association is disolved by the system. Reviewed-by: alanb, chegar, fweimer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58518
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
     1
/*
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
     4
 *
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
     8
 *
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    14
 *
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    18
 *
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    21
 * questions.
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    22
 */
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    23
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    24
/* @test
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    25
 * @library /test/lib
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    26
 * @summary Test DatagramChannel local address after disconnect.
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    27
 * @requires (os.family != "mac")
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    28
 * @run testng/othervm AddressesAfterDisconnect
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    29
 * @run testng/othervm -Djava.net.preferIPv6Addresses=true AddressesAfterDisconnect
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    30
 * @run testng/othervm -Djava.net.preferIPv4Stack=true AddressesAfterDisconnect
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    31
 */
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    32
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    33
import jdk.test.lib.net.IPSupport;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    34
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    35
import java.io.IOException;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    36
import java.net.InetAddress;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    37
import java.net.InetSocketAddress;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    38
import java.net.SocketAddress;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    39
import java.net.StandardProtocolFamily;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    40
import java.nio.channels.DatagramChannel;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    41
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    42
import org.testng.annotations.Test;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    43
import static org.testng.Assert.assertEquals;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    44
import static org.testng.Assert.assertTrue;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    45
import static org.testng.Assert.assertFalse;
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    46
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    47
public class AddressesAfterDisconnect {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    48
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    49
    public static void main(String[] args) throws IOException {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    50
        new AddressesAfterDisconnect().execute();
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    51
    }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    52
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    53
    @Test
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    54
    public void execute() throws IOException {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    55
        IPSupport.throwSkippedExceptionIfNonOperational();
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    56
        boolean preferIPv6 = Boolean.getBoolean("java.net.preferIPv6Addresses");
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    57
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    58
        // test with default protocol family
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    59
        try (DatagramChannel dc = DatagramChannel.open()) {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    60
            System.out.println("Test with default");
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    61
            dc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    62
            test(dc);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    63
            test(dc);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    64
        }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    65
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    66
        if (IPSupport.hasIPv6()) {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    67
            // test with IPv6 only
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    68
            System.out.println("Test with IPv6 only");
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    69
            try (DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET6)) {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    70
                dc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    71
                test(dc);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    72
                test(dc);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    73
            }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    74
        }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    75
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    76
        if (IPSupport.hasIPv4() && !preferIPv6) {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    77
            // test with IPv4 only
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    78
            System.out.println("Test with IPv4 only");
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    79
            try (DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET)) {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    80
                dc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    81
                test(dc);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    82
                test(dc);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    83
            }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    84
        }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    85
    }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    86
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    87
    /**
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    88
     * Connect DatagramChannel to a server, write a datagram and disconnect. Invoke
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    89
     * a second or subsequent time with the same DatagramChannel instance to check
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    90
     * that disconnect works as expected.
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    91
     */
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    92
    static void test(DatagramChannel dc) throws IOException {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    93
        SocketAddress local = dc.getLocalAddress();
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    94
        try (DatagramChannel server = DatagramChannel.open()) {
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    95
            server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    96
            SocketAddress remote = server.getLocalAddress();
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    97
            dc.connect(remote);
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    98
            assertTrue(dc.isConnected());
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
    99
            // comment the following two lines on OS X to see JDK-8231259
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
   100
            assertEquals(dc.getLocalAddress(), local, "local address after connect");
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
   101
            assertEquals(dc.getRemoteAddress(), remote, "remote address after connect");
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
   102
            dc.disconnect();
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
   103
            assertFalse(dc.isConnected());
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
   104
            assertEquals(dc.getLocalAddress(), local, "local address after disconnect");
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
   105
            assertEquals(dc.getRemoteAddress(), null, "remote address after disconnect");
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
   106
        }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
   107
    }
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
   108
705c3f88a409 8231260: (dc) DatagramChannel::disconnect changes the port of the local address to 0 (lnx)
dfuchs
parents:
diff changeset
   109
}