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