1/*
2 * Copyright (c) 2010, 2011, 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 * @bug 6935563 7044870
26 * @summary Test that Selector does not select an unconnected DatagramChannel when
27 *    ICMP port unreachable received
28 */
29
30import java.nio.ByteBuffer;
31import java.nio.channels.*;
32import java.net.*;
33import java.io.IOException;
34
35public class SelectWhenRefused {
36
37    public static void main(String[] args) throws IOException {
38        DatagramChannel dc1 = DatagramChannel.open().bind(new InetSocketAddress(0));
39        int port = dc1.socket().getLocalPort();
40
41        // datagram sent to this address should be refused
42        SocketAddress refuser = new InetSocketAddress(InetAddress.getLocalHost(), port);
43
44        DatagramChannel dc = DatagramChannel.open().bind(new InetSocketAddress(0));
45        dc1.close();
46
47        Selector sel = Selector.open();
48        try {
49            dc.configureBlocking(false);
50            dc.register(sel, SelectionKey.OP_READ);
51
52            /* Test 1: not connected so ICMP port unreachable should not be received */
53            sendDatagram(dc, refuser);
54            int n = sel.select(2000);
55            if (n > 0) {
56                sel.selectedKeys().clear();
57                // BindException will be thrown if another service is using
58                // our expected refuser port, cannot run just exit.
59                DatagramChannel.open().bind(refuser).close();
60                throw new RuntimeException("Unexpected wakeup");
61            }
62
63            /* Test 2: connected so ICMP port unreachable may be received */
64            dc.connect(refuser);
65            try {
66                sendDatagram(dc, refuser);
67                n = sel.select(2000);
68                if (n > 0) {
69                    sel.selectedKeys().clear();
70                    try {
71                        n = dc.read(ByteBuffer.allocate(100));
72                        throw new RuntimeException("Unexpected datagram received");
73                    } catch (PortUnreachableException pue) {
74                        // expected
75                    }
76                }
77            } finally {
78                dc.disconnect();
79            }
80
81            /* Test 3: not connected so ICMP port unreachable should not be received */
82            sendDatagram(dc, refuser);
83            n = sel.select(2000);
84            if (n > 0) {
85                throw new RuntimeException("Unexpected wakeup after disconnect");
86            }
87
88        } catch(BindException e) {
89            // Do nothing, some other test has used this port
90        } finally {
91            sel.close();
92            dc.close();
93        }
94    }
95
96    static void sendDatagram(DatagramChannel dc, SocketAddress remote)
97        throws IOException
98    {
99        ByteBuffer bb = ByteBuffer.wrap("Greetings!".getBytes());
100        dc.send(bb, remote);
101    }
102}
103