Path: blob/master/test/jdk/java/nio/channels/SocketChannel/AdaptSocket.java
41154 views
/*1* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 8156002 820147425* @summary Unit test for socket-channel adaptors26* @library .. /test/lib27* @build jdk.test.lib.Utils TestServers28* @run main AdaptSocket29*/3031import java.io.*;32import java.net.*;33import java.nio.channels.*;34import java.util.Arrays;353637public class AdaptSocket {3839static final java.io.PrintStream out = System.out;4041static void test(TestServers.AbstractServer server,42int timeout,43boolean shouldTimeout)44throws Exception45{46out.println();4748InetSocketAddress isa = new InetSocketAddress(server.getAddress(), server.getPort());49SocketChannel sc = SocketChannel.open();50Socket so = sc.socket();51out.println("opened: " + so);52out.println(" " + sc);5354//out.println("opts: " + sc.options());55so.setTcpNoDelay(true);56//so.setTrafficClass(SocketOpts.IP.TOS_THROUGHPUT);57so.setKeepAlive(true);58so.setSoLinger(true, 42);59so.setOOBInline(true);60so.setReceiveBufferSize(512);61so.setSendBufferSize(512);62//out.println(" " + sc.options());6364if (timeout == 0)65so.connect(isa);66else {67try {68so.connect(isa, timeout);69} catch (SocketTimeoutException x) {70if (shouldTimeout) {71out.println("Connection timed out, as expected");72return;73} else {74throw x;75}76}77}78out.println("connected: " + so);79out.println(" " + sc);80byte[] bb = new byte[100];81int n = so.getInputStream().read(bb);82String s = new String(bb, 0, n - 2, "US-ASCII");83out.println(isa + " says: \"" + s + "\"");84so.shutdownInput();85out.println("ishut: " + sc);86so.shutdownOutput();87out.println("oshut: " + sc);88so.close();89out.println("closed: " + so);90out.println(" " + sc);91}9293static String dataString = "foo\r\n";9495static void testRead(Socket so, boolean shouldTimeout)96throws Exception97{98String data = "foo\r\n";99so.getOutputStream().write(dataString.getBytes("US-ASCII"));100InputStream is = so.getInputStream();101try {102byte[] b = new byte[100];103int n = is.read(b);104if (shouldTimeout) {105throw new Exception("Should time out, but not, data: " + Arrays.toString(b));106}107if (n != 5) {108throw new Exception("Incorrect number of bytes read: " + n);109}110if (!dataString.equals(new String(b, 0, n, "US-ASCII"))) {111throw new Exception("Incorrect data read: " + n);112}113} catch (SocketTimeoutException x) {114if (shouldTimeout) {115out.println("Read timed out, as expected");116return;117}118throw x;119}120}121122static void testRead(TestServers.EchoServer echoServer,123int timeout,124boolean shouldTimeout)125throws Exception126{127out.println();128129InetSocketAddress isa130= new InetSocketAddress(echoServer.getAddress(),131echoServer.getPort());132SocketChannel sc = SocketChannel.open();133sc.connect(isa);134Socket so = sc.socket();135out.println("connected: " + so);136out.println(" " + sc);137138if (timeout > 0)139so.setSoTimeout(timeout);140out.println("timeout: " + so.getSoTimeout());141142testRead(so, shouldTimeout);143for (int i = 0; i < 4; i++) {144out.println("loop: " + i);145testRead(so, shouldTimeout);146}147148sc.close();149}150151static void testConnect(TestServers.AbstractServer server,152int timeout,153boolean shouldFail)154throws Exception155{156SocketAddress sa = new InetSocketAddress(server.getAddress(), server.getPort());157try (SocketChannel sc = SocketChannel.open()) {158Socket s = sc.socket();159try {160if (timeout > 0) {161s.connect(sa, timeout);162} else {163s.connect(sa);164}165if (shouldFail)166throw new Exception("Connection should not be established");167} catch (SocketException se) {168if (!shouldFail)169throw se;170out.println("connect failed as expected: " + se);171}172}173}174175public static void main(String[] args) throws Exception {176177try (TestServers.DayTimeServer dayTimeServer178= TestServers.DayTimeServer.startNewServer()) {179test(dayTimeServer, 0, false);180test(dayTimeServer, 1000, false);181}182183try (TestServers.DayTimeServer lingerDayTimeServer184= TestServers.DayTimeServer.startNewServer(100)) {185// this test no longer really test the connection timeout186// since there is no way to prevent the server from eagerly187// accepting connection...188test(lingerDayTimeServer, 10, true);189}190191try (TestServers.EchoServer echoServer192= TestServers.EchoServer.startNewServer()) {193testRead(echoServer, 0, false);194testRead(echoServer, 8000, false);195}196197try (TestServers.NoResponseServer noResponseServer198= TestServers.NoResponseServer.startNewServer()) {199testRead(noResponseServer, 10, true);200}201202TestServers.RefusingServer refuser = TestServers.RefusingServer.newRefusingServer();203testConnect(refuser, 0, true);204testConnect(refuser, 10000, true);205}206}207208209