author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 (2019-11-30) | |
changeset 59330 | 5b96c12f909d |
parent 49799 | 33dcb9c42f55 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
41887
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
2 |
* Copyright (c) 2003, 2016, 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 |
||
49799
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
24 |
import jdk.test.lib.NetworkConfiguration; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
25 |
|
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
26 |
import java.io.IOException; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
27 |
import java.io.InputStream; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
28 |
import java.io.OutputStream; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
29 |
import java.net.DatagramPacket; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
30 |
import java.net.DatagramSocket; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
31 |
import java.net.Inet4Address; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
32 |
import java.net.Inet6Address; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
33 |
import java.net.InetAddress; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
34 |
import java.net.InetSocketAddress; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
35 |
import java.net.Socket; |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
36 |
import java.net.SocketAddress; |
2 | 37 |
|
38 |
public class Tests { |
|
25975
80a1b7aa96d8
8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents:
7668
diff
changeset
|
39 |
|
44671
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
41887
diff
changeset
|
40 |
static final boolean isWindows = |
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
41887
diff
changeset
|
41 |
System.getProperty("os.name").startsWith("Windows"); |
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
41887
diff
changeset
|
42 |
static final boolean isMacOS = |
d1d011d4654d
8177536: Avoid Apple Peer-to-Peer interfaces in networking tests
chegar
parents:
41887
diff
changeset
|
43 |
System.getProperty("os.name").contains("OS X"); |
25975
80a1b7aa96d8
8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents:
7668
diff
changeset
|
44 |
|
2 | 45 |
/** |
46 |
* performs a simple exchange of data between the two sockets |
|
47 |
* and throws an exception if there is any problem. |
|
48 |
*/ |
|
49 |
public static void simpleDataExchange (Socket s1, Socket s2) |
|
50 |
throws Exception { |
|
51 |
||
52 |
InputStream i1 = s1.getInputStream(); |
|
53 |
InputStream i2 = s2.getInputStream(); |
|
54 |
OutputStream o1 = s1.getOutputStream(); |
|
55 |
OutputStream o2 = s2.getOutputStream(); |
|
56 |
||
5151
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
57 |
startSimpleWriter("SimpleWriter-1", o1, 100); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
58 |
startSimpleWriter("SimpleWriter-2", o2, 200); |
2 | 59 |
simpleRead (i2, 100); |
60 |
simpleRead (i1, 200); |
|
61 |
} |
|
62 |
||
5151
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
63 |
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
|
64 |
(new Thread(new Runnable() { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
65 |
public void run() { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
66 |
try { simpleWrite(os, start); } |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
67 |
catch (Exception e) {unexpected(e); } |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
68 |
}}, threadName)).start(); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
69 |
} |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
70 |
|
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
71 |
static void unexpected(Exception e ) { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
72 |
System.out.println("Unexcepted Exception: " + e); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
73 |
e.printStackTrace(); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
74 |
} |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
75 |
|
2 | 76 |
/** |
77 |
* Send a packet from s1 to s2 (ia2/s2.localPort) and check it |
|
78 |
* Send a packet from s2 to s1 (ia1/s1.localPort) and check it |
|
79 |
*/ |
|
80 |
public static void simpleDataExchange (DatagramSocket s1, InetAddress ia1, |
|
81 |
DatagramSocket s2, InetAddress ia2) |
|
82 |
throws Exception { |
|
83 |
||
84 |
SocketAddress dest1 = new InetSocketAddress (ia1, s1.getLocalPort()); |
|
85 |
dprintln ("dest1 = " + dest1); |
|
86 |
SocketAddress dest2 = new InetSocketAddress (ia2, s2.getLocalPort()); |
|
87 |
dprintln ("dest2 = " + dest2); |
|
88 |
||
89 |
byte[] ba = "Hello world".getBytes(); |
|
90 |
byte[] bb = "HELLO WORLD1".getBytes(); |
|
91 |
DatagramPacket p1 = new DatagramPacket (ba, ba.length, dest1); |
|
92 |
DatagramPacket p2 = new DatagramPacket (ba, ba.length, dest2); |
|
93 |
||
94 |
DatagramPacket r1 = new DatagramPacket (new byte[256], 256); |
|
95 |
DatagramPacket r2 = new DatagramPacket (new byte[256], 256); |
|
96 |
||
97 |
s2.send (p1); |
|
98 |
s1.send (p2); |
|
99 |
s1.receive (r1); |
|
100 |
s2.receive (r2); |
|
101 |
comparePackets (p1, r1); |
|
102 |
comparePackets (p2, r2); |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Send a packet from s1 to s2 (ia2/s2.localPort) and send same packet |
|
107 |
* back from s2 to sender. Check s1 receives original packet |
|
108 |
*/ |
|
109 |
||
110 |
public static void datagramEcho (DatagramSocket s1, DatagramSocket s2, |
|
111 |
InetAddress ia2) |
|
112 |
throws Exception { |
|
113 |
||
114 |
byte[] ba = "Hello world".getBytes(); |
|
115 |
DatagramPacket p1; |
|
116 |
||
117 |
SocketAddress dest2 = null; |
|
118 |
if (ia2 != null) { |
|
119 |
dest2 = new InetSocketAddress (ia2, s2.getLocalPort()); |
|
120 |
p1 = new DatagramPacket (ba, ba.length, dest2); |
|
121 |
} else { |
|
122 |
p1 = new DatagramPacket (ba, ba.length); |
|
123 |
} |
|
124 |
||
125 |
dprintln ("dest2 = " + dest2); |
|
126 |
||
127 |
||
128 |
DatagramPacket r1 = new DatagramPacket (new byte[256], 256); |
|
129 |
DatagramPacket r2 = new DatagramPacket (new byte[256], 256); |
|
130 |
||
131 |
s1.send (p1); |
|
132 |
s2.receive (r1); |
|
133 |
s2.send (r1); |
|
134 |
s1.receive (r2); |
|
135 |
comparePackets (p1, r1); |
|
136 |
comparePackets (p1, r2); |
|
137 |
} |
|
138 |
||
139 |
public static void comparePackets (DatagramPacket p1, DatagramPacket p2) |
|
140 |
throws Exception { |
|
141 |
||
142 |
byte[] b1 = p1.getData(); |
|
143 |
byte[] b2 = p2.getData(); |
|
144 |
int len = p1.getLength () > p2.getLength() ? p2.getLength() |
|
145 |
: p1.getLength(); |
|
146 |
for (int i=0; i<len; i++) { |
|
147 |
if (b1[i] != b2[i]) { |
|
148 |
throw new Exception ("packets not the same"); |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5981
diff
changeset
|
153 |
/* check the time got is within 50% of the time expected */ |
2 | 154 |
public static void checkTime (long got, long expected) { |
41887
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
155 |
checkTime(got, expected, expected); |
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
156 |
} |
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
157 |
|
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
158 |
/* check the time got is between start and end, given 50% tolerance */ |
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
159 |
public static void checkTime(long got, long start, long end) { |
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
160 |
dprintln("checkTime: got = " + got + " start = " + start + " end = " + end); |
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
161 |
long upper = end + (end / 2); |
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
162 |
long lower = start - (start / 2); |
2 | 163 |
if (got > upper || got < lower) { |
41887
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
164 |
throw new RuntimeException("checkTime failed: got " + got |
61839a1a7e62
8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents:
25975
diff
changeset
|
165 |
+ ", expected between " + start + " and " + end); |
2 | 166 |
} |
167 |
} |
|
168 |
||
169 |
static boolean debug = false; |
|
170 |
||
171 |
public static void checkDebug (String[] args) { |
|
172 |
debug = args.length > 0 && args[0].equals("-d"); |
|
173 |
} |
|
174 |
||
175 |
public static void dprint (String s) { |
|
176 |
if (debug) { |
|
177 |
System.out.print (s); |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
public static void dprintln (String s) { |
|
182 |
if (debug) { |
|
183 |
System.out.println (s); |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
public static Inet4Address getFirstLocalIPv4Address () { |
|
49799
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
188 |
return getNetworkConfig().ip4Addresses() |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
189 |
.filter(a -> !a.isLoopbackAddress()) |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
190 |
.findFirst() |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
191 |
.orElse(null); |
2 | 192 |
} |
193 |
||
194 |
public static Inet6Address getFirstLocalIPv6Address () { |
|
49799
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
195 |
return getNetworkConfig().ip6Addresses() |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
196 |
.filter(a -> !a.isLoopbackAddress()) |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
197 |
.findFirst() |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
198 |
.orElse(null); |
2 | 199 |
} |
200 |
||
49799
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
201 |
private static NetworkConfiguration getNetworkConfig() { |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
202 |
try { |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
203 |
return NetworkConfiguration.probe(); |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
204 |
} catch (IOException e) { |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
205 |
System.out.println("Failed to probe NetworkConfiguration"); |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
206 |
throw new RuntimeException(e); |
2 | 207 |
} |
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Throws a RuntimeException if the boolean condition is false |
|
212 |
*/ |
|
213 |
public static void t_assert (boolean assertion) { |
|
214 |
if (assertion) { |
|
215 |
return; |
|
216 |
} |
|
217 |
Throwable t = new Throwable(); |
|
218 |
StackTraceElement[] strace = t.getStackTrace(); |
|
219 |
String msg = "Assertion failed at: " + strace[1].toString(); |
|
220 |
throw new RuntimeException (msg); |
|
221 |
} |
|
222 |
||
223 |
private static void simpleRead (InputStream is, int start) throws Exception { |
|
224 |
byte b[] = new byte [2]; |
|
225 |
for (int i=start; i<start+100; i++) { |
|
226 |
int x = is.read (b); |
|
227 |
if (x == 1) { |
|
228 |
x += is.read (b,1,1); |
|
229 |
} |
|
230 |
if (x!=2) { |
|
231 |
throw new Exception ("read error"); |
|
232 |
} |
|
233 |
int r = bytes (b[0], b[1]); |
|
234 |
if (r != i) { |
|
235 |
throw new Exception ("read " + r + " expected " +i); |
|
236 |
} |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
/* convert signed bytes to unisigned int */ |
|
241 |
private static int bytes (byte b1, byte b2) { |
|
242 |
int i1 = (int)b1 & 0xFF; |
|
243 |
int i2 = (int)b2 & 0xFF; |
|
244 |
return i1 * 256 + i2; |
|
245 |
} |
|
246 |
||
247 |
static void simpleWrite (OutputStream os, int start) throws Exception { |
|
248 |
byte b[] = new byte [2]; |
|
249 |
for (int i=start; i<start+100; i++) { |
|
250 |
b[0] = (byte) (i / 256); |
|
251 |
b[1] = (byte) (i % 256); |
|
252 |
os.write (b); |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
private static class Runner extends Thread { |
|
257 |
Runnable runnee; |
|
258 |
long delay; |
|
259 |
||
260 |
Runner (Runnable runnee, long delay) { |
|
261 |
super(); |
|
262 |
this.runnee = runnee; |
|
263 |
this.delay = delay; |
|
264 |
} |
|
265 |
||
266 |
public void run () { |
|
267 |
try { |
|
268 |
Thread.sleep (delay); |
|
269 |
runnee.run (); |
|
270 |
} catch (Exception e) { |
|
271 |
e.printStackTrace(); |
|
272 |
} |
|
273 |
} |
|
274 |
} |
|
275 |
||
276 |
/* |
|
277 |
* Take the given Runnable and run it in a spawned thread |
|
278 |
* after the given time has elapsed. runAfter() returns immediately |
|
279 |
*/ |
|
280 |
public static void runAfter (long millis, Runnable runnee) { |
|
281 |
Runner runner = new Runner (runnee, millis); |
|
282 |
runner.start (); |
|
283 |
} |
|
284 |
||
285 |
static String osname; |
|
286 |
||
287 |
static { |
|
288 |
osname = System.getProperty ("os.name"); |
|
289 |
} |
|
290 |
||
291 |
static boolean isLinux () { |
|
292 |
return osname.equals ("Linux"); |
|
293 |
} |
|
294 |
||
295 |
static boolean isWindows () { |
|
296 |
return osname.startsWith ("Windows"); |
|
297 |
} |
|
298 |
} |