Path: blob/master/test/jdk/com/sun/nio/sctp/SctpChannel/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.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 java.security.PrivilegedAction;42import static com.sun.nio.sctp.SctpStandardSocketOptions.*;43import static java.lang.System.out;4445public class SocketOptionTests {46final String osName = AccessController.doPrivileged(47(PrivilegedAction<String>)() -> System.getProperty("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 (SctpChannel sc = SctpChannel.open()) {7576/* check supported options */77Set<SctpSocketOption<?>> options = sc.supportedOptions();78List<? extends SctpSocketOption<?>> expected = Arrays.<SctpSocketOption<?>>asList(79SCTP_DISABLE_FRAGMENTS, SCTP_EXPLICIT_COMPLETE,80SCTP_FRAGMENT_INTERLEAVE, SCTP_INIT_MAXSTREAMS,81SCTP_NODELAY, SCTP_PRIMARY_ADDR, SCTP_SET_PEER_PRIMARY_ADDR,82SO_SNDBUF, SO_RCVBUF, SO_LINGER);8384for (SctpSocketOption opt: expected) {85if (!options.contains(opt))86fail(opt.name() + " should be supported");87}8889InitMaxStreams streams = InitMaxStreams.create(1024, 1024);90sc.setOption(SCTP_INIT_MAXSTREAMS, streams);91checkOption(sc, SCTP_INIT_MAXSTREAMS, streams);92streams = sc.getOption(SCTP_INIT_MAXSTREAMS);93check(streams.maxInStreams() == 1024, "Max in streams: value: "94+ streams.maxInStreams() + ", expected 1024 ");95check(streams.maxOutStreams() == 1024, "Max out streams: value: "96+ streams.maxOutStreams() + ", expected 1024 ");9798optionalSupport(sc, SCTP_DISABLE_FRAGMENTS, true);99optionalSupport(sc, SCTP_EXPLICIT_COMPLETE, true);100optionalSupport(sc, SCTP_FRAGMENT_INTERLEAVE, 1);101102sc.setOption(SCTP_NODELAY, true);103checkOption(sc, SCTP_NODELAY, true);104sc.setOption(SO_SNDBUF, 16*1024);105checkOption(sc, SO_SNDBUF, 16*1024);106sc.setOption(SO_RCVBUF, 16*1024);107checkOption(sc, SO_RCVBUF, 16*1024);108checkOption(sc, SO_LINGER, -1); /* default should be negative */109sc.setOption(SO_LINGER, 2000);110checkOption(sc, SO_LINGER, 2000);111112/* SCTP_PRIMARY_ADDR */113sctpPrimaryAddr();114115/* NullPointerException */116try {117sc.setOption(null, "value");118fail("NullPointerException not thrown for setOption");119} catch (NullPointerException unused) {120pass();121}122try {123sc.getOption(null);124fail("NullPointerException not thrown for getOption");125} catch (NullPointerException unused) {126pass();127}128129/* ClosedChannelException */130sc.close();131try {132sc.setOption(SCTP_INIT_MAXSTREAMS, streams);133fail("ClosedChannelException not thrown");134} catch (ClosedChannelException unused) {135pass();136}137} catch (IOException ioe) {138unexpected(ioe);139}140}141142/* SCTP_PRIMARY_ADDR */143void sctpPrimaryAddr() throws IOException {144System.out.println("TESTING SCTP_PRIMARY_ADDR");145SctpChannel sc = SctpChannel.open();146SctpServerChannel ssc = SctpServerChannel.open().bind(null);147Set<SocketAddress> addrs = ssc.getAllLocalAddresses();148if (addrs.isEmpty())149debug("addrs should not be empty");150debug("Listening on " + addrs);151152InetSocketAddress serverAddr = (InetSocketAddress) addrs.iterator().next();153debug("connecting to " + serverAddr);154sc.connect(serverAddr);155SctpChannel peerChannel = ssc.accept();156ssc.close();157Set<SocketAddress> remoteAddresses = sc.getRemoteAddresses();158debug("Remote Addresses: ");159for (Iterator<SocketAddress> it = remoteAddresses.iterator(); it.hasNext(); ) {160InetSocketAddress addr = (InetSocketAddress)it.next();161debug("\t" + addr);162}163164SocketAddress primaryAddr = sc.getOption(SCTP_PRIMARY_ADDR);165System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);166/* Verify that this is one of the remote addresses */167check(remoteAddresses.contains(primaryAddr), "SCTP_PRIMARY_ADDR returned bogus address!");168169for (Iterator<SocketAddress> it = remoteAddresses.iterator(); it.hasNext(); ) {170InetSocketAddress addrToSet = (InetSocketAddress) it.next();171System.out.println("SCTP_PRIMARY_ADDR try set to: " + addrToSet);172sc.setOption(SCTP_PRIMARY_ADDR, addrToSet);173System.out.println("SCTP_PRIMARY_ADDR set to : " + addrToSet);174primaryAddr = sc.getOption(SCTP_PRIMARY_ADDR);175System.out.println("SCTP_PRIMARY_ADDR returned : " + primaryAddr);176check(addrToSet.equals(primaryAddr), "SCTP_PRIMARY_ADDR not set correctly");177}178sc.close();179peerChannel.close();180}181//--------------------- Infrastructure ---------------------------182boolean debug = true;183volatile int passed = 0, failed = 0;184void pass() {passed++;}185void fail() {failed++; Thread.dumpStack();}186void fail(String msg) {System.err.println(msg); fail();}187void unexpected(Throwable t) {failed++; t.printStackTrace();}188void check(boolean cond) {if (cond) pass(); else fail();}189void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}190void debug(String message) {if(debug) { System.out.println(message); } }191public static void main(String[] args) throws Throwable {192Class<?> k = new Object(){}.getClass().getEnclosingClass();193try {k.getMethod("instanceMain",String[].class)194.invoke( k.newInstance(), (Object) args);}195catch (Throwable e) {throw e.getCause();}}196public void instanceMain(String[] args) throws Throwable {197try {test(args);} catch (Throwable t) {unexpected(t);}198System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);199if (failed > 0) throw new AssertionError("Some tests failed");}200}201202203