test/jdk/java/net/SocketImpl/BadUsages.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 57341 733e9746d615
parent 55102 59567035d279
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     1
/*
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     4
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     8
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    13
 * accompanied this code).
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    14
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    18
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    21
 * questions.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    22
 */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    23
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    24
/*
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    25
 * @test
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    26
 * @bug 8221481
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    27
 * @compile/module=java.base java/net/PlatformSocketImpl.java
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    28
 * @run testng/othervm BadUsages
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    29
 * @summary Test the platform SocketImpl when used in unintended ways
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    30
 */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    31
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    32
import java.io.IOException;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    33
import java.io.InputStream;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    34
import java.io.OutputStream;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    35
import java.net.InetAddress;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    36
import java.net.InetSocketAddress;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    37
import java.net.ServerSocket;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    38
import java.net.SocketAddress;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    39
import java.net.SocketException;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    40
import java.net.SocketImpl;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    41
import java.net.SocketOption;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    42
import java.net.SocketOptions;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    43
import java.net.StandardSocketOptions;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    44
import java.util.Set;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    45
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    46
import java.net.PlatformSocketImpl;  // test helper
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    47
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    48
import org.testng.annotations.Test;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    49
import static org.testng.Assert.*;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    50
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    51
/**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    52
 * SocketImpl does not specify how the SocketImpl behaves when used in ways
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    53
 * that are not intended, e.g. invoking socket operations before the socket is
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    54
 * created or trying to establish a connection after the socket is connected or
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    55
 * closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    56
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    57
 * This test exercises the platform SocketImpl to test that it is reliable, and
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    58
 * throws reasonable exceptions, for these scenarios.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    59
 */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    60
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    61
@Test
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    62
public class BadUsages {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    63
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    64
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    65
     * Test create when already created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    66
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    67
    public void testCreate1() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    68
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    69
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    70
            expectThrows(IOException.class, () -> impl.create(true));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    71
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    72
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    73
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    74
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    75
     * Test create when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    76
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    77
    public void testCreate2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    78
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    79
        impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    80
        expectThrows(IOException.class, () -> impl.create(true));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    81
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    82
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    83
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    84
     * Test connect when not created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    85
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    86
    public void testConnect1() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    87
        try (var ss = new ServerSocket(0)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    88
            var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    89
            var address = ss.getInetAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    90
            int port = ss.getLocalPort();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    91
            expectThrows(IOException.class, () -> impl.connect(address, port));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    92
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    93
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    94
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    95
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    96
     * Test connect with unsupported address type.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    97
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    98
    public void testConnect2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
    99
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   100
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   101
            var remote = new SocketAddress() { };
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   102
            expectThrows(IOException.class, () -> impl.connect(remote, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   103
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   104
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   105
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   106
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   107
     * Test connect with an unresolved address.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   108
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   109
    public void testConnect3() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   110
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   111
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   112
            var remote = new InetSocketAddress("blah-blah.blah-blah", 80);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   113
            expectThrows(IOException.class, () -> impl.connect(remote, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   114
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   115
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   116
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   117
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   118
     * Test connect when already connected.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   119
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   120
    public void testConnect4() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   121
        try (var ss = new ServerSocket();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   122
             var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   123
            var loopback = InetAddress.getLoopbackAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   124
            ss.bind(new InetSocketAddress(loopback, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   125
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   126
            int port = ss.getLocalPort();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   127
            impl.connect(loopback, port);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   128
            expectThrows(IOException.class, () -> impl.connect(loopback, port));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   129
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   130
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   131
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   132
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   133
     * Test connect when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   134
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   135
    public void testConnect5() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   136
        try (var ss = new ServerSocket(0)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   137
            var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   138
            impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   139
            String host = ss.getInetAddress().getHostAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   140
            int port = ss.getLocalPort();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   141
            expectThrows(IOException.class, () -> impl.connect(host, port));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   142
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   143
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   144
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   145
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   146
     * Test bind when not created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   147
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   148
    public void testBind1() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   149
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   150
        var loopback = InetAddress.getLoopbackAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   151
        expectThrows(IOException.class, () -> impl.bind(loopback, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   152
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   153
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   154
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   155
     * Test bind when already bound.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   156
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   157
    public void testBind2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   158
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   159
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   160
            var loopback = InetAddress.getLoopbackAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   161
            impl.bind(loopback, 0);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   162
            expectThrows(IOException.class, () -> impl.bind(loopback, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   163
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   164
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   165
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   166
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   167
     * Test bind when connected.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   168
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   169
    public void testBind3() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   170
        try (var ss = new ServerSocket();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   171
             var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   172
            var loopback = InetAddress.getLoopbackAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   173
            ss.bind(new InetSocketAddress(loopback, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   174
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   175
            impl.connect(ss.getLocalSocketAddress(), 0);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   176
            expectThrows(IOException.class, () -> impl.bind(loopback, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   177
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   178
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   179
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   180
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   181
     * Test bind when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   182
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   183
    public void testBind4() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   184
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   185
        impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   186
        var loopback = InetAddress.getLoopbackAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   187
        expectThrows(IOException.class, () -> impl.bind(loopback, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   188
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   189
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   190
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   191
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   192
     * Test listen when not created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   193
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   194
    public void testListen1() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   195
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   196
        expectThrows(IOException.class, () -> impl.listen(16));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   197
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   198
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   199
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   200
     * Test listen when not bound.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   201
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   202
    public void testListen2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   203
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   204
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   205
            expectThrows(IOException.class, () -> impl.listen(16));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   206
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   207
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   208
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   209
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   210
     * Test listen when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   211
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   212
    public void testListen3() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   213
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   214
        impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   215
        expectThrows(IOException.class, () -> impl.listen(16));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   216
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   217
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   218
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   219
     * Test accept when not created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   220
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   221
    public void testAccept1() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   222
        var impl = new PlatformSocketImpl(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   223
        var si = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   224
        expectThrows(IOException.class, () -> impl.accept(si));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   225
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   226
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   227
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   228
     * Test accept when not bound.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   229
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   230
    public void testAccept2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   231
        try (var impl = new PlatformSocketImpl(true)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   232
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   233
            var si = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   234
            expectThrows(IOException.class, () -> impl.accept(si));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   235
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   236
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   237
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   238
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   239
     * Test accept when not a stream socket.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   240
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   241
    public void testAccept3() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   242
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   243
            impl.create(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   244
            impl.bind(InetAddress.getLoopbackAddress(), 0);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   245
            var si = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   246
            expectThrows(IOException.class, () -> impl.accept(si));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   247
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   248
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   249
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   250
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   251
     * Test accept when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   252
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   253
    public void testAccept4() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   254
        var impl = new PlatformSocketImpl(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   255
        impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   256
        var si = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   257
        expectThrows(IOException.class, () -> impl.accept(si));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   258
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   259
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   260
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   261
     * Test accept with SocketImpl that is already created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   262
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   263
    public void testAccept5() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   264
        try (var impl = new PlatformSocketImpl(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   265
             var si = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   266
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   267
            impl.bind(InetAddress.getLoopbackAddress(), 0);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   268
            si.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   269
            expectThrows(IOException.class, () -> impl.accept(si));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   270
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   271
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   272
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   273
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   274
     * Test accept with SocketImpl that is closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   275
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   276
    public void testAccept6() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   277
        try (var impl = new PlatformSocketImpl(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   278
             var si = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   279
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   280
            impl.bind(InetAddress.getLoopbackAddress(), 0);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   281
            si.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   282
            si.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   283
            expectThrows(IOException.class, () -> impl.accept(si));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   284
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   285
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   286
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   287
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   288
     * Test available when not created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   289
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   290
    public void testAvailable1() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   291
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   292
        expectThrows(IOException.class, () -> impl.available());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   293
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   294
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   295
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   296
     * Test available when created but not connected.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   297
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   298
    public void testAvailable2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   299
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   300
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   301
            expectThrows(IOException.class, () -> impl.available());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   302
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   303
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   304
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   305
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   306
     * Test available when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   307
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   308
    public void testAvailable3() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   309
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   310
        impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   311
        expectThrows(IOException.class, () -> impl.available());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   312
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   313
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   314
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   315
     * Test setOption when not created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   316
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   317
    public void testSetOption1() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   318
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   319
        expectThrows(IOException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   320
                     () -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   321
        // legacy
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   322
        expectThrows(SocketException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   323
                     () -> impl.setOption(SocketOptions.SO_REUSEADDR, true));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   324
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   325
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   326
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   327
     * Test setOption when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   328
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   329
    public void testSetOption2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   330
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   331
        impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   332
        expectThrows(IOException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   333
                     () -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   334
        // legacy
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   335
        expectThrows(SocketException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   336
                     () -> impl.setOption(SocketOptions.SO_REUSEADDR, true));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   337
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   338
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   339
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   340
     * Test setOption with unsupported option.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   341
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   342
    public void testSetOption3() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   343
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   344
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   345
            var opt = new SocketOption<String>() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   346
                @Override public String name() { return "birthday"; }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   347
                @Override public Class<String> type() { return String.class; }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   348
            };
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   349
            expectThrows(UnsupportedOperationException.class, () -> impl.setOption(opt, ""));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   350
            // legacy
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   351
            expectThrows(SocketException.class, () -> impl.setOption(-1, ""));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   352
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   353
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   354
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   355
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   356
     * Test setOption(int, Object) with invalid values.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   357
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   358
    public void testSetOption4() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   359
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   360
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   361
            expectThrows(SocketException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   362
                         () -> impl.setOption(SocketOptions.SO_REUSEADDR, -1));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   363
            expectThrows(SocketException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   364
                         () -> impl.setOption(SocketOptions.SO_TIMEOUT, -1));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   365
            expectThrows(SocketException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   366
                         () -> impl.setOption(SocketOptions.SO_SNDBUF, -1));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   367
            expectThrows(SocketException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   368
                         () -> impl.setOption(SocketOptions.SO_RCVBUF, -1));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   369
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   370
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   371
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   372
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   373
     * Test getOption when not created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   374
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   375
    public void testGetOption1() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   376
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   377
        expectThrows(IOException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   378
                     () -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   379
        expectThrows(SocketException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   380
                     () -> impl.getOption(-1));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   381
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   382
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   383
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   384
     * Test getOption when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   385
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   386
    public void testGetOption2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   387
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   388
        impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   389
        expectThrows(IOException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   390
                     () -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   391
        expectThrows(SocketException.class,
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   392
                     () -> impl.getOption(SocketOptions.SO_REUSEADDR));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   393
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   394
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   395
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   396
     * Test getOption with unsupported option.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   397
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   398
    public void testGetOption3() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   399
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   400
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   401
            var opt = new SocketOption<String>() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   402
                @Override public String name() { return "birthday"; }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   403
                @Override public Class<String> type() { return String.class; }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   404
            };
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   405
            expectThrows(UnsupportedOperationException.class, () -> impl.getOption(opt));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   406
            expectThrows(SocketException.class, () -> impl.getOption(-1));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   407
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   408
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   409
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   410
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   411
     * Test shutdownInput when not created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   412
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   413
    public void testShutdownInput1() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   414
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   415
        expectThrows(IOException.class, () -> impl.shutdownInput());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   416
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   417
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   418
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   419
     * Test shutdownInput when not connected.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   420
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   421
    public void testShutdownInput2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   422
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   423
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   424
            expectThrows(IOException.class, () -> impl.shutdownInput());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   425
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   426
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   427
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   428
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   429
     * Test shutdownInput when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   430
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   431
    public void testShutdownInput3() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   432
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   433
        impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   434
        expectThrows(IOException.class, () -> impl.shutdownInput());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   435
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   436
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   437
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   438
     * Test shutdownOutput when not created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   439
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   440
    public void testShutdownOutput1() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   441
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   442
        expectThrows(IOException.class, () -> impl.shutdownOutput());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   443
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   444
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   445
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   446
     * Test shutdownOutput when not connected.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   447
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   448
    public void testShutdownOutput2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   449
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   450
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   451
            expectThrows(IOException.class, () -> impl.shutdownOutput());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   452
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   453
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   454
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   455
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   456
     * Test shutdownOutput when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   457
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   458
    public void testShutdownOutput3() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   459
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   460
        impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   461
        expectThrows(IOException.class, () -> impl.shutdownOutput());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   462
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   463
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   464
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   465
     * Test sendUrgentData when not created.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   466
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   467
    public void testSendUrgentData1() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   468
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   469
        expectThrows(IOException.class, () -> impl.sendUrgentData(0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   470
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   471
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   472
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   473
     * Test sendUrgentData when not connected.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   474
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   475
    public void testSendUrgentData2() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   476
        try (var impl = new PlatformSocketImpl(false)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   477
            impl.create(true);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   478
            expectThrows(IOException.class, () -> impl.sendUrgentData(0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   479
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   480
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   481
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   482
    /**
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   483
     * Test sendUrgentData when closed.
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   484
     */
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   485
    public void testSendUrgentData3() throws IOException {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   486
        var impl = new PlatformSocketImpl(false);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   487
        impl.close();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   488
        expectThrows(IOException.class, () -> impl.sendUrgentData(0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   489
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents:
diff changeset
   490
}