1096N/A/*
3261N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
1096N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1096N/A *
1096N/A * This code is free software; you can redistribute it and/or modify it
1096N/A * under the terms of the GNU General Public License version 2 only, as
1096N/A * published by the Free Software Foundation.
1096N/A *
1096N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1096N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1096N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1096N/A * version 2 for more details (a copy is included in the LICENSE file that
1096N/A * accompanied this code).
1096N/A *
1096N/A * You should have received a copy of the GNU General Public License version
1096N/A * 2 along with this work; if not, write to the Free Software Foundation,
1096N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1096N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1096N/A */
1096N/A
1096N/A/* @test
1096N/A * @bug 4927640
1096N/A * @summary Tests the SCTP protocol implementation
1096N/A * @author chegar
1096N/A */
1096N/A
1096N/Aimport java.io.IOException;
1096N/Aimport java.util.Set;
1326N/Aimport java.net.InetSocketAddress;
1326N/Aimport java.net.SocketAddress;
1096N/Aimport java.util.List;
1096N/Aimport java.util.Arrays;
1326N/Aimport java.util.Iterator;
1096N/Aimport java.nio.channels.ClosedChannelException;
1096N/Aimport com.sun.nio.sctp.SctpChannel;
1326N/Aimport com.sun.nio.sctp.SctpServerChannel;
1096N/Aimport com.sun.nio.sctp.SctpSocketOption;
1326N/Aimport java.security.AccessController;
1326N/Aimport sun.security.action.GetPropertyAction;
4216N/Aimport static com.sun.nio.sctp.SctpStandardSocketOptions.*;
1096N/Aimport static java.lang.System.out;
1096N/A
1096N/Apublic class SocketOptionTests {
1326N/A final String osName = AccessController.doPrivileged(
1326N/A new GetPropertyAction("os.name"));
1326N/A
1096N/A <T> void checkOption(SctpChannel sc, SctpSocketOption<T> name,
1096N/A T expectedValue) throws IOException {
1096N/A T value = sc.getOption(name);
1096N/A check(value.equals(expectedValue), name + ": value (" + value +
1096N/A ") not as expected (" + expectedValue + ")");
1096N/A }
1096N/A
1096N/A <T> void optionalSupport(SctpChannel sc, SctpSocketOption<T> name,
1096N/A T value) {
1096N/A try {
1096N/A sc.setOption(name, value);
1096N/A checkOption(sc, name, value);
1096N/A } catch (IOException e) {
1096N/A /* Informational only, not all options have native support */
1096N/A out.println(name + " not supported. " + e);
1096N/A }
1096N/A }
1096N/A
1096N/A void test(String[] args) {
1096N/A if (!Util.isSCTPSupported()) {
1096N/A out.println("SCTP protocol is not supported");
1096N/A out.println("Test cannot be run");
1096N/A return;
1096N/A }
1096N/A
1096N/A try {
1096N/A SctpChannel sc = SctpChannel.open();
1096N/A
1096N/A /* check supported options */
1096N/A Set<SctpSocketOption<?>> options = sc.supportedOptions();
1096N/A List<? extends SctpSocketOption<?>> expected = Arrays.<SctpSocketOption<?>>asList(
1096N/A SCTP_DISABLE_FRAGMENTS, SCTP_EXPLICIT_COMPLETE,
1096N/A SCTP_FRAGMENT_INTERLEAVE, SCTP_INIT_MAXSTREAMS,
1096N/A SCTP_NODELAY, SCTP_PRIMARY_ADDR, SCTP_SET_PEER_PRIMARY_ADDR,
1096N/A SO_SNDBUF, SO_RCVBUF, SO_LINGER);
1096N/A
1096N/A for (SctpSocketOption opt: expected) {
1096N/A if (!options.contains(opt))
1096N/A fail(opt.name() + " should be supported");
1096N/A }
1096N/A
1096N/A InitMaxStreams streams = InitMaxStreams.create(1024, 1024);
1096N/A sc.setOption(SCTP_INIT_MAXSTREAMS, streams);
1096N/A checkOption(sc, SCTP_INIT_MAXSTREAMS, streams);
1096N/A streams = sc.getOption(SCTP_INIT_MAXSTREAMS);
1096N/A check(streams.maxInStreams() == 1024, "Max in streams: value: "
1096N/A + streams.maxInStreams() + ", expected 1024 ");
1096N/A check(streams.maxOutStreams() == 1024, "Max out streams: value: "
1096N/A + streams.maxOutStreams() + ", expected 1024 ");
1096N/A
1096N/A optionalSupport(sc, SCTP_DISABLE_FRAGMENTS, true);
1096N/A optionalSupport(sc, SCTP_EXPLICIT_COMPLETE, true);
1096N/A optionalSupport(sc, SCTP_FRAGMENT_INTERLEAVE, 1);
1096N/A
1096N/A sc.setOption(SCTP_NODELAY, true);
1096N/A checkOption(sc, SCTP_NODELAY, true);
1096N/A sc.setOption(SO_SNDBUF, 16*1024);
2090N/A checkOption(sc, SO_SNDBUF, 16*1024);
1096N/A sc.setOption(SO_RCVBUF, 16*1024);
2090N/A checkOption(sc, SO_RCVBUF, 16*1024);
1096N/A checkOption(sc, SO_LINGER, -1); /* default should be negative */
1096N/A sc.setOption(SO_LINGER, 2000);
1096N/A checkOption(sc, SO_LINGER, 2000);
1096N/A
1326N/A /* SCTP_PRIMARY_ADDR */
1326N/A sctpPrimaryAddr();
1096N/A
1096N/A /* NullPointerException */
1096N/A try {
1096N/A sc.setOption(null, "value");
1096N/A fail("NullPointerException not thrown for setOption");
1096N/A } catch (NullPointerException unused) {
1096N/A pass();
1096N/A }
1096N/A try {
1096N/A sc.getOption(null);
1096N/A fail("NullPointerException not thrown for getOption");
1096N/A } catch (NullPointerException unused) {
1096N/A pass();
1096N/A }
1096N/A
1096N/A /* ClosedChannelException */
1096N/A sc.close();
1096N/A try {
1096N/A sc.setOption(SCTP_INIT_MAXSTREAMS, streams);
1096N/A fail("ClosedChannelException not thrown");
1096N/A } catch (ClosedChannelException unused) {
1096N/A pass();
1096N/A }
1096N/A } catch (IOException ioe) {
1096N/A unexpected(ioe);
1096N/A }
1096N/A }
1096N/A
1326N/A /* SCTP_PRIMARY_ADDR */
1326N/A void sctpPrimaryAddr() throws IOException {
1326N/A SocketAddress addrToSet = null;;
1326N/A
1326N/A System.out.println("TESTING SCTP_PRIMARY_ADDR");
1326N/A SctpChannel sc = SctpChannel.open();
1326N/A SctpServerChannel ssc = SctpServerChannel.open().bind(null);
1326N/A Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
1326N/A if (addrs.isEmpty())
1326N/A debug("addrs should not be empty");
1326N/A debug("Listening on " + addrs);
1326N/A
1326N/A InetSocketAddress serverAddr = (InetSocketAddress) addrs.iterator().next();
1326N/A debug("connecting to " + serverAddr);
1326N/A sc.connect(serverAddr);
1326N/A SctpChannel peerChannel = ssc.accept();
1326N/A ssc.close();
1326N/A Set<SocketAddress> peerAddrs = peerChannel.getAllLocalAddresses();
1326N/A debug("Peer local Addresses: ");
1326N/A for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {
1326N/A InetSocketAddress addr = (InetSocketAddress)it.next();
1326N/A debug("\t" + addr);
1326N/A addrToSet = addr; // any of the peer addresses will do!
1326N/A }
1326N/A
1326N/A /* retrieval of SCTP_PRIMARY_ADDR is not supported on Solaris */
1326N/A if ("SunOS".equals(osName)) {
1326N/A /* For now do not set this option. There is a bug on Solaris 10 pre Update 5
1326N/A * where setting this option returns Invalid argument */
1326N/A //debug("Set SCTP_PRIMARY_ADDR with " + addrToSet);
1326N/A //sc.setOption(SCTP_PRIMARY_ADDR, addrToSet);
1326N/A return;
1326N/A } else { /* Linux */
1326N/A SocketAddress primaryAddr = sc.getOption(SCTP_PRIMARY_ADDR);
1326N/A System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);
1326N/A /* Verify that this is one of the peer addresses */
1326N/A boolean found = false;
1326N/A addrToSet = primaryAddr; // may not have more than one addr
1326N/A for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {
1326N/A InetSocketAddress addr = (InetSocketAddress)it.next();
1326N/A if (addr.equals(primaryAddr)) {
1326N/A found = true;
1326N/A }
1326N/A addrToSet = addr;
1326N/A }
1326N/A check(found, "SCTP_PRIMARY_ADDR returned bogus address!");
1326N/A
3230N/A System.out.println("SCTP_PRIMARY_ADDR try set to: " + addrToSet);
1326N/A sc.setOption(SCTP_PRIMARY_ADDR, addrToSet);
1326N/A System.out.println("SCTP_PRIMARY_ADDR set to: " + addrToSet);
1326N/A primaryAddr = sc.getOption(SCTP_PRIMARY_ADDR);
1326N/A System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);
1326N/A check(addrToSet.equals(primaryAddr),"SCTP_PRIMARY_ADDR not set correctly");
1326N/A }
1326N/A }
1096N/A //--------------------- Infrastructure ---------------------------
1096N/A boolean debug = true;
1096N/A volatile int passed = 0, failed = 0;
1096N/A void pass() {passed++;}
1096N/A void fail() {failed++; Thread.dumpStack();}
1096N/A void fail(String msg) {System.err.println(msg); fail();}
1096N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
1096N/A void check(boolean cond) {if (cond) pass(); else fail();}
1096N/A void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
1096N/A void debug(String message) {if(debug) { System.out.println(message); } }
1096N/A public static void main(String[] args) throws Throwable {
1096N/A Class<?> k = new Object(){}.getClass().getEnclosingClass();
1096N/A try {k.getMethod("instanceMain",String[].class)
1096N/A .invoke( k.newInstance(), (Object) args);}
1096N/A catch (Throwable e) {throw e.getCause();}}
1096N/A public void instanceMain(String[] args) throws Throwable {
1096N/A try {test(args);} catch (Throwable t) {unexpected(t);}
1096N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1096N/A if (failed > 0) throw new AssertionError("Some tests failed");}
1096N/A}