jdk/test/com/sun/nio/sctp/SctpMultiChannel/Send.java
changeset 2542 d859108aea12
child 4677 1b6ce3fbc01b
equal deleted inserted replaced
2418:15096652c4d4 2542:d859108aea12
       
     1 /*
       
     2  * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4927640
       
    26  * @summary Tests the SCTP protocol implementation
       
    27  * @author chegar
       
    28  */
       
    29 
       
    30 import java.net.InetSocketAddress;
       
    31 import java.net.SocketAddress;
       
    32 import java.io.IOException;
       
    33 import java.util.Set;
       
    34 import java.util.Iterator;
       
    35 import java.util.concurrent.CountDownLatch;
       
    36 import java.util.concurrent.TimeUnit;
       
    37 import java.nio.ByteBuffer;
       
    38 import com.sun.nio.sctp.Association;
       
    39 import com.sun.nio.sctp.InvalidStreamException;
       
    40 import com.sun.nio.sctp.MessageInfo;
       
    41 import com.sun.nio.sctp.SctpMultiChannel;
       
    42 import static java.lang.System.out;
       
    43 import static java.lang.System.err;
       
    44 
       
    45 public class Send {
       
    46     /* Latches used to synchronize between the client and server so that
       
    47      * connections without any IO may not be closed without being accepted */
       
    48     final CountDownLatch clientFinishedLatch = new CountDownLatch(1);
       
    49     final CountDownLatch serverFinishedLatch = new CountDownLatch(1);
       
    50 
       
    51     void test(String[] args) {
       
    52         SocketAddress address = null;
       
    53         Server server = null;
       
    54 
       
    55         if (!Util.isSCTPSupported()) {
       
    56             out.println("SCTP protocol is not supported");
       
    57             out.println("Test cannot be run");
       
    58             return;
       
    59         }
       
    60 
       
    61         if (args.length == 2) {
       
    62             /* requested to connecct to a specific address */
       
    63             try {
       
    64                 int port = Integer.valueOf(args[1]);
       
    65                 address = new InetSocketAddress(args[0], port);
       
    66             } catch (NumberFormatException nfe) {
       
    67                 err.println(nfe);
       
    68             }
       
    69         } else {
       
    70             /* start server on local machine, default */
       
    71             try {
       
    72                 server = new Server();
       
    73                 server.start();
       
    74                 address = server.address();
       
    75                 debug("Server started and listening on " + address);
       
    76             } catch (IOException ioe) {
       
    77                 ioe.printStackTrace();
       
    78                 return;
       
    79             }
       
    80         }
       
    81 
       
    82         doTest(address);
       
    83     }
       
    84 
       
    85     void doTest(SocketAddress peerAddress) {
       
    86         SctpMultiChannel channel = null;
       
    87         ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
       
    88         MessageInfo info = MessageInfo.createOutgoing(null, 0);
       
    89 
       
    90         try {
       
    91             channel = SctpMultiChannel.open();
       
    92 
       
    93             /* TEST 1: send small message */
       
    94             int streamNumber = 0;
       
    95             debug("sending to " + peerAddress + " on stream number: " + streamNumber);
       
    96             info = MessageInfo.createOutgoing(peerAddress, streamNumber);
       
    97             buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
       
    98             buffer.flip();
       
    99             int position = buffer.position();
       
   100             int remaining = buffer.remaining();
       
   101 
       
   102             debug("sending small message: " + buffer);
       
   103             int sent = channel.send(buffer, info);
       
   104 
       
   105             check(sent == remaining, "sent should be equal to remaining");
       
   106             check(buffer.position() == (position + sent),
       
   107                     "buffers position should have been incremented by sent");
       
   108 
       
   109             /* TEST 2: receive the echoed message */
       
   110             buffer.clear();
       
   111             info = channel.receive(buffer, null, null);
       
   112             buffer.flip();
       
   113             check(info != null, "info is null");
       
   114             check(info.streamNumber() == streamNumber,
       
   115                     "message not sent on the correct stream");
       
   116             check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
       
   117                   length, "bytes received not equal to message length");
       
   118             check(info.bytes() == buffer.remaining(), "bytes != remaining");
       
   119             check(Util.compare(buffer, Util.SMALL_MESSAGE),
       
   120               "received message not the same as sent message");
       
   121 
       
   122 
       
   123             /* TEST 3: send large message */
       
   124             Set<Association> assocs = channel.associations();
       
   125             check(assocs.size() == 1, "there should be only one association");
       
   126             Iterator<Association> it = assocs.iterator();
       
   127             check(it.hasNext());
       
   128             Association assoc = it.next();
       
   129             streamNumber = assoc.maxOutboundStreams() - 1;
       
   130 
       
   131             debug("sending on stream number: " + streamNumber);
       
   132             info = MessageInfo.createOutgoing(assoc, null, streamNumber);
       
   133             buffer.clear();
       
   134             buffer.put(Util.LARGE_MESSAGE.getBytes("ISO-8859-1"));
       
   135             buffer.flip();
       
   136             position = buffer.position();
       
   137             remaining = buffer.remaining();
       
   138 
       
   139             debug("sending large message: " + buffer);
       
   140             sent = channel.send(buffer, info);
       
   141 
       
   142             check(sent == remaining, "sent should be equal to remaining");
       
   143             check(buffer.position() == (position + sent),
       
   144                     "buffers position should have been incremented by sent");
       
   145 
       
   146             /* TEST 4: receive the echoed message */
       
   147             buffer.clear();
       
   148             info = channel.receive(buffer, null, null);
       
   149             buffer.flip();
       
   150             check(info != null, "info is null");
       
   151             check(info.streamNumber() == streamNumber,
       
   152                     "message not sent on the correct stream");
       
   153             check(info.bytes() == Util.LARGE_MESSAGE.getBytes("ISO-8859-1").
       
   154                   length, "bytes received not equal to message length");
       
   155             check(info.bytes() == buffer.remaining(), "bytes != remaining");
       
   156             check(Util.compare(buffer, Util.LARGE_MESSAGE),
       
   157               "received message not the same as sent message");
       
   158 
       
   159 
       
   160             /* TEST 5: InvalidStreamExcepton */
       
   161             streamNumber = assoc.maxOutboundStreams() + 1;
       
   162             info = MessageInfo.createOutgoing(assoc, null, streamNumber);
       
   163             buffer.clear();
       
   164             buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
       
   165             buffer.flip();
       
   166             position = buffer.position();
       
   167             remaining = buffer.remaining();
       
   168 
       
   169             debug("sending on stream number: " + streamNumber);
       
   170             debug("sending small message: " + buffer);
       
   171             try {
       
   172                 sent = channel.send(buffer, info);
       
   173                 fail("should have thrown InvalidStreamExcepton");
       
   174             } catch (InvalidStreamException ise){
       
   175                 pass();
       
   176             } catch (IOException ioe) {
       
   177                 unexpected(ioe);
       
   178             }
       
   179             check(buffer.remaining() == remaining,
       
   180                     "remaining should not be changed");
       
   181             check(buffer.position() == position,
       
   182                     "buffers position should not be changed");
       
   183 
       
   184 
       
   185             /* TEST 5: getRemoteAddresses(Association) */
       
   186             channel.getRemoteAddresses(assoc);
       
   187 
       
   188         } catch (IOException ioe) {
       
   189             unexpected(ioe);
       
   190         } finally {
       
   191             clientFinishedLatch.countDown();
       
   192             try { serverFinishedLatch.await(10L, TimeUnit.SECONDS); }
       
   193             catch (InterruptedException ie) { unexpected(ie); }
       
   194             if (channel != null) {
       
   195                 try { channel.close(); }
       
   196                 catch (IOException e) { unexpected (e);}
       
   197             }
       
   198         }
       
   199     }
       
   200 
       
   201     class Server implements Runnable
       
   202     {
       
   203         final InetSocketAddress serverAddr;
       
   204         private SctpMultiChannel serverChannel;
       
   205 
       
   206         public Server() throws IOException {
       
   207             serverChannel = SctpMultiChannel.open().bind(null);
       
   208             java.util.Set<SocketAddress> addrs = serverChannel.getAllLocalAddresses();
       
   209             if (addrs.isEmpty())
       
   210                 debug("addrs should not be empty");
       
   211 
       
   212             serverAddr = (InetSocketAddress) addrs.iterator().next();
       
   213         }
       
   214 
       
   215         public void start() {
       
   216             (new Thread(this, "Server-"  + serverAddr.getPort())).start();
       
   217         }
       
   218 
       
   219         public InetSocketAddress address() {
       
   220             return serverAddr;
       
   221         }
       
   222 
       
   223         @Override
       
   224         public void run() {
       
   225             ByteBuffer buffer = ByteBuffer.allocateDirect(Util.LARGE_BUFFER);
       
   226             try {
       
   227                 MessageInfo info;
       
   228 
       
   229                 /* receive a small message */
       
   230                 do {
       
   231                     info = serverChannel.receive(buffer, null, null);
       
   232                     if (info == null) {
       
   233                         fail("Server: unexpected null from receive");
       
   234                             return;
       
   235                     }
       
   236                 } while (!info.isComplete());
       
   237 
       
   238                 buffer.flip();
       
   239                 check(info != null, "info is null");
       
   240                 check(info.streamNumber() == 0,
       
   241                         "message not sent on the correct stream");
       
   242                 check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
       
   243                       length, "bytes received not equal to message length");
       
   244                 check(info.bytes() == buffer.remaining(), "bytes != remaining");
       
   245                 check(Util.compare(buffer, Util.SMALL_MESSAGE),
       
   246                   "received message not the same as sent message");
       
   247 
       
   248                 check(info != null, "info is null");
       
   249                 Set<Association> assocs = serverChannel.associations();
       
   250                 check(assocs.size() == 1, "there should be only one association");
       
   251                 Iterator<Association> it = assocs.iterator();
       
   252                 check(it.hasNext());
       
   253                 Association assoc = it.next();
       
   254 
       
   255                 /* echo the message */
       
   256                 debug("Server: echoing first message");
       
   257                 buffer.flip();
       
   258                 int bytes = serverChannel.send(buffer, info);
       
   259                 debug("Server: sent " + bytes + "bytes");
       
   260 
       
   261                 /* receive a large message */
       
   262                 buffer.clear();
       
   263                 do {
       
   264                     info = serverChannel.receive(buffer, null, null);
       
   265                     if (info == null) {
       
   266                         fail("Server: unexpected null from receive");
       
   267                             return;
       
   268                     }
       
   269                 } while (!info.isComplete());
       
   270 
       
   271                 buffer.flip();
       
   272 
       
   273                 check(info.streamNumber() == assoc.maxInboundStreams() - 1,
       
   274                         "message not sent on the correct stream");
       
   275                 check(info.bytes() == Util.LARGE_MESSAGE.getBytes("ISO-8859-1").
       
   276                       length, "bytes received not equal to message length");
       
   277                 check(info.bytes() == buffer.remaining(), "bytes != remaining");
       
   278                 check(Util.compare(buffer, Util.LARGE_MESSAGE),
       
   279                   "received message not the same as sent message");
       
   280 
       
   281                 /* echo the message */
       
   282                 debug("Server: echoing second message");
       
   283                 buffer.flip();
       
   284                 bytes = serverChannel.send(buffer, info);
       
   285                 debug("Server: sent " + bytes + "bytes");
       
   286 
       
   287                 clientFinishedLatch.await(10L, TimeUnit.SECONDS);
       
   288                 serverFinishedLatch.countDown();
       
   289             } catch (IOException ioe) {
       
   290                 unexpected(ioe);
       
   291             } catch (InterruptedException ie) {
       
   292                 unexpected(ie);
       
   293             } finally {
       
   294                 try { if (serverChannel != null) serverChannel.close(); }
       
   295                 catch (IOException  unused) {}
       
   296             }
       
   297         }
       
   298     }
       
   299 
       
   300         //--------------------- Infrastructure ---------------------------
       
   301     boolean debug = true;
       
   302     volatile int passed = 0, failed = 0;
       
   303     void pass() {passed++;}
       
   304     void fail() {failed++; Thread.dumpStack();}
       
   305     void fail(String msg) {System.err.println(msg); fail();}
       
   306     void unexpected(Throwable t) {failed++; t.printStackTrace();}
       
   307     void check(boolean cond) {if (cond) pass(); else fail();}
       
   308     void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
       
   309     void debug(String message) {if(debug) { System.out.println(message); }  }
       
   310     public static void main(String[] args) throws Throwable {
       
   311         Class<?> k = new Object(){}.getClass().getEnclosingClass();
       
   312         try {k.getMethod("instanceMain",String[].class)
       
   313                 .invoke( k.newInstance(), (Object) args);}
       
   314         catch (Throwable e) {throw e.getCause();}}
       
   315     public void instanceMain(String[] args) throws Throwable {
       
   316         try {test(args);} catch (Throwable t) {unexpected(t);}
       
   317         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
       
   318         if (failed > 0) throw new AssertionError("Some tests failed");}
       
   319 
       
   320 }