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