undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,483 @@
/*
* Copyright (c) 2019, 2025, 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 8221481
* @summary Test the platform SocketImpl when used in unintended ways
* @compile/module=java.base java/net/PlatformSocketImpl.java
* @run testng/othervm BadUsages
*/
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketOption;
import java.net.SocketOptions;
import java.net.StandardSocketOptions;
import java.net.PlatformSocketImpl; // test helper
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
* SocketImpl does not specify how the SocketImpl behaves when used in ways
* that are not intended, e.g. invoking socket operations before the socket is
* created or trying to establish a connection after the socket is connected or
* closed.
*
* This test exercises the platform SocketImpl to test that it is reliable, and
* throws reasonable exceptions, for these scenarios.
*/
@Test
public class BadUsages {
/**
* Test create when already created.
*/
public void testCreate1() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.create(true));
}
}
/**
* Test create when closed.
*/
public void testCreate2() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.create(true));
}
/**
* Test create when not a stream socket.
*/
public void testCreate3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
expectThrows(IOException.class, () -> impl.create(false));
}
}
/**
* Test connect when not created.
*/
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));
}
}
/**
* Test connect with unsupported address type.
*/
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));
}
}
/**
* Test connect with an unresolved address.
*/
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));
}
}
/**
* Test connect when already connected.
*/
public void testConnect4() throws IOException {
try (var ss = new ServerSocket();
var impl = new PlatformSocketImpl(false)) {
var loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0));
impl.create(true);
int port = ss.getLocalPort();
impl.connect(loopback, port);
expectThrows(IOException.class, () -> impl.connect(loopback, port));
}
}
/**
* Test connect when closed.
*/
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));
}
}
/**
* Test bind when not created.
*/
public void testBind1() throws IOException {
var impl = new PlatformSocketImpl(false);
var loopback = InetAddress.getLoopbackAddress();
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
}
/**
* Test bind when already bound.
*/
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));
}
}
/**
* Test bind when connected.
*/
public void testBind3() throws IOException {
try (var ss = new ServerSocket();
var impl = new PlatformSocketImpl(false)) {
var loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0));
impl.create(true);
impl.connect(ss.getLocalSocketAddress(), 0);
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
}
}
/**
* Test bind when closed.
*/
public void testBind4() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
var loopback = InetAddress.getLoopbackAddress();
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
}
/**
* Test listen when not created.
*/
public void testListen1() {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.listen(16));
}
/**
* Test listen when not bound.
*/
public void testListen2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.listen(16));
}
}
/**
* Test listen when closed.
*/
public void testListen3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.listen(16));
}
/**
* Test accept when not created.
*/
public void testAccept1() throws IOException {
var impl = new PlatformSocketImpl(true);
var si = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.accept(si));
}
/**
* Test accept when not bound.
*/
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));
}
}
/**
* Test accept when closed.
*/
public void testAccept4() throws IOException {
var impl = new PlatformSocketImpl(true);
impl.close();
var si = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.accept(si));
}
/**
* Test accept with SocketImpl that is already created.
*/
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));
}
}
/**
* Test accept with SocketImpl that is closed.
*/
public void testAccept6() throws IOException {
try (var impl = new PlatformSocketImpl(true);
var si = new PlatformSocketImpl(false)) {
impl.create(true);
impl.bind(InetAddress.getLoopbackAddress(), 0);
si.create(true);
si.close();
expectThrows(IOException.class, () -> impl.accept(si));
}
}
/**
* Test available when not created.
*/
public void testAvailable1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.available());
}
/**
* Test available when created but not connected.
*/
public void testAvailable2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.available());
}
}
/**
* Test available when closed.
*/
public void testAvailable3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.available());
}
/**
* Test setOption when not created.
*/
public void testSetOption1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class,
() -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));
// legacy
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_REUSEADDR, true));
}
/**
* Test setOption when closed.
*/
public void testSetOption2() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class,
() -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));
// legacy
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_REUSEADDR, true));
}
/**
* Test setOption with unsupported option.
*/
public void testSetOption3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
var opt = new SocketOption<String>() {
@Override public String name() { return "birthday"; }
@Override public Class<String> type() { return String.class; }
};
expectThrows(UnsupportedOperationException.class, () -> impl.setOption(opt, ""));
// legacy
expectThrows(SocketException.class, () -> impl.setOption(-1, ""));
}
}
/**
* Test setOption(int, Object) with invalid values.
*/
public void testSetOption4() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_REUSEADDR, -1));
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_TIMEOUT, -1));
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_SNDBUF, -1));
expectThrows(SocketException.class,
() -> impl.setOption(SocketOptions.SO_RCVBUF, -1));
}
}
/**
* Test getOption when not created.
*/
public void testGetOption1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class,
() -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));
expectThrows(SocketException.class,
() -> impl.getOption(-1));
}
/**
* Test getOption when closed.
*/
public void testGetOption2() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class,
() -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));
expectThrows(SocketException.class,
() -> impl.getOption(SocketOptions.SO_REUSEADDR));
}
/**
* Test getOption with unsupported option.
*/
public void testGetOption3() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
var opt = new SocketOption<String>() {
@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));
}
}
/**
* Test shutdownInput when not created.
*/
public void testShutdownInput1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.shutdownInput());
}
/**
* Test shutdownInput when not connected.
*/
public void testShutdownInput2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.shutdownInput());
}
}
/**
* Test shutdownInput when closed.
*/
public void testShutdownInput3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.shutdownInput());
}
/**
* Test shutdownOutput when not created.
*/
public void testShutdownOutput1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.shutdownOutput());
}
/**
* Test shutdownOutput when not connected.
*/
public void testShutdownOutput2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.shutdownOutput());
}
}
/**
* Test shutdownOutput when closed.
*/
public void testShutdownOutput3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.shutdownOutput());
}
/**
* Test sendUrgentData when not created.
*/
public void testSendUrgentData1() throws IOException {
var impl = new PlatformSocketImpl(false);
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
}
/**
* Test sendUrgentData when not connected.
*/
public void testSendUrgentData2() throws IOException {
try (var impl = new PlatformSocketImpl(false)) {
impl.create(true);
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
}
}
/**
* Test sendUrgentData when closed.
*/
public void testSendUrgentData3() throws IOException {
var impl = new PlatformSocketImpl(false);
impl.close();
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
}
}

View file

@ -0,0 +1,189 @@
/*
* Copyright (c) 2018, 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 8213418
* @summary Ensure correct impl supported socket options
* @run testng ImplSupportedOptions
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
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;
public class ImplSupportedOptions {
@Test
public void socketSupportedOptions() throws Exception {
Socket s = new Socket();
Set<?> standardOptions = s.supportedOptions();
assertTrue(standardOptions.contains(StandardSocketOptions.SO_LINGER),
"Expected SO_LINGER, in:" + standardOptions);
assertEquals(standardOptions, s.supportedOptions());
assertEquals(standardOptions, s.supportedOptions());
s = new DummySocket();
Set<?> dummyOptions = s.supportedOptions();
assertEquals(dummyOptions.size(), 1);
assertTrue(dummyOptions.contains(DummySocketImpl.SOCKET_OPT));
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(dummyOptions, s.supportedOptions());
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());
s = new DummySocket();
dummyOptions = s.supportedOptions();
assertEquals(dummyOptions.size(), 1);
assertTrue(dummyOptions.contains(DummySocketImpl.SOCKET_OPT));
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(dummyOptions, s.supportedOptions());
}
@Test
public void serverSocketSupportedOptions() throws Exception {
ServerSocket s = new ServerSocket();
Set<?> standardOptions = s.supportedOptions();
assertTrue(standardOptions.contains(StandardSocketOptions.SO_REUSEADDR),
"Expected SO_REUSEADDR, in:" + standardOptions);
assertEquals(standardOptions, s.supportedOptions());
assertEquals(standardOptions, s.supportedOptions());
s = new DummyServerSocket();
Set<?> dummyOptions = s.supportedOptions();
assertEquals(dummyOptions.size(), 1);
assertTrue(dummyOptions.contains(DummySocketImpl.SOCKET_OPT));
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(dummyOptions, s.supportedOptions());
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());
s = new DummyServerSocket();
dummyOptions = s.supportedOptions();
assertEquals(dummyOptions.size(), 1);
assertTrue(dummyOptions.contains(DummySocketImpl.SOCKET_OPT));
assertEquals(dummyOptions, s.supportedOptions());
assertEquals(dummyOptions, s.supportedOptions());
}
static class DummySocket extends Socket {
DummySocket() throws IOException {
super(new DummySocketImpl());
}
}
static class DummyServerSocket extends ServerSocket {
DummyServerSocket() throws IOException {
super(new DummySocketImpl());
}
}
static class DummySocketImpl extends SocketImpl {
public static final SocketOption<String> SOCKET_OPT =
new ImplSocketOption<>("SOCKET_OPT", String.class);
private static class ImplSocketOption<T> implements SocketOption<T> {
private final String name;
private final Class<T> type;
ImplSocketOption(String name, Class<T> type) {
this.name = name;
this.type = type;
}
@Override public String name() { return name; }
@Override public Class<T> type() { return type; }
@Override public String toString() { return name; }
}
private final Set<SocketOption<?>> SO = Set.of(SOCKET_OPT);
@Override
public Set<SocketOption<?>> supportedOptions() { return SO; }
// ---
@Override
protected void create(boolean stream) throws IOException { }
@Override
protected void connect(String host, int port) throws IOException { }
@Override
protected void connect(InetAddress address, int port) throws IOException { }
@Override
protected void connect(SocketAddress address, int timeout) throws IOException { }
@Override
protected void bind(InetAddress host, int port) throws IOException { }
@Override
protected void listen(int backlog) throws IOException { }
@Override
protected void accept(SocketImpl s) throws IOException { }
@Override
protected InputStream getInputStream() throws IOException { return null; }
@Override
protected OutputStream getOutputStream() throws IOException { return null; }
@Override
protected int available() throws IOException { return 0; }
@Override
protected void close() throws IOException { }
@Override
protected void sendUrgentData(int data) throws IOException { }
@Override
public void setOption(int optID, Object value) throws SocketException { }
@Override
public Object getOption(int optID) throws SocketException { return null; }
}
}

View file

@ -0,0 +1,864 @@
/*
* Copyright (c) 2019, 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 8220493
* @modules java.base/java.net:+open java.base/sun.nio.ch:+open
* @run testng/othervm SocketImplCombinations
* @summary Test Socket and ServerSocket with combinations of SocketImpls
*/
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketImpl;
import java.net.SocketImplFactory;
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.*;
@Test
public class SocketImplCombinations {
/**
* Test creating an unconnected Socket, it should be created with a platform SocketImpl.
*/
public void testNewSocket1() throws IOException {
try (Socket s = new Socket()) {
SocketImpl si = getSocketImpl(s);
assertTrue(isSocksSocketImpl(si));
SocketImpl delegate = getDelegate(si);
assertTrue(isPlatformSocketImpl(delegate));
}
}
/**
* Test creating a connected Socket, it should be created with a platform SocketImpl.
*/
public void testNewSocket2() throws IOException {
try (ServerSocket ss = boundServerSocket()) {
try (Socket s = new Socket(ss.getInetAddress(), ss.getLocalPort())) {
SocketImpl si = getSocketImpl(s);
assertTrue(isSocksSocketImpl(si));
SocketImpl delegate = getDelegate(si);
assertTrue(isPlatformSocketImpl(delegate));
}
}
}
/**
* Test creating a Socket for a DIRECT connection, it should be created with a
* platform SocketImpl.
*/
public void testNewSocket3() throws IOException {
try (Socket s = new Socket(Proxy.NO_PROXY)) {
SocketImpl si = getSocketImpl(s);
assertTrue(isPlatformSocketImpl(si));
}
}
/**
* Test creating a Socket for a SOCKS connection, it should be created with a
* SOCKS SocketImpl.
*/
public void testNewSocket4() throws IOException {
var address = new InetSocketAddress("127.0.0.1", 1080);
var socksProxy = new Proxy(Proxy.Type.SOCKS, address);
try (Socket s = new Socket(socksProxy)) {
SocketImpl si = getSocketImpl(s);
assertTrue(isSocksSocketImpl(si));
SocketImpl delegate = getDelegate(si);
assertTrue(isPlatformSocketImpl(delegate));
}
}
/**
* Test creating a Socket for a HTTP proxy connection, it should be created with
* a HTTP proxy SocketImpl.
*/
public void testNewSocket5() throws IOException {
var address = new InetSocketAddress("127.0.0.1", 8080);
var httpProxy = new Proxy(Proxy.Type.HTTP, address);
try (Socket s = new Socket(httpProxy)) {
SocketImpl si = getSocketImpl(s);
assertTrue(isHttpConnectSocketImpl(si));
SocketImpl delegate = getDelegate(si);
assertTrue(isPlatformSocketImpl(delegate));
}
}
/**
* Test creating a Socket no SocketImpl. A platform SocketImpl should be
* created lazily.
*/
public void testNewSocket6() throws IOException {
Socket s = new Socket((SocketImpl) null) { };
try (s) {
assertTrue(getSocketImpl(s) == null);
s.bind(loopbackSocketAddress()); // force SocketImpl to be created
SocketImpl si = getSocketImpl(s);
assertTrue(isSocksSocketImpl(si));
SocketImpl delegate = getDelegate(si);
assertTrue(isPlatformSocketImpl(delegate));
}
}
/**
* Test creating a Socket with a custom SocketImpl.
*/
public void testNewSocket7() throws IOException {
Socket s = new Socket(new CustomSocketImpl(false)) { };
try (s) {
SocketImpl si = getSocketImpl(s);
assertTrue(si instanceof CustomSocketImpl);
}
}
/**
* Test creating a Socket when there is a SocketImplFactory set.
*/
public void testNewSocket8() throws IOException {
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
try (Socket s = new Socket()) {
SocketImpl si = getSocketImpl(s);
assertTrue(si instanceof CustomSocketImpl);
} finally {
setSocketSocketImplFactory(null);
}
}
/**
* Test creating a Socket for a DIRECT connection when there is a
* SocketImplFactory set.
*/
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);
} finally {
setSocketSocketImplFactory(null);
}
}
/**
* Test creating a Socket for a SOCKS connection when there is a
* SocketImplFactory set.
*/
public void testNewSocket10() throws IOException {
var address = new InetSocketAddress("127.0.0.1", 1080);
var socksProxy = new Proxy(Proxy.Type.SOCKS, address);
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
try (Socket s = new Socket(socksProxy)) {
SocketImpl si = getSocketImpl(s);
assertTrue(isSocksSocketImpl(si));
SocketImpl delegate = getDelegate(si);
assertTrue(isPlatformSocketImpl(delegate));
} finally {
setSocketSocketImplFactory(null);
}
}
/**
* Test creating a Socket for a HTTP proxy connection when there is a
* SocketImplFactory set.
*/
public void testNewSocket11() throws IOException {
var address = new InetSocketAddress("127.0.0.1", 8080);
var httpProxy = new Proxy(Proxy.Type.HTTP, address);
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
try (Socket s = new Socket(httpProxy)) {
SocketImpl si = getSocketImpl(s);
assertTrue(isHttpConnectSocketImpl(si));
SocketImpl delegate = getDelegate(si);
assertTrue(isPlatformSocketImpl(delegate));
} finally {
setSocketSocketImplFactory(null);
}
}
/**
* Test creating a Socket no SocketImpl when there is a SocketImplFactory set.
*/
public void testNewSocket12() throws IOException {
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
try {
Socket s = new Socket((SocketImpl) null) { };
try (s) {
assertTrue(getSocketImpl(s) == null);
s.bind(loopbackSocketAddress()); // force SocketImpl to be created
assertTrue(getSocketImpl(s) instanceof CustomSocketImpl);
}
} finally {
setSocketSocketImplFactory(null);
}
}
/**
* Test creating an unbound ServerSocket, it should be created with a platform
* SocketImpl.
*/
public void testNewServerSocket1() throws IOException {
try (ServerSocket ss = new ServerSocket()) {
SocketImpl si = getSocketImpl(ss);
assertTrue(isPlatformSocketImpl(si));
}
}
/**
* Test creating a bound ServerSocket, it should be created with a platform
* SocketImpl.
*/
public void testNewServerSocket2() throws IOException {
try (ServerSocket ss = new ServerSocket(0)) {
SocketImpl si = getSocketImpl(ss);
assertTrue(isPlatformSocketImpl(si));
}
}
/**
* Test creating a ServerSocket with a custom SocketImpl.
*/
public void testNewServerSocket3() throws IOException {
ServerSocket ss = new ServerSocket(new CustomSocketImpl(true)) { };
try (ss) {
SocketImpl si = getSocketImpl(ss);
assertTrue(si instanceof CustomSocketImpl);
}
}
/**
* Test creating an unbound ServerSocket when there is a SocketImplFactory set.
*/
public void testNewServerSocket4() throws IOException {
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
try (ServerSocket ss = new ServerSocket()) {
SocketImpl si = getSocketImpl(ss);
assertTrue(si instanceof CustomSocketImpl);
} finally {
setServerSocketImplFactory(null);
}
}
/**
* Test creating a bound ServerSocket when there is a SocketImplFactory set.
*/
public void testNewServerSocket5() throws IOException {
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
try (ServerSocket ss = new ServerSocket(0)) {
SocketImpl si = getSocketImpl(ss);
assertTrue(si instanceof CustomSocketImpl);
} finally {
setServerSocketImplFactory(null);
}
}
/**
* Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
* the Socket to accept is created with no SocketImpl.
*/
public void testServerSocketAccept1() throws IOException {
var socket = new Socket((SocketImpl) null) { };
assertTrue(getSocketImpl(socket) == null);
serverSocketAccept(socket, (ss, s) -> {
assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
assertTrue(s == socket);
SocketImpl si = getSocketImpl(s);
assertTrue(isPlatformSocketImpl(si));
checkFields(si);
});
}
/**
* Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
* the Socket to accept is created with no SocketImpl, and there is a custom
* client SocketImplFactory set.
*/
public void testServerSocketAccept2() throws IOException {
var socket = new Socket((SocketImpl) null) { };
assertTrue(getSocketImpl(socket) == null);
serverSocketAccept(socket, () -> new CustomSocketImpl(false), (ss, s) -> {
assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
assertTrue(s == socket);
SocketImpl si = getSocketImpl(s);
assertTrue(isPlatformSocketImpl(si));
checkFields(si);
});
}
/**
* Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
* the Socket to accept is created with a SocketImpl that delegates to a
* platform SocketImpl.
*/
public void testServerSocketAccept3() throws IOException {
var socket = new Socket();
SocketImpl si = getSocketImpl(socket);
assertTrue(isSocksSocketImpl(si));
SocketImpl delegate = getDelegate(si);
assertTrue(isPlatformSocketImpl(delegate));
serverSocketAccept(socket, (ss, s) -> {
assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
assertTrue(s == socket);
SocketImpl psi = getSocketImpl(socket);
assertTrue(isPlatformSocketImpl(psi));
checkFields(psi);
});
}
/**
* Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
* the Socket to accept is created with a custom SocketImpl.
*/
public void testServerSocketAccept4a() throws IOException {
SocketImpl clientImpl = new CustomSocketImpl(false);
Socket socket = new Socket(clientImpl) { };
assertTrue(getSocketImpl(socket) == clientImpl);
try (ServerSocket ss = serverSocketToAccept(socket)) {
expectThrows(IOException.class, ss::accept);
} finally {
socket.close();
}
}
public void testServerSocketAccept4b() throws IOException {
SocketImpl clientImpl = new CustomSocketImpl(false);
Socket socket = new Socket(clientImpl) { };
assertTrue(getSocketImpl(socket) == clientImpl);
setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
try (ServerSocket ss = serverSocketToAccept(socket)) {
expectThrows(IOException.class, ss::accept);
} finally {
setSocketSocketImplFactory(null);
socket.close();
}
}
/**
* Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
* the Socket to accept is created no SocketImpl.
*/
public void testServerSocketAccept5a() throws IOException {
SocketImpl serverImpl = new CustomSocketImpl(true);
try (ServerSocket ss = new ServerSocket(serverImpl) { }) {
ss.bind(loopbackSocketAddress());
expectThrows(IOException.class, ss::accept);
}
}
public void testServerSocketAccept5b() throws IOException {
var socket = new Socket((SocketImpl) null) { };
assertTrue(getSocketImpl(socket) == null);
SocketImpl serverImpl = new CustomSocketImpl(true);
try (ServerSocket ss = serverSocketToAccept(serverImpl, socket)) {
expectThrows(IOException.class, ss::accept);
} finally {
socket.close();
}
}
public void testServerSocketAccept5c() throws IOException {
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
try (ServerSocket ss = new ServerSocket(0)) {
expectThrows(IOException.class, ss::accept);
} finally {
setServerSocketImplFactory(null);
}
}
public void testServerSocketAccept5d() throws IOException {
var socket = new Socket((SocketImpl) null) { };
assertTrue(getSocketImpl(socket) == null);
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
try (ServerSocket ss = serverSocketToAccept(socket)) {
expectThrows(IOException.class, ss::accept);
} finally {
setServerSocketImplFactory(null);
socket.close();
}
}
/**
* Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
* the Socket to accept is created with no SocketImpl, and there is a custom
* client SocketImplFactory set.
*/
public void testServerSocketAccept6() throws Exception {
var socket = new Socket((SocketImpl) null) { };
assertTrue(getSocketImpl(socket) == null);
SocketImpl serverImpl = new CustomSocketImpl(true);
SocketImplFactory clientFactory = () -> new CustomSocketImpl(false);
serverSocketAccept(serverImpl, socket, clientFactory, (ss, s) -> {
assertTrue(getSocketImpl(ss) == serverImpl);
SocketImpl si = getSocketImpl(s);
assertTrue(si instanceof CustomSocketImpl);
checkFields(si);
});
}
/**
* Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
* the Socket to accept is created with a SocketImpl that delegates to a
* platform SocketImpl.
*/
public void testServerSocketAccept7a() throws IOException {
var socket = new Socket();
SocketImpl si = getSocketImpl(socket);
assertTrue(isSocksSocketImpl(si));
SocketImpl delegate = getDelegate(si);
assertTrue(isPlatformSocketImpl(delegate));
SocketImpl serverImpl = new CustomSocketImpl(true);
try (ServerSocket ss = serverSocketToAccept(serverImpl, socket)) {
expectThrows(IOException.class, ss::accept);
} finally {
socket.close();
}
}
public void testServerSocketAccept7b() throws IOException {
var socket = new Socket();
SocketImpl si = getSocketImpl(socket);
assertTrue(isSocksSocketImpl(si));
SocketImpl delegate = getDelegate(si);
assertTrue(isPlatformSocketImpl(delegate));
setServerSocketImplFactory(() -> new CustomSocketImpl(true));
try (ServerSocket ss = serverSocketToAccept(socket)) {
expectThrows(IOException.class, ss::accept);
} finally {
setServerSocketImplFactory(null);
socket.close();
}
}
/**
* Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
* the Socket to accept is created with a custom SocketImpl.
*/
public void testServerSocketAccept8() throws Exception {
SocketImpl clientImpl = new CustomSocketImpl(false);
Socket socket = new Socket(clientImpl) { };
assertTrue(getSocketImpl(socket) == clientImpl);
SocketImpl serverImpl = new CustomSocketImpl(true);
SocketImplFactory clientFactory = () -> new CustomSocketImpl(false);
serverSocketAccept(serverImpl, socket, clientFactory, (ss, s) -> {
assertTrue(getSocketImpl(ss) == serverImpl);
assertTrue(getSocketImpl(s) == clientImpl);
checkFields(clientImpl);
});
}
/**
* Creates a ServerSocket that returns the given Socket from accept.
* The consumer is invoked with the server socket and the accepted socket.
*/
static void serverSocketAccept(Socket socket,
BiConsumer<ServerSocket, Socket> consumer)
throws IOException
{
Socket s1 = null;
Socket s2 = null;
try (ServerSocket ss = serverSocketToAccept(socket)) {
s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
s2 = ss.accept();
consumer.accept(ss, s2);
} finally {
if (s1 != null) s1.close();
if (s2 != null) s2.close();
}
}
/**
* Creates a ServerSocket that returns the given Socket from accept. The
* given SocketImplFactory is set during the accept and the consumer is
* invoked when the server socket and the accepted socket.
*/
static void serverSocketAccept(Socket socket,
SocketImplFactory factory,
BiConsumer<ServerSocket, Socket> consumer)
throws IOException
{
Socket s1 = null;
Socket s2 = null;
try (ServerSocket ss = serverSocketToAccept(socket)) {
s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
setSocketSocketImplFactory(factory);
try {
s2 = ss.accept();
} finally {
setSocketSocketImplFactory(null);
}
consumer.accept(ss, s2);
} finally {
if (s1 != null) s1.close();
if (s2 != null) s2.close();
}
}
/**
* Creates a ServerSocket with a SocketImpl returns the given Socket from
* accept. The given SocketImplFactory is set during the accept and the
* consumer is invoked when the server socket and the accepted socket.
*/
static void serverSocketAccept(SocketImpl impl,
Socket socket,
SocketImplFactory factory,
BiConsumer<ServerSocket, Socket> consumer)
throws IOException
{
Socket s1 = null;
Socket s2 = null;
try (ServerSocket ss = serverSocketToAccept(impl, socket)) {
s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
setSocketSocketImplFactory(factory);
try {
s2 = ss.accept();
} finally {
setSocketSocketImplFactory(null);
}
consumer.accept(ss, s2);
} finally {
if (s1 != null) s1.close();
if (s2 != null) s2.close();
}
}
/**
* Returns a new InetSocketAddress with the loopback interface
* and port 0.
*/
static InetSocketAddress loopbackSocketAddress() {
InetAddress loopback = InetAddress.getLoopbackAddress();
return new InetSocketAddress(loopback, 0);
}
/**
* Returns a ServerSocket bound to a port on the loopback address
*/
static ServerSocket boundServerSocket() throws IOException {
ServerSocket ss = new ServerSocket();
ss.bind(loopbackSocketAddress());
return ss;
}
/**
* Creates a ServerSocket that returns the given Socket from accept.
*/
static ServerSocket serverSocketToAccept(Socket s) throws IOException {
ServerSocket ss = new ServerSocket() {
@Override
public Socket accept() throws IOException {
implAccept(s);
return s;
}
};
ss.bind(loopbackSocketAddress());
return ss;
}
/**
* Creates a ServerSocket with a SocketImpl that returns the given Socket
* from accept.
*/
static ServerSocket serverSocketToAccept(SocketImpl impl, Socket s) throws IOException {
ServerSocket ss = new ServerSocket(impl) {
@Override
public Socket accept() throws IOException {
implAccept(s);
return s;
}
};
ss.bind(loopbackSocketAddress());
return ss;
}
/**
* Returns the socket's SocketImpl
*/
static SocketImpl getSocketImpl(Socket s) {
try {
Field f = Socket.class.getDeclaredField("impl");
f.setAccessible(true);
return (SocketImpl) f.get(s);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Returns the server socket's SocketImpl
*/
static SocketImpl getSocketImpl(ServerSocket ss) {
try {
Field f = ServerSocket.class.getDeclaredField("impl");
f.setAccessible(true);
return (SocketImpl) f.get(ss);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Returns the SocketImpl that the given SocketImpl delegates to
*/
static SocketImpl getDelegate(SocketImpl si) {
try {
Class<?> clazz = Class.forName("java.net.DelegatingSocketImpl");
Field f = clazz.getDeclaredField("delegate");
f.setAccessible(true);
return (SocketImpl) f.get(si);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Returns the value of a SocketImpl field
*/
static <T> T get(SocketImpl si, String name) {
try {
Field f = SocketImpl.class.getDeclaredField(name);
f.setAccessible(true);
return (T) f.get(si);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Sets the value of SocketImpl field
*/
static void set(SocketImpl si, String name, Object value) {
try {
Field f = SocketImpl.class.getDeclaredField(name);
f.setAccessible(true);
f.set(si, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Returns true if the SocketImpl is a PlatformSocketImpl
*/
static boolean isPlatformSocketImpl(SocketImpl si) {
try {
Class<?> clazz = Class.forName("sun.net.PlatformSocketImpl");
return clazz.isInstance(si);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Returns true if the SocketImpl is a SocksSocketImpl
*/
static boolean isSocksSocketImpl(SocketImpl si) {
try {
Class<?> clazz = Class.forName("java.net.SocksSocketImpl");
return clazz.isInstance(si);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Returns true if the SocketImpl is a HttpConnectSocketImpl
*/
static boolean isHttpConnectSocketImpl(SocketImpl si) {
try {
Class<?> clazz = Class.forName("java.net.HttpConnectSocketImpl");
return clazz.isInstance(si);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Socket.setSocketImplFactory(SocketImplFactory)
*/
static void setSocketSocketImplFactory(SocketImplFactory factory) {
try {
Field f = Socket.class.getDeclaredField("factory");
f.setAccessible(true);
f.set(null, factory);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* ServerSocket.setSocketFactory(SocketImplFactory)
*/
static void setServerSocketImplFactory(SocketImplFactory factory) {
try {
Field f = ServerSocket.class.getDeclaredField("factory");
f.setAccessible(true);
f.set(null, factory);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Checks the 4 protected fields of a SocketImpl to make sure that they
* have been initialized.
*/
static void checkFields(SocketImpl si) {
FileDescriptor fd = get(si, "fd");
InetAddress address = get(si, "address");
int port = get(si, "port");
int localport = get(si, "localport");
assertTrue(fd.valid() && address != null && port != 0 && localport != 0);
}
/**
* Custom SocketImpl that is layed on a SocketChannel or ServerSocketChannel
*/
static class CustomSocketImpl extends SocketImpl {
private final boolean server;
private ServerSocketChannel ssc;
private SocketChannel sc;
CustomSocketImpl(boolean server) {
this.server = server;
}
@Override
protected void create(boolean stream) throws IOException {
if (server) {
ssc = ServerSocketChannel.open();
} else {
sc = SocketChannel.open();
}
}
@Override
protected void connect(String host, int port) throws IOException {
connect(new InetSocketAddress(host, port), 0);
}
@Override
protected void connect(InetAddress address, int port) throws IOException {
connect(new InetSocketAddress(address, port), 0);
}
@Override
protected void connect(SocketAddress remote, int timeout) throws IOException {
sc.connect(remote);
super.address = ((InetSocketAddress) remote).getAddress();
super.port = ((InetSocketAddress) remote).getPort();
}
@Override
protected void bind(InetAddress address, int port) throws IOException {
if (server) {
ssc.bind(new InetSocketAddress(address, port));
super.localport = ssc.socket().getLocalPort();
} else {
sc.bind(new InetSocketAddress(address, port));
super.localport = sc.socket().getLocalPort();
}
super.address = address;
}
@Override
protected void listen(int backlog) {
// do nothing
}
@Override
protected void accept(SocketImpl si) throws IOException {
SocketChannel peer = ssc.accept();
FileDescriptor fd;
try {
Class<?> clazz = Class.forName("sun.nio.ch.SocketChannelImpl");
Field f = clazz.getDeclaredField("fd");
f.setAccessible(true);
fd = (FileDescriptor) f.get(peer);
} catch (Exception e) {
throw new RuntimeException(e);
}
set(si, "fd", fd);
set(si, "address", peer.socket().getInetAddress());
set(si, "port", peer.socket().getPort());
set(si, "localport", peer.socket().getLocalPort());
}
@Override
protected InputStream getInputStream() {
throw new RuntimeException();
}
@Override
protected OutputStream getOutputStream() {
throw new RuntimeException();
}
@Override
protected int available() {
return 0;
}
@Override
protected void close() {
}
@Override
protected void sendUrgentData(int data) {
throw new RuntimeException();
}
@Override
public void setOption(int option, Object value) {
throw new RuntimeException();
}
@Override
public Object getOption(int option) {
throw new RuntimeException();
}
}
}

View file

@ -0,0 +1,112 @@
/*
* Copyright (c) 2019, 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 Basic test for java.net.SocketImpl default behavior
* @run testng TestDefaultBehavior
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
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;
public class TestDefaultBehavior {
static final Class<NullPointerException> NPE = NullPointerException.class;
static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
@Test
public void socketImpl() {
CustomSocketImpl csi = new CustomSocketImpl();
assertEquals(csi.supportedOptions().size(), 0);
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));
expectThrows(NPE, () -> csi.getOption(null));
expectThrows(UOE, () -> csi.getOption(SO_RCVBUF));
expectThrows(UOE, () -> csi.getOption(SO_KEEPALIVE));
expectThrows(UOE, () -> csi.getOption(FAKE_SOCK_OPT));
}
static final SocketOption<Boolean> FAKE_SOCK_OPT = new SocketOption<>() {
@Override public String name() { return "FAKE_SOCK_OPT"; }
@Override public Class<Boolean> type() { return Boolean.class; }
};
// A SocketImpl that delegates the three new-style socket option
// methods to the default java.net.SocketImpl implementation.
static class CustomSocketImpl extends SocketImpl {
@Override
public <T> void setOption(SocketOption<T> name, T value) throws IOException {
super.setOption(name, value);
}
@Override
public Set<SocketOption<?>> supportedOptions() {
return super.supportedOptions();
}
@Override
public <T> T getOption(SocketOption<T> name) throws IOException {
return super.getOption(name);
}
// --
@Override protected void create(boolean stream) { }
@Override protected void connect(String host, int port) { }
@Override protected void connect(InetAddress address, int port) { }
@Override protected void connect(SocketAddress address, int timeout) { }
@Override protected void bind(InetAddress host, int port) { }
@Override protected void listen(int backlog) { }
@Override protected void accept(SocketImpl s) { }
@Override protected InputStream getInputStream() { return null; }
@Override protected OutputStream getOutputStream() { return null; }
@Override protected int available() { return 0; }
@Override protected void close() { }
@Override protected void sendUrgentData(int data) { }
@Override public void setOption(int optID, Object value) { }
@Override public Object getOption(int optID) { return null; }
}
}

View file

@ -0,0 +1,143 @@
/*
* Copyright (c) 2019, 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.
*/
package java.net;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Set;
/**
* A SocketImpl that delegates all operations to a platform SocketImpl. It also
* overrides all methods with public methods and implements AutoCloseable to make
* it easy to write tests.
*/
public class PlatformSocketImpl extends SocketImpl implements AutoCloseable {
private final SocketImpl impl;
public PlatformSocketImpl(boolean server) {
impl = new sun.nio.ch.NioSocketImpl(server);
}
@Override
public void close() throws IOException {
impl.close();
}
@Override
public void create(boolean stream) throws IOException {
impl.create(stream);
}
@Override
public void connect(SocketAddress remote, int millis) throws IOException {
impl.connect(remote, millis);
}
@Override
public void connect(String host, int port) throws IOException {
impl.connect(host, port);
}
@Override
public void connect(InetAddress address, int port) throws IOException {
impl.connect(address, port);
}
@Override
public void bind(InetAddress address, int port) throws IOException {
impl.bind(address, port);
}
@Override
public void listen(int backlog) throws IOException {
impl.listen(backlog);
}
@Override
public void accept(SocketImpl si) throws IOException {
impl.accept(((PlatformSocketImpl) si).impl);
}
@Override
public InputStream getInputStream() throws IOException {
return impl.getInputStream();
}
@Override
public OutputStream getOutputStream() throws IOException {
return impl.getOutputStream();
}
@Override
public int available() throws IOException {
return impl.available();
}
@Override
public Set<SocketOption<?>> supportedOptions() {
return impl.supportedOptions();
}
@Override
public <T> void setOption(SocketOption<T> opt, T value) throws IOException {
impl.setOption(opt, value);
}
@Override
public <T> T getOption(SocketOption<T> opt) throws IOException {
return impl.getOption(opt);
}
@Override
public void setOption(int opt, Object value) throws SocketException {
impl.setOption(opt, value);
}
@Override
public Object getOption(int opt) throws SocketException {
return impl.getOption(opt);
}
@Override
public void shutdownInput() throws IOException {
impl.shutdownInput();
}
@Override
public void shutdownOutput() throws IOException {
impl.shutdownOutput();
}
@Override
public boolean supportsUrgentData() {
return impl.supportsUrgentData();
}
@Override
public void sendUrgentData(int data) throws IOException {
impl.sendUrgentData(data);
}
}