jdk/test/java/net/BindException/Test.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 7668 d4a77089c587
child 16000 354638f62e27
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5981
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4417734
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that we get a BindException in all expected combinations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static Object[][] getTestCombinations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        return new Object[][]  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
            { "ServerSocket",   "Socket" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
            { "Socket",         "Socket" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
            { "DatagramSocket", "DatagramSocket" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static InetAddress ia4_this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static InetAddress ia6_this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static int failures;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static void doTest(Object test[], InetAddress ia1, InetAddress ia2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                       boolean silent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        String s1_type = (String)test[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        String s2_type = (String)test[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        int port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
         * Increment test count
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
         * Do the test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        boolean gotBindException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Exception failed_exc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            Socket sock1, sock2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            DatagramSocket dsock1, dsock2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            /* bind the first socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (s1_type.equals("Socket")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                sock1 = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                sock1.bind( new InetSocketAddress(ia1, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                port = sock1.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (s1_type.equals("ServerSocket")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                ss = new ServerSocket(0, 0, ia1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                port = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (s1_type.equals("DatagramSocket")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                dsock1 = new DatagramSocket( new InetSocketAddress(ia1, 0) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                port = dsock1.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            /* bind the second socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (s2_type.equals("Socket")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                sock2 = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                sock2.bind( new InetSocketAddress(ia2, port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (s2_type.equals("ServerSocket")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                ss = new ServerSocket(port, 0, ia2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (s2_type.equals("DatagramSocket")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                dsock2 = new DatagramSocket( new InetSocketAddress(ia2, port) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch (BindException be) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            gotBindException = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            failed_exc = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         * Did we expect a BindException?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        boolean expectedBindException = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (ia1 == ia4_this && ia2 == ia6_this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            expectedBindException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (ia1 == ia6_this && ia2 == ia4_this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            expectedBindException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * Did it fail?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (!failed && gotBindException != expectedBindException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * If test passed and running in silent mode then exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (!failed && silent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (failed || !silent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            System.out.println("**************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            System.out.println("Test " + count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            System.out.println(s1_type + " binds: " + ia1 + " port: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.out.println(s2_type + " binds: " + ia2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (!failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                if (gotBindException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    System.out.println("Got expected BindException - test passed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    System.out.println("No BindException as expected - test passed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (gotBindException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            System.out.println("BindException unexpected - test failed!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            System.out.println("No bind failure as expected - test failed!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        boolean silent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (args[0].equals("-d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                silent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * Test needs an IPv4 and IPv6 address to run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Enumeration nifs = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        while (nifs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            NetworkInterface ni = (NetworkInterface)nifs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            Enumeration addrs = ni.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            while (addrs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                InetAddress ia = (InetAddress)addrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
5981
2d5303114f44 6961029: java/net/BindException/Test.java should not use wildcard address
chegar
parents: 5506
diff changeset
   183
                if (ia.isLoopbackAddress() || ia.isAnyLocalAddress()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if ((ia instanceof Inet4Address) && (ia4_this == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    ia4_this = ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if ((ia instanceof Inet6Address) && (ia6_this == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    ia6_this = ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * Perform tests on all combinations of IPv4 and IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         * addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        InetAddress addrs[] = { ia4_this, ia6_this };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Object tests[][] = getTestCombinations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        for (int i=0; i<tests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            Object test[] = tests[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            for (int j=0; j<addrs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                for (int k=0; k<addrs.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    if (addrs[j] == null || addrs[k] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    doTest( test, addrs[j], addrs[k], silent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        System.out.println(count + " test(s) executed. " + failures + " failure(s).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new Exception(failures + " tests(s) failed - see log");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}