Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/nio/sctp/SctpMultiChannel/SocketOptionTests.java
38867 views
/*1* Copyright (c) 2009, 2011, 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 sun.security.action.GetPropertyAction;50import static com.sun.nio.sctp.SctpStandardSocketOptions.*;51import static java.lang.System.out;5253public class SocketOptionTests {54final String osName = AccessController.doPrivileged(55new GetPropertyAction("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 */117118/* Setting SO_LINGER not support for one-to-many on Solaris */119if (!"SunOS".equals(osName)) {120smc.setOption(SO_LINGER, 2000, null);121checkOption(smc, SO_LINGER, 2000);122}123124/* SCTP_PRIMARY_ADDR */125sctpPrimaryAddr();126127/* NullPointerException */128try {129smc.setOption(null, "value", null);130fail("NullPointerException not thrown for setOption");131} catch (NullPointerException unused) {132pass();133}134try {135smc.getOption(null, null);136fail("NullPointerException not thrown for getOption");137} catch (NullPointerException unused) {138pass();139}140141/* ClosedChannelException */142smc.close();143try {144smc.setOption(SCTP_INIT_MAXSTREAMS, streams, null);145fail("ClosedChannelException not thrown");146} catch (ClosedChannelException unused) {147pass();148}149} catch (IOException ioe) {150unexpected(ioe);151}152}153154/* SCTP_PRIMARY_ADDR */155void sctpPrimaryAddr() throws IOException {156SocketAddress addrToSet = null;157ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);158159System.out.println("TESTING SCTP_PRIMARY_ADDR");160161/* create listening channel */162SctpServerChannel ssc = SctpServerChannel.open().bind(null);163Set<SocketAddress> addrs = ssc.getAllLocalAddresses();164if (addrs.isEmpty())165debug("addrs should not be empty");166167InetSocketAddress serverAddr = (InetSocketAddress) addrs.iterator().next();168169/* setup an association implicitly by sending a small message */170int streamNumber = 0;171debug("sending to " + serverAddr + " on stream number: " + streamNumber);172MessageInfo info = MessageInfo.createOutgoing(serverAddr, streamNumber);173buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));174buffer.flip();175176debug("sending small message: " + buffer);177SctpMultiChannel smc = SctpMultiChannel.open();178int sent = smc.send(buffer, info);179180/* Receive the COMM_UP */181buffer.clear();182SOTNotificationHandler handler = new SOTNotificationHandler();183info = smc.receive(buffer, null, handler);184check(handler.receivedCommUp(), "COMM_UP no received");185Set<Association> associations = smc.associations();186check(!associations.isEmpty(),"There should be some associations");187Association assoc = associations.iterator().next();188189SctpChannel peerChannel = ssc.accept();190ssc.close();191Set<SocketAddress> peerAddrs = peerChannel.getAllLocalAddresses();192debug("Peer local Addresses: ");193for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {194InetSocketAddress addr = (InetSocketAddress)it.next();195debug("\t" + addr);196addrToSet = addr; // any of the peer addresses will do!197}198199/* retrieval of SCTP_PRIMARY_ADDR is not supported on Solaris */200if ("SunOS".equals(osName)) {201/* For now do not set this option. There is a bug on Solaris 10 pre Update 5202* where setting this option returns Invalid argument */203//debug("Set SCTP_PRIMARY_ADDR with " + addrToSet);204//smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);205return;206} else { /* Linux */207SocketAddress primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);208System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);209/* Verify that this is one of the peer addresses */210boolean found = false;211addrToSet = primaryAddr; // may not have more than one addr212for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {213InetSocketAddress addr = (InetSocketAddress)it.next();214if (addr.equals(primaryAddr)) {215found = true;216}217addrToSet = addr;218}219check(found, "SCTP_PRIMARY_ADDR returned bogus address!");220221System.out.println("Try SCTP_PRIMARY_ADDR set to: " + addrToSet);222smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);223System.out.println("SCTP_PRIMARY_ADDR set to: " + addrToSet);224primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);225System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);226check(addrToSet.equals(primaryAddr),"SCTP_PRIMARY_ADDR not set correctly");227}228}229230class SOTNotificationHandler extends AbstractNotificationHandler<Object>231{232boolean receivedCommUp; // false233234boolean receivedCommUp() {235return receivedCommUp;236}237238@Override239public HandlerResult handleNotification(240AssociationChangeNotification notification, Object attachment) {241AssocChangeEvent event = notification.event();242debug("AssociationChangeNotification");243debug(" Association: " + notification.association());244debug(" Event: " + event);245246if (event.equals(AssocChangeEvent.COMM_UP))247receivedCommUp = true;248249return HandlerResult.RETURN;250}251}252253//--------------------- Infrastructure ---------------------------254boolean debug = true;255volatile int passed = 0, failed = 0;256void pass() {passed++;}257void fail() {failed++; Thread.dumpStack();}258void fail(String msg) {System.err.println(msg); fail();}259void unexpected(Throwable t) {failed++; t.printStackTrace();}260void check(boolean cond) {if (cond) pass(); else fail();}261void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}262void debug(String message) {if(debug) { System.out.println(message); } }263public static void main(String[] args) throws Throwable {264Class<?> k = new Object(){}.getClass().getEnclosingClass();265try {k.getMethod("instanceMain",String[].class)266.invoke( k.newInstance(), (Object) args);}267catch (Throwable e) {throw e.getCause();}}268public void instanceMain(String[] args) throws Throwable {269try {test(args);} catch (Throwable t) {unexpected(t);}270System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);271if (failed > 0) throw new AssertionError("Some tests failed");}272}273274275