0N/A/*
3261N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @summary Unit test for socket-channel adaptors
0N/A * @library ..
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.nio.channels.*;
0N/A
0N/A
0N/Apublic class AdaptSocket {
0N/A
0N/A static java.io.PrintStream out = System.out;
0N/A
5501N/A static void test(TestServers.DayTimeServer dayTimeServer,
5501N/A int timeout,
5501N/A boolean shouldTimeout)
0N/A throws Exception
0N/A {
0N/A out.println();
0N/A
0N/A InetSocketAddress isa
5501N/A = new InetSocketAddress(dayTimeServer.getAddress(),
5501N/A dayTimeServer.getPort());
0N/A SocketChannel sc = SocketChannel.open();
0N/A Socket so = sc.socket();
0N/A out.println("opened: " + so);
0N/A out.println(" " + sc);
0N/A
0N/A //out.println("opts: " + sc.options());
0N/A so.setTcpNoDelay(true);
0N/A //so.setTrafficClass(SocketOpts.IP.TOS_THROUGHPUT);
0N/A so.setKeepAlive(true);
0N/A so.setSoLinger(true, 42);
0N/A so.setOOBInline(true);
0N/A so.setReceiveBufferSize(512);
0N/A so.setSendBufferSize(512);
0N/A //out.println(" " + sc.options());
0N/A
0N/A if (timeout == 0)
0N/A so.connect(isa);
0N/A else {
0N/A try {
0N/A so.connect(isa, timeout);
0N/A } catch (SocketTimeoutException x) {
0N/A if (shouldTimeout) {
0N/A out.println("Connection timed out, as expected");
0N/A return;
0N/A } else {
0N/A throw x;
0N/A }
0N/A }
0N/A }
0N/A out.println("connected: " + so);
0N/A out.println(" " + sc);
0N/A byte[] bb = new byte[100];
0N/A int n = so.getInputStream().read(bb);
0N/A String s = new String(bb, 0, n - 2, "US-ASCII");
0N/A out.println(isa + " says: \"" + s + "\"");
0N/A so.shutdownInput();
0N/A out.println("ishut: " + sc);
0N/A so.shutdownOutput();
0N/A out.println("oshut: " + sc);
0N/A so.close();
0N/A out.println("closed: " + so);
0N/A out.println(" " + sc);
0N/A }
0N/A
0N/A static String dataString = "foo\r\n";
0N/A
0N/A static void testRead(Socket so, boolean shouldTimeout)
0N/A throws Exception
0N/A {
0N/A String data = "foo\r\n";
0N/A so.getOutputStream().write(dataString.getBytes("US-ASCII"));
0N/A InputStream is = so.getInputStream();
0N/A try {
0N/A byte[] b = new byte[100];
0N/A int n = is.read(b);
0N/A if (n != 5)
0N/A throw new Exception("Incorrect number of bytes read: " + n);
0N/A if (!dataString.equals(new String(b, 0, n, "US-ASCII")))
0N/A throw new Exception("Incorrect data read: " + n);
0N/A } catch (SocketTimeoutException x) {
0N/A if (shouldTimeout) {
0N/A out.println("Read timed out, as expected");
0N/A return;
0N/A }
0N/A throw x;
0N/A }
0N/A }
0N/A
5501N/A static void testRead(TestServers.EchoServer echoServer,
5501N/A int timeout,
5501N/A boolean shouldTimeout)
0N/A throws Exception
0N/A {
0N/A out.println();
0N/A
0N/A InetSocketAddress isa
5501N/A = new InetSocketAddress(echoServer.getAddress(),
5501N/A echoServer.getPort());
0N/A SocketChannel sc = SocketChannel.open();
0N/A sc.connect(isa);
0N/A Socket so = sc.socket();
0N/A out.println("connected: " + so);
0N/A out.println(" " + sc);
0N/A
0N/A if (timeout > 0)
0N/A so.setSoTimeout(timeout);
0N/A out.println("timeout: " + so.getSoTimeout());
0N/A
0N/A testRead(so, shouldTimeout);
5501N/A for (int i = 0; i < 4; i++) {
5155N/A testRead(so, shouldTimeout);
5501N/A }
0N/A
0N/A sc.close();
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
5501N/A try (TestServers.DayTimeServer dayTimeServer
5501N/A = TestServers.DayTimeServer.startNewServer()) {
5501N/A test(dayTimeServer, 0, false);
5501N/A test(dayTimeServer, 1000, false);
5501N/A }
0N/A
5501N/A try (TestServers.DayTimeServer lingerDayTimeServer
5501N/A = TestServers.DayTimeServer.startNewServer(100)) {
5501N/A // this test no longer really test the connection timeout
5501N/A // since there is no way to prevent the server from eagerly
5501N/A // accepting connection...
5501N/A test(lingerDayTimeServer, 10, true);
5501N/A }
0N/A
5501N/A try (TestServers.EchoServer echoServer
5501N/A = TestServers.EchoServer.startNewServer()) {
5501N/A testRead(echoServer, 0, false);
5501N/A testRead(echoServer, 8000, false);
5501N/A }
0N/A
5501N/A try (TestServers.EchoServer lingerEchoServer
5501N/A = TestServers.EchoServer.startNewServer(100)) {
5501N/A testRead(lingerEchoServer, 10, true);
5501N/A }
5501N/A }
0N/A}