Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/SocketOption/AfterClose.java
41149 views
1
/*
2
* Copyright (c) 2019, 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
/*
25
* @test
26
* @bug 8224477
27
* @summary Ensures that IOException is thrown after the socket is closed
28
* @run testng AfterClose
29
* @run testng/othervm -Djdk.net.usePlainSocketImpl AfterClose
30
* @run testng/othervm -Djdk.net.usePlainDatagramSocketImpl AfterClose
31
*/
32
33
import java.io.IOException;
34
import java.lang.reflect.Field;
35
import java.lang.reflect.Method;
36
import java.net.DatagramSocket;
37
import java.net.MulticastSocket;
38
import java.net.NetworkInterface;
39
import java.net.ServerSocket;
40
import java.net.Socket;
41
import java.net.SocketException;
42
import java.net.SocketOption;
43
import java.nio.channels.DatagramChannel;
44
import java.nio.channels.ServerSocketChannel;
45
import java.nio.channels.SocketChannel;
46
import java.util.ArrayList;
47
import java.util.Arrays;
48
import java.util.HashMap;
49
import java.util.List;
50
import java.util.Map;
51
import java.util.stream.Collectors;
52
53
import org.testng.annotations.DataProvider;
54
import org.testng.annotations.Test;
55
import static java.lang.Boolean.*;
56
import static java.net.StandardSocketOptions.*;
57
import static org.testng.Assert.expectThrows;
58
59
public class AfterClose {
60
61
static final Class<IOException> IOE = IOException.class;
62
static final String RO = "READ_ONLY";
63
64
static Map<SocketOption<?>,List<Object>> OPTION_VALUES_MAP = optionValueMap();
65
66
static boolean supportsMulticast(NetworkInterface ni) {
67
try {
68
return ni.supportsMulticast();
69
} catch (SocketException e) {
70
return false;
71
}
72
}
73
74
static List<Object> listNetworkInterfaces() {
75
try {
76
return NetworkInterface.networkInterfaces()
77
.filter(AfterClose::supportsMulticast)
78
.collect(Collectors.toList());
79
} catch (Exception e) { }
80
return List.of();
81
}
82
83
static Map<SocketOption<?>,List<Object>> optionValueMap() {
84
Map<SocketOption<?>,List<Object>> map = new HashMap<>();
85
map.put(IP_MULTICAST_IF, listNetworkInterfaces() );
86
map.put(IP_MULTICAST_LOOP, listOf(TRUE, FALSE) );
87
map.put(IP_MULTICAST_TTL, listOf(0, 100, 255) );
88
map.put(IP_TOS, listOf(0, 101, 255) );
89
map.put(SO_BROADCAST, listOf(TRUE, FALSE) );
90
map.put(SO_KEEPALIVE, listOf(TRUE, FALSE) );
91
map.put(SO_LINGER, listOf(0, 5, 15) );
92
map.put(SO_RCVBUF, listOf(1, 100, 1000));
93
map.put(SO_REUSEADDR, listOf(TRUE, FALSE) );
94
map.put(SO_REUSEPORT, listOf(TRUE, FALSE) );
95
map.put(SO_SNDBUF, listOf(1, 100, 1000));
96
map.put(TCP_NODELAY, listOf(TRUE, FALSE) );
97
// extended options
98
try {
99
Class<?> c = Class.forName("jdk.net.ExtendedSocketOptions");
100
Field field = c.getField("TCP_QUICKACK");
101
map.put((SocketOption<?>)field.get(null), listOf(TRUE, FALSE));
102
field = c.getField("TCP_KEEPIDLE");
103
map.put((SocketOption<?>)field.get(null), listOf(10, 100));
104
field = c.getField("TCP_KEEPINTERVAL");
105
map.put((SocketOption<?>)field.get(null), listOf(10, 100));
106
field = c.getField("TCP_KEEPCOUNT");
107
map.put((SocketOption<?>)field.get(null), listOf(10, 100));
108
field = c.getField("SO_INCOMING_NAPI_ID");
109
map.put((SocketOption<?>)field.get(null), listOf(RO));
110
} catch (ClassNotFoundException e) {
111
// ignore, jdk.net module not present
112
} catch (ReflectiveOperationException e) {
113
throw new AssertionError(e);
114
}
115
return map;
116
}
117
118
// -- Socket
119
120
@DataProvider(name = "socketOptionValues")
121
public Object[][] socketOptionValues() throws Exception {
122
try (Socket s = new Socket()) {
123
return s.supportedOptions().stream()
124
.map(so -> new Object[] {so, OPTION_VALUES_MAP.get(so)})
125
.toArray(Object[][]::new);
126
}
127
}
128
129
@Test(dataProvider = "socketOptionValues")
130
public <T> void closedSocketImplUncreated(SocketOption<T> option, List<T> values)
131
throws IOException
132
{
133
Socket socket = createClosedSocketImplUncreated();
134
for (int i=0; i<3; i++); {
135
for (T value : values) {
136
expectThrows(IOE, () -> socket.setOption(option, value));
137
expectThrows(IOE, () -> socket.getOption(option));
138
}
139
}
140
}
141
142
@Test(dataProvider = "socketOptionValues")
143
public <T> void closedSocketImplCreated(SocketOption<T> option, List<T> values)
144
throws IOException
145
{
146
Socket socket = createClosedSocketImplCreated();
147
for (int i=0; i<3; i++); {
148
for (T value : values) {
149
expectThrows(IOE, () -> socket.setOption(option, value));
150
expectThrows(IOE, () -> socket.getOption(option));
151
}
152
}
153
}
154
155
@Test(dataProvider = "socketOptionValues")
156
public <T> void closedSocketAdapter(SocketOption<T> option, List<T> values)
157
throws IOException
158
{
159
Socket socket = createClosedSocketFromAdapter();
160
for (int i=0; i<3; i++); {
161
for (T value : values) {
162
if (!RO.equals(value)) expectThrows(IOE, () -> socket.setOption(option, value));
163
expectThrows(IOE, () -> socket.getOption(option));
164
}
165
}
166
}
167
168
// -- ServerSocket
169
170
@DataProvider(name = "serverSocketOptionValues")
171
public Object[][] serverSocketOptionValues() throws Exception {
172
try (ServerSocket ss = new ServerSocket()) {
173
return ss.supportedOptions().stream()
174
.map(so -> new Object[] {so, OPTION_VALUES_MAP.get(so)})
175
.toArray(Object[][]::new);
176
}
177
}
178
179
@Test(dataProvider = "serverSocketOptionValues")
180
public <T> void closedServerSocketImplUncreated(SocketOption<T> option, List<T> values)
181
throws IOException
182
{
183
ServerSocket serverSocket = createClosedServerSocketImplUncreated();
184
for (int i=0; i<3; i++); {
185
for (T value : values) {
186
expectThrows(IOE, () -> serverSocket.setOption(option, value));
187
expectThrows(IOE, () -> serverSocket.getOption(option));
188
}
189
}
190
}
191
192
@Test(dataProvider = "serverSocketOptionValues")
193
public <T> void closedServerSocketImplCreated(SocketOption<T> option, List<T> values)
194
throws IOException
195
{
196
ServerSocket serverSocket = createClosedServerSocketImplCreated();
197
for (int i=0; i<3; i++); {
198
for (T value : values) {
199
expectThrows(IOE, () -> serverSocket.setOption(option, value));
200
expectThrows(IOE, () -> serverSocket.getOption(option));
201
}
202
}
203
}
204
205
@Test(dataProvider = "serverSocketOptionValues")
206
public <T> void closedServerSocketAdapter(SocketOption<T> option, List<T> values)
207
throws IOException
208
{
209
if (option == IP_TOS)
210
return; // SSC does not support IP_TOS
211
212
ServerSocket serverSocket = createClosedServerSocketFromAdapter();
213
for (int i=0; i<3; i++); {
214
for (T value : values) {
215
if (!RO.equals(value)) expectThrows(IOE, () -> serverSocket.setOption(option, value));
216
expectThrows(IOE, () -> serverSocket.getOption(option));
217
}
218
}
219
}
220
221
// -- DatagramSocket
222
223
@DataProvider(name = "datagramSocketOptionValues")
224
public Object[][] datagramSocketOptionValues() throws Exception {
225
try (DatagramSocket ds = new DatagramSocket()) {
226
return ds.supportedOptions().stream()
227
.map(so -> new Object[] {so, OPTION_VALUES_MAP.get(so)})
228
.toArray(Object[][]::new);
229
}
230
}
231
232
@Test(dataProvider = "datagramSocketOptionValues")
233
public <T> void closedUnboundDatagramSocket(SocketOption<T> option, List<T> values)
234
throws IOException
235
{
236
DatagramSocket datagramSocket = createClosedUnboundDatagramSocket();
237
for (int i=0; i<3; i++); {
238
for (T value : values) {
239
if (!RO.equals(value)) expectThrows(IOE, () -> datagramSocket.setOption(option, value));
240
expectThrows(IOE, () -> datagramSocket.getOption(option));
241
}
242
}
243
}
244
245
@Test(dataProvider = "datagramSocketOptionValues")
246
public <T> void closedBoundDatagramSocket(SocketOption<T> option, List<T> values)
247
throws IOException
248
{
249
DatagramSocket datagramSocket = createClosedBoundDatagramSocket();
250
for (int i=0; i<3; i++); {
251
for (T value : values) {
252
if (!RO.equals(value)) expectThrows(IOE, () -> datagramSocket.setOption(option, value));
253
expectThrows(IOE, () -> datagramSocket.getOption(option));
254
}
255
}
256
}
257
258
@Test(dataProvider = "datagramSocketOptionValues")
259
public <T> void closedDatagramAdapter(SocketOption<T> option, List<T> values)
260
throws IOException
261
{
262
DatagramSocket datagramSocket = createClosedBoundDatagramSocket();
263
for (int i=0; i<3; i++); {
264
for (T value : values) {
265
if (!RO.equals(value)) expectThrows(IOE, () -> datagramSocket.setOption(option, value));
266
expectThrows(IOE, () -> datagramSocket.getOption(option));
267
}
268
}
269
}
270
271
// -- MulticastSocket
272
273
@DataProvider(name = "multicastSocketOptionValues")
274
public Object[][] multicastSocketOptionValues() throws Exception {
275
try (MulticastSocket ms = new MulticastSocket()) {
276
return ms.supportedOptions().stream()
277
.map(so -> new Object[] {so, OPTION_VALUES_MAP.get(so)})
278
.toArray(Object[][]::new);
279
}
280
}
281
282
@Test(dataProvider = "multicastSocketOptionValues")
283
public <T> void closedUnboundMulticastSocket(SocketOption<T> option, List<T> values)
284
throws IOException
285
{
286
MulticastSocket multicastSocket = createClosedUnboundMulticastSocket();
287
for (int i=0; i<3; i++); {
288
for (T value : values) {
289
if (!RO.equals(value)) expectThrows(IOE, () -> multicastSocket.setOption(option, value));
290
expectThrows(IOE, () -> multicastSocket.getOption(option));
291
}
292
}
293
}
294
295
@Test(dataProvider = "multicastSocketOptionValues")
296
public <T> void closedBoundMulticastSocket(SocketOption<T> option, List<T> values)
297
throws IOException
298
{
299
MulticastSocket multicastSocket = createClosedBoundMulticastSocket();
300
for (int i=0; i<3; i++); {
301
for (T value : values) {
302
if (!RO.equals(value)) expectThrows(IOE, () -> multicastSocket.setOption(option, value));
303
expectThrows(IOE, () -> multicastSocket.getOption(option));
304
}
305
}
306
}
307
308
// --
309
310
static List<Object> listOf(Object... objs) {
311
List<Object> l = new ArrayList<>();
312
Arrays.stream(objs).forEachOrdered(l::add);
313
return l;
314
}
315
316
// Returns a closed Socket that has an impl whose `create` method has NOT been invoked.
317
static Socket createClosedSocketImplUncreated() throws IOException {
318
Socket s = new Socket();
319
s.close();
320
return s;
321
}
322
323
// Returns a closed Socket that has an impl whose `create` method has been invoked.
324
static Socket createClosedSocketImplCreated() throws IOException {
325
Socket s = new Socket();
326
s.bind(null); // binding causes impl::create to be invoked
327
s.close();
328
return s;
329
}
330
331
// Returns a closed Socket created from a SocketChannel's adapter.
332
static Socket createClosedSocketFromAdapter() throws IOException {
333
SocketChannel sc = SocketChannel.open();
334
sc.close();
335
return sc.socket();
336
}
337
338
// Returns a closed ServerSocket that has an impl whose `create` method has NOT been invoked.
339
static ServerSocket createClosedServerSocketImplUncreated() throws IOException {
340
ServerSocket ss = new ServerSocket();
341
ss.close();
342
return ss;
343
}
344
345
// Returns a closed ServerSocket that has an impl whose `create` method has been invoked.
346
static ServerSocket createClosedServerSocketImplCreated() throws IOException {
347
ServerSocket ss = new ServerSocket();
348
ss.bind(null); // binding causes impl::create to be invoked
349
ss.close();
350
return ss;
351
}
352
353
// Returns a closed ServerSocket created from a ServerSocketChannel's adapter.
354
static ServerSocket createClosedServerSocketFromAdapter() throws IOException {
355
ServerSocketChannel ssc = ServerSocketChannel.open();
356
ssc.close();
357
return ssc.socket();
358
}
359
360
// Returns a closed unbound DatagramSocket.
361
static DatagramSocket createClosedUnboundDatagramSocket() throws IOException {
362
DatagramSocket ds = new DatagramSocket(null);
363
assert ds.isBound() == false;
364
ds.close();
365
return ds;
366
}
367
368
// Returns a closed bound DatagramSocket.
369
static DatagramSocket createClosedBoundDatagramSocket() throws IOException {
370
DatagramSocket ds = new DatagramSocket();
371
assert ds.isBound() == true;
372
ds.close();
373
return ds;
374
}
375
376
// Returns a closed DatagramSocket that created from a DatagramChannel's adapter.
377
static DatagramSocket createClosedDatagramSocketFromAdapter() throws IOException {
378
DatagramChannel dc = DatagramChannel.open();
379
dc.close();
380
return dc.socket();
381
}
382
383
// Returns a closed unbound MulticastSocket.
384
static MulticastSocket createClosedUnboundMulticastSocket() throws IOException {
385
MulticastSocket ms = new MulticastSocket(null);
386
assert ms.isBound() == false;
387
ms.close();
388
return ms;
389
}
390
391
// Returns a closed bound MulticastSocket.
392
static MulticastSocket createClosedBoundMulticastSocket() throws IOException {
393
MulticastSocket ms = new MulticastSocket();
394
assert ms.isBound() == true;
395
ms.close();
396
return ms;
397
}
398
}
399
400