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