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