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