1096N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1096N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1096N/A *
1096N/A * This code is free software; you can redistribute it and/or modify it
1096N/A * under the terms of the GNU General Public License version 2 only, as
1096N/A * published by the Free Software Foundation.
1096N/A *
1096N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1096N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1096N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1096N/A * version 2 for more details (a copy is included in the LICENSE file that
1096N/A * accompanied this code).
1096N/A *
1096N/A * You should have received a copy of the GNU General Public License version
1096N/A * 2 along with this work; if not, write to the Free Software Foundation,
1096N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1096N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1096N/A */
1096N/A
1096N/A/* @test
1096N/A * @bug 4927640
1096N/A * @summary Tests the SCTP protocol implementation
1096N/A * @author chegar
1096N/A */
1096N/A
1096N/Aimport java.net.SocketAddress;
1096N/Aimport com.sun.nio.sctp.Association;
1096N/Aimport com.sun.nio.sctp.MessageInfo;
1096N/A
1096N/Apublic class MessageInfoTests {
1096N/A static final int DEFAULT_STREAM_NUMBER = 14;
1096N/A static final int TEST_STREAM_NUMBER = 15;
1096N/A static final int TEST_PPID = 8;
1096N/A static final long TEST_TTL = 10000L;
1096N/A static final SocketAddress addr = new TestSocketAddress();
1096N/A static final Association assoc = new TestAssociation(1, 1, 1);
1096N/A
1096N/A void test(String[] args) {
1096N/A /* TEST 1 : createOutGoing(SocketAddress,int) */
1096N/A MessageInfo info = MessageInfo.createOutgoing(addr,
1096N/A DEFAULT_STREAM_NUMBER);
1096N/A checkDefaults(info);
1096N/A checkGetterSetters(info);
1096N/A
1096N/A /* TEST 2 : createOutGoing(Association,SocketAddress,int) */
1096N/A info = MessageInfo.createOutgoing(assoc, addr, DEFAULT_STREAM_NUMBER);
1096N/A checkDefaults(info);
1096N/A check(info.association().equals(assoc), "incorrect association");
1096N/A checkGetterSetters(info);
1096N/A
1096N/A /* TEST 3: null values */
1096N/A info = MessageInfo.createOutgoing(null, 0);
1096N/A check(info.address() == null, "address should be null");
1096N/A check(info.association() == null, "association should be null");
1096N/A info = MessageInfo.createOutgoing(assoc, null, 0);
1096N/A check(info.address() == null, "address should be null");
1096N/A
1096N/A /* Test 4: IllegalArgumentException */
1096N/A testIAE(new Runnable() {
1096N/A public void run() { MessageInfo.createOutgoing(addr, -1); } });
1096N/A testIAE(new Runnable() {
1096N/A public void run() { MessageInfo.createOutgoing(addr, 65537); } });
1096N/A testIAE(new Runnable() {
1096N/A public void run() { MessageInfo.createOutgoing(null, addr, 0); } });
1096N/A testIAE(new Runnable() {
1096N/A public void run() { MessageInfo.createOutgoing(assoc, addr, -1); } });
1096N/A testIAE(new Runnable() {
1096N/A public void run() { MessageInfo.createOutgoing(assoc, addr, 65537);}});
1096N/A
1096N/A final MessageInfo iaeInfo = MessageInfo.createOutgoing(assoc, addr, 0);
1096N/A testIAE(new Runnable() {
1096N/A public void run() { iaeInfo.streamNumber(-1); } });
1096N/A testIAE(new Runnable() {
1096N/A public void run() { iaeInfo.streamNumber(65537); } });
1096N/A }
1096N/A
1096N/A /* TEST : unordered = false, timeToLive = 0, complete = true,
1096N/A * payloadProtocolID = 0. */
1096N/A void checkDefaults(MessageInfo info) {
1096N/A check(info.isUnordered() == false, "default unordered value not false");
1096N/A check(info.timeToLive() == 0L, "timeToLive should be 0L");
1096N/A check(info.isComplete() == true, "default complete value not true");
1096N/A check(info.payloadProtocolID() == 0, "default PPID not 0");
1096N/A check(info.bytes() == 0, "default bytes value not 0");
1096N/A check(info.streamNumber() == DEFAULT_STREAM_NUMBER,
1096N/A "incorrect default stream number");
1096N/A check(info.address().equals(addr), "incorrect address");
1096N/A }
1096N/A
1096N/A void checkGetterSetters(MessageInfo info) {
1096N/A check(info.streamNumber(TEST_STREAM_NUMBER).streamNumber() ==
1096N/A TEST_STREAM_NUMBER, "stream number not being set correctly");
1096N/A
1096N/A check(info.complete(false).isComplete() == false,
1096N/A "complete not being set correctly");
1096N/A
1096N/A check(info.unordered(true).isUnordered() == true,
1096N/A "unordered not being set correctly");
1096N/A
1096N/A check(info.payloadProtocolID(TEST_PPID).payloadProtocolID() ==
1096N/A TEST_PPID, "PPID not being set correctly");
1096N/A
1096N/A check(info.timeToLive(TEST_TTL).timeToLive() == TEST_TTL,
1096N/A "TTL not being set correctly");
1096N/A }
1096N/A
1096N/A void testIAE(Runnable runnable) {
1096N/A try {
1096N/A runnable.run();
1096N/A fail("IllegalArgumentException should have been thrown");
1096N/A } catch(IllegalArgumentException iae) {
1096N/A pass();
1096N/A }
1096N/A }
1096N/A
1096N/A static class TestSocketAddress extends SocketAddress {}
1096N/A
1096N/A static class TestAssociation extends Association {
1096N/A TestAssociation(int assocID, int maxInStreams, int maxOutStreams) {
1096N/A super(assocID, maxInStreams, maxOutStreams);
1096N/A }
1096N/A }
1096N/A
1096N/A //--------------------- Infrastructure ---------------------------
1096N/A boolean debug = true;
1096N/A volatile int passed = 0, failed = 0;
1096N/A void pass() {passed++;}
1096N/A void fail() {failed++; Thread.dumpStack();}
1096N/A void fail(String msg) {System.err.println(msg); fail();}
1096N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
1096N/A void check(boolean cond) {if (cond) pass(); else fail();}
1096N/A void check(boolean cond, String failMessage) {if (cond) pass();
1096N/A else fail(failMessage);}
1096N/A void debug(String message) {if(debug) { System.out.println(message); } }
1096N/A public static void main(String[] args) throws Throwable {
1096N/A Class<?> k = new Object(){}.getClass().getEnclosingClass();
1096N/A try {k.getMethod("instanceMain",String[].class)
1096N/A .invoke( k.newInstance(), (Object) args);}
1096N/A catch (Throwable e) {throw e.getCause();}}
1096N/A public void instanceMain(String[] args) throws Throwable {
1096N/A try {test(args);} catch (Throwable t) {unexpected(t);}
1096N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1096N/A if (failed > 0) throw new AssertionError("Some tests failed");}
1096N/A}