jdk/test/com/sun/nio/sctp/SctpChannel/Connect.java
author chegar
Thu, 16 Apr 2009 17:42:00 +0100
changeset 2542 d859108aea12
child 3072 a801b122142f
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.Callable;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    34
import java.util.concurrent.CountDownLatch;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
import java.nio.channels.AlreadyConnectedException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
import java.nio.channels.ClosedChannelException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import java.nio.channels.ConnectionPendingException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import java.nio.channels.NoConnectionPendingException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
import java.nio.channels.UnresolvedAddressException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
import java.nio.channels.UnsupportedAddressTypeException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
import com.sun.nio.sctp.SctpServerChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
import static java.lang.System.out;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
import static java.lang.System.err;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
/**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
 * Tests connect, finishConnect, isConnectionPending,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
 * getRemoteAddresses and association.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
public class Connect {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
    final CountDownLatch finishedLatch = new CountDownLatch(1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
    void test(String[] args) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
        SocketAddress address = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
        Server server = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
        if (!Util.isSCTPSupported()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    58
            out.println("SCTP protocol is not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
            out.println("Test cannot be run");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
            return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
        if (args.length == 2) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
            /* requested to connect to a specific address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
                int port = Integer.valueOf(args[1]);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
                address = new InetSocketAddress(args[0], port);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
            } catch (NumberFormatException nfe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
                err.println(nfe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
        } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
            /* start server on local machine, default */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
                server = new Server();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
                server.start();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
                address = server.address();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
                debug("Server started and listening on " + address);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
                ioe.printStackTrace();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
                return;
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
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
        doTest(address);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
    void doTest(SocketAddress addr) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
        SctpChannel channel = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
        final SocketAddress peerAddress = addr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
            channel = SctpChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
            /* TEST 0.5 Verify default values for new/unconnected channel */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
            check(channel.getRemoteAddresses().isEmpty(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
                    "non empty set for unconnected channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
            check(channel.association() == null,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
                    "non-null association for unconnected channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
            check(!channel.isConnectionPending(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
                    "should not have a connection pending");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
            /* TEST 1: non-blocking connect */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
            channel.configureBlocking(false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
            if (channel.connect(peerAddress) != true) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
                debug("non-blocking connect did not immediately succeed");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
                check(channel.isConnectionPending(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
                        "should return true for isConnectionPending");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
                try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
                    channel.connect(peerAddress);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
                    fail("should have thrown ConnectionPendingException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
                } catch (ConnectionPendingException cpe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
                    pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
                } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
                    unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
                channel.configureBlocking(true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
                check(channel.finishConnect(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
                        "finishConnect should have returned true");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
            }
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.5 Verify after connect */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
            check(!channel.getRemoteAddresses().isEmpty(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
                    "empty set for connected channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
            check(channel.association() != null,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
                    "null association for connected channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
            check(!channel.isConnectionPending(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
                    "pending connection for connected channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
            /* TEST 2: Verify AlreadyConnectedException thrown */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
                channel.connect(peerAddress);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
                fail("should have thrown AlreadyConnectedException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
            } catch (AlreadyConnectedException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
            }  catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
            /* TEST 3: UnresolvedAddressException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
            channel.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
            channel = SctpChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
            InetSocketAddress unresolved =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
                    InetSocketAddress.createUnresolved("xxyyzzabc", 4567);
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
                channel.connect(unresolved);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
                fail("should have thrown UnresolvedAddressException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
            } catch (UnresolvedAddressException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
            }  catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
                unexpected(ioe);
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
            /* TEST 4: UnsupportedAddressTypeException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
            SocketAddress unsupported = new UnsupportedSocketAddress();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
                channel.connect(unsupported);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   157
                fail("should have thrown UnsupportedAddressTypeException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   158
            } catch (UnsupportedAddressTypeException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   159
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   160
            }  catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   161
                unexpected(ioe);
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
            /* TEST 5: ClosedChannelException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   165
            channel.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   166
            final SctpChannel closedChannel = channel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   167
            testCCE(new Callable<Void>() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
                public Void call() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
                    closedChannel.connect(peerAddress); return null; } });
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   170
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   171
            /* TEST 5.5 getRemoteAddresses */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   172
            testCCE(new Callable<Void>() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
                public Void call() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
                    closedChannel.getRemoteAddresses(); return null; } });
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
            testCCE(new Callable<Void>() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   176
                public Void call() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   177
                    closedChannel.association(); return null; } });
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   178
            check(!channel.isConnectionPending(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
                    "pending connection for closed channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   180
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   181
            /* Run some more finishConnect tests */
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 6: NoConnectionPendingException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
            channel = SctpChannel.open();
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
                channel.finishConnect();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
                fail("should have thrown NoConnectionPendingException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
            } catch (NoConnectionPendingException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
            }  catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
                unexpected(ioe);
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 7: ClosedChannelException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   195
            channel.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   196
            final SctpChannel cceChannel = channel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   197
            testCCE(new Callable<Void>() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
                public Void call() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   199
                    cceChannel.finishConnect(); return null; } });
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   200
        } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   201
            unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   202
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
            finishedLatch.countDown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   204
            try { if (channel != null) channel.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   205
            catch (IOException e) { unexpected(e);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   206
        }
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
    class UnsupportedSocketAddress extends SocketAddress { }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
    void testCCE(Callable callable) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   212
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   213
            callable.call();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   214
            fail("should have thrown ClosedChannelException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
        } catch (ClosedChannelException cce) {
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
        } catch (Exception ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
            unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
    class Server implements Runnable
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
        final InetSocketAddress serverAddr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
        private SctpServerChannel ssc;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
        public Server() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
            ssc = SctpServerChannel.open().bind(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
            java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
            if (addrs.isEmpty())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
                debug("addrs should not be empty");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   232
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   233
            serverAddr = (InetSocketAddress) addrs.iterator().next();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   234
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
        public void start() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
            (new Thread(this, "Server-"  + serverAddr.getPort())).start();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
        public InetSocketAddress address() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
            return serverAddr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
        public void run() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
            SctpChannel sc = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
                sc = ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
                finishedLatch.await();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
            } catch (InterruptedException ie) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   253
                unexpected(ie);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   254
            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   255
                try { if (ssc != null) ssc.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
                catch (IOException  ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
                try { if (sc != null) sc.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   258
                catch (IOException  ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   259
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   260
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   261
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   262
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   263
        //--------------------- Infrastructure ---------------------------
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
    boolean debug = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
    volatile int passed = 0, failed = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
    void pass() {passed++;}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
    void fail() {failed++; Thread.dumpStack();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   268
    void fail(String msg) {System.err.println(msg); fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   269
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   270
    void check(boolean cond) {if (cond) pass(); else fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
    void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   272
    void debug(String message) {if(debug) { System.out.println(message); }  }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   273
    public static void main(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   274
        Class<?> k = new Object(){}.getClass().getEnclosingClass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   275
        try {k.getMethod("instanceMain",String[].class)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   276
                .invoke( k.newInstance(), (Object) args);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   277
        catch (Throwable e) {throw e.getCause();}}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   278
    public void instanceMain(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   279
        try {test(args);} catch (Throwable t) {unexpected(t);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   280
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   281
        if (failed > 0) throw new AssertionError("Some tests failed");}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   282
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   283
}