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.Callable;
|
|
34 |
import java.util.concurrent.CountDownLatch;
|
|
35 |
import java.nio.channels.AlreadyConnectedException;
|
|
36 |
import java.nio.channels.ClosedChannelException;
|
|
37 |
import java.nio.channels.ConnectionPendingException;
|
|
38 |
import java.nio.channels.NoConnectionPendingException;
|
|
39 |
import java.nio.channels.UnresolvedAddressException;
|
|
40 |
import java.nio.channels.UnsupportedAddressTypeException;
|
|
41 |
import com.sun.nio.sctp.SctpChannel;
|
|
42 |
import com.sun.nio.sctp.SctpServerChannel;
|
|
43 |
import static java.lang.System.out;
|
|
44 |
import static java.lang.System.err;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Tests connect, finishConnect, isConnectionPending,
|
|
48 |
* getRemoteAddresses and association.
|
|
49 |
*/
|
|
50 |
public class Connect {
|
|
51 |
final CountDownLatch finishedLatch = new CountDownLatch(1);
|
|
52 |
|
|
53 |
void test(String[] args) {
|
|
54 |
SocketAddress address = null;
|
|
55 |
Server server = null;
|
|
56 |
|
|
57 |
if (!Util.isSCTPSupported()) {
|
|
58 |
out.println("SCTP protocol is not supported");
|
|
59 |
out.println("Test cannot be run");
|
|
60 |
return;
|
|
61 |
}
|
|
62 |
|
|
63 |
if (args.length == 2) {
|
|
64 |
/* requested to connect to a specific address */
|
|
65 |
try {
|
|
66 |
int port = Integer.valueOf(args[1]);
|
|
67 |
address = new InetSocketAddress(args[0], port);
|
|
68 |
} catch (NumberFormatException nfe) {
|
|
69 |
err.println(nfe);
|
|
70 |
}
|
|
71 |
} else {
|
|
72 |
/* start server on local machine, default */
|
|
73 |
try {
|
|
74 |
server = new Server();
|
|
75 |
server.start();
|
|
76 |
address = server.address();
|
|
77 |
debug("Server started and listening on " + address);
|
|
78 |
} catch (IOException ioe) {
|
|
79 |
ioe.printStackTrace();
|
|
80 |
return;
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
doTest(address);
|
|
85 |
}
|
|
86 |
|
|
87 |
void doTest(SocketAddress addr) {
|
|
88 |
SctpChannel channel = null;
|
|
89 |
final SocketAddress peerAddress = addr;
|
|
90 |
|
|
91 |
try {
|
|
92 |
channel = SctpChannel.open();
|
|
93 |
|
|
94 |
/* TEST 0.5 Verify default values for new/unconnected channel */
|
|
95 |
check(channel.getRemoteAddresses().isEmpty(),
|
|
96 |
"non empty set for unconnected channel");
|
|
97 |
check(channel.association() == null,
|
|
98 |
"non-null association for unconnected channel");
|
|
99 |
check(!channel.isConnectionPending(),
|
|
100 |
"should not have a connection pending");
|
|
101 |
|
|
102 |
/* TEST 1: non-blocking connect */
|
|
103 |
channel.configureBlocking(false);
|
|
104 |
if (channel.connect(peerAddress) != true) {
|
|
105 |
debug("non-blocking connect did not immediately succeed");
|
|
106 |
check(channel.isConnectionPending(),
|
|
107 |
"should return true for isConnectionPending");
|
|
108 |
try {
|
|
109 |
channel.connect(peerAddress);
|
|
110 |
fail("should have thrown ConnectionPendingException");
|
|
111 |
} catch (ConnectionPendingException cpe) {
|
|
112 |
pass();
|
|
113 |
} catch (IOException ioe) {
|
|
114 |
unexpected(ioe);
|
|
115 |
}
|
|
116 |
channel.configureBlocking(true);
|
|
117 |
check(channel.finishConnect(),
|
|
118 |
"finishConnect should have returned true");
|
|
119 |
}
|
|
120 |
|
|
121 |
/* TEST 1.5 Verify after connect */
|
|
122 |
check(!channel.getRemoteAddresses().isEmpty(),
|
|
123 |
"empty set for connected channel");
|
|
124 |
check(channel.association() != null,
|
|
125 |
"null association for connected channel");
|
|
126 |
check(!channel.isConnectionPending(),
|
|
127 |
"pending connection for connected channel");
|
|
128 |
|
|
129 |
/* TEST 2: Verify AlreadyConnectedException thrown */
|
|
130 |
try {
|
|
131 |
channel.connect(peerAddress);
|
|
132 |
fail("should have thrown AlreadyConnectedException");
|
|
133 |
} catch (AlreadyConnectedException unused) {
|
|
134 |
pass();
|
|
135 |
} catch (IOException ioe) {
|
|
136 |
unexpected(ioe);
|
|
137 |
}
|
|
138 |
|
|
139 |
/* TEST 3: UnresolvedAddressException */
|
|
140 |
channel.close();
|
|
141 |
channel = SctpChannel.open();
|
|
142 |
InetSocketAddress unresolved =
|
|
143 |
InetSocketAddress.createUnresolved("xxyyzzabc", 4567);
|
|
144 |
try {
|
|
145 |
channel.connect(unresolved);
|
|
146 |
fail("should have thrown UnresolvedAddressException");
|
|
147 |
} catch (UnresolvedAddressException unused) {
|
|
148 |
pass();
|
|
149 |
} catch (IOException ioe) {
|
|
150 |
unexpected(ioe);
|
|
151 |
}
|
|
152 |
|
|
153 |
/* TEST 4: UnsupportedAddressTypeException */
|
|
154 |
SocketAddress unsupported = new UnsupportedSocketAddress();
|
|
155 |
try {
|
|
156 |
channel.connect(unsupported);
|
|
157 |
fail("should have thrown UnsupportedAddressTypeException");
|
|
158 |
} catch (UnsupportedAddressTypeException unused) {
|
|
159 |
pass();
|
|
160 |
} catch (IOException ioe) {
|
|
161 |
unexpected(ioe);
|
|
162 |
}
|
|
163 |
|
|
164 |
/* TEST 5: ClosedChannelException */
|
|
165 |
channel.close();
|
|
166 |
final SctpChannel closedChannel = channel;
|
|
167 |
testCCE(new Callable<Void>() {
|
|
168 |
public Void call() throws IOException {
|
|
169 |
closedChannel.connect(peerAddress); return null; } });
|
|
170 |
|
|
171 |
/* TEST 5.5 getRemoteAddresses */
|
|
172 |
testCCE(new Callable<Void>() {
|
|
173 |
public Void call() throws IOException {
|
|
174 |
closedChannel.getRemoteAddresses(); return null; } });
|
|
175 |
testCCE(new Callable<Void>() {
|
|
176 |
public Void call() throws IOException {
|
|
177 |
closedChannel.association(); return null; } });
|
|
178 |
check(!channel.isConnectionPending(),
|
|
179 |
"pending connection for closed channel");
|
|
180 |
|
|
181 |
/* Run some more finishConnect tests */
|
|
182 |
|
|
183 |
/* TEST 6: NoConnectionPendingException */
|
|
184 |
channel = SctpChannel.open();
|
|
185 |
try {
|
|
186 |
channel.finishConnect();
|
|
187 |
fail("should have thrown NoConnectionPendingException");
|
|
188 |
} catch (NoConnectionPendingException unused) {
|
|
189 |
pass();
|
|
190 |
} catch (IOException ioe) {
|
|
191 |
unexpected(ioe);
|
|
192 |
}
|
|
193 |
|
|
194 |
/* TEST 7: ClosedChannelException */
|
|
195 |
channel.close();
|
|
196 |
final SctpChannel cceChannel = channel;
|
|
197 |
testCCE(new Callable<Void>() {
|
|
198 |
public Void call() throws IOException {
|
|
199 |
cceChannel.finishConnect(); return null; } });
|
|
200 |
} catch (IOException ioe) {
|
|
201 |
unexpected(ioe);
|
|
202 |
} finally {
|
|
203 |
finishedLatch.countDown();
|
|
204 |
try { if (channel != null) channel.close(); }
|
|
205 |
catch (IOException e) { unexpected(e);}
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
class UnsupportedSocketAddress extends SocketAddress { }
|
|
210 |
|
|
211 |
void testCCE(Callable callable) {
|
|
212 |
try {
|
|
213 |
callable.call();
|
|
214 |
fail("should have thrown ClosedChannelException");
|
|
215 |
} catch (ClosedChannelException cce) {
|
|
216 |
pass();
|
|
217 |
} catch (Exception ioe) {
|
|
218 |
unexpected(ioe);
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
|
222 |
class Server implements Runnable
|
|
223 |
{
|
|
224 |
final InetSocketAddress serverAddr;
|
|
225 |
private SctpServerChannel ssc;
|
|
226 |
|
|
227 |
public Server() throws IOException {
|
|
228 |
ssc = SctpServerChannel.open().bind(null);
|
|
229 |
java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
|
|
230 |
if (addrs.isEmpty())
|
|
231 |
debug("addrs should not be empty");
|
|
232 |
|
|
233 |
serverAddr = (InetSocketAddress) addrs.iterator().next();
|
|
234 |
}
|
|
235 |
|
|
236 |
public void start() {
|
|
237 |
(new Thread(this, "Server-" + serverAddr.getPort())).start();
|
|
238 |
}
|
|
239 |
|
|
240 |
public InetSocketAddress address() {
|
|
241 |
return serverAddr;
|
|
242 |
}
|
|
243 |
|
|
244 |
@Override
|
|
245 |
public void run() {
|
|
246 |
SctpChannel sc = null;
|
|
247 |
try {
|
|
248 |
sc = ssc.accept();
|
|
249 |
finishedLatch.await();
|
|
250 |
} catch (IOException ioe) {
|
|
251 |
unexpected(ioe);
|
|
252 |
} catch (InterruptedException ie) {
|
|
253 |
unexpected(ie);
|
|
254 |
} finally {
|
|
255 |
try { if (ssc != null) ssc.close(); }
|
|
256 |
catch (IOException ioe) { unexpected(ioe); }
|
|
257 |
try { if (sc != null) sc.close(); }
|
|
258 |
catch (IOException ioe) { unexpected(ioe); }
|
|
259 |
}
|
|
260 |
}
|
|
261 |
}
|
|
262 |
|
|
263 |
//--------------------- Infrastructure ---------------------------
|
|
264 |
boolean debug = true;
|
|
265 |
volatile int passed = 0, failed = 0;
|
|
266 |
void pass() {passed++;}
|
|
267 |
void fail() {failed++; Thread.dumpStack();}
|
|
268 |
void fail(String msg) {System.err.println(msg); fail();}
|
|
269 |
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
|
270 |
void check(boolean cond) {if (cond) pass(); else fail();}
|
|
271 |
void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
|
|
272 |
void debug(String message) {if(debug) { System.out.println(message); } }
|
|
273 |
public static void main(String[] args) throws Throwable {
|
|
274 |
Class<?> k = new Object(){}.getClass().getEnclosingClass();
|
|
275 |
try {k.getMethod("instanceMain",String[].class)
|
|
276 |
.invoke( k.newInstance(), (Object) args);}
|
|
277 |
catch (Throwable e) {throw e.getCause();}}
|
|
278 |
public void instanceMain(String[] args) throws Throwable {
|
|
279 |
try {test(args);} catch (Throwable t) {unexpected(t);}
|
|
280 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
|
281 |
if (failed > 0) throw new AssertionError("Some tests failed");}
|
|
282 |
|
|
283 |
}
|