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.io.IOException;
|
|
31 |
import java.util.Set;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.Arrays;
|
|
34 |
import java.nio.channels.ClosedChannelException;
|
|
35 |
import com.sun.nio.sctp.SctpChannel;
|
|
36 |
import com.sun.nio.sctp.SctpSocketOption;
|
|
37 |
import static com.sun.nio.sctp.SctpStandardSocketOption.*;
|
|
38 |
import static java.lang.System.out;
|
|
39 |
|
|
40 |
public class SocketOptionTests {
|
|
41 |
<T> void checkOption(SctpChannel sc, SctpSocketOption<T> name,
|
|
42 |
T expectedValue) throws IOException {
|
|
43 |
T value = sc.getOption(name);
|
|
44 |
check(value.equals(expectedValue), name + ": value (" + value +
|
|
45 |
") not as expected (" + expectedValue + ")");
|
|
46 |
}
|
|
47 |
|
|
48 |
<T> void optionalSupport(SctpChannel sc, SctpSocketOption<T> name,
|
|
49 |
T value) {
|
|
50 |
try {
|
|
51 |
sc.setOption(name, value);
|
|
52 |
checkOption(sc, name, value);
|
|
53 |
} catch (IOException e) {
|
|
54 |
/* Informational only, not all options have native support */
|
|
55 |
out.println(name + " not supported. " + e);
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
void test(String[] args) {
|
|
60 |
if (!Util.isSCTPSupported()) {
|
|
61 |
out.println("SCTP protocol is not supported");
|
|
62 |
out.println("Test cannot be run");
|
|
63 |
return;
|
|
64 |
}
|
|
65 |
|
|
66 |
try {
|
|
67 |
SctpChannel sc = SctpChannel.open();
|
|
68 |
|
|
69 |
/* check supported options */
|
|
70 |
Set<SctpSocketOption<?>> options = sc.supportedOptions();
|
|
71 |
List<? extends SctpSocketOption<?>> expected = Arrays.<SctpSocketOption<?>>asList(
|
|
72 |
SCTP_DISABLE_FRAGMENTS, SCTP_EXPLICIT_COMPLETE,
|
|
73 |
SCTP_FRAGMENT_INTERLEAVE, SCTP_INIT_MAXSTREAMS,
|
|
74 |
SCTP_NODELAY, SCTP_PRIMARY_ADDR, SCTP_SET_PEER_PRIMARY_ADDR,
|
|
75 |
SO_SNDBUF, SO_RCVBUF, SO_LINGER);
|
|
76 |
|
|
77 |
for (SctpSocketOption opt: expected) {
|
|
78 |
if (!options.contains(opt))
|
|
79 |
fail(opt.name() + " should be supported");
|
|
80 |
}
|
|
81 |
|
|
82 |
InitMaxStreams streams = InitMaxStreams.create(1024, 1024);
|
|
83 |
sc.setOption(SCTP_INIT_MAXSTREAMS, streams);
|
|
84 |
checkOption(sc, SCTP_INIT_MAXSTREAMS, streams);
|
|
85 |
streams = sc.getOption(SCTP_INIT_MAXSTREAMS);
|
|
86 |
check(streams.maxInStreams() == 1024, "Max in streams: value: "
|
|
87 |
+ streams.maxInStreams() + ", expected 1024 ");
|
|
88 |
check(streams.maxOutStreams() == 1024, "Max out streams: value: "
|
|
89 |
+ streams.maxOutStreams() + ", expected 1024 ");
|
|
90 |
|
|
91 |
optionalSupport(sc, SCTP_DISABLE_FRAGMENTS, true);
|
|
92 |
optionalSupport(sc, SCTP_EXPLICIT_COMPLETE, true);
|
|
93 |
optionalSupport(sc, SCTP_FRAGMENT_INTERLEAVE, 1);
|
|
94 |
|
|
95 |
|
|
96 |
//TODO: SCTP_PRIMARY_ADDR
|
|
97 |
//sc.bind(null);
|
|
98 |
//connect
|
|
99 |
//InetSocketAddress addr = new InetSocketAddress(0);
|
|
100 |
//sc.setOption(SCTP_PRIMARY_ADDR, addr);
|
|
101 |
|
|
102 |
sc.setOption(SCTP_NODELAY, true);
|
|
103 |
checkOption(sc, SCTP_NODELAY, true);
|
|
104 |
sc.setOption(SO_SNDBUF, 16*1024);
|
|
105 |
sc.setOption(SO_RCVBUF, 16*1024);
|
|
106 |
checkOption(sc, SO_LINGER, -1); /* default should be negative */
|
|
107 |
sc.setOption(SO_LINGER, 2000);
|
|
108 |
checkOption(sc, SO_LINGER, 2000);
|
|
109 |
|
|
110 |
|
|
111 |
/* NullPointerException */
|
|
112 |
try {
|
|
113 |
sc.setOption(null, "value");
|
|
114 |
fail("NullPointerException not thrown for setOption");
|
|
115 |
} catch (NullPointerException unused) {
|
|
116 |
pass();
|
|
117 |
}
|
|
118 |
try {
|
|
119 |
sc.getOption(null);
|
|
120 |
fail("NullPointerException not thrown for getOption");
|
|
121 |
} catch (NullPointerException unused) {
|
|
122 |
pass();
|
|
123 |
}
|
|
124 |
|
|
125 |
/* ClosedChannelException */
|
|
126 |
sc.close();
|
|
127 |
try {
|
|
128 |
sc.setOption(SCTP_INIT_MAXSTREAMS, streams);
|
|
129 |
fail("ClosedChannelException not thrown");
|
|
130 |
} catch (ClosedChannelException unused) {
|
|
131 |
pass();
|
|
132 |
}
|
|
133 |
} catch (IOException ioe) {
|
|
134 |
unexpected(ioe);
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
//--------------------- Infrastructure ---------------------------
|
|
139 |
boolean debug = true;
|
|
140 |
volatile int passed = 0, failed = 0;
|
|
141 |
void pass() {passed++;}
|
|
142 |
void fail() {failed++; Thread.dumpStack();}
|
|
143 |
void fail(String msg) {System.err.println(msg); fail();}
|
|
144 |
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
|
145 |
void check(boolean cond) {if (cond) pass(); else fail();}
|
|
146 |
void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
|
|
147 |
void debug(String message) {if(debug) { System.out.println(message); } }
|
|
148 |
public static void main(String[] args) throws Throwable {
|
|
149 |
Class<?> k = new Object(){}.getClass().getEnclosingClass();
|
|
150 |
try {k.getMethod("instanceMain",String[].class)
|
|
151 |
.invoke( k.newInstance(), (Object) args);}
|
|
152 |
catch (Throwable e) {throw e.getCause();}}
|
|
153 |
public void instanceMain(String[] args) throws Throwable {
|
|
154 |
try {test(args);} catch (Throwable t) {unexpected(t);}
|
|
155 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
|
156 |
if (failed > 0) throw new AssertionError("Some tests failed");}
|
|
157 |
}
|