Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/SocketOption/UnsupportedOptionsTest.java
41149 views
1
/*
2
* Copyright (c) 2016, 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 java.io.IOException;
25
import java.lang.reflect.Field;
26
import java.net.*;
27
import java.util.ArrayList;
28
import java.util.List;
29
30
import jdk.test.lib.net.IPSupport;
31
32
/*
33
* @test
34
* @bug 8143554 8044773
35
* @library /test/lib
36
* @summary Test checks that UnsupportedOperationException for unsupported
37
* SOCKET_OPTIONS is thrown by both getOption() and setOption() methods.
38
* @requires !vm.graal.enabled
39
* @run main UnsupportedOptionsTest
40
* @run main/othervm -Djava.net.preferIPv4Stack=true UnsupportedOptionsTest
41
* @run main/othervm --limit-modules=java.base UnsupportedOptionsTest
42
*/
43
44
public class UnsupportedOptionsTest {
45
46
private static final List<SocketOption<?>> socketOptions = new ArrayList<>();
47
48
static {
49
socketOptions.add(StandardSocketOptions.IP_MULTICAST_IF);
50
socketOptions.add(StandardSocketOptions.IP_MULTICAST_LOOP);
51
socketOptions.add(StandardSocketOptions.IP_MULTICAST_TTL);
52
socketOptions.add(StandardSocketOptions.IP_TOS);
53
socketOptions.add(StandardSocketOptions.SO_BROADCAST);
54
socketOptions.add(StandardSocketOptions.SO_KEEPALIVE);
55
socketOptions.add(StandardSocketOptions.SO_LINGER);
56
socketOptions.add(StandardSocketOptions.SO_RCVBUF);
57
socketOptions.add(StandardSocketOptions.SO_REUSEADDR);
58
socketOptions.add(StandardSocketOptions.SO_SNDBUF);
59
socketOptions.add(StandardSocketOptions.TCP_NODELAY);
60
61
try {
62
Class<?> c = Class.forName("jdk.net.ExtendedSocketOptions");
63
Field field = c.getField("TCP_QUICKACK");
64
socketOptions.add((SocketOption<?>)field.get(null));
65
field = c.getField("TCP_KEEPIDLE");
66
socketOptions.add((SocketOption<?>)field.get(null));
67
field = c.getField("TCP_KEEPINTERVAL");
68
socketOptions.add((SocketOption<?>)field.get(null));
69
field = c.getField("TCP_KEEPCOUNT");
70
socketOptions.add((SocketOption<?>)field.get(null));
71
72
} catch (ClassNotFoundException e) {
73
// ignore, jdk.net module not present
74
} catch (ReflectiveOperationException e) {
75
throw new AssertionError(e);
76
}
77
}
78
79
public static void main(String[] args) throws IOException {
80
IPSupport.throwSkippedExceptionIfNonOperational();
81
82
Socket s = new Socket();
83
ServerSocket ss = new ServerSocket();
84
DatagramSocket ds = new DatagramSocket();
85
MulticastSocket ms = new MulticastSocket();
86
87
for (SocketOption option : socketOptions) {
88
if (!s.supportedOptions().contains(option)) {
89
testUnsupportedSocketOption(s, option);
90
}
91
92
if (!ss.supportedOptions().contains(option)) {
93
testUnsupportedSocketOption(ss, option);
94
}
95
96
if (!ms.supportedOptions().contains(option)) {
97
testUnsupportedSocketOption(ms, option);
98
}
99
100
if (!ds.supportedOptions().contains(option)) {
101
testUnsupportedSocketOption(ds, option);
102
}
103
}
104
}
105
106
/*
107
* Check that UnsupportedOperationException for unsupported option is
108
* thrown from both getOption() and setOption() methods.
109
*/
110
private static void testUnsupportedSocketOption(Object socket,
111
SocketOption option) {
112
testSet(socket, option);
113
testGet(socket, option);
114
}
115
116
private static void testSet(Object socket, SocketOption option) {
117
try {
118
setOption(socket, option);
119
} catch (UnsupportedOperationException e) {
120
System.out.println("UnsupportedOperationException was throw " +
121
"as expected. Socket: " + socket + " Option: " + option);
122
return;
123
} catch (Exception e) {
124
throw new RuntimeException("FAIL. Unexpected exception.", e);
125
}
126
throw new RuntimeException("FAIL. UnsupportedOperationException " +
127
"hasn't been thrown. Socket: " + socket + " Option: " + option);
128
}
129
130
private static void testGet(Object socket, SocketOption option) {
131
try {
132
getOption(socket, option);
133
} catch (UnsupportedOperationException e) {
134
System.out.println("UnsupportedOperationException was throw " +
135
"as expected. Socket: " + socket + " Option: " + option);
136
return;
137
} catch (Exception e) {
138
throw new RuntimeException("FAIL. Unexpected exception.", e);
139
}
140
throw new RuntimeException("FAIL. UnsupportedOperationException " +
141
"hasn't been thrown. Socket: " + socket + " Option: " + option);
142
}
143
144
private static void getOption(Object socket,
145
SocketOption option) throws IOException {
146
if (socket instanceof Socket) {
147
((Socket) socket).getOption(option);
148
} else if (socket instanceof ServerSocket) {
149
((ServerSocket) socket).getOption(option);
150
} else if (socket instanceof DatagramSocket) {
151
((DatagramSocket) socket).getOption(option);
152
} else {
153
throw new RuntimeException("Unsupported socket type");
154
}
155
}
156
157
private static void setOption(Object socket,
158
SocketOption option) throws IOException {
159
if (socket instanceof Socket) {
160
((Socket) socket).setOption(option, null);
161
} else if (socket instanceof ServerSocket) {
162
((ServerSocket) socket).setOption(option, null);
163
} else if (socket instanceof DatagramSocket) {
164
((DatagramSocket) socket).setOption(option, null);
165
} else {
166
throw new RuntimeException("Unsupported socket type");
167
}
168
}
169
}
170
171