1326N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1326N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1326N/A *
1326N/A * This code is free software; you can redistribute it and/or modify it
1326N/A * under the terms of the GNU General Public License version 2 only, as
1326N/A * published by the Free Software Foundation.
1326N/A *
1326N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1326N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1326N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1326N/A * version 2 for more details (a copy is included in the LICENSE file that
1326N/A * accompanied this code).
1326N/A *
1326N/A * You should have received a copy of the GNU General Public License version
1326N/A * 2 along with this work; if not, write to the Free Software Foundation,
1326N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1326N/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.
1326N/A */
1326N/A
1326N/A/* @test
1326N/A * @bug 4927640
1326N/A * @summary Tests the SCTP protocol implementation
1326N/A * @author chegar
1326N/A */
1326N/A
1326N/Aimport java.net.InetSocketAddress;
1326N/Aimport java.net.SocketAddress;
1326N/Aimport java.io.IOException;
1326N/Aimport java.util.Set;
1326N/Aimport java.util.Iterator;
1326N/Aimport java.util.concurrent.CountDownLatch;
1326N/Aimport java.util.concurrent.TimeUnit;
1326N/Aimport java.nio.ByteBuffer;
1326N/Aimport com.sun.nio.sctp.AbstractNotificationHandler;
1326N/Aimport com.sun.nio.sctp.Association;
1326N/Aimport com.sun.nio.sctp.AssociationChangeNotification;
1326N/Aimport com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent;
1326N/Aimport com.sun.nio.sctp.HandlerResult;
1326N/Aimport com.sun.nio.sctp.InvalidStreamException;
1326N/Aimport com.sun.nio.sctp.MessageInfo;
1326N/Aimport com.sun.nio.sctp.SctpChannel;
1326N/Aimport com.sun.nio.sctp.SctpMultiChannel;
1326N/Aimport com.sun.nio.sctp.ShutdownNotification;
1326N/Aimport static java.lang.System.out;
1326N/Aimport static java.lang.System.err;
1326N/A
1326N/Apublic class Branch {
1326N/A /* Latches used to synchronize between the client and server so that
1326N/A * connections without any IO may not be closed without being accepted */
1326N/A final CountDownLatch clientFinishedLatch = new CountDownLatch(1);
1326N/A final CountDownLatch serverFinishedLatch = new CountDownLatch(1);
1326N/A
1326N/A void test(String[] args) {
1326N/A SocketAddress address = null;
1326N/A Server server = null;
1326N/A
1326N/A if (!Util.isSCTPSupported()) {
1326N/A out.println("SCTP protocol is not supported");
1326N/A out.println("Test cannot be run");
1326N/A return;
1326N/A }
1326N/A
1326N/A if (args.length == 2) {
1326N/A /* requested to connecct to a specific address */
1326N/A try {
1326N/A int port = Integer.valueOf(args[1]);
1326N/A address = new InetSocketAddress(args[0], port);
1326N/A } catch (NumberFormatException nfe) {
1326N/A err.println(nfe);
1326N/A }
1326N/A } else {
1326N/A /* start server on local machine, default */
1326N/A try {
1326N/A server = new Server();
1326N/A server.start();
1326N/A address = server.address();
1326N/A debug("Server started and listening on " + address);
1326N/A } catch (IOException ioe) {
1326N/A ioe.printStackTrace();
1326N/A return;
1326N/A }
1326N/A }
1326N/A
1326N/A doTest(address);
1326N/A }
1326N/A
1326N/A void doTest(SocketAddress peerAddress) {
1326N/A SctpMultiChannel channel = null;
1326N/A ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
1326N/A MessageInfo info = MessageInfo.createOutgoing(null, 0);
1326N/A
1326N/A try {
1326N/A channel = SctpMultiChannel.open();
1326N/A
1326N/A /* setup an association implicitly by sending a small message */
1326N/A int streamNumber = 0;
1326N/A debug("sending to " + peerAddress + " on stream number: " + streamNumber);
1326N/A info = MessageInfo.createOutgoing(peerAddress, streamNumber);
1326N/A buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
1326N/A buffer.flip();
1326N/A int position = buffer.position();
1326N/A int remaining = buffer.remaining();
1326N/A
1326N/A debug("sending small message: " + buffer);
1326N/A int sent = channel.send(buffer, info);
1326N/A
1326N/A check(sent == remaining, "sent should be equal to remaining");
1326N/A check(buffer.position() == (position + sent),
1326N/A "buffers position should have been incremented by sent");
1326N/A
1326N/A /* Receive the COMM_UP */
1326N/A buffer.clear();
1326N/A BranchNotificationHandler handler = new BranchNotificationHandler();
1326N/A info = channel.receive(buffer, null, handler);
1326N/A check(handler.receivedCommUp(), "COMM_UP no received");
1326N/A Set<Association> associations = channel.associations();
1326N/A check(!associations.isEmpty(),"There should be some associations");
1326N/A Association bassoc = associations.iterator().next();
1326N/A
1326N/A /* TEST 1: branch */
1326N/A SctpChannel bchannel = channel.branch(bassoc);
1326N/A
1326N/A check(!bchannel.getAllLocalAddresses().isEmpty(),
1326N/A "branched channel should be bound");
1326N/A check(!bchannel.getRemoteAddresses().isEmpty(),
1326N/A "branched channel should be connected");
1326N/A check(channel.associations().isEmpty(),
1326N/A "there should be no associations since the only one was branched off");
1326N/A
1326N/A buffer.clear();
1326N/A info = bchannel.receive(buffer, null, null);
1326N/A buffer.flip();
1326N/A check(info != null, "info is null");
1326N/A check(info.streamNumber() == streamNumber,
1326N/A "message not sent on the correct stream");
1326N/A check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
1326N/A length, "bytes received not equal to message length");
1326N/A check(info.bytes() == buffer.remaining(), "bytes != remaining");
1326N/A check(Util.compare(buffer, Util.SMALL_MESSAGE),
1326N/A "received message not the same as sent message");
1326N/A
1326N/A } catch (IOException ioe) {
1326N/A unexpected(ioe);
1326N/A } finally {
1326N/A clientFinishedLatch.countDown();
1326N/A try { serverFinishedLatch.await(10L, TimeUnit.SECONDS); }
1326N/A catch (InterruptedException ie) { unexpected(ie); }
1326N/A if (channel != null) {
1326N/A try { channel.close(); }
1326N/A catch (IOException e) { unexpected (e);}
1326N/A }
1326N/A }
1326N/A }
1326N/A
1326N/A class Server implements Runnable
1326N/A {
1326N/A final InetSocketAddress serverAddr;
1326N/A private SctpMultiChannel serverChannel;
1326N/A
1326N/A public Server() throws IOException {
1326N/A serverChannel = SctpMultiChannel.open().bind(null);
1326N/A java.util.Set<SocketAddress> addrs = serverChannel.getAllLocalAddresses();
1326N/A if (addrs.isEmpty())
1326N/A debug("addrs should not be empty");
1326N/A
1326N/A serverAddr = (InetSocketAddress) addrs.iterator().next();
1326N/A }
1326N/A
1326N/A public void start() {
1326N/A (new Thread(this, "Server-" + serverAddr.getPort())).start();
1326N/A }
1326N/A
1326N/A public InetSocketAddress address() {
1326N/A return serverAddr;
1326N/A }
1326N/A
1326N/A @Override
1326N/A public void run() {
1326N/A ByteBuffer buffer = ByteBuffer.allocateDirect(Util.LARGE_BUFFER);
1326N/A try {
1326N/A MessageInfo info;
1326N/A
1326N/A /* receive a small message */
1326N/A do {
1326N/A info = serverChannel.receive(buffer, null, null);
1326N/A if (info == null) {
1326N/A fail("Server: unexpected null from receive");
1326N/A return;
1326N/A }
1326N/A } while (!info.isComplete());
1326N/A
1326N/A buffer.flip();
1326N/A check(info != null, "info is null");
1326N/A check(info.streamNumber() == 0,
1326N/A "message not sent on the correct stream");
1326N/A check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
1326N/A length, "bytes received not equal to message length");
1326N/A check(info.bytes() == buffer.remaining(), "bytes != remaining");
1326N/A check(Util.compare(buffer, Util.SMALL_MESSAGE),
1326N/A "received message not the same as sent message");
1326N/A
1326N/A check(info != null, "info is null");
1326N/A Set<Association> assocs = serverChannel.associations();
1326N/A check(assocs.size() == 1, "there should be only one association");
1326N/A
1326N/A /* echo the message */
1326N/A debug("Server: echoing first message");
1326N/A buffer.flip();
1326N/A int bytes = serverChannel.send(buffer, info);
1326N/A debug("Server: sent " + bytes + "bytes");
1326N/A
1326N/A clientFinishedLatch.await(10L, TimeUnit.SECONDS);
1326N/A serverFinishedLatch.countDown();
1326N/A } catch (IOException ioe) {
1326N/A unexpected(ioe);
1326N/A } catch (InterruptedException ie) {
1326N/A unexpected(ie);
1326N/A } finally {
1326N/A try { if (serverChannel != null) serverChannel.close(); }
1326N/A catch (IOException unused) {}
1326N/A }
1326N/A }
1326N/A }
1326N/A
1326N/A class BranchNotificationHandler extends AbstractNotificationHandler<Object>
1326N/A {
1326N/A boolean receivedCommUp; // false
1326N/A
1326N/A boolean receivedCommUp() {
1326N/A return receivedCommUp;
1326N/A }
1326N/A
1326N/A @Override
1326N/A public HandlerResult handleNotification(
1326N/A AssociationChangeNotification notification, Object attachment) {
1326N/A AssocChangeEvent event = notification.event();
1326N/A debug("AssociationChangeNotification");
1326N/A debug(" Association: " + notification.association());
1326N/A debug(" Event: " + event);
1326N/A
1326N/A if (event.equals(AssocChangeEvent.COMM_UP))
1326N/A receivedCommUp = true;
1326N/A
1326N/A return HandlerResult.RETURN;
1326N/A }
1326N/A
1326N/A /* A ShutdownNotification handler is provided to ensure that no
1326N/A * shutdown notification are being handled since we don't expect
1326N/A * to receive them. This is not part of branch testing, it just
1326N/A * fits here to test another bug. */
1326N/A @Override
1326N/A public HandlerResult handleNotification(
1326N/A ShutdownNotification notification, Object attachment) {
1326N/A debug("ShutdownNotification");
1326N/A debug(" Association: " + notification.association());
1326N/A
1326N/A fail("Shutdown should not be received");
1326N/A
1326N/A return HandlerResult.RETURN;
1326N/A }
1326N/A
1326N/A }
1326N/A
1326N/A //--------------------- Infrastructure ---------------------------
1326N/A boolean debug = true;
1326N/A volatile int passed = 0, failed = 0;
1326N/A void pass() {passed++;}
1326N/A void fail() {failed++; Thread.dumpStack();}
1326N/A void fail(String msg) {System.err.println(msg); fail();}
1326N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
1326N/A void check(boolean cond) {if (cond) pass(); else fail();}
1326N/A void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
1326N/A void debug(String message) {if(debug) { System.out.println(message); } }
1326N/A public static void main(String[] args) throws Throwable {
1326N/A Class<?> k = new Object(){}.getClass().getEnclosingClass();
1326N/A try {k.getMethod("instanceMain",String[].class)
1326N/A .invoke( k.newInstance(), (Object) args);}
1326N/A catch (Throwable e) {throw e.getCause();}}
1326N/A public void instanceMain(String[] args) throws Throwable {
1326N/A try {test(args);} catch (Throwable t) {unexpected(t);}
1326N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1326N/A if (failed > 0) throw new AssertionError("Some tests failed");}
1326N/A
1326N/A}