test/jdk/java/nio/channels/DatagramChannel/SelectWhenRefused.java
author bpb
Mon, 30 Apr 2018 13:40:39 -0700
changeset 49930 3aaaa5370999
parent 47216 71c04702a3d5
permissions -rw-r--r--
8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13 Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5777
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10361
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
5777
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
     4
 *
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
     8
 *
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    13
 * accompanied this code).
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    14
 *
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    18
 *
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    21
 * questions.
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    22
 */
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    23
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    24
/* @test
10361
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    25
 * @bug 6935563 7044870
5777
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    26
 * @summary Test that Selector does not select an unconnected DatagramChannel when
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    27
 *    ICMP port unreachable received
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    28
 */
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    29
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    30
import java.nio.ByteBuffer;
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    31
import java.nio.channels.*;
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    32
import java.net.*;
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    33
import java.io.IOException;
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    34
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    35
public class SelectWhenRefused {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    36
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    37
    public static void main(String[] args) throws IOException {
10361
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    38
        DatagramChannel dc1 = DatagramChannel.open().bind(new InetSocketAddress(0));
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    39
        int port = dc1.socket().getLocalPort();
5777
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    40
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    41
        // datagram sent to this address should be refused
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    42
        SocketAddress refuser = new InetSocketAddress(InetAddress.getLocalHost(), port);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    43
10361
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    44
        DatagramChannel dc = DatagramChannel.open().bind(new InetSocketAddress(0));
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    45
        dc1.close();
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    46
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5777
diff changeset
    47
        Selector sel = Selector.open();
5777
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    48
        try {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    49
            dc.configureBlocking(false);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    50
            dc.register(sel, SelectionKey.OP_READ);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    51
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    52
            /* Test 1: not connected so ICMP port unreachable should not be received */
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    53
            sendDatagram(dc, refuser);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    54
            int n = sel.select(2000);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    55
            if (n > 0) {
10361
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    56
                sel.selectedKeys().clear();
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    57
                // BindException will be thrown if another service is using
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    58
                // our expected refuser port, cannot run just exit.
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    59
                DatagramChannel.open().bind(refuser).close();
5777
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    60
                throw new RuntimeException("Unexpected wakeup");
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    61
            }
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    62
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    63
            /* Test 2: connected so ICMP port unreachable may be received */
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    64
            dc.connect(refuser);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    65
            try {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    66
                sendDatagram(dc, refuser);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    67
                n = sel.select(2000);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    68
                if (n > 0) {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    69
                    sel.selectedKeys().clear();
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    70
                    try {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    71
                        n = dc.read(ByteBuffer.allocate(100));
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    72
                        throw new RuntimeException("Unexpected datagram received");
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    73
                    } catch (PortUnreachableException pue) {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    74
                        // expected
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    75
                    }
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    76
                }
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    77
            } finally {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    78
                dc.disconnect();
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    79
            }
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    80
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    81
            /* Test 3: not connected so ICMP port unreachable should not be received */
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    82
            sendDatagram(dc, refuser);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    83
            n = sel.select(2000);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    84
            if (n > 0) {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    85
                throw new RuntimeException("Unexpected wakeup after disconnect");
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    86
            }
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    87
10361
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    88
        } catch(BindException e) {
b089baaa1d3e 7044870: java/nio/channels/DatagramChannel/SelectWhenRefused.java failed on SUSE Linux 10
chegar
parents: 5970
diff changeset
    89
            // Do nothing, some other test has used this port
5777
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    90
        } finally {
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5777
diff changeset
    91
            sel.close();
5777
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    92
            dc.close();
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    93
        }
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    94
    }
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    95
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    96
    static void sendDatagram(DatagramChannel dc, SocketAddress remote)
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    97
        throws IOException
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    98
    {
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
    99
        ByteBuffer bb = ByteBuffer.wrap("Greetings!".getBytes());
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
   100
        dc.send(bb, remote);
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
   101
    }
1c86018749bd 6935563: (dc) Improve connection reset/port unreachable handling [win]
alanb
parents:
diff changeset
   102
}