Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/setReuseAddress/Basic.java
38828 views
1
/*
2
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
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
*
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.
22
*/
23
24
/*
25
* @test
26
* @bug 4476378
27
* @summary Check the specific behaviour of the setReuseAddress(boolean)
28
* method.
29
* @run main Basic
30
* @run main/othervm -Dsun.net.useExclusiveBind Basic
31
*/
32
import java.net.*;
33
34
public class Basic {
35
36
static int testCount = 0;
37
static int failures = 0;
38
39
void test(String msg) {
40
testCount++;
41
System.out.println("***************************************");
42
System.out.println("Test " + testCount + ": " + msg);
43
}
44
45
void passed() {
46
System.out.println("Test passed.");
47
}
48
49
void failed() {
50
failures++;
51
System.out.println("Test failed.");
52
}
53
54
void check(boolean pass) {
55
if (pass) {
56
passed();
57
} else {
58
failed();
59
}
60
}
61
62
void SocketTests() throws Exception {
63
Socket s1 = new Socket();
64
65
test("Socket should be created with SO_REUSEADDR disabled");
66
check(!s1.getReuseAddress());
67
68
test("Socket.setReuseAddress(true)");
69
s1.setReuseAddress(true);
70
check(s1.getReuseAddress());
71
72
test("Socket.setReuseAddress(false)");
73
s1.setReuseAddress(false);
74
check(!s1.getReuseAddress() );
75
76
/* bind to any port */
77
s1.bind( new InetSocketAddress(0) );
78
79
test("Binding Socket to port already in use should throw " +
80
"a BindException");
81
Socket s2 = new Socket();
82
try {
83
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
84
failed();
85
} catch (BindException e) {
86
passed();
87
}
88
s2.close();
89
90
s1.close();
91
}
92
93
void ServerSocketTests() throws Exception {
94
ServerSocket s1 = new ServerSocket();
95
96
test("ServerSocket.setReuseAddress(true)");
97
s1.setReuseAddress(true);
98
check(s1.getReuseAddress());
99
100
test("Socket.setReuseAddress(false)");
101
s1.setReuseAddress(false);
102
check(!s1.getReuseAddress() );
103
104
/* bind to any port */
105
s1.bind( new InetSocketAddress(0) );
106
107
test("Binding ServerSocket to port already in use should throw " +
108
"a BindException");
109
ServerSocket s2 = new ServerSocket();
110
try {
111
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
112
failed();
113
} catch (BindException e) {
114
passed();
115
}
116
s2.close();
117
118
s1.close();
119
}
120
121
void DatagramSocketTests() throws Exception {
122
DatagramSocket s1 = new DatagramSocket(null);
123
124
test("DatagramSocket should be created with SO_REUSEADDR disabled");
125
check(!s1.getReuseAddress());
126
127
test("DatagramSocket.setReuseAddress(true)");
128
s1.setReuseAddress(true);
129
check(s1.getReuseAddress());
130
131
test("DatagramSocket.setReuseAddress(false)");
132
s1.setReuseAddress(false);
133
check(!s1.getReuseAddress() );
134
135
/* bind to any port */
136
s1.bind( new InetSocketAddress(0) );
137
138
test("Binding datagram socket to port already in use should throw " +
139
"a BindException");
140
DatagramSocket s2 = new DatagramSocket(null);
141
try {
142
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
143
failed();
144
} catch (BindException e) {
145
passed();
146
}
147
s2.close();
148
s1.close();
149
150
// bind with SO_REUSEADDR enabled
151
152
s1 = new DatagramSocket(null);
153
s1.setReuseAddress(true);
154
s1.bind( new InetSocketAddress(0) );
155
156
test("Bind 2 datagram sockets to the same port - second " +
157
"bind doesn't have SO_REUSEADDR enabled");
158
s2 = new DatagramSocket(null);
159
try {
160
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
161
failed();
162
} catch (BindException e) {
163
passed();
164
}
165
s2.close();
166
167
test("Bind 2 datagram sockets to the same port - both have " +
168
"SO_REUSEADDR enabled");
169
s2 = new DatagramSocket(null);
170
s2.setReuseAddress(true);
171
try {
172
s2.bind( new InetSocketAddress(s1.getLocalPort()) );
173
passed();
174
} catch (BindException e) {
175
if (System.getProperty("sun.net.useExclusiveBind") != null) {
176
// exclusive bind enabled - expected result
177
passed();
178
} else {
179
failed();
180
}
181
}
182
s2.close();
183
184
s1.close();
185
186
}
187
188
void MulticastSocketTests() throws Exception {
189
test("Check SO_REUSEADDR is enabled in MulticastSocket()");
190
MulticastSocket s1 = new MulticastSocket();
191
check(s1.getReuseAddress());
192
s1.close();
193
194
test("Check that SO_REUSEADDR is not disabled by " +
195
"MulticastSocket.bind()");
196
197
s1 = new MulticastSocket(null);
198
199
// bind to specific address
200
InetSocketAddress isa = new InetSocketAddress(
201
InetAddress.getLocalHost(), 0);
202
s1.bind(isa);
203
check(s1.getReuseAddress());
204
s1.close();
205
}
206
207
Basic() throws Exception {
208
209
SocketTests();
210
ServerSocketTests();
211
DatagramSocketTests();
212
MulticastSocketTests();
213
214
System.out.println("***************************************");
215
System.out.println(testCount + " test(s) executed, " +
216
failures + " failure(s).");
217
if (failures > 0) {
218
throw new Exception(failures + " test(s) failed");
219
}
220
}
221
222
public static void main(String args[]) throws Exception {
223
new Basic();
224
}
225
226
}
227
228