2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4476378
|
|
27 |
* @summary Check the specific behaviour of the setReuseAddress(boolean)
|
|
28 |
* method.
|
|
29 |
*/
|
|
30 |
import java.net.*;
|
|
31 |
|
|
32 |
public class Basic {
|
|
33 |
|
|
34 |
static int testCount = 0;
|
|
35 |
static int failures = 0;
|
|
36 |
|
|
37 |
void test(String msg) {
|
|
38 |
testCount++;
|
|
39 |
System.out.println("***************************************");
|
|
40 |
System.out.println("Test " + testCount + ": " + msg);
|
|
41 |
}
|
|
42 |
|
|
43 |
void passed() {
|
|
44 |
System.out.println("Test passed.");
|
|
45 |
}
|
|
46 |
|
|
47 |
void failed() {
|
|
48 |
failures++;
|
|
49 |
System.out.println("Test failed.");
|
|
50 |
}
|
|
51 |
|
|
52 |
void check(boolean pass) {
|
|
53 |
if (pass) {
|
|
54 |
passed();
|
|
55 |
} else {
|
|
56 |
failed();
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
|
60 |
void SocketTests() throws Exception {
|
|
61 |
Socket s1 = new Socket();
|
|
62 |
|
|
63 |
test("Socket should be created with SO_REUSEADDR disabled");
|
|
64 |
check(!s1.getReuseAddress());
|
|
65 |
|
|
66 |
test("Socket.setReuseAddress(true)");
|
|
67 |
s1.setReuseAddress(true);
|
|
68 |
check(s1.getReuseAddress());
|
|
69 |
|
|
70 |
test("Socket.setReuseAddress(false)");
|
|
71 |
s1.setReuseAddress(false);
|
|
72 |
check(!s1.getReuseAddress() );
|
|
73 |
|
|
74 |
/* bind to any port */
|
|
75 |
s1.bind( new InetSocketAddress(0) );
|
|
76 |
|
|
77 |
test("Binding Socket to port already in use should throw " +
|
|
78 |
"a BindException");
|
|
79 |
Socket s2 = new Socket();
|
|
80 |
try {
|
|
81 |
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
|
|
82 |
failed();
|
|
83 |
} catch (BindException e) {
|
|
84 |
passed();
|
|
85 |
}
|
|
86 |
s2.close();
|
|
87 |
|
|
88 |
s1.close();
|
|
89 |
}
|
|
90 |
|
|
91 |
void ServerSocketTests() throws Exception {
|
|
92 |
ServerSocket s1 = new ServerSocket();
|
|
93 |
|
|
94 |
test("ServerSocket.setReuseAddress(true)");
|
|
95 |
s1.setReuseAddress(true);
|
|
96 |
check(s1.getReuseAddress());
|
|
97 |
|
|
98 |
test("Socket.setReuseAddress(false)");
|
|
99 |
s1.setReuseAddress(false);
|
|
100 |
check(!s1.getReuseAddress() );
|
|
101 |
|
|
102 |
/* bind to any port */
|
|
103 |
s1.bind( new InetSocketAddress(0) );
|
|
104 |
|
|
105 |
test("Binding ServerSocket to port already in use should throw " +
|
|
106 |
"a BindException");
|
|
107 |
ServerSocket s2 = new ServerSocket();
|
|
108 |
try {
|
|
109 |
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
|
|
110 |
failed();
|
|
111 |
} catch (BindException e) {
|
|
112 |
passed();
|
|
113 |
}
|
|
114 |
s2.close();
|
|
115 |
|
|
116 |
s1.close();
|
|
117 |
}
|
|
118 |
|
|
119 |
void DatagramSocketTests() throws Exception {
|
|
120 |
DatagramSocket s1 = new DatagramSocket(null);
|
|
121 |
|
|
122 |
test("DatagramSocket should be created with SO_REUSEADDR disabled");
|
|
123 |
check(!s1.getReuseAddress());
|
|
124 |
|
|
125 |
test("DatagramSocket.setReuseAddress(true)");
|
|
126 |
s1.setReuseAddress(true);
|
|
127 |
check(s1.getReuseAddress());
|
|
128 |
|
|
129 |
test("DatagramSocket.setReuseAddress(false)");
|
|
130 |
s1.setReuseAddress(false);
|
|
131 |
check(!s1.getReuseAddress() );
|
|
132 |
|
|
133 |
/* bind to any port */
|
|
134 |
s1.bind( new InetSocketAddress(0) );
|
|
135 |
|
|
136 |
test("Binding datagram socket to port already in use should throw " +
|
|
137 |
"a BindException");
|
|
138 |
DatagramSocket s2 = new DatagramSocket(null);
|
|
139 |
try {
|
|
140 |
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
|
|
141 |
failed();
|
|
142 |
} catch (BindException e) {
|
|
143 |
passed();
|
|
144 |
}
|
|
145 |
s2.close();
|
|
146 |
s1.close();
|
|
147 |
|
|
148 |
// bind with SO_REUSEADDR enabled
|
|
149 |
|
|
150 |
s1 = new DatagramSocket(null);
|
|
151 |
s1.setReuseAddress(true);
|
|
152 |
s1.bind( new InetSocketAddress(0) );
|
|
153 |
|
|
154 |
test("Bind 2 datagram sockets to the same port - second " +
|
|
155 |
"bind doesn't have SO_REUSEADDR enabled");
|
|
156 |
s2 = new DatagramSocket(null);
|
|
157 |
try {
|
|
158 |
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
|
|
159 |
failed();
|
|
160 |
} catch (BindException e) {
|
|
161 |
passed();
|
|
162 |
}
|
|
163 |
s2.close();
|
|
164 |
|
|
165 |
test("Bind 2 datagram sockets to the same port - both have " +
|
|
166 |
"SO_REUSEADDR enabled");
|
|
167 |
s2 = new DatagramSocket(null);
|
|
168 |
s2.setReuseAddress(true);
|
|
169 |
try {
|
|
170 |
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
|
|
171 |
passed();
|
|
172 |
} catch (BindException e) {
|
|
173 |
failed();
|
|
174 |
}
|
|
175 |
s2.close();
|
|
176 |
|
|
177 |
s1.close();
|
|
178 |
|
|
179 |
}
|
|
180 |
|
|
181 |
void MulticastSocketTests() throws Exception {
|
|
182 |
test("Check SO_REUSEADDR is enabled in MulticastSocket()");
|
|
183 |
MulticastSocket s1 = new MulticastSocket();
|
|
184 |
check(s1.getReuseAddress());
|
|
185 |
s1.close();
|
|
186 |
|
|
187 |
test("Check that SO_REUSEADDR is not disabled by " +
|
|
188 |
"MulticastSocket.bind()");
|
|
189 |
|
|
190 |
s1 = new MulticastSocket(null);
|
|
191 |
|
|
192 |
// bind to specific address
|
|
193 |
InetSocketAddress isa = new InetSocketAddress(
|
|
194 |
InetAddress.getLocalHost(), 0);
|
|
195 |
s1.bind(isa);
|
|
196 |
check(s1.getReuseAddress());
|
|
197 |
s1.close();
|
|
198 |
}
|
|
199 |
|
|
200 |
Basic() throws Exception {
|
|
201 |
|
|
202 |
SocketTests();
|
|
203 |
ServerSocketTests();
|
|
204 |
DatagramSocketTests();
|
|
205 |
MulticastSocketTests();
|
|
206 |
|
|
207 |
System.out.println("***************************************");
|
|
208 |
System.out.println(testCount + " test(s) executed, " +
|
|
209 |
failures + " failure(s).");
|
|
210 |
if (failures > 0) {
|
|
211 |
throw new Exception(failures + " test(s) failed");
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
public static void main(String args[]) throws Exception {
|
|
216 |
new Basic();
|
|
217 |
}
|
|
218 |
|
|
219 |
}
|