More test cleanup niosocketimpl-branch
authoralanb
Thu, 21 Feb 2019 08:29:16 +0000
branchniosocketimpl-branch
changeset 57201 b15114decb01
parent 57200 4446b7c9bc1f
child 57202 f946ae816b8a
More test cleanup
test/jdk/java/net/SocketImpl/CustomSocketImpls.java
test/jdk/java/net/SocketImpl/SocketImplCombinations.java
--- a/test/jdk/java/net/SocketImpl/CustomSocketImpls.java	Wed Feb 20 21:12:08 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,620 +0,0 @@
-/*
- * 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
- * @modules java.base/java.net:+open java.base/sun.nio.ch:+open
- * @run testng/othervm CustomSocketImpls
- * @run testng/othervm -Djdk.net.usePlainSocketImpl CustomSocketImpls
- */
-
-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.Consumer;
-
-import org.testng.annotations.Test;
-import static org.testng.Assert.*;
-
-@Test
-public class CustomSocketImpls {
-
-    /**
-     * Test ServerSocket is created with the expected SocketImpl.
-     */
-    public void testServerSocketSocketImpl() throws Exception {
-        try (ServerSocket ss = new ServerSocket()) {
-            SocketImpl si = getSocketImpl(ss);
-            assertTrue(isPlatformSocketImpl(si));
-        }
-
-        SocketImpl impl = new CustomSocketImpl(true);
-        try (ServerSocket ss = new ServerSocket(impl){}) {
-            SocketImpl si = getSocketImpl(ss);
-            assertTrue(si instanceof CustomSocketImpl);
-        }
-
-        setSocketFactory(() -> new CustomSocketImpl(true));
-        try (ServerSocket ss = new ServerSocket()) {
-            SocketImpl si = getSocketImpl(ss);
-            assertTrue(si instanceof CustomSocketImpl);
-        } finally {
-            setSocketFactory(null);
-        }
-    }
-
-    /**
-     * Test Socket is created with the expected SocketImpl.
-     */
-    public void tesSocketSocketImpl() throws Exception {
-        try (Socket s = new Socket()) {
-            SocketImpl si = getSocketImpl(s);
-            assertTrue(isSocksSocketImpl(si));
-            SocketImpl delegate = getDelegate(si);
-            assertTrue(isPlatformSocketImpl(delegate));
-        }
-
-        try (Socket s = new Socket((SocketImpl) null){}) {
-            assertTrue(getSocketImpl(s) == null);
-        }
-
-        try (Socket s = new Socket(Proxy.NO_PROXY)) {
-            SocketImpl si = getSocketImpl(s);
-            assertTrue(isPlatformSocketImpl(si));
-        }
-
-        var address = new InetSocketAddress("127.0.0.1", 1000);
-        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));
-        }
-
-        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));
-        }
-
-        try (Socket s = new Socket(new CustomSocketImpl(false)){}) {
-            SocketImpl si = getSocketImpl(s);
-            assertTrue(si instanceof CustomSocketImpl);
-        }
-
-        setSocketImplFactory(() -> new CustomSocketImpl(false));
-        try (Socket s = new Socket()) {
-            SocketImpl si = getSocketImpl(s);
-            assertTrue(si instanceof CustomSocketImpl);
-        } finally {
-            setSocketImplFactory(null);
-        }
-    }
-
-    /**
-     * ServerSocket using default SocketImpl. Test accept returning a Socket
-     * that initially doesn't have a SocketImpl.
-     */
-    public void test1() throws Exception {
-        var socket = new Socket((SocketImpl) null) { };
-        assertTrue(getSocketImpl(socket) == null);
-        testAccept(socket, s -> {
-            assertTrue(s == socket);
-            SocketImpl si = getSocketImpl(s);
-            assertTrue(isPlatformSocketImpl(si));
-            checkFields(si);
-        });
-    }
-
-    /**
-     * ServerSocket using default SocketImpl. Test accept returning a Socket
-     * that has an existing default SocketImpl.
-     */
-    public void test2() throws Exception {
-        var socket = new Socket();
-        SocketImpl si = getSocketImpl(socket);
-        assertTrue(isSocksSocketImpl(si));
-        SocketImpl delegate = getDelegate(si);
-        assertTrue(isPlatformSocketImpl(delegate));
-        testAccept(socket, s -> {
-            assertTrue(s == socket);
-            assertTrue(getSocketImpl(s) == si);
-            assertTrue(getDelegate(si) == delegate);
-            checkFields(delegate);
-        });
-    }
-
-    /**
-     * ServerSocket using default SocketImpl. A SocketImplFactory is set to
-     * return a custom SocketImpl. Test accept returning a Socket that initially
-     * doesn't have a SocketImpl.
-     */
-    public void test3() throws Exception {
-        var socket = new Socket((SocketImpl) null) { };
-        assertTrue(getSocketImpl(socket) == null);
-        testAccept(socket, () -> new CustomSocketImpl(false), s -> {
-            assertTrue(s == socket);
-            SocketImpl si = getSocketImpl(s);
-            assertTrue(si instanceof CustomSocketImpl);
-            checkFields(si);
-        });
-    }
-
-    /**
-     * ServerSocket using default SocketImpl. Test accept returning a Socket
-     * that has an existing custom SocketImpl.
-     */
-    public void test4() throws Exception {
-        SocketImpl si = new CustomSocketImpl(false);
-        Socket socket = new Socket(si) { };
-        assertTrue(getSocketImpl(socket) == si);
-        testAccept(socket, s -> {
-            assertTrue(s == socket);
-            assertTrue(getSocketImpl(s) == si);
-            checkFields(si);
-        });
-    }
-
-    /**
-     * ServerSocket using a custom SocketImpl. Test accept returning a Socket
-     * that initially doesn't have a SocketImpl.
-     */
-    public void test5() throws Exception {
-        SocketImpl impl = new CustomSocketImpl(true);
-        Socket socket = new Socket((SocketImpl) null) { };
-        assertTrue(getSocketImpl(socket) == null);
-        testAccept(impl, socket, s -> {
-            assertTrue(s == socket);
-            SocketImpl si = getSocketImpl(s);
-            assertTrue(isPlatformSocketImpl(si));
-            checkFields(si);
-        });
-    }
-
-    /**
-     * ServerSocket using a custom SocketImpl. Test accept returning a Socket
-     * that has an existing default SocketImpl.
-     */
-    public void test6() throws Exception {
-        SocketImpl impl = new CustomSocketImpl(true);
-        var socket = new Socket();
-        SocketImpl si = getSocketImpl(socket);
-        assertTrue(isSocksSocketImpl(si));
-        SocketImpl delegate = getDelegate(si);
-        assertTrue(isPlatformSocketImpl(delegate));
-        testAccept(impl, socket, s -> {
-            assertTrue(s == socket);
-            assertTrue(getSocketImpl(s) == si);
-            assertTrue(getDelegate(si) == delegate);
-            checkFields(delegate);
-        });
-    }
-
-    /**
-     * ServerSocket using a custom SocketImpl. A SocketImplFactory is set to
-     * return a custom SocketImpl. Test accept returning a Socket that initially
-     * doesn't have a SocketImpl.
-     */
-    public void test7() throws Exception {
-        var socket = new Socket((SocketImpl) null) { };
-        assertTrue(getSocketImpl(socket) == null);
-        Socket s1 = null;
-        Socket s2 = null;
-        try (ServerSocket ss = prepareToAccept(new CustomSocketImpl(true), socket)) {
-            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
-            setSocketImplFactory(() -> new CustomSocketImpl(false));
-            try {
-                s2 = ss.accept();
-                SocketImpl si = getSocketImpl(s2);
-                assertTrue(si instanceof CustomSocketImpl);
-                checkFields(si);
-            } finally {
-                setSocketImplFactory(null);
-            }
-        } finally {
-            if (s1 != null) s1.close();
-            if (s2 != null) s2.close();
-        }
-    }
-
-    /**
-     * ServerSocket using a custom SocketImpl. A SocketImplFactory is set to
-     * return a custom SocketImpl. Test accept returning a Socket has an existing
-     * custom SocketImpl.
-     */
-    public void test8() throws Exception {
-        SocketImpl si = new CustomSocketImpl(false);
-        Socket socket = new Socket(si) { };
-        assertTrue(getSocketImpl(socket) == si);
-        Socket s1 = null;
-        Socket s2 = null;
-        try (ServerSocket ss = prepareToAccept(new CustomSocketImpl(true), socket)) {
-            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
-            setSocketImplFactory(() -> new CustomSocketImpl(false));
-            try {
-                s2 = ss.accept();
-                assertTrue(getSocketImpl(s2) == si);
-                checkFields(si);
-            } finally {
-                setSocketImplFactory(null);
-            }
-        } finally {
-            if (s1 != null) s1.close();
-            if (s2 != null) s2.close();
-        }
-    }
-
-    /**
-     * Creates a ServerSocket that returns the given Socket from accept.
-     * The consumer is invoked with the accepted socket.
-     */
-    static void testAccept(Socket socket, Consumer<Socket> consumer)
-        throws IOException
-    {
-        Socket s1 = null;
-        Socket s2 = null;
-        try (ServerSocket ss = prepareToAccept(socket)) {
-            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
-            s2 = ss.accept();
-            consumer.accept(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 accepted socket.
-     */
-    static void testAccept(Socket socket, SocketImplFactory factory, Consumer<Socket> consumer)
-        throws IOException
-    {
-        Socket s1 = null;
-        Socket s2 = null;
-        try (ServerSocket ss = prepareToAccept(socket)) {
-            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
-            setSocketImplFactory(factory);
-            try {
-                s2 = ss.accept();
-            } finally {
-                setSocketImplFactory(null);
-            }
-            consumer.accept(s2);
-        } finally {
-            if (s1 != null) s1.close();
-            if (s2 != null) s2.close();
-        }
-    }
-
-    /**
-     * Creates a ServerSocket with a SocketImpl that returns the given Socket
-     * from accept. The consumer is invoked with the accepted socket.
-     */
-    static void testAccept(SocketImpl impl, Socket socket, Consumer<Socket> consumer)
-        throws IOException
-    {
-        Socket s1 = null;
-        Socket s2 = null;
-        try (ServerSocket ss = prepareToAccept(impl, socket)) {
-            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
-            s2 = ss.accept();
-            consumer.accept(s2);
-        } finally {
-            if (s1 != null) s1.close();
-            if (s2 != null) s2.close();
-        }
-    }
-
-    /**
-     * Creates a ServerSocket that returns the given Socket from accept.
-     */
-    static ServerSocket prepareToAccept(Socket s) throws IOException {
-        return new ServerSocket(0) {
-            @Override
-            public Socket accept() throws IOException {
-                implAccept(s);
-                return s;
-            }
-        };
-    }
-
-    /**
-     * Creates a ServerSocket with a SocketImpl that returns the given Socket
-     * from accept.
-     */
-    static ServerSocket prepareToAccept(SocketImpl impl, Socket s) throws IOException {
-        ServerSocket ss = new ServerSocket(impl) {
-            @Override
-            public Socket accept() throws IOException {
-                implAccept(s);
-                return s;
-            }
-        };
-        ss.bind(new InetSocketAddress(0));
-        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
-     */
-    static void setSocketImplFactory(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
-     */
-    static void setSocketFactory(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));
-            } else {
-                sc.bind(new InetSocketAddress(address, port));
-            }
-            super.address = address;
-            super.localport = ssc.socket().getLocalPort();
-        }
-
-        @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();
-        }
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/SocketImpl/SocketImplCombinations.java	Thu Feb 21 08:29:16 2019 +0000
@@ -0,0 +1,721 @@
+/*
+ * 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
+ * @modules java.base/java.net:+open java.base/sun.nio.ch:+open
+ * @run testng/othervm SocketImplCombinations
+ * @run testng/othervm -Djdk.net.usePlainSocketImpl 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 {
+
+    // Tests to ensure that a Socket is created with the expected 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));
+        }
+    }
+
+    public void testNewSocket2() throws IOException {
+        try (ServerSocket ss = new ServerSocket(0)) {
+            try (Socket s = new Socket(ss.getInetAddress(), ss.getLocalPort())) {
+                SocketImpl si = getSocketImpl(s);
+                assertTrue(isSocksSocketImpl(si));
+                SocketImpl delegate = getDelegate(si);
+                assertTrue(isPlatformSocketImpl(delegate));
+            }
+        }
+    }
+
+    public void testNewSocket3() throws IOException {
+        try (Socket s = new Socket(Proxy.NO_PROXY)) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isPlatformSocketImpl(si));
+        }
+    }
+
+    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));
+        }
+    }
+
+    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));
+        }
+    }
+
+    public void testNewSocket6() throws IOException {
+        Socket s = new Socket((SocketImpl) null) { };
+        try (s) {
+            assertTrue(getSocketImpl(s) == null);
+            s.bind(new InetSocketAddress(0));   // force SocketImpl to be created
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isSocksSocketImpl(si));
+            SocketImpl delegate = getDelegate(si);
+            assertTrue(isPlatformSocketImpl(delegate));
+        }
+    }
+
+    public void testNewSocket7() throws IOException {
+        Socket s = new Socket(new CustomSocketImpl(false)) { };
+        try (s) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(si instanceof CustomSocketImpl);
+        }
+    }
+
+    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);
+        }
+    }
+
+    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);
+        }
+    }
+
+    public void testNewSocket10() throws IOException {
+        setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
+        try {
+            Socket s = new Socket((SocketImpl) null) { };
+            try (s) {
+                assertTrue(getSocketImpl(s) == null);
+                s.bind(new InetSocketAddress(0));   // force SocketImpl to be created
+                assertTrue(getSocketImpl(s) instanceof CustomSocketImpl);
+            }
+        } finally {
+            setSocketSocketImplFactory(null);
+        }
+    }
+
+    // Tests to ensure that a ServerSocket is created with the expected SocketImpl
+
+    public void testNewServerSocket1() throws IOException {
+        try (ServerSocket ss = new ServerSocket()) {
+            SocketImpl si = getSocketImpl(ss);
+            assertTrue(isPlatformSocketImpl(si));
+        }
+    }
+
+    public void testNewServerSocket2() throws IOException {
+        try (ServerSocket ss = new ServerSocket(0)) {
+            SocketImpl si = getSocketImpl(ss);
+            assertTrue(isPlatformSocketImpl(si));
+        }
+    }
+
+    public void testNewServerSocket3() throws IOException {
+        ServerSocket ss = new ServerSocket(new CustomSocketImpl(true)) { };
+        try (ss) {
+            SocketImpl si = getSocketImpl(ss);
+            assertTrue(si instanceof CustomSocketImpl);
+        }
+    }
+
+    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);
+        }
+    }
+
+    // Tests to ensure that a ServerSocket.accept returns a Socket with the
+    // expected SocketImpl
+
+    /**
+     * Test ServerSocket.accept returning a Socket that initially doesn't have a
+     * SocketImpl. The ServerSocket uses the default 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 returning a Socket that has an existing SocketImpl.
+     * The ServerSocket uses the default SocketImpl.
+     */
+    public void testServerSocketAccept2() 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);
+            assertTrue(getSocketImpl(s) == si);
+            assertTrue(getDelegate(si) == delegate);
+            checkFields(delegate);
+        });
+    }
+
+    /**
+     * Test ServerSocket.accept returning a Socket that initially doesn't have a
+     * SocketImpl. A SocketImplFactory is set to a custom SocketImplFactory so
+     * the accepted Socket should have a custom SocketImpl. The ServerSocket
+     * uses the default SocketImpl.
+     */
+    public void testServerSocketAccept3() 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(si instanceof CustomSocketImpl);
+            checkFields(si);
+        });
+    }
+
+    /**
+     * Test ServerSocket.accept returning a Socket that has an existing custom
+     * SocketImpl. The ServerSocket uses the default SocketImpl.
+     */
+    public void testServerSocketAccept4() throws IOException {
+        SocketImpl si = new CustomSocketImpl(false);
+        Socket socket = new Socket(si) { };
+        assertTrue(getSocketImpl(socket) == si);
+
+        serverSocketAccept(socket, (ss, s) -> {
+            assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
+            assertTrue(s == socket);
+            assertTrue(getSocketImpl(s) == si);
+            checkFields(si);
+        });
+    }
+
+    /**
+     * Test ServerSocket.accept returning a Socket that initially doesn't have a
+     * SocketImpl. The ServerSocket uses a custom SocketImpl.
+     */
+    public void testServerSocketAccept5() throws Exception {
+        Socket socket = new Socket((SocketImpl) null) { };
+        assertTrue(getSocketImpl(socket) == null);
+
+        SocketImpl serverImpl = new CustomSocketImpl(true);
+        serverSocketAccept(serverImpl, socket, (ss, s) -> {
+            assertTrue(getSocketImpl(ss) == serverImpl);
+            assertTrue(s == socket);
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isPlatformSocketImpl(si));
+            checkFields(si);
+        });
+    }
+
+
+    /**
+     * Test ServerSocket.accept returning a Socket that has an existing
+     * SocketImpl. The ServerSocket uses a custom SocketImpl.
+     */
+    public void testServerSocketAccept6() throws Exception {
+        var socket = new Socket();
+        SocketImpl si = getSocketImpl(socket);
+        assertTrue(isSocksSocketImpl(si));
+        SocketImpl delegate = getDelegate(si);
+        assertTrue(isPlatformSocketImpl(delegate));
+
+        SocketImpl serverImpl = new CustomSocketImpl(true);
+        serverSocketAccept(serverImpl, socket, (ss, s) -> {
+            assertTrue(getSocketImpl(ss) == serverImpl);
+            assertTrue(s == socket);
+            assertTrue(getSocketImpl(s) == si);
+            assertTrue(getDelegate(si) == delegate);
+            checkFields(delegate);
+        });
+    }
+
+    /**
+     * Test ServerSocket.accept returning a Socket that initially doesn't have a
+     * SocketImpl. A SocketImplFactory is set to create custom SocketImpls.
+     * The ServerSocket also uses custom SocketImpl.
+     */
+    public void testServerSocketAccept7() 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 returning a Socket that has an existing custom
+     * SocketImpl. A SocketImplFactory is set to create custom SocketImpls.
+     * The ServerSocket also uses custom SocketImpl.
+     */
+    public void testServerSocketAccept8() throws Exception {
+        SocketImpl si = new CustomSocketImpl(false);
+        Socket socket = new Socket(si) { };
+        assertTrue(getSocketImpl(socket) == si);
+
+        SocketImpl serverImpl = new CustomSocketImpl(true);
+        SocketImplFactory clientFactory = () -> new CustomSocketImpl(false);
+        serverSocketAccept(serverImpl, socket, clientFactory, (ss, s) -> {
+            assertTrue(getSocketImpl(ss) == serverImpl);
+            assertTrue(getSocketImpl(s) == si);
+            checkFields(si);
+        });
+    }
+
+    /**
+     * 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 that returns the given Socket
+     * from accept. The consumer is invoked with the server socket and the
+     * accepted socket.
+     */
+    static void serverSocketAccept(SocketImpl impl,
+                                   Socket socket,
+                                   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());
+            s2 = ss.accept();
+            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();
+        }
+    }
+
+    /**
+     * Creates a ServerSocket that returns the given Socket from accept.
+     */
+    static ServerSocket serverSocketToAccept(Socket s) throws IOException {
+        return new ServerSocket(0) {
+            @Override
+            public Socket accept() throws IOException {
+                implAccept(s);
+                return s;
+            }
+        };
+    }
+
+    /**
+     * 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(new InetSocketAddress(0));
+        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();
+        }
+    }
+}