Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/nio/sctp/SctpChannel/Bind.java
41155 views
1
/*
2
* Copyright (c) 2009, 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
/* @test
25
* @bug 4927640
26
* @summary Tests the SCTP protocol implementation
27
* @author chegar
28
*/
29
30
import java.net.*;
31
import java.io.*;
32
import java.util.List;
33
import java.util.Set;
34
import java.util.Iterator;
35
import java.nio.ByteBuffer;
36
import java.nio.channels.AlreadyBoundException;
37
import java.nio.channels.AlreadyConnectedException;
38
import java.nio.channels.ClosedChannelException;
39
import java.nio.channels.UnsupportedAddressTypeException;
40
import com.sun.nio.sctp.AssociationChangeNotification;
41
import com.sun.nio.sctp.AbstractNotificationHandler;
42
import com.sun.nio.sctp.HandlerResult;
43
import com.sun.nio.sctp.IllegalUnbindException;
44
import com.sun.nio.sctp.MessageInfo;
45
import com.sun.nio.sctp.PeerAddressChangeNotification;
46
import com.sun.nio.sctp.SctpChannel;
47
import com.sun.nio.sctp.SctpServerChannel;
48
import com.sun.nio.sctp.ShutdownNotification;
49
import static java.lang.System.out;
50
51
/**
52
* Tests bind, bindAddress, unbindAddress, getLocalAddress, and
53
* getAllLocalAddresses.
54
*/
55
public class Bind {
56
void test(String[] args) {
57
if (!Util.isSCTPSupported()) {
58
out.println("SCTP protocol is not supported");
59
out.println("Test cannot be run");
60
return;
61
}
62
63
/* Simply bind tests */
64
testBind();
65
66
/* Test unconnected */
67
testBindUnbind(false);
68
69
/* Test connected */
70
/* Adding/Removing addresses from a connected association is optional.
71
* This test can be run on systems that support dynamic address
72
* reconfiguration */
73
//testBindUnbind(true);
74
}
75
76
void testBind() {
77
SctpChannel channel = null;
78
try {
79
channel = SctpChannel.open();
80
81
/* TEST 1: empty set if channel is not bound */
82
check(channel.getAllLocalAddresses().isEmpty(),
83
"getAllLocalAddresses returned non empty set for unbound channel");
84
85
/* TEST 2: null to bind the channel to an automatically assigned
86
* socket address */
87
channel.bind(null);
88
89
/* TEST 3: non empty set if the channel is bound */
90
check(!channel.getAllLocalAddresses().isEmpty(),
91
"getAllLocalAddresses returned empty set for bound channel");
92
debug("getAllLocalAddresses on channel bound to the wildcard:\n"
93
+ channel.getAllLocalAddresses());
94
95
/* TEST 4: AlreadyBoundException if this channel is already bound */
96
try { channel.bind(null); }
97
catch (AlreadyBoundException unused) { pass(); }
98
catch (IOException ioe) { unexpected(ioe); }
99
100
/* TEST 5: UnsupportedAddressTypeException */
101
try {
102
channel.close(); /* open a new unbound channel for test */
103
channel = SctpChannel.open();
104
channel.bind(new UnsupportedSocketAddress());
105
fail("UnsupportedSocketAddress expected");
106
} catch (UnsupportedAddressTypeException unused) { pass();
107
} catch (IOException ioe) { unexpected(ioe); }
108
109
/* TEST 6: AlreadyConnectedException */
110
try {
111
channel.close(); /* open a new unbound channel for test */
112
channel = SctpChannel.open();
113
try (var peer = connectChannel(channel)) {
114
channel.bind(null);
115
fail("AlreadyConnectedException expected");
116
}
117
} catch (AlreadyConnectedException unused) { pass();
118
} catch (IOException ioe) { unexpected(ioe); }
119
120
/* TEST 7: ClosedChannelException - If this channel is closed */
121
try {
122
channel.close(); /* open a new unbound channel for test */
123
channel = SctpChannel.open();
124
channel.close();
125
channel.bind(null);
126
fail("ClosedChannelException expected");
127
} catch (ClosedChannelException unused) { pass();
128
} catch (IOException ioe) { unexpected(ioe); }
129
130
/* TEST 8: ClosedChannelException if channel is closed */
131
try {
132
channel.getAllLocalAddresses();
133
fail("should have thrown ClosedChannelException");
134
} catch (ClosedChannelException cce) {
135
pass();
136
} catch (Exception ioe) {
137
unexpected(ioe);
138
}
139
} catch (IOException ioe) {
140
unexpected(ioe);
141
} finally {
142
try { channel.close(); }
143
catch (IOException ioe) { unexpected(ioe); }
144
}
145
}
146
147
void testBindUnbind(boolean connected) {
148
SctpChannel channel = null;
149
SctpChannel peerChannel = null;
150
151
debug("testBindUnbind, connected: " + connected);
152
try {
153
channel = SctpChannel.open();
154
155
List<InetAddress> addresses = Util.getAddresses(true, false);
156
Iterator iterator = addresses.iterator();
157
InetSocketAddress a = new InetSocketAddress((InetAddress)iterator.next(), 0);
158
debug("channel.bind( " + a + ")");
159
channel.bind(a);
160
while (iterator.hasNext()) {
161
InetAddress ia = (InetAddress)iterator.next();
162
debug("channel.bindAddress(" + ia + ")");
163
channel.bindAddress(ia);
164
}
165
if (debug) {Util.dumpAddresses(channel, out);}
166
167
if (connected) {
168
/* Test with connected channel */
169
peerChannel = connectChannel(channel);
170
}
171
172
/* TEST 1: bind/unbindAddresses on the system addresses */
173
debug("bind/unbindAddresses on the system addresses");
174
List<InetAddress> addrs = Util.getAddresses(true, false);
175
for (InetAddress addr : addrs) {
176
try {
177
debug("unbindAddress: " + addr);
178
check(boundAddress(channel, addr), "trying to remove address that is not bound");
179
channel.unbindAddress(addr);
180
if (debug) {Util.dumpAddresses(channel, out);}
181
check(!boundAddress(channel, addr), "address was not removed");
182
183
debug("bindAddress: " + addr);
184
channel.bindAddress(addr);
185
if (debug) {Util.dumpAddresses(channel, out);}
186
check(boundAddress(channel, addr), "address is not bound");
187
} catch (IOException ioe) {
188
unexpected(ioe);
189
}
190
}
191
192
/* TEST 2: bindAddress - already bound address. */
193
InetAddress againAddress = addrs.get(0);
194
try {
195
debug("bind already bound address " + againAddress);
196
channel.bindAddress(againAddress);
197
} catch (AlreadyBoundException unused) {
198
debug("Caught AlreadyBoundException - OK");
199
pass();
200
} catch (IOException ioe) {
201
unexpected(ioe);
202
}
203
204
/* TEST 3: bind non local address */
205
try {
206
InetAddress nla = InetAddress.getByName("123.123.123.123");
207
debug("bind non local address " + nla);
208
channel.bindAddress(nla);
209
} catch (IOException ioe) {
210
debug("Informative only " + ioe);
211
}
212
213
/* TEST 4: unbind address that is not bound */
214
try {
215
debug("unbind address that is not bound " + againAddress);
216
/* remove address first then again */
217
channel.unbindAddress(againAddress);
218
channel.unbindAddress(againAddress);
219
} catch (IllegalUnbindException unused) {
220
debug("Caught IllegalUnbindException - OK");
221
pass();
222
} catch (IOException ioe) {
223
unexpected(ioe);
224
}
225
226
/* TEST 5: unbind address that is not bound */
227
try {
228
InetAddress nla = InetAddress.getByName("123.123.123.123");
229
debug("unbind address that is not bound " + nla);
230
channel.unbindAddress(nla);
231
232
} catch (IllegalUnbindException unused) {
233
debug("Caught IllegalUnbindException - OK");
234
pass();
235
} catch (IOException ioe) {
236
unexpected(ioe);
237
}
238
239
if (connected) {
240
channel.shutdown();
241
242
BindNotificationHandler handler = new BindNotificationHandler();
243
ByteBuffer buffer = ByteBuffer.allocate(10);
244
MessageInfo info;
245
while((info = peerChannel.receive(buffer, null, handler)) != null) {
246
if (info != null) {
247
if (info.bytes() == -1) {
248
debug("peerChannel Reached EOF");
249
break;
250
}
251
}
252
}
253
254
while((info = channel.receive(buffer, null, handler)) != null) {
255
if (info != null) {
256
if (info.bytes() == -1) {
257
debug("channel Reached EOF");
258
break;
259
}
260
}
261
}
262
}
263
} catch (IOException ioe) {
264
ioe.printStackTrace();
265
} finally {
266
try { if (channel != null) channel.close(); }
267
catch (IOException ioe) { unexpected(ioe); }
268
try { if (peerChannel != null) peerChannel.close(); }
269
catch (IOException ioe) { unexpected(ioe); }
270
}
271
}
272
273
boolean boundAddress(SctpChannel channel, InetAddress addr)
274
throws IOException {
275
for (SocketAddress boundAddr : channel.getAllLocalAddresses()) {
276
if (((InetSocketAddress) boundAddr).getAddress().equals(addr))
277
return true;
278
}
279
return false;
280
}
281
282
SctpChannel connectChannel(SctpChannel channel)
283
throws IOException {
284
debug("connecting channel...");
285
try {
286
SctpServerChannel ssc = SctpServerChannel.open();
287
ssc.bind(null);
288
Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
289
Iterator<SocketAddress> iterator = addrs.iterator();
290
SocketAddress addr = iterator.next();
291
debug("using " + addr + "...");
292
channel.connect(addr);
293
SctpChannel peerChannel = ssc.accept();
294
ssc.close();
295
debug("connected");
296
return peerChannel;
297
} catch (IOException ioe) {
298
debug("Cannot connect channel");
299
unexpected(ioe);
300
throw ioe;
301
}
302
}
303
304
class BindNotificationHandler extends AbstractNotificationHandler<Object>
305
{
306
@Override
307
public HandlerResult handleNotification(
308
AssociationChangeNotification acn, Object unused)
309
{
310
debug("AssociationChangeNotification: " + acn);
311
return HandlerResult.CONTINUE;
312
}
313
314
@Override
315
public HandlerResult handleNotification(
316
PeerAddressChangeNotification pacn, Object unused)
317
{
318
debug("PeerAddressChangeNotification: " + pacn);
319
return HandlerResult.CONTINUE;
320
}
321
322
@Override
323
public HandlerResult handleNotification(
324
ShutdownNotification sn, Object unused)
325
{
326
debug("ShutdownNotification: " + sn);
327
return HandlerResult.CONTINUE;
328
}
329
}
330
331
class UnsupportedSocketAddress extends SocketAddress { }
332
333
//--------------------- Infrastructure ---------------------------
334
boolean debug = true;
335
volatile int passed = 0, failed = 0;
336
void pass() {passed++;}
337
void fail() {failed++; Thread.dumpStack();}
338
void fail(String msg) {System.err.println(msg); fail();}
339
void unexpected(Throwable t) {failed++; t.printStackTrace();}
340
void check(boolean cond) {if (cond) pass(); else fail();}
341
void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
342
void debug(String message) {if(debug) { System.out.println(message); } }
343
public static void main(String[] args) throws Throwable {
344
Class<?> k = new Object(){}.getClass().getEnclosingClass();
345
try {k.getMethod("instanceMain",String[].class)
346
.invoke( k.newInstance(), (Object) args);}
347
catch (Throwable e) {throw e.getCause();}}
348
public void instanceMain(String[] args) throws Throwable {
349
try {test(args);} catch (Throwable t) {unexpected(t);}
350
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
351
if (failed > 0) throw new AssertionError("Some tests failed");}
352
353
}
354
355