jdk/test/com/sun/nio/sctp/MessageInfoTests.java
author chegar
Thu, 16 Apr 2009 17:42:00 +0100
changeset 2542 d859108aea12
child 5506 202f599c92aa
permissions -rw-r--r--
4927640: Implementation of the sctp protocol Summary: An implementation-specific API for the Stream Control Transmission Protocol Reviewed-by: alanb, michaelm, jccollet
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     1
/*
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     4
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     8
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    13
 * accompanied this code).
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    14
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    18
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    21
 * have any questions.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    22
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    23
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    24
/* @test
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    25
 * @bug 4927640
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    26
 * @summary Tests the SCTP protocol implementation
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    27
 * @author chegar
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    28
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    29
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    30
import java.net.SocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    31
import com.sun.nio.sctp.Association;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    32
import com.sun.nio.sctp.MessageInfo;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    33
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    34
public class MessageInfoTests {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
    static final int DEFAULT_STREAM_NUMBER = 14;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
    static final int TEST_STREAM_NUMBER = 15;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
    static final int TEST_PPID = 8;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
    static final long TEST_TTL = 10000L;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
    static final SocketAddress addr = new TestSocketAddress();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
    static final Association assoc = new TestAssociation(1, 1, 1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
    void test(String[] args) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
        /* TEST 1 : createOutGoing(SocketAddress,int) */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
        MessageInfo info = MessageInfo.createOutgoing(addr,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
                                                      DEFAULT_STREAM_NUMBER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
        checkDefaults(info);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
        checkGetterSetters(info);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
        /* TEST 2 : createOutGoing(Association,SocketAddress,int) */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
        info = MessageInfo.createOutgoing(assoc, addr, DEFAULT_STREAM_NUMBER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
        checkDefaults(info);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
        check(info.association().equals(assoc), "incorrect association");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
        checkGetterSetters(info);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
        /* TEST 3: null values */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
        info = MessageInfo.createOutgoing(null, 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
        check(info.address() == null, "address should be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    58
        check(info.association() == null, "association should be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
        info = MessageInfo.createOutgoing(assoc, null, 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
        check(info.address() == null, "address should be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
        /* Test 4: IllegalArgumentException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
        testIAE(new Runnable() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
          public void run() { MessageInfo.createOutgoing(addr, -1); } });
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
        testIAE(new Runnable() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
          public void run() { MessageInfo.createOutgoing(addr, 65537); } });
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
        testIAE(new Runnable() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
          public void run() { MessageInfo.createOutgoing(null, addr, 0); } });
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
        testIAE(new Runnable() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
          public void run() { MessageInfo.createOutgoing(assoc, addr, -1); } });
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
        testIAE(new Runnable() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
          public void run() { MessageInfo.createOutgoing(assoc, addr, 65537);}});
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
        final MessageInfo iaeInfo = MessageInfo.createOutgoing(assoc, addr, 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
        testIAE(new Runnable() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
          public void run() { iaeInfo.streamNumber(-1); } });
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
        testIAE(new Runnable() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
          public void run() { iaeInfo.streamNumber(65537); } });
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
    }
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 : unordered = false, timeToLive = 0, complete = true,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
    *        payloadProtocolID = 0. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
    void checkDefaults(MessageInfo info) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
        check(info.isUnordered() == false, "default unordered value not false");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
        check(info.timeToLive() == 0L, "timeToLive should be 0L");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
        check(info.isComplete() == true, "default complete value not true");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
        check(info.payloadProtocolID() == 0, "default PPID not 0");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
        check(info.bytes() == 0, "default bytes value not 0");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
        check(info.streamNumber() == DEFAULT_STREAM_NUMBER,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
                "incorrect default stream number");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
        check(info.address().equals(addr), "incorrect address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
    void checkGetterSetters(MessageInfo info) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
        check(info.streamNumber(TEST_STREAM_NUMBER).streamNumber() ==
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
                TEST_STREAM_NUMBER, "stream number not being set correctly");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
        check(info.complete(false).isComplete() == false,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
                "complete not being set correctly");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
        check(info.unordered(true).isUnordered() == true,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
                "unordered not being set correctly");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
        check(info.payloadProtocolID(TEST_PPID).payloadProtocolID() ==
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
                                  TEST_PPID, "PPID not being set correctly");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
        check(info.timeToLive(TEST_TTL).timeToLive() == TEST_TTL,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
                "TTL not being set correctly");
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
    void testIAE(Runnable runnable) {
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
            runnable.run();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
            fail("IllegalArgumentException should have been thrown");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
        } catch(IllegalArgumentException iae) {
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
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
    static class TestSocketAddress extends SocketAddress {}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
    static class TestAssociation extends Association {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
        TestAssociation(int assocID, int maxInStreams, int maxOutStreams) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
            super(assocID, maxInStreams, maxOutStreams);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
          //--------------------- Infrastructure ---------------------------
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
    boolean debug = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
    volatile int passed = 0, failed = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
    void pass() {passed++;}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
    void fail() {failed++; Thread.dumpStack();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
    void fail(String msg) {System.err.println(msg); fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
    void check(boolean cond) {if (cond) pass(); else fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
    void check(boolean cond, String failMessage) {if (cond) pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
          else fail(failMessage);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
    void debug(String message) {if(debug) { System.out.println(message); }  }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
    public static void main(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
        Class<?> k = new Object(){}.getClass().getEnclosingClass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
        try {k.getMethod("instanceMain",String[].class)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
                .invoke( k.newInstance(), (Object) args);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
        catch (Throwable e) {throw e.getCause();}}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
    public void instanceMain(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   145
        try {test(args);} catch (Throwable t) {unexpected(t);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
        if (failed > 0) throw new AssertionError("Some tests failed");}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
}