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