author | chegar |
Mon, 28 Jun 2010 20:52:37 +0100 | |
changeset 5981 | 2d5303114f44 |
parent 5506 | 202f599c92aa |
child 6115 | 7c523cf2bc8a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2005, 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 |
import java.net.*; |
|
25 |
import java.io.*; |
|
26 |
import java.util.*; |
|
27 |
||
28 |
public class Tests { |
|
29 |
/** |
|
30 |
* performs a simple exchange of data between the two sockets |
|
31 |
* and throws an exception if there is any problem. |
|
32 |
*/ |
|
33 |
public static void simpleDataExchange (Socket s1, Socket s2) |
|
34 |
throws Exception { |
|
35 |
||
36 |
InputStream i1 = s1.getInputStream(); |
|
37 |
InputStream i2 = s2.getInputStream(); |
|
38 |
OutputStream o1 = s1.getOutputStream(); |
|
39 |
OutputStream o2 = s2.getOutputStream(); |
|
40 |
||
5151
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
41 |
startSimpleWriter("SimpleWriter-1", o1, 100); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
42 |
startSimpleWriter("SimpleWriter-2", o2, 200); |
2 | 43 |
simpleRead (i2, 100); |
44 |
simpleRead (i1, 200); |
|
45 |
} |
|
46 |
||
5151
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
47 |
static void startSimpleWriter(String threadName, final OutputStream os, final int start) { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
48 |
(new Thread(new Runnable() { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
49 |
public void run() { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
50 |
try { simpleWrite(os, start); } |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
51 |
catch (Exception e) {unexpected(e); } |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
52 |
}}, threadName)).start(); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
53 |
} |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
54 |
|
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
55 |
static void unexpected(Exception e ) { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
56 |
System.out.println("Unexcepted Exception: " + e); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
57 |
e.printStackTrace(); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
58 |
} |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
59 |
|
2 | 60 |
/** |
61 |
* Send a packet from s1 to s2 (ia2/s2.localPort) and check it |
|
62 |
* Send a packet from s2 to s1 (ia1/s1.localPort) and check it |
|
63 |
*/ |
|
64 |
public static void simpleDataExchange (DatagramSocket s1, InetAddress ia1, |
|
65 |
DatagramSocket s2, InetAddress ia2) |
|
66 |
throws Exception { |
|
67 |
||
68 |
SocketAddress dest1 = new InetSocketAddress (ia1, s1.getLocalPort()); |
|
69 |
dprintln ("dest1 = " + dest1); |
|
70 |
SocketAddress dest2 = new InetSocketAddress (ia2, s2.getLocalPort()); |
|
71 |
dprintln ("dest2 = " + dest2); |
|
72 |
||
73 |
byte[] ba = "Hello world".getBytes(); |
|
74 |
byte[] bb = "HELLO WORLD1".getBytes(); |
|
75 |
DatagramPacket p1 = new DatagramPacket (ba, ba.length, dest1); |
|
76 |
DatagramPacket p2 = new DatagramPacket (ba, ba.length, dest2); |
|
77 |
||
78 |
DatagramPacket r1 = new DatagramPacket (new byte[256], 256); |
|
79 |
DatagramPacket r2 = new DatagramPacket (new byte[256], 256); |
|
80 |
||
81 |
s2.send (p1); |
|
82 |
s1.send (p2); |
|
83 |
s1.receive (r1); |
|
84 |
s2.receive (r2); |
|
85 |
comparePackets (p1, r1); |
|
86 |
comparePackets (p2, r2); |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Send a packet from s1 to s2 (ia2/s2.localPort) and send same packet |
|
91 |
* back from s2 to sender. Check s1 receives original packet |
|
92 |
*/ |
|
93 |
||
94 |
public static void datagramEcho (DatagramSocket s1, DatagramSocket s2, |
|
95 |
InetAddress ia2) |
|
96 |
throws Exception { |
|
97 |
||
98 |
byte[] ba = "Hello world".getBytes(); |
|
99 |
DatagramPacket p1; |
|
100 |
||
101 |
SocketAddress dest2 = null; |
|
102 |
if (ia2 != null) { |
|
103 |
dest2 = new InetSocketAddress (ia2, s2.getLocalPort()); |
|
104 |
p1 = new DatagramPacket (ba, ba.length, dest2); |
|
105 |
} else { |
|
106 |
p1 = new DatagramPacket (ba, ba.length); |
|
107 |
} |
|
108 |
||
109 |
dprintln ("dest2 = " + dest2); |
|
110 |
||
111 |
||
112 |
DatagramPacket r1 = new DatagramPacket (new byte[256], 256); |
|
113 |
DatagramPacket r2 = new DatagramPacket (new byte[256], 256); |
|
114 |
||
115 |
s1.send (p1); |
|
116 |
s2.receive (r1); |
|
117 |
s2.send (r1); |
|
118 |
s1.receive (r2); |
|
119 |
comparePackets (p1, r1); |
|
120 |
comparePackets (p1, r2); |
|
121 |
} |
|
122 |
||
123 |
public static void comparePackets (DatagramPacket p1, DatagramPacket p2) |
|
124 |
throws Exception { |
|
125 |
||
126 |
byte[] b1 = p1.getData(); |
|
127 |
byte[] b2 = p2.getData(); |
|
128 |
int len = p1.getLength () > p2.getLength() ? p2.getLength() |
|
129 |
: p1.getLength(); |
|
130 |
for (int i=0; i<len; i++) { |
|
131 |
if (b1[i] != b2[i]) { |
|
132 |
throw new Exception ("packets not the same"); |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
/* check the time got is within 20% of the time expected */ |
|
138 |
public static void checkTime (long got, long expected) { |
|
139 |
dprintln ("checkTime: got " + got + " expected " + expected); |
|
140 |
long upper = expected + (expected / 5); |
|
141 |
long lower = expected - (expected / 5); |
|
142 |
if (got > upper || got < lower) { |
|
143 |
throw new RuntimeException ("checkTime failed: got " + got + " expected " + expected); |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
static boolean debug = false; |
|
148 |
||
149 |
public static void checkDebug (String[] args) { |
|
150 |
debug = args.length > 0 && args[0].equals("-d"); |
|
151 |
} |
|
152 |
||
153 |
public static void dprint (String s) { |
|
154 |
if (debug) { |
|
155 |
System.out.print (s); |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
public static void dprintln (String s) { |
|
160 |
if (debug) { |
|
161 |
System.out.println (s); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
static int numberInterfaces () { |
|
166 |
try { |
|
167 |
Enumeration ifs = NetworkInterface.getNetworkInterfaces(); |
|
168 |
int nifs=0; |
|
169 |
while (ifs.hasMoreElements()) { |
|
170 |
nifs++; |
|
171 |
ifs.nextElement(); |
|
172 |
} |
|
173 |
return nifs; |
|
174 |
} catch (SocketException e) { |
|
175 |
return 0; |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
public static Enumeration ipv4Addresses() { |
|
180 |
return new AddrEnum (Inet4Address.class); |
|
181 |
} |
|
182 |
||
183 |
public static Inet4Address getFirstLocalIPv4Address () { |
|
184 |
Enumeration e = ipv4Addresses(); |
|
185 |
if (!e.hasMoreElements()) { |
|
186 |
return null; |
|
187 |
} |
|
188 |
return (Inet4Address)e.nextElement(); |
|
189 |
} |
|
190 |
||
191 |
public static Inet6Address getFirstLocalIPv6Address () { |
|
192 |
Enumeration e = ipv6Addresses(); |
|
193 |
if (!e.hasMoreElements()) { |
|
194 |
return null; |
|
195 |
} |
|
196 |
return (Inet6Address)e.nextElement(); |
|
197 |
} |
|
198 |
||
199 |
public static Enumeration ipv6Addresses() { |
|
200 |
return new AddrEnum (Inet6Address.class); |
|
201 |
} |
|
202 |
||
203 |
/* enumerates the Inet4Addresses or Inet6Addresses on the system */ |
|
204 |
||
205 |
private static class AddrEnum implements Enumeration { |
|
206 |
||
207 |
Enumeration ifs; |
|
208 |
NetworkInterface currIf = null; |
|
209 |
InetAddress nextAddr=null; |
|
210 |
Enumeration addrs=null; |
|
211 |
Class filter; |
|
212 |
||
213 |
static final byte[] fe80_loopback = new byte [] { |
|
214 |
(byte)0xfe,(byte)0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,1 |
|
215 |
}; |
|
216 |
||
217 |
AddrEnum (Class filter) { |
|
218 |
this.filter = filter; |
|
219 |
try { |
|
220 |
ifs = NetworkInterface.getNetworkInterfaces(); |
|
221 |
} catch (SocketException e) {} |
|
222 |
} |
|
223 |
||
224 |
public boolean hasMoreElements () { |
|
225 |
if (nextAddr == null) { |
|
226 |
nextAddr = getNext(); |
|
227 |
} |
|
228 |
return (nextAddr != null); |
|
229 |
} |
|
230 |
||
231 |
public Object nextElement () { |
|
232 |
if (!hasMoreElements()) { |
|
233 |
throw new NoSuchElementException ("no more addresses"); |
|
234 |
} |
|
235 |
Object next = nextAddr; |
|
236 |
nextAddr = null; |
|
237 |
return next; |
|
238 |
} |
|
239 |
||
240 |
private InetAddress getNext() { |
|
241 |
while (true) { |
|
242 |
if (currIf == null) { |
|
243 |
currIf = getNextIf(); |
|
244 |
if (currIf == null) { |
|
245 |
return null; |
|
246 |
} |
|
247 |
addrs = currIf.getInetAddresses(); |
|
248 |
} |
|
249 |
while (addrs.hasMoreElements()) { |
|
250 |
InetAddress addr = (InetAddress) addrs.nextElement(); |
|
5981
2d5303114f44
6961029: java/net/BindException/Test.java should not use wildcard address
chegar
parents:
5506
diff
changeset
|
251 |
if (filter.isInstance (addr) && !addr.isLoopbackAddress() |
2d5303114f44
6961029: java/net/BindException/Test.java should not use wildcard address
chegar
parents:
5506
diff
changeset
|
252 |
&& !addr.isAnyLocalAddress()) { |
2 | 253 |
if (Arrays.equals (addr.getAddress(), fe80_loopback)) { |
254 |
continue; |
|
255 |
} |
|
256 |
return addr; |
|
257 |
} |
|
258 |
} |
|
259 |
currIf = null; |
|
260 |
} |
|
261 |
} |
|
262 |
||
263 |
private NetworkInterface getNextIf () { |
|
5151
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
264 |
if (ifs != null) { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
265 |
while (ifs.hasMoreElements()) { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
266 |
NetworkInterface nic = (NetworkInterface)ifs.nextElement(); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
267 |
try { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
268 |
if (nic.isUp() && !nic.isLoopback()) |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
269 |
return nic; |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
270 |
} catch (SocketException e) { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
271 |
// ignore |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
272 |
} |
2 | 273 |
} |
274 |
} |
|
275 |
||
276 |
return null; |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
/** |
|
281 |
* Throws a RuntimeException if the boolean condition is false |
|
282 |
*/ |
|
283 |
public static void t_assert (boolean assertion) { |
|
284 |
if (assertion) { |
|
285 |
return; |
|
286 |
} |
|
287 |
Throwable t = new Throwable(); |
|
288 |
StackTraceElement[] strace = t.getStackTrace(); |
|
289 |
String msg = "Assertion failed at: " + strace[1].toString(); |
|
290 |
throw new RuntimeException (msg); |
|
291 |
} |
|
292 |
||
293 |
private static void simpleRead (InputStream is, int start) throws Exception { |
|
294 |
byte b[] = new byte [2]; |
|
295 |
for (int i=start; i<start+100; i++) { |
|
296 |
int x = is.read (b); |
|
297 |
if (x == 1) { |
|
298 |
x += is.read (b,1,1); |
|
299 |
} |
|
300 |
if (x!=2) { |
|
301 |
throw new Exception ("read error"); |
|
302 |
} |
|
303 |
int r = bytes (b[0], b[1]); |
|
304 |
if (r != i) { |
|
305 |
throw new Exception ("read " + r + " expected " +i); |
|
306 |
} |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
/* convert signed bytes to unisigned int */ |
|
311 |
private static int bytes (byte b1, byte b2) { |
|
312 |
int i1 = (int)b1 & 0xFF; |
|
313 |
int i2 = (int)b2 & 0xFF; |
|
314 |
return i1 * 256 + i2; |
|
315 |
} |
|
316 |
||
317 |
static void simpleWrite (OutputStream os, int start) throws Exception { |
|
318 |
byte b[] = new byte [2]; |
|
319 |
for (int i=start; i<start+100; i++) { |
|
320 |
b[0] = (byte) (i / 256); |
|
321 |
b[1] = (byte) (i % 256); |
|
322 |
os.write (b); |
|
323 |
} |
|
324 |
} |
|
325 |
||
326 |
private static class Runner extends Thread { |
|
327 |
Runnable runnee; |
|
328 |
long delay; |
|
329 |
||
330 |
Runner (Runnable runnee, long delay) { |
|
331 |
super(); |
|
332 |
this.runnee = runnee; |
|
333 |
this.delay = delay; |
|
334 |
} |
|
335 |
||
336 |
public void run () { |
|
337 |
try { |
|
338 |
Thread.sleep (delay); |
|
339 |
runnee.run (); |
|
340 |
} catch (Exception e) { |
|
341 |
e.printStackTrace(); |
|
342 |
} |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
/* |
|
347 |
* Take the given Runnable and run it in a spawned thread |
|
348 |
* after the given time has elapsed. runAfter() returns immediately |
|
349 |
*/ |
|
350 |
public static void runAfter (long millis, Runnable runnee) { |
|
351 |
Runner runner = new Runner (runnee, millis); |
|
352 |
runner.start (); |
|
353 |
} |
|
354 |
||
355 |
static String osname; |
|
356 |
||
357 |
static { |
|
358 |
osname = System.getProperty ("os.name"); |
|
359 |
} |
|
360 |
||
361 |
static boolean isLinux () { |
|
362 |
return osname.equals ("Linux"); |
|
363 |
} |
|
364 |
||
365 |
static boolean isWindows () { |
|
366 |
return osname.startsWith ("Windows"); |
|
367 |
} |
|
368 |
} |