Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/nio/sctp/SctpChannel/Connect.java
38867 views
/*1* Copyright (c) 2009, 2010, 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.InetSocketAddress;30import java.net.SocketAddress;31import java.io.IOException;32import java.util.Set;33import java.util.concurrent.Callable;34import java.nio.channels.AlreadyConnectedException;35import java.nio.channels.ClosedChannelException;36import java.nio.channels.ConnectionPendingException;37import java.nio.channels.NoConnectionPendingException;38import java.nio.channels.UnresolvedAddressException;39import java.nio.channels.UnsupportedAddressTypeException;40import com.sun.nio.sctp.SctpChannel;41import com.sun.nio.sctp.SctpServerChannel;42import static java.lang.System.out;43import static java.lang.System.err;4445/**46* Tests connect, finishConnect, isConnectionPending,47* getRemoteAddresses and association.48*/49public class Connect {5051void test(String[] args) {52if (!Util.isSCTPSupported()) {53out.println("SCTP protocol is not supported");54out.println("Test cannot be run");55return;56}5758doTest();59}6061void doTest() {62SctpChannel channel = null;63SctpServerChannel ssc = null;6465try {66/* Create a server channel to connect to */67ssc = SctpServerChannel.open().bind(null);68Set<SocketAddress> addrs = ssc.getAllLocalAddresses();69if (addrs.isEmpty())70debug("addrs should not be empty");71final SocketAddress peerAddress = (InetSocketAddress) addrs.iterator().next();7273channel = SctpChannel.open();7475/* TEST 0.5 Verify default values for new/unconnected channel */76check(channel.getRemoteAddresses().isEmpty(),77"non empty set for unconnected channel");78check(channel.association() == null,79"non-null association for unconnected channel");80check(!channel.isConnectionPending(),81"should not have a connection pending");8283/* TEST 1: non-blocking connect */84channel.configureBlocking(false);85if (channel.connect(peerAddress) != true) {86debug("non-blocking connect did not immediately succeed");87check(channel.isConnectionPending(),88"should return true for isConnectionPending");89try {90channel.connect(peerAddress);91fail("should have thrown ConnectionPendingException");92} catch (ConnectionPendingException cpe) {93pass();94} catch (IOException ioe) {95unexpected(ioe);96}97channel.configureBlocking(true);98check(channel.finishConnect(),99"finishConnect should have returned true");100}101102ssc.accept();103ssc.close();104105/* TEST 1.5 Verify after connect */106check(!channel.getRemoteAddresses().isEmpty(),107"empty set for connected channel");108check(channel.association() != null,109"null association for connected channel");110check(!channel.isConnectionPending(),111"pending connection for connected channel");112113/* TEST 2: Verify AlreadyConnectedException thrown */114try {115channel.connect(peerAddress);116fail("should have thrown AlreadyConnectedException");117} catch (AlreadyConnectedException unused) {118pass();119} catch (IOException ioe) {120unexpected(ioe);121}122123/* TEST 2.5: Verify AlreadyConnectedException thrown */124try {125channel.connect(peerAddress, 5, 5);126fail("should have thrown AlreadyConnectedException");127} catch (AlreadyConnectedException unused) {128pass();129} catch (IOException ioe) {130unexpected(ioe);131}132133/* TEST 3: UnresolvedAddressException */134channel.close();135channel = SctpChannel.open();136InetSocketAddress unresolved =137InetSocketAddress.createUnresolved("xxyyzzabc", 4567);138try {139channel.connect(unresolved);140fail("should have thrown UnresolvedAddressException");141} catch (UnresolvedAddressException unused) {142pass();143} catch (IOException ioe) {144unexpected(ioe);145}146147/* TEST 4: UnsupportedAddressTypeException */148SocketAddress unsupported = new UnsupportedSocketAddress();149try {150channel.connect(unsupported);151fail("should have thrown UnsupportedAddressTypeException");152} catch (UnsupportedAddressTypeException unused) {153pass();154} catch (IOException ioe) {155unexpected(ioe);156}157158/* TEST 5: ClosedChannelException */159channel.close();160final SctpChannel closedChannel = channel;161testCCE(new Callable<Void>() {162public Void call() throws IOException {163closedChannel.connect(peerAddress); return null; } });164165/* TEST 5.5 getRemoteAddresses */166testCCE(new Callable<Void>() {167public Void call() throws IOException {168closedChannel.getRemoteAddresses(); return null; } });169testCCE(new Callable<Void>() {170public Void call() throws IOException {171closedChannel.association(); return null; } });172check(!channel.isConnectionPending(),173"pending connection for closed channel");174175/* Run some more finishConnect tests */176177/* TEST 6: NoConnectionPendingException */178channel = SctpChannel.open();179try {180channel.finishConnect();181fail("should have thrown NoConnectionPendingException");182} catch (NoConnectionPendingException unused) {183pass();184} catch (IOException ioe) {185unexpected(ioe);186}187188/* TEST 7: ClosedChannelException */189channel.close();190final SctpChannel cceChannel = channel;191testCCE(new Callable<Void>() {192public Void call() throws IOException {193cceChannel.finishConnect(); return null; } });194195/* TEST 8: IOException: Connection refused. Exercises handleSocketError.196* Assumption: no sctp socket listening on 3456 */197SocketAddress addr = new InetSocketAddress("localhost", 3456);198channel = SctpChannel.open();199try {200channel.connect(addr);201fail("should have thrown ConnectException: Connection refused");202} catch (IOException ioe) {203pass();204}205206} catch (IOException ioe) {207unexpected(ioe);208} finally {209try { if (channel != null) channel.close(); }210catch (IOException unused) {}211try { if (ssc != null) ssc.close(); }212catch (IOException unused) {}213}214}215216class UnsupportedSocketAddress extends SocketAddress { }217218void testCCE(Callable callable) {219try {220callable.call();221fail("should have thrown ClosedChannelException");222} catch (ClosedChannelException cce) {223pass();224} catch (Exception ioe) {225unexpected(ioe);226}227}228229//--------------------- Infrastructure ---------------------------230boolean debug = true;231volatile int passed = 0, failed = 0;232void pass() {passed++;}233void fail() {failed++; Thread.dumpStack();}234void fail(String msg) {System.err.println(msg); fail();}235void unexpected(Throwable t) {failed++; t.printStackTrace();}236void check(boolean cond) {if (cond) pass(); else fail();}237void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}238void debug(String message) {if(debug) { System.out.println(message); } }239public static void main(String[] args) throws Throwable {240Class<?> k = new Object(){}.getClass().getEnclosingClass();241try {k.getMethod("instanceMain",String[].class)242.invoke( k.newInstance(), (Object) args);}243catch (Throwable e) {throw e.getCause();}}244public void instanceMain(String[] args) throws Throwable {245try {test(args);} catch (Throwable t) {unexpected(t);}246System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);247if (failed > 0) throw new AssertionError("Some tests failed");}248249}250251252