jdk/test/com/sun/nio/sctp/SctpServerChannel/Accept.java
author chegar
Thu, 16 Apr 2009 17:42:00 +0100
changeset 2542 d859108aea12
child 5506 202f599c92aa
permissions -rw-r--r--
4927640: Implementation of the sctp protocol Summary: An implementation-specific API for the Stream Control Transmission Protocol Reviewed-by: alanb, michaelm, jccollet
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     1
/*
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     4
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     8
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    13
 * accompanied this code).
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    14
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    18
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    21
 * have any questions.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    22
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    23
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    24
/* @test
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    25
 * @bug 4927640
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    26
 * @summary Tests the SCTP protocol implementation
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    27
 * @author chegar
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    28
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    29
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    30
import java.net.InetSocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    31
import java.net.SocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    32
import java.io.IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    33
import java.util.concurrent.CountDownLatch;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    34
import java.nio.channels.AlreadyConnectedException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
import java.nio.channels.AsynchronousCloseException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
import java.nio.channels.NotYetBoundException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import java.nio.channels.ClosedByInterruptException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import java.nio.channels.ClosedChannelException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
import com.sun.nio.sctp.SctpServerChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
import static java.lang.System.out;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
import static java.lang.System.err;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
public class Accept {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
    static CountDownLatch acceptLatch = new CountDownLatch(1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
    static CountDownLatch closeByIntLatch = new CountDownLatch(1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
    static CountDownLatch asyncCloseLatch = new CountDownLatch(1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
    AcceptServer server = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
    void test(String[] args) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
        SocketAddress address = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
        if (!Util.isSCTPSupported()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
            out.println("SCTP protocol is not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
            out.println("Test cannot be run");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
            return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    58
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
        if (args.length == 2) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
            /* requested to connecct to a specific address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
                int port = Integer.valueOf(args[1]);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
                address = new InetSocketAddress(args[0], port);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
            } catch (NumberFormatException nfe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
                err.println(nfe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
        } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
            /* start server on local machine, default */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
                server = new AcceptServer();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
                server.start();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
                address = server.address();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
                debug("Server started and listening on " + address);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
                ioe.printStackTrace();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
        doClient(address);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
    void doClient(SocketAddress peerAddress) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
        SctpChannel channel = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
            channel = SctpChannel.open(peerAddress, 0, 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
            acceptLatch.await();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
            /* for test 4 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
            closeByIntLatch.await();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
            sleep(500);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
            server.thread().interrupt();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
            /* for test 5 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
            asyncCloseLatch.await();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
            sleep(500);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
            server.channel().close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
            /* wait for the server thread to finish */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
            join(server.thread(), 10000);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
        } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
            unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
        } catch (InterruptedException ie) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
            unexpected(ie);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
            try { if (channel != null) channel.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
            catch (IOException e) { unexpected(e);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
    class AcceptServer implements Runnable
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
        final InetSocketAddress serverAddr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
        private SctpServerChannel ssc;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
        private Thread serverThread;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
        public AcceptServer() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
            ssc = SctpServerChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
            /* TEST 1: NotYetBoundException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
            debug("TEST 1: NotYetBoundException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
                ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
                fail();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
            } catch (NotYetBoundException nybe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
                debug("  caught NotYetBoundException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
            ssc.bind(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
            java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
            if (addrs.isEmpty())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
                debug("addrs should not be empty");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
            serverAddr = (InetSocketAddress) addrs.iterator().next();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
            /* TEST 2: null if this channel is in non-blocking mode and no
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
             *         association is available to be accepted  */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
            ssc.configureBlocking(false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
            debug("TEST 2: non-blocking mode null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   145
                SctpChannel sc = ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
                check(sc == null, "non-blocking mode should return null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
                ssc.configureBlocking(true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   153
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
        void start() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
            serverThread = new Thread(this, "AcceptServer-"  +
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
                                              serverAddr.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   157
            serverThread.start();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   158
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   159
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   160
        InetSocketAddress address() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   161
            return serverAddr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   162
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   163
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   164
        SctpServerChannel channel() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   165
            return ssc;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   166
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   167
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
        Thread thread() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
            return serverThread;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   170
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   171
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   172
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
        public void run() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
            SctpChannel sc = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   176
                /* TEST 3: accepted channel */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   177
                debug("TEST 3: accepted channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   178
                sc = ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   180
                checkAcceptedChannel(sc);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   181
                acceptLatch.countDown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   182
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   183
                /* TEST 4: ClosedByInterruptException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
                debug("TEST 4: ClosedByInterruptException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   185
                try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   186
                    closeByIntLatch.countDown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
                    ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
                    fail();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
                } catch (ClosedByInterruptException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
                    debug("  caught ClosedByInterruptException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
                    pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   192
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   193
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   194
                /* TEST 5: AsynchronousCloseException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   195
                debug("TEST 5: AsynchronousCloseException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   196
                /* reset thread interrupt status */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   197
                Thread.currentThread().interrupted();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   199
                ssc = SctpServerChannel.open().bind(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   200
                try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   201
                    asyncCloseLatch.countDown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   202
                    ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
                    fail();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   204
                } catch (AsynchronousCloseException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   205
                    debug("  caught AsynchronousCloseException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   206
                    pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   207
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   208
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   209
                /* TEST 6: ClosedChannelException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
                debug("TEST 6: ClosedChannelException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
                try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   212
                    ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   213
                    fail();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   214
                } catch (ClosedChannelException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
                    debug("  caught ClosedChannelException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   216
                    pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   217
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
                ssc = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
                ioe.printStackTrace();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
                try { if (ssc != null) ssc.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
                catch (IOException  ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
                try { if (sc != null) sc.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
                catch (IOException  ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
    void checkAcceptedChannel(SctpChannel sc) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   232
            debug("Checking accepted SctpChannel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   233
            check(sc.association() != null,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   234
                  "accepted channel should have an association");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
            check(!(sc.getRemoteAddresses().isEmpty()),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
                  "accepted channel should be connected");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
            check(!(sc.isConnectionPending()),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
                  "accepted channel should not have a connection pending");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
            check(sc.isBlocking(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
                  "accepted channel should be blocking");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
            try { sc.connect(new TestSocketAddress()); fail(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
            catch (AlreadyConnectedException unused) { pass(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
            try { sc.bind(new TestSocketAddress()); fail(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
            catch (AlreadyConnectedException unused) { pass(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
        } catch (IOException unused) { fail(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
    static class TestSocketAddress extends SocketAddress {}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
        //--------------------- Infrastructure ---------------------------
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
    boolean debug = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
    volatile int passed = 0, failed = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   253
    void pass() {passed++;}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   254
    void fail() {failed++; Thread.dumpStack();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   255
    void fail(String msg) {err.println(msg); fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
    void check(boolean cond) {if (cond) pass(); else fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   258
    void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   259
    void debug(String message) {if(debug) { out.println(message); }  }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   260
    void sleep(long millis) { try { Thread.currentThread().sleep(millis); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   261
                          catch(InterruptedException ie) { unexpected(ie); }}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   262
    void join(Thread thread, long millis) { try { thread.join(millis); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   263
                          catch(InterruptedException ie) { unexpected(ie); }}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
    public static void main(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
        Class<?> k = new Object(){}.getClass().getEnclosingClass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
        try {k.getMethod("instanceMain",String[].class)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
                .invoke( k.newInstance(), (Object) args);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   268
        catch (Throwable e) {throw e.getCause();}}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   269
    public void instanceMain(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   270
        try {test(args);} catch (Throwable t) {unexpected(t);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
        out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   272
        if (failed > 0) throw new AssertionError("Some tests failed");}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   273
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   274
}