Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/SocketOption/OptionsTest.java
41149 views
1
/*
2
* Copyright (c) 2014, 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 8036979 8072384 8044773 8225214 8233296 8234083
27
* @library /test/lib
28
* @requires !vm.graal.enabled
29
* @run main/othervm -Xcheck:jni OptionsTest
30
* @run main/othervm -Djdk.net.usePlainSocketImpl OptionsTest
31
* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl OptionsTest
32
* @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true OptionsTest
33
* @run main/othervm --limit-modules=java.base OptionsTest
34
* @run main/othervm/policy=options.policy OptionsTest
35
*/
36
37
import java.lang.reflect.Method;
38
import java.net.*;
39
import java.util.*;
40
import jdk.test.lib.net.IPSupport;
41
42
public class OptionsTest {
43
44
static class Test<T> {
45
final SocketOption<T> option;
46
final T value;
47
Test(SocketOption<T> option, T value) {
48
this.option = option;
49
this.value = value;
50
}
51
static <T> Test<T> create(SocketOption<T> option, T value) {
52
return new Test<T>(option, value);
53
}
54
55
}
56
57
// The tests set the option using the new API, read back the set value
58
// which could be different, and then use the legacy get API to check
59
// these values are the same
60
61
static Test<?>[] socketTests = new Test<?>[] {
62
Test.create(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE),
63
Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
64
Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
65
Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
66
Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
67
Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(-1)),
68
Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(0)),
69
Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(80)),
70
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(0)), // lower-bound
71
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)),
72
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(255)) //upper-bound
73
};
74
75
static Test<?>[] serverSocketTests = new Test<?>[] {
76
Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
77
Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
78
Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
79
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(0)), // lower-bound
80
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)),
81
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(255)) //upper-bound
82
};
83
84
static Test<?>[] datagramSocketTests = new Test<?>[] {
85
Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
86
Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
87
Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
88
Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
89
Test.create(StandardSocketOptions.SO_BROADCAST, Boolean.FALSE),
90
Test.create(StandardSocketOptions.SO_BROADCAST, Boolean.TRUE),
91
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(0)), // lower-bound
92
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)),
93
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(255)) //upper-bound
94
};
95
96
static Test<?>[] multicastSocketTests = new Test<?>[] {
97
Test.create(StandardSocketOptions.SO_BROADCAST, Boolean.FALSE),
98
Test.create(StandardSocketOptions.SO_BROADCAST, Boolean.TRUE),
99
Test.create(StandardSocketOptions.IP_MULTICAST_IF, getNetworkInterface()),
100
Test.create(StandardSocketOptions.IP_MULTICAST_TTL, Integer.valueOf(0)), // lower-bound
101
Test.create(StandardSocketOptions.IP_MULTICAST_TTL, Integer.valueOf(10)),
102
Test.create(StandardSocketOptions.IP_MULTICAST_TTL, Integer.valueOf(255)), //upper-bound
103
Test.create(StandardSocketOptions.IP_MULTICAST_LOOP, Boolean.TRUE)
104
};
105
106
static NetworkInterface getNetworkInterface() {
107
try {
108
Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
109
while (nifs.hasMoreElements()) {
110
NetworkInterface ni = nifs.nextElement();
111
if (ni.supportsMulticast()) {
112
return ni;
113
}
114
}
115
} catch (Exception e) {
116
}
117
return null;
118
}
119
120
static boolean okayToTest(Socket s, SocketOption<?> option) {
121
if (option == StandardSocketOptions.SO_REUSEPORT) {
122
// skip SO_REUSEPORT if option is not supported
123
return s.supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT);
124
}
125
if (option == StandardSocketOptions.IP_TOS && s.isConnected()) {
126
// skip IP_TOS if connected
127
return false;
128
}
129
return true;
130
}
131
132
static <T> void testEqual(SocketOption<T> option, T value1, T value2) {
133
if (!value1.equals(value2)) {
134
throw new RuntimeException("Test of " + option.name() + " failed: "
135
+ value1 + " != " + value2);
136
}
137
}
138
139
static <T> void test(Socket s, Test<T> test) throws Exception {
140
SocketOption<T> option = test.option;
141
s.setOption(option, test.value);
142
T value1 = s.getOption(test.option);
143
T value2 = (T) legacyGetOption(Socket.class, s, test.option);
144
testEqual(option, value1, value2);
145
}
146
147
static <T> void test(ServerSocket ss, Test<T> test) throws Exception {
148
SocketOption<T> option = test.option;
149
ss.setOption(option, test.value);
150
T value1 = ss.getOption(test.option);
151
T value2 = (T) legacyGetOption(ServerSocket.class, ss, test.option);
152
testEqual(option, value1, value2);
153
}
154
155
static <T> void test(DatagramSocket ds, Test<T> test) throws Exception {
156
SocketOption<T> option = test.option;
157
ds.setOption(option, test.value);
158
T value1 = ds.getOption(test.option);
159
T value2 = (T) legacyGetOption(ds.getClass(), ds, test.option);
160
testEqual(option, value1, value2);
161
}
162
163
// Tests default and negative values of SO_LINGER. All negative values should
164
// retrieve as -1.
165
static void testSoLingerValues() throws Exception {
166
try (Socket s = new Socket()) {
167
// retrieve without set
168
int defaultValue = s.getOption(StandardSocketOptions.SO_LINGER);
169
testEqual(StandardSocketOptions.SO_LINGER, -1, defaultValue);
170
171
for (int v : List.of(-1, -2, -100, -65534, -65535, -65536, -100000)) {
172
System.out.println("Testing SO_LINGER with:" + v);
173
s.setOption(StandardSocketOptions.SO_LINGER, v);
174
int value = s.getOption(StandardSocketOptions.SO_LINGER);
175
testEqual(StandardSocketOptions.SO_LINGER, -1, value);
176
}
177
}
178
}
179
180
@SuppressWarnings("try")
181
static void doSocketTests() throws Exception {
182
// unconnected socket
183
try (Socket s = new Socket()) {
184
for (Test<?> test : socketTests) {
185
if (okayToTest(s, test.option)) {
186
test(s, test);
187
}
188
}
189
}
190
191
// connected socket
192
try (ServerSocket ss = new ServerSocket()) {
193
var loopback = InetAddress.getLoopbackAddress();
194
ss.bind(new InetSocketAddress(loopback, 0));
195
try (Socket s1 = new Socket()) {
196
s1.connect(ss.getLocalSocketAddress());
197
try (Socket s2 = ss.accept()) {
198
for (Test<?> test : socketTests) {
199
if (okayToTest(s1, test.option)) {
200
test(s1, test);
201
}
202
}
203
}
204
}
205
}
206
207
testSoLingerValues();
208
}
209
210
static void doServerSocketTests() throws Exception {
211
try (ServerSocket ss = new ServerSocket(0)) {
212
Set<SocketOption<?>> options = ss.supportedOptions();
213
boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
214
for (Test<?> test : serverSocketTests) {
215
if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
216
test(ss, test);
217
}
218
}
219
}
220
}
221
222
static void doDatagramSocketTests() throws Exception {
223
try (DatagramSocket ds = new DatagramSocket(0)) {
224
Set<SocketOption<?>> options = ds.supportedOptions();
225
boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
226
for (Test<?> test : datagramSocketTests) {
227
if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
228
test(ds, test);
229
}
230
}
231
}
232
}
233
234
static void doMulticastSocketTests() throws Exception {
235
try (MulticastSocket ms = new MulticastSocket(0)) {
236
for (Test<?> test : multicastSocketTests) {
237
test(ms, test);
238
}
239
}
240
}
241
242
static Object legacyGetOption(Class<?> type, Object s, Object option) throws Exception {
243
if (type.equals(Socket.class)) {
244
Socket socket = (Socket)s;
245
Set<SocketOption<?>> options = socket.supportedOptions();
246
boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
247
248
if (option.equals(StandardSocketOptions.SO_KEEPALIVE)) {
249
return Boolean.valueOf(socket.getKeepAlive());
250
} else if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
251
return Integer.valueOf(socket.getSendBufferSize());
252
} else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
253
return Integer.valueOf(socket.getReceiveBufferSize());
254
} else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
255
return Boolean.valueOf(socket.getReuseAddress());
256
} else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
257
return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
258
} else if (option.equals(StandardSocketOptions.SO_LINGER)) {
259
return Integer.valueOf(socket.getSoLinger());
260
} else if (option.equals(StandardSocketOptions.IP_TOS)) {
261
return Integer.valueOf(socket.getTrafficClass());
262
} else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
263
return Boolean.valueOf(socket.getTcpNoDelay());
264
} else {
265
throw new RuntimeException("unexpected socket option");
266
}
267
} else if (type.equals(ServerSocket.class)) {
268
ServerSocket socket = (ServerSocket)s;
269
Set<SocketOption<?>> options = socket.supportedOptions();
270
boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
271
272
if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
273
return Integer.valueOf(socket.getReceiveBufferSize());
274
} else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
275
return Boolean.valueOf(socket.getReuseAddress());
276
} else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
277
return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
278
} else if (option.equals(StandardSocketOptions.IP_TOS)) {
279
return getServerSocketTrafficClass(socket);
280
} else {
281
throw new RuntimeException("unexpected socket option");
282
}
283
} else if (type.equals(DatagramSocket.class)) {
284
DatagramSocket socket = (DatagramSocket)s;
285
Set<SocketOption<?>> options = socket.supportedOptions();
286
boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
287
288
if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
289
return Integer.valueOf(socket.getSendBufferSize());
290
} else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
291
return Integer.valueOf(socket.getReceiveBufferSize());
292
} else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
293
return Boolean.valueOf(socket.getReuseAddress());
294
} else if (option.equals(StandardSocketOptions.SO_BROADCAST)) {
295
return Boolean.valueOf(socket.getBroadcast());
296
} else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
297
return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
298
} else if (option.equals(StandardSocketOptions.IP_TOS)) {
299
return Integer.valueOf(socket.getTrafficClass());
300
} else {
301
throw new RuntimeException("unexpected socket option");
302
}
303
304
} else if (type.equals(MulticastSocket.class)) {
305
MulticastSocket socket = (MulticastSocket)s;
306
Set<SocketOption<?>> options = socket.supportedOptions();
307
boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
308
309
if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
310
return Integer.valueOf(socket.getSendBufferSize());
311
} else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
312
return Integer.valueOf(socket.getReceiveBufferSize());
313
} else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
314
return Boolean.valueOf(socket.getReuseAddress());
315
} else if (option.equals(StandardSocketOptions.SO_BROADCAST)) {
316
return Boolean.valueOf(socket.getBroadcast());
317
} else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
318
return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
319
} else if (option.equals(StandardSocketOptions.IP_TOS)) {
320
return Integer.valueOf(socket.getTrafficClass());
321
} else if (option.equals(StandardSocketOptions.IP_MULTICAST_IF)) {
322
return socket.getNetworkInterface();
323
} else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) {
324
return Integer.valueOf(socket.getTimeToLive());
325
} else if (option.equals(StandardSocketOptions.IP_MULTICAST_LOOP)) {
326
return !Boolean.valueOf(socket.getLoopbackMode());
327
} else {
328
throw new RuntimeException("unexpected socket option");
329
}
330
}
331
throw new RuntimeException("unexpected socket type");
332
}
333
334
public static void main(String args[]) throws Exception {
335
IPSupport.throwSkippedExceptionIfNonOperational();
336
doSocketTests();
337
doServerSocketTests();
338
doDatagramSocketTests();
339
doMulticastSocketTests();
340
}
341
342
// Reflectively access jdk.net.Sockets.getOption so that the test can run
343
// without the jdk.net module.
344
static Object getServerSocketTrafficClass(ServerSocket ss) throws Exception {
345
try {
346
Class<?> c = Class.forName("jdk.net.Sockets");
347
Method m = c.getMethod("getOption", ServerSocket.class, SocketOption.class);
348
return m.invoke(null, ss, StandardSocketOptions.IP_TOS);
349
} catch (ClassNotFoundException e) {
350
// Ok, jdk.net module not present, just fall back
351
System.out.println("jdk.net module not present, falling back.");
352
return Integer.valueOf(ss.getOption(StandardSocketOptions.IP_TOS));
353
} catch (ReflectiveOperationException e) {
354
throw new AssertionError(e);
355
}
356
}
357
}
358
359