1 /*
   2  * Copyright (c) 2001, 2010, 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  * @summary Unit test for socket-channel adaptors
  26  * @library ..
  27  */
  28 
  29 import java.io.*;
  30 import java.net.*;
  31 import java.nio.channels.*;
  32 
  33 
  34 public class AdaptSocket {
  35 
  36     static java.io.PrintStream out = System.out;
  37 
  38     static final int ECHO_PORT = 7;
  39     static final int DAYTIME_PORT = 13;
  40     static final String REMOTE_HOST = TestUtil.HOST;
  41     static final String VERY_REMOTE_HOST = TestUtil.FAR_HOST;
  42 
  43     static void test(String hn, int timeout, boolean shouldTimeout)
  44         throws Exception
  45     {
  46         out.println();
  47 
  48         InetSocketAddress isa
  49             = new InetSocketAddress(InetAddress.getByName(hn),
  50                                     DAYTIME_PORT);
  51         SocketChannel sc = SocketChannel.open();
  52         Socket so = sc.socket();
  53         out.println("opened: " + so);
  54         out.println("        " + sc);
  55 
  56         //out.println("opts:   " + sc.options());
  57         so.setTcpNoDelay(true);
  58         //so.setTrafficClass(SocketOpts.IP.TOS_THROUGHPUT);
  59         so.setKeepAlive(true);
  60         so.setSoLinger(true, 42);
  61         so.setOOBInline(true);
  62         so.setReceiveBufferSize(512);
  63         so.setSendBufferSize(512);
  64         //out.println("        " + sc.options());
  65 
  66         if (timeout == 0)
  67             so.connect(isa);
  68         else {
  69             try {
  70                 so.connect(isa, timeout);
  71             } catch (SocketTimeoutException x) {
  72                 if (shouldTimeout) {
  73                     out.println("Connection timed out, as expected");
  74                     return;
  75                 } else {
  76                     throw x;
  77                 }
  78             }
  79         }
  80         out.println("connected: " + so);
  81         out.println("           " + sc);
  82         byte[] bb = new byte[100];
  83         int n = so.getInputStream().read(bb);
  84         String s = new String(bb, 0, n - 2, "US-ASCII");
  85         out.println(isa + " says: \"" + s + "\"");
  86         so.shutdownInput();
  87         out.println("ishut: " + sc);
  88         so.shutdownOutput();
  89         out.println("oshut: " + sc);
  90         so.close();
  91         out.println("closed: " + so);
  92         out.println("        " + sc);
  93     }
  94 
  95     static String dataString = "foo\r\n";
  96 
  97     static void testRead(Socket so, boolean shouldTimeout)
  98         throws Exception
  99     {
 100         String data = "foo\r\n";
 101         so.getOutputStream().write(dataString.getBytes("US-ASCII"));
 102         InputStream is = so.getInputStream();
 103         try {
 104             byte[] b = new byte[100];
 105             int n = is.read(b);
 106             if (n != 5)
 107                 throw new Exception("Incorrect number of bytes read: " + n);
 108             if (!dataString.equals(new String(b, 0, n, "US-ASCII")))
 109                 throw new Exception("Incorrect data read: " + n);
 110         } catch (SocketTimeoutException x) {
 111             if (shouldTimeout) {
 112                 out.println("Read timed out, as expected");
 113                 return;
 114             }
 115             throw x;
 116         }
 117     }
 118 
 119     static void testRead(String hn, int timeout, boolean shouldTimeout)
 120         throws Exception
 121     {
 122         out.println();
 123 
 124         InetSocketAddress isa
 125             = new InetSocketAddress(InetAddress.getByName(hn), ECHO_PORT);
 126         SocketChannel sc = SocketChannel.open();
 127         sc.connect(isa);
 128         Socket so = sc.socket();
 129         out.println("connected: " + so);
 130         out.println("           " + sc);
 131 
 132         if (timeout > 0)
 133             so.setSoTimeout(timeout);
 134         out.println("timeout: " + so.getSoTimeout());
 135 
 136         testRead(so, shouldTimeout);
 137         for (int i = 0; i < 4; i++)
 138             testRead(so, shouldTimeout);
 139 
 140         sc.close();
 141     }
 142 
 143     public static void main(String[] args) throws Exception {
 144 
 145         test(REMOTE_HOST, 0, false);
 146         test(REMOTE_HOST, 1000, false);
 147         test(VERY_REMOTE_HOST, 10, true);
 148 
 149         testRead(REMOTE_HOST, 0, false);
 150         testRead(REMOTE_HOST, 8000, false);
 151         testRead(VERY_REMOTE_HOST, 10, true);
 152 
 153     }
 154 
 155 }