Refused.java revision 5116:d45bc4307996
1/*
2 * Copyright (c) 2002, 2003, 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 * @summary Test DatagramChannel's receive when port unreachable
26 * @author Mike McCloskey
27 */
28
29import java.io.*;
30import java.net.*;
31import java.nio.*;
32import java.nio.channels.*;
33
34public class Refused {
35
36    static ByteBuffer outBuf = ByteBuffer.allocateDirect(100);
37    static ByteBuffer inBuf  = ByteBuffer.allocateDirect(100);
38    static DatagramChannel client;
39    static DatagramChannel server;
40    static InetSocketAddress isa;
41
42    public static void main(String[] args) throws Exception {
43        outBuf.put("Blah Blah".getBytes());
44        outBuf.flip();
45        test1();
46
47        // This test has been disabled because there are many circumstances
48        // under which no ICMP port unreachable packets are received
49        // See http://java.sun.com/j2se/1.4/networking-relnotes.html
50        if ((args.length > 0) && (args[0].equals("test2"))) {
51            outBuf.rewind();
52            test2();
53        }
54    }
55
56    public static void setup() throws Exception {
57        client = DatagramChannel.open();
58        server = DatagramChannel.open();
59
60        client.socket().bind((SocketAddress)null);
61        server.socket().bind((SocketAddress)null);
62
63        client.configureBlocking(false);
64        server.configureBlocking(false);
65
66        InetAddress address = InetAddress.getLocalHost();
67        int port = client.socket().getLocalPort();
68        isa = new InetSocketAddress(address, port);
69    }
70
71    // Since this is not connected no PortUnreachableException should be thrown
72    public static void test1() throws Exception {
73        setup();
74
75        server.send(outBuf, isa);
76        server.receive(inBuf);
77
78        client.close();
79
80        outBuf.rewind();
81        server.send(outBuf, isa);
82        server.receive(inBuf);
83
84        server.close();
85    }
86
87    // Test the connected case to see if PUE is thrown
88    public static void test2() throws Exception {
89
90        setup();
91        server.configureBlocking(true);
92        server.connect(isa);
93        server.configureBlocking(false);
94        outBuf.rewind();
95        server.write(outBuf);
96        server.receive(inBuf);
97
98        client.close();
99        Thread.sleep(2000);
100        outBuf.rewind();
101
102        try {
103            server.write(outBuf);
104            Thread.sleep(2000);
105            inBuf.clear();
106            server.read(inBuf);
107        } catch (PortUnreachableException pue) {
108            System.err.println("received PUE");
109        }
110        server.close();
111    }
112}
113