2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2002, 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 4630914
|
|
27 |
* @summary Should only be one PUE even if multiple ICMPs were received
|
|
28 |
*/
|
|
29 |
import java.net.*;
|
|
30 |
import java.nio.*;
|
|
31 |
import java.nio.channels.*;
|
|
32 |
|
|
33 |
public class ThereCanBeOnlyOne {
|
|
34 |
|
|
35 |
static void doTest(InetAddress ia, boolean testSend) throws Exception {
|
|
36 |
DatagramChannel dc1 = DatagramChannel.open();
|
|
37 |
dc1.socket().bind((SocketAddress)null);
|
|
38 |
int port = dc1.socket().getLocalPort();
|
|
39 |
InetSocketAddress isa = new InetSocketAddress(ia, port);
|
|
40 |
dc1.connect(isa);
|
|
41 |
|
|
42 |
ByteBuffer bb = ByteBuffer.allocateDirect(512);
|
|
43 |
bb.put("hello".getBytes());
|
|
44 |
bb.flip();
|
|
45 |
|
|
46 |
/*
|
|
47 |
* Send a bunch of packets to the destination
|
|
48 |
*/
|
|
49 |
int outstanding = 0;
|
|
50 |
for (int i=0; i<20; i++) {
|
|
51 |
try {
|
|
52 |
bb.rewind();
|
|
53 |
dc1.write(bb);
|
|
54 |
outstanding++;
|
|
55 |
} catch (PortUnreachableException e) {
|
|
56 |
/* PUE throw => assume none outstanding now */
|
|
57 |
outstanding = 0;
|
|
58 |
}
|
|
59 |
if (outstanding > 1) {
|
|
60 |
break;
|
|
61 |
}
|
|
62 |
}
|
|
63 |
if (outstanding < 1) {
|
|
64 |
System.err.println("Insufficient exceptions outstanding - Test Skipped (Passed).");
|
|
65 |
dc1.close();
|
|
66 |
return;
|
|
67 |
}
|
|
68 |
|
|
69 |
/*
|
|
70 |
* Give time for ICMP port unreachables to return
|
|
71 |
*/
|
|
72 |
Thread.currentThread().sleep(5000);
|
|
73 |
|
|
74 |
/*
|
|
75 |
* The next send or receive should cause a PUE to be thrown
|
|
76 |
*/
|
|
77 |
boolean gotPUE = false;
|
|
78 |
boolean gotTimeout = false;
|
|
79 |
dc1.configureBlocking(false);
|
|
80 |
|
|
81 |
try {
|
|
82 |
if (testSend) {
|
|
83 |
bb.rewind();
|
|
84 |
dc1.write(bb);
|
|
85 |
} else {
|
|
86 |
bb.clear();
|
|
87 |
dc1.receive(bb);
|
|
88 |
}
|
|
89 |
} catch (PortUnreachableException pue) {
|
|
90 |
System.err.println("Got one PUE...");
|
|
91 |
gotPUE = true;
|
|
92 |
}
|
|
93 |
|
|
94 |
/*
|
|
95 |
* The next receive should not get another PUE
|
|
96 |
*/
|
|
97 |
if (gotPUE) {
|
|
98 |
try {
|
|
99 |
dc1.receive(bb);
|
|
100 |
} catch (PortUnreachableException pue) {
|
|
101 |
throw new Exception("PUs should have been consumed");
|
|
102 |
}
|
|
103 |
} else {
|
|
104 |
// packets discarded. Okay
|
|
105 |
}
|
|
106 |
|
|
107 |
dc1.close();
|
|
108 |
}
|
|
109 |
|
|
110 |
|
|
111 |
public static void main(String args[]) throws Exception {
|
|
112 |
InetAddress ia = InetAddress.getLocalHost();
|
|
113 |
doTest(ia, true);
|
|
114 |
doTest(ia, false);
|
|
115 |
}
|
|
116 |
|
|
117 |
}
|