test/jdk/java/net/SocketOption/OptionsTest.java
changeset 55226 ce8bab2c4185
parent 55102 59567035d279
child 58679 9c3209ff7550
child 59206 61091a42f19d
equal deleted inserted replaced
55225:ae3ef834deed 55226:ce8bab2c4185
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 8036979 8072384 8044773
    26  * @bug 8036979 8072384 8044773 8225214
    27  * @library /test/lib
    27  * @library /test/lib
    28  * @requires !vm.graal.enabled
    28  * @requires !vm.graal.enabled
    29  * @run main/othervm -Xcheck:jni OptionsTest
    29  * @run main/othervm -Xcheck:jni OptionsTest
       
    30  * @run main/othervm -Djdk.net.usePlainSocketImpl OptionsTest
    30  * @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true OptionsTest
    31  * @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true OptionsTest
    31  * @run main/othervm --limit-modules=java.base OptionsTest
    32  * @run main/othervm --limit-modules=java.base OptionsTest
    32  */
    33  */
    33 
    34 
    34 import java.lang.reflect.Method;
    35 import java.lang.reflect.Method;
    50         }
    51         }
    51 
    52 
    52     }
    53     }
    53 
    54 
    54     // The tests set the option using the new API, read back the set value
    55     // The tests set the option using the new API, read back the set value
    55     // which could be diferent, and then use the legacy get API to check
    56     // which could be different, and then use the legacy get API to check
    56     // these values are the same
    57     // these values are the same
    57 
    58 
    58     static Test<?>[] socketTests = new Test<?>[] {
    59     static Test<?>[] socketTests = new Test<?>[] {
    59         Test.create(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE),
    60         Test.create(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE),
    60         Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
    61         Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
    61         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
    62         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
    62         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
    63         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
    63         Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
    64         Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
       
    65         Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(-1)),
       
    66         Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(0)),
    64         Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(80)),
    67         Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(80)),
    65         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(0)),  // lower-bound
    68         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(0)),  // lower-bound
    66         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)),
    69         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)),
    67         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(255))  //upper-bound
    70         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(255))  //upper-bound
    68     };
    71     };
   147         SocketOption<T> option = test.option;
   150         SocketOption<T> option = test.option;
   148         ds.setOption(option, test.value);
   151         ds.setOption(option, test.value);
   149         T value1 = ds.getOption(test.option);
   152         T value1 = ds.getOption(test.option);
   150         T value2 = (T) legacyGetOption(ds.getClass(), ds, test.option);
   153         T value2 = (T) legacyGetOption(ds.getClass(), ds, test.option);
   151         testEqual(option, value1, value2);
   154         testEqual(option, value1, value2);
       
   155     }
       
   156 
       
   157     // Tests default and negative values of SO_LINGER. All negative values should
       
   158     // retrieve as -1.
       
   159     static void testSoLingerValues() throws Exception {
       
   160         try (Socket s = new Socket()) {
       
   161             // retrieve without set
       
   162             int defaultValue = s.getOption(StandardSocketOptions.SO_LINGER);
       
   163             testEqual(StandardSocketOptions.SO_LINGER, -1, defaultValue);
       
   164 
       
   165             for (int v : List.of(-1, -2, -100, -65534, -65535, -65536, -100000)) {
       
   166                 System.out.println("Testing SO_LINGER with:" + v);
       
   167                 s.setOption(StandardSocketOptions.SO_LINGER, v);
       
   168                 int value = s.getOption(StandardSocketOptions.SO_LINGER);
       
   169                 testEqual(StandardSocketOptions.SO_LINGER, -1, value);
       
   170             }
       
   171         }
   152     }
   172     }
   153 
   173 
   154     @SuppressWarnings("try")
   174     @SuppressWarnings("try")
   155     static void doSocketTests() throws Exception {
   175     static void doSocketTests() throws Exception {
   156         // unconnected socket
   176         // unconnected socket
   175                         }
   195                         }
   176                     }
   196                     }
   177                 }
   197                 }
   178             }
   198             }
   179         }
   199         }
       
   200 
       
   201         testSoLingerValues();
   180     }
   202     }
   181 
   203 
   182     static void doServerSocketTests() throws Exception {
   204     static void doServerSocketTests() throws Exception {
   183         try (ServerSocket ss = new ServerSocket(0)) {
   205         try (ServerSocket ss = new ServerSocket(0)) {
   184             Set<SocketOption<?>> options = ss.supportedOptions();
   206             Set<SocketOption<?>> options = ss.supportedOptions();
   232             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   254             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   233                 return Integer.valueOf(socket.getTrafficClass());
   255                 return Integer.valueOf(socket.getTrafficClass());
   234             } else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
   256             } else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
   235                 return Boolean.valueOf(socket.getTcpNoDelay());
   257                 return Boolean.valueOf(socket.getTcpNoDelay());
   236             } else {
   258             } else {
   237                 throw new RuntimeException("unexecpted socket option");
   259                 throw new RuntimeException("unexpected socket option");
   238             }
   260             }
   239         } else if  (type.equals(ServerSocket.class)) {
   261         } else if  (type.equals(ServerSocket.class)) {
   240             ServerSocket socket = (ServerSocket)s;
   262             ServerSocket socket = (ServerSocket)s;
   241             Set<SocketOption<?>> options = socket.supportedOptions();
   263             Set<SocketOption<?>> options = socket.supportedOptions();
   242             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
   264             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
   248             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
   270             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
   249                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
   271                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
   250             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   272             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   251                 return getServerSocketTrafficClass(socket);
   273                 return getServerSocketTrafficClass(socket);
   252             } else {
   274             } else {
   253                 throw new RuntimeException("unexecpted socket option");
   275                 throw new RuntimeException("unexpected socket option");
   254             }
   276             }
   255         } else if  (type.equals(DatagramSocket.class)) {
   277         } else if  (type.equals(DatagramSocket.class)) {
   256             DatagramSocket socket = (DatagramSocket)s;
   278             DatagramSocket socket = (DatagramSocket)s;
   257             Set<SocketOption<?>> options = socket.supportedOptions();
   279             Set<SocketOption<?>> options = socket.supportedOptions();
   258             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
   280             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
   266             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
   288             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
   267                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
   289                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
   268             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   290             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   269                 return Integer.valueOf(socket.getTrafficClass());
   291                 return Integer.valueOf(socket.getTrafficClass());
   270             } else {
   292             } else {
   271                 throw new RuntimeException("unexecpted socket option");
   293                 throw new RuntimeException("unexpected socket option");
   272             }
   294             }
   273 
   295 
   274         } else if  (type.equals(MulticastSocket.class)) {
   296         } else if  (type.equals(MulticastSocket.class)) {
   275             MulticastSocket socket = (MulticastSocket)s;
   297             MulticastSocket socket = (MulticastSocket)s;
   276             Set<SocketOption<?>> options = socket.supportedOptions();
   298             Set<SocketOption<?>> options = socket.supportedOptions();
   291             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) {
   313             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) {
   292                 return Integer.valueOf(socket.getTimeToLive());
   314                 return Integer.valueOf(socket.getTimeToLive());
   293             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_LOOP)) {
   315             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_LOOP)) {
   294                 return Boolean.valueOf(socket.getLoopbackMode());
   316                 return Boolean.valueOf(socket.getLoopbackMode());
   295             } else {
   317             } else {
   296                 throw new RuntimeException("unexecpted socket option");
   318                 throw new RuntimeException("unexpected socket option");
   297             }
   319             }
   298         }
   320         }
   299         throw new RuntimeException("unexecpted socket type");
   321         throw new RuntimeException("unexpected socket type");
   300     }
   322     }
   301 
   323 
   302     public static void main(String args[]) throws Exception {
   324     public static void main(String args[]) throws Exception {
   303         IPSupport.throwSkippedExceptionIfNonOperational();
   325         IPSupport.throwSkippedExceptionIfNonOperational();
   304         doSocketTests();
   326         doSocketTests();