Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/BindException/Test.java
41149 views
1
/*
2
* Copyright (c) 2001, 2019, 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 4417734
27
* @key intermittent
28
* @summary Test that we get a BindException in all expected combinations
29
* @library /test/lib
30
* @build jdk.test.lib.NetworkConfiguration
31
* jdk.test.lib.Platform
32
* @run main Test -d
33
*/
34
35
import java.net.*;
36
import java.util.Enumeration;
37
import jdk.test.lib.NetworkConfiguration;
38
39
public class Test {
40
41
static Object[][] getTestCombinations() {
42
return new Object[][] {
43
{ "ServerSocket", "Socket" },
44
{ "Socket", "Socket" },
45
{ "DatagramSocket", "DatagramSocket" },
46
};
47
}
48
49
static InetAddress ia4_this;
50
static InetAddress ia6_this;
51
52
static int count;
53
static int failures;
54
static boolean retried;
55
56
static void doTest(Object test[], InetAddress ia1, InetAddress ia2,
57
boolean silent) throws Exception {
58
/*
59
* Increment test count
60
*/
61
count++;
62
63
doTest(test, count, ia1, ia2, silent, !retried);
64
}
65
66
static void doTest(Object test[], int count, InetAddress ia1, InetAddress ia2,
67
boolean silent, boolean retry) throws Exception {
68
String s1_type = (String)test[0];
69
String s2_type = (String)test[1];
70
int port = 0;
71
72
/*
73
* Do the test
74
*/
75
76
boolean gotBindException = false;
77
boolean failed = false;
78
Exception failed_exc = null;
79
80
Socket sock1 = null;
81
ServerSocket ss = null;
82
DatagramSocket dsock1 = null;
83
boolean firstBound = false;
84
85
try {
86
/* bind the first socket */
87
88
if (s1_type.equals("Socket")) {
89
sock1 = new Socket();
90
sock1.bind( new InetSocketAddress(ia1, 0));
91
port = sock1.getLocalPort();
92
}
93
94
if (s1_type.equals("ServerSocket")) {
95
ss = new ServerSocket(0, 0, ia1);
96
port = ss.getLocalPort();
97
}
98
99
if (s1_type.equals("DatagramSocket")) {
100
dsock1 = new DatagramSocket( new InetSocketAddress(ia1, 0) );
101
port = dsock1.getLocalPort();
102
}
103
104
/* bind the second socket */
105
106
// The fact that the port was available for ia1 does not
107
// guarantee that it will also be available for ia2 as something
108
// else might already be bound to that port.
109
// For the sake of test stability we will retry once in
110
// case of unexpected bind exception.
111
112
firstBound = true;
113
if (s2_type.equals("Socket")) {
114
try (Socket sock2 = new Socket()) {
115
sock2.bind( new InetSocketAddress(ia2, port));
116
}
117
}
118
119
if (s2_type.equals("ServerSocket")) {
120
try (ServerSocket ss2 = new ServerSocket(port, 0, ia2)) { }
121
}
122
123
if (s2_type.equals("DatagramSocket")) {
124
try (DatagramSocket ds =
125
new DatagramSocket(new InetSocketAddress(ia2, port))) { }
126
}
127
128
} catch (BindException be) {
129
gotBindException = true;
130
failed_exc = be;
131
} catch (Exception e) {
132
failed = true;
133
failed_exc = e;
134
} finally {
135
if (sock1 != null) sock1.close();
136
if (ss != null) ss.close();
137
if (dsock1 != null) dsock1.close();
138
}
139
140
/*
141
* Did we expect a BindException?
142
*/
143
boolean expectedBindException = true;
144
if (ia1 == ia4_this && ia2 == ia6_this) {
145
expectedBindException = false;
146
}
147
if (ia1 == ia6_this && ia2 == ia4_this) {
148
expectedBindException = false;
149
}
150
151
/*
152
* Did it fail?
153
*/
154
155
if (!failed && gotBindException != expectedBindException) {
156
failed = true;
157
}
158
159
/*
160
* If test passed and running in silent mode then exit
161
*/
162
if (!failed && silent) {
163
return;
164
}
165
166
if (failed && retry && firstBound) {
167
// retry once at the first failure only
168
retried = true;
169
if (!silent) {
170
System.out.println("");
171
System.out.println("**************************");
172
System.out.println("Test " + count + ": Retrying...");
173
}
174
doTest(test, count, ia1, ia2, silent, false);
175
return;
176
}
177
178
if (failed || !silent) {
179
System.out.println("");
180
System.out.println("**************************");
181
System.out.println("Test " + count);
182
183
System.out.println(s1_type + " binds: " + ia1 + " port: " + port);
184
System.out.println(s2_type + " binds: " + ia2);
185
186
if (!failed) {
187
if (gotBindException) {
188
System.out.println("Got expected BindException - test passed!");
189
failed_exc.printStackTrace(System.out);
190
} else {
191
System.out.println("No BindException as expected - test passed!");
192
}
193
return;
194
}
195
}
196
if (gotBindException) {
197
System.out.println("BindException unexpected - test failed!!!");
198
failed_exc.printStackTrace(System.out);
199
} else {
200
System.out.println("No bind failure as expected - test failed!!!");
201
}
202
failures++;
203
}
204
205
public static void main(String args[]) throws Exception {
206
207
boolean silent = true;
208
if (args.length > 0) {
209
if (args[0].equals("-d")) {
210
silent = false;
211
}
212
}
213
214
/*
215
* Test needs an IPv4 and IPv6 address to run.
216
*/
217
Enumeration nifs = NetworkInterface.getNetworkInterfaces();
218
while (nifs.hasMoreElements()) {
219
NetworkInterface ni = (NetworkInterface)nifs.nextElement();
220
221
Enumeration addrs = ni.getInetAddresses();
222
while (addrs.hasMoreElements()) {
223
InetAddress ia = (InetAddress)addrs.nextElement();
224
225
if (ia.isLoopbackAddress() || ia.isAnyLocalAddress()) {
226
continue;
227
}
228
229
if ((ia instanceof Inet4Address) && (ia4_this == null)) {
230
ia4_this = ia;
231
}
232
233
if ((ia instanceof Inet6Address) && (ia6_this == null)) {
234
ia6_this = ia;
235
}
236
}
237
}
238
239
/*
240
* Perform tests on all combinations of IPv4 and IPv6
241
* addresses.
242
*/
243
InetAddress addrs[] = { ia4_this, ia6_this };
244
245
if (!silent) {
246
System.out.println("Using ia4_this:" + ia4_this);
247
System.out.println("Using ia6_this:" + ia6_this);
248
}
249
250
Object tests[][] = getTestCombinations();
251
252
for (int i=0; i<tests.length; i++) {
253
Object test[] = tests[i];
254
255
for (int j=0; j<addrs.length; j++) {
256
for (int k=0; k<addrs.length; k++) {
257
258
if (addrs[j] == null || addrs[k] == null) {
259
continue;
260
}
261
262
doTest( test, addrs[j], addrs[k], silent);
263
}
264
}
265
}
266
267
System.out.println("");
268
System.out.println(count + " test(s) executed. " + failures + " failure(s).");
269
270
if (failures > 0) {
271
System.err.println("********************************");
272
NetworkConfiguration.printSystemConfiguration(System.err);
273
System.out.println("********************************");
274
throw new Exception(failures + " tests(s) failed - see log");
275
}
276
}
277
}
278
279