Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/ipv6tests/UdpTest.java
41152 views
1
/*
2
* Copyright (c) 2003, 2019, 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
/*
25
* @test
26
* @bug 4868820
27
* @key intermittent
28
* @summary IPv6 support for Windows XP and 2003 server.
29
* This test requires binding to the wildcard address and as such
30
* may fail intermittently on some platforms.
31
* @library /test/lib
32
* @build jdk.test.lib.NetworkConfiguration
33
* jdk.test.lib.Platform
34
* @run main UdpTest -d
35
*/
36
37
import java.net.DatagramPacket;
38
import java.net.DatagramSocket;
39
import java.net.Inet4Address;
40
import java.net.Inet6Address;
41
import java.net.InetAddress;
42
import java.net.PortUnreachableException;
43
import java.net.SocketTimeoutException;
44
45
public class UdpTest extends Tests {
46
static DatagramSocket c3, s1, s2, s3;
47
static InetAddress s1peer, s2peer;
48
49
static InetAddress ia4any;
50
static InetAddress ia6any;
51
static Inet6Address ia6addr;
52
static InetAddress ia6bad; /* a global 6to4 IPv6 address, which cant be connected to */
53
static InetAddress ia6rem1;
54
static Inet4Address ia4addr;
55
56
static {
57
try {
58
ia4any = InetAddress.getByName ("0.0.0.0");
59
ia6any = InetAddress.getByName ("::0");
60
try {
61
ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566%net0");
62
} catch (Exception e) {
63
ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566");
64
}
65
//ia6rem1 = InetAddress.getByName ("fe80::a00:20ff:feed:b08d%eth0");
66
//ia6rem1 = InetAddress.getByName ("129.156.220.63");
67
} catch (Exception e) {
68
e.printStackTrace();
69
}
70
ia6addr = getFirstLocalIPv6Address ();
71
ia4addr = getFirstLocalIPv4Address ();
72
}
73
74
public static void main (String[] args) throws Exception {
75
checkDebug(args);
76
if (ia4addr == null) {
77
System.out.println ("No local IPv4 addresses: exiting now");
78
return;
79
}
80
if (ia6addr == null) {
81
System.out.println ("No local IPv6 addresses: exiting now");
82
return;
83
}
84
dprintln ("Local Addresses");
85
dprintln (ia4addr.toString());
86
dprintln (ia6addr.toString());
87
test1 ();
88
test2 ();
89
if (!isLinux()) {
90
test3 ();
91
}
92
test4 ();
93
}
94
95
/* basic UDP connectivity test using IPv6 only and IPv4/IPv6 together */
96
97
static void test1 () throws Exception {
98
System.out.println("Test1 starting");
99
s1 = new DatagramSocket ();
100
s2 = new DatagramSocket ();
101
simpleDataExchange (s1, ia4addr, s2, ia4addr);
102
s1.close (); s2.close ();
103
104
/* IPv6 */
105
s1 = new DatagramSocket ();
106
s2 = new DatagramSocket ();
107
simpleDataExchange (s1, ia6addr, s2, ia6addr);
108
s1.close (); s2.close ();
109
110
/* IPv6 only */
111
s1 = new DatagramSocket (0, ia6addr);
112
s2 = new DatagramSocket (0, ia6addr);
113
simpleDataExchange (s1, ia6addr, s2, ia6addr);
114
s1.close (); s2.close ();
115
116
/* IPv6 and IPv4 */
117
s1 = new DatagramSocket ();
118
s2 = new DatagramSocket ();
119
simpleDataExchange (s1, ia6addr, s2, ia4addr);
120
s1.close (); s2.close ();
121
122
/* listen on anyaddr and check receive from IPv4 and IPv6 */
123
124
s1 = new DatagramSocket ();
125
s2 = new DatagramSocket (0, ia6addr);
126
s3 = new DatagramSocket (0, ia4addr);
127
datagramEcho (s2, s1, ia6addr);
128
datagramEcho (s3, s1, ia4addr);
129
s1.close (); s2.close (); s3.close();
130
131
System.out.println ("Test1: OK");
132
}
133
134
/* check timeouts on receive */
135
136
static void test2 () throws Exception {
137
System.out.println("Test2 starting");
138
s1 = new DatagramSocket ();
139
s2 = new DatagramSocket ();
140
s1.setSoTimeout (4000);
141
long t1 = System.currentTimeMillis();
142
try {
143
s1.receive (new DatagramPacket (new byte [128], 128));
144
throw new Exception ("expected receive timeout ");
145
} catch (SocketTimeoutException e) {
146
}
147
checkTime (System.currentTimeMillis() - t1, 4000);
148
149
/* check data can be exchanged now */
150
151
simpleDataExchange (s1, ia6addr, s2, ia4addr);
152
153
/* double check timeout still works */
154
t1 = System.currentTimeMillis();
155
try {
156
s1.receive (new DatagramPacket (new byte [128], 128));
157
throw new Exception ("expected receive timeout ");
158
} catch (SocketTimeoutException e) {
159
}
160
checkTime (System.currentTimeMillis() - t1, 4000);
161
162
/* check receive works after a delay < timeout */
163
164
final DatagramSocket s = s2;
165
final InetAddress ia6 = ia6addr;
166
final int port = s1.getLocalPort();
167
168
s1.setSoTimeout(10000);
169
runAfter (2000, new Runnable () {
170
public void run () {
171
try {
172
DatagramPacket p = new DatagramPacket ("Hello 123".getBytes(), 0, 8, ia6, port);
173
s.send (p);
174
} catch (Exception e) {}
175
}
176
});
177
t1 = System.currentTimeMillis();
178
s1.receive (new DatagramPacket (new byte [128], 128));
179
checkTime (System.currentTimeMillis() - t1, 2000, 10000);
180
s1.close ();
181
s2.close ();
182
System.out.println ("Test2: OK");
183
}
184
185
/* check connected sockets */
186
187
static void test3 () throws Exception {
188
System.out.println("Test3 starting");
189
s1 = new DatagramSocket ();
190
s2 = new DatagramSocket ();
191
s1.connect (ia6addr, s2.getLocalPort());
192
datagramEcho (s1, s2, null);
193
s1.close (); s2.close();
194
System.out.println ("Test3: OK");
195
}
196
197
/* check PortUnreachable */
198
199
static void test4 () throws Exception {
200
System.out.println("Test4 starting");
201
s1 = new DatagramSocket ();
202
s1.connect (ia6addr, 5000);
203
s1.setSoTimeout (3000);
204
try {
205
DatagramPacket p = new DatagramPacket ("HelloWorld".getBytes(), "HelloWorld".length());
206
s1.send (p);
207
p = new DatagramPacket (new byte[128], 128);
208
s1.receive (p);
209
} catch (PortUnreachableException e) {
210
System.out.println ("Test4: OK");
211
return;
212
} catch (SocketTimeoutException e) {
213
System.out.println ("Test4: failed. Never mind, it's an OS bug");
214
}
215
}
216
217
}
218
219