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