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