author | chegar |
Thu, 21 Nov 2019 12:14:29 +0000 | |
changeset 59205 | afcc8af048e6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2542 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. |
2542 | 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 |
* |
|
5506 | 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. |
|
2542 | 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; |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
33 |
import java.util.Set; |
2542 | 34 |
import java.util.concurrent.Callable; |
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 |
||
52 |
void test(String[] args) { |
|
53 |
if (!Util.isSCTPSupported()) { |
|
54 |
out.println("SCTP protocol is not supported"); |
|
55 |
out.println("Test cannot be run"); |
|
56 |
return; |
|
57 |
} |
|
58 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
59 |
doTest(); |
2542 | 60 |
} |
61 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
62 |
void doTest() { |
2542 | 63 |
SctpChannel channel = null; |
64 |
||
59205
afcc8af048e6
8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents:
47216
diff
changeset
|
65 |
try (SctpServerChannel ssc = SctpServerChannel.open()) { |
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
66 |
/* Create a server channel to connect to */ |
59205
afcc8af048e6
8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents:
47216
diff
changeset
|
67 |
ssc.bind(null); |
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
68 |
Set<SocketAddress> addrs = ssc.getAllLocalAddresses(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
69 |
if (addrs.isEmpty()) |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
70 |
debug("addrs should not be empty"); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
71 |
final SocketAddress peerAddress = (InetSocketAddress) addrs.iterator().next(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
72 |
|
2542 | 73 |
channel = SctpChannel.open(); |
74 |
||
75 |
/* TEST 0.5 Verify default values for new/unconnected channel */ |
|
76 |
check(channel.getRemoteAddresses().isEmpty(), |
|
77 |
"non empty set for unconnected channel"); |
|
78 |
check(channel.association() == null, |
|
79 |
"non-null association for unconnected channel"); |
|
80 |
check(!channel.isConnectionPending(), |
|
81 |
"should not have a connection pending"); |
|
82 |
||
83 |
/* TEST 1: non-blocking connect */ |
|
84 |
channel.configureBlocking(false); |
|
85 |
if (channel.connect(peerAddress) != true) { |
|
86 |
debug("non-blocking connect did not immediately succeed"); |
|
87 |
check(channel.isConnectionPending(), |
|
88 |
"should return true for isConnectionPending"); |
|
89 |
try { |
|
90 |
channel.connect(peerAddress); |
|
91 |
fail("should have thrown ConnectionPendingException"); |
|
92 |
} catch (ConnectionPendingException cpe) { |
|
93 |
pass(); |
|
94 |
} catch (IOException ioe) { |
|
95 |
unexpected(ioe); |
|
96 |
} |
|
97 |
channel.configureBlocking(true); |
|
98 |
check(channel.finishConnect(), |
|
99 |
"finishConnect should have returned true"); |
|
100 |
} |
|
101 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
102 |
ssc.accept(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
103 |
ssc.close(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
104 |
|
2542 | 105 |
/* TEST 1.5 Verify after connect */ |
106 |
check(!channel.getRemoteAddresses().isEmpty(), |
|
107 |
"empty set for connected channel"); |
|
108 |
check(channel.association() != null, |
|
109 |
"null association for connected channel"); |
|
110 |
check(!channel.isConnectionPending(), |
|
111 |
"pending connection for connected channel"); |
|
112 |
||
113 |
/* TEST 2: Verify AlreadyConnectedException thrown */ |
|
114 |
try { |
|
115 |
channel.connect(peerAddress); |
|
116 |
fail("should have thrown AlreadyConnectedException"); |
|
117 |
} catch (AlreadyConnectedException unused) { |
|
118 |
pass(); |
|
119 |
} catch (IOException ioe) { |
|
120 |
unexpected(ioe); |
|
121 |
} |
|
122 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
123 |
/* TEST 2.5: Verify AlreadyConnectedException thrown */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
124 |
try { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
125 |
channel.connect(peerAddress, 5, 5); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
126 |
fail("should have thrown AlreadyConnectedException"); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
127 |
} catch (AlreadyConnectedException unused) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
128 |
pass(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
129 |
} catch (IOException ioe) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
130 |
unexpected(ioe); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
131 |
} |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
132 |
|
2542 | 133 |
/* TEST 3: UnresolvedAddressException */ |
134 |
channel.close(); |
|
135 |
channel = SctpChannel.open(); |
|
136 |
InetSocketAddress unresolved = |
|
137 |
InetSocketAddress.createUnresolved("xxyyzzabc", 4567); |
|
138 |
try { |
|
139 |
channel.connect(unresolved); |
|
140 |
fail("should have thrown UnresolvedAddressException"); |
|
141 |
} catch (UnresolvedAddressException unused) { |
|
142 |
pass(); |
|
143 |
} catch (IOException ioe) { |
|
144 |
unexpected(ioe); |
|
145 |
} |
|
146 |
||
147 |
/* TEST 4: UnsupportedAddressTypeException */ |
|
148 |
SocketAddress unsupported = new UnsupportedSocketAddress(); |
|
149 |
try { |
|
150 |
channel.connect(unsupported); |
|
151 |
fail("should have thrown UnsupportedAddressTypeException"); |
|
152 |
} catch (UnsupportedAddressTypeException unused) { |
|
153 |
pass(); |
|
154 |
} catch (IOException ioe) { |
|
155 |
unexpected(ioe); |
|
156 |
} |
|
157 |
||
158 |
/* TEST 5: ClosedChannelException */ |
|
159 |
channel.close(); |
|
160 |
final SctpChannel closedChannel = channel; |
|
161 |
testCCE(new Callable<Void>() { |
|
162 |
public Void call() throws IOException { |
|
163 |
closedChannel.connect(peerAddress); return null; } }); |
|
164 |
||
165 |
/* TEST 5.5 getRemoteAddresses */ |
|
166 |
testCCE(new Callable<Void>() { |
|
167 |
public Void call() throws IOException { |
|
168 |
closedChannel.getRemoteAddresses(); return null; } }); |
|
169 |
testCCE(new Callable<Void>() { |
|
170 |
public Void call() throws IOException { |
|
171 |
closedChannel.association(); return null; } }); |
|
172 |
check(!channel.isConnectionPending(), |
|
173 |
"pending connection for closed channel"); |
|
174 |
||
175 |
/* Run some more finishConnect tests */ |
|
176 |
||
177 |
/* TEST 6: NoConnectionPendingException */ |
|
178 |
channel = SctpChannel.open(); |
|
179 |
try { |
|
180 |
channel.finishConnect(); |
|
181 |
fail("should have thrown NoConnectionPendingException"); |
|
182 |
} catch (NoConnectionPendingException unused) { |
|
183 |
pass(); |
|
184 |
} catch (IOException ioe) { |
|
185 |
unexpected(ioe); |
|
186 |
} |
|
187 |
||
188 |
/* TEST 7: ClosedChannelException */ |
|
189 |
channel.close(); |
|
190 |
final SctpChannel cceChannel = channel; |
|
191 |
testCCE(new Callable<Void>() { |
|
192 |
public Void call() throws IOException { |
|
193 |
cceChannel.finishConnect(); return null; } }); |
|
4678
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
194 |
|
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
195 |
/* TEST 8: IOException: Connection refused. Exercises handleSocketError. |
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
196 |
* Assumption: no sctp socket listening on 3456 */ |
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
197 |
SocketAddress addr = new InetSocketAddress("localhost", 3456); |
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
198 |
channel = SctpChannel.open(); |
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
199 |
try { |
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
200 |
channel.connect(addr); |
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
201 |
fail("should have thrown ConnectException: Connection refused"); |
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
202 |
} catch (IOException ioe) { |
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
203 |
pass(); |
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
204 |
} |
99fdf34405de
6917317: (sctp) Remove dependency on handleSocketError
chegar
parents:
3072
diff
changeset
|
205 |
|
2542 | 206 |
} catch (IOException ioe) { |
207 |
unexpected(ioe); |
|
208 |
} finally { |
|
209 |
try { if (channel != null) channel.close(); } |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
210 |
catch (IOException unused) {} |
2542 | 211 |
} |
212 |
} |
|
213 |
||
214 |
class UnsupportedSocketAddress extends SocketAddress { } |
|
215 |
||
216 |
void testCCE(Callable callable) { |
|
217 |
try { |
|
218 |
callable.call(); |
|
219 |
fail("should have thrown ClosedChannelException"); |
|
220 |
} catch (ClosedChannelException cce) { |
|
221 |
pass(); |
|
222 |
} catch (Exception ioe) { |
|
223 |
unexpected(ioe); |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
//--------------------- Infrastructure --------------------------- |
|
228 |
boolean debug = true; |
|
229 |
volatile int passed = 0, failed = 0; |
|
230 |
void pass() {passed++;} |
|
231 |
void fail() {failed++; Thread.dumpStack();} |
|
232 |
void fail(String msg) {System.err.println(msg); fail();} |
|
233 |
void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
234 |
void check(boolean cond) {if (cond) pass(); else fail();} |
|
235 |
void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);} |
|
236 |
void debug(String message) {if(debug) { System.out.println(message); } } |
|
237 |
public static void main(String[] args) throws Throwable { |
|
238 |
Class<?> k = new Object(){}.getClass().getEnclosingClass(); |
|
239 |
try {k.getMethod("instanceMain",String[].class) |
|
240 |
.invoke( k.newInstance(), (Object) args);} |
|
241 |
catch (Throwable e) {throw e.getCause();}} |
|
242 |
public void instanceMain(String[] args) throws Throwable { |
|
243 |
try {test(args);} catch (Throwable t) {unexpected(t);} |
|
244 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
245 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
246 |
||
247 |
} |