test/jdk/java/nio/channels/DatagramChannel/ChangingAddress.java
author alanb
Sat, 30 Nov 2019 16:21:19 +0000
changeset 59329 289000934908
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234805: (dc) Remove JNI upcall from DatagramChannel.receive implementation Reviewed-by: dfuchs, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7045
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
     1
/*
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
     4
 *
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
     8
 *
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    13
 * accompanied this code).
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    14
 *
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    18
 *
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    21
 * questions.
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    22
 */
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    23
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    24
/* @test
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    25
 * @bug 6431343
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    26
 * @summary Test that DatagramChannel.getLocalAddress returns the right local
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    27
 *    address after connect/disconnect.
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    28
 */
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    29
import java.net.*;
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    30
import java.nio.channels.DatagramChannel;
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    31
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    32
public class ChangingAddress {
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    33
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    34
    // Checks that the given DatagramSocket and DatagramChannel are bound to the
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    35
    // same local address.
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    36
    static void check(DatagramSocket ds, DatagramChannel dc) {
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    37
        InetAddress expected = ds.getLocalAddress();
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    38
        InetAddress actual = dc.socket().getLocalAddress();
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    39
        // okay if one bound to 0.0.0.0 and the other to ::0
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    40
        if ((expected.isAnyLocalAddress() != actual.isAnyLocalAddress()) &&
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    41
            !expected.equals(actual))
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    42
        {
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    43
            throw new RuntimeException("Expected: " + expected + ", actual: " + actual);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    44
        }
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    45
    }
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    46
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    48
        InetAddress lh = InetAddress.getLocalHost();
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    49
        SocketAddress remote = new InetSocketAddress(lh, 1234);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    50
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    51
        DatagramSocket ds = null;
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    52
        DatagramChannel dc = null;
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    53
        try {
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    54
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    55
            ds = new DatagramSocket();
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    56
            dc = DatagramChannel.open().bind(new InetSocketAddress(0));
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    57
            check(ds, dc);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    58
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    59
            ds.connect(remote);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    60
            dc.connect(remote);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    61
            check(ds, dc);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    62
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    63
            ds.disconnect();
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    64
            dc.disconnect();
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    65
            check(ds, dc);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    66
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    67
            // repeat tests using socket adapter
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    68
            ds.connect(remote);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    69
            dc.socket().connect(remote);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    70
            check(ds, dc);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    71
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    72
            ds.disconnect();
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    73
            dc.socket().disconnect();
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    74
            check(ds, dc);
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    75
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    76
       } finally {
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    77
            if (ds != null) ds.close();
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    78
            if (dc != null) dc.close();
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    79
       }
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    80
    }
525f00c555b2 6431343: (dc) DatagramChannel may not report its local address correctly after connect or disconnect
alanb
parents:
diff changeset
    81
}