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.ServerSocket;
  26 import java.net.Socket;
  27 import java.net.SocketOption;
  28 import java.net.StandardSocketOptions;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 import jdk.net.Sockets;
  32 import jdk.net.RdmaSocketOptions;
  33 
  34 /* @test
  35  * @bug 8195160
  36  * @summary Test checks that UnsupportedOperationException for unsupported
  37  *          SOCKET_OPTIONS is thrown by both getOption() and setOption() methods.
  38  * @requires (os.family == "linux")
  39  * @requires !vm.graal.enabled
  40  * @library .. /test/lib
  41  * @build RsocketTest
  42  * @run main/othervm -Djava.net.preferIPv4Stack=true UnsupportedOptionsTest
  43  */
  44 public class UnsupportedOptionsTest {
  45 
  46     private static final List<SocketOption<?>> socketOptions = new ArrayList<>();
  47 
  48     static {
  49         socketOptions.add(StandardSocketOptions.SO_RCVBUF);
  50         socketOptions.add(StandardSocketOptions.SO_REUSEADDR);
  51         socketOptions.add(StandardSocketOptions.SO_SNDBUF);
  52         socketOptions.add(StandardSocketOptions.TCP_NODELAY);
  53         socketOptions.add(RdmaSocketOptions.RDMA_SQSIZE);
  54         socketOptions.add(RdmaSocketOptions.RDMA_RQSIZE);
  55         socketOptions.add(RdmaSocketOptions.RDMA_INLINE);
  56     }
  57 
  58     public static void main(String[] args) throws IOException {
  59         if (!RsocketTest.isRsocketAvailable())
  60             return;
  61 
  62         Socket s = Sockets.openRdmaSocket();
  63         ServerSocket ss = Sockets.openRdmaServerSocket();
  64 
  65         for (SocketOption option : socketOptions) {
  66             if (!s.supportedOptions().contains(option)) {
  67                 testUnsupportedSocketOption(s, option);
  68             }
  69 
  70             if (!ss.supportedOptions().contains(option)) {
  71                 testUnsupportedSocketOption(ss, option);
  72             }
  73         }
  74     }
  75 
  76     /*
  77      * Check that UnsupportedOperationException for unsupported option is
  78      * thrown from both getOption() and setOption() methods.
  79      */
  80     private static void testUnsupportedSocketOption(Object socket,
  81                                                     SocketOption option) {
  82         testSet(socket, option);
  83         testGet(socket, option);
  84     }
  85 
  86     private static void testSet(Object socket, SocketOption option) {
  87         try {
  88             setOption(socket, option);
  89         } catch (UnsupportedOperationException e) {
  90             System.out.println("UnsupportedOperationException was throw " +
  91                     "as expected. Socket: " + socket + " Option: " + option);
  92             return;
  93         } catch (Exception e) {
  94             throw new RuntimeException("FAIL. Unexpected exception.", e);
  95         }
  96         throw new RuntimeException("FAIL. UnsupportedOperationException " +
  97                 "hasn't been thrown. Socket: " + socket + " Option: " + option);
  98     }
  99 
 100     private static void testGet(Object socket, SocketOption option) {
 101         try {
 102             getOption(socket, option);
 103         } catch (UnsupportedOperationException e) {
 104             System.out.println("UnsupportedOperationException was throw " +
 105                     "as expected. Socket: " + socket + " Option: " + option);
 106             return;
 107         } catch (Exception e) {
 108             throw new RuntimeException("FAIL. Unexpected exception.", e);
 109         }
 110         throw new RuntimeException("FAIL. UnsupportedOperationException " +
 111                 "hasn't been thrown. Socket: " + socket + " Option: " + option);
 112     }
 113 
 114     private static void getOption(Object socket,
 115                                   SocketOption option) throws IOException {
 116         if (socket instanceof Socket) {
 117             ((Socket) socket).getOption(option);
 118         } else if (socket instanceof ServerSocket) {
 119             ((ServerSocket) socket).getOption(option);
 120         } else {
 121             throw new RuntimeException("Unsupported socket type");
 122         }
 123     }
 124 
 125     private static void setOption(Object socket,
 126                                   SocketOption option) throws IOException {
 127         if (socket instanceof Socket) {
 128             ((Socket) socket).setOption(option, null);
 129         } else if (socket instanceof ServerSocket) {
 130             ((ServerSocket) socket).setOption(option, null);
 131         } else {
 132             throw new RuntimeException("Unsupported socket type");
 133         }
 134     }
 135 }