1 /*
   2  * Copyright (c) 2002, 2018, 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 8195160
  26  * @summary Unit test for shutdown, shutdownInput  and isInputShutdown
  27  * @requires (os.family == "linux")
  28  * @library .. /test/lib
  29  * @build RsocketTest
  30  * @run main/othervm -Djava.net.preferIPv4Stack=true Connect
  31  */
  32 
  33 import java.io.IOException;
  34 import java.net.*;
  35 import java.nio.ByteBuffer;
  36 import java.nio.channels.*;
  37 import jdk.net.Sockets;
  38 
  39 public class Shutdown {
  40 
  41     /**
  42      * Accept a connection, and close it immediately causing a hard reset.
  43      */
  44     static void acceptAndReset(ServerSocketChannel ssc) throws IOException {
  45         SocketChannel peer = ssc.accept();
  46         try {
  47             peer.configureBlocking(false);
  48             peer.write(ByteBuffer.wrap(new byte[128*1024]));
  49         } finally {
  50             peer.close();
  51         }
  52     }
  53 
  54     public static void main(String[] args) throws Exception {
  55         if (!RsocketTest.isRsocketAvailable())
  56             return;
  57         InetAddress lh = InetAddress.getLocalHost();
  58 
  59         ServerSocketChannel ssc = Sockets.openRdmaServerSocketChannel()
  60             .bind(new InetSocketAddress(lh, 0));
  61         try {
  62             int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();
  63             SocketAddress remote = new InetSocketAddress(lh, port);
  64 
  65             // Test SocketChannel shutdownXXX
  66             SocketChannel sc;
  67             sc = Sockets.openRdmaSocketChannel();
  68             sc.connect(remote);
  69             try {
  70                 acceptAndReset(ssc);
  71                 sc.shutdownInput();
  72                 sc.shutdownOutput();
  73             } catch (Exception e) {
  74                 throw new RuntimeException("Test Failed");
  75             } finally {
  76                 sc.close();
  77             }
  78 
  79             // Test Socket adapter shutdownXXX and isShutdownInput
  80             sc = Sockets.openRdmaSocketChannel();
  81             sc.connect(remote);
  82 
  83             try {
  84                 acceptAndReset(ssc);
  85                 boolean before = sc.socket().isInputShutdown();
  86                 sc.socket().shutdownInput();
  87                 boolean after = sc.socket().isInputShutdown();
  88                 if (before || !after)
  89                     throw new RuntimeException("Before and after test failed");
  90                 sc.socket().shutdownOutput();
  91             } finally {
  92                 sc.close();
  93             }
  94         } finally {
  95             ssc.close();
  96         }
  97     }
  98 }