author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 4675 | 68f287672813 |
child 7545 | 409091945418 |
permissions | -rw-r--r-- |
2542 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2009, 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.io.IOException; |
|
31 |
import java.util.Set; |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
32 |
import java.net.InetSocketAddress; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
33 |
import java.net.SocketAddress; |
2542 | 34 |
import java.util.List; |
35 |
import java.util.Arrays; |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
36 |
import java.util.Iterator; |
2542 | 37 |
import java.nio.channels.ClosedChannelException; |
38 |
import com.sun.nio.sctp.SctpChannel; |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
39 |
import com.sun.nio.sctp.SctpServerChannel; |
2542 | 40 |
import com.sun.nio.sctp.SctpSocketOption; |
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
41 |
import java.security.AccessController; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
42 |
import sun.security.action.GetPropertyAction; |
2542 | 43 |
import static com.sun.nio.sctp.SctpStandardSocketOption.*; |
44 |
import static java.lang.System.out; |
|
45 |
||
46 |
public class SocketOptionTests { |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
47 |
final String osName = AccessController.doPrivileged( |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
48 |
new GetPropertyAction("os.name")); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
49 |
|
2542 | 50 |
<T> void checkOption(SctpChannel sc, SctpSocketOption<T> name, |
51 |
T expectedValue) throws IOException { |
|
52 |
T value = sc.getOption(name); |
|
53 |
check(value.equals(expectedValue), name + ": value (" + value + |
|
54 |
") not as expected (" + expectedValue + ")"); |
|
55 |
} |
|
56 |
||
57 |
<T> void optionalSupport(SctpChannel sc, SctpSocketOption<T> name, |
|
58 |
T value) { |
|
59 |
try { |
|
60 |
sc.setOption(name, value); |
|
61 |
checkOption(sc, name, value); |
|
62 |
} catch (IOException e) { |
|
63 |
/* Informational only, not all options have native support */ |
|
64 |
out.println(name + " not supported. " + e); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
void test(String[] args) { |
|
69 |
if (!Util.isSCTPSupported()) { |
|
70 |
out.println("SCTP protocol is not supported"); |
|
71 |
out.println("Test cannot be run"); |
|
72 |
return; |
|
73 |
} |
|
74 |
||
75 |
try { |
|
76 |
SctpChannel sc = SctpChannel.open(); |
|
77 |
||
78 |
/* check supported options */ |
|
79 |
Set<SctpSocketOption<?>> options = sc.supportedOptions(); |
|
80 |
List<? extends SctpSocketOption<?>> expected = Arrays.<SctpSocketOption<?>>asList( |
|
81 |
SCTP_DISABLE_FRAGMENTS, SCTP_EXPLICIT_COMPLETE, |
|
82 |
SCTP_FRAGMENT_INTERLEAVE, SCTP_INIT_MAXSTREAMS, |
|
83 |
SCTP_NODELAY, SCTP_PRIMARY_ADDR, SCTP_SET_PEER_PRIMARY_ADDR, |
|
84 |
SO_SNDBUF, SO_RCVBUF, SO_LINGER); |
|
85 |
||
86 |
for (SctpSocketOption opt: expected) { |
|
87 |
if (!options.contains(opt)) |
|
88 |
fail(opt.name() + " should be supported"); |
|
89 |
} |
|
90 |
||
91 |
InitMaxStreams streams = InitMaxStreams.create(1024, 1024); |
|
92 |
sc.setOption(SCTP_INIT_MAXSTREAMS, streams); |
|
93 |
checkOption(sc, SCTP_INIT_MAXSTREAMS, streams); |
|
94 |
streams = sc.getOption(SCTP_INIT_MAXSTREAMS); |
|
95 |
check(streams.maxInStreams() == 1024, "Max in streams: value: " |
|
96 |
+ streams.maxInStreams() + ", expected 1024 "); |
|
97 |
check(streams.maxOutStreams() == 1024, "Max out streams: value: " |
|
98 |
+ streams.maxOutStreams() + ", expected 1024 "); |
|
99 |
||
100 |
optionalSupport(sc, SCTP_DISABLE_FRAGMENTS, true); |
|
101 |
optionalSupport(sc, SCTP_EXPLICIT_COMPLETE, true); |
|
102 |
optionalSupport(sc, SCTP_FRAGMENT_INTERLEAVE, 1); |
|
103 |
||
104 |
sc.setOption(SCTP_NODELAY, true); |
|
105 |
checkOption(sc, SCTP_NODELAY, true); |
|
106 |
sc.setOption(SO_SNDBUF, 16*1024); |
|
4675
68f287672813
6916922: (sctp) SO_RCVBUF & SO_SNDBUF returns twice the value set
chegar
parents:
3072
diff
changeset
|
107 |
checkOption(sc, SO_SNDBUF, 16*1024); |
2542 | 108 |
sc.setOption(SO_RCVBUF, 16*1024); |
4675
68f287672813
6916922: (sctp) SO_RCVBUF & SO_SNDBUF returns twice the value set
chegar
parents:
3072
diff
changeset
|
109 |
checkOption(sc, SO_RCVBUF, 16*1024); |
2542 | 110 |
checkOption(sc, SO_LINGER, -1); /* default should be negative */ |
111 |
sc.setOption(SO_LINGER, 2000); |
|
112 |
checkOption(sc, SO_LINGER, 2000); |
|
113 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
114 |
/* SCTP_PRIMARY_ADDR */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
115 |
sctpPrimaryAddr(); |
2542 | 116 |
|
117 |
/* NullPointerException */ |
|
118 |
try { |
|
119 |
sc.setOption(null, "value"); |
|
120 |
fail("NullPointerException not thrown for setOption"); |
|
121 |
} catch (NullPointerException unused) { |
|
122 |
pass(); |
|
123 |
} |
|
124 |
try { |
|
125 |
sc.getOption(null); |
|
126 |
fail("NullPointerException not thrown for getOption"); |
|
127 |
} catch (NullPointerException unused) { |
|
128 |
pass(); |
|
129 |
} |
|
130 |
||
131 |
/* ClosedChannelException */ |
|
132 |
sc.close(); |
|
133 |
try { |
|
134 |
sc.setOption(SCTP_INIT_MAXSTREAMS, streams); |
|
135 |
fail("ClosedChannelException not thrown"); |
|
136 |
} catch (ClosedChannelException unused) { |
|
137 |
pass(); |
|
138 |
} |
|
139 |
} catch (IOException ioe) { |
|
140 |
unexpected(ioe); |
|
141 |
} |
|
142 |
} |
|
143 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
144 |
/* SCTP_PRIMARY_ADDR */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
145 |
void sctpPrimaryAddr() throws IOException { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
146 |
SocketAddress addrToSet = null;; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
147 |
|
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
148 |
System.out.println("TESTING SCTP_PRIMARY_ADDR"); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
149 |
SctpChannel sc = SctpChannel.open(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
150 |
SctpServerChannel ssc = SctpServerChannel.open().bind(null); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
151 |
Set<SocketAddress> addrs = ssc.getAllLocalAddresses(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
152 |
if (addrs.isEmpty()) |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
153 |
debug("addrs should not be empty"); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
154 |
debug("Listening on " + addrs); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
155 |
|
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
156 |
InetSocketAddress serverAddr = (InetSocketAddress) addrs.iterator().next(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
157 |
debug("connecting to " + serverAddr); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
158 |
sc.connect(serverAddr); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
159 |
SctpChannel peerChannel = ssc.accept(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
160 |
ssc.close(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
161 |
Set<SocketAddress> peerAddrs = peerChannel.getAllLocalAddresses(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
162 |
debug("Peer local Addresses: "); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
163 |
for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
164 |
InetSocketAddress addr = (InetSocketAddress)it.next(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
165 |
debug("\t" + addr); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
166 |
addrToSet = addr; // any of the peer addresses will do! |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
167 |
} |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
168 |
|
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
169 |
/* retrieval of SCTP_PRIMARY_ADDR is not supported on Solaris */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
170 |
if ("SunOS".equals(osName)) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
171 |
/* For now do not set this option. There is a bug on Solaris 10 pre Update 5 |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
172 |
* where setting this option returns Invalid argument */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
173 |
//debug("Set SCTP_PRIMARY_ADDR with " + addrToSet); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
174 |
//sc.setOption(SCTP_PRIMARY_ADDR, addrToSet); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
175 |
return; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
176 |
} else { /* Linux */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
177 |
SocketAddress primaryAddr = sc.getOption(SCTP_PRIMARY_ADDR); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
178 |
System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
179 |
/* Verify that this is one of the peer addresses */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
180 |
boolean found = false; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
181 |
addrToSet = primaryAddr; // may not have more than one addr |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
182 |
for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
183 |
InetSocketAddress addr = (InetSocketAddress)it.next(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
184 |
if (addr.equals(primaryAddr)) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
185 |
found = true; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
186 |
} |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
187 |
addrToSet = addr; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
188 |
} |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
189 |
check(found, "SCTP_PRIMARY_ADDR returned bogus address!"); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
190 |
|
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
191 |
sc.setOption(SCTP_PRIMARY_ADDR, addrToSet); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
192 |
System.out.println("SCTP_PRIMARY_ADDR set to: " + addrToSet); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
193 |
primaryAddr = sc.getOption(SCTP_PRIMARY_ADDR); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
194 |
System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
195 |
check(addrToSet.equals(primaryAddr),"SCTP_PRIMARY_ADDR not set correctly"); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
196 |
} |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
197 |
} |
2542 | 198 |
//--------------------- Infrastructure --------------------------- |
199 |
boolean debug = true; |
|
200 |
volatile int passed = 0, failed = 0; |
|
201 |
void pass() {passed++;} |
|
202 |
void fail() {failed++; Thread.dumpStack();} |
|
203 |
void fail(String msg) {System.err.println(msg); fail();} |
|
204 |
void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
205 |
void check(boolean cond) {if (cond) pass(); else fail();} |
|
206 |
void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);} |
|
207 |
void debug(String message) {if(debug) { System.out.println(message); } } |
|
208 |
public static void main(String[] args) throws Throwable { |
|
209 |
Class<?> k = new Object(){}.getClass().getEnclosingClass(); |
|
210 |
try {k.getMethod("instanceMain",String[].class) |
|
211 |
.invoke( k.newInstance(), (Object) args);} |
|
212 |
catch (Throwable e) {throw e.getCause();}} |
|
213 |
public void instanceMain(String[] args) throws Throwable { |
|
214 |
try {test(args);} catch (Throwable t) {unexpected(t);} |
|
215 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
216 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
217 |
} |