2542
|
1 |
/*
|
|
2 |
* Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any 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.nio.ByteBuffer;
|
|
35 |
import java.nio.channels.ClosedChannelException;
|
|
36 |
import java.nio.channels.NotYetConnectedException;
|
|
37 |
import com.sun.nio.sctp.AbstractNotificationHandler;
|
|
38 |
import com.sun.nio.sctp.HandlerResult;
|
|
39 |
import com.sun.nio.sctp.MessageInfo;
|
|
40 |
import com.sun.nio.sctp.SctpChannel;
|
|
41 |
import com.sun.nio.sctp.SctpServerChannel;
|
|
42 |
import com.sun.nio.sctp.ShutdownNotification;
|
|
43 |
import static java.lang.System.out;
|
|
44 |
import static java.lang.System.err;
|
|
45 |
|
|
46 |
public class Shutdown {
|
|
47 |
static CountDownLatch finishedLatch = new CountDownLatch(1);
|
|
48 |
static CountDownLatch sentLatch = new CountDownLatch(1);
|
|
49 |
|
|
50 |
void test(String[] args) {
|
|
51 |
SocketAddress address = null;
|
|
52 |
ShutdownServer server = null;
|
|
53 |
|
|
54 |
if (!Util.isSCTPSupported()) {
|
|
55 |
out.println("SCTP protocol is not supported");
|
|
56 |
out.println("Test cannot be run");
|
|
57 |
return;
|
|
58 |
}
|
|
59 |
|
|
60 |
if (args.length == 2) {
|
|
61 |
/* requested to connecct to a specific address */
|
|
62 |
try {
|
|
63 |
int port = Integer.valueOf(args[1]);
|
|
64 |
address = new InetSocketAddress(args[0], port);
|
|
65 |
} catch (NumberFormatException nfe) {
|
|
66 |
err.println(nfe);
|
|
67 |
}
|
|
68 |
} else {
|
|
69 |
/* start server on local machine, default */
|
|
70 |
try {
|
|
71 |
server = new ShutdownServer();
|
|
72 |
server.start();
|
|
73 |
address = server.address();
|
|
74 |
debug("Server started and listening on " + address);
|
|
75 |
} catch (IOException ioe) {
|
|
76 |
ioe.printStackTrace();
|
|
77 |
return;
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
doTest(address);
|
|
82 |
}
|
|
83 |
|
|
84 |
void doTest(SocketAddress peerAddress) {
|
|
85 |
SctpChannel channel = null;
|
|
86 |
ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);
|
|
87 |
MessageInfo info;
|
|
88 |
|
|
89 |
try {
|
|
90 |
channel = SctpChannel.open();
|
|
91 |
|
|
92 |
/* TEST 1: Verify NotYetConnectedException thrown */
|
|
93 |
debug("Test 1: NotYetConnectedException");
|
|
94 |
try {
|
|
95 |
channel.shutdown();
|
|
96 |
fail("shutdown not throwing expected NotYetConnectedException");
|
|
97 |
} catch (NotYetConnectedException unused) {
|
|
98 |
pass();
|
|
99 |
} catch (IOException ioe) {
|
|
100 |
unexpected(ioe);
|
|
101 |
}
|
|
102 |
|
|
103 |
channel.connect(peerAddress);
|
|
104 |
sentLatch.await();
|
|
105 |
channel.shutdown();
|
|
106 |
|
|
107 |
/* TEST 2: receive data sent before shutdown */
|
|
108 |
do {
|
|
109 |
debug("Test 2: invoking receive");
|
|
110 |
info = channel.receive(buffer, null, null);
|
|
111 |
if (info == null) {
|
|
112 |
fail("unexpected null from receive");
|
|
113 |
return;
|
|
114 |
}
|
|
115 |
} while (!info.isComplete());
|
|
116 |
|
|
117 |
buffer.flip();
|
|
118 |
check(info != null, "info is null");
|
|
119 |
check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
|
|
120 |
length, "bytes received not equal to message length");
|
|
121 |
check(info.bytes() == buffer.remaining(), "bytes != remaining");
|
|
122 |
check(Util.compare(buffer, Util.SMALL_MESSAGE),
|
|
123 |
"received message not the same as sent message");
|
|
124 |
|
|
125 |
buffer.clear();
|
|
126 |
|
|
127 |
/* TEST 3: receive notifications on the SCTP stack */
|
|
128 |
debug("Test 3: receive notifications");
|
|
129 |
while ((info = channel.receive(buffer, null, null )) != null &&
|
|
130 |
info.bytes() != -1 );
|
|
131 |
|
|
132 |
|
|
133 |
/* TEST 4: If the channel is already shutdown then invoking this
|
|
134 |
* method has no effect. */
|
|
135 |
debug("Test 4: no-op");
|
|
136 |
try {
|
|
137 |
channel.shutdown();
|
|
138 |
pass();
|
|
139 |
} catch (IOException ioe) {
|
|
140 |
unexpected(ioe);
|
|
141 |
}
|
|
142 |
|
|
143 |
/* TEST 5: Further sends will throw ClosedChannelException */
|
|
144 |
debug("Test 5: ClosedChannelException");
|
|
145 |
info = MessageInfo.createOutgoing(null, 1);
|
|
146 |
try {
|
|
147 |
channel.send(buffer, info);
|
|
148 |
fail("shutdown not throwing expected ClosedChannelException");
|
|
149 |
} catch (ClosedChannelException unused) {
|
|
150 |
pass();
|
|
151 |
} catch (IOException ioe) {
|
|
152 |
unexpected(ioe);
|
|
153 |
}
|
|
154 |
} catch (IOException ioe) {
|
|
155 |
unexpected(ioe);
|
|
156 |
} catch (InterruptedException ie) {
|
|
157 |
unexpected(ie);
|
|
158 |
}finally {
|
|
159 |
finishedLatch.countDown();
|
|
160 |
try { if (channel != null) channel.close(); }
|
|
161 |
catch (IOException e) { unexpected(e);}
|
|
162 |
}
|
|
163 |
}
|
|
164 |
|
|
165 |
class ShutdownServer implements Runnable
|
|
166 |
{
|
|
167 |
final InetSocketAddress serverAddr;
|
|
168 |
private SctpServerChannel ssc;
|
|
169 |
|
|
170 |
public ShutdownServer() throws IOException {
|
|
171 |
ssc = SctpServerChannel.open().bind(null);
|
|
172 |
//serverAddr = (InetSocketAddress)(ssc.getAllLocalAddresses().iterator().next());
|
|
173 |
|
|
174 |
java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
|
|
175 |
if (addrs.isEmpty())
|
|
176 |
debug("addrs should not be empty");
|
|
177 |
|
|
178 |
serverAddr = (InetSocketAddress) addrs.iterator().next();
|
|
179 |
|
|
180 |
}
|
|
181 |
|
|
182 |
public void start() {
|
|
183 |
(new Thread(this, "ShutdownServer-" + serverAddr.getPort())).start();
|
|
184 |
}
|
|
185 |
|
|
186 |
public InetSocketAddress address() {
|
|
187 |
return serverAddr;
|
|
188 |
}
|
|
189 |
|
|
190 |
@Override
|
|
191 |
public void run() {
|
|
192 |
SctpChannel sc = null;
|
|
193 |
try {
|
|
194 |
sc = ssc.accept();
|
|
195 |
|
|
196 |
/* send a message */
|
|
197 |
MessageInfo info = MessageInfo.createOutgoing(null, 1);
|
|
198 |
ByteBuffer buf = ByteBuffer.allocateDirect(Util.SMALL_BUFFER);
|
|
199 |
buf.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
|
|
200 |
buf.flip();
|
|
201 |
sc.send(buf, info);
|
|
202 |
|
|
203 |
/* notify client that the data has been sent */
|
|
204 |
sentLatch.countDown();
|
|
205 |
|
|
206 |
/* wait until after the client has finished its tests */
|
|
207 |
finishedLatch.await();
|
|
208 |
|
|
209 |
buf.clear();
|
|
210 |
ShutdownNotificationHandler handler =
|
|
211 |
new ShutdownNotificationHandler();
|
|
212 |
BooleanWrapper bool = new BooleanWrapper();
|
|
213 |
sc.configureBlocking(false);
|
|
214 |
sc.receive(buf, bool, handler);
|
|
215 |
check(bool.booleanValue(), "SHUTDOWN not received on Server");
|
|
216 |
|
|
217 |
} catch (IOException ioe) {
|
|
218 |
ioe.printStackTrace();
|
|
219 |
} catch (InterruptedException ie) {
|
|
220 |
ie.printStackTrace();
|
|
221 |
} finally {
|
|
222 |
try { if (ssc != null) ssc.close(); }
|
|
223 |
catch (IOException ioe) { unexpected(ioe); }
|
|
224 |
try { if (sc != null) sc.close(); }
|
|
225 |
catch (IOException ioe) { unexpected(ioe); }
|
|
226 |
}
|
|
227 |
}
|
|
228 |
}
|
|
229 |
|
|
230 |
class BooleanWrapper {
|
|
231 |
boolean bool;
|
|
232 |
|
|
233 |
boolean booleanValue() {
|
|
234 |
return bool;
|
|
235 |
}
|
|
236 |
|
|
237 |
void booleanValue(boolean value) {
|
|
238 |
bool = value;
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
|
242 |
class ShutdownNotificationHandler extends AbstractNotificationHandler<BooleanWrapper>
|
|
243 |
{
|
|
244 |
@Override
|
|
245 |
public HandlerResult handleNotification(
|
|
246 |
ShutdownNotification sn, BooleanWrapper bool)
|
|
247 |
{
|
|
248 |
bool.booleanValue(true);
|
|
249 |
debug(sn.toString());
|
|
250 |
return HandlerResult.RETURN;
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
//--------------------- Infrastructure ---------------------------
|
|
255 |
boolean debug = true;
|
|
256 |
volatile int passed = 0, failed = 0;
|
|
257 |
void pass() {passed++;}
|
|
258 |
void fail() {failed++; Thread.dumpStack();}
|
|
259 |
void fail(String msg) {System.err.println(msg); fail();}
|
|
260 |
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
|
261 |
void check(boolean cond) {if (cond) pass(); else fail();}
|
|
262 |
void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
|
|
263 |
void debug(String message) {if(debug) { System.out.println(message); } }
|
|
264 |
public static void main(String[] args) throws Throwable {
|
|
265 |
Class<?> k = new Object(){}.getClass().getEnclosingClass();
|
|
266 |
try {k.getMethod("instanceMain",String[].class)
|
|
267 |
.invoke( k.newInstance(), (Object) args);}
|
|
268 |
catch (Throwable e) {throw e.getCause();}}
|
|
269 |
public void instanceMain(String[] args) throws Throwable {
|
|
270 |
try {test(args);} catch (Throwable t) {unexpected(t);}
|
|
271 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
|
272 |
if (failed > 0) throw new AssertionError("Some tests failed");}
|
|
273 |
|
|
274 |
}
|