Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/nio/sctp/SctpChannel/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.util.Set;31import java.net.InetSocketAddress;32import java.net.SocketAddress;33import java.util.List;34import java.util.Arrays;35import java.util.Iterator;36import java.nio.channels.ClosedChannelException;37import com.sun.nio.sctp.SctpChannel;38import com.sun.nio.sctp.SctpServerChannel;39import com.sun.nio.sctp.SctpSocketOption;40import java.security.AccessController;41import sun.security.action.GetPropertyAction;42import static com.sun.nio.sctp.SctpStandardSocketOptions.*;43import static java.lang.System.out;4445public class SocketOptionTests {46final String osName = AccessController.doPrivileged(47new GetPropertyAction("os.name"));4849<T> void checkOption(SctpChannel sc, SctpSocketOption<T> name,50T expectedValue) throws IOException {51T value = sc.getOption(name);52check(value.equals(expectedValue), name + ": value (" + value +53") not as expected (" + expectedValue + ")");54}5556<T> void optionalSupport(SctpChannel sc, SctpSocketOption<T> name,57T value) {58try {59sc.setOption(name, value);60checkOption(sc, name, value);61} catch (IOException e) {62/* Informational only, not all options have native support */63out.println(name + " not supported. " + e);64}65}6667void test(String[] args) {68if (!Util.isSCTPSupported()) {69out.println("SCTP protocol is not supported");70out.println("Test cannot be run");71return;72}7374try {75SctpChannel sc = SctpChannel.open();7677/* check supported options */78Set<SctpSocketOption<?>> options = sc.supportedOptions();79List<? extends SctpSocketOption<?>> expected = Arrays.<SctpSocketOption<?>>asList(80SCTP_DISABLE_FRAGMENTS, SCTP_EXPLICIT_COMPLETE,81SCTP_FRAGMENT_INTERLEAVE, SCTP_INIT_MAXSTREAMS,82SCTP_NODELAY, SCTP_PRIMARY_ADDR, SCTP_SET_PEER_PRIMARY_ADDR,83SO_SNDBUF, SO_RCVBUF, SO_LINGER);8485for (SctpSocketOption opt: expected) {86if (!options.contains(opt))87fail(opt.name() + " should be supported");88}8990InitMaxStreams streams = InitMaxStreams.create(1024, 1024);91sc.setOption(SCTP_INIT_MAXSTREAMS, streams);92checkOption(sc, SCTP_INIT_MAXSTREAMS, streams);93streams = sc.getOption(SCTP_INIT_MAXSTREAMS);94check(streams.maxInStreams() == 1024, "Max in streams: value: "95+ streams.maxInStreams() + ", expected 1024 ");96check(streams.maxOutStreams() == 1024, "Max out streams: value: "97+ streams.maxOutStreams() + ", expected 1024 ");9899optionalSupport(sc, SCTP_DISABLE_FRAGMENTS, true);100optionalSupport(sc, SCTP_EXPLICIT_COMPLETE, true);101optionalSupport(sc, SCTP_FRAGMENT_INTERLEAVE, 1);102103sc.setOption(SCTP_NODELAY, true);104checkOption(sc, SCTP_NODELAY, true);105sc.setOption(SO_SNDBUF, 16*1024);106checkOption(sc, SO_SNDBUF, 16*1024);107sc.setOption(SO_RCVBUF, 16*1024);108checkOption(sc, SO_RCVBUF, 16*1024);109checkOption(sc, SO_LINGER, -1); /* default should be negative */110sc.setOption(SO_LINGER, 2000);111checkOption(sc, SO_LINGER, 2000);112113/* SCTP_PRIMARY_ADDR */114sctpPrimaryAddr();115116/* NullPointerException */117try {118sc.setOption(null, "value");119fail("NullPointerException not thrown for setOption");120} catch (NullPointerException unused) {121pass();122}123try {124sc.getOption(null);125fail("NullPointerException not thrown for getOption");126} catch (NullPointerException unused) {127pass();128}129130/* ClosedChannelException */131sc.close();132try {133sc.setOption(SCTP_INIT_MAXSTREAMS, streams);134fail("ClosedChannelException not thrown");135} catch (ClosedChannelException unused) {136pass();137}138} catch (IOException ioe) {139unexpected(ioe);140}141}142143/* SCTP_PRIMARY_ADDR */144void sctpPrimaryAddr() throws IOException {145SocketAddress addrToSet = null;;146147System.out.println("TESTING SCTP_PRIMARY_ADDR");148SctpChannel sc = SctpChannel.open();149SctpServerChannel ssc = SctpServerChannel.open().bind(null);150Set<SocketAddress> addrs = ssc.getAllLocalAddresses();151if (addrs.isEmpty())152debug("addrs should not be empty");153debug("Listening on " + addrs);154155InetSocketAddress serverAddr = (InetSocketAddress) addrs.iterator().next();156debug("connecting to " + serverAddr);157sc.connect(serverAddr);158SctpChannel peerChannel = ssc.accept();159ssc.close();160Set<SocketAddress> peerAddrs = peerChannel.getAllLocalAddresses();161debug("Peer local Addresses: ");162for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {163InetSocketAddress addr = (InetSocketAddress)it.next();164debug("\t" + addr);165addrToSet = addr; // any of the peer addresses will do!166}167168/* retrieval of SCTP_PRIMARY_ADDR is not supported on Solaris */169if ("SunOS".equals(osName)) {170/* For now do not set this option. There is a bug on Solaris 10 pre Update 5171* where setting this option returns Invalid argument */172//debug("Set SCTP_PRIMARY_ADDR with " + addrToSet);173//sc.setOption(SCTP_PRIMARY_ADDR, addrToSet);174return;175} else { /* Linux */176SocketAddress primaryAddr = sc.getOption(SCTP_PRIMARY_ADDR);177System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);178/* Verify that this is one of the peer addresses */179boolean found = false;180addrToSet = primaryAddr; // may not have more than one addr181for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {182InetSocketAddress addr = (InetSocketAddress)it.next();183if (addr.equals(primaryAddr)) {184found = true;185}186addrToSet = addr;187}188check(found, "SCTP_PRIMARY_ADDR returned bogus address!");189190System.out.println("SCTP_PRIMARY_ADDR try set to: " + addrToSet);191sc.setOption(SCTP_PRIMARY_ADDR, addrToSet);192System.out.println("SCTP_PRIMARY_ADDR set to: " + addrToSet);193primaryAddr = sc.getOption(SCTP_PRIMARY_ADDR);194System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);195check(addrToSet.equals(primaryAddr),"SCTP_PRIMARY_ADDR not set correctly");196}197}198//--------------------- Infrastructure ---------------------------199boolean debug = true;200volatile int passed = 0, failed = 0;201void pass() {passed++;}202void fail() {failed++; Thread.dumpStack();}203void fail(String msg) {System.err.println(msg); fail();}204void unexpected(Throwable t) {failed++; t.printStackTrace();}205void check(boolean cond) {if (cond) pass(); else fail();}206void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}207void debug(String message) {if(debug) { System.out.println(message); } }208public static void main(String[] args) throws Throwable {209Class<?> k = new Object(){}.getClass().getEnclosingClass();210try {k.getMethod("instanceMain",String[].class)211.invoke( k.newInstance(), (Object) args);}212catch (Throwable e) {throw e.getCause();}}213public void instanceMain(String[] args) throws Throwable {214try {test(args);} catch (Throwable t) {unexpected(t);}215System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);216if (failed > 0) throw new AssertionError("Some tests failed");}217}218219220