0N/A/*
2362N/A * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4413768
0N/A * @summary Checking that PortUnreachableException is thrown when
0N/A * ICMP Port Unreachable is received.
0N/A */
0N/Aimport java.net.*;
0N/Aimport java.util.Properties;
0N/A
0N/Apublic class Test {
0N/A
0N/A /*
0N/A * Return an available port
0N/A */
0N/A int getPort() throws Exception {
0N/A DatagramSocket s = new DatagramSocket(0);
0N/A int port = s.getLocalPort();
0N/A s.close();
0N/A return port;
0N/A }
0N/A
0N/A /*
0N/A * Perform test by sending to remote_host:port
0N/A * sendOnly => send datagram to host and expect PUE on subsequent
0N/A * send
0N/A * !sendOnly => send datagram to host and expect PUE on subsequent
0N/A * send or receive.
0N/A */
0N/A void doTest(String remote_host, int port, boolean sendOnly) throws Exception {
0N/A
0N/A System.out.println("***");
0N/A System.out.println("Test Description:");
0N/A System.out.println(" DatagramSocket.connect");
0N/A System.out.println(" Loop: DatagramSocket.send");
0N/A if (!sendOnly) {
0N/A System.out.println(" DatagramSocket.receive");
0N/A }
0N/A System.out.println("");
0N/A System.out.println("Test Run:");
0N/A
0N/A InetAddress ia = InetAddress.getByName(remote_host);
0N/A DatagramSocket s = new DatagramSocket(0);
0N/A s.setSoTimeout(1000);
0N/A s.connect(ia, port);
0N/A
0N/A byte[] b = "Hello".getBytes();
0N/A DatagramPacket p1 = new DatagramPacket(b, b.length, ia, port);
0N/A
0N/A DatagramPacket p2 = new DatagramPacket(b, b.length);
0N/A
0N/A int i = 0;
0N/A boolean gotPUE = false;
0N/A
0N/A do {
0N/A
0N/A System.out.println("Sending datagram to unreachable port...");
0N/A try {
0N/A s.send(p1);
0N/A } catch (PortUnreachableException e) {
0N/A System.out.println("DatagramSocket.send threw PUE");
0N/A gotPUE = true;
0N/A }
0N/A
0N/A if (!gotPUE) {
0N/A Thread.currentThread().sleep(1000);
0N/A }
0N/A
0N/A if (!sendOnly && !gotPUE) {
0N/A System.out.println("DatagramSocket.receive...");
0N/A try {
0N/A s.receive(p2);
0N/A } catch (PortUnreachableException e) {
0N/A System.out.println("DatagramSocket.receive threw PUE");
0N/A gotPUE = true;
0N/A } catch (SocketTimeoutException e) {
0N/A System.out.println("DatagramSocket.receive timed out - no PUE");
0N/A }
0N/A }
0N/A
0N/A i++;
0N/A } while (i < 10 && !gotPUE);
0N/A
0N/A if (!gotPUE) {
0N/A System.out.println("DatagramSocket.{send,receive} didn't throw " +
0N/A "PortUnreachableException - passing anyway!");
0N/A } else {
0N/A System.out.println(" Test passed.");
0N/A }
0N/A System.out.println("");
0N/A }
0N/A
0N/A /*
0N/A * Perform tests via remote_host.
0N/A */
0N/A Test(String remote_host) throws Exception {
0N/A
0N/A int port = getPort();
0N/A
0N/A doTest(remote_host, port, true);
0N/A doTest(remote_host, port, false);
0N/A }
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A
0N/A String remote_host = "localhost";
0N/A if (args.length > 0) {
0N/A remote_host = args[0];
0N/A }
0N/A
0N/A new Test(remote_host);
0N/A }
0N/A}