test/jdk/java/net/SocketImpl/CustomSocketImpls.java
author alanb
Wed, 20 Feb 2019 20:55:24 +0000
branchniosocketimpl-branch
changeset 57199 88a41734ddbe
child 57200 4446b7c9bc1f
permissions -rw-r--r--
Add test to exercise using custom SocketImpls
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57199
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
     1
/*
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
     4
 *
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
     8
 *
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    13
 * accompanied this code).
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    14
 *
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    18
 *
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    21
 * questions.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    22
 */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    23
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    24
/**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    25
 * @test
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    26
 * @modules java.base/java.net:+open java.base/sun.nio.ch:+open
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    27
 * @run testng/othervm CustomSocketImpls
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    28
 * @run testng/othervm -Djdk.net.usePlainSocketImpl CustomSocketImpls
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    29
 */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    30
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    31
import java.io.FileDescriptor;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    32
import java.io.IOException;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    33
import java.io.InputStream;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    34
import java.io.OutputStream;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    35
import java.lang.reflect.Field;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    36
import java.net.InetAddress;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    37
import java.net.InetSocketAddress;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    38
import java.net.Proxy;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    39
import java.net.ServerSocket;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    40
import java.net.Socket;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    41
import java.net.SocketAddress;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    42
import java.net.SocketImpl;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    43
import java.net.SocketImplFactory;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    44
import java.nio.channels.ServerSocketChannel;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    45
import java.nio.channels.SocketChannel;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    46
import java.util.function.Consumer;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    47
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    48
import org.testng.annotations.Test;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    49
import static org.testng.Assert.*;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    50
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    51
@Test
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    52
public class CustomSocketImpls {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    53
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    54
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    55
     * Test ServerSocket is created with the expected SocketImpl.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    56
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    57
    public void testServerSocketSocketImpl() throws Exception {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    58
        try (ServerSocket ss = new ServerSocket()) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    59
            SocketImpl si = getSocketImpl(ss);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    60
            assertTrue(isPlatformSocketImpl(si));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    61
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    62
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    63
        SocketImpl impl = new CustomSocketImpl(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    64
        try (ServerSocket ss = new ServerSocket(impl){}) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    65
            SocketImpl si = getSocketImpl(ss);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    66
            assertTrue(si instanceof CustomSocketImpl);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    67
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    68
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    69
        setSocketFactory(() -> new CustomSocketImpl(true));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    70
        try (ServerSocket ss = new ServerSocket()) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    71
            SocketImpl si = getSocketImpl(ss);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    72
            assertTrue(si instanceof CustomSocketImpl);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    73
        } finally {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    74
            setSocketFactory(null);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    75
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    76
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    77
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    78
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    79
     * Test Socket is created with the expected SocketImpl.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    80
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    81
    public void tesSocketSocketImpl() throws Exception {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    82
        try (Socket s = new Socket()) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    83
            SocketImpl si = getSocketImpl(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    84
            assertTrue(isSocksSocketImpl(si));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    85
            SocketImpl delegate = getDelegate(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    86
            assertTrue(isPlatformSocketImpl(delegate));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    87
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    88
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    89
        try (Socket s = new Socket((SocketImpl) null){}) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    90
            assertTrue(getSocketImpl(s) == null);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    91
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    92
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    93
        try (Socket s = new Socket(Proxy.NO_PROXY)) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    94
            SocketImpl si = getSocketImpl(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    95
            assertTrue(isPlatformSocketImpl(si));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    96
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    97
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    98
        var address = new InetSocketAddress("127.0.0.1", 1000);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
    99
        var socksProxy = new Proxy(Proxy.Type.SOCKS, address);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   100
        try (Socket s = new Socket(socksProxy)) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   101
            SocketImpl si = getSocketImpl(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   102
            assertTrue(isSocksSocketImpl(si));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   103
            SocketImpl delegate = getDelegate(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   104
            assertTrue(isPlatformSocketImpl(delegate));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   105
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   106
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   107
        var httpProxy = new Proxy(Proxy.Type.HTTP, address);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   108
        try (Socket s = new Socket(httpProxy)) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   109
            SocketImpl si = getSocketImpl(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   110
            assertTrue(isHttpConnectSocketImpl(si));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   111
            SocketImpl delegate = getDelegate(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   112
            assertTrue(isPlatformSocketImpl(delegate));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   113
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   114
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   115
        try (Socket s = new Socket(new CustomSocketImpl(false)){}) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   116
            SocketImpl si = getSocketImpl(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   117
            assertTrue(si instanceof CustomSocketImpl);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   118
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   119
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   120
        setSocketImplFactory(() -> new CustomSocketImpl(false));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   121
        try (Socket s = new Socket()) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   122
            SocketImpl si = getSocketImpl(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   123
            assertTrue(si instanceof CustomSocketImpl);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   124
        } finally {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   125
            setSocketImplFactory(null);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   126
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   127
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   128
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   129
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   130
     * ServerSocket using default SocketImpl. Test accept returning a Socket
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   131
     * that initially doesn't have a SocketImpl.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   132
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   133
    public void test1() throws Exception {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   134
        var socket = new Socket((SocketImpl) null) { };
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   135
        assertTrue(getSocketImpl(socket) == null);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   136
        testAccept(socket, s -> {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   137
            assertTrue(s == socket);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   138
            SocketImpl si = getSocketImpl(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   139
            assertTrue(isPlatformSocketImpl(si));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   140
            checkFields(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   141
        });
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   142
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   143
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   144
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   145
     * ServerSocket using default SocketImpl. Test accept returning a Socket
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   146
     * that has an existing default SocketImpl.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   147
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   148
    public void test2() throws Exception {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   149
        var socket = new Socket();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   150
        SocketImpl si = getSocketImpl(socket);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   151
        assertTrue(isSocksSocketImpl(si));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   152
        SocketImpl delegate = getDelegate(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   153
        assertTrue(isPlatformSocketImpl(delegate));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   154
        testAccept(socket, s -> {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   155
            assertTrue(s == socket);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   156
            assertTrue(getSocketImpl(s) == si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   157
            assertTrue(getDelegate(si) == delegate);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   158
            checkFields(delegate);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   159
        });
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   160
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   161
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   162
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   163
     * ServerSocket using default SocketImpl. A SocketImplFactory is set to
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   164
     * return a custom SocketImpl. Test accept returning a Socket that initially
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   165
     * doesn't have a SocketImpl.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   166
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   167
    public void test3() throws Exception {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   168
        var socket = new Socket((SocketImpl) null) { };
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   169
        assertTrue(getSocketImpl(socket) == null);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   170
        testAccept(socket, () -> new CustomSocketImpl(false), s -> {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   171
            assertTrue(s == socket);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   172
            SocketImpl si = getSocketImpl(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   173
            assertTrue(si instanceof CustomSocketImpl);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   174
            checkFields(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   175
        });
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   176
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   177
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   178
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   179
     * ServerSocket using default SocketImpl. Test accept returning a Socket
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   180
     * that has an existing custom SocketImpl.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   181
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   182
    public void test4() throws Exception {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   183
        SocketImpl si = new CustomSocketImpl(false);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   184
        Socket socket = new Socket(si) { };
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   185
        assertTrue(getSocketImpl(socket) == si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   186
        testAccept(socket, s -> {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   187
            assertTrue(s == socket);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   188
            assertTrue(getSocketImpl(s) == si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   189
            checkFields(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   190
        });
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   191
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   192
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   193
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   194
     * ServerSocket using a custom SocketImpl. Test accept returning a Socket
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   195
     * that initially doesn't have a SocketImpl.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   196
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   197
    public void test5() throws Exception {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   198
        SocketImpl impl = new CustomSocketImpl(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   199
        Socket socket = new Socket((SocketImpl) null) { };
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   200
        assertTrue(getSocketImpl(socket) == null);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   201
        testAccept(impl, socket, s -> {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   202
            assertTrue(s == socket);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   203
            SocketImpl si = getSocketImpl(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   204
            assertTrue(isPlatformSocketImpl(si));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   205
            checkFields(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   206
        });
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   207
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   208
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   209
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   210
     * ServerSocket using a custom SocketImpl. Test accept returning a Socket
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   211
     * that has an existing default SocketImpl.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   212
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   213
    public void test6() throws Exception {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   214
        SocketImpl impl = new CustomSocketImpl(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   215
        var socket = new Socket();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   216
        SocketImpl si = getSocketImpl(socket);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   217
        assertTrue(isSocksSocketImpl(si));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   218
        SocketImpl delegate = getDelegate(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   219
        assertTrue(isPlatformSocketImpl(delegate));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   220
        testAccept(impl, socket, s -> {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   221
            assertTrue(s == socket);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   222
            assertTrue(getSocketImpl(s) == si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   223
            assertTrue(getDelegate(si) == delegate);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   224
            checkFields(delegate);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   225
        });
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   226
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   227
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   228
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   229
     * ServerSocket using a custom SocketImpl. A SocketImplFactory is set to
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   230
     * return a custom SocketImpl. Test accept returning a Socket that initially
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   231
     * doesn't have a SocketImpl.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   232
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   233
    public void test7() throws Exception {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   234
        var socket = new Socket((SocketImpl) null) { };
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   235
        assertTrue(getSocketImpl(socket) == null);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   236
        Socket s1 = null;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   237
        Socket s2 = null;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   238
        try (ServerSocket ss = prepareToAccept(new CustomSocketImpl(true), socket)) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   239
            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   240
            setSocketImplFactory(() -> new CustomSocketImpl(false));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   241
            try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   242
                s2 = ss.accept();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   243
                SocketImpl si = getSocketImpl(s2);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   244
                assertTrue(si instanceof CustomSocketImpl);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   245
                checkFields(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   246
            } finally {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   247
                setSocketImplFactory(null);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   248
            }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   249
        } finally {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   250
            if (s1 != null) s1.close();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   251
            if (s2 != null) s1.close();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   252
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   253
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   254
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   255
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   256
     * ServerSocket using a custom SocketImpl. A SocketImplFactory is set to
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   257
     * return a custom SocketImpl. Test accept returning a Socket has an existing
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   258
     * custom SocketImpl.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   259
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   260
    public void test8() throws Exception {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   261
        SocketImpl si = new CustomSocketImpl(false);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   262
        Socket socket = new Socket(si) { };
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   263
        assertTrue(getSocketImpl(socket) == si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   264
        Socket s1 = null;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   265
        Socket s2 = null;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   266
        try (ServerSocket ss = prepareToAccept(new CustomSocketImpl(true), socket)) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   267
            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   268
            setSocketImplFactory(() -> new CustomSocketImpl(false));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   269
            try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   270
                s2 = ss.accept();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   271
                assertTrue(getSocketImpl(s2) == si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   272
                checkFields(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   273
            } finally {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   274
                setSocketImplFactory(null);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   275
            }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   276
        } finally {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   277
            if (s1 != null) s1.close();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   278
            if (s2 != null) s1.close();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   279
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   280
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   281
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   282
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   283
     * Creates a ServerSocket that returns the given Socket from accept.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   284
     * The consumer is invoked with the accepted socket.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   285
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   286
    static void testAccept(Socket socket, Consumer<Socket> consumer)
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   287
        throws IOException
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   288
    {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   289
        Socket s1 = null;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   290
        Socket s2 = null;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   291
        try (ServerSocket ss = prepareToAccept(socket)) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   292
            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   293
            s2 = ss.accept();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   294
            consumer.accept(s2);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   295
        } finally {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   296
            if (s1 != null) s1.close();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   297
            if (s2 != null) s1.close();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   298
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   299
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   300
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   301
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   302
     * Creates a ServerSocket that returns the given Socket from accept. The
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   303
     * given SocketImplFactory is set during the accept and the consumer is
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   304
     * invoked when the accepted socket.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   305
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   306
    static void testAccept(Socket socket, SocketImplFactory factory, Consumer<Socket> consumer)
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   307
        throws IOException
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   308
    {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   309
        Socket s1 = null;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   310
        Socket s2 = null;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   311
        try (ServerSocket ss = prepareToAccept(socket)) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   312
            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   313
            setSocketImplFactory(factory);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   314
            try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   315
                s2 = ss.accept();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   316
            } finally {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   317
                setSocketImplFactory(null);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   318
            }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   319
            consumer.accept(s2);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   320
        } finally {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   321
            if (s1 != null) s1.close();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   322
            if (s2 != null) s1.close();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   323
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   324
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   325
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   326
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   327
     * Creates a ServerSocket with a SocketImpl that returns the given Socket
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   328
     * from accept. The consumer is invoked with the accepted socket.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   329
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   330
    static void testAccept(SocketImpl impl, Socket socket, Consumer<Socket> consumer)
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   331
        throws IOException
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   332
    {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   333
        Socket s1 = null;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   334
        Socket s2 = null;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   335
        try (ServerSocket ss = prepareToAccept(impl, socket)) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   336
            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   337
            s2 = ss.accept();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   338
            consumer.accept(s2);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   339
        } finally {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   340
            if (s1 != null) s1.close();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   341
            if (s2 != null) s1.close();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   342
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   343
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   344
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   345
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   346
     * Creates a ServerSocket that returns the given Socket from accept.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   347
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   348
    static ServerSocket prepareToAccept(Socket s) throws IOException {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   349
        return new ServerSocket(0) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   350
            @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   351
            public Socket accept() throws IOException {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   352
                implAccept(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   353
                return s;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   354
            }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   355
        };
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   356
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   357
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   358
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   359
     * Creates a ServerSocket with a SocketImpl that returns the given Socket
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   360
     * from accept.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   361
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   362
    static ServerSocket prepareToAccept(SocketImpl impl, Socket s) throws IOException {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   363
        ServerSocket ss = new ServerSocket(impl) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   364
            @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   365
            public Socket accept() throws IOException {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   366
                implAccept(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   367
                return s;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   368
            }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   369
        };
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   370
        ss.bind(new InetSocketAddress(0));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   371
        return ss;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   372
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   373
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   374
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   375
     * Returns the socket's SocketImpl
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   376
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   377
    static SocketImpl getSocketImpl(Socket s) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   378
        try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   379
            Field f = Socket.class.getDeclaredField("impl");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   380
            f.setAccessible(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   381
            return (SocketImpl) f.get(s);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   382
        } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   383
            throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   384
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   385
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   386
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   387
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   388
     * Returns the server socket's SocketImpl
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   389
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   390
    static SocketImpl getSocketImpl(ServerSocket ss) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   391
        try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   392
            Field f = ServerSocket.class.getDeclaredField("impl");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   393
            f.setAccessible(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   394
            return (SocketImpl) f.get(ss);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   395
        } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   396
            throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   397
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   398
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   399
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   400
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   401
     * Returns the SocketImpl that the given SocketImpl delegates to
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   402
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   403
    static SocketImpl getDelegate(SocketImpl si) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   404
        try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   405
            Class<?> clazz = Class.forName("java.net.DelegatingSocketImpl");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   406
            Field f = clazz.getDeclaredField("delegate");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   407
            f.setAccessible(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   408
            return (SocketImpl) f.get(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   409
        } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   410
            throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   411
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   412
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   413
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   414
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   415
     * Returns the value of a SocketImpl field
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   416
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   417
    static <T> T get(SocketImpl si, String name) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   418
        try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   419
            Field f = SocketImpl.class.getDeclaredField(name);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   420
            f.setAccessible(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   421
            return (T) f.get(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   422
        } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   423
            throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   424
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   425
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   426
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   427
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   428
     * Sets the value of SocketImpl field
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   429
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   430
    static void set(SocketImpl si, String name, Object value) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   431
        try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   432
            Field f = SocketImpl.class.getDeclaredField(name);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   433
            f.setAccessible(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   434
            f.set(si, value);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   435
        } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   436
            throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   437
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   438
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   439
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   440
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   441
     * Returns true if the SocketImpl is a PlatformSocketImpl
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   442
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   443
    static boolean isPlatformSocketImpl(SocketImpl si) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   444
        try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   445
            Class<?> clazz = Class.forName("sun.net.PlatformSocketImpl");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   446
            return clazz.isInstance(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   447
        } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   448
            throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   449
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   450
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   451
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   452
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   453
     * Returns true if the SocketImpl is a SocksSocketImpl
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   454
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   455
    static boolean isSocksSocketImpl(SocketImpl si) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   456
        try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   457
            Class<?> clazz = Class.forName("java.net.SocksSocketImpl");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   458
            return clazz.isInstance(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   459
        } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   460
            throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   461
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   462
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   463
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   464
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   465
     * Returns true if the SocketImpl is a HttpConnectSocketImpl
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   466
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   467
    static boolean isHttpConnectSocketImpl(SocketImpl si) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   468
        try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   469
            Class<?> clazz = Class.forName("java.net.HttpConnectSocketImpl");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   470
            return clazz.isInstance(si);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   471
        } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   472
            throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   473
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   474
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   475
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   476
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   477
     * Socket.setSocketImplFactory
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   478
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   479
    static void setSocketImplFactory(SocketImplFactory factory) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   480
        try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   481
            Field f = Socket.class.getDeclaredField("factory");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   482
            f.setAccessible(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   483
            f.set(null, factory);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   484
        } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   485
            throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   486
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   487
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   488
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   489
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   490
     * ServerSocket.setSocketFactory
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   491
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   492
    static void setSocketFactory(SocketImplFactory factory) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   493
        try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   494
            Field f = ServerSocket.class.getDeclaredField("factory");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   495
            f.setAccessible(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   496
            f.set(null, factory);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   497
        } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   498
            throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   499
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   500
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   501
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   502
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   503
     * Checks the 4 protected fields of a SocketImpl to make sure that they
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   504
     * have been initialized.
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   505
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   506
    static void checkFields(SocketImpl si) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   507
        FileDescriptor fd = get(si, "fd");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   508
        InetAddress address = get(si, "address");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   509
        int port = get(si, "port");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   510
        int localport = get(si, "localport");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   511
        assertTrue(fd.valid() && address != null && port != 0 && localport != 0);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   512
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   513
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   514
    /**
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   515
     * Custom SocketImpl that is layed on a SocketChannel or ServerSocketChannel
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   516
     */
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   517
    static class CustomSocketImpl extends SocketImpl {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   518
        private final boolean server;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   519
        private ServerSocketChannel ssc;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   520
        private SocketChannel sc;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   521
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   522
        CustomSocketImpl(boolean server) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   523
            this.server = server;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   524
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   525
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   526
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   527
        protected void create(boolean stream) throws IOException {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   528
            if (server) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   529
                ssc = ServerSocketChannel.open();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   530
            } else {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   531
                sc = SocketChannel.open();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   532
            }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   533
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   534
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   535
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   536
        protected void connect(String host, int port) throws IOException {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   537
            connect(new InetSocketAddress(host, port), 0);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   538
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   539
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   540
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   541
        protected void connect(InetAddress address, int port) throws IOException {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   542
            connect(new InetSocketAddress(address, port), 0);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   543
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   544
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   545
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   546
        protected void connect(SocketAddress remote, int timeout) throws IOException {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   547
            sc.connect(remote);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   548
            super.address = ((InetSocketAddress) remote).getAddress();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   549
            super.port = ((InetSocketAddress) remote).getPort();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   550
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   551
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   552
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   553
        protected void bind(InetAddress address, int port) throws IOException {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   554
            if (server) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   555
                ssc.bind(new InetSocketAddress(address, port));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   556
            } else {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   557
                sc.bind(new InetSocketAddress(address, port));
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   558
            }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   559
            super.address = address;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   560
            super.localport = ssc.socket().getLocalPort();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   561
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   562
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   563
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   564
        protected void listen(int backlog) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   565
            // do nothing
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   566
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   567
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   568
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   569
        protected void accept(SocketImpl si) throws IOException {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   570
            SocketChannel peer = ssc.accept();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   571
            FileDescriptor fd;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   572
            try {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   573
                Class<?> clazz = Class.forName("sun.nio.ch.SocketChannelImpl");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   574
                Field f = clazz.getDeclaredField("fd");
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   575
                f.setAccessible(true);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   576
                fd = (FileDescriptor) f.get(peer);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   577
            } catch (Exception e) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   578
                throw new RuntimeException(e);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   579
            }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   580
            set(si, "fd", fd);
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   581
            set(si, "address", peer.socket().getInetAddress());
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   582
            set(si, "port", peer.socket().getPort());
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   583
            set(si, "localport", peer.socket().getLocalPort());
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   584
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   585
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   586
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   587
        protected InputStream getInputStream() {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   588
            throw new RuntimeException();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   589
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   590
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   591
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   592
        protected OutputStream getOutputStream() {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   593
            throw new RuntimeException();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   594
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   595
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   596
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   597
        protected int available() {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   598
            return 0;
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   599
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   600
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   601
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   602
        protected void close() {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   603
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   604
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   605
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   606
        protected void sendUrgentData(int data) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   607
            throw new RuntimeException();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   608
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   609
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   610
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   611
        public void setOption(int option, Object value) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   612
            throw new RuntimeException();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   613
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   614
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   615
        @Override
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   616
        public Object getOption(int option) {
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   617
            throw new RuntimeException();
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   618
        }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   619
    }
88a41734ddbe Add test to exercise using custom SocketImpls
alanb
parents:
diff changeset
   620
}