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