test/java/net/SocketOption/OptionsTest.java

Print this page
rev 14282 : 8044773: Refactor jdk.net API so that it can be moved out of the base module
Reviewed-by:


   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_REUSEPORT, Boolean.FALSE),
  58         Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(80)),
  59         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100))
  60     };
  61 
  62     static Test[] serverSocketTests = new Test[] {
  63         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
  64         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
  65         Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
  66         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100))


 206                 return Integer.valueOf(socket.getSoLinger());
 207             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
 208                 return Integer.valueOf(socket.getTrafficClass());
 209             } else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
 210                 return Boolean.valueOf(socket.getTcpNoDelay());
 211             } else {
 212                 throw new RuntimeException("unexecpted socket option");
 213             }
 214         } else if  (type.equals(ServerSocket.class)) {
 215             ServerSocket socket = (ServerSocket)s;
 216             Set<SocketOption<?>> options = socket.supportedOptions();
 217             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
 218 
 219             if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
 220                 return Integer.valueOf(socket.getReceiveBufferSize());
 221             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
 222                 return Boolean.valueOf(socket.getReuseAddress());
 223             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
 224                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
 225             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
 226                 return Integer.valueOf(jdk.net.Sockets.getOption(
 227                     socket, StandardSocketOptions.IP_TOS));
 228             } else {
 229                 throw new RuntimeException("unexecpted socket option");
 230             }
 231         } else if  (type.equals(DatagramSocket.class)) {
 232             DatagramSocket socket = (DatagramSocket)s;
 233             Set<SocketOption<?>> options = socket.supportedOptions();
 234             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
 235 
 236             if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
 237                 return Integer.valueOf(socket.getSendBufferSize());
 238             } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
 239                 return Integer.valueOf(socket.getReceiveBufferSize());
 240             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
 241                 return Boolean.valueOf(socket.getReuseAddress());
 242             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
 243                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
 244             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
 245                 return Integer.valueOf(socket.getTrafficClass());
 246             } else {
 247                 throw new RuntimeException("unexecpted socket option");


 263             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
 264                 return Integer.valueOf(socket.getTrafficClass());
 265             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_IF)) {
 266                 return socket.getNetworkInterface();
 267             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) {
 268                 return Integer.valueOf(socket.getTimeToLive());
 269             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_LOOP)) {
 270                 return Boolean.valueOf(socket.getLoopbackMode());
 271             } else {
 272                 throw new RuntimeException("unexecpted socket option");
 273             }
 274         }
 275         throw new RuntimeException("unexecpted socket type");
 276     }
 277 
 278     public static void main(String args[]) throws Exception {
 279         doSocketTests();
 280         doServerSocketTests();
 281         doDgSocketTests();
 282         doMcSocketTests();
















 283     }
 284 }


   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 8044773
  27  * @run main/othervm -Xcheck:jni OptionsTest
  28  * @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true OptionsTest
  29  * @run main/othervm -Djdk.launcher.limitmods=java.base OptionsTest
  30  */
  31 
  32 import java.lang.reflect.Method;
  33 import java.net.*;
  34 import java.util.*;
  35 
  36 public class OptionsTest {
  37 
  38     static class Test {
  39         Test(SocketOption<?> option, Object testValue) {
  40             this.option = option;
  41             this.testValue = testValue;
  42         }
  43         static Test create (SocketOption<?> option, Object testValue) {
  44             return new Test(option, testValue);
  45         }
  46         Object option;
  47         Object testValue;
  48     }
  49 
  50     // The tests set the option using the new API, read back the set value
  51     // which could be diferent, and then use the legacy get API to check
  52     // these values are the same
  53 
  54     static Test[] socketTests = new Test[] {
  55         Test.create(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE),
  56         Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
  57         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
  58         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
  59         Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
  60         Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(80)),
  61         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100))
  62     };
  63 
  64     static Test[] serverSocketTests = new Test[] {
  65         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
  66         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
  67         Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
  68         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100))


 208                 return Integer.valueOf(socket.getSoLinger());
 209             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
 210                 return Integer.valueOf(socket.getTrafficClass());
 211             } else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
 212                 return Boolean.valueOf(socket.getTcpNoDelay());
 213             } else {
 214                 throw new RuntimeException("unexecpted socket option");
 215             }
 216         } else if  (type.equals(ServerSocket.class)) {
 217             ServerSocket socket = (ServerSocket)s;
 218             Set<SocketOption<?>> options = socket.supportedOptions();
 219             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
 220 
 221             if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
 222                 return Integer.valueOf(socket.getReceiveBufferSize());
 223             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
 224                 return Boolean.valueOf(socket.getReuseAddress());
 225             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
 226                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
 227             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
 228                 return getServerSocketTrafficClass(socket);

 229             } else {
 230                 throw new RuntimeException("unexecpted socket option");
 231             }
 232         } else if  (type.equals(DatagramSocket.class)) {
 233             DatagramSocket socket = (DatagramSocket)s;
 234             Set<SocketOption<?>> options = socket.supportedOptions();
 235             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
 236 
 237             if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
 238                 return Integer.valueOf(socket.getSendBufferSize());
 239             } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
 240                 return Integer.valueOf(socket.getReceiveBufferSize());
 241             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
 242                 return Boolean.valueOf(socket.getReuseAddress());
 243             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
 244                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
 245             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
 246                 return Integer.valueOf(socket.getTrafficClass());
 247             } else {
 248                 throw new RuntimeException("unexecpted socket option");


 264             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
 265                 return Integer.valueOf(socket.getTrafficClass());
 266             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_IF)) {
 267                 return socket.getNetworkInterface();
 268             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) {
 269                 return Integer.valueOf(socket.getTimeToLive());
 270             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_LOOP)) {
 271                 return Boolean.valueOf(socket.getLoopbackMode());
 272             } else {
 273                 throw new RuntimeException("unexecpted socket option");
 274             }
 275         }
 276         throw new RuntimeException("unexecpted socket type");
 277     }
 278 
 279     public static void main(String args[]) throws Exception {
 280         doSocketTests();
 281         doServerSocketTests();
 282         doDgSocketTests();
 283         doMcSocketTests();
 284     }
 285 
 286     // Reflectively access jdk.net.Sockets.getOption so that the test can run
 287     // without the jdk.net module.
 288     static Object getServerSocketTrafficClass(ServerSocket ss) throws Exception {
 289         try {
 290             Class<?> c = Class.forName("jdk.net.Sockets");
 291             Method m = c.getDeclaredMethod("getOption", ServerSocket.class, SocketOption.class);
 292             return m.invoke(null, ss, StandardSocketOptions.IP_TOS);
 293         } catch (ClassNotFoundException e) {
 294             // Ok, jdk.net module not present, just fall back
 295             System.out.println("jdk.net module not present, falling back.");
 296             return Integer.valueOf(ss.getOption(StandardSocketOptions.IP_TOS));
 297         } catch (ReflectiveOperationException e) {
 298             throw new AssertionError(e);
 299         }
 300     }
 301 }