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