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