Path: blob/master/test/jdk/com/sun/nio/sctp/SctpServerChannel/Accept.java
41155 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.InetSocketAddress;30import java.net.SocketAddress;31import java.io.IOException;32import java.util.concurrent.CountDownLatch;33import java.nio.channels.AlreadyConnectedException;34import java.nio.channels.AsynchronousCloseException;35import java.nio.channels.NotYetBoundException;36import java.nio.channels.ClosedByInterruptException;37import java.nio.channels.ClosedChannelException;38import com.sun.nio.sctp.SctpChannel;39import com.sun.nio.sctp.SctpServerChannel;40import static java.lang.System.out;41import static java.lang.System.err;4243public class Accept {44static CountDownLatch acceptLatch = new CountDownLatch(1);45static CountDownLatch closeByIntLatch = new CountDownLatch(1);46static CountDownLatch asyncCloseLatch = new CountDownLatch(1);47AcceptServer server = null;4849void test(String[] args) {50SocketAddress address = null;5152if (!Util.isSCTPSupported()) {53out.println("SCTP protocol is not supported");54out.println("Test cannot be run");55return;56}5758if (args.length == 2) {59/* requested to connecct to a specific address */60try {61int port = Integer.valueOf(args[1]);62address = new InetSocketAddress(args[0], port);63} catch (NumberFormatException nfe) {64err.println(nfe);65}66} else {67/* start server on local machine, default */68try {69server = new AcceptServer();70server.start();71address = server.address();72debug("Server started and listening on " + address);73} catch (IOException ioe) {74ioe.printStackTrace();75return;76}77}7879doClient(address);80}8182void doClient(SocketAddress peerAddress) {83SctpChannel channel = null;8485try {86channel = SctpChannel.open(peerAddress, 0, 0);87acceptLatch.await();8889/* for test 4 */90closeByIntLatch.await();91sleep(500);92server.thread().interrupt();9394/* for test 5 */95asyncCloseLatch.await();96sleep(500);97server.channel().close();9899/* wait for the server thread to finish */100join(server.thread(), 10000);101} catch (IOException ioe) {102unexpected(ioe);103} catch (InterruptedException ie) {104unexpected(ie);105} finally {106try { if (channel != null) channel.close(); }107catch (IOException e) { unexpected(e);}108}109}110111class AcceptServer implements Runnable112{113final InetSocketAddress serverAddr;114private SctpServerChannel ssc;115private Thread serverThread;116117public AcceptServer() throws IOException {118ssc = SctpServerChannel.open();119120/* TEST 1: NotYetBoundException */121debug("TEST 1: NotYetBoundException");122try {123ssc.accept();124fail();125} catch (NotYetBoundException nybe) {126debug(" caught NotYetBoundException");127pass();128} catch (IOException ioe) {129unexpected(ioe);130}131132ssc.bind(null);133java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();134if (addrs.isEmpty())135debug("addrs should not be empty");136137serverAddr = (InetSocketAddress) addrs.iterator().next();138139/* TEST 2: null if this channel is in non-blocking mode and no140* association is available to be accepted */141ssc.configureBlocking(false);142debug("TEST 2: non-blocking mode null");143try {144SctpChannel sc = ssc.accept();145check(sc == null, "non-blocking mode should return null");146} catch (IOException ioe) {147unexpected(ioe);148} finally {149ssc.configureBlocking(true);150}151}152153void start() {154serverThread = new Thread(this, "AcceptServer-" +155serverAddr.getPort());156serverThread.start();157}158159InetSocketAddress address() {160return serverAddr;161}162163SctpServerChannel channel() {164return ssc;165}166167Thread thread() {168return serverThread;169}170171@Override172public void run() {173SctpChannel sc = null;174try {175/* TEST 3: accepted channel */176debug("TEST 3: accepted channel");177sc = ssc.accept();178179checkAcceptedChannel(sc);180acceptLatch.countDown();181182/* TEST 4: ClosedByInterruptException */183debug("TEST 4: ClosedByInterruptException");184try {185closeByIntLatch.countDown();186ssc.accept();187fail();188} catch (ClosedByInterruptException unused) {189debug(" caught ClosedByInterruptException");190pass();191}192193/* TEST 5: AsynchronousCloseException */194debug("TEST 5: AsynchronousCloseException");195/* reset thread interrupt status */196Thread.currentThread().interrupted();197198ssc = SctpServerChannel.open().bind(null);199try {200asyncCloseLatch.countDown();201ssc.accept();202fail();203} catch (AsynchronousCloseException unused) {204debug(" caught AsynchronousCloseException");205pass();206}207208/* TEST 6: ClosedChannelException */209debug("TEST 6: ClosedChannelException");210try {211ssc.accept();212fail();213} catch (ClosedChannelException unused) {214debug(" caught ClosedChannelException");215pass();216}217ssc = null;218} catch (IOException ioe) {219ioe.printStackTrace();220} finally {221try { if (ssc != null) ssc.close(); }222catch (IOException ioe) { unexpected(ioe); }223try { if (sc != null) sc.close(); }224catch (IOException ioe) { unexpected(ioe); }225}226}227}228229void checkAcceptedChannel(SctpChannel sc) {230try {231debug("Checking accepted SctpChannel");232check(sc.association() != null,233"accepted channel should have an association");234check(!(sc.getRemoteAddresses().isEmpty()),235"accepted channel should be connected");236check(!(sc.isConnectionPending()),237"accepted channel should not have a connection pending");238check(sc.isBlocking(),239"accepted channel should be blocking");240try { sc.connect(new TestSocketAddress()); fail(); }241catch (AlreadyConnectedException unused) { pass(); }242try { sc.bind(new TestSocketAddress()); fail(); }243catch (AlreadyConnectedException unused) { pass(); }244} catch (IOException unused) { fail(); }245}246247static class TestSocketAddress extends SocketAddress {}248249//--------------------- Infrastructure ---------------------------250boolean debug = true;251volatile int passed = 0, failed = 0;252void pass() {passed++;}253void fail() {failed++; Thread.dumpStack();}254void fail(String msg) {err.println(msg); fail();}255void unexpected(Throwable t) {failed++; t.printStackTrace();}256void check(boolean cond) {if (cond) pass(); else fail();}257void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}258void debug(String message) {if(debug) { out.println(message); } }259void sleep(long millis) { try { Thread.currentThread().sleep(millis); }260catch(InterruptedException ie) { unexpected(ie); }}261void join(Thread thread, long millis) { try { thread.join(millis); }262catch(InterruptedException ie) { unexpected(ie); }}263public static void main(String[] args) throws Throwable {264Class<?> k = new Object(){}.getClass().getEnclosingClass();265try {k.getMethod("instanceMain",String[].class)266.invoke( k.newInstance(), (Object) args);}267catch (Throwable e) {throw e.getCause();}}268public void instanceMain(String[] args) throws Throwable {269try {test(args);} catch (Throwable t) {unexpected(t);}270out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);271if (failed > 0) throw new AssertionError("Some tests failed");}272273}274275276