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