2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4431020
|
|
27 |
* @summary On Windows 2000 we observed behaviour that reflects the underlying
|
|
28 |
* implementation :-
|
|
29 |
* 1. PortUnreachableException throw per underlying reset
|
|
30 |
* 2. No PUE throw for DatagramSocket.send
|
|
31 |
*/
|
|
32 |
import java.net.*;
|
|
33 |
|
|
34 |
public class OneExceptionOnly {
|
|
35 |
|
|
36 |
static void doTest(InetAddress ia, int port, boolean testSend) throws Exception {
|
|
37 |
|
|
38 |
System.out.println("");
|
|
39 |
System.out.println("***");
|
|
40 |
System.out.println("Test Description:");
|
|
41 |
System.out.println(" - Send 10 datagrams to bad destination");
|
|
42 |
System.out.println(" - <wait a wee while>");
|
|
43 |
if (testSend) {
|
|
44 |
System.out.println(" - Send another datagram - should throw PUE or timeout");
|
|
45 |
} else {
|
|
46 |
System.out.println(" - Receive another datagram - should throw PUE or timeout");
|
|
47 |
}
|
|
48 |
System.out.println(" - Receive another receive - a SocketTimeoutException expected");
|
|
49 |
System.out.println("");
|
|
50 |
|
|
51 |
/*
|
|
52 |
* Create the datagram and connect it to destination
|
|
53 |
*/
|
|
54 |
DatagramSocket s1 = new DatagramSocket();
|
|
55 |
s1.connect(ia, port);
|
|
56 |
|
|
57 |
byte b[] = new byte[512];
|
|
58 |
DatagramPacket p = new DatagramPacket(b, b.length);
|
|
59 |
|
|
60 |
/*
|
|
61 |
* Send a bunch of packets to the destination
|
|
62 |
*/
|
|
63 |
int outstanding = 0;
|
|
64 |
for (int i=0; i<20; i++) {
|
|
65 |
try {
|
|
66 |
s1.send(p);
|
|
67 |
outstanding++;
|
|
68 |
} catch (PortUnreachableException e) {
|
|
69 |
|
|
70 |
/* PUE throw => assume none outstanding now */
|
|
71 |
outstanding = 0;
|
|
72 |
}
|
|
73 |
if (outstanding > 1) {
|
|
74 |
break;
|
|
75 |
}
|
|
76 |
}
|
|
77 |
if (outstanding < 1) {
|
|
78 |
System.out.println("Insufficient exceptions outstanding - Test Skipped (Passed).");
|
|
79 |
s1.close();
|
|
80 |
return;
|
|
81 |
}
|
|
82 |
|
|
83 |
/*
|
|
84 |
* Give time for ICMP port unreachables to return
|
|
85 |
*/
|
|
86 |
Thread.currentThread().sleep(5000);
|
|
87 |
|
|
88 |
/*
|
|
89 |
* The next send or receive should cause a PUE to be thrown
|
|
90 |
*/
|
|
91 |
boolean gotPUE = false;
|
|
92 |
boolean gotTimeout = false;
|
|
93 |
s1.setSoTimeout(2000);
|
|
94 |
try {
|
|
95 |
if (testSend) {
|
|
96 |
s1.send(p);
|
|
97 |
} else {
|
|
98 |
s1.receive(p);
|
|
99 |
}
|
|
100 |
} catch (PortUnreachableException pue) {
|
|
101 |
gotPUE = true;
|
|
102 |
System.out.println("Expected PortUnreachableException thrown - good!");
|
|
103 |
} catch (SocketTimeoutException exc) {
|
|
104 |
}
|
|
105 |
|
|
106 |
/*
|
|
107 |
* The next receive should timeout
|
|
108 |
*/
|
|
109 |
if (gotPUE) {
|
|
110 |
try {
|
|
111 |
s1.receive(p);
|
|
112 |
} catch (PortUnreachableException pue) {
|
|
113 |
throw new Exception("Unexpected PUE received - assumed that PUs would be consumed");
|
|
114 |
} catch (SocketTimeoutException exc) {
|
|
115 |
System.out.println("Expected SocketTimeoutException thrown - excellent! - Test Passed.");
|
|
116 |
}
|
|
117 |
} else {
|
|
118 |
System.out.println("Expected PUE not thrown - packets probably discarded (Passed).");
|
|
119 |
}
|
|
120 |
|
|
121 |
s1.close();
|
|
122 |
}
|
|
123 |
|
|
124 |
|
|
125 |
public static void main(String args[]) throws Exception {
|
|
126 |
|
|
127 |
InetAddress ia;
|
|
128 |
int port;
|
|
129 |
if (args.length >= 2) {
|
|
130 |
ia = InetAddress.getByName(args[0]);
|
|
131 |
port = Integer.parseInt(args[1]);
|
|
132 |
} else {
|
|
133 |
ia = InetAddress.getLocalHost();
|
|
134 |
DatagramSocket s1 = new DatagramSocket();
|
|
135 |
port = s1.getLocalPort();
|
|
136 |
s1.close();
|
|
137 |
}
|
|
138 |
|
|
139 |
doTest(ia, port, true);
|
|
140 |
doTest(ia, port, false);
|
|
141 |
|
|
142 |
}
|
|
143 |
|
|
144 |
}
|