8381616: Refactor various java/net/*Socket*/ TestNG tests to use JUnit

Reviewed-by: alanb
This commit is contained in:
Daniel Fuchs 2026-04-08 15:41:27 +00:00
parent 988ec86dc6
commit 52a54dacb8
12 changed files with 374 additions and 301 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, 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
@ -25,10 +25,9 @@
* @test
* @bug 8238231
* @summary test that DatagramSocket calls java.net.DatagramSocketImpl::create
* @run testng/othervm TestCreate
* @run junit/othervm ${test.main.class}
*/
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.DatagramPacket;
@ -44,7 +43,9 @@ import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.testng.Assert.assertTrue;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestCreate {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2026, 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
@ -25,7 +25,7 @@
* @test
* @bug 8224477
* @summary Basic test for java.net.DatagramSocketImpl default behavior
* @run testng TestDefaultBehavior
* @run junit ${test.main.class}
*/
import java.io.IOException;
@ -36,11 +36,13 @@ import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.SocketOption;
import java.util.Set;
import org.testng.annotations.Test;
import static java.lang.Boolean.*;
import static java.net.StandardSocketOptions.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.expectThrows;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class TestDefaultBehavior {
@ -51,21 +53,21 @@ public class TestDefaultBehavior {
public void datagramSocketImpl() {
CustomDatagramSocketImpl dsi = new CustomDatagramSocketImpl();
assertEquals(dsi.supportedOptions().size(), 0);
assertEquals(0, dsi.supportedOptions().size());
expectThrows(NPE, () -> dsi.setOption(null, null));
expectThrows(NPE, () -> dsi.setOption(null, 1));
expectThrows(UOE, () -> dsi.setOption(SO_RCVBUF, 100));
expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE));
expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, FALSE));
expectThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, TRUE));
expectThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, FALSE));
expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE));
assertThrows(NPE, () -> dsi.setOption(null, null));
assertThrows(NPE, () -> dsi.setOption(null, 1));
assertThrows(UOE, () -> dsi.setOption(SO_RCVBUF, 100));
assertThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE));
assertThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, FALSE));
assertThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, TRUE));
assertThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, FALSE));
assertThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE));
expectThrows(NPE, () -> dsi.getOption(null));
expectThrows(UOE, () -> dsi.getOption(SO_RCVBUF));
expectThrows(UOE, () -> dsi.getOption(SO_KEEPALIVE));
expectThrows(UOE, () -> dsi.getOption(FAKE_SOCK_OPT));
assertThrows(NPE, () -> dsi.getOption(null));
assertThrows(UOE, () -> dsi.getOption(SO_RCVBUF));
assertThrows(UOE, () -> dsi.getOption(SO_KEEPALIVE));
assertThrows(UOE, () -> dsi.getOption(FAKE_SOCK_OPT));
}
static final SocketOption<Boolean> FAKE_SOCK_OPT = new SocketOption<>() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, 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
@ -24,15 +24,15 @@
/* @test
* @bug 8243999
* @summary Checks to ensure that Multicast constructors behave as expected
* @run testng Constructor
* @run junit ${test.main.class}
*/
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.MulticastSocket;
import static org.testng.Assert.assertTrue;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class Constructor {
@Test

View File

@ -27,44 +27,41 @@ import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import jdk.test.lib.NetworkConfiguration;
import org.testng.SkipException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static jdk.test.lib.net.IPSupport.diagnoseConfigurationIssue;
import static java.lang.String.format;
import static java.lang.System.out;
import static java.net.StandardSocketOptions.IP_MULTICAST_IF;
import static java.util.stream.Collectors.toList;
import static jdk.test.lib.net.IPSupport.diagnoseConfigurationIssue;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @test
* @bug 8236441
* @summary Bound MulticastSocket fails when setting outbound interface on Windows
* @library /test/lib
* @run testng IPMulticastIF
* @run testng/othervm -Djava.net.preferIPv4Stack=true IPMulticastIF
* @run testng/othervm -Djava.net.preferIPv6Addresses=true IPMulticastIF
* @run testng/othervm -Djava.net.preferIPv6Addresses=true -Djava.net.preferIPv4Stack=true IPMulticastIF
* @run junit ${test.main.class}
* @run junit/othervm -Djava.net.preferIPv4Stack=true ${test.main.class}
* @run junit/othervm -Djava.net.preferIPv6Addresses=true ${test.main.class}
* @run junit/othervm -Djava.net.preferIPv6Addresses=true -Djava.net.preferIPv4Stack=true ${test.main.class}
*/
public class IPMulticastIF {
@BeforeTest
public void sanity() {
Optional<String> configurationIssue = diagnoseConfigurationIssue();
configurationIssue.map(SkipException::new).ifPresent(x -> {
throw x;
});
@BeforeAll
public static void sanity() {
diagnoseConfigurationIssue().ifPresent(Assumptions::abort);
NetworkConfiguration.printSystemConfiguration(out);
}
@DataProvider(name = "scenarios")
public Object[][] positive() throws Exception {
public static Object[][] positive() throws Exception {
List<InetAddress> addrs = List.of(InetAddress.getLocalHost(),
InetAddress.getLoopbackAddress());
List<Object[]> list = new ArrayList<>();
@ -81,8 +78,7 @@ public class IPMulticastIF {
return list.stream().toArray(Object[][]::new);
}
@DataProvider(name = "interfaces")
public Object[][] interfaces() throws Exception {
public static Object[][] interfaces() throws Exception {
List<Object[]> list = new ArrayList<>();
NetworkConfiguration nc = NetworkConfiguration.probe();
nc.multicastInterfaces(true)
@ -92,7 +88,8 @@ public class IPMulticastIF {
return list.stream().toArray(Object[][]::new);
}
@Test(dataProvider = "scenarios")
@ParameterizedTest
@MethodSource("positive")
public void testSetGetInterfaceBound(InetSocketAddress bindAddr, NetworkInterface nif)
throws Exception
{
@ -100,11 +97,12 @@ public class IPMulticastIF {
try (MulticastSocket ms = new MulticastSocket(bindAddr)) {
ms.setNetworkInterface(nif);
NetworkInterface msNetIf = ms.getNetworkInterface();
assertEquals(msNetIf, nif);
assertEquals(nif, msNetIf);
}
}
@Test(dataProvider = "interfaces")
@ParameterizedTest
@MethodSource("interfaces")
public void testSetGetInterfaceUnbound(NetworkInterface nif)
throws Exception
{
@ -112,11 +110,12 @@ public class IPMulticastIF {
try (MulticastSocket ms = new MulticastSocket()) {
ms.setNetworkInterface(nif);
NetworkInterface msNetIf = ms.getNetworkInterface();
assertEquals(msNetIf, nif);
assertEquals(nif, msNetIf);
}
}
@Test(dataProvider = "scenarios")
@ParameterizedTest
@MethodSource("positive")
public void testSetGetOptionBound(InetSocketAddress bindAddr, NetworkInterface nif)
throws Exception
{
@ -124,11 +123,12 @@ public class IPMulticastIF {
try (MulticastSocket ms = new MulticastSocket(bindAddr)) {
ms.setOption(IP_MULTICAST_IF, nif);
NetworkInterface msNetIf = ms.getOption(IP_MULTICAST_IF);
assertEquals(msNetIf, nif);
assertEquals(nif, msNetIf);
}
}
@Test(dataProvider = "interfaces")
@ParameterizedTest
@MethodSource("interfaces")
public void testSetGetOptionUnbound(NetworkInterface nif)
throws Exception
{
@ -136,21 +136,21 @@ public class IPMulticastIF {
try (MulticastSocket ms = new MulticastSocket()) {
ms.setOption(IP_MULTICAST_IF, nif);
NetworkInterface msNetIf = ms.getOption(IP_MULTICAST_IF);
assertEquals(msNetIf, nif);
assertEquals(nif, msNetIf);
}
}
// -- get without set
@DataProvider(name = "bindAddresses")
public Object[][] bindAddresses() throws Exception {
public static Object[][] bindAddresses() throws Exception {
return new Object[][] {
{ new InetSocketAddress(InetAddress.getLocalHost(), 0) },
{ new InetSocketAddress(InetAddress.getLoopbackAddress(), 0) },
};
}
@Test(dataProvider = "bindAddresses")
@ParameterizedTest
@MethodSource("bindAddresses")
public void testGetInterfaceBound(InetSocketAddress bindAddr)
throws Exception
{
@ -168,13 +168,14 @@ public class IPMulticastIF {
}
}
@Test(dataProvider = "bindAddresses")
@ParameterizedTest
@MethodSource("bindAddresses")
public void testGetOptionBound(InetSocketAddress bindAddr)
throws Exception
{
out.println(format("\n\n--- testGetOptionBound bindAddr=[%s]", bindAddr));
try (MulticastSocket ms = new MulticastSocket(bindAddr)) {
assertEquals(ms.getOption(IP_MULTICAST_IF), null);
assertEquals(null, ms.getOption(IP_MULTICAST_IF));
}
}
@ -182,7 +183,7 @@ public class IPMulticastIF {
public void testGetOptionUnbound() throws Exception {
out.println("\n\n--- testGetOptionUnbound ");
try (MulticastSocket ms = new MulticastSocket()) {
assertEquals(ms.getOption(IP_MULTICAST_IF), null);
assertEquals(null, ms.getOption(IP_MULTICAST_IF));
}
}
@ -190,7 +191,7 @@ public class IPMulticastIF {
// that represent any local address.
static void assertPlaceHolder(NetworkInterface nif) {
List<InetAddress> addrs = nif.inetAddresses().collect(toList());
assertEquals(addrs.size(), 1);
assertEquals(1, addrs.size());
assertTrue(addrs.get(0).isAnyLocalAddress());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, 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
@ -21,39 +21,39 @@
* questions.
*/
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.MulticastSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.nio.channels.DatagramChannel;
import static org.testng.Assert.assertThrows;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
/*
* @test
* @bug 8243408
* @summary Check that MulticastSocket throws expected
* Exception when sending a DatagramPacket with port 0
* @run testng/othervm SendPortZero
* @run junit/othervm ${test.main.class}
*/
public class SendPortZero {
private InetAddress loopbackAddr, wildcardAddr;
private MulticastSocket multicastSocket;
private DatagramPacket loopbackZeroPkt, wildcardZeroPkt, wildcardValidPkt;
private static InetAddress loopbackAddr, wildcardAddr;
private static MulticastSocket multicastSocket;
private static DatagramPacket loopbackZeroPkt, wildcardZeroPkt, wildcardValidPkt;
private static final Class<SocketException> SE = SocketException.class;
@BeforeTest
public void setUp() throws IOException {
@BeforeAll
public static void setUp() throws IOException {
multicastSocket = new MulticastSocket();
byte[] buf = "test".getBytes();
@ -80,23 +80,26 @@ public class SendPortZero {
wildcardValidPkt.setPort(multicastSocket.getLocalPort());
}
@DataProvider(name = "data")
public Object[][] variants() {
public static Object[][] testCases() throws IOException {
return new Object[][]{
{ multicastSocket, loopbackZeroPkt },
{ multicastSocket, wildcardZeroPkt },
{ new MulticastSocket(), loopbackZeroPkt },
{ new MulticastSocket(), wildcardZeroPkt },
// Not currently tested. See JDK-8236807
//{ multicastSocket, wildcardValidPkt }
//{ new MulticastSocket(), wildcardValidPkt }
};
}
@Test(dataProvider = "data")
@ParameterizedTest
@MethodSource("testCases")
public void testSend(MulticastSocket ms, DatagramPacket pkt) {
assertThrows(SE, () -> ms.send(pkt));
try (ms) {
assertFalse(ms.isClosed());
assertThrows(SE, () -> ms.send(pkt));
}
}
@AfterTest
public void tearDown() {
@AfterAll
public static void tearDown() {
multicastSocket.close();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2026, 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
@ -28,9 +28,9 @@
* return the correct result for StandardSocketOptions.IP_MULTICAST_LOOP.
* The test sets a DatagramSocketImplFactory and needs to run in /othervm
* mode.
* @run testng/othervm SetLoopbackOption
* @run testng/othervm -Djava.net.preferIPv4Stack=true SetLoopbackOption
* @run testng/othervm -Djava.net.preferIPv6Addresses=true SetLoopbackOption
* @run junit/othervm ${test.main.class}
* @run junit/othervm -Djava.net.preferIPv4Stack=true ${test.main.class}
* @run junit/othervm -Djava.net.preferIPv6Addresses=true ${test.main.class}
*/
import java.io.FileDescriptor;
@ -52,11 +52,12 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import static java.lang.System.out;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SetLoopbackOption {
final InetAddress loopbackAddress = InetAddress.getLoopbackAddress();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2026, 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
@ -23,7 +23,7 @@
/**
* @test
* @run testng ConnectionReset
* @run junit ${test.main.class}
* @summary Test behavior of read and available when a connection is reset
*/
@ -34,10 +34,13 @@ import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Test
public class ConnectionReset {
static final int REPEAT_COUNT = 5;
@ -45,25 +48,23 @@ public class ConnectionReset {
/**
* Tests available before read when there are no bytes to read
*/
@Test
public void testAvailableBeforeRead1() throws IOException {
System.out.println("testAvailableBeforeRead1");
withResetConnection(null, s -> {
InputStream in = s.getInputStream();
for (int i=0; i<REPEAT_COUNT; i++) {
int bytesAvailable = in.available();
System.out.format("available => %d%n", bytesAvailable);
assertTrue(bytesAvailable == 0);
try {
System.err.format("available => %d%n", bytesAvailable);
assertEquals(0, bytesAvailable);
IOException ioe = assertThrows(IOException.class, () -> {
int bytesRead = in.read();
if (bytesRead == -1) {
System.out.println("read => EOF");
System.err.println("read => EOF");
} else {
System.out.println("read => 1 byte");
System.err.println("read => 1 byte");
}
assertTrue(false);
} catch (IOException ioe) {
System.out.format("read => %s (expected)%n", ioe);
}
});
System.err.format("read => %s (expected)%n", ioe);
}
});
}
@ -71,28 +72,25 @@ public class ConnectionReset {
/**
* Tests available before read when there are bytes to read
*/
@Test
public void testAvailableBeforeRead2() throws IOException {
System.out.println("testAvailableBeforeRead2");
byte[] data = { 1, 2, 3 };
withResetConnection(data, s -> {
InputStream in = s.getInputStream();
int remaining = data.length;
for (int i=0; i<REPEAT_COUNT; i++) {
int bytesAvailable = in.available();
System.out.format("available => %d%n", bytesAvailable);
System.err.format("available => %d%n", bytesAvailable);
assertTrue(bytesAvailable <= remaining);
try {
int bytesRead = in.read();
if (bytesRead == -1) {
System.out.println("read => EOF");
assertTrue(false);
} else {
System.out.println("read => 1 byte");
assertTrue(remaining > 0);
remaining--;
}
assertNotEquals(-1, bytesRead, "EOF not expected");
System.err.println("read => 1 byte");
assertTrue(remaining > 0);
remaining--;
} catch (IOException ioe) {
System.out.format("read => %s%n", ioe);
System.err.format("read => %s%n", ioe);
remaining = 0;
}
}
@ -102,25 +100,24 @@ public class ConnectionReset {
/**
* Tests read before available when there are no bytes to read
*/
@Test
public void testReadBeforeAvailable1() throws IOException {
System.out.println("testReadBeforeAvailable1");
withResetConnection(null, s -> {
InputStream in = s.getInputStream();
for (int i=0; i<REPEAT_COUNT; i++) {
try {
IOException ioe = assertThrows(IOException.class, () -> {
int bytesRead = in.read();
if (bytesRead == -1) {
System.out.println("read => EOF");
System.err.println("read => EOF");
} else {
System.out.println("read => 1 byte");
System.err.println("read => 1 byte");
}
assertTrue(false);
} catch (IOException ioe) {
System.out.format("read => %s (expected)%n", ioe);
}
});
System.err.format("read => %s (expected)%n", ioe);
int bytesAvailable = in.available();
System.out.format("available => %d%n", bytesAvailable);
assertTrue(bytesAvailable == 0);
System.err.format("available => %d%n", bytesAvailable);
assertEquals(0, bytesAvailable);
}
});
}
@ -128,8 +125,8 @@ public class ConnectionReset {
/**
* Tests read before available when there are bytes to read
*/
@Test
public void testReadBeforeAvailable2() throws IOException {
System.out.println("testReadBeforeAvailable2");
byte[] data = { 1, 2, 3 };
withResetConnection(data, s -> {
InputStream in = s.getInputStream();
@ -137,20 +134,17 @@ public class ConnectionReset {
for (int i=0; i<REPEAT_COUNT; i++) {
try {
int bytesRead = in.read();
if (bytesRead == -1) {
System.out.println("read => EOF");
assertTrue(false);
} else {
System.out.println("read => 1 byte");
assertTrue(remaining > 0);
remaining--;
}
assertNotEquals(-1, bytesRead, "EOF not expected");
System.err.println("read => 1 byte");
assertTrue(remaining > 0);
remaining--;
} catch (IOException ioe) {
System.out.format("read => %s%n", ioe);
System.err.format("read => %s%n", ioe);
remaining = 0;
}
int bytesAvailable = in.available();
System.out.format("available => %d%n", bytesAvailable);
System.err.format("available => %d%n", bytesAvailable);
assertTrue(bytesAvailable <= remaining);
}
});
@ -159,31 +153,22 @@ public class ConnectionReset {
/**
* Tests available and read on a socket closed after connection reset
*/
@Test
public void testAfterClose() throws IOException {
System.out.println("testAfterClose");
withResetConnection(null, s -> {
InputStream in = s.getInputStream();
try {
in.read();
assertTrue(false);
} catch (IOException ioe) {
// expected
}
assertThrows(IOException.class, () -> in.read());
s.close();
try {
IOException ioe = assertThrows(IOException.class, () -> {
int bytesAvailable = in.available();
System.out.format("available => %d%n", bytesAvailable);
assertTrue(false);
} catch (IOException ioe) {
System.out.format("available => %s (expected)%n", ioe);
}
try {
System.err.format("available => %d%n", bytesAvailable);
});
System.err.format("available => %s (expected)%n", ioe);
ioe = assertThrows(IOException.class, () -> {
int n = in.read();
System.out.format("read => %d%n", n);
assertTrue(false);
} catch (IOException ioe) {
System.out.format("read => %s (expected)%n", ioe);
}
System.err.format("read => %d%n", n);
});
System.err.format("read => %s (expected)%n", ioe);
});
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2026, 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
@ -24,13 +24,13 @@
import java.net.InetAddress;
import java.net.Socket;
import org.testng.annotations.Test;
import static org.testng.Assert.fail;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
/*
* @test
* @summary Basic test for the UDP sockets through the java.net.Socket constructors
* @run testng UdpSocket
* @run junit ${test.main.class}
*/
public class UdpSocket {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2026, 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
@ -26,7 +26,7 @@
* @bug 8221481
* @summary Test the platform SocketImpl when used in unintended ways
* @compile/module=java.base java/net/PlatformSocketImpl.java
* @run testng/othervm BadUsages
* @run junit/othervm ${test.main.class}
*/
import java.io.IOException;
@ -41,8 +41,8 @@ import java.net.StandardSocketOptions;
import java.net.PlatformSocketImpl; // test helper
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* SocketImpl does not specify how the SocketImpl behaves when used in ways
@ -54,74 +54,80 @@ import static org.testng.Assert.*;
* throws reasonable exceptions, for these scenarios.
*/
@Test
public class BadUsages {
/**
* Test create when already created.
*/
@Test
public void testCreate1() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.create(true));
assertThrows(IOException.class, () -> impl.create(true));
}
}
/**
* Test create when closed.
*/
@Test
public void testCreate2() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.create(true));
assertThrows(IOException.class, () -> impl.create(true));
}
/**
* Test create when not a stream socket.
*/
@Test
public void testCreate3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
expectThrows(IOException.class, () -> impl.create(false));
assertThrows(IOException.class, () -> impl.create(false));
}
}
/**
* Test connect when not created.
*/
@Test
public void testConnect1() throws IOException {
try (var ss = new ServerSocket(0)) {
var impl = new PlatformSocketImpl(false);
var address = ss.getInetAddress();
int port = ss.getLocalPort();
expectThrows(IOException.class, () -> impl.connect(address, port));
assertThrows(IOException.class, () -> impl.connect(address, port));
}
}
/**
* Test connect with unsupported address type.
*/
@Test
public void testConnect2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
var remote = new SocketAddress() { };
expectThrows(IOException.class, () -> impl.connect(remote, 0));
assertThrows(IOException.class, () -> impl.connect(remote, 0));
}
}
/**
* Test connect with an unresolved address.
*/
@Test
public void testConnect3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
var remote = new InetSocketAddress("blah-blah.blah-blah", 80);
expectThrows(IOException.class, () -> impl.connect(remote, 0));
assertThrows(IOException.class, () -> impl.connect(remote, 0));
}
}
/**
* Test connect when already connected.
*/
@Test
public void testConnect4() throws IOException {
try (var ss = new ServerSocket();
var impl = new PlatformSocketImpl(false)) {
@ -130,47 +136,51 @@ public class BadUsages {
impl.create(true);
int port = ss.getLocalPort();
impl.connect(loopback, port);
expectThrows(IOException.class, () -> impl.connect(loopback, port));
assertThrows(IOException.class, () -> impl.connect(loopback, port));
}
}
/**
* Test connect when closed.
*/
@Test
public void testConnect5() throws IOException {
try (var ss = new ServerSocket(0)) {
var impl = new PlatformSocketImpl(false);
impl.close();
String host = ss.getInetAddress().getHostAddress();
int port = ss.getLocalPort();
expectThrows(IOException.class, () -> impl.connect(host, port));
assertThrows(IOException.class, () -> impl.connect(host, port));
}
}
/**
* Test bind when not created.
*/
@Test
public void testBind1() throws IOException {
var impl = new PlatformSocketImpl(false);
var loopback = InetAddress.getLoopbackAddress();
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
assertThrows(IOException.class, () -> impl.bind(loopback, 0));
}
/**
* Test bind when already bound.
*/
@Test
public void testBind2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
var loopback = InetAddress.getLoopbackAddress();
impl.bind(loopback, 0);
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
assertThrows(IOException.class, () -> impl.bind(loopback, 0));
}
}
/**
* Test bind when connected.
*/
@Test
public void testBind3() throws IOException {
try (var ss = new ServerSocket();
var impl = new PlatformSocketImpl(false)) {
@ -178,94 +188,103 @@ public class BadUsages {
ss.bind(new InetSocketAddress(loopback, 0));
impl.create(true);
impl.connect(ss.getLocalSocketAddress(), 0);
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
assertThrows(IOException.class, () -> impl.bind(loopback, 0));
}
}
/**
* Test bind when closed.
*/
@Test
public void testBind4() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
var loopback = InetAddress.getLoopbackAddress();
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
assertThrows(IOException.class, () -> impl.bind(loopback, 0));
}
/**
* Test listen when not created.
*/
@Test
public void testListen1() {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.listen(16));
assertThrows(IOException.class, () -> impl.listen(16));
}
/**
* Test listen when not bound.
*/
@Test
public void testListen2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.listen(16));
assertThrows(IOException.class, () -> impl.listen(16));
}
}
/**
* Test listen when closed.
*/
@Test
public void testListen3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.listen(16));
assertThrows(IOException.class, () -> impl.listen(16));
}
/**
* Test accept when not created.
*/
@Test
public void testAccept1() throws IOException {
var impl = new PlatformSocketImpl(true);
var si = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.accept(si));
assertThrows(IOException.class, () -> impl.accept(si));
}
/**
* Test accept when not bound.
*/
@Test
public void testAccept2() throws IOException {
try (var impl = new PlatformSocketImpl(true)) {
impl.create(true);
var si = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.accept(si));
assertThrows(IOException.class, () -> impl.accept(si));
}
}
/**
* Test accept when closed.
*/
@Test
public void testAccept4() throws IOException {
var impl = new PlatformSocketImpl(true);
impl.close();
var si = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.accept(si));
assertThrows(IOException.class, () -> impl.accept(si));
}
/**
* Test accept with SocketImpl that is already created.
*/
@Test
public void testAccept5() throws IOException {
try (var impl = new PlatformSocketImpl(true);
var si = new PlatformSocketImpl(false)) {
impl.create(true);
impl.bind(InetAddress.getLoopbackAddress(), 0);
si.create(true);
expectThrows(IOException.class, () -> impl.accept(si));
assertThrows(IOException.class, () -> impl.accept(si));
}
}
/**
* Test accept with SocketImpl that is closed.
*/
@Test
public void testAccept6() throws IOException {
try (var impl = new PlatformSocketImpl(true);
var si = new PlatformSocketImpl(false)) {
@ -273,65 +292,71 @@ public class BadUsages {
impl.bind(InetAddress.getLoopbackAddress(), 0);
si.create(true);
si.close();
expectThrows(IOException.class, () -> impl.accept(si));
assertThrows(IOException.class, () -> impl.accept(si));
}
}
/**
* Test available when not created.
*/
@Test
public void testAvailable1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.available());
assertThrows(IOException.class, () -> impl.available());
}
/**
* Test available when created but not connected.
*/
@Test
public void testAvailable2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.available());
assertThrows(IOException.class, () -> impl.available());
}
}
/**
* Test available when closed.
*/
@Test
public void testAvailable3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.available());
assertThrows(IOException.class, () -> impl.available());
}
/**
* Test setOption when not created.
*/
@Test
public void testSetOption1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class,
assertThrows(IOException.class,
() -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));
// legacy
expectThrows(SocketException.class,
assertThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_REUSEADDR, true));
}
/**
* Test setOption when closed.
*/
@Test
public void testSetOption2() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class,
assertThrows(IOException.class,
() -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));
// legacy
expectThrows(SocketException.class,
assertThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_REUSEADDR, true));
}
/**
* Test setOption with unsupported option.
*/
@Test
public void testSetOption3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
@ -339,25 +364,26 @@ public class BadUsages {
@Override public String name() { return "birthday"; }
@Override public Class<String> type() { return String.class; }
};
expectThrows(UnsupportedOperationException.class, () -> impl.setOption(opt, ""));
assertThrows(UnsupportedOperationException.class, () -> impl.setOption(opt, ""));
// legacy
expectThrows(SocketException.class, () -> impl.setOption(-1, ""));
assertThrows(SocketException.class, () -> impl.setOption(-1, ""));
}
}
/**
* Test setOption(int, Object) with invalid values.
*/
@Test
public void testSetOption4() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(SocketException.class,
assertThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_REUSEADDR, -1));
expectThrows(SocketException.class,
assertThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_TIMEOUT, -1));
expectThrows(SocketException.class,
assertThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_SNDBUF, -1));
expectThrows(SocketException.class,
assertThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_RCVBUF, -1));
}
}
@ -365,29 +391,32 @@ public class BadUsages {
/**
* Test getOption when not created.
*/
@Test
public void testGetOption1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class,
assertThrows(IOException.class,
() -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));
expectThrows(SocketException.class,
assertThrows(SocketException.class,
() -> impl.getOption(-1));
}
/**
* Test getOption when closed.
*/
@Test
public void testGetOption2() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class,
assertThrows(IOException.class,
() -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));
expectThrows(SocketException.class,
assertThrows(SocketException.class,
() -> impl.getOption(SocketOptions.SO_REUSEADDR));
}
/**
* Test getOption with unsupported option.
*/
@Test
public void testGetOption3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
@ -395,89 +424,98 @@ public class BadUsages {
@Override public String name() { return "birthday"; }
@Override public Class<String> type() { return String.class; }
};
expectThrows(UnsupportedOperationException.class, () -> impl.getOption(opt));
expectThrows(SocketException.class, () -> impl.getOption(-1));
assertThrows(UnsupportedOperationException.class, () -> impl.getOption(opt));
assertThrows(SocketException.class, () -> impl.getOption(-1));
}
}
/**
* Test shutdownInput when not created.
*/
@Test
public void testShutdownInput1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.shutdownInput());
assertThrows(IOException.class, () -> impl.shutdownInput());
}
/**
* Test shutdownInput when not connected.
*/
@Test
public void testShutdownInput2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.shutdownInput());
assertThrows(IOException.class, () -> impl.shutdownInput());
}
}
/**
* Test shutdownInput when closed.
*/
@Test
public void testShutdownInput3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.shutdownInput());
assertThrows(IOException.class, () -> impl.shutdownInput());
}
/**
* Test shutdownOutput when not created.
*/
@Test
public void testShutdownOutput1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.shutdownOutput());
assertThrows(IOException.class, () -> impl.shutdownOutput());
}
/**
* Test shutdownOutput when not connected.
*/
@Test
public void testShutdownOutput2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.shutdownOutput());
assertThrows(IOException.class, () -> impl.shutdownOutput());
}
}
/**
* Test shutdownOutput when closed.
*/
@Test
public void testShutdownOutput3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.shutdownOutput());
assertThrows(IOException.class, () -> impl.shutdownOutput());
}
/**
* Test sendUrgentData when not created.
*/
@Test
public void testSendUrgentData1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
assertThrows(IOException.class, () -> impl.sendUrgentData(0));
}
/**
* Test sendUrgentData when not connected.
*/
@Test
public void testSendUrgentData2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
assertThrows(IOException.class, () -> impl.sendUrgentData(0));
}
}
/**
* Test sendUrgentData when closed.
*/
@Test
public void testSendUrgentData3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
assertThrows(IOException.class, () -> impl.sendUrgentData(0));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2026, 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
@ -25,7 +25,7 @@
* @test
* @bug 8213418
* @summary Ensure correct impl supported socket options
* @run testng ImplSupportedOptions
* @run junit ${test.main.class}
*/
import java.io.IOException;
@ -40,9 +40,10 @@ import java.net.SocketImpl;
import java.net.SocketOption;
import java.net.StandardSocketOptions;
import java.util.Set;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ImplSupportedOptions {
@ -52,29 +53,29 @@ public class ImplSupportedOptions {
Set<?> standardOptions = s.supportedOptions();
assertTrue(standardOptions.contains(StandardSocketOptions.SO_LINGER),
"Expected SO_LINGER, in:" + standardOptions);
assertEquals(standardOptions, s.supportedOptions());
assertEquals(standardOptions, s.supportedOptions());
assertEquals(s.supportedOptions(), standardOptions);
assertEquals(s.supportedOptions(), standardOptions);
s = new DummySocket();
Set<?> dummyOptions = s.supportedOptions();
assertEquals(dummyOptions.size(), 1);
assertEquals(1, dummyOptions.size());
assertTrue(dummyOptions.contains(DummySocketImpl.SOCKET_OPT));
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(s.supportedOptions(), dummyOptions);
assertEquals(s.supportedOptions(), dummyOptions);
s = new Socket();
standardOptions = s.supportedOptions();
assertTrue(standardOptions.contains(StandardSocketOptions.SO_LINGER),
"Expected SO_LINGER, in:" + standardOptions);
assertEquals(standardOptions, s.supportedOptions());
assertEquals(standardOptions, s.supportedOptions());
assertEquals(s.supportedOptions(), standardOptions);
assertEquals(s.supportedOptions(), standardOptions);
s = new DummySocket();
dummyOptions = s.supportedOptions();
assertEquals(dummyOptions.size(), 1);
assertEquals(1, dummyOptions.size());
assertTrue(dummyOptions.contains(DummySocketImpl.SOCKET_OPT));
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(s.supportedOptions(), dummyOptions);
assertEquals(s.supportedOptions(), dummyOptions);
}
@Test
@ -83,29 +84,29 @@ public class ImplSupportedOptions {
Set<?> standardOptions = s.supportedOptions();
assertTrue(standardOptions.contains(StandardSocketOptions.SO_REUSEADDR),
"Expected SO_REUSEADDR, in:" + standardOptions);
assertEquals(standardOptions, s.supportedOptions());
assertEquals(standardOptions, s.supportedOptions());
assertEquals(s.supportedOptions(), standardOptions);
assertEquals(s.supportedOptions(), standardOptions);
s = new DummyServerSocket();
Set<?> dummyOptions = s.supportedOptions();
assertEquals(dummyOptions.size(), 1);
assertEquals(1, dummyOptions.size());
assertTrue(dummyOptions.contains(DummySocketImpl.SOCKET_OPT));
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(s.supportedOptions(), dummyOptions);
assertEquals(s.supportedOptions(), dummyOptions);
s = new ServerSocket();
standardOptions = s.supportedOptions();
assertTrue(standardOptions.contains(StandardSocketOptions.SO_REUSEADDR),
"Expected SO_REUSEADDR, in:" + standardOptions);
assertEquals(standardOptions, s.supportedOptions());
assertEquals(standardOptions, s.supportedOptions());
assertEquals(s.supportedOptions(), standardOptions);
assertEquals(s.supportedOptions(), standardOptions);
s = new DummyServerSocket();
dummyOptions = s.supportedOptions();
assertEquals(dummyOptions.size(), 1);
assertEquals(1, dummyOptions.size());
assertTrue(dummyOptions.contains(DummySocketImpl.SOCKET_OPT));
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(s.supportedOptions(), dummyOptions);
assertEquals(s.supportedOptions(), dummyOptions);
}
static class DummySocket extends Socket {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2026, 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
@ -25,7 +25,7 @@
* @test
* @bug 8220493
* @modules java.base/java.net:+open java.base/sun.nio.ch:+open
* @run testng/othervm SocketImplCombinations
* @run junit/othervm ${test.main.class}
* @summary Test Socket and ServerSocket with combinations of SocketImpls
*/
@ -46,15 +46,22 @@ import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.function.BiConsumer;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Test
public class SocketImplCombinations {
/**
* Test creating an unconnected Socket, it should be created with a platform SocketImpl.
*/
@Test
public void testNewSocket1() throws IOException {
try (Socket s = new Socket()) {
SocketImpl si = getSocketImpl(s);
@ -67,6 +74,7 @@ public class SocketImplCombinations {
/**
* Test creating a connected Socket, it should be created with a platform SocketImpl.
*/
@Test
public void testNewSocket2() throws IOException {
try (ServerSocket ss = boundServerSocket()) {
try (Socket s = new Socket(ss.getInetAddress(), ss.getLocalPort())) {
@ -82,6 +90,7 @@ public class SocketImplCombinations {
* Test creating a Socket for a DIRECT connection, it should be created with a
* platform SocketImpl.
*/
@Test
public void testNewSocket3() throws IOException {
try (Socket s = new Socket(Proxy.NO_PROXY)) {
SocketImpl si = getSocketImpl(s);
@ -93,6 +102,7 @@ public class SocketImplCombinations {
* Test creating a Socket for a SOCKS connection, it should be created with a
* SOCKS SocketImpl.
*/
@Test
public void testNewSocket4() throws IOException {
var address = new InetSocketAddress("127.0.0.1", 1080);
var socksProxy = new Proxy(Proxy.Type.SOCKS, address);
@ -105,9 +115,10 @@ public class SocketImplCombinations {
}
/**
* Test creating a Socket for a HTTP proxy connection, it should be created with
* a HTTP proxy SocketImpl.
* Test creating a Socket for an HTTP proxy connection, it should be created with
* an HTTP proxy SocketImpl.
*/
@Test
public void testNewSocket5() throws IOException {
var address = new InetSocketAddress("127.0.0.1", 8080);
var httpProxy = new Proxy(Proxy.Type.HTTP, address);
@ -123,10 +134,11 @@ public class SocketImplCombinations {
* Test creating a Socket no SocketImpl. A platform SocketImpl should be
* created lazily.
*/
@Test
public void testNewSocket6() throws IOException {
Socket s = new Socket((SocketImpl) null) { };
try (s) {
assertTrue(getSocketImpl(s) == null);
assertNull(getSocketImpl(s));
s.bind(loopbackSocketAddress()); // force SocketImpl to be created
SocketImpl si = getSocketImpl(s);
assertTrue(isSocksSocketImpl(si));
@ -138,22 +150,24 @@ public class SocketImplCombinations {
/**
* Test creating a Socket with a custom SocketImpl.
*/
@Test
public void testNewSocket7() throws IOException {
Socket s = new Socket(new CustomSocketImpl(false)) { };
try (s) {
SocketImpl si = getSocketImpl(s);
assertTrue(si instanceof CustomSocketImpl);
assertInstanceOf(CustomSocketImpl.class, si);
}
}
/**
* Test creating a Socket when there is a SocketImplFactory set.
*/
@Test
public void testNewSocket8() throws IOException {
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
try (Socket s = new Socket()) {
SocketImpl si = getSocketImpl(s);
assertTrue(si instanceof CustomSocketImpl);
assertInstanceOf(CustomSocketImpl.class, si);
} finally {
setSocketSocketImplFactory(null);
}
@ -163,11 +177,12 @@ public class SocketImplCombinations {
* Test creating a Socket for a DIRECT connection when there is a
* SocketImplFactory set.
*/
@Test
public void testNewSocket9() throws IOException {
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
try (Socket s = new Socket(Proxy.NO_PROXY)) {
SocketImpl si = getSocketImpl(s);
assertTrue(si instanceof CustomSocketImpl);
assertInstanceOf(CustomSocketImpl.class, si);
} finally {
setSocketSocketImplFactory(null);
}
@ -177,6 +192,7 @@ public class SocketImplCombinations {
* Test creating a Socket for a SOCKS connection when there is a
* SocketImplFactory set.
*/
@Test
public void testNewSocket10() throws IOException {
var address = new InetSocketAddress("127.0.0.1", 1080);
var socksProxy = new Proxy(Proxy.Type.SOCKS, address);
@ -192,9 +208,10 @@ public class SocketImplCombinations {
}
/**
* Test creating a Socket for a HTTP proxy connection when there is a
* Test creating a Socket for an HTTP proxy connection when there is a
* SocketImplFactory set.
*/
@Test
public void testNewSocket11() throws IOException {
var address = new InetSocketAddress("127.0.0.1", 8080);
var httpProxy = new Proxy(Proxy.Type.HTTP, address);
@ -212,14 +229,15 @@ public class SocketImplCombinations {
/**
* Test creating a Socket no SocketImpl when there is a SocketImplFactory set.
*/
@Test
public void testNewSocket12() throws IOException {
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
try {
Socket s = new Socket((SocketImpl) null) { };
try (s) {
assertTrue(getSocketImpl(s) == null);
assertNull(getSocketImpl(s));
s.bind(loopbackSocketAddress()); // force SocketImpl to be created
assertTrue(getSocketImpl(s) instanceof CustomSocketImpl);
assertInstanceOf(CustomSocketImpl.class, getSocketImpl(s));
}
} finally {
setSocketSocketImplFactory(null);
@ -230,6 +248,7 @@ public class SocketImplCombinations {
* Test creating an unbound ServerSocket, it should be created with a platform
* SocketImpl.
*/
@Test
public void testNewServerSocket1() throws IOException {
try (ServerSocket ss = new ServerSocket()) {
SocketImpl si = getSocketImpl(ss);
@ -241,6 +260,7 @@ public class SocketImplCombinations {
* Test creating a bound ServerSocket, it should be created with a platform
* SocketImpl.
*/
@Test
public void testNewServerSocket2() throws IOException {
try (ServerSocket ss = new ServerSocket(0)) {
SocketImpl si = getSocketImpl(ss);
@ -251,22 +271,24 @@ public class SocketImplCombinations {
/**
* Test creating a ServerSocket with a custom SocketImpl.
*/
@Test
public void testNewServerSocket3() throws IOException {
ServerSocket ss = new ServerSocket(new CustomSocketImpl(true)) { };
try (ss) {
SocketImpl si = getSocketImpl(ss);
assertTrue(si instanceof CustomSocketImpl);
assertInstanceOf(CustomSocketImpl.class, si);
}
}
/**
* Test creating an unbound ServerSocket when there is a SocketImplFactory set.
*/
@Test
public void testNewServerSocket4() throws IOException {
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
try (ServerSocket ss = new ServerSocket()) {
SocketImpl si = getSocketImpl(ss);
assertTrue(si instanceof CustomSocketImpl);
assertInstanceOf(CustomSocketImpl.class, si);
} finally {
setServerSocketImplFactory(null);
}
@ -275,11 +297,12 @@ public class SocketImplCombinations {
/**
* Test creating a bound ServerSocket when there is a SocketImplFactory set.
*/
@Test
public void testNewServerSocket5() throws IOException {
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
try (ServerSocket ss = new ServerSocket(0)) {
SocketImpl si = getSocketImpl(ss);
assertTrue(si instanceof CustomSocketImpl);
assertInstanceOf(CustomSocketImpl.class, si);
} finally {
setServerSocketImplFactory(null);
}
@ -289,13 +312,14 @@ public class SocketImplCombinations {
* Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
* the Socket to accept is created with no SocketImpl.
*/
@Test
public void testServerSocketAccept1() throws IOException {
var socket = new Socket((SocketImpl) null) { };
assertTrue(getSocketImpl(socket) == null);
assertNull(getSocketImpl(socket));
serverSocketAccept(socket, (ss, s) -> {
assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
assertTrue(s == socket);
assertSame(socket, s);
SocketImpl si = getSocketImpl(s);
assertTrue(isPlatformSocketImpl(si));
checkFields(si);
@ -307,13 +331,14 @@ public class SocketImplCombinations {
* the Socket to accept is created with no SocketImpl, and there is a custom
* client SocketImplFactory set.
*/
@Test
public void testServerSocketAccept2() throws IOException {
var socket = new Socket((SocketImpl) null) { };
assertTrue(getSocketImpl(socket) == null);
assertNull(getSocketImpl(socket));
serverSocketAccept(socket, () -> new CustomSocketImpl(false), (ss, s) -> {
assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
assertTrue(s == socket);
assertSame(socket, s);
SocketImpl si = getSocketImpl(s);
assertTrue(isPlatformSocketImpl(si));
checkFields(si);
@ -325,6 +350,7 @@ public class SocketImplCombinations {
* the Socket to accept is created with a SocketImpl that delegates to a
* platform SocketImpl.
*/
@Test
public void testServerSocketAccept3() throws IOException {
var socket = new Socket();
SocketImpl si = getSocketImpl(socket);
@ -334,7 +360,7 @@ public class SocketImplCombinations {
serverSocketAccept(socket, (ss, s) -> {
assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
assertTrue(s == socket);
assertSame(socket, s);
SocketImpl psi = getSocketImpl(socket);
assertTrue(isPlatformSocketImpl(psi));
checkFields(psi);
@ -345,26 +371,28 @@ public class SocketImplCombinations {
* Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
* the Socket to accept is created with a custom SocketImpl.
*/
@Test
public void testServerSocketAccept4a() throws IOException {
SocketImpl clientImpl = new CustomSocketImpl(false);
Socket socket = new Socket(clientImpl) { };
assertTrue(getSocketImpl(socket) == clientImpl);
assertSame(clientImpl, getSocketImpl(socket));
try (ServerSocket ss = serverSocketToAccept(socket)) {
expectThrows(IOException.class, ss::accept);
assertThrows(IOException.class, ss::accept);
} finally {
socket.close();
}
}
@Test
public void testServerSocketAccept4b() throws IOException {
SocketImpl clientImpl = new CustomSocketImpl(false);
Socket socket = new Socket(clientImpl) { };
assertTrue(getSocketImpl(socket) == clientImpl);
assertSame(clientImpl, getSocketImpl(socket));
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
try (ServerSocket ss = serverSocketToAccept(socket)) {
expectThrows(IOException.class, ss::accept);
assertThrows(IOException.class, ss::accept);
} finally {
setSocketSocketImplFactory(null);
socket.close();
@ -375,42 +403,46 @@ public class SocketImplCombinations {
* Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
* the Socket to accept is created no SocketImpl.
*/
@Test
public void testServerSocketAccept5a() throws IOException {
SocketImpl serverImpl = new CustomSocketImpl(true);
try (ServerSocket ss = new ServerSocket(serverImpl) { }) {
ss.bind(loopbackSocketAddress());
expectThrows(IOException.class, ss::accept);
assertThrows(IOException.class, ss::accept);
}
}
@Test
public void testServerSocketAccept5b() throws IOException {
var socket = new Socket((SocketImpl) null) { };
assertTrue(getSocketImpl(socket) == null);
assertNull(getSocketImpl(socket));
SocketImpl serverImpl = new CustomSocketImpl(true);
try (ServerSocket ss = serverSocketToAccept(serverImpl, socket)) {
expectThrows(IOException.class, ss::accept);
assertThrows(IOException.class, ss::accept);
} finally {
socket.close();
}
}
@Test
public void testServerSocketAccept5c() throws IOException {
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
try (ServerSocket ss = new ServerSocket(0)) {
expectThrows(IOException.class, ss::accept);
assertThrows(IOException.class, ss::accept);
} finally {
setServerSocketImplFactory(null);
}
}
@Test
public void testServerSocketAccept5d() throws IOException {
var socket = new Socket((SocketImpl) null) { };
assertTrue(getSocketImpl(socket) == null);
assertNull(getSocketImpl(socket));
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
try (ServerSocket ss = serverSocketToAccept(socket)) {
expectThrows(IOException.class, ss::accept);
assertThrows(IOException.class, ss::accept);
} finally {
setServerSocketImplFactory(null);
socket.close();
@ -422,16 +454,17 @@ public class SocketImplCombinations {
* the Socket to accept is created with no SocketImpl, and there is a custom
* client SocketImplFactory set.
*/
@Test
public void testServerSocketAccept6() throws Exception {
var socket = new Socket((SocketImpl) null) { };
assertTrue(getSocketImpl(socket) == null);
assertNull(getSocketImpl(socket));
SocketImpl serverImpl = new CustomSocketImpl(true);
SocketImplFactory clientFactory = () -> new CustomSocketImpl(false);
serverSocketAccept(serverImpl, socket, clientFactory, (ss, s) -> {
assertTrue(getSocketImpl(ss) == serverImpl);
assertSame(serverImpl, getSocketImpl(ss));
SocketImpl si = getSocketImpl(s);
assertTrue(si instanceof CustomSocketImpl);
assertInstanceOf(CustomSocketImpl.class, si);
checkFields(si);
});
}
@ -441,6 +474,7 @@ public class SocketImplCombinations {
* the Socket to accept is created with a SocketImpl that delegates to a
* platform SocketImpl.
*/
@Test
public void testServerSocketAccept7a() throws IOException {
var socket = new Socket();
SocketImpl si = getSocketImpl(socket);
@ -450,12 +484,13 @@ public class SocketImplCombinations {
SocketImpl serverImpl = new CustomSocketImpl(true);
try (ServerSocket ss = serverSocketToAccept(serverImpl, socket)) {
expectThrows(IOException.class, ss::accept);
assertThrows(IOException.class, ss::accept);
} finally {
socket.close();
}
}
@Test
public void testServerSocketAccept7b() throws IOException {
var socket = new Socket();
SocketImpl si = getSocketImpl(socket);
@ -465,7 +500,7 @@ public class SocketImplCombinations {
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
try (ServerSocket ss = serverSocketToAccept(socket)) {
expectThrows(IOException.class, ss::accept);
assertThrows(IOException.class, ss::accept);
} finally {
setServerSocketImplFactory(null);
socket.close();
@ -476,16 +511,17 @@ public class SocketImplCombinations {
* Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
* the Socket to accept is created with a custom SocketImpl.
*/
@Test
public void testServerSocketAccept8() throws Exception {
SocketImpl clientImpl = new CustomSocketImpl(false);
Socket socket = new Socket(clientImpl) { };
assertTrue(getSocketImpl(socket) == clientImpl);
assertSame(clientImpl, getSocketImpl(socket));
SocketImpl serverImpl = new CustomSocketImpl(true);
SocketImplFactory clientFactory = () -> new CustomSocketImpl(false);
serverSocketAccept(serverImpl, socket, clientFactory, (ss, s) -> {
assertTrue(getSocketImpl(ss) == serverImpl);
assertTrue(getSocketImpl(s) == clientImpl);
assertSame(serverImpl, getSocketImpl(ss));
assertSame(clientImpl, getSocketImpl(s));
checkFields(clientImpl);
});
}
@ -751,11 +787,14 @@ public class SocketImplCombinations {
InetAddress address = get(si, "address");
int port = get(si, "port");
int localport = get(si, "localport");
assertTrue(fd.valid() && address != null && port != 0 && localport != 0);
assertTrue(fd.valid());
assertNotNull(address);
assertNotEquals(0, port);
assertNotEquals(0, localport);
}
/**
* Custom SocketImpl that is layed on a SocketChannel or ServerSocketChannel
* Custom SocketImpl that is layered on a SocketChannel or ServerSocketChannel
*/
static class CustomSocketImpl extends SocketImpl {
private final boolean server;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2026, 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
@ -25,7 +25,7 @@
* @test
* @bug 8224477
* @summary Basic test for java.net.SocketImpl default behavior
* @run testng TestDefaultBehavior
* @run junit ${test.main.class}
*/
import java.io.IOException;
@ -36,11 +36,13 @@ import java.net.SocketAddress;
import java.net.SocketImpl;
import java.net.SocketOption;
import java.util.Set;
import org.testng.annotations.Test;
import static java.lang.Boolean.*;
import static java.net.StandardSocketOptions.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.expectThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
public class TestDefaultBehavior {
@ -51,21 +53,21 @@ public class TestDefaultBehavior {
public void socketImpl() {
CustomSocketImpl csi = new CustomSocketImpl();
assertEquals(csi.supportedOptions().size(), 0);
assertEquals(0, csi.supportedOptions().size());
expectThrows(NPE, () -> csi.setOption(null, null));
expectThrows(NPE, () -> csi.setOption(null, 1));
expectThrows(UOE, () -> csi.setOption(SO_RCVBUF, 100));
expectThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, TRUE));
expectThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, FALSE));
expectThrows(UOE, () -> csi.setOption(FAKE_SOCK_OPT, TRUE));
expectThrows(UOE, () -> csi.setOption(FAKE_SOCK_OPT, FALSE));
expectThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, TRUE));
assertThrows(NPE, () -> csi.setOption(null, null));
assertThrows(NPE, () -> csi.setOption(null, 1));
assertThrows(UOE, () -> csi.setOption(SO_RCVBUF, 100));
assertThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, TRUE));
assertThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, FALSE));
assertThrows(UOE, () -> csi.setOption(FAKE_SOCK_OPT, TRUE));
assertThrows(UOE, () -> csi.setOption(FAKE_SOCK_OPT, FALSE));
assertThrows(UOE, () -> csi.setOption(SO_KEEPALIVE, TRUE));
expectThrows(NPE, () -> csi.getOption(null));
expectThrows(UOE, () -> csi.getOption(SO_RCVBUF));
expectThrows(UOE, () -> csi.getOption(SO_KEEPALIVE));
expectThrows(UOE, () -> csi.getOption(FAKE_SOCK_OPT));
assertThrows(NPE, () -> csi.getOption(null));
assertThrows(UOE, () -> csi.getOption(SO_RCVBUF));
assertThrows(UOE, () -> csi.getOption(SO_KEEPALIVE));
assertThrows(UOE, () -> csi.getOption(FAKE_SOCK_OPT));
}
static final SocketOption<Boolean> FAKE_SOCK_OPT = new SocketOption<>() {