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/BindException/Test.java
38811 views
1
/*
2
* Copyright (c) 2001, 2013, 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
* @summary Test that we get a BindException in all expected combinations
28
*/
29
import java.net.*;
30
import java.util.Enumeration;
31
32
public class Test {
33
34
static Object[][] getTestCombinations() {
35
return new Object[][] {
36
{ "ServerSocket", "Socket" },
37
{ "Socket", "Socket" },
38
{ "DatagramSocket", "DatagramSocket" },
39
};
40
}
41
42
static InetAddress ia4_this;
43
static InetAddress ia6_this;
44
45
static int count;
46
static int failures;
47
48
static void doTest(Object test[], InetAddress ia1, InetAddress ia2,
49
boolean silent) throws Exception {
50
String s1_type = (String)test[0];
51
String s2_type = (String)test[1];
52
int port = 0;
53
54
/*
55
* Increment test count
56
*/
57
count++;
58
59
/*
60
* Do the test
61
*/
62
63
boolean gotBindException = false;
64
boolean failed = false;
65
Exception failed_exc = null;
66
67
Socket sock1 = null;
68
ServerSocket ss = null;
69
DatagramSocket dsock1 = null;
70
try {
71
/* bind the first socket */
72
73
if (s1_type.equals("Socket")) {
74
sock1 = new Socket();
75
sock1.bind( new InetSocketAddress(ia1, 0));
76
port = sock1.getLocalPort();
77
}
78
79
if (s1_type.equals("ServerSocket")) {
80
ss = new ServerSocket(0, 0, ia1);
81
port = ss.getLocalPort();
82
}
83
84
if (s1_type.equals("DatagramSocket")) {
85
dsock1 = new DatagramSocket( new InetSocketAddress(ia1, 0) );
86
port = dsock1.getLocalPort();
87
}
88
89
/* bind the second socket */
90
91
if (s2_type.equals("Socket")) {
92
try (Socket sock2 = new Socket()) {
93
sock2.bind( new InetSocketAddress(ia2, port));
94
}
95
}
96
97
if (s2_type.equals("ServerSocket")) {
98
try (ServerSocket ss2 = new ServerSocket(port, 0, ia2)) { }
99
}
100
101
if (s2_type.equals("DatagramSocket")) {
102
try (DatagramSocket ds =
103
new DatagramSocket(new InetSocketAddress(ia2, port))) { }
104
}
105
106
} catch (BindException be) {
107
gotBindException = true;
108
} catch (Exception e) {
109
failed = true;
110
failed_exc = e;
111
} finally {
112
if (sock1 != null) sock1.close();
113
if (ss != null) ss.close();
114
if (dsock1 != null) dsock1.close();
115
}
116
117
/*
118
* Did we expect a BindException?
119
*/
120
boolean expectedBindException = true;
121
if (ia1 == ia4_this && ia2 == ia6_this) {
122
expectedBindException = false;
123
}
124
if (ia1 == ia6_this && ia2 == ia4_this) {
125
expectedBindException = false;
126
}
127
128
/*
129
* Did it fail?
130
*/
131
132
if (!failed && gotBindException != expectedBindException) {
133
failed = true;
134
}
135
136
/*
137
* If test passed and running in silent mode then exit
138
*/
139
if (!failed && silent) {
140
return;
141
}
142
143
if (failed || !silent) {
144
System.out.println("");
145
System.out.println("**************************");
146
System.out.println("Test " + count);
147
148
System.out.println(s1_type + " binds: " + ia1 + " port: " + port);
149
System.out.println(s2_type + " binds: " + ia2);
150
151
if (!failed) {
152
if (gotBindException) {
153
System.out.println("Got expected BindException - test passed!");
154
} else {
155
System.out.println("No BindException as expected - test passed!");
156
}
157
return;
158
}
159
}
160
if (gotBindException) {
161
System.out.println("BindException unexpected - test failed!!!");
162
} else {
163
System.out.println("No bind failure as expected - test failed!!!");
164
}
165
failures++;
166
}
167
168
public static void main(String args[]) throws Exception {
169
170
boolean silent = true;
171
if (args.length > 0) {
172
if (args[0].equals("-d")) {
173
silent = false;
174
}
175
}
176
177
/*
178
* Test needs an IPv4 and IPv6 address to run.
179
*/
180
Enumeration nifs = NetworkInterface.getNetworkInterfaces();
181
while (nifs.hasMoreElements()) {
182
NetworkInterface ni = (NetworkInterface)nifs.nextElement();
183
184
Enumeration addrs = ni.getInetAddresses();
185
while (addrs.hasMoreElements()) {
186
InetAddress ia = (InetAddress)addrs.nextElement();
187
188
if (ia.isLoopbackAddress() || ia.isAnyLocalAddress()) {
189
continue;
190
}
191
192
if ((ia instanceof Inet4Address) && (ia4_this == null)) {
193
ia4_this = ia;
194
}
195
196
if ((ia instanceof Inet6Address) && (ia6_this == null)) {
197
ia6_this = ia;
198
}
199
}
200
}
201
202
/*
203
* Perform tests on all combinations of IPv4 and IPv6
204
* addresses.
205
*/
206
InetAddress addrs[] = { ia4_this, ia6_this };
207
208
Object tests[][] = getTestCombinations();
209
210
for (int i=0; i<tests.length; i++) {
211
Object test[] = tests[i];
212
213
for (int j=0; j<addrs.length; j++) {
214
for (int k=0; k<addrs.length; k++) {
215
216
if (addrs[j] == null || addrs[k] == null) {
217
continue;
218
}
219
220
doTest( test, addrs[j], addrs[k], silent);
221
}
222
}
223
}
224
225
System.out.println("");
226
System.out.println(count + " test(s) executed. " + failures + " failure(s).");
227
228
if (failures > 0) {
229
throw new Exception(failures + " tests(s) failed - see log");
230
}
231
}
232
}
233
234