--- old/test/jdk/net/Sockets/Test.java 2015-11-18 17:26:33.836896522 -0800 +++ new/test/jdk/net/Sockets/Test.java 2015-11-18 17:26:33.729896521 -0800 @@ -40,11 +40,34 @@ 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 @@ -69,59 +92,203 @@ .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(); + boolean flowsupported = true; + boolean reuseportsupported = true; // If option not available, end test + DatagramSocket dg = new DatagramSocket(0); Set> options = dg.supportedOptions(); if (!options.contains(ExtendedSocketOptions.SO_FLOW_SLA)) { System.out.println("SO_FLOW_SLA not supported"); - return; + flowsupported = false; + } + if (!options.contains(ExtendedSocketOptions.SO_REUSEPORT)) { + System.out.println("SO_REUSEPORT not supported"); + reuseportsupported = false; } - 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(()->{ - Sockets.setOption(s, ExtendedSocketOptions.SO_FLOW_SLA, flowIn); - }); - doTest(()->{ - Sockets.getOption(s, ExtendedSocketOptions.SO_FLOW_SLA); - }); - doTest(()->{ - sc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn); - }); - doTest(()->{ - sc.getOption(ExtendedSocketOptions.SO_FLOW_SLA); - }); - doTest(()->{ - DatagramSocket dg1 = new DatagramSocket(0); - dg1.connect(loop, udp_port); - Sockets.setOption(dg1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn); - }); - doTest(()->{ - DatagramChannel dg2 = DatagramChannel.open(); - dg2.bind(new InetSocketAddress(loop, 0)); - dg2.connect(new InetSocketAddress(loop, udp_port)); - dg2.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn); - }); - doTest(()->{ - MulticastSocket mc1 = new MulticastSocket(0); - mc1.connect(loop, udp_port); - Sockets.setOption(mc1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn); - }); - doTest(()->{ - AsynchronousSocketChannel asc = AsynchronousSocketChannel.open(); - Future f = asc.connect(loopad); - f.get(); - asc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn); - }); + 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(()->{ + Sockets.setOption(s, ExtendedSocketOptions.SO_FLOW_SLA, flowIn); + }); + doTest(()->{ + Sockets.getOption(s, ExtendedSocketOptions.SO_FLOW_SLA); + }); + doTest(()->{ + sc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn); + }); + doTest(()->{ + sc.getOption(ExtendedSocketOptions.SO_FLOW_SLA); + }); + doTest(()->{ + DatagramSocket dg1 = new DatagramSocket(0); + dg1.connect(loop, udp_port); + Sockets.setOption(dg1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn); + }); + doTest(()->{ + DatagramChannel dg2 = DatagramChannel.open(); + dg2.bind(new InetSocketAddress(loop, 0)); + dg2.connect(new InetSocketAddress(loop, udp_port)); + dg2.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn); + }); + doTest(()->{ + MulticastSocket mc1 = new MulticastSocket(0); + mc1.connect(loop, udp_port); + Sockets.setOption(mc1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn); + }); + doTest(()->{ + AsynchronousSocketChannel asc = AsynchronousSocketChannel.open(); + Future 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 {