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         String os = System.getProperty("os.name").toLowerCase();
  48 
  49         if (!Util.isSCTPSupported()) {
  50             out.println("SCTP protocol is not supported");
  51             out.println("Test cannot be run");
  52             return;
  53         } else if (os.startsWith("sunos")) {
  54             out.println("Test not supported on Solaris");
  55             out.println("Test cannot be run");
  56             return;
  57         }
  58 
  59         System.out.println("remote address: " + remoteAddress);
  60         System.out.println("Note, remote address should not be up");
  61 
  62         /* combinations with various buffer sizes, and offsets */
  63         for (int send=0; send < bufferSizes.length; send++) {
  64             for (int recv=0; recv < bufferSizes.length; recv++) {
  65                 for (boolean direct : new boolean[] {true, false})
  66                     runWithManyOffsets(bufferSizes[send], bufferSizes[recv], direct);
  67             }
  68         }
  69     }
  70 
  71     void runWithManyOffsets(int sendBufferSize, int recvBufferSize, boolean direct)
  72         throws IOException
  73     {
  74         doTest(sendBufferSize, recvBufferSize, direct, 0);
  75         doTest(sendBufferSize, recvBufferSize, direct, 1);
  76         doTest(sendBufferSize, recvBufferSize, direct, 3);
  77         doTest(sendBufferSize, recvBufferSize, direct, 7);
  78         doTest(sendBufferSize, recvBufferSize, direct, 9);
  79         doTest(sendBufferSize, recvBufferSize, direct, 13);
  80         doTest(sendBufferSize, recvBufferSize, direct, 15);
  81     }
  82 
  83     void doTest(int sendBufferSize, int recvBufferSize, boolean direct, int offset)
  84         throws IOException
  85     {
  86         debug("%n--- Testing with send size:[%d], recv size:[%d], offset:[%d] "
  87                 + ", direct [%s]. ", sendBufferSize, recvBufferSize, offset, direct);
  88 
  89         try (SctpMultiChannel channel = SctpMultiChannel.open()) {
  90             MessageInfo messageInfo = MessageInfo.createOutgoing(remoteAddress, 0);
  91             ByteBuffer sendBuffer = filledBuffer(sendBufferSize, direct);
  92 
  93             debug("%nAttempting to send to %s. ", remoteAddress);
  94             int sent = channel.send(sendBuffer, messageInfo);
  95             sendBuffer.flip();
  96 
  97             SendFailedNotificationHandler handler =
  98                     new SendFailedNotificationHandler();
  99             ByteBuffer recvBuffer = direct ? allocateDirect(recvBufferSize)
 100                                            : allocate((recvBufferSize));
 101             MessageInfo info = channel.receive(recvBuffer, null, handler);
 102             debug("receive returned info:" + info);
 103 
 104             if (handler.receivedSendFailed) {
 105                 // verify sent buffer received by send failed notification
 106                 ByteBuffer buffer = handler.getSendFailedByteBuffer();
 107                 check(buffer.remaining() == sent);
 108                 check(buffer.position() == 0);
 109                 check(buffer.limit() == sent);
 110                 assertSameContent(sendBuffer, handler.getSendFailedByteBuffer());
 111             } else {
 112                 debug("Unexpected event or received data. Check output.");
 113             }
 114         }
 115     }
 116 
 117     class SendFailedNotificationHandler extends AbstractNotificationHandler<Object>
 118     {
 119         /** Reference to the buffer captured in send failed notification */
 120         private ByteBuffer sentBuffer;
 121         boolean receivedSendFailed;
 122 
 123         @Override
 124         public HandlerResult handleNotification(
 125                 Notification notification, Object attachment) {
 126             fail("Unknown notification type");
 127             return HandlerResult.CONTINUE;
 128         }
 129 
 130         @Override
 131         public HandlerResult handleNotification(
 132                 AssociationChangeNotification notification, Object attachment) {
 133             AssociationChangeNotification.AssocChangeEvent event = notification.event();
 134             debug("%nAssociationChangeNotification");
 135             debug("%n  Association: %s. ", notification.association());
 136             debug("%n  Event: %s. ", event);
 137             return HandlerResult.CONTINUE;
 138         }
 139 
 140         @Override
 141         public HandlerResult handleNotification(
 142                 SendFailedNotification notification, Object attachment) {
 143             debug("%nSendFailedNotification: %s. ", notification);
 144             receivedSendFailed = true;
 145             sentBuffer = notification.buffer();
 146             return HandlerResult.RETURN;
 147         }
 148 
 149         public ByteBuffer getSendFailedByteBuffer() {
 150             return sentBuffer;
 151         }
 152 
 153         @Override
 154         public HandlerResult handleNotification(
 155                 PeerAddressChangeNotification pacn, Object attachment)
 156         {
 157             debug("%nPeerAddressChangeNotification: %s", pacn);
 158             return HandlerResult.CONTINUE;
 159         }
 160 
 161         @Override
 162         public HandlerResult handleNotification(
 163                 ShutdownNotification notification, Object attachment) {
 164             debug("%nShutdownNotification");
 165             debug("%n  Association: %s. ", notification.association());
 166             return HandlerResult.CONTINUE;
 167         }
 168     }
 169 
 170     static ByteBuffer filledBuffer(int size, boolean direct) {
 171         ByteBuffer buffer = direct ? allocateDirect(size) : allocate((size));
 172         for (int i=0; i< size; i++)
 173             buffer.put((byte)i);
 174         buffer.flip();
 175         return buffer;
 176     }
 177 
 178     static void assertSameContent(ByteBuffer bb1, ByteBuffer bb2) {
 179         if (!bb1.equals(bb2))
 180             throw new RuntimeException("Buffers are not equal; bb1: " + bb1 + ", bb2: " + bb2);
 181     }
 182 
 183     //--------------------- Infrastructure ---------------------------
 184     boolean debug = true;
 185     volatile int passed = 0, failed = 0;
 186     void pass() {passed++;}
 187     void fail() {failed++; Thread.dumpStack();}
 188     void fail(String msg) {System.err.println(msg); fail();}
 189     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 190     void check(boolean cond) {if (cond) pass(); else fail();}
 191     void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
 192     void debug(String message, Object... args) {if(debug) { out.printf(message, args); } }
 193     public static void main(String[] args) throws Throwable {
 194         Class<?> k = new Object(){}.getClass().getEnclosingClass();
 195         try {k.getMethod("instanceMain",String[].class)
 196                 .invoke( k.newInstance(), (Object) args);}
 197         catch (Throwable e) {throw e.getCause();}}
 198     public void instanceMain(String[] args) throws Throwable {
 199         try {test(args);} catch (Throwable t) {unexpected(t);}
 200         out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 201         if (failed > 0) throw new AssertionError("Some tests failed");}
 202 }