Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ipv6tests/Tests.java
38812 views
1
/*
2
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. 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 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.
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
41
startSimpleWriter("SimpleWriter-1", o1, 100);
42
startSimpleWriter("SimpleWriter-2", o2, 200);
43
simpleRead (i2, 100);
44
simpleRead (i1, 200);
45
}
46
47
static void startSimpleWriter(String threadName, final OutputStream os, final int start) {
48
(new Thread(new Runnable() {
49
public void run() {
50
try { simpleWrite(os, start); }
51
catch (Exception e) {unexpected(e); }
52
}}, threadName)).start();
53
}
54
55
static void unexpected(Exception e ) {
56
System.out.println("Unexcepted Exception: " + e);
57
e.printStackTrace();
58
}
59
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 50% of the time expected */
138
public static void checkTime (long got, long expected) {
139
checkTime(got, expected, expected);
140
}
141
142
/* check the time got is between start and end, given 50% tolerance */
143
public static void checkTime(long got, long start, long end) {
144
dprintln("checkTime: got = " + got + " start = " + start + " end = " + end);
145
long upper = end + (end / 2);
146
long lower = start - (start / 2);
147
if (got > upper || got < lower) {
148
throw new RuntimeException("checkTime failed: got " + got
149
+ ", expected between " + start + " and " + end);
150
}
151
}
152
153
static boolean debug = false;
154
155
public static void checkDebug (String[] args) {
156
debug = args.length > 0 && args[0].equals("-d");
157
}
158
159
public static void dprint (String s) {
160
if (debug) {
161
System.out.print (s);
162
}
163
}
164
165
public static void dprintln (String s) {
166
if (debug) {
167
System.out.println (s);
168
}
169
}
170
171
static int numberInterfaces () {
172
try {
173
Enumeration ifs = NetworkInterface.getNetworkInterfaces();
174
int nifs=0;
175
while (ifs.hasMoreElements()) {
176
nifs++;
177
ifs.nextElement();
178
}
179
return nifs;
180
} catch (SocketException e) {
181
return 0;
182
}
183
}
184
185
public static Enumeration ipv4Addresses() {
186
return new AddrEnum (Inet4Address.class);
187
}
188
189
public static Inet4Address getFirstLocalIPv4Address () {
190
Enumeration e = ipv4Addresses();
191
if (!e.hasMoreElements()) {
192
return null;
193
}
194
return (Inet4Address)e.nextElement();
195
}
196
197
public static Inet6Address getFirstLocalIPv6Address () {
198
Enumeration e = ipv6Addresses();
199
if (!e.hasMoreElements()) {
200
return null;
201
}
202
return (Inet6Address)e.nextElement();
203
}
204
205
public static Enumeration ipv6Addresses() {
206
return new AddrEnum (Inet6Address.class);
207
}
208
209
/* enumerates the Inet4Addresses or Inet6Addresses on the system */
210
211
private static class AddrEnum implements Enumeration {
212
213
Enumeration ifs;
214
NetworkInterface currIf = null;
215
InetAddress nextAddr=null;
216
Enumeration addrs=null;
217
Class filter;
218
219
static final byte[] fe80_loopback = new byte [] {
220
(byte)0xfe,(byte)0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,1
221
};
222
223
AddrEnum (Class filter) {
224
this.filter = filter;
225
try {
226
ifs = NetworkInterface.getNetworkInterfaces();
227
} catch (SocketException e) {}
228
}
229
230
public boolean hasMoreElements () {
231
if (nextAddr == null) {
232
nextAddr = getNext();
233
}
234
return (nextAddr != null);
235
}
236
237
public Object nextElement () {
238
if (!hasMoreElements()) {
239
throw new NoSuchElementException ("no more addresses");
240
}
241
Object next = nextAddr;
242
nextAddr = null;
243
return next;
244
}
245
246
private InetAddress getNext() {
247
while (true) {
248
if (currIf == null) {
249
currIf = getNextIf();
250
if (currIf == null) {
251
return null;
252
}
253
addrs = currIf.getInetAddresses();
254
}
255
while (addrs.hasMoreElements()) {
256
InetAddress addr = (InetAddress) addrs.nextElement();
257
if (filter.isInstance (addr) && !addr.isLoopbackAddress()
258
&& !addr.isAnyLocalAddress()) {
259
if (Arrays.equals (addr.getAddress(), fe80_loopback)) {
260
continue;
261
}
262
return addr;
263
}
264
}
265
currIf = null;
266
}
267
}
268
269
private NetworkInterface getNextIf () {
270
if (ifs != null) {
271
while (ifs.hasMoreElements()) {
272
NetworkInterface nic = (NetworkInterface)ifs.nextElement();
273
try {
274
if (nic.isUp() && !nic.isLoopback())
275
return nic;
276
} catch (SocketException e) {
277
// ignore
278
}
279
}
280
}
281
282
return null;
283
}
284
}
285
286
/**
287
* Throws a RuntimeException if the boolean condition is false
288
*/
289
public static void t_assert (boolean assertion) {
290
if (assertion) {
291
return;
292
}
293
Throwable t = new Throwable();
294
StackTraceElement[] strace = t.getStackTrace();
295
String msg = "Assertion failed at: " + strace[1].toString();
296
throw new RuntimeException (msg);
297
}
298
299
private static void simpleRead (InputStream is, int start) throws Exception {
300
byte b[] = new byte [2];
301
for (int i=start; i<start+100; i++) {
302
int x = is.read (b);
303
if (x == 1) {
304
x += is.read (b,1,1);
305
}
306
if (x!=2) {
307
throw new Exception ("read error");
308
}
309
int r = bytes (b[0], b[1]);
310
if (r != i) {
311
throw new Exception ("read " + r + " expected " +i);
312
}
313
}
314
}
315
316
/* convert signed bytes to unisigned int */
317
private static int bytes (byte b1, byte b2) {
318
int i1 = (int)b1 & 0xFF;
319
int i2 = (int)b2 & 0xFF;
320
return i1 * 256 + i2;
321
}
322
323
static void simpleWrite (OutputStream os, int start) throws Exception {
324
byte b[] = new byte [2];
325
for (int i=start; i<start+100; i++) {
326
b[0] = (byte) (i / 256);
327
b[1] = (byte) (i % 256);
328
os.write (b);
329
}
330
}
331
332
private static class Runner extends Thread {
333
Runnable runnee;
334
long delay;
335
336
Runner (Runnable runnee, long delay) {
337
super();
338
this.runnee = runnee;
339
this.delay = delay;
340
}
341
342
public void run () {
343
try {
344
Thread.sleep (delay);
345
runnee.run ();
346
} catch (Exception e) {
347
e.printStackTrace();
348
}
349
}
350
}
351
352
/*
353
* Take the given Runnable and run it in a spawned thread
354
* after the given time has elapsed. runAfter() returns immediately
355
*/
356
public static void runAfter (long millis, Runnable runnee) {
357
Runner runner = new Runner (runnee, millis);
358
runner.start ();
359
}
360
361
static String osname;
362
363
static {
364
osname = System.getProperty ("os.name");
365
}
366
367
static boolean isLinux () {
368
return osname.equals ("Linux");
369
}
370
371
static boolean isWindows () {
372
return osname.startsWith ("Windows");
373
}
374
}
375
376