1 /* 2 * Copyright (c) 2016, 2017, 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 30 /* 31 * @test 32 * @bug 8143554 8044773 33 * @summary Test checks that UnsupportedOperationException for unsupported 34 * SOCKET_OPTIONS is thrown by both getOption() and setOption() methods. 35 * @run main UnsupportedOptionsTest 36 * @run main/othervm --limit-modules=java.base UnsupportedOptionsTest 37 */ 38 39 public class UnsupportedOptionsTest { 40 41 private static final List<SocketOption<?>> socketOptions = new ArrayList<>(); 42 43 static { 44 socketOptions.add(StandardSocketOptions.IP_MULTICAST_IF); 45 socketOptions.add(StandardSocketOptions.IP_MULTICAST_LOOP); 46 socketOptions.add(StandardSocketOptions.IP_MULTICAST_TTL); 47 socketOptions.add(StandardSocketOptions.IP_TOS); 48 socketOptions.add(StandardSocketOptions.SO_BROADCAST); 49 socketOptions.add(StandardSocketOptions.SO_KEEPALIVE); 50 socketOptions.add(StandardSocketOptions.SO_LINGER); 51 socketOptions.add(StandardSocketOptions.SO_RCVBUF); 52 socketOptions.add(StandardSocketOptions.SO_REUSEADDR); 53 socketOptions.add(StandardSocketOptions.SO_SNDBUF); 54 socketOptions.add(StandardSocketOptions.TCP_NODELAY); 55 56 try { 57 Class<?> c = Class.forName("jdk.net.ExtendedSocketOptions"); 58 Field field = c.getField("SO_FLOW_SLA"); 59 socketOptions.add((SocketOption<?>)field.get(null)); 60 field = c.getField("TCP_QUICKACK"); 61 socketOptions.add((SocketOption<?>)field.get(null)); 62 } catch (ClassNotFoundException e) { 63 // ignore, jdk.net module not present 64 } catch (ReflectiveOperationException e) { 65 throw new AssertionError(e); 66 } 67 } 68 69 public static void main(String[] args) throws IOException { 70 Socket s = new Socket(); 71 ServerSocket ss = new ServerSocket(); 72 DatagramSocket ds = new DatagramSocket(); 73 MulticastSocket ms = new MulticastSocket(); 74 75 for (SocketOption option : socketOptions) { 76 if (!s.supportedOptions().contains(option)) { 77 testUnsupportedSocketOption(s, option); 78 } 79 80 if (!ss.supportedOptions().contains(option)) { 81 testUnsupportedSocketOption(ss, option); 82 } 83 84 if (!ms.supportedOptions().contains(option)) { 85 testUnsupportedSocketOption(ms, option); 86 } 87 88 if (!ds.supportedOptions().contains(option)) { 89 testUnsupportedSocketOption(ds, option); 90 } 91 } 92 } 93 94 /* 95 * Check that UnsupportedOperationException for unsupported option is 96 * thrown from both getOption() and setOption() methods. 97 */ 98 private static void testUnsupportedSocketOption(Object socket, 99 SocketOption option) { 100 testSet(socket, option); 101 testGet(socket, option); 102 } 103 104 private static void testSet(Object socket, SocketOption option) { 105 try { 106 setOption(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 testGet(Object socket, SocketOption option) { 119 try { 120 getOption(socket, option); 121 } catch (UnsupportedOperationException e) { 122 System.out.println("UnsupportedOperationException was throw " + 123 "as expected. Socket: " + socket + " Option: " + option); 124 return; 125 } catch (Exception e) { 126 throw new RuntimeException("FAIL. Unexpected exception.", e); 127 } 128 throw new RuntimeException("FAIL. UnsupportedOperationException " + 129 "hasn't been thrown. Socket: " + socket + " Option: " + option); 130 } 131 132 private static void getOption(Object socket, 133 SocketOption option) throws IOException { 134 if (socket instanceof Socket) { 135 ((Socket) socket).getOption(option); 136 } else if (socket instanceof ServerSocket) { 137 ((ServerSocket) socket).getOption(option); 138 } else if (socket instanceof DatagramSocket) { 139 ((DatagramSocket) socket).getOption(option); 140 } else { 141 throw new RuntimeException("Unsupported socket type"); 142 } 143 } 144 145 private static void setOption(Object socket, 146 SocketOption option) throws IOException { 147 if (socket instanceof Socket) { 148 ((Socket) socket).setOption(option, null); 149 } else if (socket instanceof ServerSocket) { 150 ((ServerSocket) socket).setOption(option, null); 151 } else if (socket instanceof DatagramSocket) { 152 ((DatagramSocket) socket).setOption(option, null); 153 } else { 154 throw new RuntimeException("Unsupported socket type"); 155 } 156 } 157 }