1 /*
   2  * Copyright (c) 2001, 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 socket-channel adaptors
  27  * @requires (os.family == "linux")
  28  * @library .. /test/lib
  29  * @build jdk.test.lib.Utils TestServers
  30  * @build RsocketTest
  31  * @run main/othervm -Djava.net.preferIPv4Stack=true AdaptSocket
  32  */
  33 
  34 import java.io.*;
  35 import java.net.*;
  36 import java.nio.channels.*;
  37 import java.util.Arrays;
  38 import jdk.net.Sockets;
  39 
  40 public class AdaptSocket {
  41 
  42     static final java.io.PrintStream out = System.out;
  43 
  44     static void test(TestServers.AbstractServer server,
  45                      int timeout,
  46                      boolean shouldTimeout)
  47         throws Exception
  48     {
  49         out.println();
  50 
  51         InetSocketAddress isa = new InetSocketAddress(server.getAddress(), server.getPort());
  52         SocketChannel sc = Sockets.openRdmaSocketChannel();
  53         Socket so = sc.socket();
  54         out.println("opened: " + so);
  55         out.println("        " + sc);
  56 
  57         so.setTcpNoDelay(true);
  58         so.setKeepAlive(true);
  59         so.setReceiveBufferSize(512);
  60         so.setSendBufferSize(512);
  61 
  62         if (timeout == 0)
  63             so.connect(isa);
  64         else {
  65             try {
  66                 so.connect(isa, timeout);
  67             } catch (SocketTimeoutException x) {
  68                 if (shouldTimeout) {
  69                     out.println("Connection timed out, as expected");
  70                     return;
  71                 } else {
  72                     throw x;
  73                 }
  74             }
  75         }
  76         out.println("connected: " + so);
  77         out.println("           " + sc);
  78         byte[] bb = new byte[100];
  79         int n = so.getInputStream().read(bb);
  80         String s = new String(bb, 0, n - 2, "US-ASCII");
  81         out.println(isa + " says: \"" + s + "\"");
  82         so.shutdownInput();
  83         out.println("ishut: " + sc);
  84         so.shutdownOutput();
  85         out.println("oshut: " + sc);
  86         so.close();
  87         out.println("closed: " + so);
  88         out.println("        " + sc);
  89     }
  90 
  91     static String dataString = "foo\r\n";
  92 
  93     static void testRead(Socket so, boolean shouldTimeout)
  94         throws Exception
  95     {
  96         String data = "foo\r\n";
  97         so.getOutputStream().write(dataString.getBytes("US-ASCII"));
  98         InputStream is = so.getInputStream();
  99         try {
 100             byte[] b = new byte[100];
 101             int n = is.read(b);
 102             if (shouldTimeout) {
 103                 throw new Exception("Should time out, but not, data: " + Arrays.toString(b));
 104             }
 105             if (n != 5) {
 106                 throw new Exception("Incorrect number of bytes read: " + n);
 107             }
 108             if (!dataString.equals(new String(b, 0, n, "US-ASCII"))) {
 109                 throw new Exception("Incorrect data read: " + n);
 110             }
 111         } catch (SocketTimeoutException x) {
 112             if (shouldTimeout) {
 113                 out.println("Read timed out, as expected");
 114                 return;
 115             }
 116             throw x;
 117         }
 118     }
 119 
 120     static void testRead(TestServers.EchoServer echoServer,
 121                          int timeout,
 122                          boolean shouldTimeout)
 123         throws Exception
 124     {
 125         out.println();
 126 
 127         InetSocketAddress isa
 128             = new InetSocketAddress(echoServer.getAddress(),
 129                                     echoServer.getPort());
 130         SocketChannel sc = Sockets.openRdmaSocketChannel();
 131         sc.connect(isa);
 132         Socket so = sc.socket();
 133         out.println("connected: " + so);
 134         out.println("           " + sc);
 135 
 136         if (timeout > 0)
 137             so.setSoTimeout(timeout);
 138         out.println("timeout: " + so.getSoTimeout());
 139 
 140         testRead(so, shouldTimeout);
 141         for (int i = 0; i < 4; i++) {
 142             out.println("loop: " + i);
 143             testRead(so, shouldTimeout);
 144         }
 145 
 146         sc.close();
 147     }
 148 
 149     public static void main(String[] args) throws Exception {
 150         if (!RsocketTest.isRsocketAvailable())
 151             return;
 152 
 153         try (TestServers.DayTimeServer dayTimeServer
 154                 = TestServers.DayTimeServer.startNewServer()) {
 155             test(dayTimeServer, 0, false);
 156             test(dayTimeServer, 1000, false);
 157         }
 158 
 159         try (TestServers.DayTimeServer lingerDayTimeServer
 160                 = TestServers.DayTimeServer.startNewServer(100)) {
 161             // this test no longer really test the connection timeout
 162             // since there is no way to prevent the server from eagerly
 163             // accepting connection...
 164             test(lingerDayTimeServer, 10, true);
 165         }
 166 
 167         try (TestServers.EchoServer echoServer
 168                 = TestServers.EchoServer.startNewServer()) {
 169             testRead(echoServer, 0, false);
 170             testRead(echoServer, 8000, false);
 171         }
 172 
 173         try (TestServers.NoResponseServer noResponseServer
 174                 = TestServers.NoResponseServer.startNewServer()) {
 175             testRead(noResponseServer, 10, true);
 176         }
 177     }
 178 }