Path: blob/master/test/jdk/java/net/BindException/Test.java
41149 views
/*1* Copyright (c) 2001, 2019, 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 441773426* @key intermittent27* @summary Test that we get a BindException in all expected combinations28* @library /test/lib29* @build jdk.test.lib.NetworkConfiguration30* jdk.test.lib.Platform31* @run main Test -d32*/3334import java.net.*;35import java.util.Enumeration;36import jdk.test.lib.NetworkConfiguration;3738public class Test {3940static Object[][] getTestCombinations() {41return new Object[][] {42{ "ServerSocket", "Socket" },43{ "Socket", "Socket" },44{ "DatagramSocket", "DatagramSocket" },45};46}4748static InetAddress ia4_this;49static InetAddress ia6_this;5051static int count;52static int failures;53static boolean retried;5455static void doTest(Object test[], InetAddress ia1, InetAddress ia2,56boolean silent) throws Exception {57/*58* Increment test count59*/60count++;6162doTest(test, count, ia1, ia2, silent, !retried);63}6465static void doTest(Object test[], int count, InetAddress ia1, InetAddress ia2,66boolean silent, boolean retry) throws Exception {67String s1_type = (String)test[0];68String s2_type = (String)test[1];69int port = 0;7071/*72* Do the test73*/7475boolean gotBindException = false;76boolean failed = false;77Exception failed_exc = null;7879Socket sock1 = null;80ServerSocket ss = null;81DatagramSocket dsock1 = null;82boolean firstBound = false;8384try {85/* bind the first socket */8687if (s1_type.equals("Socket")) {88sock1 = new Socket();89sock1.bind( new InetSocketAddress(ia1, 0));90port = sock1.getLocalPort();91}9293if (s1_type.equals("ServerSocket")) {94ss = new ServerSocket(0, 0, ia1);95port = ss.getLocalPort();96}9798if (s1_type.equals("DatagramSocket")) {99dsock1 = new DatagramSocket( new InetSocketAddress(ia1, 0) );100port = dsock1.getLocalPort();101}102103/* bind the second socket */104105// The fact that the port was available for ia1 does not106// guarantee that it will also be available for ia2 as something107// else might already be bound to that port.108// For the sake of test stability we will retry once in109// case of unexpected bind exception.110111firstBound = true;112if (s2_type.equals("Socket")) {113try (Socket sock2 = new Socket()) {114sock2.bind( new InetSocketAddress(ia2, port));115}116}117118if (s2_type.equals("ServerSocket")) {119try (ServerSocket ss2 = new ServerSocket(port, 0, ia2)) { }120}121122if (s2_type.equals("DatagramSocket")) {123try (DatagramSocket ds =124new DatagramSocket(new InetSocketAddress(ia2, port))) { }125}126127} catch (BindException be) {128gotBindException = true;129failed_exc = be;130} catch (Exception e) {131failed = true;132failed_exc = e;133} finally {134if (sock1 != null) sock1.close();135if (ss != null) ss.close();136if (dsock1 != null) dsock1.close();137}138139/*140* Did we expect a BindException?141*/142boolean expectedBindException = true;143if (ia1 == ia4_this && ia2 == ia6_this) {144expectedBindException = false;145}146if (ia1 == ia6_this && ia2 == ia4_this) {147expectedBindException = false;148}149150/*151* Did it fail?152*/153154if (!failed && gotBindException != expectedBindException) {155failed = true;156}157158/*159* If test passed and running in silent mode then exit160*/161if (!failed && silent) {162return;163}164165if (failed && retry && firstBound) {166// retry once at the first failure only167retried = true;168if (!silent) {169System.out.println("");170System.out.println("**************************");171System.out.println("Test " + count + ": Retrying...");172}173doTest(test, count, ia1, ia2, silent, false);174return;175}176177if (failed || !silent) {178System.out.println("");179System.out.println("**************************");180System.out.println("Test " + count);181182System.out.println(s1_type + " binds: " + ia1 + " port: " + port);183System.out.println(s2_type + " binds: " + ia2);184185if (!failed) {186if (gotBindException) {187System.out.println("Got expected BindException - test passed!");188failed_exc.printStackTrace(System.out);189} else {190System.out.println("No BindException as expected - test passed!");191}192return;193}194}195if (gotBindException) {196System.out.println("BindException unexpected - test failed!!!");197failed_exc.printStackTrace(System.out);198} else {199System.out.println("No bind failure as expected - test failed!!!");200}201failures++;202}203204public static void main(String args[]) throws Exception {205206boolean silent = true;207if (args.length > 0) {208if (args[0].equals("-d")) {209silent = false;210}211}212213/*214* Test needs an IPv4 and IPv6 address to run.215*/216Enumeration nifs = NetworkInterface.getNetworkInterfaces();217while (nifs.hasMoreElements()) {218NetworkInterface ni = (NetworkInterface)nifs.nextElement();219220Enumeration addrs = ni.getInetAddresses();221while (addrs.hasMoreElements()) {222InetAddress ia = (InetAddress)addrs.nextElement();223224if (ia.isLoopbackAddress() || ia.isAnyLocalAddress()) {225continue;226}227228if ((ia instanceof Inet4Address) && (ia4_this == null)) {229ia4_this = ia;230}231232if ((ia instanceof Inet6Address) && (ia6_this == null)) {233ia6_this = ia;234}235}236}237238/*239* Perform tests on all combinations of IPv4 and IPv6240* addresses.241*/242InetAddress addrs[] = { ia4_this, ia6_this };243244if (!silent) {245System.out.println("Using ia4_this:" + ia4_this);246System.out.println("Using ia6_this:" + ia6_this);247}248249Object tests[][] = getTestCombinations();250251for (int i=0; i<tests.length; i++) {252Object test[] = tests[i];253254for (int j=0; j<addrs.length; j++) {255for (int k=0; k<addrs.length; k++) {256257if (addrs[j] == null || addrs[k] == null) {258continue;259}260261doTest( test, addrs[j], addrs[k], silent);262}263}264}265266System.out.println("");267System.out.println(count + " test(s) executed. " + failures + " failure(s).");268269if (failures > 0) {270System.err.println("********************************");271NetworkConfiguration.printSystemConfiguration(System.err);272System.out.println("********************************");273throw new Exception(failures + " tests(s) failed - see log");274}275}276}277278279