jdk/test/java/net/PortUnreachableException/Test.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 5506 202f599c92aa
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4413768
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Checking that PortUnreachableException is thrown when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          ICMP Port Unreachable is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     * Return an available port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    int getPort() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        DatagramSocket s = new DatagramSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        int port = s.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Perform test by sending to remote_host:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * sendOnly => send datagram to host and expect PUE on subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *             send
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * !sendOnly => send datagram to host and expect PUE on subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *              send or receive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    void doTest(String remote_host, int port, boolean sendOnly) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        System.out.println("***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        System.out.println("Test Description:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        System.out.println("    DatagramSocket.connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        System.out.println("    Loop: DatagramSocket.send");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (!sendOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            System.out.println("          DatagramSocket.receive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        System.out.println("Test Run:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        InetAddress ia = InetAddress.getByName(remote_host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        DatagramSocket s = new DatagramSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        s.setSoTimeout(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        s.connect(ia, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        byte[] b = "Hello".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        DatagramPacket p1 = new DatagramPacket(b, b.length, ia, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        DatagramPacket p2 = new DatagramPacket(b, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        boolean gotPUE = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.out.println("Sending datagram to unreachable port...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                s.send(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            } catch (PortUnreachableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                System.out.println("DatagramSocket.send threw PUE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                gotPUE = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (!gotPUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                Thread.currentThread().sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (!sendOnly && !gotPUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                System.out.println("DatagramSocket.receive...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    s.receive(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                } catch (PortUnreachableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    System.out.println("DatagramSocket.receive threw PUE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    gotPUE = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                } catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    System.out.println("DatagramSocket.receive timed out - no PUE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } while (i < 10 && !gotPUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (!gotPUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            System.out.println("DatagramSocket.{send,receive} didn't throw " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                "PortUnreachableException - passing anyway!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.out.println("    Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Perform tests via remote_host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    Test(String remote_host) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int port = getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        doTest(remote_host, port, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        doTest(remote_host, port, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        String remote_host = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            remote_host = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        new Test(remote_host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}