Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/nio/sctp/SctpChannel/Bind.java
38867 views
/*1* Copyright (c) 2009, 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.net.*;30import java.io.*;31import java.util.List;32import java.util.Set;33import java.util.Iterator;34import java.nio.ByteBuffer;35import java.nio.channels.AlreadyBoundException;36import java.nio.channels.AlreadyConnectedException;37import java.nio.channels.ClosedChannelException;38import java.nio.channels.UnsupportedAddressTypeException;39import com.sun.nio.sctp.AssociationChangeNotification;40import com.sun.nio.sctp.AbstractNotificationHandler;41import com.sun.nio.sctp.HandlerResult;42import com.sun.nio.sctp.IllegalUnbindException;43import com.sun.nio.sctp.MessageInfo;44import com.sun.nio.sctp.PeerAddressChangeNotification;45import com.sun.nio.sctp.SctpChannel;46import com.sun.nio.sctp.SctpServerChannel;47import com.sun.nio.sctp.ShutdownNotification;48import static java.lang.System.out;4950/**51* Tests bind, bindAddress, unbindAddress, getLocalAddress, and52* getAllLocalAddresses.53*/54public class Bind {55void test(String[] args) {56if (!Util.isSCTPSupported()) {57out.println("SCTP protocol is not supported");58out.println("Test cannot be run");59return;60}6162/* Simply bind tests */63testBind();6465/* Test unconnected */66testBindUnbind(false);6768/* Test connected */69/* Adding/Removing addresses from a connected association is optional.70* This test can be run on systems that support dynamic address71* reconfiguration */72//testBindUnbind(true);73}7475void testBind() {76SctpChannel channel = null;77try {78channel = SctpChannel.open();7980/* TEST 1: empty set if channel is not bound */81check(channel.getAllLocalAddresses().isEmpty(),82"getAllLocalAddresses returned non empty set for unbound channel");8384/* TEST 2: null to bind the channel to an automatically assigned85* socket address */86channel.bind(null);8788/* TEST 3: non empty set if the channel is bound */89check(!channel.getAllLocalAddresses().isEmpty(),90"getAllLocalAddresses returned empty set for bound channel");91debug("getAllLocalAddresses on channel bound to the wildcard:\n"92+ channel.getAllLocalAddresses());9394/* TEST 4: AlreadyBoundException if this channel is already bound */95try { channel.bind(null); }96catch (AlreadyBoundException unused) { pass(); }97catch (IOException ioe) { unexpected(ioe); }9899/* TEST 5: UnsupportedAddressTypeException */100try {101channel.close(); /* open a new unbound channel for test */102channel = SctpChannel.open();103channel.bind(new UnsupportedSocketAddress());104fail("UnsupportedSocketAddress expected");105} catch (UnsupportedAddressTypeException unused) { pass();106} catch (IOException ioe) { unexpected(ioe); }107108/* TEST 6: AlreadyConnectedException */109try {110channel.close(); /* open a new unbound channel for test */111channel = SctpChannel.open();112connectChannel(channel);113channel.bind(null);114fail("AlreadyConnectedException expected");115} catch (AlreadyConnectedException unused) { pass();116} catch (IOException ioe) { unexpected(ioe); }117118/* TEST 7: ClosedChannelException - If this channel is closed */119try {120channel.close(); /* open a new unbound channel for test */121channel = SctpChannel.open();122channel.close();123channel.bind(null);124fail("ClosedChannelException expected");125} catch (ClosedChannelException unused) { pass();126} catch (IOException ioe) { unexpected(ioe); }127128/* TEST 8: ClosedChannelException if channel is closed */129try {130channel.getAllLocalAddresses();131fail("should have thrown ClosedChannelException");132} catch (ClosedChannelException cce) {133pass();134} catch (Exception ioe) {135unexpected(ioe);136}137} catch (IOException ioe) {138unexpected(ioe);139} finally {140try { channel.close(); }141catch (IOException ioe) { unexpected(ioe); }142}143}144145void testBindUnbind(boolean connected) {146SctpChannel channel = null;147SctpChannel peerChannel = null;148149debug("testBindUnbind, connected: " + connected);150try {151channel = SctpChannel.open();152153List<InetAddress> addresses = Util.getAddresses(true, false);154Iterator iterator = addresses.iterator();155InetSocketAddress a = new InetSocketAddress((InetAddress)iterator.next(), 0);156debug("channel.bind( " + a + ")");157channel.bind(a);158while (iterator.hasNext()) {159InetAddress ia = (InetAddress)iterator.next();160debug("channel.bindAddress(" + ia + ")");161channel.bindAddress(ia);162}163if (debug) {Util.dumpAddresses(channel, out);}164165if (connected) {166/* Test with connected channel */167peerChannel = connectChannel(channel);168}169170/* TEST 1: bind/unbindAddresses on the system addresses */171debug("bind/unbindAddresses on the system addresses");172List<InetAddress> addrs = Util.getAddresses(true, false);173for (InetAddress addr : addrs) {174try {175debug("unbindAddress: " + addr);176check(boundAddress(channel, addr), "trying to remove address that is not bound");177channel.unbindAddress(addr);178if (debug) {Util.dumpAddresses(channel, out);}179check(!boundAddress(channel, addr), "address was not removed");180181debug("bindAddress: " + addr);182channel.bindAddress(addr);183if (debug) {Util.dumpAddresses(channel, out);}184check(boundAddress(channel, addr), "address is not bound");185} catch (IOException ioe) {186unexpected(ioe);187}188}189190/* TEST 2: bindAddress - already bound address. */191InetAddress againAddress = addrs.get(0);192try {193debug("bind already bound address " + againAddress);194channel.bindAddress(againAddress);195} catch (AlreadyBoundException unused) {196debug("Caught AlreadyBoundException - OK");197pass();198} catch (IOException ioe) {199unexpected(ioe);200}201202/* TEST 3: bind non local address */203try {204InetAddress nla = InetAddress.getByName("123.123.123.123");205debug("bind non local address " + nla);206channel.bindAddress(nla);207} catch (IOException ioe) {208debug("Informative only " + ioe);209}210211/* TEST 4: unbind address that is not bound */212try {213debug("unbind address that is not bound " + againAddress);214/* remove address first then again */215channel.unbindAddress(againAddress);216channel.unbindAddress(againAddress);217} catch (IllegalUnbindException unused) {218debug("Caught IllegalUnbindException - OK");219pass();220} catch (IOException ioe) {221unexpected(ioe);222}223224/* TEST 5: unbind address that is not bound */225try {226InetAddress nla = InetAddress.getByName("123.123.123.123");227debug("unbind address that is not bound " + nla);228channel.unbindAddress(nla);229230} catch (IllegalUnbindException unused) {231debug("Caught IllegalUnbindException - OK");232pass();233} catch (IOException ioe) {234unexpected(ioe);235}236237if (connected) {238channel.shutdown();239240BindNotificationHandler handler = new BindNotificationHandler();241ByteBuffer buffer = ByteBuffer.allocate(10);242MessageInfo info;243while((info = peerChannel.receive(buffer, null, handler)) != null) {244if (info != null) {245if (info.bytes() == -1) {246debug("peerChannel Reached EOF");247break;248}249}250}251252while((info = channel.receive(buffer, null, handler)) != null) {253if (info != null) {254if (info.bytes() == -1) {255debug("channel Reached EOF");256break;257}258}259}260}261} catch (IOException ioe) {262ioe.printStackTrace();263} finally {264try { if (channel != null) channel.close(); }265catch (IOException ioe) { unexpected(ioe); }266}267}268269boolean boundAddress(SctpChannel channel, InetAddress addr)270throws IOException {271for (SocketAddress boundAddr : channel.getAllLocalAddresses()) {272if (((InetSocketAddress) boundAddr).getAddress().equals(addr))273return true;274}275return false;276}277278SctpChannel connectChannel(SctpChannel channel)279throws IOException {280debug("connecting channel...");281try {282SctpServerChannel ssc = SctpServerChannel.open();283ssc.bind(null);284Set<SocketAddress> addrs = ssc.getAllLocalAddresses();285Iterator<SocketAddress> iterator = addrs.iterator();286SocketAddress addr = iterator.next();287debug("using " + addr + "...");288channel.connect(addr);289SctpChannel peerChannel = ssc.accept();290ssc.close();291debug("connected");292return peerChannel;293} catch (IOException ioe) {294debug("Cannot connect channel");295unexpected(ioe);296throw ioe;297}298}299300class BindNotificationHandler extends AbstractNotificationHandler<Object>301{302@Override303public HandlerResult handleNotification(304AssociationChangeNotification acn, Object unused)305{306debug("AssociationChangeNotification: " + acn);307return HandlerResult.CONTINUE;308}309310@Override311public HandlerResult handleNotification(312PeerAddressChangeNotification pacn, Object unused)313{314debug("PeerAddressChangeNotification: " + pacn);315return HandlerResult.CONTINUE;316}317318@Override319public HandlerResult handleNotification(320ShutdownNotification sn, Object unused)321{322debug("ShutdownNotification: " + sn);323return HandlerResult.CONTINUE;324}325}326327class UnsupportedSocketAddress extends SocketAddress { }328329//--------------------- Infrastructure ---------------------------330boolean debug = true;331volatile int passed = 0, failed = 0;332void pass() {passed++;}333void fail() {failed++; Thread.dumpStack();}334void fail(String msg) {System.err.println(msg); fail();}335void unexpected(Throwable t) {failed++; t.printStackTrace();}336void check(boolean cond) {if (cond) pass(); else fail();}337void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}338void debug(String message) {if(debug) { System.out.println(message); } }339public static void main(String[] args) throws Throwable {340Class<?> k = new Object(){}.getClass().getEnclosingClass();341try {k.getMethod("instanceMain",String[].class)342.invoke( k.newInstance(), (Object) args);}343catch (Throwable e) {throw e.getCause();}}344public void instanceMain(String[] args) throws Throwable {345try {test(args);} catch (Throwable t) {unexpected(t);}346System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);347if (failed > 0) throw new AssertionError("Some tests failed");}348349}350351352