jdk/test/java/net/Socket/setReuseAddress/Basic.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 13043 10fa4f21ea0f
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13043
diff changeset
     2
 * Copyright (c) 2001, 2012, 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 4476378
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check the specific behaviour of the setReuseAddress(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * method.
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 5506
diff changeset
    29
 * @run main Basic
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 5506
diff changeset
    30
 * @run main/othervm -Dsun.net.useExclusiveBind Basic
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static int testCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    void test(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        testCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        System.out.println("***************************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        System.out.println("Test " + testCount + ": " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    void passed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    void failed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.out.println("Test failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    void check(boolean pass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (pass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            passed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            failed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    void SocketTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Socket s1 = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        test("Socket should be created with SO_REUSEADDR disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        check(!s1.getReuseAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        test("Socket.setReuseAddress(true)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        s1.setReuseAddress(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        check(s1.getReuseAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        test("Socket.setReuseAddress(false)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        s1.setReuseAddress(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        check(!s1.getReuseAddress() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        /* bind to any port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        s1.bind( new InetSocketAddress(0) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        test("Binding Socket to port already in use should throw " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
             "a BindException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Socket s2 = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            s2.bind( new InetSocketAddress(s1.getLocalPort()) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            failed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } catch (BindException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            passed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        s2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        s1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    void ServerSocketTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        ServerSocket s1 = new ServerSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        test("ServerSocket.setReuseAddress(true)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        s1.setReuseAddress(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        check(s1.getReuseAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        test("Socket.setReuseAddress(false)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        s1.setReuseAddress(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        check(!s1.getReuseAddress() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        /* bind to any port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        s1.bind( new InetSocketAddress(0) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        test("Binding ServerSocket to port already in use should throw " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                "a BindException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        ServerSocket s2 = new ServerSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            s2.bind( new InetSocketAddress(s1.getLocalPort()) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            failed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (BindException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            passed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        s2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        s1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    void DatagramSocketTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        DatagramSocket s1 = new DatagramSocket(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        test("DatagramSocket should be created with SO_REUSEADDR disabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        check(!s1.getReuseAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        test("DatagramSocket.setReuseAddress(true)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        s1.setReuseAddress(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        check(s1.getReuseAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        test("DatagramSocket.setReuseAddress(false)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        s1.setReuseAddress(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        check(!s1.getReuseAddress() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        /* bind to any port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        s1.bind( new InetSocketAddress(0) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        test("Binding datagram socket to port already in use should throw " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
             "a BindException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        DatagramSocket s2 = new DatagramSocket(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            s2.bind( new InetSocketAddress(s1.getLocalPort()) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            failed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } catch (BindException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            passed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        s2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        s1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // bind with SO_REUSEADDR enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        s1 = new DatagramSocket(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        s1.setReuseAddress(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        s1.bind( new InetSocketAddress(0) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        test("Bind 2 datagram sockets to the same port - second " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
             "bind doesn't have SO_REUSEADDR enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        s2 = new DatagramSocket(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            s2.bind( new InetSocketAddress(s1.getLocalPort()) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            failed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch (BindException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            passed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        s2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        test("Bind 2 datagram sockets to the same port - both have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
             "SO_REUSEADDR enabled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        s2 = new DatagramSocket(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        s2.setReuseAddress(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            s2.bind( new InetSocketAddress(s1.getLocalPort()) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            passed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (BindException e) {
13043
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 5506
diff changeset
   175
            if (System.getProperty("sun.net.useExclusiveBind") != null) {
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 5506
diff changeset
   176
                // exclusive bind enabled - expected result
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 5506
diff changeset
   177
                passed();
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 5506
diff changeset
   178
            } else {
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 5506
diff changeset
   179
                failed();
10fa4f21ea0f 7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents: 5506
diff changeset
   180
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        s2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        s1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    void MulticastSocketTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        test("Check SO_REUSEADDR is enabled in MulticastSocket()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        MulticastSocket s1 = new MulticastSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        check(s1.getReuseAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        s1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        test("Check that SO_REUSEADDR is not disabled by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
             "MulticastSocket.bind()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        s1 = new MulticastSocket(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // bind to specific address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        InetSocketAddress isa = new InetSocketAddress(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                   InetAddress.getLocalHost(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        s1.bind(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        check(s1.getReuseAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        s1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    Basic() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        SocketTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        ServerSocketTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        DatagramSocketTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        MulticastSocketTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        System.out.println("***************************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        System.out.println(testCount + " test(s) executed, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                           failures + " failure(s).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throw new Exception(failures + " test(s) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        new Basic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}