Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/etc/OpenAndConnect.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
30
import java.io.IOException;
31
import java.net.*;
32
import java.nio.channels.*;
33
import java.util.Arrays;
34
import java.util.List;
35
import java.util.LinkedList;
36
37
import static java.lang.System.getProperty;
38
import static java.lang.System.out;
39
import static java.net.StandardProtocolFamily.INET;
40
import static java.net.StandardProtocolFamily.INET6;
41
import static jdk.test.lib.net.IPSupport.*;
42
43
/*
44
* @test
45
* @summary Test SocketChannel, ServerSocketChannel and DatagramChannel
46
* open() and connect(), taking into consideration combinations of
47
* protocol families (INET, INET6, default),
48
* addresses (Inet4Address, Inet6Address).
49
* @library /test/lib
50
* @build jdk.test.lib.NetworkConfiguration
51
* @run testng/othervm OpenAndConnect
52
*/
53
54
55
public class OpenAndConnect {
56
static final Inet4Address IA4ANYLOCAL;
57
static final Inet6Address IA6ANYLOCAL;
58
static final Inet4Address IA4LOOPBACK;
59
static final Inet6Address IA6LOOPBACK;
60
static Inet4Address IA4LOCAL = null;
61
static Inet6Address IA6LOCAL = null;
62
static InetAddress DONT_BIND;
63
64
static {
65
try {
66
IA4ANYLOCAL = (Inet4Address) InetAddress.getByName("0.0.0.0");
67
IA6ANYLOCAL = (Inet6Address) InetAddress.getByName("::0");
68
IA4LOOPBACK = (Inet4Address) InetAddress.getByName("127.0.0.1");
69
IA6LOOPBACK = (Inet6Address) InetAddress.getByName("::1");
70
71
// Special value to tell test not to call bind (address is not used)
72
DONT_BIND = (Inet4Address) InetAddress.getByName("127.0.0.3");
73
74
initAddrs();
75
} catch (Exception e) {
76
throw new RuntimeException("Could not initialize addresses", e);
77
}
78
}
79
80
@BeforeTest()
81
public void setup() {
82
NetworkConfiguration.printSystemConfiguration(out);
83
IPSupport.printPlatformSupport(out);
84
throwSkippedExceptionIfNonOperational();
85
86
out.println("IA4LOCAL: " + IA4LOCAL);
87
out.println("IA6LOCAL: " + IA6LOCAL);
88
out.println("IA4ANYLOCAL: " + IA4ANYLOCAL);
89
out.println("IA6ANYLOCAL: " + IA6ANYLOCAL);
90
out.println("IA4LOOPBACK: " + IA4LOOPBACK);
91
out.println("IA6LOOPBACK: " + IA6LOOPBACK);
92
}
93
94
@DataProvider(name = "openConnect")
95
public Object[][] openConnect() {
96
LinkedList<Object[]> l = new LinkedList<>();
97
if (IPSupport.hasIPv4()) {
98
l.addAll(openConnectV4Tests);
99
if (IA4LOCAL != null) {
100
l.addAll(openConnectV4LocalTests);
101
}
102
}
103
if (IPSupport.hasIPv6()) {
104
l.addAll(openConnectV6Tests);
105
if (IA6LOCAL != null) {
106
l.addAll(openConnectV6LocalTests);
107
}
108
}
109
if (IPSupport.hasIPv4() && IPSupport.hasIPv6()) {
110
l.addAll(openConnectV4AndV6Tests);
111
if (IA4LOCAL != null) {
112
l.addAll(openConnectV4LocalAndV6Tests);
113
}
114
}
115
return l.toArray(new Object[][]{});
116
}
117
118
// +----- sfam is server/first socket family
119
// |
120
// | +------ saddr is bind address for server/first socket
121
// | |
122
// | | +---- cfam is family for client/second socket
123
// | | |
124
// | | | +---- caddr is address client/second
125
// | | | | socket binds to. When the server
126
// | | | | has bound to a wildcard address
127
// | | | | this is address used for connect
128
// | | | | also.
129
// | | | |
130
// | | | |
131
// | | | |
132
// | | | |
133
// + + + +
134
// { sfam, saddr, cfam, caddr, }
135
136
// Basic tests for when an IPv4 is available
137
public static List<Object[]> openConnectV4Tests =
138
Arrays.asList(new Object[][] {
139
{ INET, IA4LOOPBACK, INET, IA4LOOPBACK },
140
{ INET, IA4LOOPBACK, null, IA4LOOPBACK },
141
{ INET, IA4ANYLOCAL, null, IA4LOOPBACK },
142
{ INET, IA4ANYLOCAL, INET, IA4LOOPBACK },
143
{ null, IA4LOOPBACK, INET, IA4ANYLOCAL },
144
{ null, IA4LOOPBACK, INET, IA4LOOPBACK },
145
{ null, IA4LOOPBACK, INET, null },
146
{ null, IA4LOOPBACK, null, null }
147
});
148
149
// Additional tests for when an IPv4 local address is available
150
public List<Object[]> openConnectV4LocalTests =
151
Arrays.asList(new Object[][] {
152
{ INET, IA4LOCAL, INET, IA4LOCAL },
153
{ INET, IA4LOCAL, null, IA4LOCAL },
154
{ INET, IA4LOCAL, null, DONT_BIND },
155
{ INET, IA4ANYLOCAL, INET, IA4LOCAL },
156
{ INET, IA4ANYLOCAL, null, IA4LOCAL },
157
{ null, IA4LOCAL, INET, IA4ANYLOCAL },
158
{ null, IA4LOCAL, INET, IA4LOCAL },
159
{ null, IA4LOCAL, INET, null },
160
{ null, IA4LOCAL, null, null }
161
});
162
163
// Basic tests for when an IPv6 is available
164
public List<Object[]> openConnectV6Tests =
165
Arrays.asList(new Object[][] {
166
{ INET6, IA6ANYLOCAL, null, IA6LOOPBACK },
167
{ INET6, IA6ANYLOCAL, INET6, IA6LOOPBACK },
168
{ INET6, IA6LOOPBACK, INET6, IA6LOOPBACK },
169
{ INET6, IA6LOOPBACK, INET6, IA6LOOPBACK },
170
{ null, IA6ANYLOCAL, null, IA6LOOPBACK },
171
{ null, IA6ANYLOCAL, INET6, IA6LOOPBACK },
172
{ null, IA6LOOPBACK, INET6, IA6LOOPBACK },
173
{ null, IA6LOOPBACK, INET6, DONT_BIND },
174
{ null, IA6LOOPBACK, INET6, null },
175
{ null, IA6LOOPBACK, null, IA6LOOPBACK },
176
{ null, IA6LOOPBACK, null, null },
177
{ null, IA6LOOPBACK, INET6, IA6ANYLOCAL },
178
{ null, IA6LOOPBACK, null, IA6ANYLOCAL }
179
});
180
181
// Additional tests for when an IPv6 local address is available
182
public List<Object[]> openConnectV6LocalTests =
183
Arrays.asList(new Object[][] {
184
{ INET6, IA6ANYLOCAL, null, IA6LOCAL },
185
{ INET6, IA6ANYLOCAL, INET6, IA6LOCAL },
186
{ INET6, IA6LOCAL, INET6, IA6LOCAL },
187
{ INET6, IA6LOCAL, null, IA6LOCAL },
188
{ INET6, IA6LOCAL, null, DONT_BIND },
189
{ INET6, IA6LOCAL, INET6, IA6LOCAL },
190
{ null, IA6ANYLOCAL, null, IA6LOCAL },
191
{ null, IA6ANYLOCAL, INET6, IA6LOCAL },
192
{ null, IA6LOCAL, INET6, IA6LOCAL },
193
{ null, IA6LOCAL, INET6, IA6ANYLOCAL },
194
{ null, IA6LOCAL, null, IA6ANYLOCAL },
195
{ null, IA6LOCAL, null, IA6LOCAL },
196
{ null, IA6LOCAL, INET6, null },
197
{ null, IA6LOCAL, null, null }
198
});
199
200
// Additional tests for when IPv4 and IPv6 are available
201
public List<Object[]> openConnectV4AndV6Tests =
202
Arrays.asList(new Object[][] {
203
{ null, IA4LOOPBACK, INET6, IA6ANYLOCAL },
204
{ null, IA4LOOPBACK, null, IA6ANYLOCAL },
205
{ null, IA4LOOPBACK, INET6, DONT_BIND },
206
{ null, IA4LOOPBACK, INET6, null }
207
});
208
209
// Additional tests for when IPv4 local address and IPv6 are available
210
public List<Object[]> openConnectV4LocalAndV6Tests =
211
Arrays.asList(new Object[][] {
212
{ null, IA4LOCAL, INET6, IA6ANYLOCAL },
213
{ null, IA4LOCAL, INET6, null },
214
{ null, IA4LOCAL, null, IA6ANYLOCAL }
215
});
216
217
/**
218
* If the destination address is the wildcard, it is replaced by the alternate
219
* using the port number from destination. Otherwise destination is returned.
220
* Only used by dcOpenAndConnect
221
*/
222
static InetSocketAddress getDestinationAddress(SocketAddress destination, InetAddress alternate) {
223
InetSocketAddress isa = (InetSocketAddress)destination;
224
if (isa.getAddress().isAnyLocalAddress())
225
return new InetSocketAddress(alternate, isa.getPort());
226
else
227
return isa;
228
}
229
230
@Test(dataProvider = "openConnect")
231
public void scOpenAndConnect(ProtocolFamily sfam,
232
InetAddress saddr,
233
ProtocolFamily cfam,
234
InetAddress caddr) throws IOException
235
{
236
out.printf("scOpenAndConnect: server bind: %s client bind: %s\n", saddr, caddr);
237
try (ServerSocketChannel ssc = openSSC(sfam)) {
238
ssc.bind(getSocketAddress(saddr));
239
InetSocketAddress ssa = (InetSocketAddress)ssc.getLocalAddress();
240
ssa = getDestinationAddress(ssa, caddr);
241
out.println(ssa);
242
try (SocketChannel csc = openSC(cfam)) {
243
if (caddr != DONT_BIND) {
244
csc.bind(getSocketAddress(caddr));
245
}
246
csc.connect(ssa);
247
}
248
}
249
}
250
251
@Test(dataProvider = "openConnect")
252
public void dcOpenAndConnect(ProtocolFamily sfam,
253
InetAddress saddr,
254
ProtocolFamily cfam,
255
InetAddress caddr) throws IOException
256
{
257
try (DatagramChannel sdc = openDC(sfam)) {
258
sdc.bind(getSocketAddress(saddr));
259
SocketAddress ssa = sdc.socket().getLocalSocketAddress();
260
ssa = getDestinationAddress(ssa, caddr);
261
out.println(ssa);
262
try (DatagramChannel dc = openDC(cfam)) {
263
if (caddr != DONT_BIND) {
264
dc.bind(getSocketAddress(caddr));
265
}
266
dc.connect(ssa);
267
}
268
}
269
}
270
271
// Helper methods
272
273
private static SocketChannel openSC(ProtocolFamily fam) throws IOException {
274
return fam == null ? SocketChannel.open() : SocketChannel.open(fam);
275
}
276
277
private static ServerSocketChannel openSSC(ProtocolFamily fam)
278
throws IOException {
279
return fam == null ? ServerSocketChannel.open()
280
: ServerSocketChannel.open(fam);
281
}
282
283
private static DatagramChannel openDC(ProtocolFamily fam)
284
throws IOException {
285
return fam == null ? DatagramChannel.open()
286
: DatagramChannel.open(fam);
287
}
288
289
private static SocketAddress getSocketAddress(InetAddress ia) {
290
return ia == null ? null : new InetSocketAddress(ia, 0);
291
}
292
293
private static void initAddrs() throws IOException {
294
295
NetworkConfiguration cfg = NetworkConfiguration.probe();
296
297
IA4LOCAL = cfg.ip4Addresses()
298
.filter(a -> !a.isLoopbackAddress())
299
.findFirst()
300
.orElse(null);
301
302
IA6LOCAL = cfg.ip6Addresses()
303
.filter(a -> !a.isLoopbackAddress())
304
.findFirst()
305
.orElse(null);
306
}
307
}
308
309