Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/nio/sctp/SctpChannel/Receive.java
41155 views
1
/*
2
* Copyright (c) 2009, 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.InetSocketAddress;
31
import java.net.SocketAddress;
32
import java.io.IOException;
33
import java.util.concurrent.CountDownLatch;
34
import java.util.concurrent.TimeUnit;
35
import java.nio.ByteBuffer;
36
import java.nio.channels.NotYetConnectedException;
37
import java.nio.channels.ClosedChannelException;
38
import com.sun.nio.sctp.AbstractNotificationHandler;
39
import com.sun.nio.sctp.AssociationChangeNotification;
40
import com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent;
41
import com.sun.nio.sctp.HandlerResult;
42
import com.sun.nio.sctp.IllegalReceiveException;
43
import com.sun.nio.sctp.MessageInfo;
44
import com.sun.nio.sctp.Notification;
45
import com.sun.nio.sctp.SctpChannel;
46
import com.sun.nio.sctp.SctpServerChannel;
47
import com.sun.nio.sctp.ShutdownNotification;
48
import static java.lang.System.out;
49
import static java.lang.System.err;
50
51
public class Receive {
52
/* Latches used to synchronize between the client and server so that
53
* connections without any IO may not be closed without being accepted */
54
final CountDownLatch clientFinishedLatch = new CountDownLatch(1);
55
final CountDownLatch serverFinishedLatch = new CountDownLatch(1);
56
57
/* Used to verify that the ppid is being sent and received correctly */
58
static final int PPID = 5;
59
60
void test(String[] args) {
61
SocketAddress address = null;
62
Server server;
63
64
65
if (!Util.isSCTPSupported()) {
66
out.println("SCTP protocol is not supported");
67
out.println("Test cannot be run");
68
return;
69
}
70
71
if (args.length == 2) {
72
/* requested to connecct to a specific address */
73
try {
74
int port = Integer.valueOf(args[1]);
75
address = new InetSocketAddress(args[0], port);
76
} catch (NumberFormatException nfe) {
77
err.println(nfe);
78
}
79
} else {
80
/* start server on local machine, default */
81
try {
82
server = new Server();
83
server.start();
84
address = server.address();
85
debug("Server started and listening on " + address);
86
} catch (IOException ioe) {
87
ioe.printStackTrace();
88
return;
89
}
90
}
91
92
doTest(address);
93
}
94
95
void doTest(SocketAddress peerAddress) {
96
SctpChannel channel = null;
97
ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
98
MessageInfo info;
99
100
try {
101
channel = SctpChannel.open();
102
ReceiveNotificationHandler handler =
103
new ReceiveNotificationHandler(channel);
104
105
/* TEST 1: Verify NotYetConnectedException thrown */
106
try {
107
channel.receive(buffer, null, handler);
108
fail("should have thrown NotYetConnectedException");
109
} catch (NotYetConnectedException unused) {
110
pass();
111
} catch (IOException ioe) {
112
unexpected(ioe);
113
}
114
115
channel.connect(peerAddress);
116
117
/* TEST 2: receive small message */
118
do {
119
debug("Test 2: invoking receive");
120
info = channel.receive(buffer, null, handler);
121
if (info == null) {
122
fail("unexpected null from receive");
123
return;
124
}
125
} while (!info.isComplete());
126
127
buffer.flip();
128
check(handler.receivedCommUp(), "SCTP_COMM_UP not received");
129
check(info != null, "info is null");
130
check(info.address() != null, "address is null");
131
check(info.association() != null, "association is null");
132
check(info.isComplete(), "message is not complete");
133
check(info.isUnordered() != true,
134
"message should not be unordered");
135
check(info.streamNumber() >= 0, "invalid stream number");
136
check(info.payloadProtocolID() == PPID, "PPID incorrect");
137
check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
138
length, "bytes received not equal to message length");
139
check(info.bytes() == buffer.remaining(), "bytes != remaining");
140
check(Util.compare(buffer, Util.SMALL_MESSAGE),
141
"received message not the same as sent message");
142
143
buffer.clear();
144
145
/* TEST 3: receive large message */
146
do {
147
debug("Test 3: invoking receive");
148
info = channel.receive(buffer, null, handler);
149
if (info == null) {
150
fail("unexpected null from receive");
151
return;
152
}
153
} while (!info.isComplete());
154
155
buffer.flip();
156
check(info != null, "info is null");
157
check(info.address() != null, "address is null");
158
check(info.association() != null, "association is null");
159
check(info.isComplete(), "message is not complete");
160
check(info.isUnordered() != true,
161
"message should not be unordered");
162
check(info.streamNumber() >= 0, "invalid stream number");
163
check(info.bytes() == Util.LARGE_MESSAGE.getBytes("ISO-8859-1").
164
length, "bytes received not equal to message length");
165
check(info.bytes() == buffer.remaining(), "bytes != remaining");
166
check(Util.compare(buffer, Util.LARGE_MESSAGE),
167
"received message not the same as sent message");
168
169
buffer.clear();
170
171
/* TEST 4: EOF */
172
buffer.clear(); // buffer position 0
173
info = channel.receive(buffer,null, handler);
174
check(info != null, "info is null");
175
check(info.bytes() == -1, "should have received EOF");
176
check(buffer.position() == 0, "buffer position should be unchanged");
177
178
/* TEST 5: ClosedChannelException */
179
channel.close();
180
try {
181
channel.receive(buffer, null, null);
182
fail("should have thrown ClosedChannelException");
183
} catch (ClosedChannelException cce) {
184
pass();
185
} catch (IOException ioe) {
186
unexpected(ioe);
187
}
188
handler = null;
189
190
/* TEST 6: handler returns RETURN after handling a notification */
191
ReceiveNotificationHandler handler2 =
192
new ReceiveNotificationHandler(null); /* HandlerResult.RETURN */
193
channel = SctpChannel.open(peerAddress, 0, 0);
194
info = channel.receive(buffer, null, handler2);
195
check(info == null, "channel should return null");
196
check(handler2.receivedCommUp(), "SCTP_COMM_UP not received");
197
check(buffer.position() == 0, "buffer position should be unchanged");
198
199
/* TEST 7: Non blocking channel return null if no data */
200
channel.configureBlocking(false);
201
info = channel.receive(buffer, null, null);
202
check(info == null, "non-blocking channel should return null");
203
check(buffer.position() == 0, "buffer position should be unchanged");
204
} catch (IOException ioe) {
205
unexpected(ioe);
206
} finally {
207
clientFinishedLatch.countDown();
208
try { serverFinishedLatch.await(10L, TimeUnit.SECONDS); }
209
catch (InterruptedException ie) { unexpected(ie); }
210
if (channel != null) {
211
try { channel.close(); }
212
catch (IOException e) { unexpected (e);}
213
}
214
}
215
}
216
217
class Server implements Runnable
218
{
219
final InetSocketAddress serverAddr;
220
private SctpServerChannel ssc;
221
222
public Server() throws IOException {
223
ssc = SctpServerChannel.open().bind(null);
224
java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
225
if (addrs.isEmpty())
226
debug("addrs should not be empty");
227
228
serverAddr = (InetSocketAddress) addrs.iterator().next();
229
}
230
231
public void start() {
232
(new Thread(this, "Server-" + serverAddr.getPort())).start();
233
}
234
235
public InetSocketAddress address() {
236
return serverAddr;
237
}
238
239
@Override
240
public void run() {
241
try {
242
SctpChannel sc = ssc.accept();
243
244
/* send a small message */
245
MessageInfo info = MessageInfo.createOutgoing(null, 0)
246
.payloadProtocolID(PPID);
247
ByteBuffer buf = ByteBuffer.allocateDirect(Util.SMALL_BUFFER);
248
buf.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
249
buf.flip();
250
251
debug("sending small message: " + buf);
252
sc.send(buf, info);
253
254
/* send a large message */
255
buf = ByteBuffer.allocateDirect(Util.LARGE_BUFFER);
256
buf.put(Util.LARGE_MESSAGE.getBytes("ISO-8859-1"));
257
buf.flip();
258
259
debug("sending large message: " + buf);
260
sc.send(buf, info);
261
sc.shutdown();
262
debug("shutdown");
263
ReceiveNotificationHandler handler =
264
new ReceiveNotificationHandler(sc);
265
sc.receive(buf, null, handler);
266
sc.close();
267
268
/* accept another socket for the TEST 6 */
269
sc = ssc.accept();
270
ssc.close();
271
272
clientFinishedLatch.await(10L, TimeUnit.SECONDS);
273
serverFinishedLatch.countDown();
274
sc.close();
275
} catch (IOException ioe) {
276
unexpected(ioe);
277
} catch (InterruptedException ie) {
278
unexpected(ie);
279
}
280
}
281
}
282
283
class ReceiveNotificationHandler extends AbstractNotificationHandler<Object>
284
{
285
SctpChannel channel;
286
boolean receivedCommUp; // false
287
288
public ReceiveNotificationHandler(SctpChannel channel) {
289
this.channel = channel;
290
}
291
292
public boolean receivedCommUp() {
293
return receivedCommUp;
294
}
295
296
@Override
297
public HandlerResult handleNotification(
298
Notification notification, Object attachment) {
299
fail("Unknown notification type");
300
return HandlerResult.CONTINUE;
301
}
302
303
@Override
304
public HandlerResult handleNotification(
305
AssociationChangeNotification notification, Object attachment) {
306
AssocChangeEvent event = notification.event();
307
debug("AssociationChangeNotification");
308
debug(" Association: " + notification.association());
309
debug(" Event: " + event);
310
311
if (event.equals(AssocChangeEvent.COMM_UP))
312
receivedCommUp = true;
313
314
if (channel == null)
315
return HandlerResult.RETURN;
316
317
/* TEST 4: IllegalReceiveException - If the given handler invokes
318
* the receive method of this channel*/
319
ByteBuffer buffer = ByteBuffer.allocate(10);
320
try {
321
channel.receive(buffer, null, this);
322
fail("IllegalReceiveException expected");
323
} catch (IllegalReceiveException unused) {
324
pass();
325
} catch (IOException ioe) {
326
unexpected(ioe);
327
}
328
329
return HandlerResult.CONTINUE;
330
}
331
332
@Override
333
public HandlerResult handleNotification(
334
ShutdownNotification notification, Object attachment) {
335
debug("ShutdownNotification");
336
debug(" Association: " + notification.association());
337
return HandlerResult.CONTINUE;
338
}
339
}
340
//--------------------- Infrastructure ---------------------------
341
boolean debug = true;
342
volatile int passed = 0, failed = 0;
343
void pass() {passed++;}
344
void fail() {failed++; Thread.dumpStack();}
345
void fail(String msg) {System.err.println(msg); fail();}
346
void unexpected(Throwable t) {failed++; t.printStackTrace();}
347
void check(boolean cond) {if (cond) pass(); else fail();}
348
void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
349
void debug(String message) {if(debug) {
350
System.out.println(Thread.currentThread() + " " + message); } }
351
public static void main(String[] args) throws Throwable {
352
Class<?> k = new Object(){}.getClass().getEnclosingClass();
353
try {k.getMethod("instanceMain",String[].class)
354
.invoke( k.newInstance(), (Object) args);}
355
catch (Throwable e) {throw e.getCause();}}
356
public void instanceMain(String[] args) throws Throwable {
357
try {test(args);} catch (Throwable t) {unexpected(t);}
358
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
359
if (failed > 0) throw new AssertionError("Some tests failed");}
360
361
}
362
363