Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/SocketOption/RequiredOptions.java
41149 views
1
/*
2
* Copyright (c) 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
import java.io.IOException;
25
import java.net.DatagramSocket;
26
import java.net.MulticastSocket;
27
import java.net.ServerSocket;
28
import java.net.Socket;
29
import java.net.SocketOption;
30
import java.nio.channels.DatagramChannel;
31
import java.nio.channels.NetworkChannel;
32
import java.nio.channels.ServerSocketChannel;
33
import java.nio.channels.SocketChannel;
34
import java.util.Set;
35
import java.util.stream.Stream;
36
import org.testng.annotations.Test;
37
import org.testng.annotations.DataProvider;
38
39
import static java.net.StandardSocketOptions.*;
40
41
/*
42
* @test
43
* @bug 8235141
44
* @summary verifies that our implementation supports the set
45
* of SocketOptions that are required by the API documentation.
46
* @run testng/othervm -Djdk.net.usePlainSocketImpl RequiredOptions
47
* @run testng/othervm RequiredOptions
48
*/
49
public class RequiredOptions {
50
51
static final Set<SocketOption<?>> DATAGRAM_OPTIONS =
52
Set.of(SO_BROADCAST, SO_SNDBUF, SO_RCVBUF, SO_REUSEADDR, IP_TOS);
53
static final Set<SocketOption<?>> MULTICAST_OPTIONS =
54
concat(DATAGRAM_OPTIONS, Set.of(IP_MULTICAST_IF, IP_MULTICAST_LOOP, IP_MULTICAST_TTL));
55
static final Set<SocketOption<?>> SOCKET_OPTIONS =
56
Set.of(SO_KEEPALIVE, SO_LINGER, SO_SNDBUF, SO_RCVBUF, SO_REUSEADDR, TCP_NODELAY);
57
static final Set<SocketOption<?>> SERVER_OPTIONS =
58
Set.of(SO_RCVBUF, SO_REUSEADDR);
59
60
static Set<SocketOption<?>> concat(Set<SocketOption<?>> ...options) {
61
return Set.of(Stream.of(options).flatMap(Set::stream).distinct().toArray(SocketOption[]::new));
62
}
63
64
@DataProvider(name = "sockets")
65
static Object[][] provider() throws IOException {
66
return new Object[][] {
67
// UDP
68
{ Configurable.of(new DatagramSocket(null)), DATAGRAM_OPTIONS },
69
{ Configurable.of(new MulticastSocket(null)), MULTICAST_OPTIONS },
70
// TCP
71
{ Configurable.of(new Socket()), SOCKET_OPTIONS },
72
{ Configurable.of(new ServerSocket()), SERVER_OPTIONS },
73
// Adaptors
74
{ Configurable.of(DatagramChannel.open().socket()), MULTICAST_OPTIONS },
75
{ Configurable.of(SocketChannel.open().socket()), SOCKET_OPTIONS },
76
{ Configurable.of(ServerSocketChannel.open().socket()), SERVER_OPTIONS },
77
};
78
}
79
80
@Test(dataProvider = "sockets")
81
public <R, E extends Exception>
82
void test(Configurable<R,E> socket, Set<SocketOption<?>> options) throws E {
83
try (var s = socket) {
84
var impl = socket.socket().getClass();
85
System.out.println("Testing " + impl + " with " + options);
86
Set<SocketOption<?>> supported = socket.supportedOptions();
87
if (!supported.containsAll(options)) {
88
for (var option : options) {
89
if (!supported.contains(option)) {
90
System.err.println("Option " + option + " not supported by " + impl);
91
}
92
}
93
throw new AssertionError("Not all documented options are supported by " + impl);
94
}
95
}
96
}
97
98
static interface Configurable<R, E extends Exception> extends AutoCloseable {
99
<T> R setOption(SocketOption<T> name, T value) throws E;
100
<T> T getOption(SocketOption<T> name) throws E;
101
Set<SocketOption<?>> supportedOptions() throws E;
102
R socket();
103
void close() throws E;
104
105
static Configurable<DatagramSocket, IOException> of(DatagramSocket socket) {
106
return new ConfigurableImpl<>(socket, socket::setOption,
107
socket::getOption, socket::supportedOptions, socket::close);
108
}
109
static Configurable<Socket, IOException> of(Socket socket) {
110
return new ConfigurableImpl<>(socket, socket::setOption,
111
socket::getOption, socket::supportedOptions, socket::close);
112
}
113
static Configurable<ServerSocket, IOException> of(ServerSocket socket) {
114
return new ConfigurableImpl<>(socket, socket::setOption,
115
socket::getOption, socket::supportedOptions, socket::close);
116
}
117
}
118
119
static final class ConfigurableImpl<R, E extends Exception> implements Configurable<R, E> {
120
@FunctionalInterface
121
interface SetOption<R, E extends Exception> {
122
<T> R setOption(SocketOption<T> name, T value) throws E;
123
}
124
@FunctionalInterface
125
interface GetOption<E extends Exception> {
126
<T> T getOption(SocketOption<T> name) throws E;
127
}
128
@FunctionalInterface
129
interface SupportedOption<E extends Exception> {
130
Set<SocketOption<?>> supportedOptions() throws E;
131
}
132
@FunctionalInterface
133
interface Closer<E extends Exception> {
134
void close() throws E;
135
}
136
137
private final R socket;
138
private final SetOption<R, E> setter;
139
private final GetOption<E> getter;
140
private final SupportedOption<E> support;
141
private final Closer<E> closer;
142
143
public ConfigurableImpl(R socket, SetOption<R, E> setter, GetOption<E> getter,
144
SupportedOption<E> support, Closer<E> closer) {
145
this.socket = socket;
146
this.setter = setter;
147
this.getter = getter;
148
this.support = support;
149
this.closer = closer;
150
}
151
152
@Override
153
public <T> R setOption(SocketOption<T> name, T value) throws E {
154
return setter.setOption(name, value);
155
}
156
@Override
157
public <T> T getOption(SocketOption<T> name) throws E {
158
return getter.getOption(name);
159
}
160
@Override
161
public Set<SocketOption<?>> supportedOptions() throws E {
162
return support.supportedOptions();
163
}
164
@Override
165
public R socket() {
166
return socket;
167
}
168
@Override
169
public void close() throws E {
170
closer.close();
171
}
172
}
173
174
175
}
176
177