Path: blob/master/test/jdk/com/sun/nio/sctp/SctpMultiChannel/SendFailed.java
41155 views
/*1* Copyright (c) 2015, 2020, 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 806784625* @summary Test for send failed notification26*/2728import com.sun.nio.sctp.*;29import java.io.IOException;30import java.net.InetAddress;31import java.net.InetSocketAddress;32import java.net.SocketAddress;33import java.nio.ByteBuffer;34import static java.lang.System.out;35import static java.nio.ByteBuffer.*;3637public class SendFailed {3839static final SocketAddress remoteAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 3000);4041static final int[] bufferSizes =42{ 20, 49, 50, 51, 100, 101, 1024, 1025, 4095, 4096, 4097, 8191, 8192, 8193};4344void test(String[] args) throws IOException {45SocketAddress address = null;4647if (!Util.isSCTPSupported()) {48out.println("SCTP protocol is not supported");49out.println("Test cannot be run");50return;51}5253System.out.println("remote address: " + remoteAddress);54System.out.println("Note, remote address should not be up");5556/* combinations with various buffer sizes, and offsets */57for (int send=0; send < bufferSizes.length; send++) {58for (int recv=0; recv < bufferSizes.length; recv++) {59for (boolean direct : new boolean[] {true, false})60runWithManyOffsets(bufferSizes[send], bufferSizes[recv], direct);61}62}63}6465void runWithManyOffsets(int sendBufferSize, int recvBufferSize, boolean direct)66throws IOException67{68doTest(sendBufferSize, recvBufferSize, direct, 0);69doTest(sendBufferSize, recvBufferSize, direct, 1);70doTest(sendBufferSize, recvBufferSize, direct, 3);71doTest(sendBufferSize, recvBufferSize, direct, 7);72doTest(sendBufferSize, recvBufferSize, direct, 9);73doTest(sendBufferSize, recvBufferSize, direct, 13);74doTest(sendBufferSize, recvBufferSize, direct, 15);75}7677void doTest(int sendBufferSize, int recvBufferSize, boolean direct, int offset)78throws IOException79{80debug("%n--- Testing with send size:[%d], recv size:[%d], offset:[%d] "81+ ", direct [%s]. ", sendBufferSize, recvBufferSize, offset, direct);8283try (SctpMultiChannel channel = SctpMultiChannel.open()) {84MessageInfo messageInfo = MessageInfo.createOutgoing(remoteAddress, 0);85ByteBuffer sendBuffer = filledBuffer(sendBufferSize, direct);8687debug("%nAttempting to send to %s. ", remoteAddress);88int sent = channel.send(sendBuffer, messageInfo);89sendBuffer.flip();9091SendFailedNotificationHandler handler =92new SendFailedNotificationHandler();93ByteBuffer recvBuffer = direct ? allocateDirect(recvBufferSize)94: allocate((recvBufferSize));95MessageInfo info = channel.receive(recvBuffer, null, handler);96debug("receive returned info:" + info);9798if (handler.receivedSendFailed) {99// verify sent buffer received by send failed notification100ByteBuffer buffer = handler.getSendFailedByteBuffer();101check(buffer.remaining() == sent);102check(buffer.position() == 0);103check(buffer.limit() == sent);104assertSameContent(sendBuffer, handler.getSendFailedByteBuffer());105} else {106debug("Unexpected event or received data. Check output.");107}108}109}110111class SendFailedNotificationHandler extends AbstractNotificationHandler<Object>112{113/** Reference to the buffer captured in send failed notification */114private ByteBuffer sentBuffer;115boolean receivedSendFailed;116117@Override118public HandlerResult handleNotification(119Notification notification, Object attachment) {120fail("Unknown notification type");121return HandlerResult.CONTINUE;122}123124@Override125public HandlerResult handleNotification(126AssociationChangeNotification notification, Object attachment) {127AssociationChangeNotification.AssocChangeEvent event = notification.event();128debug("%nAssociationChangeNotification");129debug("%n Association: %s. ", notification.association());130debug("%n Event: %s. ", event);131return HandlerResult.CONTINUE;132}133134@Override135public HandlerResult handleNotification(136SendFailedNotification notification, Object attachment) {137debug("%nSendFailedNotification: %s. ", notification);138receivedSendFailed = true;139sentBuffer = notification.buffer();140return HandlerResult.RETURN;141}142143public ByteBuffer getSendFailedByteBuffer() {144return sentBuffer;145}146147@Override148public HandlerResult handleNotification(149PeerAddressChangeNotification pacn, Object attachment)150{151debug("%nPeerAddressChangeNotification: %s", pacn);152return HandlerResult.CONTINUE;153}154155@Override156public HandlerResult handleNotification(157ShutdownNotification notification, Object attachment) {158debug("%nShutdownNotification");159debug("%n Association: %s. ", notification.association());160return HandlerResult.CONTINUE;161}162}163164static ByteBuffer filledBuffer(int size, boolean direct) {165ByteBuffer buffer = direct ? allocateDirect(size) : allocate((size));166for (int i=0; i< size; i++)167buffer.put((byte)i);168buffer.flip();169return buffer;170}171172static void assertSameContent(ByteBuffer bb1, ByteBuffer bb2) {173if (!bb1.equals(bb2))174throw new RuntimeException("Buffers are not equal; bb1: " + bb1 + ", bb2: " + bb2);175}176177//--------------------- Infrastructure ---------------------------178boolean debug = true;179volatile int passed = 0, failed = 0;180void pass() {passed++;}181void fail() {failed++; Thread.dumpStack();}182void fail(String msg) {System.err.println(msg); fail();}183void unexpected(Throwable t) {failed++; t.printStackTrace();}184void check(boolean cond) {if (cond) pass(); else fail();}185void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}186void debug(String message, Object... args) {if(debug) { out.printf(message, args); } }187public static void main(String[] args) throws Throwable {188Class<?> k = new Object(){}.getClass().getEnclosingClass();189try {k.getMethod("instanceMain",String[].class)190.invoke( k.newInstance(), (Object) args);}191catch (Throwable e) {throw e.getCause();}}192public void instanceMain(String[] args) throws Throwable {193try {test(args);} catch (Throwable t) {unexpected(t);}194out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);195if (failed > 0) throw new AssertionError("Some tests failed");}196}197198199