test/jdk/com/sun/nio/sctp/SctpMultiChannel/Branch.java
author chegar
Thu, 21 Nov 2019 12:14:29 +0000
changeset 59205 afcc8af048e6
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3320
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     4
 *
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     8
 *
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    13
 * accompanied this code).
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    14
 *
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3320
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3320
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3320
diff changeset
    21
 * questions.
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    22
 */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    23
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    24
/* @test
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    25
 * @bug 4927640
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    26
 * @summary Tests the SCTP protocol implementation
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    27
 * @author chegar
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    28
 */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    29
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    30
import java.net.InetSocketAddress;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    31
import java.net.SocketAddress;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    32
import java.io.IOException;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    33
import java.util.Set;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    34
import java.util.Iterator;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    35
import java.util.concurrent.CountDownLatch;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    36
import java.util.concurrent.TimeUnit;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    37
import java.nio.ByteBuffer;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    38
import com.sun.nio.sctp.AbstractNotificationHandler;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    39
import com.sun.nio.sctp.Association;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    40
import com.sun.nio.sctp.AssociationChangeNotification;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    41
import com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    42
import com.sun.nio.sctp.HandlerResult;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    43
import com.sun.nio.sctp.InvalidStreamException;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    44
import com.sun.nio.sctp.MessageInfo;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    45
import com.sun.nio.sctp.SctpChannel;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    46
import com.sun.nio.sctp.SctpMultiChannel;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    47
import com.sun.nio.sctp.ShutdownNotification;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    48
import static java.lang.System.out;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    49
import static java.lang.System.err;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    50
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    51
public class Branch {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    52
    /* Latches used to synchronize between the client and server so that
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    53
     * connections without any IO may not be closed without being accepted */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    54
    final CountDownLatch clientFinishedLatch = new CountDownLatch(1);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    55
    final CountDownLatch serverFinishedLatch = new CountDownLatch(1);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    56
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    57
    void test(String[] args) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    58
        SocketAddress address = null;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    59
        Server server = null;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    60
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    61
        if (!Util.isSCTPSupported()) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    62
            out.println("SCTP protocol is not supported");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    63
            out.println("Test cannot be run");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    64
            return;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    65
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    66
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    67
        if (args.length == 2) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    68
            /* requested to connecct to a specific address */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    69
            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    70
                int port = Integer.valueOf(args[1]);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    71
                address = new InetSocketAddress(args[0], port);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    72
            } catch (NumberFormatException nfe) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    73
                err.println(nfe);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    74
            }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    75
        } else {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    76
            /* start server on local machine, default */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    77
            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    78
                server = new Server();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    79
                server.start();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    80
                address = server.address();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    81
                debug("Server started and listening on " + address);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    82
            } catch (IOException ioe) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    83
                ioe.printStackTrace();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    84
                return;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    85
            }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    86
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    87
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    88
        doTest(address);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    89
    }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    90
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    91
    void doTest(SocketAddress peerAddress) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    92
        SctpMultiChannel channel = null;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    93
        ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    94
        MessageInfo info = MessageInfo.createOutgoing(null, 0);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    95
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    96
        try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    97
            channel = SctpMultiChannel.open();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    98
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    99
            /* setup an association implicitly by sending a small message */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   100
            int streamNumber = 0;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   101
            debug("sending to " + peerAddress + " on stream number: " + streamNumber);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   102
            info = MessageInfo.createOutgoing(peerAddress, streamNumber);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   103
            buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   104
            buffer.flip();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   105
            int position = buffer.position();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   106
            int remaining = buffer.remaining();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   107
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   108
            debug("sending small message: " + buffer);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   109
            int sent = channel.send(buffer, info);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   110
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   111
            check(sent == remaining, "sent should be equal to remaining");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   112
            check(buffer.position() == (position + sent),
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   113
                    "buffers position should have been incremented by sent");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   114
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   115
            /* Receive the COMM_UP */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   116
            buffer.clear();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   117
            BranchNotificationHandler handler = new BranchNotificationHandler();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   118
            info = channel.receive(buffer, null, handler);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   119
            check(handler.receivedCommUp(), "COMM_UP no received");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   120
            Set<Association> associations = channel.associations();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   121
            check(!associations.isEmpty(),"There should be some associations");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   122
            Association bassoc = associations.iterator().next();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   123
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   124
            /* TEST 1: branch */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   125
            SctpChannel bchannel = channel.branch(bassoc);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   126
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   127
            check(!bchannel.getAllLocalAddresses().isEmpty(),
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   128
                                   "branched channel should be bound");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   129
            check(!bchannel.getRemoteAddresses().isEmpty(),
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   130
                                   "branched channel should be connected");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   131
            check(channel.associations().isEmpty(),
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   132
                  "there should be no associations since the only one was branched off");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   133
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   134
            buffer.clear();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   135
            info = bchannel.receive(buffer, null, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   136
            buffer.flip();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   137
            check(info != null, "info is null");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   138
            check(info.streamNumber() == streamNumber,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   139
                    "message not sent on the correct stream");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   140
            check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   141
                  length, "bytes received not equal to message length");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   142
            check(info.bytes() == buffer.remaining(), "bytes != remaining");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   143
            check(Util.compare(buffer, Util.SMALL_MESSAGE),
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   144
              "received message not the same as sent message");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   145
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   146
        } catch (IOException ioe) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   147
            unexpected(ioe);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   148
        } finally {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   149
            clientFinishedLatch.countDown();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   150
            try { serverFinishedLatch.await(10L, TimeUnit.SECONDS); }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   151
            catch (InterruptedException ie) { unexpected(ie); }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   152
            if (channel != null) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   153
                try { channel.close(); }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   154
                catch (IOException e) { unexpected (e);}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   155
            }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   156
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   157
    }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   158
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   159
    class Server implements Runnable
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   160
    {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   161
        final InetSocketAddress serverAddr;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   162
        private SctpMultiChannel serverChannel;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   163
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   164
        public Server() throws IOException {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   165
            serverChannel = SctpMultiChannel.open().bind(null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   166
            java.util.Set<SocketAddress> addrs = serverChannel.getAllLocalAddresses();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   167
            if (addrs.isEmpty())
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   168
                debug("addrs should not be empty");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   169
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   170
            serverAddr = (InetSocketAddress) addrs.iterator().next();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   171
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   172
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   173
        public void start() {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   174
            (new Thread(this, "Server-"  + serverAddr.getPort())).start();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   175
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   176
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   177
        public InetSocketAddress address() {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   178
            return serverAddr;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   179
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   180
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   181
        @Override
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   182
        public void run() {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   183
            ByteBuffer buffer = ByteBuffer.allocateDirect(Util.LARGE_BUFFER);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   184
            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   185
                MessageInfo info;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   186
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   187
                /* receive a small message */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   188
                do {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   189
                    info = serverChannel.receive(buffer, null, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   190
                    if (info == null) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   191
                        fail("Server: unexpected null from receive");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   192
                            return;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   193
                    }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   194
                } while (!info.isComplete());
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   195
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   196
                buffer.flip();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   197
                check(info != null, "info is null");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   198
                check(info.streamNumber() == 0,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   199
                        "message not sent on the correct stream");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   200
                check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   201
                      length, "bytes received not equal to message length");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   202
                check(info.bytes() == buffer.remaining(), "bytes != remaining");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   203
                check(Util.compare(buffer, Util.SMALL_MESSAGE),
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   204
                  "received message not the same as sent message");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   205
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   206
                check(info != null, "info is null");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   207
                Set<Association> assocs = serverChannel.associations();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   208
                check(assocs.size() == 1, "there should be only one association");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   209
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   210
                /* echo the message */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   211
                debug("Server: echoing first message");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   212
                buffer.flip();
59205
afcc8af048e6 8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents: 47216
diff changeset
   213
                MessageInfo sendInfo = MessageInfo.createOutgoing(info.association(), null, 0);
afcc8af048e6 8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents: 47216
diff changeset
   214
                int bytes = serverChannel.send(buffer, sendInfo);
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   215
                debug("Server: sent " + bytes + "bytes");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   216
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   217
                clientFinishedLatch.await(10L, TimeUnit.SECONDS);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   218
                serverFinishedLatch.countDown();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   219
            } catch (IOException ioe) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   220
                unexpected(ioe);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   221
            } catch (InterruptedException ie) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   222
                unexpected(ie);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   223
            } finally {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   224
                try { if (serverChannel != null) serverChannel.close(); }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   225
                catch (IOException  unused) {}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   226
            }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   227
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   228
    }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   229
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   230
    class BranchNotificationHandler extends AbstractNotificationHandler<Object>
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   231
    {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   232
        boolean receivedCommUp;  // false
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   233
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   234
        boolean receivedCommUp() {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   235
            return receivedCommUp;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   236
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   237
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   238
        @Override
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   239
        public HandlerResult handleNotification(
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   240
                AssociationChangeNotification notification, Object attachment) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   241
            AssocChangeEvent event = notification.event();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   242
            debug("AssociationChangeNotification");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   243
            debug("  Association: " + notification.association());
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   244
            debug("  Event: " + event);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   245
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   246
            if (event.equals(AssocChangeEvent.COMM_UP))
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   247
                receivedCommUp = true;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   248
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   249
            return HandlerResult.RETURN;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   250
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   251
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   252
        /* A ShutdownNotification handler is provided to ensure that no
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   253
         * shutdown notification are being handled since we don't expect
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   254
         * to receive them. This is not part of branch testing, it just
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   255
         * fits here to test another bug. */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   256
        @Override
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   257
        public HandlerResult handleNotification(
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   258
                ShutdownNotification notification, Object attachment) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   259
            debug("ShutdownNotification");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   260
            debug("  Association: " + notification.association());
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   261
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   262
            fail("Shutdown should not be received");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   263
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   264
            return HandlerResult.RETURN;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   265
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   266
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   267
    }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   268
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   269
        //--------------------- Infrastructure ---------------------------
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   270
    boolean debug = true;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   271
    volatile int passed = 0, failed = 0;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   272
    void pass() {passed++;}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   273
    void fail() {failed++; Thread.dumpStack();}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   274
    void fail(String msg) {System.err.println(msg); fail();}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   275
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   276
    void check(boolean cond) {if (cond) pass(); else fail();}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   277
    void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   278
    void debug(String message) {if(debug) { System.out.println(message); }  }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   279
    public static void main(String[] args) throws Throwable {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   280
        Class<?> k = new Object(){}.getClass().getEnclosingClass();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   281
        try {k.getMethod("instanceMain",String[].class)
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   282
                .invoke( k.newInstance(), (Object) args);}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   283
        catch (Throwable e) {throw e.getCause();}}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   284
    public void instanceMain(String[] args) throws Throwable {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   285
        try {test(args);} catch (Throwable t) {unexpected(t);}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   286
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   287
        if (failed > 0) throw new AssertionError("Some tests failed");}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   288
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   289
}