1 /*
2 * Copyright (c) 2001, 2012, 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 void test(TestServers.DayTimeServer dayTimeServer,
39 int timeout,
40 boolean shouldTimeout)
41 throws Exception
42 {
43 out.println();
44
45 InetSocketAddress isa
46 = new InetSocketAddress(dayTimeServer.getAddress(),
47 dayTimeServer.getPort());
48 SocketChannel sc = SocketChannel.open();
49 Socket so = sc.socket();
50 out.println("opened: " + so);
51 out.println(" " + sc);
83 so.shutdownInput();
84 out.println("ishut: " + sc);
85 so.shutdownOutput();
86 out.println("oshut: " + sc);
87 so.close();
88 out.println("closed: " + so);
89 out.println(" " + sc);
90 }
91
92 static String dataString = "foo\r\n";
93
94 static void testRead(Socket so, boolean shouldTimeout)
95 throws Exception
96 {
97 String data = "foo\r\n";
98 so.getOutputStream().write(dataString.getBytes("US-ASCII"));
99 InputStream is = so.getInputStream();
100 try {
101 byte[] b = new byte[100];
102 int n = is.read(b);
103 if (n != 5)
104 throw new Exception("Incorrect number of bytes read: " + n);
105 if (!dataString.equals(new String(b, 0, n, "US-ASCII")))
106 throw new Exception("Incorrect data read: " + n);
107 } catch (SocketTimeoutException x) {
108 if (shouldTimeout) {
109 out.println("Read timed out, as expected");
110 return;
111 }
112 throw x;
113 }
114 }
115
116 static void testRead(TestServers.EchoServer echoServer,
117 int timeout,
118 boolean shouldTimeout)
119 throws Exception
120 {
121 out.println();
122
123 InetSocketAddress isa
124 = new InetSocketAddress(echoServer.getAddress(),
125 echoServer.getPort());
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
141 sc.close();
142 }
143
144 public static void main(String[] args) throws Exception {
145
146 try (TestServers.DayTimeServer dayTimeServer
147 = TestServers.DayTimeServer.startNewServer()) {
148 test(dayTimeServer, 0, false);
149 test(dayTimeServer, 1000, false);
150 }
151
152 try (TestServers.DayTimeServer lingerDayTimeServer
153 = TestServers.DayTimeServer.startNewServer(100)) {
154 // this test no longer really test the connection timeout
155 // since there is no way to prevent the server from eagerly
156 // accepting connection...
157 test(lingerDayTimeServer, 10, true);
158 }
159
160 try (TestServers.EchoServer echoServer
161 = TestServers.EchoServer.startNewServer()) {
162 testRead(echoServer, 0, false);
163 testRead(echoServer, 8000, false);
164 }
165
166 try (TestServers.EchoServer lingerEchoServer
167 = TestServers.EchoServer.startNewServer(100)) {
168 testRead(lingerEchoServer, 10, true);
169 }
170 }
171 }
|
1 /*
2 * Copyright (c) 2001, 2016, 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
26 * @summary Unit test for socket-channel adaptors
27 * @library ..
28 */
29
30 import java.io.*;
31 import java.net.*;
32 import java.nio.channels.*;
33 import java.util.Arrays;
34
35
36 public class AdaptSocket {
37
38 static java.io.PrintStream out = System.out;
39
40 static void test(TestServers.DayTimeServer dayTimeServer,
41 int timeout,
42 boolean shouldTimeout)
43 throws Exception
44 {
45 out.println();
46
47 InetSocketAddress isa
48 = new InetSocketAddress(dayTimeServer.getAddress(),
49 dayTimeServer.getPort());
50 SocketChannel sc = SocketChannel.open();
51 Socket so = sc.socket();
52 out.println("opened: " + so);
53 out.println(" " + sc);
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 public static void main(String[] args) throws Exception {
153
154 try (TestServers.DayTimeServer dayTimeServer
155 = TestServers.DayTimeServer.startNewServer()) {
156 test(dayTimeServer, 0, false);
157 test(dayTimeServer, 1000, false);
158 }
159
160 try (TestServers.DayTimeServer lingerDayTimeServer
161 = TestServers.DayTimeServer.startNewServer(100)) {
162 // this test no longer really test the connection timeout
163 // since there is no way to prevent the server from eagerly
164 // accepting connection...
165 test(lingerDayTimeServer, 10, true);
166 }
167
168 try (TestServers.EchoServer echoServer
169 = TestServers.EchoServer.startNewServer()) {
170 testRead(echoServer, 0, false);
171 testRead(echoServer, 8000, false);
172 }
173
174 try (TestServers.NoResponseServer noResponseServer
175 = TestServers.NoResponseServer.startNewServer()) {
176 testRead(noResponseServer, 10, true);
177 }
178 }
179 }
|