Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/setReuseAddress/Basic.java
38828 views
/*1* Copyright (c) 2001, 2012, 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* @summary Check the specific behaviour of the setReuseAddress(boolean)27* method.28* @run main Basic29* @run main/othervm -Dsun.net.useExclusiveBind Basic30*/31import java.net.*;3233public class Basic {3435static int testCount = 0;36static int failures = 0;3738void test(String msg) {39testCount++;40System.out.println("***************************************");41System.out.println("Test " + testCount + ": " + msg);42}4344void passed() {45System.out.println("Test passed.");46}4748void failed() {49failures++;50System.out.println("Test failed.");51}5253void check(boolean pass) {54if (pass) {55passed();56} else {57failed();58}59}6061void SocketTests() throws Exception {62Socket s1 = new Socket();6364test("Socket should be created with SO_REUSEADDR disabled");65check(!s1.getReuseAddress());6667test("Socket.setReuseAddress(true)");68s1.setReuseAddress(true);69check(s1.getReuseAddress());7071test("Socket.setReuseAddress(false)");72s1.setReuseAddress(false);73check(!s1.getReuseAddress() );7475/* bind to any port */76s1.bind( new InetSocketAddress(0) );7778test("Binding Socket to port already in use should throw " +79"a BindException");80Socket s2 = new Socket();81try {82s2.bind( new InetSocketAddress(s1.getLocalPort()) );83failed();84} catch (BindException e) {85passed();86}87s2.close();8889s1.close();90}9192void ServerSocketTests() throws Exception {93ServerSocket s1 = new ServerSocket();9495test("ServerSocket.setReuseAddress(true)");96s1.setReuseAddress(true);97check(s1.getReuseAddress());9899test("Socket.setReuseAddress(false)");100s1.setReuseAddress(false);101check(!s1.getReuseAddress() );102103/* bind to any port */104s1.bind( new InetSocketAddress(0) );105106test("Binding ServerSocket to port already in use should throw " +107"a BindException");108ServerSocket s2 = new ServerSocket();109try {110s2.bind( new InetSocketAddress(s1.getLocalPort()) );111failed();112} catch (BindException e) {113passed();114}115s2.close();116117s1.close();118}119120void DatagramSocketTests() throws Exception {121DatagramSocket s1 = new DatagramSocket(null);122123test("DatagramSocket should be created with SO_REUSEADDR disabled");124check(!s1.getReuseAddress());125126test("DatagramSocket.setReuseAddress(true)");127s1.setReuseAddress(true);128check(s1.getReuseAddress());129130test("DatagramSocket.setReuseAddress(false)");131s1.setReuseAddress(false);132check(!s1.getReuseAddress() );133134/* bind to any port */135s1.bind( new InetSocketAddress(0) );136137test("Binding datagram socket to port already in use should throw " +138"a BindException");139DatagramSocket s2 = new DatagramSocket(null);140try {141s2.bind( new InetSocketAddress(s1.getLocalPort()) );142failed();143} catch (BindException e) {144passed();145}146s2.close();147s1.close();148149// bind with SO_REUSEADDR enabled150151s1 = new DatagramSocket(null);152s1.setReuseAddress(true);153s1.bind( new InetSocketAddress(0) );154155test("Bind 2 datagram sockets to the same port - second " +156"bind doesn't have SO_REUSEADDR enabled");157s2 = new DatagramSocket(null);158try {159s2.bind( new InetSocketAddress(s1.getLocalPort()) );160failed();161} catch (BindException e) {162passed();163}164s2.close();165166test("Bind 2 datagram sockets to the same port - both have " +167"SO_REUSEADDR enabled");168s2 = new DatagramSocket(null);169s2.setReuseAddress(true);170try {171s2.bind( new InetSocketAddress(s1.getLocalPort()) );172passed();173} catch (BindException e) {174if (System.getProperty("sun.net.useExclusiveBind") != null) {175// exclusive bind enabled - expected result176passed();177} else {178failed();179}180}181s2.close();182183s1.close();184185}186187void MulticastSocketTests() throws Exception {188test("Check SO_REUSEADDR is enabled in MulticastSocket()");189MulticastSocket s1 = new MulticastSocket();190check(s1.getReuseAddress());191s1.close();192193test("Check that SO_REUSEADDR is not disabled by " +194"MulticastSocket.bind()");195196s1 = new MulticastSocket(null);197198// bind to specific address199InetSocketAddress isa = new InetSocketAddress(200InetAddress.getLocalHost(), 0);201s1.bind(isa);202check(s1.getReuseAddress());203s1.close();204}205206Basic() throws Exception {207208SocketTests();209ServerSocketTests();210DatagramSocketTests();211MulticastSocketTests();212213System.out.println("***************************************");214System.out.println(testCount + " test(s) executed, " +215failures + " failure(s).");216if (failures > 0) {217throw new Exception(failures + " test(s) failed");218}219}220221public static void main(String args[]) throws Exception {222new Basic();223}224225}226227228