test/jdk/com/sun/nio/sctp/SctpChannel/Bind.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:
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     1
/*
59205
afcc8af048e6 8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents: 47216
diff changeset
     2
 * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
2542
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2542
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2542
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2542
diff changeset
    21
 * questions.
2542
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.*;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    31
import java.io.*;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    32
import java.util.List;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    33
import java.util.Set;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    34
import java.util.Iterator;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
import java.nio.ByteBuffer;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
import java.nio.channels.AlreadyBoundException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import java.nio.channels.AlreadyConnectedException;
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 java.nio.channels.UnsupportedAddressTypeException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
import com.sun.nio.sctp.AssociationChangeNotification;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
import com.sun.nio.sctp.AbstractNotificationHandler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
import com.sun.nio.sctp.HandlerResult;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
import com.sun.nio.sctp.IllegalUnbindException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
import com.sun.nio.sctp.MessageInfo;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
import com.sun.nio.sctp.PeerAddressChangeNotification;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
import com.sun.nio.sctp.SctpServerChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
import com.sun.nio.sctp.ShutdownNotification;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
import static java.lang.System.out;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
/**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
 * Tests bind, bindAddress, unbindAddress, getLocalAddress, and
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
 * getAllLocalAddresses.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
public class Bind {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
     void test(String[] args) {
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
        /* Simply bind tests */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
        testBind();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
        /* Test unconnected */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
        testBindUnbind(false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
        /* Test connected */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
        /* Adding/Removing addresses from a connected association is optional.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
         * This test can be run on systems that support dynamic address
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
         * reconfiguration */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
        //testBindUnbind(true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
    void testBind() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
        SctpChannel channel = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
            channel = SctpChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
            /* TEST 1: empty set if channel is not bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
            check(channel.getAllLocalAddresses().isEmpty(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
                    "getAllLocalAddresses returned non empty set for unbound channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
            /* TEST 2: null to bind the channel to an automatically assigned
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
             *         socket address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
            channel.bind(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
            /* TEST 3: non empty set if the channel is bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
            check(!channel.getAllLocalAddresses().isEmpty(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
                    "getAllLocalAddresses returned empty set for bound channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
            debug("getAllLocalAddresses on channel bound to the wildcard:\n"
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
                    + channel.getAllLocalAddresses());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
            /* TEST 4: AlreadyBoundException if this channel is already bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
            try { channel.bind(null); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
            catch (AlreadyBoundException unused) { pass(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
            catch (IOException ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
            /* TEST 5: UnsupportedAddressTypeException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
                channel.close();  /* open a new unbound channel for test */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
                channel = SctpChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
                channel.bind(new UnsupportedSocketAddress());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
                fail("UnsupportedSocketAddress expected");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
            } catch (UnsupportedAddressTypeException unused) { pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
            } catch (IOException ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
            /* TEST 6: AlreadyConnectedException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
                channel.close();  /* open a new unbound channel for test */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
                channel = SctpChannel.open();
59205
afcc8af048e6 8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents: 47216
diff changeset
   113
                try (var peer = connectChannel(channel)) {
afcc8af048e6 8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents: 47216
diff changeset
   114
                    channel.bind(null);
afcc8af048e6 8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents: 47216
diff changeset
   115
                    fail("AlreadyConnectedException expected");
afcc8af048e6 8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents: 47216
diff changeset
   116
                }
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
            } catch (AlreadyConnectedException unused) { pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
            } catch (IOException ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
            /* TEST 7: ClosedChannelException - If this channel is closed */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
                channel.close();  /* open a new unbound channel for test */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
                channel = SctpChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
                channel.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
                channel.bind(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
                fail("ClosedChannelException expected");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
            } catch (ClosedChannelException unused) { pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
            } catch (IOException ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
            /* TEST 8: ClosedChannelException if channel is closed */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
                channel.getAllLocalAddresses();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
                fail("should have thrown ClosedChannelException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
            } catch (ClosedChannelException cce) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
               pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
            } catch (Exception ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
        } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
            unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
            try { channel.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
            catch (IOException ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   145
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
    void testBindUnbind(boolean connected) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
        SctpChannel channel = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
        SctpChannel peerChannel = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
        debug("testBindUnbind, connected: " + connected);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   153
            channel = SctpChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
            List<InetAddress> addresses = Util.getAddresses(true, false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
            Iterator iterator = addresses.iterator();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   157
            InetSocketAddress a = new InetSocketAddress((InetAddress)iterator.next(), 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   158
            debug("channel.bind( " + a + ")");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   159
            channel.bind(a);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   160
            while (iterator.hasNext()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   161
                InetAddress ia = (InetAddress)iterator.next();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   162
                debug("channel.bindAddress(" + ia + ")");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   163
                channel.bindAddress(ia);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   164
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   165
            if (debug) {Util.dumpAddresses(channel, out);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   166
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   167
            if (connected) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
                /* Test with connected channel */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
                peerChannel = connectChannel(channel);
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
            /* TEST 1: bind/unbindAddresses on the system addresses */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
            debug("bind/unbindAddresses on the system addresses");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
            List<InetAddress> addrs = Util.getAddresses(true, false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
            for (InetAddress addr : addrs) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   176
                try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   177
                    debug("unbindAddress: " + addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   178
                    check(boundAddress(channel, addr), "trying to remove address that is not bound");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
                    channel.unbindAddress(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   180
                    if (debug) {Util.dumpAddresses(channel, out);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   181
                    check(!boundAddress(channel, addr), "address was not removed");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   182
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   183
                    debug("bindAddress: " + addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
                    channel.bindAddress(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   185
                    if (debug) {Util.dumpAddresses(channel, out);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   186
                    check(boundAddress(channel, addr), "address is not bound");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
                } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
                    unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   192
            /* TEST 2: bindAddress - already bound address. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   193
            InetAddress againAddress = addrs.get(0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   194
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   195
                debug("bind already bound address " + againAddress);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   196
                channel.bindAddress(againAddress);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   197
            } catch (AlreadyBoundException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
                debug("Caught AlreadyBoundException - OK");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   199
                pass();
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
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   204
            /* TEST 3: bind non local address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   205
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   206
                InetAddress nla = InetAddress.getByName("123.123.123.123");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   207
                debug("bind non local address " + nla);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   208
                channel.bindAddress(nla);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   209
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
                debug("Informative only " + ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   212
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   213
            /* TEST 4: unbind address that is not bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   214
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
                debug("unbind address that is not bound " + againAddress);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   216
                /* remove address first then again */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   217
                channel.unbindAddress(againAddress);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
                channel.unbindAddress(againAddress);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
            } catch (IllegalUnbindException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
                debug("Caught IllegalUnbindException - OK");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
            /* TEST 5: unbind address that is not bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
                InetAddress nla = InetAddress.getByName("123.123.123.123");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
                debug("unbind address that is not bound " + nla);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
                channel.unbindAddress(nla);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   232
            } catch (IllegalUnbindException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   233
                debug("Caught IllegalUnbindException - OK");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   234
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
            if (connected) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
                channel.shutdown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
                BindNotificationHandler handler = new BindNotificationHandler();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
                ByteBuffer buffer = ByteBuffer.allocate(10);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
                MessageInfo info;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
                while((info = peerChannel.receive(buffer, null, handler)) != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
                    if (info != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
                        if (info.bytes() == -1) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
                            debug("peerChannel Reached EOF");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
                            break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   253
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   254
                while((info = channel.receive(buffer, null, handler)) != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   255
                    if (info != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
                        if (info.bytes() == -1) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
                            debug("channel Reached EOF");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   258
                            break;
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
        } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
            ioe.printStackTrace();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
            try { if (channel != null) channel.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
            catch (IOException ioe) { unexpected(ioe); }
59205
afcc8af048e6 8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents: 47216
diff changeset
   268
            try { if (peerChannel != null) peerChannel.close(); }
afcc8af048e6 8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents: 47216
diff changeset
   269
            catch (IOException ioe) { unexpected(ioe); }
afcc8af048e6 8233847: (sctp) Flx link-local IPv6 scope handling and test cleanup
chegar
parents: 47216
diff changeset
   270
         }
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   272
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   273
    boolean boundAddress(SctpChannel channel, InetAddress addr)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   274
        throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   275
        for (SocketAddress boundAddr : channel.getAllLocalAddresses()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   276
            if (((InetSocketAddress) boundAddr).getAddress().equals(addr))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   277
                return true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   278
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   279
        return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   280
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   281
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   282
    SctpChannel connectChannel(SctpChannel channel)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   283
        throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   284
        debug("connecting channel...");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   285
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   286
            SctpServerChannel ssc = SctpServerChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   287
            ssc.bind(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   288
            Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   289
            Iterator<SocketAddress> iterator = addrs.iterator();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   290
            SocketAddress addr = iterator.next();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   291
            debug("using " + addr + "...");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   292
            channel.connect(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   293
            SctpChannel peerChannel = ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   294
            ssc.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   295
            debug("connected");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   296
            return peerChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   297
        } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   298
            debug("Cannot connect channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   299
            unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   300
            throw ioe;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   301
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   302
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   303
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   304
    class BindNotificationHandler extends AbstractNotificationHandler<Object>
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   305
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   306
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   307
        public HandlerResult handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   308
                AssociationChangeNotification acn, Object unused)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   309
        {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   310
            debug("AssociationChangeNotification: " +  acn);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   311
            return HandlerResult.CONTINUE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   312
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   313
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   314
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   315
        public HandlerResult handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   316
                PeerAddressChangeNotification pacn, Object unused)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   317
        {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   318
            debug("PeerAddressChangeNotification: " +  pacn);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   319
            return HandlerResult.CONTINUE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   320
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   321
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   322
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   323
        public HandlerResult handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   324
                ShutdownNotification sn, Object unused)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   325
        {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   326
            debug("ShutdownNotification: " +  sn);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   327
            return HandlerResult.CONTINUE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   328
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   329
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   330
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   331
    class UnsupportedSocketAddress extends SocketAddress { }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   332
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   333
    //--------------------- Infrastructure ---------------------------
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   334
    boolean debug = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   335
    volatile int passed = 0, failed = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   336
    void pass() {passed++;}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   337
    void fail() {failed++; Thread.dumpStack();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   338
    void fail(String msg) {System.err.println(msg); fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   339
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   340
    void check(boolean cond) {if (cond) pass(); else fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   341
    void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   342
    void debug(String message) {if(debug) { System.out.println(message); }  }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   343
    public static void main(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   344
        Class<?> k = new Object(){}.getClass().getEnclosingClass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   345
        try {k.getMethod("instanceMain",String[].class)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   346
                .invoke( k.newInstance(), (Object) args);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   347
        catch (Throwable e) {throw e.getCause();}}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   348
    public void instanceMain(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   349
        try {test(args);} catch (Throwable t) {unexpected(t);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   350
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   351
        if (failed > 0) throw new AssertionError("Some tests failed");}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   352
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   353
}