Path: blob/master/test/jdk/java/net/PortUnreachableException/Test.java
41149 views
/*1* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 441376826* @summary Checking that PortUnreachableException is thrown when27* ICMP Port Unreachable is received.28*/29import java.net.*;30import java.util.Properties;3132public class Test {3334/*35* Return an available port36*/37int getPort() throws Exception {38DatagramSocket s = new DatagramSocket(0);39int port = s.getLocalPort();40s.close();41return port;42}4344/*45* Perform test by sending to remote_host:port46* sendOnly => send datagram to host and expect PUE on subsequent47* send48* !sendOnly => send datagram to host and expect PUE on subsequent49* send or receive.50*/51void doTest(String remote_host, int port, boolean sendOnly) throws Exception {5253System.out.println("***");54System.out.println("Test Description:");55System.out.println(" DatagramSocket.connect");56System.out.println(" Loop: DatagramSocket.send");57if (!sendOnly) {58System.out.println(" DatagramSocket.receive");59}60System.out.println("");61System.out.println("Test Run:");6263InetAddress ia = InetAddress.getByName(remote_host);64DatagramSocket s = new DatagramSocket(0);65s.setSoTimeout(1000);66s.connect(ia, port);6768byte[] b = "Hello".getBytes();69DatagramPacket p1 = new DatagramPacket(b, b.length, ia, port);7071DatagramPacket p2 = new DatagramPacket(b, b.length);7273int i = 0;74boolean gotPUE = false;7576do {7778System.out.println("Sending datagram to unreachable port...");79try {80s.send(p1);81} catch (PortUnreachableException e) {82System.out.println("DatagramSocket.send threw PUE");83gotPUE = true;84}8586if (!gotPUE) {87Thread.currentThread().sleep(1000);88}8990if (!sendOnly && !gotPUE) {91System.out.println("DatagramSocket.receive...");92try {93s.receive(p2);94} catch (PortUnreachableException e) {95System.out.println("DatagramSocket.receive threw PUE");96gotPUE = true;97} catch (SocketTimeoutException e) {98System.out.println("DatagramSocket.receive timed out - no PUE");99}100}101102i++;103} while (i < 10 && !gotPUE);104105if (!gotPUE) {106System.out.println("DatagramSocket.{send,receive} didn't throw " +107"PortUnreachableException - passing anyway!");108} else {109System.out.println(" Test passed.");110}111System.out.println("");112}113114/*115* Perform tests via remote_host.116*/117Test(String remote_host) throws Exception {118119int port = getPort();120121doTest(remote_host, port, true);122doTest(remote_host, port, false);123}124125public static void main(String args[]) throws Exception {126127String remote_host = "localhost";128if (args.length > 0) {129remote_host = args[0];130}131132new Test(remote_host);133}134}135136137