Path: blob/master/test/jdk/java/net/PortUnreachableException/OneExceptionOnly.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 443102026* @summary On Windows 2000 we observed behaviour that reflects the underlying27* implementation :-28* 1. PortUnreachableException throw per underlying reset29* 2. No PUE throw for DatagramSocket.send30*/31import java.net.*;3233public class OneExceptionOnly {3435static void doTest(InetAddress ia, int port, boolean testSend) throws Exception {3637System.out.println("");38System.out.println("***");39System.out.println("Test Description:");40System.out.println(" - Send 10 datagrams to bad destination");41System.out.println(" - <wait a wee while>");42if (testSend) {43System.out.println(" - Send another datagram - should throw PUE or timeout");44} else {45System.out.println(" - Receive another datagram - should throw PUE or timeout");46}47System.out.println(" - Receive another receive - a SocketTimeoutException expected");48System.out.println("");4950/*51* Create the datagram and connect it to destination52*/53DatagramSocket s1 = new DatagramSocket();54s1.connect(ia, port);5556byte b[] = new byte[512];57DatagramPacket p = new DatagramPacket(b, b.length);5859/*60* Send a bunch of packets to the destination61*/62int outstanding = 0;63for (int i=0; i<20; i++) {64try {65s1.send(p);66outstanding++;67} catch (PortUnreachableException e) {6869/* PUE throw => assume none outstanding now */70outstanding = 0;71}72if (outstanding > 1) {73break;74}75}76if (outstanding < 1) {77System.out.println("Insufficient exceptions outstanding - Test Skipped (Passed).");78s1.close();79return;80}8182/*83* Give time for ICMP port unreachables to return84*/85Thread.currentThread().sleep(5000);8687/*88* The next send or receive should cause a PUE to be thrown89*/90boolean gotPUE = false;91boolean gotTimeout = false;92s1.setSoTimeout(2000);93try {94if (testSend) {95s1.send(p);96} else {97s1.receive(p);98}99} catch (PortUnreachableException pue) {100gotPUE = true;101System.out.println("Expected PortUnreachableException thrown - good!");102} catch (SocketTimeoutException exc) {103}104105/*106* The next receive should timeout107*/108if (gotPUE) {109try {110s1.receive(p);111} catch (PortUnreachableException pue) {112throw new Exception("Unexpected PUE received - assumed that PUs would be consumed");113} catch (SocketTimeoutException exc) {114System.out.println("Expected SocketTimeoutException thrown - excellent! - Test Passed.");115}116} else {117System.out.println("Expected PUE not thrown - packets probably discarded (Passed).");118}119120s1.close();121}122123124public static void main(String args[]) throws Exception {125126InetAddress ia;127int port;128if (args.length >= 2) {129ia = InetAddress.getByName(args[0]);130port = Integer.parseInt(args[1]);131} else {132ia = InetAddress.getLocalHost();133DatagramSocket s1 = new DatagramSocket();134port = s1.getLocalPort();135s1.close();136}137138doTest(ia, port, true);139doTest(ia, port, false);140141}142143}144145146