< prev index next >
test/jdk/java/net/SocketOption/OptionsTest.java
Print this page
@@ -21,14 +21,15 @@
* questions.
*/
/*
* @test
- * @bug 8036979 8072384 8044773
+ * @bug 8036979 8072384 8044773 8225214
* @library /test/lib
* @requires !vm.graal.enabled
* @run main/othervm -Xcheck:jni OptionsTest
+ * @run main/othervm -Djdk.net.usePlainSocketImpl OptionsTest
* @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true OptionsTest
* @run main/othervm --limit-modules=java.base OptionsTest
*/
import java.lang.reflect.Method;
@@ -50,19 +51,21 @@
}
}
// The tests set the option using the new API, read back the set value
- // which could be diferent, and then use the legacy get API to check
+ // which could be different, and then use the legacy get API to check
// these values are the same
static Test<?>[] socketTests = new Test<?>[] {
Test.create(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE),
Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
+ Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(-1)),
+ Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(0)),
Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(80)),
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(0)), // lower-bound
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100)),
Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(255)) //upper-bound
};
@@ -232,11 +235,11 @@
} else if (option.equals(StandardSocketOptions.IP_TOS)) {
return Integer.valueOf(socket.getTrafficClass());
} else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
return Boolean.valueOf(socket.getTcpNoDelay());
} else {
- throw new RuntimeException("unexecpted socket option");
+ throw new RuntimeException("unexpected socket option");
}
} else if (type.equals(ServerSocket.class)) {
ServerSocket socket = (ServerSocket)s;
Set<SocketOption<?>> options = socket.supportedOptions();
boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
@@ -248,11 +251,11 @@
} else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
} else if (option.equals(StandardSocketOptions.IP_TOS)) {
return getServerSocketTrafficClass(socket);
} else {
- throw new RuntimeException("unexecpted socket option");
+ throw new RuntimeException("unexpected socket option");
}
} else if (type.equals(DatagramSocket.class)) {
DatagramSocket socket = (DatagramSocket)s;
Set<SocketOption<?>> options = socket.supportedOptions();
boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
@@ -266,11 +269,11 @@
} else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
} else if (option.equals(StandardSocketOptions.IP_TOS)) {
return Integer.valueOf(socket.getTrafficClass());
} else {
- throw new RuntimeException("unexecpted socket option");
+ throw new RuntimeException("unexpected socket option");
}
} else if (type.equals(MulticastSocket.class)) {
MulticastSocket socket = (MulticastSocket)s;
Set<SocketOption<?>> options = socket.supportedOptions();
@@ -291,22 +294,39 @@
} else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) {
return Integer.valueOf(socket.getTimeToLive());
} else if (option.equals(StandardSocketOptions.IP_MULTICAST_LOOP)) {
return Boolean.valueOf(socket.getLoopbackMode());
} else {
- throw new RuntimeException("unexecpted socket option");
+ throw new RuntimeException("unexpected socket option");
+ }
+ }
+ throw new RuntimeException("unexpected socket type");
+ }
+
+ // Tests default and negative values of SO_LINGER. All negative values should
+ // retrieve as -1.
+ public static void soLinger() throws Exception {
+ try (Socket s = new Socket()) {
+ // retrieve without set
+ int ignoreValue = s.getOption(StandardSocketOptions.SO_LINGER);
+
+ for (int v : List.of(-1, -2, -100, -65534, -65535, -65536, -100000)) {
+ System.out.println("Testing SO_LINGER with:" + v);
+ s.setOption(StandardSocketOptions.SO_LINGER, v);
+ int value = s.getOption(StandardSocketOptions.SO_LINGER);
+ testEqual(StandardSocketOptions.SO_LINGER, -1, value);
}
}
- throw new RuntimeException("unexecpted socket type");
}
public static void main(String args[]) throws Exception {
IPSupport.throwSkippedExceptionIfNonOperational();
doSocketTests();
doServerSocketTests();
doDatagramSocketTests();
doMulticastSocketTests();
+ soLinger();
}
// Reflectively access jdk.net.Sockets.getOption so that the test can run
// without the jdk.net module.
static Object getServerSocketTrafficClass(ServerSocket ss) throws Exception {
< prev index next >