test/jdk/java/net/SocketOption/OptionsTest.java
author alanb
Thu, 30 May 2019 07:19:19 +0100
changeset 55102 59567035d279
parent 55081 dd321e3596c0
child 55226 ce8bab2c4185
permissions -rw-r--r--
8221481: Reimplement the Legacy Socket API Reviewed-by: michaelm, 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
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    26
 * @bug 8036979 8072384 8044773
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
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 23879
diff changeset
    30
 * @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
    31
 * @run main/othervm --limit-modules=java.base OptionsTest
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    32
 */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    33
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    34
import java.lang.reflect.Method;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    35
import java.net.*;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    36
import java.util.*;
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 54681
diff changeset
    37
import jdk.test.lib.net.IPSupport;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    38
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    39
public class OptionsTest {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    40
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    41
    static class Test<T> {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    42
        final SocketOption<T> option;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    43
        final T value;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    44
        Test(SocketOption<T> option, T value) {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    45
            this.option = option;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    46
            this.value = value;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    47
        }
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    48
        static <T> Test<T> create(SocketOption<T> option, T value) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    49
            return new Test<T>(option, value);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    50
        }
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    51
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    52
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    53
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    54
    // The tests set the option using the new API, read back the set value
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    55
    // which could be diferent, and then use the legacy get API to check
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    56
    // these values are the same
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    57
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    58
    static Test<?>[] socketTests = new Test<?>[] {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    59
        Test.create(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE),
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    60
        Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    61
        Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    62
        Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
    63
        Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    64
        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
    65
        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
    66
        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
    67
        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
    68
    };
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    69
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    70
    static Test<?>[] serverSocketTests = new Test<?>[] {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    71
        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
    72
        Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
    73
        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
    74
        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
    75
        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
    76
        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
    77
    };
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    78
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    79
    static Test<?>[] datagramSocketTests = new Test<?>[] {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    80
        Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    81
        Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    82
        Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
    83
        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
    84
        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
    85
        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
    86
        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
    87
    };
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    88
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    89
    static Test<?>[] multicastSocketTests = new Test<?>[] {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    90
        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
    91
        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
    92
        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
    93
        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
    94
        Test.create(StandardSocketOptions.IP_MULTICAST_LOOP, Boolean.TRUE)
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    95
    };
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    96
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    97
    static NetworkInterface getNetworkInterface() {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    98
        try {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    99
            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
   100
            while (nifs.hasMoreElements()) {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   101
                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
   102
                if (ni.supportsMulticast()) {
9aac81c72779 8146482: [TESTBUG] java/net/SocketOption/OptionTest should only use multicast capable interfaces for multicast tests
simonis
parents: 30963
diff changeset
   103
                    return ni;
9aac81c72779 8146482: [TESTBUG] java/net/SocketOption/OptionTest should only use multicast capable interfaces for multicast tests
simonis
parents: 30963
diff changeset
   104
                }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   105
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   106
        } catch (Exception e) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   107
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   108
        return null;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   109
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   110
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   111
    static boolean okayToTest(Socket s, SocketOption<?> option) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   112
        if (option == StandardSocketOptions.SO_REUSEPORT) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   113
            // skip SO_REUSEPORT if option is not supported
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   114
            return s.supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   115
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   116
        if (option == StandardSocketOptions.IP_TOS && s.isConnected()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   117
            // skip IP_TOS if connected
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   118
            return false;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   119
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   120
        return true;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   121
    }
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
    static <T> void testEqual(SocketOption<T> option, T value1, T value2) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   124
        if (!value1.equals(value2)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   125
            throw new RuntimeException("Test of " + option.name() + " failed: "
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   126
                    + value1 + " != " + value2);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   127
        }
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 test(Socket s, Test<T> test) throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   131
        SocketOption<T> option = test.option;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   132
        s.setOption(option, test.value);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   133
        T value1 = s.getOption(test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   134
        T value2 = (T) legacyGetOption(Socket.class, s, test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   135
        testEqual(option, value1, value2);
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   138
    static <T> void test(ServerSocket ss, Test<T> test) throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   139
        SocketOption<T> option = test.option;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   140
        ss.setOption(option, test.value);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   141
        T value1 = ss.getOption(test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   142
        T value2 = (T) legacyGetOption(ServerSocket.class, ss, test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   143
        testEqual(option, value1, value2);
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   146
    static <T> void test(DatagramSocket ds, Test<T> test) throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   147
        SocketOption<T> option = test.option;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   148
        ds.setOption(option, test.value);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   149
        T value1 = ds.getOption(test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   150
        T value2 = (T) legacyGetOption(ds.getClass(), ds, test.option);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   151
        testEqual(option, value1, value2);
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
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   154
    @SuppressWarnings("try")
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   155
    static void doSocketTests() throws Exception {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   156
        // unconnected socket
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   157
        try (Socket s = new Socket()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   158
            for (Test<?> test : socketTests) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   159
                if (okayToTest(s, test.option)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   160
                    test(s, test);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   161
                }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   162
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   163
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   164
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   165
        // connected socket
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   166
        try (ServerSocket ss = new ServerSocket()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   167
            var loopback = InetAddress.getLoopbackAddress();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   168
            ss.bind(new InetSocketAddress(loopback, 0));
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   169
            try (Socket s1 = new Socket()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   170
                s1.connect(ss.getLocalSocketAddress());
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   171
                try (Socket s2 = ss.accept()) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   172
                    for (Test<?> test : socketTests) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   173
                        if (okayToTest(s1, test.option)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   174
                            test(s1, test);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   175
                        }
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   176
                    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   177
                }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   178
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   179
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   180
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   181
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   182
    static void doServerSocketTests() throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   183
        try (ServerSocket ss = new ServerSocket(0)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   184
            Set<SocketOption<?>> options = ss.supportedOptions();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   185
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   186
            for (Test<?> test : serverSocketTests) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   187
                if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   188
                    test(ss, test);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   189
                }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   190
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   191
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   192
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   193
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   194
    static void doDatagramSocketTests() throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   195
        try (DatagramSocket ds = new DatagramSocket(0)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   196
            Set<SocketOption<?>> options = ds.supportedOptions();
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   197
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   198
            for (Test<?> test : datagramSocketTests) {
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   199
                if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   200
                    test(ds, test);
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
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   204
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   205
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   206
    static void doMulticastSocketTests() throws Exception {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   207
        try (MulticastSocket ms = new MulticastSocket(0)) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   208
            for (Test<?> test : multicastSocketTests) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   209
                test(ms, test);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   210
            }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   211
        }
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   212
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   213
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   214
    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
   215
        if (type.equals(Socket.class)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   216
            Socket socket = (Socket)s;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   217
            Set<SocketOption<?>> options = socket.supportedOptions();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   218
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   219
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   220
            if (option.equals(StandardSocketOptions.SO_KEEPALIVE)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   221
                return Boolean.valueOf(socket.getKeepAlive());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   222
            } else if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   223
                return Integer.valueOf(socket.getSendBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   224
            } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   225
                return Integer.valueOf(socket.getReceiveBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   226
            } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   227
                return Boolean.valueOf(socket.getReuseAddress());
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   228
            } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   229
                return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   230
            } else if (option.equals(StandardSocketOptions.SO_LINGER)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   231
                return Integer.valueOf(socket.getSoLinger());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   232
            } else if (option.equals(StandardSocketOptions.IP_TOS)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   233
                return Integer.valueOf(socket.getTrafficClass());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   234
            } else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   235
                return Boolean.valueOf(socket.getTcpNoDelay());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   236
            } else {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   237
                throw new RuntimeException("unexecpted socket option");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   238
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   239
        } else if  (type.equals(ServerSocket.class)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   240
            ServerSocket socket = (ServerSocket)s;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   241
            Set<SocketOption<?>> options = socket.supportedOptions();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   242
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   243
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   244
            if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   245
                return Integer.valueOf(socket.getReceiveBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   246
            } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   247
                return Boolean.valueOf(socket.getReuseAddress());
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   248
            } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   249
                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
   250
            } 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
   251
                return getServerSocketTrafficClass(socket);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   252
            } else {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   253
                throw new RuntimeException("unexecpted socket option");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   254
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   255
        } else if  (type.equals(DatagramSocket.class)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   256
            DatagramSocket socket = (DatagramSocket)s;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   257
            Set<SocketOption<?>> options = socket.supportedOptions();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   258
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   259
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   260
            if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   261
                return Integer.valueOf(socket.getSendBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   262
            } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   263
                return Integer.valueOf(socket.getReceiveBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   264
            } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   265
                return Boolean.valueOf(socket.getReuseAddress());
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   266
            } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   267
                return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   268
            } else if (option.equals(StandardSocketOptions.IP_TOS)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   269
                return Integer.valueOf(socket.getTrafficClass());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   270
            } else {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   271
                throw new RuntimeException("unexecpted socket option");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   272
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   273
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   274
        } else if  (type.equals(MulticastSocket.class)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   275
            MulticastSocket socket = (MulticastSocket)s;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   276
            Set<SocketOption<?>> options = socket.supportedOptions();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   277
            boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   278
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   279
            if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   280
                return Integer.valueOf(socket.getSendBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   281
            } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   282
                return Integer.valueOf(socket.getReceiveBufferSize());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   283
            } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   284
                return Boolean.valueOf(socket.getReuseAddress());
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   285
            } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34889
diff changeset
   286
                return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   287
            } else if (option.equals(StandardSocketOptions.IP_TOS)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   288
                return Integer.valueOf(socket.getTrafficClass());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   289
            } else if (option.equals(StandardSocketOptions.IP_MULTICAST_IF)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   290
                return socket.getNetworkInterface();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   291
            } else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   292
                return Integer.valueOf(socket.getTimeToLive());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   293
            } else if (option.equals(StandardSocketOptions.IP_MULTICAST_LOOP)) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   294
                return Boolean.valueOf(socket.getLoopbackMode());
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   295
            } else {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   296
                throw new RuntimeException("unexecpted socket option");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   297
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   298
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   299
        throw new RuntimeException("unexecpted socket type");
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
    public static void main(String args[]) throws Exception {
54811
9db7c0f561a6 8223652: Rename IPSupport.skipIfCurrentConfigurationIsInvalid() to IPSupport.throwSkippedExceptionIfNonOperational()
aeubanks
parents: 54770
diff changeset
   303
        IPSupport.throwSkippedExceptionIfNonOperational();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   304
        doSocketTests();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   305
        doServerSocketTests();
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   306
        doDatagramSocketTests();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
   307
        doMulticastSocketTests();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   308
    }
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   309
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   310
    // 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
   311
    // 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
   312
    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
   313
        try {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   314
            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
   315
            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
   316
            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
   317
        } 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
   318
            // 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
   319
            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
   320
            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
   321
        } 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
   322
            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
   323
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   324
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   325
}