test/jdk/java/net/SocketOption/OptionsTest.java
author dfuchs
Fri, 22 Nov 2019 11:52:48 +0000
changeset 59224 55fdee124e89
parent 59206 61091a42f19d
permissions -rw-r--r--
8233296: MulticastSocket getOption/setOption inverts the value of IP_MULTICAST_LOOP Summary: MulticastSocket.getOption(StandardSocketOption.IP_MULTICAST_LOOP) now returns true if loopback mode is enabled, and MulticastSocket.setOption(StandardSocketOption.IP_MULTICAST_LOOP, true) enables loopback mode. No other behavioral changes. Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     1
/*
54086
ccb4a50bee06 8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents: 48397
diff changeset
     2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     4
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     8
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    13
 * accompanied this code).
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    14
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    18
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    21
 * questions.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    22
 */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    23
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    24
/*
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    25
 * @test
59224
55fdee124e89 8233296: MulticastSocket getOption/setOption inverts the value of IP_MULTICAST_LOOP
dfuchs
parents: 59206
diff changeset
    26
 * @bug 8036979 8072384 8044773 8225214 8233296
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 54681
diff changeset
    27
 * @library /test/lib
48397
ead47ddf5844 8191788: add jdk.internal.vm.compiler to --limit-modules if -Djvmci.Compiler=graal is in the command line
kvn
parents: 47216
diff changeset
    28
 * @requires !vm.graal.enabled
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    29
 * @run main/othervm -Xcheck:jni OptionsTest
55226
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
    30
 * @run main/othervm -Djdk.net.usePlainSocketImpl OptionsTest
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 23879
diff changeset
    31
 * @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true OptionsTest
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 38474
diff changeset
    32
 * @run main/othervm --limit-modules=java.base OptionsTest
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    33
 */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    34
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    35
import java.lang.reflect.Method;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    36
import java.net.*;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    37
import java.util.*;
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 54681
diff changeset
    38
import jdk.test.lib.net.IPSupport;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    39
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    40
public class OptionsTest {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    41
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    42
    static class Test<T> {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    43
        final SocketOption<T> option;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    44
        final T value;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    45
        Test(SocketOption<T> option, T value) {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    46
            this.option = option;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    47
            this.value = value;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    48
        }
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    49
        static <T> Test<T> create(SocketOption<T> option, T value) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    50
            return new Test<T>(option, value);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    51
        }
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    52
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    53
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    54
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    55
    // The tests set the option using the new API, read back the set value
55226
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
    56
    // which could be different, and then use the legacy get API to check
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    57
    // these values are the same
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    58
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    59
    static Test<?>[] socketTests = new Test<?>[] {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    60
        Test.create(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE),
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    61
        Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    62
        Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    63
        Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
    64
        Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
55226
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
    65
        Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(-1)),
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
    66
        Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(0)),
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    67
        Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(80)),
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
    68
        Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(0)),  // lower-bound
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
    69
        Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)),
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
    70
        Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(255))  //upper-bound
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    71
    };
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    72
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    73
    static Test<?>[] serverSocketTests = new Test<?>[] {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    74
        Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 23879
diff changeset
    75
        Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
    76
        Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
    77
        Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(0)),  // lower-bound
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
    78
        Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)),
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
    79
        Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(255))  //upper-bound
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    80
    };
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    81
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    82
    static Test<?>[] datagramSocketTests = new Test<?>[] {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    83
        Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    84
        Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    85
        Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
    86
        Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
59206
61091a42f19d 8234083: DatagramSocket should report SO_BROADCAST as a supported option
pconcannon
parents: 55226
diff changeset
    87
        Test.create(StandardSocketOptions.SO_BROADCAST, Boolean.FALSE),
61091a42f19d 8234083: DatagramSocket should report SO_BROADCAST as a supported option
pconcannon
parents: 55226
diff changeset
    88
        Test.create(StandardSocketOptions.SO_BROADCAST, Boolean.TRUE),
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
    89
        Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(0)),  // lower-bound
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
    90
        Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)),
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
    91
        Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(255))  //upper-bound
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    92
    };
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    93
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    94
    static Test<?>[] multicastSocketTests = new Test<?>[] {
59206
61091a42f19d 8234083: DatagramSocket should report SO_BROADCAST as a supported option
pconcannon
parents: 55226
diff changeset
    95
        Test.create(StandardSocketOptions.SO_BROADCAST, Boolean.FALSE),
61091a42f19d 8234083: DatagramSocket should report SO_BROADCAST as a supported option
pconcannon
parents: 55226
diff changeset
    96
        Test.create(StandardSocketOptions.SO_BROADCAST, Boolean.TRUE),
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    97
        Test.create(StandardSocketOptions.IP_MULTICAST_IF, getNetworkInterface()),
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
    98
        Test.create(StandardSocketOptions.IP_MULTICAST_TTL, Integer.valueOf(0)),   // lower-bound
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    99
        Test.create(StandardSocketOptions.IP_MULTICAST_TTL, Integer.valueOf(10)),
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54811
diff changeset
   100
        Test.create(StandardSocketOptions.IP_MULTICAST_TTL, Integer.valueOf(255)), //upper-bound
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   101
        Test.create(StandardSocketOptions.IP_MULTICAST_LOOP, Boolean.TRUE)
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   102
    };
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   103
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   104
    static NetworkInterface getNetworkInterface() {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   105
        try {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   106
            Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
34889
9aac81c72779 8146482: [TESTBUG] java/net/SocketOption/OptionTest should only use multicast capable interfaces for multicast tests
simonis
parents: 30963
diff changeset
   107
            while (nifs.hasMoreElements()) {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   108
                NetworkInterface ni = nifs.nextElement();
34889
9aac81c72779 8146482: [TESTBUG] java/net/SocketOption/OptionTest should only use multicast capable interfaces for multicast tests
simonis
parents: 30963
diff changeset
   109
                if (ni.supportsMulticast()) {
9aac81c72779 8146482: [TESTBUG] java/net/SocketOption/OptionTest should only use multicast capable interfaces for multicast tests
simonis
parents: 30963
diff changeset
   110
                    return ni;
9aac81c72779 8146482: [TESTBUG] java/net/SocketOption/OptionTest should only use multicast capable interfaces for multicast tests
simonis
parents: 30963
diff changeset
   111
                }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   112
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   113
        } catch (Exception e) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   114
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   115
        return null;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   116
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   117
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   118
    static boolean okayToTest(Socket s, SocketOption<?> option) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   119
        if (option == StandardSocketOptions.SO_REUSEPORT) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   120
            // skip SO_REUSEPORT if option is not supported
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   121
            return s.supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   122
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   123
        if (option == StandardSocketOptions.IP_TOS && s.isConnected()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   124
            // skip IP_TOS if connected
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   125
            return false;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   126
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   127
        return true;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   128
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   129
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   130
    static <T> void testEqual(SocketOption<T> option, T value1, T value2) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   131
        if (!value1.equals(value2)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   132
            throw new RuntimeException("Test of " + option.name() + " failed: "
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   133
                    + value1 + " != " + value2);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   134
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   135
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   136
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   137
    static <T> void test(Socket s, Test<T> test) throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   138
        SocketOption<T> option = test.option;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   139
        s.setOption(option, test.value);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   140
        T value1 = s.getOption(test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   141
        T value2 = (T) legacyGetOption(Socket.class, s, test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   142
        testEqual(option, value1, value2);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   143
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   144
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   145
    static <T> void test(ServerSocket ss, Test<T> test) throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   146
        SocketOption<T> option = test.option;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   147
        ss.setOption(option, test.value);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   148
        T value1 = ss.getOption(test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   149
        T value2 = (T) legacyGetOption(ServerSocket.class, ss, test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   150
        testEqual(option, value1, value2);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   151
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   152
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   153
    static <T> void test(DatagramSocket ds, Test<T> test) throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   154
        SocketOption<T> option = test.option;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   155
        ds.setOption(option, test.value);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   156
        T value1 = ds.getOption(test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   157
        T value2 = (T) legacyGetOption(ds.getClass(), ds, test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   158
        testEqual(option, value1, value2);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   159
    }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   160
55226
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   161
    // Tests default and negative values of SO_LINGER. All negative values should
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   162
    // retrieve as -1.
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   163
    static void testSoLingerValues() throws Exception {
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   164
        try (Socket s = new Socket()) {
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   165
            // retrieve without set
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   166
            int defaultValue = s.getOption(StandardSocketOptions.SO_LINGER);
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   167
            testEqual(StandardSocketOptions.SO_LINGER, -1, defaultValue);
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   168
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   169
            for (int v : List.of(-1, -2, -100, -65534, -65535, -65536, -100000)) {
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   170
                System.out.println("Testing SO_LINGER with:" + v);
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   171
                s.setOption(StandardSocketOptions.SO_LINGER, v);
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   172
                int value = s.getOption(StandardSocketOptions.SO_LINGER);
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   173
                testEqual(StandardSocketOptions.SO_LINGER, -1, value);
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   174
            }
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   175
        }
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   176
    }
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   177
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   178
    @SuppressWarnings("try")
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   179
    static void doSocketTests() throws Exception {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   180
        // unconnected socket
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   181
        try (Socket s = new Socket()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   182
            for (Test<?> test : socketTests) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   183
                if (okayToTest(s, test.option)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   184
                    test(s, test);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   185
                }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   186
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   187
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   188
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   189
        // connected socket
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   190
        try (ServerSocket ss = new ServerSocket()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   191
            var loopback = InetAddress.getLoopbackAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   192
            ss.bind(new InetSocketAddress(loopback, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   193
            try (Socket s1 = new Socket()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   194
                s1.connect(ss.getLocalSocketAddress());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   195
                try (Socket s2 = ss.accept()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   196
                    for (Test<?> test : socketTests) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   197
                        if (okayToTest(s1, test.option)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   198
                            test(s1, test);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   199
                        }
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   200
                    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   201
                }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   202
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   203
        }
55226
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   204
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   205
        testSoLingerValues();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   206
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   207
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   208
    static void doServerSocketTests() throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   209
        try (ServerSocket ss = new ServerSocket(0)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   210
            Set<SocketOption<?>> options = ss.supportedOptions();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   211
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   212
            for (Test<?> test : serverSocketTests) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   213
                if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   214
                    test(ss, test);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   215
                }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   216
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   217
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   218
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   219
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   220
    static void doDatagramSocketTests() throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   221
        try (DatagramSocket ds = new DatagramSocket(0)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   222
            Set<SocketOption<?>> options = ds.supportedOptions();
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   223
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   224
            for (Test<?> test : datagramSocketTests) {
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   225
                if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   226
                    test(ds, test);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   227
                }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   228
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   229
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   230
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   231
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   232
    static void doMulticastSocketTests() throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   233
        try (MulticastSocket ms = new MulticastSocket(0)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   234
            for (Test<?> test : multicastSocketTests) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   235
                test(ms, test);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   236
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   237
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   238
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   239
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   240
    static Object legacyGetOption(Class<?> type, Object s, Object option) throws Exception {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   241
        if (type.equals(Socket.class)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   242
            Socket socket = (Socket)s;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   243
            Set<SocketOption<?>> options = socket.supportedOptions();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   244
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   245
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   246
            if (option.equals(StandardSocketOptions.SO_KEEPALIVE)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   247
                return Boolean.valueOf(socket.getKeepAlive());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   248
            } else if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   249
                return Integer.valueOf(socket.getSendBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   250
            } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   251
                return Integer.valueOf(socket.getReceiveBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   252
            } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   253
                return Boolean.valueOf(socket.getReuseAddress());
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   254
            } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   255
                return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   256
            } else if (option.equals(StandardSocketOptions.SO_LINGER)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   257
                return Integer.valueOf(socket.getSoLinger());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   258
            } else if (option.equals(StandardSocketOptions.IP_TOS)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   259
                return Integer.valueOf(socket.getTrafficClass());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   260
            } else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   261
                return Boolean.valueOf(socket.getTcpNoDelay());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   262
            } else {
55226
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   263
                throw new RuntimeException("unexpected socket option");
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   264
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   265
        } else if  (type.equals(ServerSocket.class)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   266
            ServerSocket socket = (ServerSocket)s;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   267
            Set<SocketOption<?>> options = socket.supportedOptions();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   268
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   269
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   270
            if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   271
                return Integer.valueOf(socket.getReceiveBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   272
            } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   273
                return Boolean.valueOf(socket.getReuseAddress());
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   274
            } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   275
                return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 23879
diff changeset
   276
            } else if (option.equals(StandardSocketOptions.IP_TOS)) {
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   277
                return getServerSocketTrafficClass(socket);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   278
            } else {
55226
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   279
                throw new RuntimeException("unexpected socket option");
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   280
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   281
        } else if  (type.equals(DatagramSocket.class)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   282
            DatagramSocket socket = (DatagramSocket)s;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   283
            Set<SocketOption<?>> options = socket.supportedOptions();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   284
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   285
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   286
            if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   287
                return Integer.valueOf(socket.getSendBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   288
            } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   289
                return Integer.valueOf(socket.getReceiveBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   290
            } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   291
                return Boolean.valueOf(socket.getReuseAddress());
59206
61091a42f19d 8234083: DatagramSocket should report SO_BROADCAST as a supported option
pconcannon
parents: 55226
diff changeset
   292
            } else if (option.equals(StandardSocketOptions.SO_BROADCAST)) {
61091a42f19d 8234083: DatagramSocket should report SO_BROADCAST as a supported option
pconcannon
parents: 55226
diff changeset
   293
                return Boolean.valueOf(socket.getBroadcast());
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   294
            } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   295
                return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   296
            } else if (option.equals(StandardSocketOptions.IP_TOS)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   297
                return Integer.valueOf(socket.getTrafficClass());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   298
            } else {
55226
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   299
                throw new RuntimeException("unexpected socket option");
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   300
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   301
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   302
        } else if  (type.equals(MulticastSocket.class)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   303
            MulticastSocket socket = (MulticastSocket)s;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   304
            Set<SocketOption<?>> options = socket.supportedOptions();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   305
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   306
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   307
            if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   308
                return Integer.valueOf(socket.getSendBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   309
            } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   310
                return Integer.valueOf(socket.getReceiveBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   311
            } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   312
                return Boolean.valueOf(socket.getReuseAddress());
59206
61091a42f19d 8234083: DatagramSocket should report SO_BROADCAST as a supported option
pconcannon
parents: 55226
diff changeset
   313
            } else if (option.equals(StandardSocketOptions.SO_BROADCAST)) {
61091a42f19d 8234083: DatagramSocket should report SO_BROADCAST as a supported option
pconcannon
parents: 55226
diff changeset
   314
                return Boolean.valueOf(socket.getBroadcast());
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   315
            } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   316
                return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   317
            } else if (option.equals(StandardSocketOptions.IP_TOS)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   318
                return Integer.valueOf(socket.getTrafficClass());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   319
            } else if (option.equals(StandardSocketOptions.IP_MULTICAST_IF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   320
                return socket.getNetworkInterface();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   321
            } else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   322
                return Integer.valueOf(socket.getTimeToLive());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   323
            } else if (option.equals(StandardSocketOptions.IP_MULTICAST_LOOP)) {
59224
55fdee124e89 8233296: MulticastSocket getOption/setOption inverts the value of IP_MULTICAST_LOOP
dfuchs
parents: 59206
diff changeset
   324
                return !Boolean.valueOf(socket.getLoopbackMode());
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   325
            } else {
55226
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   326
                throw new RuntimeException("unexpected socket option");
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   327
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   328
        }
55226
ce8bab2c4185 8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents: 55102
diff changeset
   329
        throw new RuntimeException("unexpected socket type");
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   330
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   331
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   332
    public static void main(String args[]) throws Exception {
54811
9db7c0f561a6 8223652: Rename IPSupport.skipIfCurrentConfigurationIsInvalid() to IPSupport.throwSkippedExceptionIfNonOperational()
aeubanks
parents: 54770
diff changeset
   333
        IPSupport.throwSkippedExceptionIfNonOperational();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   334
        doSocketTests();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   335
        doServerSocketTests();
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   336
        doDatagramSocketTests();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   337
        doMulticastSocketTests();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   338
    }
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   339
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   340
    // Reflectively access jdk.net.Sockets.getOption so that the test can run
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   341
    // without the jdk.net module.
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   342
    static Object getServerSocketTrafficClass(ServerSocket ss) throws Exception {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   343
        try {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   344
            Class<?> c = Class.forName("jdk.net.Sockets");
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   345
            Method m = c.getDeclaredMethod("getOption", ServerSocket.class, SocketOption.class);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   346
            return m.invoke(null, ss, StandardSocketOptions.IP_TOS);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   347
        } catch (ClassNotFoundException e) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   348
            // Ok, jdk.net module not present, just fall back
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   349
            System.out.println("jdk.net module not present, falling back.");
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   350
            return Integer.valueOf(ss.getOption(StandardSocketOptions.IP_TOS));
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   351
        } catch (ReflectiveOperationException e) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   352
            throw new AssertionError(e);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   353
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   354
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   355
}