Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/BindException/Test.java
38811 views
/*1* Copyright (c) 2001, 2013, 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* @summary Test that we get a BindException in all expected combinations27*/28import java.net.*;29import java.util.Enumeration;3031public class Test {3233static Object[][] getTestCombinations() {34return new Object[][] {35{ "ServerSocket", "Socket" },36{ "Socket", "Socket" },37{ "DatagramSocket", "DatagramSocket" },38};39}4041static InetAddress ia4_this;42static InetAddress ia6_this;4344static int count;45static int failures;4647static void doTest(Object test[], InetAddress ia1, InetAddress ia2,48boolean silent) throws Exception {49String s1_type = (String)test[0];50String s2_type = (String)test[1];51int port = 0;5253/*54* Increment test count55*/56count++;5758/*59* Do the test60*/6162boolean gotBindException = false;63boolean failed = false;64Exception failed_exc = null;6566Socket sock1 = null;67ServerSocket ss = null;68DatagramSocket dsock1 = null;69try {70/* bind the first socket */7172if (s1_type.equals("Socket")) {73sock1 = new Socket();74sock1.bind( new InetSocketAddress(ia1, 0));75port = sock1.getLocalPort();76}7778if (s1_type.equals("ServerSocket")) {79ss = new ServerSocket(0, 0, ia1);80port = ss.getLocalPort();81}8283if (s1_type.equals("DatagramSocket")) {84dsock1 = new DatagramSocket( new InetSocketAddress(ia1, 0) );85port = dsock1.getLocalPort();86}8788/* bind the second socket */8990if (s2_type.equals("Socket")) {91try (Socket sock2 = new Socket()) {92sock2.bind( new InetSocketAddress(ia2, port));93}94}9596if (s2_type.equals("ServerSocket")) {97try (ServerSocket ss2 = new ServerSocket(port, 0, ia2)) { }98}99100if (s2_type.equals("DatagramSocket")) {101try (DatagramSocket ds =102new DatagramSocket(new InetSocketAddress(ia2, port))) { }103}104105} catch (BindException be) {106gotBindException = true;107} catch (Exception e) {108failed = true;109failed_exc = e;110} finally {111if (sock1 != null) sock1.close();112if (ss != null) ss.close();113if (dsock1 != null) dsock1.close();114}115116/*117* Did we expect a BindException?118*/119boolean expectedBindException = true;120if (ia1 == ia4_this && ia2 == ia6_this) {121expectedBindException = false;122}123if (ia1 == ia6_this && ia2 == ia4_this) {124expectedBindException = false;125}126127/*128* Did it fail?129*/130131if (!failed && gotBindException != expectedBindException) {132failed = true;133}134135/*136* If test passed and running in silent mode then exit137*/138if (!failed && silent) {139return;140}141142if (failed || !silent) {143System.out.println("");144System.out.println("**************************");145System.out.println("Test " + count);146147System.out.println(s1_type + " binds: " + ia1 + " port: " + port);148System.out.println(s2_type + " binds: " + ia2);149150if (!failed) {151if (gotBindException) {152System.out.println("Got expected BindException - test passed!");153} else {154System.out.println("No BindException as expected - test passed!");155}156return;157}158}159if (gotBindException) {160System.out.println("BindException unexpected - test failed!!!");161} else {162System.out.println("No bind failure as expected - test failed!!!");163}164failures++;165}166167public static void main(String args[]) throws Exception {168169boolean silent = true;170if (args.length > 0) {171if (args[0].equals("-d")) {172silent = false;173}174}175176/*177* Test needs an IPv4 and IPv6 address to run.178*/179Enumeration nifs = NetworkInterface.getNetworkInterfaces();180while (nifs.hasMoreElements()) {181NetworkInterface ni = (NetworkInterface)nifs.nextElement();182183Enumeration addrs = ni.getInetAddresses();184while (addrs.hasMoreElements()) {185InetAddress ia = (InetAddress)addrs.nextElement();186187if (ia.isLoopbackAddress() || ia.isAnyLocalAddress()) {188continue;189}190191if ((ia instanceof Inet4Address) && (ia4_this == null)) {192ia4_this = ia;193}194195if ((ia instanceof Inet6Address) && (ia6_this == null)) {196ia6_this = ia;197}198}199}200201/*202* Perform tests on all combinations of IPv4 and IPv6203* addresses.204*/205InetAddress addrs[] = { ia4_this, ia6_this };206207Object tests[][] = getTestCombinations();208209for (int i=0; i<tests.length; i++) {210Object test[] = tests[i];211212for (int j=0; j<addrs.length; j++) {213for (int k=0; k<addrs.length; k++) {214215if (addrs[j] == null || addrs[k] == null) {216continue;217}218219doTest( test, addrs[j], addrs[k], silent);220}221}222}223224System.out.println("");225System.out.println(count + " test(s) executed. " + failures + " failure(s).");226227if (failures > 0) {228throw new Exception(failures + " tests(s) failed - see log");229}230}231}232233234