1 /* 2 * Copyright (c) 2016, 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 jdk.net.ExtendedSocketOptions; 25 26 import java.io.IOException; 27 import java.net.*; 28 29 /* 30 * @test 31 * @bug 8143554 32 * @run main UnsupportedOptionsTest 33 * @summary Test checks that UnsupportedOperationException for unsupported 34 * SOCKET_OPTIONS is thrown by both getOption() and setOption() methods. 35 */ 36 public class UnsupportedOptionsTest { 37 38 private static final SocketOption[] SOCKET_OPTIONS = { 39 StandardSocketOptions.IP_MULTICAST_IF, 40 StandardSocketOptions.IP_MULTICAST_LOOP, 41 StandardSocketOptions.IP_MULTICAST_TTL, 42 StandardSocketOptions.IP_TOS, 43 StandardSocketOptions.SO_BROADCAST, 44 StandardSocketOptions.SO_KEEPALIVE, 45 StandardSocketOptions.SO_LINGER, 46 StandardSocketOptions.SO_RCVBUF, 47 StandardSocketOptions.SO_REUSEADDR, 48 StandardSocketOptions.SO_SNDBUF, 49 StandardSocketOptions.TCP_NODELAY, 50 ExtendedSocketOptions.SO_FLOW_SLA 51 }; 52 53 public static void main(String[] args) throws IOException { 54 Socket s = new Socket(); 55 ServerSocket ss = new ServerSocket(); 56 DatagramSocket ds = new DatagramSocket(); 57 MulticastSocket ms = new MulticastSocket(); 58 59 for (SocketOption option : SOCKET_OPTIONS) { 60 if (!s.supportedOptions().contains(option)) { 61 testUnsupportedSocketOption(s, option); 62 } 63 64 if (!ss.supportedOptions().contains(option)) { 65 testUnsupportedSocketOption(ss, option); 66 } 67 68 if (!ms.supportedOptions().contains(option)) { 69 testUnsupportedSocketOption(ms, option); 70 } 71 72 if (!ds.supportedOptions().contains(option)) { 73 testUnsupportedSocketOption(ds, option); 74 } 75 } 76 } 77 78 /* 79 * Check that UnsupportedOperationException for unsupported option is 80 * thrown from both getOption() and setOption() methods. 81 */ 82 private static void testUnsupportedSocketOption(Object socket, 83 SocketOption option) { 84 testSet(socket, option); 85 testGet(socket, option); 86 } 87 88 private static void testSet(Object socket, SocketOption option) { 89 try { 90 setOption(socket, option); 91 } catch (UnsupportedOperationException e) { 92 System.out.println("UnsupportedOperationException was throw " + 93 "as expected. Socket: " + socket + " Option: " + option); 94 return; 95 } catch (Exception e) { 96 throw new RuntimeException("FAIL. Unexpected exception.", e); 97 } 98 throw new RuntimeException("FAIL. UnsupportedOperationException " + 99 "hasn't been thrown. Socket: " + socket + " Option: " + option); 100 } 101 102 private static void testGet(Object socket, SocketOption option) { 103 try { 104 getOption(socket, option); 105 } catch (UnsupportedOperationException e) { 106 System.out.println("UnsupportedOperationException was throw " + 107 "as expected. Socket: " + socket + " Option: " + option); 108 return; 109 } catch (Exception e) { 110 throw new RuntimeException("FAIL. Unexpected exception.", e); 111 } 112 throw new RuntimeException("FAIL. UnsupportedOperationException " + 113 "hasn't been thrown. Socket: " + socket + " Option: " + option); 114 } 115 116 private static void getOption(Object socket, 117 SocketOption option) throws IOException { 118 if (socket instanceof Socket) { 119 ((Socket) socket).getOption(option); 120 } else if (socket instanceof ServerSocket) { 121 ((ServerSocket) socket).getOption(option); 122 } else if (socket instanceof DatagramSocket) { 123 ((DatagramSocket) 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 if (socket instanceof DatagramSocket) { 136 ((DatagramSocket) socket).setOption(option, null); 137 } else { 138 throw new RuntimeException("Unsupported socket type"); 139 } 140 } 141 }