Path: blob/master/test/jdk/java/net/Socket/setReuseAddress/Basic.java
41153 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 447637826* @library /test/lib27* @summary Check the specific behaviour of the setReuseAddress(boolean)28* method.29* @run main Basic30* @run main/othervm -Dsun.net.useExclusiveBind Basic31* @run main/othervm -Dsun.net.useExclusiveBind=true Basic32* @run main/othervm -Djava.net.preferIPv4Stack=true Basic33* @run main/othervm -Dsun.net.useExclusiveBind34* -Djava.net.preferIPv4Stack=true Basic35* @run main/othervm -Dsun.net.useExclusiveBind=true36* -Djava.net.preferIPv4Stack=true Basic37*/38import java.net.*;39import jdk.test.lib.net.IPSupport;4041public class Basic {4243static int testCount = 0;44static int failures = 0;4546void test(String msg) {47testCount++;48System.out.println("***************************************");49System.out.println("Test " + testCount + ": " + msg);50}5152void passed() {53System.out.println("Test passed.");54}5556void failed() {57failures++;58System.out.println("Test failed.");59}6061void check(boolean pass) {62if (pass) {63passed();64} else {65failed();66}67}6869void SocketTests() throws Exception {70Socket s1 = new Socket();7172test("Socket should be created with SO_REUSEADDR disabled");73check(!s1.getReuseAddress());7475test("Socket.setReuseAddress(true)");76s1.setReuseAddress(true);77check(s1.getReuseAddress());7879test("Socket.setReuseAddress(false)");80s1.setReuseAddress(false);81check(!s1.getReuseAddress() );8283/* bind to any port */84s1.bind( new InetSocketAddress(0) );8586test("Binding Socket to port already in use should throw " +87"a BindException");88Socket s2 = new Socket();89try {90s2.bind( new InetSocketAddress(s1.getLocalPort()) );91failed();92} catch (BindException e) {93passed();94}95s2.close();9697s1.close();98}99100void ServerSocketTests() throws Exception {101ServerSocket s1 = new ServerSocket();102103test("ServerSocket.setReuseAddress(true)");104s1.setReuseAddress(true);105check(s1.getReuseAddress());106107test("Socket.setReuseAddress(false)");108s1.setReuseAddress(false);109check(!s1.getReuseAddress() );110111/* bind to any port */112s1.bind( new InetSocketAddress(0) );113114test("Binding ServerSocket to port already in use should throw " +115"a BindException");116ServerSocket s2 = new ServerSocket();117try {118s2.bind( new InetSocketAddress(s1.getLocalPort()) );119failed();120} catch (BindException e) {121passed();122}123s2.close();124125s1.close();126}127128void DatagramSocketTests() throws Exception {129DatagramSocket s1 = new DatagramSocket(null);130131test("DatagramSocket should be created with SO_REUSEADDR disabled");132check(!s1.getReuseAddress());133134test("DatagramSocket.setReuseAddress(true)");135s1.setReuseAddress(true);136check(s1.getReuseAddress());137138test("DatagramSocket.setReuseAddress(false)");139s1.setReuseAddress(false);140check(!s1.getReuseAddress() );141142/* bind to any port */143s1.bind( new InetSocketAddress(0) );144145test("Binding datagram socket to port already in use should throw " +146"a BindException");147DatagramSocket s2 = new DatagramSocket(null);148try {149s2.bind( new InetSocketAddress(s1.getLocalPort()) );150failed();151} catch (BindException e) {152passed();153}154s2.close();155s1.close();156157// bind with SO_REUSEADDR enabled158159s1 = new DatagramSocket(null);160s1.setReuseAddress(true);161s1.bind( new InetSocketAddress(0) );162163test("Bind 2 datagram sockets to the same port - second " +164"bind doesn't have SO_REUSEADDR enabled");165s2 = new DatagramSocket(null);166try {167s2.bind( new InetSocketAddress(s1.getLocalPort()) );168failed();169} catch (BindException e) {170passed();171}172s2.close();173174test("Bind 2 datagram sockets to the same port - both have " +175"SO_REUSEADDR enabled");176s2 = new DatagramSocket(null);177s2.setReuseAddress(true);178try {179s2.bind( new InetSocketAddress(s1.getLocalPort()) );180passed();181} catch (BindException e) {182if (System.getProperty("sun.net.useExclusiveBind") != null) {183// exclusive bind enabled - expected result184passed();185} else {186failed();187}188}189s2.close();190191s1.close();192193}194195void MulticastSocketTests() throws Exception {196test("Check SO_REUSEADDR is enabled in MulticastSocket()");197MulticastSocket s1 = new MulticastSocket();198check(s1.getReuseAddress());199s1.close();200201test("Check that SO_REUSEADDR is not disabled by " +202"MulticastSocket.bind()");203204s1 = new MulticastSocket(null);205206// bind to specific address207InetSocketAddress isa = new InetSocketAddress(208InetAddress.getLocalHost(), 0);209s1.bind(isa);210check(s1.getReuseAddress());211s1.close();212}213214Basic() throws Exception {215216SocketTests();217ServerSocketTests();218DatagramSocketTests();219MulticastSocketTests();220221System.out.println("***************************************");222System.out.println(testCount + " test(s) executed, " +223failures + " failure(s).");224if (failures > 0) {225throw new Exception(failures + " test(s) failed");226}227}228229public static void main(String args[]) throws Exception {230IPSupport.throwSkippedExceptionIfNonOperational();231new Basic();232}233234}235236237