|
1 /* |
|
2 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved. |
|
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 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /** |
|
25 * @test |
|
26 * @bug 4413768 |
|
27 * @summary Checking that PortUnreachableException is thrown when |
|
28 * ICMP Port Unreachable is received. |
|
29 */ |
|
30 import java.net.*; |
|
31 import java.util.Properties; |
|
32 |
|
33 public class Test { |
|
34 |
|
35 /* |
|
36 * Return an available port |
|
37 */ |
|
38 int getPort() throws Exception { |
|
39 DatagramSocket s = new DatagramSocket(0); |
|
40 int port = s.getLocalPort(); |
|
41 s.close(); |
|
42 return port; |
|
43 } |
|
44 |
|
45 /* |
|
46 * Perform test by sending to remote_host:port |
|
47 * sendOnly => send datagram to host and expect PUE on subsequent |
|
48 * send |
|
49 * !sendOnly => send datagram to host and expect PUE on subsequent |
|
50 * send or receive. |
|
51 */ |
|
52 void doTest(String remote_host, int port, boolean sendOnly) throws Exception { |
|
53 |
|
54 System.out.println("***"); |
|
55 System.out.println("Test Description:"); |
|
56 System.out.println(" DatagramSocket.connect"); |
|
57 System.out.println(" Loop: DatagramSocket.send"); |
|
58 if (!sendOnly) { |
|
59 System.out.println(" DatagramSocket.receive"); |
|
60 } |
|
61 System.out.println(""); |
|
62 System.out.println("Test Run:"); |
|
63 |
|
64 InetAddress ia = InetAddress.getByName(remote_host); |
|
65 DatagramSocket s = new DatagramSocket(0); |
|
66 s.setSoTimeout(1000); |
|
67 s.connect(ia, port); |
|
68 |
|
69 byte[] b = "Hello".getBytes(); |
|
70 DatagramPacket p1 = new DatagramPacket(b, b.length, ia, port); |
|
71 |
|
72 DatagramPacket p2 = new DatagramPacket(b, b.length); |
|
73 |
|
74 int i = 0; |
|
75 boolean gotPUE = false; |
|
76 |
|
77 do { |
|
78 |
|
79 System.out.println("Sending datagram to unreachable port..."); |
|
80 try { |
|
81 s.send(p1); |
|
82 } catch (PortUnreachableException e) { |
|
83 System.out.println("DatagramSocket.send threw PUE"); |
|
84 gotPUE = true; |
|
85 } |
|
86 |
|
87 if (!gotPUE) { |
|
88 Thread.currentThread().sleep(1000); |
|
89 } |
|
90 |
|
91 if (!sendOnly && !gotPUE) { |
|
92 System.out.println("DatagramSocket.receive..."); |
|
93 try { |
|
94 s.receive(p2); |
|
95 } catch (PortUnreachableException e) { |
|
96 System.out.println("DatagramSocket.receive threw PUE"); |
|
97 gotPUE = true; |
|
98 } catch (SocketTimeoutException e) { |
|
99 System.out.println("DatagramSocket.receive timed out - no PUE"); |
|
100 } |
|
101 } |
|
102 |
|
103 i++; |
|
104 } while (i < 10 && !gotPUE); |
|
105 |
|
106 if (!gotPUE) { |
|
107 System.out.println("DatagramSocket.{send,receive} didn't throw " + |
|
108 "PortUnreachableException - passing anyway!"); |
|
109 } else { |
|
110 System.out.println(" Test passed."); |
|
111 } |
|
112 System.out.println(""); |
|
113 } |
|
114 |
|
115 /* |
|
116 * Perform tests via remote_host. |
|
117 */ |
|
118 Test(String remote_host) throws Exception { |
|
119 |
|
120 int port = getPort(); |
|
121 |
|
122 doTest(remote_host, port, true); |
|
123 doTest(remote_host, port, false); |
|
124 } |
|
125 |
|
126 public static void main(String args[]) throws Exception { |
|
127 |
|
128 String remote_host = "localhost"; |
|
129 if (args.length > 0) { |
|
130 remote_host = args[0]; |
|
131 } |
|
132 |
|
133 new Test(remote_host); |
|
134 } |
|
135 } |