1 /* 2 * Copyright (c) 2014, 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 /* 25 * @test 26 * @bug 8036979 8072384 27 * @run main/othervm -Xcheck:jni OptionsTest 28 * @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true OptionsTest 29 */ 30 31 import java.net.*; 32 import java.util.*; 33 34 public class OptionsTest { 35 36 static class Test { 37 Test(SocketOption<?> option, Object testValue) { 38 this.option = option; 39 this.testValue = testValue; 40 } 41 static Test create (SocketOption<?> option, Object testValue) { 42 return new Test(option, testValue); 43 } 44 Object option; 45 Object testValue; 46 }; 47 48 // The tests set the option using the new API, read back the set value 49 // which could be diferent, and then use the legacy get API to check 50 // these values are the same 51 52 static Test[] socketTests = new Test[] { 53 Test.create(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE), 54 Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)), 55 Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)), 56 Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE), 57 Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(80)), 58 Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)) 59 }; 60 61 static Test[] serverSocketTests = new Test[] { 62 Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)), 63 Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE), 64 Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)) 65 }; 66 67 static Test[] dgSocketTests = new Test[] { 68 Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)), 69 Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)), 70 Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE), 71 Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)) 72 }; 73 74 static Test[] mcSocketTests = new Test[] { 75 Test.create(StandardSocketOptions.IP_MULTICAST_IF, getNetworkInterface()), 76 Test.create(StandardSocketOptions.IP_MULTICAST_TTL, Integer.valueOf(10)), 77 Test.create(StandardSocketOptions.IP_MULTICAST_LOOP, Boolean.TRUE) 78 }; 79 80 static NetworkInterface getNetworkInterface() { 81 try { 82 Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces(); 83 while (nifs.hasMoreElements()) { 84 NetworkInterface ni = (NetworkInterface)nifs.nextElement(); 85 if (ni.supportsMulticast()) { 86 return ni; 87 } 88 } 89 } catch (Exception e) { 90 } 91 return null; 92 } 93 94 static void doSocketTests() throws Exception { 95 try ( 96 ServerSocket srv = new ServerSocket(0); 97 Socket c = new Socket("127.0.0.1", srv.getLocalPort()); 98 Socket s = srv.accept(); 99 ) { 100 for (int i=0; i<socketTests.length; i++) { 101 Test test = socketTests[i]; 102 c.setOption((SocketOption)test.option, test.testValue); 103 Object getval = c.getOption((SocketOption)test.option); 104 Object legacyget = legacyGetOption(Socket.class, c,test.option); 105 if (!getval.equals(legacyget)) { 106 Formatter f = new Formatter(); 107 f.format("S Err %d: %s/%s", i, getval, legacyget); 108 throw new RuntimeException(f.toString()); 109 } 110 } 111 } 112 } 113 114 static void doDgSocketTests() throws Exception { 115 try ( 116 DatagramSocket c = new DatagramSocket(0); 117 ) { 118 for (int i=0; i<dgSocketTests.length; i++) { 119 Test test = dgSocketTests[i]; 120 c.setOption((SocketOption)test.option, test.testValue); 121 Object getval = c.getOption((SocketOption)test.option); 122 Object legacyget = legacyGetOption(DatagramSocket.class, c,test.option); 123 if (!getval.equals(legacyget)) { 124 Formatter f = new Formatter(); 125 f.format("DG Err %d: %s/%s", i, getval, legacyget); 126 throw new RuntimeException(f.toString()); 127 } 128 } 129 } 130 } 131 132 static void doMcSocketTests() throws Exception { 133 try ( 134 MulticastSocket c = new MulticastSocket(0); 135 ) { 136 for (int i=0; i<mcSocketTests.length; i++) { 137 Test test = mcSocketTests[i]; 138 c.setOption((SocketOption)test.option, test.testValue); 139 Object getval = c.getOption((SocketOption)test.option); 140 Object legacyget = legacyGetOption(MulticastSocket.class, c,test.option); 141 if (!getval.equals(legacyget)) { 142 Formatter f = new Formatter(); 143 f.format("MC Err %d: %s/%s", i, getval, legacyget); 144 throw new RuntimeException(f.toString()); 145 } 146 } 147 } 148 } 149 150 static void doServerSocketTests() throws Exception { 151 try ( 152 ServerSocket c = new ServerSocket(0); 153 ) { 154 for (int i=0; i<serverSocketTests.length; i++) { 155 Test test = serverSocketTests[i]; 156 c.setOption((SocketOption)test.option, test.testValue); 157 Object getval = c.getOption((SocketOption)test.option); 158 Object legacyget = legacyGetOption( 159 ServerSocket.class, c, test.option 160 ); 161 if (!getval.equals(legacyget)) { 162 Formatter f = new Formatter(); 163 f.format("SS Err %d: %s/%s", i, getval, legacyget); 164 throw new RuntimeException(f.toString()); 165 } 166 } 167 } 168 } 169 170 static Object legacyGetOption( 171 Class<?> type, Object s, Object option) 172 173 throws Exception 174 { 175 if (type.equals(Socket.class)) { 176 Socket socket = (Socket)s; 177 178 if (option.equals(StandardSocketOptions.SO_KEEPALIVE)) { 179 return Boolean.valueOf(socket.getKeepAlive()); 180 } else if (option.equals(StandardSocketOptions.SO_SNDBUF)) { 181 return Integer.valueOf(socket.getSendBufferSize()); 182 } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) { 183 return Integer.valueOf(socket.getReceiveBufferSize()); 184 } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) { 185 return Boolean.valueOf(socket.getReuseAddress()); 186 } else if (option.equals(StandardSocketOptions.SO_LINGER)) { 187 return Integer.valueOf(socket.getSoLinger()); 188 } else if (option.equals(StandardSocketOptions.IP_TOS)) { 189 return Integer.valueOf(socket.getTrafficClass()); 190 } else if (option.equals(StandardSocketOptions.TCP_NODELAY)) { 191 return Boolean.valueOf(socket.getTcpNoDelay()); 192 } else { 193 throw new RuntimeException("unexecpted socket option"); 194 } 195 } else if (type.equals(ServerSocket.class)) { 196 ServerSocket socket = (ServerSocket)s; 197 if (option.equals(StandardSocketOptions.SO_RCVBUF)) { 198 return Integer.valueOf(socket.getReceiveBufferSize()); 199 } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) { 200 return Boolean.valueOf(socket.getReuseAddress()); 201 } else if (option.equals(StandardSocketOptions.IP_TOS)) { 202 return Integer.valueOf(jdk.net.Sockets.getOption( 203 socket, StandardSocketOptions.IP_TOS)); 204 } else { 205 throw new RuntimeException("unexecpted socket option"); 206 } 207 } else if (type.equals(DatagramSocket.class)) { 208 DatagramSocket socket = (DatagramSocket)s; 209 210 if (option.equals(StandardSocketOptions.SO_SNDBUF)) { 211 return Integer.valueOf(socket.getSendBufferSize()); 212 } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) { 213 return Integer.valueOf(socket.getReceiveBufferSize()); 214 } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) { 215 return Boolean.valueOf(socket.getReuseAddress()); 216 } else if (option.equals(StandardSocketOptions.IP_TOS)) { 217 return Integer.valueOf(socket.getTrafficClass()); 218 } else { 219 throw new RuntimeException("unexecpted socket option"); 220 } 221 222 } else if (type.equals(MulticastSocket.class)) { 223 MulticastSocket socket = (MulticastSocket)s; 224 225 if (option.equals(StandardSocketOptions.SO_SNDBUF)) { 226 return Integer.valueOf(socket.getSendBufferSize()); 227 } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) { 228 return Integer.valueOf(socket.getReceiveBufferSize()); 229 } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) { 230 return Boolean.valueOf(socket.getReuseAddress()); 231 } else if (option.equals(StandardSocketOptions.IP_TOS)) { 232 return Integer.valueOf(socket.getTrafficClass()); 233 } else if (option.equals(StandardSocketOptions.IP_MULTICAST_IF)) { 234 return socket.getNetworkInterface(); 235 } else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) { 236 return Integer.valueOf(socket.getTimeToLive()); 237 } else if (option.equals(StandardSocketOptions.IP_MULTICAST_LOOP)) { 238 return Boolean.valueOf(socket.getLoopbackMode()); 239 } else { 240 throw new RuntimeException("unexecpted socket option"); 241 } 242 } 243 throw new RuntimeException("unexecpted socket type"); 244 } 245 246 public static void main(String args[]) throws Exception { 247 doSocketTests(); 248 doServerSocketTests(); 249 doDgSocketTests(); 250 doMcSocketTests(); 251 } 252 }