< prev index next >
test/jdk/net/Sockets/Test.java
Print this page
*** 38,52 ****
--- 38,75 ----
public class Test {
static boolean security;
static boolean success;
+ static int testCount = 0;
interface Runner {
public void run() throws Exception;
}
+ static void test(String msg) {
+ testCount++;
+ System.out.println("***************************************");
+ System.out.println("Test " + testCount + ": " + msg);
+ }
+
+ static void passed() {
+ System.out.println("Test passed.");
+ }
+
+ static void failed() {
+ System.out.println("Test failed.");
+ }
+
+ static void check(boolean pass) {
+ if (pass) {
+ passed();
+ } else {
+ failed();
+ }
+ }
+
public static void main(String[] args) throws Exception {
// quick check to see if supportedOptions() working before
// creating any sockets and libnet loaded
*** 67,91 ****
final SocketFlow flowIn = SocketFlow.create()
.bandwidth(1000)
.priority(SocketFlow.HIGH_PRIORITY);
! ServerSocket ss = new ServerSocket(0);
! int tcp_port = ss.getLocalPort();
! final InetAddress loop = InetAddress.getByName("127.0.0.1");
! final InetSocketAddress loopad = new InetSocketAddress(loop, tcp_port);
!
! DatagramSocket dg = new DatagramSocket(0);
! final int udp_port = dg.getLocalPort();
// If option not available, end test
Set<SocketOption<?>> options = dg.supportedOptions();
if (!options.contains(ExtendedSocketOptions.SO_FLOW_SLA)) {
System.out.println("SO_FLOW_SLA not supported");
! return;
}
final Socket s = new Socket("127.0.0.1", tcp_port);
final SocketChannel sc = SocketChannel.open();
sc.connect (new InetSocketAddress("127.0.0.1", tcp_port));
doTest(()->{
--- 90,122 ----
final SocketFlow flowIn = SocketFlow.create()
.bandwidth(1000)
.priority(SocketFlow.HIGH_PRIORITY);
! boolean flowsupported = true;
! boolean reuseportsupported = true;
// If option not available, end test
+ DatagramSocket dg = new DatagramSocket(0);
Set<SocketOption<?>> options = dg.supportedOptions();
if (!options.contains(ExtendedSocketOptions.SO_FLOW_SLA)) {
System.out.println("SO_FLOW_SLA not supported");
! flowsupported = false;
! }
! if (!options.contains(ExtendedSocketOptions.SO_REUSEPORT)) {
! System.out.println("SO_REUSEPORT not supported");
! reuseportsupported = false;
}
+ if (flowsupported) {
+ ServerSocket ss = new ServerSocket(0);
+ int tcp_port = ss.getLocalPort();
+ final InetAddress loop = InetAddress.getByName("127.0.0.1");
+ final InetSocketAddress loopad = new InetSocketAddress(loop, tcp_port);
+
+ final int udp_port = dg.getLocalPort();
+
final Socket s = new Socket("127.0.0.1", tcp_port);
final SocketChannel sc = SocketChannel.open();
sc.connect (new InetSocketAddress("127.0.0.1", tcp_port));
doTest(()->{
*** 121,130 ****
--- 152,297 ----
Future<Void> f = asc.connect(loopad);
f.get();
asc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
});
}
+ if (reuseportsupported) {
+ Socket s1 = new Socket();
+ Socket s2 = new Socket();
+ test("Socket should be created with SO_REUSEPORT disabled");
+ check(!Sockets.getOption(s1, ExtendedSocketOptions.SO_REUSEPORT));
+
+ test("Socket.set ReusePort(true)");
+ Sockets.setOption(s1, ExtendedSocketOptions.SO_REUSEPORT, true);
+ check(Sockets.getOption(s1, ExtendedSocketOptions.SO_REUSEPORT));
+
+ test("Socket.set ReusePort(false)");
+ Sockets.setOption(s1, ExtendedSocketOptions.SO_REUSEPORT, false);
+ check(!Sockets.getOption(s1, ExtendedSocketOptions.SO_REUSEPORT));
+
+ test("Without setting SO_REUSEPORT, binding Socket to port already in use should throw a SocketException");
+ s1.bind(new InetSocketAddress(0));
+ try {
+ s2.bind( new InetSocketAddress(s1.getLocalPort()));
+ failed();
+ } catch (SocketException e) {
+ passed();
+ }
+ s1.close();
+ s2.close();
+
+ test("By setting SO_REUSEPORT, binding Socket to port already in use should not throw a SocketException");
+ s1 = new Socket();
+ s2 = new Socket();
+ Sockets.setOption(s1, ExtendedSocketOptions.SO_REUSEPORT, true);
+ Sockets.setOption(s2, ExtendedSocketOptions.SO_REUSEPORT, true);
+ s1.bind(new InetSocketAddress(0));
+ try {
+ s2.bind( new InetSocketAddress(s1.getLocalPort()) );
+ passed();
+ } catch (SocketException e) {
+ failed();
+ }
+ s1.close();
+ s2.close();
+
+ ServerSocket ss1 = new ServerSocket();
+ ServerSocket ss2 = new ServerSocket();
+
+ test("Server Socket should be created with SO_REUSEPORT disabled");
+ check(!Sockets.getOption(ss1, ExtendedSocketOptions.SO_REUSEPORT));
+
+ test("ServerSocket set ReusePort(true)");
+ Sockets.setOption(ss1, ExtendedSocketOptions.SO_REUSEPORT, true);
+ check(Sockets.getOption(ss1, ExtendedSocketOptions.SO_REUSEPORT));
+
+ test("ServerSocket set ReusePort(false)");
+ Sockets.setOption(ss1, ExtendedSocketOptions.SO_REUSEPORT, false);
+ check(!Sockets.getOption(ss1, ExtendedSocketOptions.SO_REUSEPORT));
+
+ test("Without setting SO_REUSEPORT, binding Server Socket to port already in use should throw a SocketException");
+ ss1.bind(new InetSocketAddress(0));
+ try {
+ ss2.bind( new InetSocketAddress(ss1.getLocalPort()));
+ failed();
+ } catch (SocketException e) {
+ passed();
+ }
+ ss1.close();
+ ss2.close();
+
+ test("By setting SO_REUSEPORT, binding Server Socket to port already in use should not throw a SocketException");
+ ss1 = new ServerSocket();
+ ss2 = new ServerSocket();
+ Sockets.setOption(ss1, ExtendedSocketOptions.SO_REUSEPORT, true);
+ Sockets.setOption(ss2, ExtendedSocketOptions.SO_REUSEPORT, true);
+ ss1.bind(new InetSocketAddress(0));
+ try {
+ ss2.bind( new InetSocketAddress(ss1.getLocalPort()) );
+ passed();
+ } catch (SocketException e) {
+ failed();
+ }
+ ss1.close();
+ ss2.close();
+
+ DatagramSocket dg1 = new DatagramSocket(null);
+ DatagramSocket dg2 = new DatagramSocket(null);
+
+ test("DatagramSocket should be created with SO_REUSEPORT disabled");
+ check(!Sockets.getOption(dg1, ExtendedSocketOptions.SO_REUSEPORT));
+
+ test("DatagramSocket.setReusePort(true)");
+ Sockets.setOption(dg1, ExtendedSocketOptions.SO_REUSEPORT, true);
+ check(Sockets.getOption(dg1, ExtendedSocketOptions.SO_REUSEPORT));
+
+ test("DatagramSocket.setReusePort(false)");
+ Sockets.setOption(dg1, ExtendedSocketOptions.SO_REUSEPORT, false);
+ check(!Sockets.getOption(dg1, ExtendedSocketOptions.SO_REUSEPORT));
+
+ test("Without setting SO_REUSEPORT, binding Server Socket to port already in use should throw a SocketException");
+ dg1.bind(new InetSocketAddress(0));
+ try {
+ dg2.bind( new InetSocketAddress(dg1.getLocalPort()));
+ failed();
+ } catch (SocketException e) {
+ passed();
+ }
+ dg1.close();
+ dg2.close();
+
+ test("By setting SO_REUSEPORT, binding Server Socket to port already in use should not throw a SocketException");
+ dg1 = new DatagramSocket(null);
+ dg2 = new DatagramSocket(null);
+ Sockets.setOption(dg1, ExtendedSocketOptions.SO_REUSEPORT, true);
+ Sockets.setOption(dg2, ExtendedSocketOptions.SO_REUSEPORT, true);
+ dg1.bind(new InetSocketAddress(0));
+ try {
+ dg2.bind( new InetSocketAddress(dg1.getLocalPort()) );
+ passed();
+ } catch (SocketException e) {
+ failed();
+ }
+ dg1.close();
+ dg2.close();
+
+ MulticastSocket mc1 = new MulticastSocket();
+
+ test("Check SO_REUSEPORT is enabled in MulticastSocket");
+ check(Sockets.getOption(mc1, ExtendedSocketOptions.SO_REUSEPORT));
+ mc1.close();
+
+ test("Check SO_REUSEPORT is not disabled by MulticastSocket.bind()");
+ mc1 = new MulticastSocket(null);
+
+ InetSocketAddress isa = new InetSocketAddress(
+ InetAddress.getLocalHost(), 0);
+ mc1.bind(isa);
+ check(Sockets.getOption(mc1, ExtendedSocketOptions.SO_REUSEPORT));
+ mc1.close();
+ }
+ }
static void doTest(Runner func) throws Exception {
try {
func.run();
if (security && !success) {
< prev index next >