Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/etc/ProtocolFamilies.java
41153 views
1
/*
2
* Copyright (c) 2020, 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 jdk.test.lib.NetworkConfiguration;
25
import jdk.test.lib.net.IPSupport;
26
import org.testng.annotations.BeforeTest;
27
import org.testng.annotations.DataProvider;
28
import org.testng.annotations.Test;
29
import org.testng.Assert.ThrowingRunnable;
30
import java.io.IOException;
31
import java.net.*;
32
import java.nio.channels.*;
33
import java.nio.channels.spi.SelectorProvider;
34
import static java.lang.System.out;
35
import static java.net.StandardProtocolFamily.INET;
36
import static java.net.StandardProtocolFamily.INET6;
37
import static jdk.test.lib.net.IPSupport.*;
38
import static org.testng.Assert.assertEquals;
39
import static org.testng.Assert.assertThrows;
40
41
/*
42
* @test
43
* @summary Test SocketChannel, ServerSocketChannel and DatagramChannel
44
* with various ProtocolFamily combinations
45
* @library /test/lib
46
* @build jdk.test.lib.NetworkConfiguration
47
* @run testng ProtocolFamilies
48
* @run testng/othervm -Djava.net.preferIPv4Stack=true ProtocolFamilies
49
*/
50
51
52
public class ProtocolFamilies {
53
static final boolean hasIPv6 = hasIPv6();
54
static final boolean preferIPv4 = preferIPv4Stack();
55
static Inet4Address ia4;
56
static Inet6Address ia6;
57
58
@BeforeTest()
59
public void setup() throws Exception {
60
NetworkConfiguration.printSystemConfiguration(out);
61
IPSupport.printPlatformSupport(out);
62
throwSkippedExceptionIfNonOperational();
63
64
ia4 = getLocalIPv4Address();
65
ia6 = getLocalIPv6Address();
66
out.println("ia4: " + ia4);
67
out.println("ia6: " + ia6 + "\n");
68
}
69
70
static final Class<UnsupportedAddressTypeException> UATE = UnsupportedAddressTypeException.class;
71
static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
72
73
@DataProvider(name = "open")
74
public Object[][] open() {
75
if (hasIPv6 && !preferIPv4) {
76
return new Object[][]{
77
{ INET, null },
78
{ INET6, null }
79
};
80
} else {
81
return new Object[][]{
82
{ INET, null },
83
{ INET6, UOE }
84
};
85
}
86
}
87
88
@Test(dataProvider = "open")
89
public void scOpen(StandardProtocolFamily family,
90
Class<? extends Exception> expectedException)
91
throws Throwable
92
{
93
SocketChannel sc = null;
94
try {
95
if (expectedException == UOE) {
96
try {
97
sc = openSC(family);
98
} catch (UnsupportedOperationException e) {}
99
} else {
100
sc = openSC(family);
101
}
102
} finally {
103
if (sc != null)
104
sc.close();
105
}
106
}
107
108
@Test(dataProvider = "open")
109
public void sscOpen(StandardProtocolFamily family,
110
Class<? extends Exception> expectedException)
111
throws Throwable
112
{
113
ServerSocketChannel ssc = null;
114
try {
115
if (expectedException == UOE) {
116
try {
117
ssc = openSSC(family);
118
} catch (UnsupportedOperationException e) {}
119
} else {
120
openSSC(family);
121
}
122
} finally {
123
if (ssc != null)
124
ssc.close();
125
}
126
}
127
128
@Test(dataProvider = "open")
129
public void dcOpen(StandardProtocolFamily family,
130
Class<? extends Exception> expectedException)
131
throws Throwable
132
{
133
DatagramChannel dc = null;
134
try {
135
if (expectedException == UOE) {
136
try {
137
dc = openDC(family);
138
} catch (UnsupportedOperationException e) {}
139
} else {
140
openDC(family);
141
}
142
} finally {
143
if (dc != null)
144
dc.close();
145
}
146
}
147
148
@DataProvider(name = "openBind")
149
public Object[][] openBind() {
150
if (hasIPv6 && !preferIPv4) {
151
return new Object[][]{
152
{ INET, INET, null },
153
{ INET, INET6, UATE },
154
{ INET, null, null },
155
{ INET6, INET, null },
156
{ INET6, INET6, null },
157
{ INET6, null, null },
158
{ null, INET, null },
159
{ null, INET6, null },
160
{ null, null, null }
161
};
162
} else {
163
return new Object[][]{
164
{ INET, INET, null },
165
{ INET, INET6, UATE },
166
{ INET, null, null },
167
{ null, INET, null },
168
{ null, INET6, UATE },
169
{ null, null, null }
170
};
171
}
172
}
173
174
// SocketChannel open - INET, INET6, default
175
// SocketChannel bind - INET, INET6, null
176
177
@Test(dataProvider = "openBind")
178
public void scOpenBind(StandardProtocolFamily ofamily,
179
StandardProtocolFamily bfamily,
180
Class<? extends Exception> expectedException)
181
throws Throwable
182
{
183
try (SocketChannel sc = openSC(ofamily)) {
184
SocketAddress addr = getSocketAddress(bfamily);
185
ThrowingRunnable bindOp = () -> sc.bind(addr);
186
if (expectedException == null)
187
bindOp.run();
188
else
189
assertThrows(expectedException, bindOp);
190
}
191
}
192
193
// ServerSocketChannel open - INET, INET6, default
194
// ServerSocketChannel bind - INET, INET6, null
195
196
@Test(dataProvider = "openBind")
197
public void sscOpenBind(StandardProtocolFamily ofamily,
198
StandardProtocolFamily bfamily,
199
Class<? extends Exception> expectedException)
200
throws Throwable
201
{
202
try (ServerSocketChannel ssc = openSSC(ofamily)) {
203
SocketAddress addr = getSocketAddress(bfamily);
204
ThrowingRunnable bindOp = () -> ssc.bind(addr);
205
if (expectedException == null)
206
bindOp.run();
207
else
208
assertThrows(expectedException, bindOp);
209
}
210
}
211
212
// DatagramChannel open - INET, INET6, default
213
// DatagramChannel bind - INET, INET6, null
214
215
@Test(dataProvider = "openBind")
216
public void dcOpenBind(StandardProtocolFamily ofamily,
217
StandardProtocolFamily bfamily,
218
Class<? extends Exception> expectedException)
219
throws Throwable
220
{
221
try (DatagramChannel dc = openDC(ofamily)) {
222
SocketAddress addr = getSocketAddress(bfamily);
223
ThrowingRunnable bindOp = () -> dc.bind(addr);
224
if (expectedException == null)
225
bindOp.run();
226
else
227
assertThrows(expectedException, bindOp);
228
}
229
}
230
231
// SocketChannel open - INET, INET6, default
232
// SocketChannel connect - INET, INET6, default
233
234
@DataProvider(name = "openConnect")
235
public Object[][] openConnect() {
236
if (hasIPv6 && !preferIPv4) {
237
return new Object[][]{
238
{ INET, INET, null },
239
{ INET, INET6, null },
240
{ INET, null, null },
241
{ INET6, INET, UATE },
242
{ INET6, INET6, null },
243
{ INET6, null, null },
244
{ null, INET, UATE },
245
{ null, INET6, null },
246
{ null, null, null }
247
};
248
} else {
249
// INET6 channels cannot be created - UOE - tested elsewhere
250
return new Object[][]{
251
{ INET, INET, null },
252
{ INET, null, null },
253
{ null, INET, null },
254
{ null, null, null }
255
};
256
}
257
}
258
259
@Test(dataProvider = "openConnect")
260
public void scOpenConnect(StandardProtocolFamily sfamily,
261
StandardProtocolFamily cfamily,
262
Class<? extends Exception> expectedException)
263
throws Throwable
264
{
265
try (ServerSocketChannel ssc = openSSC(sfamily)) {
266
ssc.bind(null);
267
SocketAddress saddr = ssc.getLocalAddress();
268
try (SocketChannel sc = openSC(cfamily)) {
269
if (expectedException == null)
270
sc.connect(saddr);
271
else
272
assertThrows(expectedException, () -> sc.connect(saddr));
273
}
274
}
275
}
276
277
static final Class<NullPointerException> NPE = NullPointerException.class;
278
279
// Tests null handling
280
@Test
281
public void testNulls() {
282
assertThrows(NPE, () -> SocketChannel.open((ProtocolFamily)null));
283
assertThrows(NPE, () -> ServerSocketChannel.open(null));
284
assertThrows(NPE, () -> DatagramChannel.open(null));
285
286
assertThrows(NPE, () -> SelectorProvider.provider().openSocketChannel(null));
287
assertThrows(NPE, () -> SelectorProvider.provider().openServerSocketChannel(null));
288
assertThrows(NPE, () -> SelectorProvider.provider().openDatagramChannel(null));
289
}
290
291
static final ProtocolFamily BAD_PF = () -> "BAD_PROTOCOL_FAMILY";
292
293
// Tests UOE handling
294
@Test
295
public void testUoe() {
296
assertThrows(UOE, () -> SocketChannel.open(BAD_PF));
297
assertThrows(UOE, () -> ServerSocketChannel.open(BAD_PF));
298
assertThrows(UOE, () -> DatagramChannel.open(BAD_PF));
299
300
assertThrows(UOE, () -> SelectorProvider.provider().openSocketChannel(BAD_PF));
301
assertThrows(UOE, () -> SelectorProvider.provider().openServerSocketChannel(BAD_PF));
302
assertThrows(UOE, () -> SelectorProvider.provider().openDatagramChannel(BAD_PF));
303
}
304
305
// Helper methods
306
307
private static SocketChannel openSC(StandardProtocolFamily family)
308
throws IOException {
309
SocketChannel sc = family == null ? SocketChannel.open()
310
: SocketChannel.open(family);
311
return sc;
312
}
313
314
private static ServerSocketChannel openSSC(StandardProtocolFamily family)
315
throws IOException {
316
ServerSocketChannel ssc = family == null ? ServerSocketChannel.open()
317
: ServerSocketChannel.open(family);
318
return ssc;
319
}
320
321
private static DatagramChannel openDC(StandardProtocolFamily family)
322
throws IOException {
323
DatagramChannel dc = family == null ? DatagramChannel.open()
324
: DatagramChannel.open(family);
325
return dc;
326
}
327
328
private static SocketAddress getSocketAddress(StandardProtocolFamily family) {
329
return family == null ? null : switch (family) {
330
case INET -> new InetSocketAddress(ia4, 0);
331
case INET6 -> new InetSocketAddress(ia6, 0);
332
default -> throw new RuntimeException("Unexpected protocol family");
333
};
334
}
335
336
private static SocketAddress getLoopback(StandardProtocolFamily family, int port)
337
throws UnknownHostException {
338
if ((family == null || family == INET6) && hasIPv6) {
339
return new InetSocketAddress(InetAddress.getByName("::1"), port);
340
} else {
341
return new InetSocketAddress(InetAddress.getByName("127.0.0.1"), port);
342
}
343
}
344
345
private static Inet4Address getLocalIPv4Address()
346
throws Exception {
347
return NetworkConfiguration.probe()
348
.ip4Addresses()
349
.filter(a -> !a.isLoopbackAddress())
350
.findFirst()
351
.orElse((Inet4Address)InetAddress.getByName("0.0.0.0"));
352
}
353
354
private static Inet6Address getLocalIPv6Address()
355
throws Exception {
356
return NetworkConfiguration.probe()
357
.ip6Addresses()
358
.filter(a -> !a.isLoopbackAddress())
359
.findFirst()
360
.orElse((Inet6Address) InetAddress.getByName("::0"));
361
}
362
}
363
364