jdk/test/com/sun/nio/sctp/SctpChannel/SocketOptionTests.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.io.IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    31
import java.util.Set;
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.Arrays;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    34
import java.nio.channels.ClosedChannelException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
import com.sun.nio.sctp.SctpSocketOption;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import static com.sun.nio.sctp.SctpStandardSocketOption.*;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import static java.lang.System.out;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
public class SocketOptionTests {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
    <T> void checkOption(SctpChannel sc, SctpSocketOption<T> name,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
            T expectedValue) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
        T value = sc.getOption(name);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
        check(value.equals(expectedValue), name + ": value (" + value +
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
                ") not as expected (" + expectedValue + ")");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
       }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
    <T> void optionalSupport(SctpChannel sc, SctpSocketOption<T> name,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
            T value) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
            sc.setOption(name, value);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
            checkOption(sc, name, value);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
        } catch (IOException e) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
            /* Informational only, not all options have native support */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
            out.println(name + " not supported. " + e);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
        }
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
    void test(String[] args) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
        if (!Util.isSCTPSupported()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
            out.println("SCTP protocol is not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
            out.println("Test cannot be run");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
            return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
            SctpChannel sc = SctpChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
            /* check supported options */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
            Set<SctpSocketOption<?>> options = sc.supportedOptions();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
            List<? extends SctpSocketOption<?>> expected = Arrays.<SctpSocketOption<?>>asList(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
                    SCTP_DISABLE_FRAGMENTS, SCTP_EXPLICIT_COMPLETE,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
                    SCTP_FRAGMENT_INTERLEAVE, SCTP_INIT_MAXSTREAMS,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
                    SCTP_NODELAY, SCTP_PRIMARY_ADDR, SCTP_SET_PEER_PRIMARY_ADDR,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
                    SO_SNDBUF, SO_RCVBUF, SO_LINGER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
            for (SctpSocketOption opt: expected) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
                if (!options.contains(opt))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
                    fail(opt.name() + " should be supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
            InitMaxStreams streams = InitMaxStreams.create(1024, 1024);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
            sc.setOption(SCTP_INIT_MAXSTREAMS, streams);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
            checkOption(sc, SCTP_INIT_MAXSTREAMS, streams);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
            streams = sc.getOption(SCTP_INIT_MAXSTREAMS);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
            check(streams.maxInStreams() == 1024, "Max in streams: value: "
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
                    + streams.maxInStreams() + ", expected 1024 ");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
            check(streams.maxOutStreams() == 1024, "Max out streams: value: "
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
                    + streams.maxOutStreams() + ", expected 1024 ");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
            optionalSupport(sc, SCTP_DISABLE_FRAGMENTS, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
            optionalSupport(sc, SCTP_EXPLICIT_COMPLETE, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
            optionalSupport(sc, SCTP_FRAGMENT_INTERLEAVE, 1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
            //TODO: SCTP_PRIMARY_ADDR
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
            //sc.bind(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
            //connect
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
            //InetSocketAddress addr = new InetSocketAddress(0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
            //sc.setOption(SCTP_PRIMARY_ADDR, addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
            sc.setOption(SCTP_NODELAY, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
            checkOption(sc, SCTP_NODELAY, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
            sc.setOption(SO_SNDBUF, 16*1024);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
            sc.setOption(SO_RCVBUF, 16*1024);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
            checkOption(sc, SO_LINGER, -1);  /* default should be negative */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
            sc.setOption(SO_LINGER, 2000);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
            checkOption(sc, SO_LINGER, 2000);
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
            /* NullPointerException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
                sc.setOption(null, "value");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
                fail("NullPointerException not thrown for setOption");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
            } catch (NullPointerException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
               sc.getOption(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
               fail("NullPointerException not thrown for getOption");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
            } catch (NullPointerException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
               pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
            /* ClosedChannelException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
            sc.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
               sc.setOption(SCTP_INIT_MAXSTREAMS, streams);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
               fail("ClosedChannelException not thrown");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
            } catch (ClosedChannelException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
        } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
            unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
            //--------------------- Infrastructure ---------------------------
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
    boolean debug = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
    volatile int passed = 0, failed = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
    void pass() {passed++;}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
    void fail() {failed++; Thread.dumpStack();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
    void fail(String msg) {System.err.println(msg); fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   145
    void check(boolean cond) {if (cond) pass(); else fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
    void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
    void debug(String message) {if(debug) { System.out.println(message); }  }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
    public static void main(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
        Class<?> k = new Object(){}.getClass().getEnclosingClass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
        try {k.getMethod("instanceMain",String[].class)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
                .invoke( k.newInstance(), (Object) args);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
        catch (Throwable e) {throw e.getCause();}}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   153
    public void instanceMain(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
        try {test(args);} catch (Throwable t) {unexpected(t);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
        if (failed > 0) throw new AssertionError("Some tests failed");}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   157
}