Basic.java revision 5107
0N/A/*
2362N/A * Copyright (c) 2001, 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
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/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 *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/A/*
0N/A * @test
0N/A * @bug 4476378
0N/A * @summary Check the specific behaviour of the setReuseAddress(boolean)
0N/A * method.
0N/A * @run main Basic
0N/A * @run main/othervm -Dsun.net.useExclusiveBind Basic
0N/A */
0N/Aimport java.net.*;
0N/A
0N/Apublic class Basic {
0N/A
0N/A static int testCount = 0;
0N/A static int failures = 0;
0N/A
0N/A void test(String msg) {
0N/A testCount++;
0N/A System.out.println("***************************************");
0N/A System.out.println("Test " + testCount + ": " + msg);
0N/A }
0N/A
0N/A void passed() {
0N/A System.out.println("Test passed.");
0N/A }
0N/A
0N/A void failed() {
0N/A failures++;
0N/A System.out.println("Test failed.");
0N/A }
0N/A
0N/A void check(boolean pass) {
0N/A if (pass) {
0N/A passed();
0N/A } else {
0N/A failed();
0N/A }
0N/A }
0N/A
0N/A void SocketTests() throws Exception {
0N/A Socket s1 = new Socket();
0N/A
0N/A test("Socket should be created with SO_REUSEADDR disabled");
0N/A check(!s1.getReuseAddress());
0N/A
0N/A test("Socket.setReuseAddress(true)");
s1.setReuseAddress(true);
check(s1.getReuseAddress());
test("Socket.setReuseAddress(false)");
s1.setReuseAddress(false);
check(!s1.getReuseAddress() );
/* bind to any port */
s1.bind( new InetSocketAddress(0) );
test("Binding Socket to port already in use should throw " +
"a BindException");
Socket s2 = new Socket();
try {
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
failed();
} catch (BindException e) {
passed();
}
s2.close();
s1.close();
}
void ServerSocketTests() throws Exception {
ServerSocket s1 = new ServerSocket();
test("ServerSocket.setReuseAddress(true)");
s1.setReuseAddress(true);
check(s1.getReuseAddress());
test("Socket.setReuseAddress(false)");
s1.setReuseAddress(false);
check(!s1.getReuseAddress() );
/* bind to any port */
s1.bind( new InetSocketAddress(0) );
test("Binding ServerSocket to port already in use should throw " +
"a BindException");
ServerSocket s2 = new ServerSocket();
try {
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
failed();
} catch (BindException e) {
passed();
}
s2.close();
s1.close();
}
void DatagramSocketTests() throws Exception {
DatagramSocket s1 = new DatagramSocket(null);
test("DatagramSocket should be created with SO_REUSEADDR disabled");
check(!s1.getReuseAddress());
test("DatagramSocket.setReuseAddress(true)");
s1.setReuseAddress(true);
check(s1.getReuseAddress());
test("DatagramSocket.setReuseAddress(false)");
s1.setReuseAddress(false);
check(!s1.getReuseAddress() );
/* bind to any port */
s1.bind( new InetSocketAddress(0) );
test("Binding datagram socket to port already in use should throw " +
"a BindException");
DatagramSocket s2 = new DatagramSocket(null);
try {
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
failed();
} catch (BindException e) {
passed();
}
s2.close();
s1.close();
// bind with SO_REUSEADDR enabled
s1 = new DatagramSocket(null);
s1.setReuseAddress(true);
s1.bind( new InetSocketAddress(0) );
test("Bind 2 datagram sockets to the same port - second " +
"bind doesn't have SO_REUSEADDR enabled");
s2 = new DatagramSocket(null);
try {
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
failed();
} catch (BindException e) {
passed();
}
s2.close();
test("Bind 2 datagram sockets to the same port - both have " +
"SO_REUSEADDR enabled");
s2 = new DatagramSocket(null);
s2.setReuseAddress(true);
try {
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
passed();
} catch (BindException e) {
if (System.getProperty("sun.net.useExclusiveBind") != null) {
// exclusive bind enabled - expected result
passed();
} else {
failed();
}
}
s2.close();
s1.close();
}
void MulticastSocketTests() throws Exception {
test("Check SO_REUSEADDR is enabled in MulticastSocket()");
MulticastSocket s1 = new MulticastSocket();
check(s1.getReuseAddress());
s1.close();
test("Check that SO_REUSEADDR is not disabled by " +
"MulticastSocket.bind()");
s1 = new MulticastSocket(null);
// bind to specific address
InetSocketAddress isa = new InetSocketAddress(
InetAddress.getLocalHost(), 0);
s1.bind(isa);
check(s1.getReuseAddress());
s1.close();
}
Basic() throws Exception {
SocketTests();
ServerSocketTests();
DatagramSocketTests();
MulticastSocketTests();
System.out.println("***************************************");
System.out.println(testCount + " test(s) executed, " +
failures + " failure(s).");
if (failures > 0) {
throw new Exception(failures + " test(s) failed");
}
}
public static void main(String args[]) throws Exception {
new Basic();
}
}