test/jdk/jdk/net/RdmaSockets/rsocket/SocketOption/UnsupportedOptionsTest.java
author bpb
Wed, 06 Feb 2019 11:54:19 -0800
branchrsocket-branch
changeset 57154 4fdc05fd6996
parent 57115 512e7cc6ccce
child 57156 81e4a12fd1a4
permissions -rw-r--r--
rsocket-branch: test options that neither Socket nor ServerSocket support
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
import java.io.IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
import java.net.StandardProtocolFamily;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
import java.net.ServerSocket;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
import java.net.Socket;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
import java.net.SocketOption;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
import java.net.StandardSocketOptions;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    30
import java.util.ArrayList;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    31
import java.util.List;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
import jdk.net.RdmaSockets;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    33
import jdk.net.RdmaSocketOptions;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
import jtreg.SkippedException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
/* @test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
 * @bug 8195160
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
 * @summary Test checks that UnsupportedOperationException for unsupported
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
 *          SOCKET_OPTIONS is thrown by both getOption() and setOption() methods.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
 * @requires (os.family == "linux")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
 * @requires !vm.graal.enabled
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
 * @library .. /test/lib
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
 * @build RsocketTest
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
 * @run main/othervm UnsupportedOptionsTest
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
public class UnsupportedOptionsTest {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
    private static final List<SocketOption<?>> socketOptions = new ArrayList<>();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
    static {
57154
4fdc05fd6996 rsocket-branch: test options that neither Socket nor ServerSocket support
bpb
parents: 57115
diff changeset
    52
        socketOptions.add(StandardSocketOptions.SO_KEEPALIVE); // Unsupported
4fdc05fd6996 rsocket-branch: test options that neither Socket nor ServerSocket support
bpb
parents: 57115
diff changeset
    53
        socketOptions.add(StandardSocketOptions.SO_LINGER);    // Unsupported
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
        socketOptions.add(StandardSocketOptions.SO_RCVBUF);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
        socketOptions.add(StandardSocketOptions.SO_REUSEADDR);
57154
4fdc05fd6996 rsocket-branch: test options that neither Socket nor ServerSocket support
bpb
parents: 57115
diff changeset
    56
        socketOptions.add(StandardSocketOptions.SO_SNDBUF);    // Socket only
4fdc05fd6996 rsocket-branch: test options that neither Socket nor ServerSocket support
bpb
parents: 57115
diff changeset
    57
        socketOptions.add(StandardSocketOptions.TCP_NODELAY);  // Socket only
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
        socketOptions.add(RdmaSocketOptions.RDMA_SQSIZE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
        socketOptions.add(RdmaSocketOptions.RDMA_RQSIZE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
        socketOptions.add(RdmaSocketOptions.RDMA_INLINE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
    public static void main(String[] args) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
        if (!RsocketTest.isRsocketAvailable())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
            throw new SkippedException("rsocket is not available");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
        Socket s = RdmaSockets.openSocket(StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
        ServerSocket ss = RdmaSockets.openServerSocket(
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
            StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
        for (SocketOption option : socketOptions) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
            if (!s.supportedOptions().contains(option)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
                testUnsupportedSocketOption(s, option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
            if (!ss.supportedOptions().contains(option)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
                testUnsupportedSocketOption(ss, option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
    /*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
     * Check that UnsupportedOperationException for unsupported option is
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
     * thrown from both getOption() and setOption() methods.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
     */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
    private static void testUnsupportedSocketOption(Object socket,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
                                                    SocketOption option) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
        testSet(socket, option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
        testGet(socket, option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
    private static void testSet(Object socket, SocketOption option) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    94
            setOption(socket, option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
        } catch (UnsupportedOperationException e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
            System.out.println("UnsupportedOperationException was throw " +
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
                    "as expected. Socket: " + socket + " Option: " + option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
            return;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
        } catch (Exception e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
            throw new RuntimeException("FAIL. Unexpected exception.", e);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
        throw new RuntimeException("FAIL. UnsupportedOperationException " +
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
                "hasn't been thrown. Socket: " + socket + " Option: " + option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
    private static void testGet(Object socket, SocketOption option) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
            getOption(socket, option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
        } catch (UnsupportedOperationException e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   110
            System.out.println("UnsupportedOperationException was throw " +
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   111
                    "as expected. Socket: " + socket + " Option: " + option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   112
            return;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   113
        } catch (Exception e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   114
            throw new RuntimeException("FAIL. Unexpected exception.", e);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   115
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   116
        throw new RuntimeException("FAIL. UnsupportedOperationException " +
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   117
                "hasn't been thrown. Socket: " + socket + " Option: " + option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   118
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   119
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   120
    private static void getOption(Object socket,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   121
                                  SocketOption option) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   122
        if (socket instanceof Socket) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   123
            ((Socket) socket).getOption(option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   124
        } else if (socket instanceof ServerSocket) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   125
            ((ServerSocket) socket).getOption(option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   126
        } else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   127
            throw new RuntimeException("Unsupported socket type");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   128
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   129
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   130
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   131
    private static void setOption(Object socket,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   132
                                  SocketOption option) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   133
        if (socket instanceof Socket) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   134
            ((Socket) socket).setOption(option, null);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   135
        } else if (socket instanceof ServerSocket) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   136
            ((ServerSocket) socket).setOption(option, null);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   137
        } else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   138
            throw new RuntimeException("Unsupported socket type");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   139
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   140
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   141
}