1 /*
   2  * Copyright (c) 2015, 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8067846
  26  * @summary Test for send failed notification
  27  */
  28 
  29 import com.sun.nio.sctp.*;
  30 import java.io.IOException;
  31 import java.net.InetAddress;
  32 import java.net.InetSocketAddress;
  33 import java.net.SocketAddress;
  34 import java.nio.ByteBuffer;
  35 import static java.lang.System.out;
  36 import static java.nio.ByteBuffer.*;
  37 
  38 public class SendFailed {
  39 
  40     static final SocketAddress remoteAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 3000);
  41 
  42     static final int[] bufferSizes =
  43             { 20, 49, 50, 51, 100, 101, 1024, 1025, 4095, 4096, 4097, 8191, 8192, 8193};
  44 
  45     void test(String[] args) throws IOException {
  46         SocketAddress address = null;
  47 
  48         if (!Util.isSCTPSupported()) {
  49             out.println("SCTP protocol is not supported");
  50             out.println("Test cannot be run");
  51             return;
  52         }
  53 
  54         System.out.println("remote address: " + remoteAddress);
  55         System.out.println("Note, remote address should not be up");
  56 
  57         /* combinations with various buffer sizes, and offsets */
  58         for (int send=0; send < bufferSizes.length; send++) {
  59             for (int recv=0; recv < bufferSizes.length; recv++) {
  60                 for (boolean direct : new boolean[] {true, false})
  61                     runWithManyOffsets(bufferSizes[send], bufferSizes[recv], direct);
  62             }
  63         }
  64     }
  65 
  66     void runWithManyOffsets(int sendBufferSize, int recvBufferSize, boolean direct)
  67         throws IOException
  68     {
  69         doTest(sendBufferSize, recvBufferSize, direct, 0);
  70         doTest(sendBufferSize, recvBufferSize, direct, 1);
  71         doTest(sendBufferSize, recvBufferSize, direct, 3);
  72         doTest(sendBufferSize, recvBufferSize, direct, 7);
  73         doTest(sendBufferSize, recvBufferSize, direct, 9);
  74         doTest(sendBufferSize, recvBufferSize, direct, 13);
  75         doTest(sendBufferSize, recvBufferSize, direct, 15);
  76     }
  77 
  78     void doTest(int sendBufferSize, int recvBufferSize, boolean direct, int offset)
  79         throws IOException
  80     {
  81         debug("%n--- Testing with send size:[%d], recv size:[%d], offset:[%d] "
  82                 + ", direct [%s]. ", sendBufferSize, recvBufferSize, offset, direct);
  83 
  84         try (SctpMultiChannel channel = SctpMultiChannel.open()) {
  85             MessageInfo messageInfo = MessageInfo.createOutgoing(remoteAddress, 0);
  86             ByteBuffer sendBuffer = filledBuffer(sendBufferSize, direct);
  87 
  88             debug("%nAttempting to send to %s. ", remoteAddress);
  89             int sent = channel.send(sendBuffer, messageInfo);
  90             sendBuffer.flip();
  91 
  92             SendFailedNotificationHandler handler =
  93                     new SendFailedNotificationHandler();
  94             ByteBuffer recvBuffer = direct ? allocateDirect(recvBufferSize)
  95                                            : allocate((recvBufferSize));
  96             MessageInfo info = channel.receive(recvBuffer, null, handler);
  97             debug("receive returned info:" + info);
  98 
  99             if (handler.receivedSendFailed) {
 100                 // verify sent buffer received by send failed notification
 101                 ByteBuffer buffer = handler.getSendFailedByteBuffer();
 102                 check(buffer.remaining() == sent);
 103                 check(buffer.position() == 0);
 104                 check(buffer.limit() == sent);
 105                 assertSameContent(sendBuffer, handler.getSendFailedByteBuffer());
 106             } else {
 107                 debug("Unexpected event or received data. Check output.");
 108             }
 109         }
 110     }
 111 
 112     class SendFailedNotificationHandler extends AbstractNotificationHandler<Object>
 113     {
 114         /** Reference to the buffer captured in send failed notification */
 115         private ByteBuffer sentBuffer;
 116         boolean receivedSendFailed;
 117 
 118         @Override
 119         public HandlerResult handleNotification(
 120                 Notification notification, Object attachment) {
 121             fail("Unknown notification type");
 122             return HandlerResult.CONTINUE;
 123         }
 124 
 125         @Override
 126         public HandlerResult handleNotification(
 127                 AssociationChangeNotification notification, Object attachment) {
 128             AssociationChangeNotification.AssocChangeEvent event = notification.event();
 129             debug("%nAssociationChangeNotification");
 130             debug("%n  Association: %s. ", notification.association());
 131             debug("%n  Event: %s. ", event);
 132             return HandlerResult.CONTINUE;
 133         }
 134 
 135         @Override
 136         public HandlerResult handleNotification(
 137                 SendFailedNotification notification, Object attachment) {
 138             debug("%nSendFailedNotification: %s. ", notification);
 139             receivedSendFailed = true;
 140             sentBuffer = notification.buffer();
 141             return HandlerResult.RETURN;
 142         }
 143 
 144         public ByteBuffer getSendFailedByteBuffer() {
 145             return sentBuffer;
 146         }
 147 
 148         @Override
 149         public HandlerResult handleNotification(
 150                 PeerAddressChangeNotification pacn, Object attachment)
 151         {
 152             debug("%nPeerAddressChangeNotification: %s", pacn);
 153             return HandlerResult.CONTINUE;
 154         }
 155 
 156         @Override
 157         public HandlerResult handleNotification(
 158                 ShutdownNotification notification, Object attachment) {
 159             debug("%nShutdownNotification");
 160             debug("%n  Association: %s. ", notification.association());
 161             return HandlerResult.CONTINUE;
 162         }
 163     }
 164 
 165     static ByteBuffer filledBuffer(int size, boolean direct) {
 166         ByteBuffer buffer = direct ? allocateDirect(size) : allocate((size));
 167         for (int i=0; i< size; i++)
 168             buffer.put((byte)i);
 169         buffer.flip();
 170         return buffer;
 171     }
 172 
 173     static void assertSameContent(ByteBuffer bb1, ByteBuffer bb2) {
 174         if (!bb1.equals(bb2))
 175             throw new RuntimeException("Buffers are not equal; bb1: " + bb1 + ", bb2: " + bb2);
 176     }
 177 
 178     //--------------------- Infrastructure ---------------------------
 179     boolean debug = true;
 180     volatile int passed = 0, failed = 0;
 181     void pass() {passed++;}
 182     void fail() {failed++; Thread.dumpStack();}
 183     void fail(String msg) {System.err.println(msg); fail();}
 184     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 185     void check(boolean cond) {if (cond) pass(); else fail();}
 186     void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
 187     void debug(String message, Object... args) {if(debug) { out.printf(message, args); } }
 188     public static void main(String[] args) throws Throwable {
 189         Class<?> k = new Object(){}.getClass().getEnclosingClass();
 190         try {k.getMethod("instanceMain",String[].class)
 191                 .invoke( k.newInstance(), (Object) args);}
 192         catch (Throwable e) {throw e.getCause();}}
 193     public void instanceMain(String[] args) throws Throwable {
 194         try {test(args);} catch (Throwable t) {unexpected(t);}
 195         out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 196         if (failed > 0) throw new AssertionError("Some tests failed");}
 197 }