Path: blob/master/test/jdk/com/sun/nio/sctp/SctpChannel/Connect.java
41155 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;6364try (SctpServerChannel ssc = SctpServerChannel.open()) {65/* Create a server channel to connect to */66ssc.bind(null);67Set<SocketAddress> addrs = ssc.getAllLocalAddresses();68if (addrs.isEmpty())69debug("addrs should not be empty");70final SocketAddress peerAddress = (InetSocketAddress) addrs.iterator().next();7172channel = SctpChannel.open();7374/* TEST 0.5 Verify default values for new/unconnected channel */75check(channel.getRemoteAddresses().isEmpty(),76"non empty set for unconnected channel");77check(channel.association() == null,78"non-null association for unconnected channel");79check(!channel.isConnectionPending(),80"should not have a connection pending");8182/* TEST 1: non-blocking connect */83channel.configureBlocking(false);84if (channel.connect(peerAddress) != true) {85debug("non-blocking connect did not immediately succeed");86check(channel.isConnectionPending(),87"should return true for isConnectionPending");88try {89channel.connect(peerAddress);90fail("should have thrown ConnectionPendingException");91} catch (ConnectionPendingException cpe) {92pass();93} catch (IOException ioe) {94unexpected(ioe);95}96channel.configureBlocking(true);97check(channel.finishConnect(),98"finishConnect should have returned true");99}100101ssc.accept();102ssc.close();103104/* TEST 1.5 Verify after connect */105check(!channel.getRemoteAddresses().isEmpty(),106"empty set for connected channel");107check(channel.association() != null,108"null association for connected channel");109check(!channel.isConnectionPending(),110"pending connection for connected channel");111112/* TEST 2: Verify AlreadyConnectedException thrown */113try {114channel.connect(peerAddress);115fail("should have thrown AlreadyConnectedException");116} catch (AlreadyConnectedException unused) {117pass();118} catch (IOException ioe) {119unexpected(ioe);120}121122/* TEST 2.5: Verify AlreadyConnectedException thrown */123try {124channel.connect(peerAddress, 5, 5);125fail("should have thrown AlreadyConnectedException");126} catch (AlreadyConnectedException unused) {127pass();128} catch (IOException ioe) {129unexpected(ioe);130}131132/* TEST 3: UnresolvedAddressException */133channel.close();134channel = SctpChannel.open();135InetSocketAddress unresolved =136InetSocketAddress.createUnresolved("xxyyzzabc", 4567);137try {138channel.connect(unresolved);139fail("should have thrown UnresolvedAddressException");140} catch (UnresolvedAddressException unused) {141pass();142} catch (IOException ioe) {143unexpected(ioe);144}145146/* TEST 4: UnsupportedAddressTypeException */147SocketAddress unsupported = new UnsupportedSocketAddress();148try {149channel.connect(unsupported);150fail("should have thrown UnsupportedAddressTypeException");151} catch (UnsupportedAddressTypeException unused) {152pass();153} catch (IOException ioe) {154unexpected(ioe);155}156157/* TEST 5: ClosedChannelException */158channel.close();159final SctpChannel closedChannel = channel;160testCCE(new Callable<Void>() {161public Void call() throws IOException {162closedChannel.connect(peerAddress); return null; } });163164/* TEST 5.5 getRemoteAddresses */165testCCE(new Callable<Void>() {166public Void call() throws IOException {167closedChannel.getRemoteAddresses(); return null; } });168testCCE(new Callable<Void>() {169public Void call() throws IOException {170closedChannel.association(); return null; } });171check(!channel.isConnectionPending(),172"pending connection for closed channel");173174/* Run some more finishConnect tests */175176/* TEST 6: NoConnectionPendingException */177channel = SctpChannel.open();178try {179channel.finishConnect();180fail("should have thrown NoConnectionPendingException");181} catch (NoConnectionPendingException unused) {182pass();183} catch (IOException ioe) {184unexpected(ioe);185}186187/* TEST 7: ClosedChannelException */188channel.close();189final SctpChannel cceChannel = channel;190testCCE(new Callable<Void>() {191public Void call() throws IOException {192cceChannel.finishConnect(); return null; } });193194/* TEST 8: IOException: Connection refused. Exercises handleSocketError.195* Assumption: no sctp socket listening on 3456 */196SocketAddress addr = new InetSocketAddress("localhost", 3456);197channel = SctpChannel.open();198try {199channel.connect(addr);200fail("should have thrown ConnectException: Connection refused");201} catch (IOException ioe) {202pass();203}204205} catch (IOException ioe) {206unexpected(ioe);207} finally {208try { if (channel != null) channel.close(); }209catch (IOException unused) {}210}211}212213class UnsupportedSocketAddress extends SocketAddress { }214215void testCCE(Callable callable) {216try {217callable.call();218fail("should have thrown ClosedChannelException");219} catch (ClosedChannelException cce) {220pass();221} catch (Exception ioe) {222unexpected(ioe);223}224}225226//--------------------- Infrastructure ---------------------------227boolean debug = true;228volatile int passed = 0, failed = 0;229void pass() {passed++;}230void fail() {failed++; Thread.dumpStack();}231void fail(String msg) {System.err.println(msg); fail();}232void unexpected(Throwable t) {failed++; t.printStackTrace();}233void check(boolean cond) {if (cond) pass(); else fail();}234void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}235void debug(String message) {if(debug) { System.out.println(message); } }236public static void main(String[] args) throws Throwable {237Class<?> k = new Object(){}.getClass().getEnclosingClass();238try {k.getMethod("instanceMain",String[].class)239.invoke( k.newInstance(), (Object) args);}240catch (Throwable e) {throw e.getCause();}}241public void instanceMain(String[] args) throws Throwable {242try {test(args);} catch (Throwable t) {unexpected(t);}243System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);244if (failed > 0) throw new AssertionError("Some tests failed");}245246}247248249