Path: blob/master/test/jdk/com/sun/nio/sctp/MessageInfoTests.java
41153 views
/*1* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 492764025* @summary Tests the SCTP protocol implementation26* @author chegar27*/2829import java.net.SocketAddress;30import com.sun.nio.sctp.Association;31import com.sun.nio.sctp.MessageInfo;3233public class MessageInfoTests {34static final int DEFAULT_STREAM_NUMBER = 14;35static final int TEST_STREAM_NUMBER = 15;36static final int TEST_PPID = 8;37static final long TEST_TTL = 10000L;38static final SocketAddress addr = new TestSocketAddress();39static final Association assoc = new TestAssociation(1, 1, 1);4041void test(String[] args) {42/* TEST 1 : createOutGoing(SocketAddress,int) */43MessageInfo info = MessageInfo.createOutgoing(addr,44DEFAULT_STREAM_NUMBER);45checkDefaults(info);46checkGetterSetters(info);4748/* TEST 2 : createOutGoing(Association,SocketAddress,int) */49info = MessageInfo.createOutgoing(assoc, addr, DEFAULT_STREAM_NUMBER);50checkDefaults(info);51check(info.association().equals(assoc), "incorrect association");52checkGetterSetters(info);5354/* TEST 3: null values */55info = MessageInfo.createOutgoing(null, 0);56check(info.address() == null, "address should be null");57check(info.association() == null, "association should be null");58info = MessageInfo.createOutgoing(assoc, null, 0);59check(info.address() == null, "address should be null");6061/* Test 4: IllegalArgumentException */62testIAE(new Runnable() {63public void run() { MessageInfo.createOutgoing(addr, -1); } });64testIAE(new Runnable() {65public void run() { MessageInfo.createOutgoing(addr, 65537); } });66testIAE(new Runnable() {67public void run() { MessageInfo.createOutgoing(null, addr, 0); } });68testIAE(new Runnable() {69public void run() { MessageInfo.createOutgoing(assoc, addr, -1); } });70testIAE(new Runnable() {71public void run() { MessageInfo.createOutgoing(assoc, addr, 65537);}});7273final MessageInfo iaeInfo = MessageInfo.createOutgoing(assoc, addr, 0);74testIAE(new Runnable() {75public void run() { iaeInfo.streamNumber(-1); } });76testIAE(new Runnable() {77public void run() { iaeInfo.streamNumber(65537); } });78}7980/* TEST : unordered = false, timeToLive = 0, complete = true,81* payloadProtocolID = 0. */82void checkDefaults(MessageInfo info) {83check(info.isUnordered() == false, "default unordered value not false");84check(info.timeToLive() == 0L, "timeToLive should be 0L");85check(info.isComplete() == true, "default complete value not true");86check(info.payloadProtocolID() == 0, "default PPID not 0");87check(info.bytes() == 0, "default bytes value not 0");88check(info.streamNumber() == DEFAULT_STREAM_NUMBER,89"incorrect default stream number");90check(info.address().equals(addr), "incorrect address");91}9293void checkGetterSetters(MessageInfo info) {94check(info.streamNumber(TEST_STREAM_NUMBER).streamNumber() ==95TEST_STREAM_NUMBER, "stream number not being set correctly");9697check(info.complete(false).isComplete() == false,98"complete not being set correctly");99100check(info.unordered(true).isUnordered() == true,101"unordered not being set correctly");102103check(info.payloadProtocolID(TEST_PPID).payloadProtocolID() ==104TEST_PPID, "PPID not being set correctly");105106check(info.timeToLive(TEST_TTL).timeToLive() == TEST_TTL,107"TTL not being set correctly");108}109110void testIAE(Runnable runnable) {111try {112runnable.run();113fail("IllegalArgumentException should have been thrown");114} catch(IllegalArgumentException iae) {115pass();116}117}118119static class TestSocketAddress extends SocketAddress {}120121static class TestAssociation extends Association {122TestAssociation(int assocID, int maxInStreams, int maxOutStreams) {123super(assocID, maxInStreams, maxOutStreams);124}125}126127//--------------------- Infrastructure ---------------------------128boolean debug = true;129volatile int passed = 0, failed = 0;130void pass() {passed++;}131void fail() {failed++; Thread.dumpStack();}132void fail(String msg) {System.err.println(msg); fail();}133void unexpected(Throwable t) {failed++; t.printStackTrace();}134void check(boolean cond) {if (cond) pass(); else fail();}135void check(boolean cond, String failMessage) {if (cond) pass();136else fail(failMessage);}137void debug(String message) {if(debug) { System.out.println(message); } }138public static void main(String[] args) throws Throwable {139Class<?> k = new Object(){}.getClass().getEnclosingClass();140try {k.getMethod("instanceMain",String[].class)141.invoke( k.newInstance(), (Object) args);}142catch (Throwable e) {throw e.getCause();}}143public void instanceMain(String[] args) throws Throwable {144try {test(args);} catch (Throwable t) {unexpected(t);}145System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);146if (failed > 0) throw new AssertionError("Some tests failed");}147}148149150