test/jdk/java/nio/channels/DatagramChannel/Refused.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:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12047
diff changeset
     2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @summary Test DatagramChannel's receive when port unreachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class Refused {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static ByteBuffer outBuf = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static ByteBuffer inBuf  = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static DatagramChannel client;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static DatagramChannel server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static InetSocketAddress isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        outBuf.put("Blah Blah".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        outBuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        test1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        // This test has been disabled because there are many circumstances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        // under which no ICMP port unreachable packets are received
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        // See http://java.sun.com/j2se/1.4/networking-relnotes.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if ((args.length > 0) && (args[0].equals("test2"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            outBuf.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            test2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static void setup() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        client = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        server = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        client.socket().bind((SocketAddress)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        server.socket().bind((SocketAddress)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        client.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        server.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        InetAddress address = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        int port = client.socket().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        isa = new InetSocketAddress(address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Since this is not connected no PortUnreachableException should be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static void test1() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        setup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        server.send(outBuf, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        server.receive(inBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        outBuf.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        server.send(outBuf, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        server.receive(inBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        server.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // Test the connected case to see if PUE is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static void test2() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        setup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        server.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        server.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        server.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        outBuf.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        server.write(outBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        server.receive(inBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Thread.sleep(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        outBuf.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            server.write(outBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            Thread.sleep(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            inBuf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            server.read(inBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (PortUnreachableException pue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.err.println("received PUE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        server.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}