1096N/A/*
3261N/A * Copyright (c) 2009, 2010, 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.util.concurrent.TimeUnit;
1096N/Aimport java.nio.ByteBuffer;
1096N/Aimport java.nio.channels.NotYetConnectedException;
1096N/Aimport java.nio.channels.ClosedChannelException;
1096N/Aimport com.sun.nio.sctp.AbstractNotificationHandler;
1096N/Aimport com.sun.nio.sctp.Association;
1096N/Aimport com.sun.nio.sctp.AssociationChangeNotification;
1096N/Aimport com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent;
1096N/Aimport com.sun.nio.sctp.HandlerResult;
1096N/Aimport com.sun.nio.sctp.InvalidStreamException;
1096N/Aimport com.sun.nio.sctp.MessageInfo;
1096N/Aimport com.sun.nio.sctp.Notification;
1096N/Aimport com.sun.nio.sctp.SctpChannel;
1096N/Aimport com.sun.nio.sctp.SctpServerChannel;
1096N/Aimport static java.lang.System.out;
1096N/Aimport static java.lang.System.err;
1096N/A
1096N/Apublic class Send {
1096N/A /* Latches used to synchronize between the client and server so that
1096N/A * connections without any IO may not be closed without being accepted */
1096N/A final CountDownLatch clientFinishedLatch = new CountDownLatch(1);
1096N/A final CountDownLatch serverFinishedLatch = new CountDownLatch(1);
1096N/A
1096N/A SendNotificationHandler handler = new SendNotificationHandler();
1096N/A
1096N/A void test(String[] args) {
1096N/A SocketAddress address = null;
1096N/A Server 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 Server();
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.LARGE_BUFFER);
1096N/A MessageInfo info = MessageInfo.createOutgoing(null, 0);
1096N/A
1096N/A try {
1096N/A channel = SctpChannel.open();
1096N/A
1096N/A /* TEST 1: Verify NotYetConnectedException thrown */
1096N/A try {
1096N/A channel.send(buffer, info);
1096N/A fail("should have thrown 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 /* Receive CommUp */
1096N/A channel.receive(buffer, null, handler);
1096N/A
1096N/A /* TEST 2: send small message */
1096N/A int streamNumber = 0;
1096N/A debug("sending on stream number: " + streamNumber);
1096N/A info = MessageInfo.createOutgoing(null, streamNumber);
1096N/A buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
1096N/A buffer.flip();
1096N/A int position = buffer.position();
1096N/A int remaining = buffer.remaining();
1096N/A
1096N/A debug("sending small message: " + buffer);
1096N/A int sent = channel.send(buffer, info);
1096N/A
1096N/A check(sent == remaining, "sent should be equal to remaining");
1096N/A check(buffer.position() == (position + sent),
1096N/A "buffers position should have been incremented by sent");
1096N/A
1096N/A buffer.clear();
1096N/A
1096N/A /* TEST 3: send large message */
1096N/A streamNumber = handler.maxOutStreams() - 1;
1096N/A debug("sending on stream number: " + streamNumber);
1096N/A info = MessageInfo.createOutgoing(null, streamNumber);
1096N/A buffer.put(Util.LARGE_MESSAGE.getBytes("ISO-8859-1"));
1096N/A buffer.flip();
1096N/A position = buffer.position();
1096N/A remaining = buffer.remaining();
1096N/A
1096N/A debug("sending large message: " + buffer);
1096N/A sent = channel.send(buffer, info);
1096N/A
1096N/A check(sent == remaining, "sent should be equal to remaining");
1096N/A check(buffer.position() == (position + sent),
1096N/A "buffers position should have been incremented by sent");
1096N/A
1096N/A /* TEST 4: InvalidStreamExcepton */
1096N/A streamNumber = handler.maxInStreams;
1096N/A info = MessageInfo.createOutgoing(null, streamNumber);
1096N/A buffer.clear();
1096N/A buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
1096N/A buffer.flip();
1096N/A position = buffer.position();
1096N/A remaining = buffer.remaining();
1096N/A
1096N/A debug("sending on stream number: " + streamNumber);
1096N/A debug("sending small message: " + buffer);
1096N/A try {
1096N/A sent = channel.send(buffer, info);
1096N/A fail("should have thrown InvalidStreamExcepton");
1096N/A } catch (InvalidStreamException ise){
1096N/A pass();
1096N/A } catch (IOException ioe) {
1096N/A unexpected(ioe);
1096N/A }
1096N/A check(buffer.remaining() == remaining,
1096N/A "remaining should not be changed");
1096N/A check(buffer.position() == position,
1096N/A "buffers position should not be changed");
1096N/A
1096N/A /* TEST 5: Non blocking send should return zero if there is
1096N/A insufficient room in the underlying output buffer */
1096N/A buffer.clear();
1096N/A channel.configureBlocking(false);
1096N/A info = MessageInfo.createOutgoing(null, 1);
1096N/A buffer.put(Util.LARGE_MESSAGE.getBytes("ISO-8859-1"));
1096N/A buffer.flip();
1096N/A
1096N/A int count = 0; // do not loop forever
1096N/A do {
1096N/A position = buffer.position();
1096N/A remaining = buffer.remaining();
1096N/A debug("sending large message: " + buffer);
1096N/A sent = channel.send(buffer, info);
1096N/A if (sent == 0) {
1096N/A check(buffer.remaining() == remaining,
1096N/A "remaining should not be changed");
1096N/A check(buffer.position() == position,
1096N/A "buffers position should not be changed");
1096N/A }
1096N/A buffer.rewind();
1096N/A } while (sent != 0 && count++ < 100);
1096N/A
1096N/A /* TEST 6: ClosedChannelException */
1096N/A channel.close();
1096N/A try {
1096N/A channel.send(buffer, info);
1096N/A fail("should have thrown ClosedChannelException");
1096N/A } catch (ClosedChannelException cce) {
1096N/A pass();
1096N/A } catch (IOException ioe) {
1096N/A unexpected(ioe);
1096N/A }
1096N/A
1096N/A /* TEST 7: send without previous receive.
1096N/A * Verify that send can still throw InvalidStreamExcepton */
1096N/A debug("Opening new channel.");
1096N/A channel = SctpChannel.open(peerAddress, 0, 0);
1096N/A streamNumber = Short.MAX_VALUE - 1;
1096N/A info = MessageInfo.createOutgoing(null, streamNumber);
1096N/A buffer.clear();
1096N/A buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
1096N/A buffer.flip();
1096N/A position = buffer.position();
1096N/A remaining = buffer.remaining();
1096N/A
1096N/A debug("sending on stream number: " + streamNumber);
1096N/A debug("sending small message: " + buffer);
1096N/A try {
1096N/A sent = channel.send(buffer, info);
1096N/A fail("should have thrown InvalidStreamExcepton");
1096N/A } catch (InvalidStreamException ise){
1096N/A pass();
1096N/A } catch (IOException ioe) {
1096N/A unexpected(ioe);
1096N/A }
1096N/A check(buffer.remaining() == remaining,
1096N/A "remaining should not be changed");
1096N/A check(buffer.position() == position,
1096N/A "buffers position should not be changed");
1096N/A
1096N/A /* Receive CommUp */
1096N/A channel.receive(buffer, null, handler);
1096N/A check(handler.receivedCommUp(), "should have received COMM_UP");
1096N/A
1096N/A /* TEST 8: Send to an invalid preferred SocketAddress */
1096N/A SocketAddress addr = new InetSocketAddress("123.123.123.123", 3456);
1096N/A info = MessageInfo.createOutgoing(addr, 0);
1096N/A debug("sending to " + addr);
1096N/A debug("sending small message: " + buffer);
1096N/A try {
1096N/A sent = channel.send(buffer, info);
1096N/A fail("Invalid address should have thrown an Exception.");
1096N/A } catch (Exception e){
1096N/A pass();
1096N/A debug("OK, caught " + e);
1096N/A }
2092N/A
2092N/A /* TEST 9: Send from heap buffer to force implementation to
2092N/A * substitute with a native buffer, then check that its position
2092N/A * is updated correctly */
2092N/A buffer.clear();
2092N/A info = MessageInfo.createOutgoing(null, 0);
2092N/A buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
2092N/A buffer.flip();
2092N/A final int offset = 1;
2092N/A buffer.position(offset);
2092N/A remaining = buffer.remaining();
2092N/A
2092N/A debug("sending small message: " + buffer);
2092N/A try {
2092N/A sent = channel.send(buffer, info);
2092N/A
2092N/A check(sent == remaining, "sent should be equal to remaining");
2092N/A check(buffer.position() == (offset + sent),
2092N/A "buffers position should have been incremented by sent");
2092N/A } catch (IllegalArgumentException iae) {
2092N/A fail(iae + ", Error updating buffers position");
2092N/A }
2092N/A
1096N/A } catch (IOException ioe) {
1096N/A unexpected(ioe);
1096N/A } finally {
1096N/A clientFinishedLatch.countDown();
1096N/A try { serverFinishedLatch.await(10L, TimeUnit.SECONDS); }
1096N/A catch (InterruptedException ie) { unexpected(ie); }
1096N/A if (channel != null) {
1096N/A try { channel.close(); }
1096N/A catch (IOException e) { unexpected (e);}
1096N/A }
1096N/A }
1096N/A }
1096N/A
1096N/A class Server implements Runnable
1096N/A {
1096N/A final InetSocketAddress serverAddr;
1096N/A private SctpServerChannel ssc;
1096N/A
1096N/A public Server() throws IOException {
1096N/A ssc = SctpServerChannel.open().bind(null);
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 public void start() {
1096N/A (new Thread(this, "Server-" + 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 ByteBuffer buffer = ByteBuffer.allocateDirect(Util.LARGE_BUFFER);
1096N/A SctpChannel sc1 = null, sc2 = null;
1096N/A try {
1096N/A sc1 = ssc.accept();
1096N/A
1096N/A /* receive a small message */
1096N/A MessageInfo info;
1096N/A do {
1096N/A info = sc1.receive(buffer, null, null);
1096N/A if (info == null) {
1096N/A fail("Server: 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.streamNumber() == 0,
1096N/A "message not sent on the correct stream");
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 /* receive a large message */
1096N/A buffer.clear();
1096N/A do {
1096N/A info = sc1.receive(buffer, null, null);
1096N/A if (info == null) {
1096N/A fail("Server: 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.streamNumber() == handler.maxOutStreams() - 1,
1096N/A "message not sent on the correct stream");
1096N/A check(info.bytes() == Util.LARGE_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.LARGE_MESSAGE),
1096N/A "received message not the same as sent message");
1096N/A
1096N/A /* TEST 7 ++ */
1096N/A sc2 = ssc.accept();
1096N/A
2092N/A /* TEST 9 */
2092N/A ByteBuffer expected = ByteBuffer.allocate(Util.SMALL_BUFFER);
2092N/A expected.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
2092N/A expected.flip();
2092N/A final int offset = 1;
2092N/A expected.position(offset);
2092N/A buffer.clear();
2092N/A do {
2092N/A info = sc2.receive(buffer, null, null);
2092N/A if (info == null) {
2092N/A fail("Server: unexpected null from receive");
2092N/A return;
2092N/A }
2092N/A } while (!info.isComplete());
2092N/A
2092N/A buffer.flip();
2092N/A check(info != null, "info is null");
2092N/A check(info.streamNumber() == 0, "message not sent on the correct stream");
2092N/A check(info.bytes() == expected.remaining(),
2092N/A "bytes received not equal to message length");
2092N/A check(info.bytes() == buffer.remaining(), "bytes != remaining");
2092N/A check(expected.equals(buffer),
2092N/A "received message not the same as sent message");
2092N/A
1096N/A clientFinishedLatch.await(10L, TimeUnit.SECONDS);
1096N/A serverFinishedLatch.countDown();
1096N/A } catch (IOException ioe) {
1096N/A unexpected(ioe);
1096N/A } catch (InterruptedException ie) {
1096N/A unexpected(ie);
1096N/A } finally {
1096N/A try { if (ssc != null) ssc.close(); }
1096N/A catch (IOException unused) {}
1096N/A try { if (sc1 != null) sc1.close(); }
1096N/A catch (IOException unused) {}
1096N/A try { if (sc2 != null) sc2.close(); }
1096N/A catch (IOException unused) {}
1096N/A }
1096N/A }
1096N/A }
1096N/A
1096N/A class SendNotificationHandler extends AbstractNotificationHandler<Void>
1096N/A {
1096N/A boolean receivedCommUp; // false
1096N/A int maxInStreams;
1096N/A int maxOutStreams;
1096N/A
1096N/A public boolean receivedCommUp() {
1096N/A return receivedCommUp;
1096N/A }
1096N/A
1096N/A public int maxInStreams() {
1096N/A return maxInStreams;
1096N/A }
1096N/A
1096N/A public int maxOutStreams(){
1096N/A return maxOutStreams;
1096N/A }
1096N/A
1096N/A @Override
1096N/A public HandlerResult handleNotification(
1096N/A Notification notification, Void attachment) {
1096N/A fail("Unknown notification type");
1096N/A return HandlerResult.CONTINUE;
1096N/A }
1096N/A
1096N/A @Override
1096N/A public HandlerResult handleNotification(
1096N/A AssociationChangeNotification notification, Void attachment) {
1096N/A AssocChangeEvent event = notification.event();
1096N/A Association association = notification.association();
1096N/A debug("AssociationChangeNotification");
1096N/A debug(" Association: " + notification.association());
1096N/A debug(" Event: " + event);
1096N/A
1096N/A if (event.equals(AssocChangeEvent.COMM_UP))
1096N/A receivedCommUp = true;
1096N/A
1096N/A this.maxInStreams = association.maxInboundStreams();
1096N/A this.maxOutStreams = association.maxOutboundStreams();
1096N/A
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}