1096N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1096N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1096N/A *
1096N/A * This code is free software; you can redistribute it and/or modify it
1096N/A * under the terms of the GNU General Public License version 2 only, as
1096N/A * published by the Free Software Foundation.
1096N/A *
1096N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1096N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1096N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1096N/A * version 2 for more details (a copy is included in the LICENSE file that
1096N/A * accompanied this code).
1096N/A *
1096N/A * You should have received a copy of the GNU General Public License version
1096N/A * 2 along with this work; if not, write to the Free Software Foundation,
1096N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1096N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1096N/A */
1096N/A
1096N/A/* @test
1096N/A * @bug 4927640
1096N/A * @summary Tests the SCTP protocol implementation
1096N/A * @author chegar
1096N/A */
1096N/A
1096N/Aimport java.net.InetSocketAddress;
1096N/Aimport java.net.SocketAddress;
1096N/Aimport java.io.IOException;
1096N/Aimport java.util.concurrent.CountDownLatch;
1096N/Aimport java.nio.ByteBuffer;
1096N/Aimport java.nio.channels.ClosedChannelException;
1096N/Aimport java.nio.channels.NotYetConnectedException;
1096N/Aimport com.sun.nio.sctp.AbstractNotificationHandler;
1096N/Aimport com.sun.nio.sctp.HandlerResult;
1096N/Aimport com.sun.nio.sctp.MessageInfo;
1096N/Aimport com.sun.nio.sctp.SctpChannel;
1096N/Aimport com.sun.nio.sctp.SctpServerChannel;
1096N/Aimport com.sun.nio.sctp.ShutdownNotification;
1096N/Aimport static java.lang.System.out;
1096N/Aimport static java.lang.System.err;
1096N/A
1096N/Apublic class Shutdown {
1096N/A static CountDownLatch finishedLatch = new CountDownLatch(1);
1096N/A static CountDownLatch sentLatch = new CountDownLatch(1);
1096N/A
1096N/A void test(String[] args) {
1096N/A SocketAddress address = null;
1096N/A ShutdownServer server = null;
1096N/A
1096N/A if (!Util.isSCTPSupported()) {
1096N/A out.println("SCTP protocol is not supported");
1096N/A out.println("Test cannot be run");
1096N/A return;
1096N/A }
1096N/A
1096N/A if (args.length == 2) {
1096N/A /* requested to connecct to a specific address */
1096N/A try {
1096N/A int port = Integer.valueOf(args[1]);
1096N/A address = new InetSocketAddress(args[0], port);
1096N/A } catch (NumberFormatException nfe) {
1096N/A err.println(nfe);
1096N/A }
1096N/A } else {
1096N/A /* start server on local machine, default */
1096N/A try {
1096N/A server = new ShutdownServer();
1096N/A server.start();
1096N/A address = server.address();
1096N/A debug("Server started and listening on " + address);
1096N/A } catch (IOException ioe) {
1096N/A ioe.printStackTrace();
1096N/A return;
1096N/A }
1096N/A }
1096N/A
1096N/A doTest(address);
1096N/A }
1096N/A
1096N/A void doTest(SocketAddress peerAddress) {
1096N/A SctpChannel channel = null;
1096N/A ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);
1096N/A MessageInfo info;
1096N/A
1096N/A try {
1096N/A channel = SctpChannel.open();
1096N/A
1096N/A /* TEST 1: Verify NotYetConnectedException thrown */
1096N/A debug("Test 1: NotYetConnectedException");
1096N/A try {
1096N/A channel.shutdown();
1096N/A fail("shutdown not throwing expected NotYetConnectedException");
1096N/A } catch (NotYetConnectedException unused) {
1096N/A pass();
1096N/A } catch (IOException ioe) {
1096N/A unexpected(ioe);
1096N/A }
1096N/A
1096N/A channel.connect(peerAddress);
1096N/A sentLatch.await();
1096N/A channel.shutdown();
1096N/A
1096N/A /* TEST 2: receive data sent before shutdown */
1096N/A do {
1096N/A debug("Test 2: invoking receive");
1096N/A info = channel.receive(buffer, null, null);
1096N/A if (info == null) {
1096N/A fail("unexpected null from receive");
1096N/A return;
1096N/A }
1096N/A } while (!info.isComplete());
1096N/A
1096N/A buffer.flip();
1096N/A check(info != null, "info is null");
1096N/A check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
1096N/A length, "bytes received not equal to message length");
1096N/A check(info.bytes() == buffer.remaining(), "bytes != remaining");
1096N/A check(Util.compare(buffer, Util.SMALL_MESSAGE),
1096N/A "received message not the same as sent message");
1096N/A
1096N/A buffer.clear();
1096N/A
1096N/A /* TEST 3: receive notifications on the SCTP stack */
1096N/A debug("Test 3: receive notifications");
1096N/A while ((info = channel.receive(buffer, null, null )) != null &&
1096N/A info.bytes() != -1 );
1096N/A
1096N/A
1096N/A /* TEST 4: If the channel is already shutdown then invoking this
1096N/A * method has no effect. */
1096N/A debug("Test 4: no-op");
1096N/A try {
1096N/A channel.shutdown();
1096N/A pass();
1096N/A } catch (IOException ioe) {
1096N/A unexpected(ioe);
1096N/A }
1096N/A
1096N/A /* TEST 5: Further sends will throw ClosedChannelException */
1096N/A debug("Test 5: ClosedChannelException");
1096N/A info = MessageInfo.createOutgoing(null, 1);
1096N/A try {
1096N/A channel.send(buffer, info);
1096N/A fail("shutdown not throwing expected ClosedChannelException");
1096N/A } catch (ClosedChannelException unused) {
1096N/A pass();
1096N/A } catch (IOException ioe) {
1096N/A unexpected(ioe);
1096N/A }
1326N/A
1326N/A /* TEST 6: getRemoteAddresses */
1326N/A debug("Test 6: getRemoteAddresses");
1326N/A try {
1326N/A java.util.Set<SocketAddress> remoteAddrs = channel.getRemoteAddresses();
1326N/A check(remoteAddrs.isEmpty(),
1326N/A "A shutdown channel should not have remote addresses");
1326N/A } catch (IOException ioe) {
1326N/A unexpected(ioe);
1326N/A }
1096N/A } catch (IOException ioe) {
1096N/A unexpected(ioe);
1096N/A } catch (InterruptedException ie) {
1096N/A unexpected(ie);
1096N/A }finally {
1096N/A finishedLatch.countDown();
1096N/A try { if (channel != null) channel.close(); }
1096N/A catch (IOException e) { unexpected(e);}
1096N/A }
1096N/A }
1096N/A
1096N/A class ShutdownServer implements Runnable
1096N/A {
1096N/A final InetSocketAddress serverAddr;
1096N/A private SctpServerChannel ssc;
1096N/A
1096N/A public ShutdownServer() throws IOException {
1096N/A ssc = SctpServerChannel.open().bind(null);
1096N/A //serverAddr = (InetSocketAddress)(ssc.getAllLocalAddresses().iterator().next());
1096N/A
1096N/A java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
1096N/A if (addrs.isEmpty())
1096N/A debug("addrs should not be empty");
1096N/A
1096N/A serverAddr = (InetSocketAddress) addrs.iterator().next();
1096N/A
1096N/A }
1096N/A
1096N/A public void start() {
1096N/A (new Thread(this, "ShutdownServer-" + serverAddr.getPort())).start();
1096N/A }
1096N/A
1096N/A public InetSocketAddress address() {
1096N/A return serverAddr;
1096N/A }
1096N/A
1096N/A @Override
1096N/A public void run() {
1096N/A SctpChannel sc = null;
1096N/A try {
1096N/A sc = ssc.accept();
1096N/A
1096N/A /* send a message */
1096N/A MessageInfo info = MessageInfo.createOutgoing(null, 1);
1096N/A ByteBuffer buf = ByteBuffer.allocateDirect(Util.SMALL_BUFFER);
1096N/A buf.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
1096N/A buf.flip();
1096N/A sc.send(buf, info);
1096N/A
1096N/A /* notify client that the data has been sent */
1096N/A sentLatch.countDown();
1096N/A
1096N/A /* wait until after the client has finished its tests */
1096N/A finishedLatch.await();
1096N/A
1096N/A buf.clear();
1096N/A ShutdownNotificationHandler handler =
1096N/A new ShutdownNotificationHandler();
1096N/A BooleanWrapper bool = new BooleanWrapper();
1096N/A sc.configureBlocking(false);
1096N/A sc.receive(buf, bool, handler);
1096N/A check(bool.booleanValue(), "SHUTDOWN not received on Server");
1096N/A
1096N/A } catch (IOException ioe) {
1096N/A ioe.printStackTrace();
1096N/A } catch (InterruptedException ie) {
1096N/A ie.printStackTrace();
1096N/A } finally {
1096N/A try { if (ssc != null) ssc.close(); }
1096N/A catch (IOException ioe) { unexpected(ioe); }
1096N/A try { if (sc != null) sc.close(); }
1096N/A catch (IOException ioe) { unexpected(ioe); }
1096N/A }
1096N/A }
1096N/A }
1096N/A
1096N/A class BooleanWrapper {
1096N/A boolean bool;
1096N/A
1096N/A boolean booleanValue() {
1096N/A return bool;
1096N/A }
1096N/A
1096N/A void booleanValue(boolean value) {
1096N/A bool = value;
1096N/A }
1096N/A }
1096N/A
1096N/A class ShutdownNotificationHandler extends AbstractNotificationHandler<BooleanWrapper>
1096N/A {
1096N/A @Override
1096N/A public HandlerResult handleNotification(
1096N/A ShutdownNotification sn, BooleanWrapper bool)
1096N/A {
1096N/A bool.booleanValue(true);
1096N/A debug(sn.toString());
1096N/A return HandlerResult.RETURN;
1096N/A }
1096N/A }
1096N/A
1096N/A //--------------------- Infrastructure ---------------------------
1096N/A boolean debug = true;
1096N/A volatile int passed = 0, failed = 0;
1096N/A void pass() {passed++;}
1096N/A void fail() {failed++; Thread.dumpStack();}
1096N/A void fail(String msg) {System.err.println(msg); fail();}
1096N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
1096N/A void check(boolean cond) {if (cond) pass(); else fail();}
1096N/A void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
1096N/A void debug(String message) {if(debug) { System.out.println(message); } }
1096N/A public static void main(String[] args) throws Throwable {
1096N/A Class<?> k = new Object(){}.getClass().getEnclosingClass();
1096N/A try {k.getMethod("instanceMain",String[].class)
1096N/A .invoke( k.newInstance(), (Object) args);}
1096N/A catch (Throwable e) {throw e.getCause();}}
1096N/A public void instanceMain(String[] args) throws Throwable {
1096N/A try {test(args);} catch (Throwable t) {unexpected(t);}
1096N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1096N/A if (failed > 0) throw new AssertionError("Some tests failed");}
1096N/A
1096N/A}