Path: blob/master/test/jdk/com/sun/nio/sctp/SctpChannel/Shutdown.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.ByteBuffer;34import java.nio.channels.ClosedChannelException;35import java.nio.channels.NotYetConnectedException;36import com.sun.nio.sctp.AbstractNotificationHandler;37import com.sun.nio.sctp.HandlerResult;38import com.sun.nio.sctp.MessageInfo;39import com.sun.nio.sctp.SctpChannel;40import com.sun.nio.sctp.SctpServerChannel;41import com.sun.nio.sctp.ShutdownNotification;42import static java.lang.System.out;43import static java.lang.System.err;4445public class Shutdown {46static CountDownLatch finishedLatch = new CountDownLatch(1);47static CountDownLatch sentLatch = new CountDownLatch(1);4849void test(String[] args) {50SocketAddress address = null;51ShutdownServer server = null;5253if (!Util.isSCTPSupported()) {54out.println("SCTP protocol is not supported");55out.println("Test cannot be run");56return;57}5859if (args.length == 2) {60/* requested to connecct to a specific address */61try {62int port = Integer.valueOf(args[1]);63address = new InetSocketAddress(args[0], port);64} catch (NumberFormatException nfe) {65err.println(nfe);66}67} else {68/* start server on local machine, default */69try {70server = new ShutdownServer();71server.start();72address = server.address();73debug("Server started and listening on " + address);74} catch (IOException ioe) {75ioe.printStackTrace();76return;77}78}7980doTest(address);81}8283void doTest(SocketAddress peerAddress) {84SctpChannel channel = null;85ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);86MessageInfo info;8788try {89channel = SctpChannel.open();9091/* TEST 1: Verify NotYetConnectedException thrown */92debug("Test 1: NotYetConnectedException");93try {94channel.shutdown();95fail("shutdown not throwing expected NotYetConnectedException");96} catch (NotYetConnectedException unused) {97pass();98} catch (IOException ioe) {99unexpected(ioe);100}101102channel.connect(peerAddress);103sentLatch.await();104channel.shutdown();105106/* TEST 2: receive data sent before shutdown */107do {108debug("Test 2: invoking receive");109info = channel.receive(buffer, null, null);110if (info == null) {111fail("unexpected null from receive");112return;113}114} while (!info.isComplete());115116buffer.flip();117check(info != null, "info is null");118check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").119length, "bytes received not equal to message length");120check(info.bytes() == buffer.remaining(), "bytes != remaining");121check(Util.compare(buffer, Util.SMALL_MESSAGE),122"received message not the same as sent message");123124buffer.clear();125126/* TEST 3: receive notifications on the SCTP stack */127debug("Test 3: receive notifications");128while ((info = channel.receive(buffer, null, null )) != null &&129info.bytes() != -1 );130131132/* TEST 4: If the channel is already shutdown then invoking this133* method has no effect. */134debug("Test 4: no-op");135try {136channel.shutdown();137pass();138} catch (IOException ioe) {139unexpected(ioe);140}141142/* TEST 5: Further sends will throw ClosedChannelException */143debug("Test 5: ClosedChannelException");144info = MessageInfo.createOutgoing(null, 1);145try {146channel.send(buffer, info);147fail("shutdown not throwing expected ClosedChannelException");148} catch (ClosedChannelException unused) {149pass();150} catch (IOException ioe) {151unexpected(ioe);152}153154/* TEST 6: getRemoteAddresses */155debug("Test 6: getRemoteAddresses");156try {157java.util.Set<SocketAddress> remoteAddrs = channel.getRemoteAddresses();158check(remoteAddrs.isEmpty(),159"A shutdown channel should not have remote addresses");160} catch (IOException ioe) {161unexpected(ioe);162}163} catch (IOException ioe) {164unexpected(ioe);165} catch (InterruptedException ie) {166unexpected(ie);167}finally {168finishedLatch.countDown();169try { if (channel != null) channel.close(); }170catch (IOException e) { unexpected(e);}171}172}173174class ShutdownServer implements Runnable175{176final InetSocketAddress serverAddr;177private SctpServerChannel ssc;178179public ShutdownServer() throws IOException {180ssc = SctpServerChannel.open().bind(null);181//serverAddr = (InetSocketAddress)(ssc.getAllLocalAddresses().iterator().next());182183java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();184if (addrs.isEmpty())185debug("addrs should not be empty");186187serverAddr = (InetSocketAddress) addrs.iterator().next();188189}190191public void start() {192(new Thread(this, "ShutdownServer-" + serverAddr.getPort())).start();193}194195public InetSocketAddress address() {196return serverAddr;197}198199@Override200public void run() {201SctpChannel sc = null;202try {203sc = ssc.accept();204205/* send a message */206MessageInfo info = MessageInfo.createOutgoing(null, 1);207ByteBuffer buf = ByteBuffer.allocateDirect(Util.SMALL_BUFFER);208buf.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));209buf.flip();210sc.send(buf, info);211212/* notify client that the data has been sent */213sentLatch.countDown();214215/* wait until after the client has finished its tests */216finishedLatch.await();217218buf.clear();219ShutdownNotificationHandler handler =220new ShutdownNotificationHandler();221BooleanWrapper bool = new BooleanWrapper();222sc.configureBlocking(false);223sc.receive(buf, bool, handler);224check(bool.booleanValue(), "SHUTDOWN not received on Server");225226} catch (IOException ioe) {227ioe.printStackTrace();228} catch (InterruptedException ie) {229ie.printStackTrace();230} finally {231try { if (ssc != null) ssc.close(); }232catch (IOException ioe) { unexpected(ioe); }233try { if (sc != null) sc.close(); }234catch (IOException ioe) { unexpected(ioe); }235}236}237}238239class BooleanWrapper {240boolean bool;241242boolean booleanValue() {243return bool;244}245246void booleanValue(boolean value) {247bool = value;248}249}250251class ShutdownNotificationHandler extends AbstractNotificationHandler<BooleanWrapper>252{253@Override254public HandlerResult handleNotification(255ShutdownNotification sn, BooleanWrapper bool)256{257bool.booleanValue(true);258debug(sn.toString());259return HandlerResult.RETURN;260}261}262263//--------------------- Infrastructure ---------------------------264boolean debug = true;265volatile int passed = 0, failed = 0;266void pass() {passed++;}267void fail() {failed++; Thread.dumpStack();}268void fail(String msg) {System.err.println(msg); fail();}269void unexpected(Throwable t) {failed++; t.printStackTrace();}270void check(boolean cond) {if (cond) pass(); else fail();}271void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}272void debug(String message) {if(debug) { System.out.println(message); } }273public static void main(String[] args) throws Throwable {274Class<?> k = new Object(){}.getClass().getEnclosingClass();275try {k.getMethod("instanceMain",String[].class)276.invoke( k.newInstance(), (Object) args);}277catch (Throwable e) {throw e.getCause();}}278public void instanceMain(String[] args) throws Throwable {279try {test(args);} catch (Throwable t) {unexpected(t);}280System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);281if (failed > 0) throw new AssertionError("Some tests failed");}282283}284285286