Path: blob/master/test/jdk/com/sun/nio/sctp/SctpMultiChannel/SocketOptionTests.java
41155 views
/*1* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 492764025* @summary Tests the SCTP protocol implementation26* @author chegar27*/2829import java.io.IOException;30import java.net.InetSocketAddress;31import java.net.SocketAddress;32import java.util.Iterator;33import java.util.Set;34import java.util.List;35import java.util.Arrays;36import java.nio.ByteBuffer;37import java.nio.channels.ClosedChannelException;38import com.sun.nio.sctp.AbstractNotificationHandler;39import com.sun.nio.sctp.Association;40import com.sun.nio.sctp.AssociationChangeNotification;41import com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent;42import com.sun.nio.sctp.HandlerResult;43import com.sun.nio.sctp.MessageInfo;44import com.sun.nio.sctp.SctpChannel;45import com.sun.nio.sctp.SctpMultiChannel;46import com.sun.nio.sctp.SctpServerChannel;47import com.sun.nio.sctp.SctpSocketOption;48import java.security.AccessController;49import java.security.PrivilegedAction;50import static com.sun.nio.sctp.SctpStandardSocketOptions.*;51import static java.lang.System.out;5253public class SocketOptionTests {54final String osName = AccessController.doPrivileged(55(PrivilegedAction<String>)() -> System.getProperty("os.name"));5657<T> void checkOption(SctpMultiChannel smc, SctpSocketOption<T> name,58T expectedValue) throws IOException {59T value = smc.getOption(name, null);60check(value.equals(expectedValue), name + ": value (" + value +61") not as expected (" + expectedValue + ")");62}6364<T> void optionalSupport(SctpMultiChannel smc, SctpSocketOption<T> name,65T value) {66try {67smc.setOption(name, value, null);68checkOption(smc, name, value);69} catch (IOException e) {70/* Informational only, not all options have native support */71out.println(name + " not supported. " + e);72}73}7475void test(String[] args) {76if (!Util.isSCTPSupported()) {77out.println("SCTP protocol is not supported");78out.println("Test cannot be run");79return;80}8182try {83SctpMultiChannel smc = SctpMultiChannel.open();8485/* check supported options */86Set<SctpSocketOption<?>> options = smc.supportedOptions();87List<? extends SctpSocketOption<?>> expected = Arrays.<SctpSocketOption<?>>asList(88SCTP_DISABLE_FRAGMENTS, SCTP_EXPLICIT_COMPLETE,89SCTP_FRAGMENT_INTERLEAVE, SCTP_INIT_MAXSTREAMS,90SCTP_NODELAY, SCTP_PRIMARY_ADDR, SCTP_SET_PEER_PRIMARY_ADDR,91SO_SNDBUF, SO_RCVBUF, SO_LINGER);9293for (SctpSocketOption opt: expected) {94if (!options.contains(opt))95fail(opt.name() + " should be supported");96}9798InitMaxStreams streams = InitMaxStreams.create(1024, 1024);99smc.setOption(SCTP_INIT_MAXSTREAMS, streams, null);100checkOption(smc, SCTP_INIT_MAXSTREAMS, streams);101streams = smc.getOption(SCTP_INIT_MAXSTREAMS, null);102check(streams.maxInStreams() == 1024, "Max in streams: value: "103+ streams.maxInStreams() + ", expected 1024 ");104check(streams.maxOutStreams() == 1024, "Max out streams: value: "105+ streams.maxOutStreams() + ", expected 1024 ");106107optionalSupport(smc, SCTP_DISABLE_FRAGMENTS, true);108optionalSupport(smc, SCTP_EXPLICIT_COMPLETE, true);109optionalSupport(smc, SCTP_FRAGMENT_INTERLEAVE, 1);110111smc.setOption(SCTP_NODELAY, true, null);112checkOption(smc, SCTP_NODELAY, true);113smc.setOption(SO_SNDBUF, 16*1024, null);114smc.setOption(SO_RCVBUF, 16*1024, null);115116checkOption(smc, SO_LINGER, -1); /* default should be negative */117118smc.setOption(SO_LINGER, 2000, null);119checkOption(smc, SO_LINGER, 2000);120121/* SCTP_PRIMARY_ADDR */122sctpPrimaryAddr();123124/* NullPointerException */125try {126smc.setOption(null, "value", null);127fail("NullPointerException not thrown for setOption");128} catch (NullPointerException unused) {129pass();130}131try {132smc.getOption(null, null);133fail("NullPointerException not thrown for getOption");134} catch (NullPointerException unused) {135pass();136}137138/* ClosedChannelException */139smc.close();140try {141smc.setOption(SCTP_INIT_MAXSTREAMS, streams, null);142fail("ClosedChannelException not thrown");143} catch (ClosedChannelException unused) {144pass();145}146} catch (IOException ioe) {147unexpected(ioe);148}149}150151/* SCTP_PRIMARY_ADDR */152void sctpPrimaryAddr() throws IOException {153ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);154155System.out.println("TESTING SCTP_PRIMARY_ADDR");156157/* create listening channel */158SctpServerChannel ssc = SctpServerChannel.open().bind(null);159Set<SocketAddress> addrs = ssc.getAllLocalAddresses();160if (addrs.isEmpty())161debug("addrs should not be empty");162163InetSocketAddress serverAddr = (InetSocketAddress) addrs.iterator().next();164165/* setup an association implicitly by sending a small message */166int streamNumber = 0;167debug("sending to " + serverAddr + " on stream number: " + streamNumber);168MessageInfo info = MessageInfo.createOutgoing(serverAddr, streamNumber);169buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));170buffer.flip();171172debug("sending small message: " + buffer);173SctpMultiChannel smc = SctpMultiChannel.open();174int sent = smc.send(buffer, info);175176/* Receive the COMM_UP */177buffer.clear();178SOTNotificationHandler handler = new SOTNotificationHandler();179info = smc.receive(buffer, null, handler);180check(handler.receivedCommUp(), "COMM_UP no received");181Set<Association> associations = smc.associations();182check(!associations.isEmpty(),"There should be some associations");183Association assoc = associations.iterator().next();184185SctpChannel peerChannel = ssc.accept();186ssc.close();187Set<SocketAddress> remoteAddresses = smc.getRemoteAddresses(assoc);188debug("Remote Addresses: ");189for (Iterator<SocketAddress> it = remoteAddresses.iterator(); it.hasNext(); ) {190InetSocketAddress addr = (InetSocketAddress)it.next();191debug("\t" + addr);192}193194SocketAddress primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);195System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);196/* Verify that this is one of the remote addresses */197check(remoteAddresses.contains(primaryAddr), "SCTP_PRIMARY_ADDR returned bogus address!");198199for (Iterator<SocketAddress> it = remoteAddresses.iterator(); it.hasNext(); ) {200InetSocketAddress addrToSet = (InetSocketAddress) it.next();201System.out.println("SCTP_PRIMARY_ADDR try set to: " + addrToSet);202smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);203System.out.println("SCTP_PRIMARY_ADDR set to : " + addrToSet);204primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);205System.out.println("SCTP_PRIMARY_ADDR returned : " + primaryAddr);206check(addrToSet.equals(primaryAddr), "SCTP_PRIMARY_ADDR not set correctly");207}208smc.close();209peerChannel.close();210}211212class SOTNotificationHandler extends AbstractNotificationHandler<Object>213{214boolean receivedCommUp; // false215216boolean receivedCommUp() {217return receivedCommUp;218}219220@Override221public HandlerResult handleNotification(222AssociationChangeNotification notification, Object attachment) {223AssocChangeEvent event = notification.event();224debug("AssociationChangeNotification");225debug(" Association: " + notification.association());226debug(" Event: " + event);227228if (event.equals(AssocChangeEvent.COMM_UP))229receivedCommUp = true;230231return HandlerResult.RETURN;232}233}234235//--------------------- Infrastructure ---------------------------236boolean debug = true;237volatile int passed = 0, failed = 0;238void pass() {passed++;}239void fail() {failed++; Thread.dumpStack();}240void fail(String msg) {System.err.println(msg); fail();}241void unexpected(Throwable t) {failed++; t.printStackTrace();}242void check(boolean cond) {if (cond) pass(); else fail();}243void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}244void debug(String message) {if(debug) { System.out.println(message); } }245public static void main(String[] args) throws Throwable {246Class<?> k = new Object(){}.getClass().getEnclosingClass();247try {k.getMethod("instanceMain",String[].class)248.invoke( k.newInstance(), (Object) args);}249catch (Throwable e) {throw e.getCause();}}250public void instanceMain(String[] args) throws Throwable {251try {test(args);} catch (Throwable t) {unexpected(t);}252System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);253if (failed > 0) throw new AssertionError("Some tests failed");}254}255256257