0N/A/*
3261N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A /*
0N/A * @test
0N/A * @bug 4417734
0N/A * @summary Test that we get a BindException in all expected combinations
0N/A */
0N/Aimport java.net.*;
0N/Aimport java.util.Enumeration;
0N/A
0N/Apublic class Test {
0N/A
0N/A static Object[][] getTestCombinations() {
0N/A return new Object[][] {
0N/A { "ServerSocket", "Socket" },
0N/A { "Socket", "Socket" },
0N/A { "DatagramSocket", "DatagramSocket" },
0N/A };
0N/A }
0N/A
0N/A static InetAddress ia4_this;
0N/A static InetAddress ia6_this;
0N/A
0N/A static int count;
0N/A static int failures;
0N/A
0N/A static void doTest(Object test[], InetAddress ia1, InetAddress ia2,
5775N/A boolean silent) throws Exception {
0N/A String s1_type = (String)test[0];
0N/A String s2_type = (String)test[1];
0N/A int port = 0;
0N/A
0N/A /*
0N/A * Increment test count
0N/A */
0N/A count++;
0N/A
0N/A /*
0N/A * Do the test
0N/A */
0N/A
0N/A boolean gotBindException = false;
0N/A boolean failed = false;
0N/A Exception failed_exc = null;
0N/A
5775N/A Socket sock1 = null;
5775N/A ServerSocket ss = null;
5775N/A DatagramSocket dsock1 = null;
0N/A try {
0N/A /* bind the first socket */
0N/A
0N/A if (s1_type.equals("Socket")) {
0N/A sock1 = new Socket();
0N/A sock1.bind( new InetSocketAddress(ia1, 0));
0N/A port = sock1.getLocalPort();
0N/A }
0N/A
0N/A if (s1_type.equals("ServerSocket")) {
0N/A ss = new ServerSocket(0, 0, ia1);
0N/A port = ss.getLocalPort();
0N/A }
0N/A
0N/A if (s1_type.equals("DatagramSocket")) {
0N/A dsock1 = new DatagramSocket( new InetSocketAddress(ia1, 0) );
0N/A port = dsock1.getLocalPort();
0N/A }
0N/A
0N/A /* bind the second socket */
0N/A
0N/A if (s2_type.equals("Socket")) {
5775N/A try (Socket sock2 = new Socket()) {
5775N/A sock2.bind( new InetSocketAddress(ia2, port));
5775N/A }
0N/A }
0N/A
0N/A if (s2_type.equals("ServerSocket")) {
5775N/A try (ServerSocket ss2 = new ServerSocket(port, 0, ia2)) { }
0N/A }
0N/A
0N/A if (s2_type.equals("DatagramSocket")) {
5775N/A try (DatagramSocket ds =
5775N/A new DatagramSocket(new InetSocketAddress(ia2, port))) { }
0N/A }
0N/A
0N/A } catch (BindException be) {
0N/A gotBindException = true;
0N/A } catch (Exception e) {
0N/A failed = true;
0N/A failed_exc = e;
5775N/A } finally {
5775N/A if (sock1 != null) sock1.close();
5775N/A if (ss != null) ss.close();
5775N/A if (dsock1 != null) dsock1.close();
0N/A }
0N/A
0N/A /*
0N/A * Did we expect a BindException?
0N/A */
0N/A boolean expectedBindException = true;
0N/A if (ia1 == ia4_this && ia2 == ia6_this) {
0N/A expectedBindException = false;
0N/A }
0N/A if (ia1 == ia6_this && ia2 == ia4_this) {
0N/A expectedBindException = false;
0N/A }
0N/A
0N/A /*
0N/A * Did it fail?
0N/A */
0N/A
0N/A if (!failed && gotBindException != expectedBindException) {
0N/A failed = true;
0N/A }
0N/A
0N/A /*
0N/A * If test passed and running in silent mode then exit
0N/A */
0N/A if (!failed && silent) {
0N/A return;
0N/A }
0N/A
0N/A if (failed || !silent) {
0N/A System.out.println("");
0N/A System.out.println("**************************");
0N/A System.out.println("Test " + count);
0N/A
0N/A System.out.println(s1_type + " binds: " + ia1 + " port: " + port);
0N/A System.out.println(s2_type + " binds: " + ia2);
0N/A
0N/A if (!failed) {
0N/A if (gotBindException) {
0N/A System.out.println("Got expected BindException - test passed!");
0N/A } else {
0N/A System.out.println("No BindException as expected - test passed!");
0N/A }
0N/A return;
0N/A }
0N/A }
0N/A if (gotBindException) {
0N/A System.out.println("BindException unexpected - test failed!!!");
0N/A } else {
0N/A System.out.println("No bind failure as expected - test failed!!!");
0N/A }
0N/A failures++;
0N/A }
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A
0N/A boolean silent = true;
0N/A if (args.length > 0) {
0N/A if (args[0].equals("-d")) {
0N/A silent = false;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Test needs an IPv4 and IPv6 address to run.
0N/A */
0N/A Enumeration nifs = NetworkInterface.getNetworkInterfaces();
0N/A while (nifs.hasMoreElements()) {
0N/A NetworkInterface ni = (NetworkInterface)nifs.nextElement();
0N/A
0N/A Enumeration addrs = ni.getInetAddresses();
0N/A while (addrs.hasMoreElements()) {
0N/A InetAddress ia = (InetAddress)addrs.nextElement();
0N/A
2557N/A if (ia.isLoopbackAddress() || ia.isAnyLocalAddress()) {
0N/A continue;
0N/A }
0N/A
0N/A if ((ia instanceof Inet4Address) && (ia4_this == null)) {
0N/A ia4_this = ia;
0N/A }
0N/A
0N/A if ((ia instanceof Inet6Address) && (ia6_this == null)) {
0N/A ia6_this = ia;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Perform tests on all combinations of IPv4 and IPv6
0N/A * addresses.
0N/A */
0N/A InetAddress addrs[] = { ia4_this, ia6_this };
0N/A
0N/A Object tests[][] = getTestCombinations();
0N/A
0N/A for (int i=0; i<tests.length; i++) {
0N/A Object test[] = tests[i];
0N/A
0N/A for (int j=0; j<addrs.length; j++) {
0N/A for (int k=0; k<addrs.length; k++) {
0N/A
0N/A if (addrs[j] == null || addrs[k] == null) {
0N/A continue;
0N/A }
0N/A
0N/A doTest( test, addrs[j], addrs[k], silent);
0N/A }
0N/A }
0N/A }
0N/A
0N/A System.out.println("");
0N/A System.out.println(count + " test(s) executed. " + failures + " failure(s).");
0N/A
0N/A if (failures > 0) {
0N/A throw new Exception(failures + " tests(s) failed - see log");
0N/A }
0N/A }
0N/A}