2
|
1 |
/*
|
7668
|
2 |
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4868820
|
|
27 |
* @summary IPv6 support for Windows XP and 2003 server
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.net.*;
|
|
31 |
import java.io.*;
|
|
32 |
|
|
33 |
public class TcpTest extends Tests {
|
|
34 |
static ServerSocket server, server1, server2;
|
|
35 |
static Socket c1, c2, c3, s1, s2, s3;
|
|
36 |
static InetAddress s1peer, s2peer;
|
|
37 |
|
|
38 |
static InetAddress ia4any;
|
|
39 |
static InetAddress ia6any;
|
|
40 |
static Inet6Address ia6addr;
|
|
41 |
static Inet4Address ia4addr;
|
|
42 |
|
|
43 |
static {
|
|
44 |
ia6addr = getFirstLocalIPv6Address ();
|
|
45 |
ia4addr = getFirstLocalIPv4Address ();
|
|
46 |
try {
|
|
47 |
ia4any = InetAddress.getByName ("0.0.0.0");
|
|
48 |
ia6any = InetAddress.getByName ("::0");
|
|
49 |
} catch (Exception e) {
|
|
50 |
e.printStackTrace();
|
|
51 |
}
|
|
52 |
}
|
|
53 |
|
|
54 |
public static void main (String[] args) throws Exception {
|
|
55 |
checkDebug(args);
|
|
56 |
if (ia6addr == null) {
|
|
57 |
System.out.println ("No IPV6 addresses: exiting test");
|
|
58 |
return;
|
|
59 |
}
|
|
60 |
dprintln ("Local Addresses");
|
|
61 |
dprintln (ia4addr.toString());
|
|
62 |
dprintln (ia6addr.toString());
|
|
63 |
test1 (0);
|
|
64 |
test1 (5100);
|
|
65 |
test2();
|
|
66 |
test3();
|
|
67 |
test4();
|
|
68 |
}
|
|
69 |
|
|
70 |
/* basic TCP connectivity test using IPv6 only and IPv4/IPv6 together */
|
|
71 |
|
|
72 |
static void test1 (int port) throws Exception {
|
|
73 |
server = new ServerSocket (port);
|
|
74 |
if (port == 0) {
|
|
75 |
port = server.getLocalPort();
|
|
76 |
}
|
|
77 |
// try Ipv6 only
|
|
78 |
c1 = new Socket ("::1", port);
|
|
79 |
s1 = server.accept ();
|
|
80 |
simpleDataExchange (c1, s1);
|
|
81 |
s1.close ();
|
|
82 |
c1.close();
|
|
83 |
// try with both IPv4 and Ipv6
|
|
84 |
c1 = new Socket ("127.0.0.1", port);
|
|
85 |
c2 = new Socket ("::1", port);
|
|
86 |
s1 = server.accept();
|
|
87 |
s2 = server.accept();
|
|
88 |
s1peer = s1.getInetAddress();
|
|
89 |
s2peer = s2.getInetAddress();
|
|
90 |
if (s1peer instanceof Inet6Address) {
|
|
91 |
t_assert ((s2peer instanceof Inet4Address));
|
|
92 |
simpleDataExchange (c2, s1);
|
|
93 |
simpleDataExchange (c1, s2);
|
|
94 |
} else {
|
|
95 |
t_assert ((s2peer instanceof Inet6Address));
|
|
96 |
simpleDataExchange (c1, s1);
|
|
97 |
simpleDataExchange (c2, s2);
|
|
98 |
}
|
|
99 |
c1.close();
|
|
100 |
c2.close();
|
|
101 |
s1.close();
|
|
102 |
s2.close();
|
|
103 |
server.close ();
|
|
104 |
System.out.println ("Test1: OK");
|
|
105 |
}
|
|
106 |
|
|
107 |
/** bind tests:
|
|
108 |
* 1. bind to specific address IPv4 only (any port)
|
|
109 |
* 2. bind to specific address IPv6 only (any port)
|
|
110 |
* 3. bind to specific address IPv4 only (specific port)
|
|
111 |
* 4. bind to specific address IPv4 only (specific port)
|
|
112 |
* 5. bind to any address IPv4 (test collision)
|
|
113 |
*/
|
|
114 |
|
|
115 |
static void test2 () throws Exception {
|
|
116 |
|
|
117 |
server = new ServerSocket ();
|
|
118 |
InetSocketAddress sadr = new InetSocketAddress (ia4addr, 0);
|
|
119 |
server.bind (sadr);
|
|
120 |
dprintln ("server bound to " + sadr);
|
|
121 |
int port = server.getLocalPort();
|
|
122 |
InetSocketAddress sadr6 = new InetSocketAddress (ia6addr, port);
|
|
123 |
|
|
124 |
c1 = new Socket (ia4addr, port);
|
|
125 |
try {
|
|
126 |
dprintln ("connecting to " + ia6addr);
|
|
127 |
c2 = new Socket ();
|
|
128 |
c2.connect (sadr6, 1000);
|
|
129 |
throw new RuntimeException ("connect to IPv6 address should be refused");
|
|
130 |
} catch (IOException e) { }
|
|
131 |
server.close ();
|
|
132 |
c1.close ();
|
|
133 |
|
|
134 |
/* now try IPv6 only */
|
|
135 |
|
|
136 |
server = new ServerSocket ();
|
|
137 |
sadr = new InetSocketAddress (ia6addr, 0);
|
|
138 |
dprintln ("binding to " + sadr);
|
|
139 |
server.bind (sadr);
|
|
140 |
port = server.getLocalPort();
|
|
141 |
|
|
142 |
c1 = new Socket (ia6addr, port);
|
|
143 |
try {
|
|
144 |
c2 = new Socket (ia4addr, port);
|
|
145 |
throw new RuntimeException ("connect to IPv4 address should be refused");
|
|
146 |
} catch (IOException e) { }
|
|
147 |
server.close ();
|
|
148 |
c1.close ();
|
|
149 |
|
|
150 |
/* now try IPv6 specific port only */
|
|
151 |
|
|
152 |
server = new ServerSocket ();
|
|
153 |
sadr = new InetSocketAddress (ia6addr, 5200);
|
|
154 |
server.bind (sadr);
|
|
155 |
port = server.getLocalPort();
|
|
156 |
t_assert (port == 5200);
|
|
157 |
|
|
158 |
c1 = new Socket (ia6addr, port);
|
|
159 |
try {
|
|
160 |
c2 = new Socket (ia4addr, port);
|
|
161 |
throw new RuntimeException ("connect to IPv4 address should be refused");
|
|
162 |
} catch (IOException e) { }
|
|
163 |
server.close ();
|
|
164 |
c1.close ();
|
|
165 |
|
|
166 |
/* now try IPv4 specific port only */
|
|
167 |
|
|
168 |
server = new ServerSocket ();
|
|
169 |
sadr = new InetSocketAddress (ia4addr, 5200);
|
|
170 |
server.bind (sadr);
|
|
171 |
port = server.getLocalPort();
|
|
172 |
t_assert (port == 5200);
|
|
173 |
|
|
174 |
c1 = new Socket (ia4addr, port);
|
|
175 |
|
|
176 |
try {
|
|
177 |
c2 = new Socket (ia6addr, port);
|
|
178 |
throw new RuntimeException ("connect to IPv6 address should be refused");
|
|
179 |
} catch (IOException e) { }
|
|
180 |
server.accept().close();
|
|
181 |
c1.close ();
|
|
182 |
server.close();
|
|
183 |
System.out.println ("Test2: OK");
|
|
184 |
}
|
|
185 |
|
|
186 |
/* Test timeouts on accept(), connect() */
|
|
187 |
|
|
188 |
static void test3 () throws Exception {
|
|
189 |
server = new ServerSocket (0);
|
|
190 |
server.setSoTimeout (5000);
|
|
191 |
int port = server.getLocalPort();
|
|
192 |
long t1 = System.currentTimeMillis();
|
|
193 |
try {
|
|
194 |
server.accept ();
|
|
195 |
throw new RuntimeException ("accept should not have returned");
|
|
196 |
} catch (SocketTimeoutException e) {}
|
|
197 |
t1 = System.currentTimeMillis() - t1;
|
|
198 |
checkTime (t1, 5000);
|
|
199 |
|
|
200 |
c1 = new Socket ();
|
|
201 |
c1.connect (new InetSocketAddress (ia4addr, port), 1000);
|
|
202 |
s1 = server.accept ();
|
|
203 |
simpleDataExchange (c1,s1);
|
|
204 |
c2 = new Socket ();
|
|
205 |
c2.connect (new InetSocketAddress (ia6addr, port), 1000);
|
|
206 |
s2 = server.accept ();
|
|
207 |
simpleDataExchange (c2,s2);
|
|
208 |
c3 = new Socket ();
|
|
209 |
c3.connect (new InetSocketAddress (ia6addr, port), 1000);
|
|
210 |
s3 = server.accept ();
|
|
211 |
c2.close (); s2.close();
|
|
212 |
server.close();
|
|
213 |
simpleDataExchange (c3,s3);
|
|
214 |
c1.close (); c2.close();
|
|
215 |
s1.close (); s2.close();
|
|
216 |
|
|
217 |
System.out.println ("Test3: OK");
|
|
218 |
}
|
|
219 |
|
|
220 |
/* Test: connect to IPv4 mapped address */
|
|
221 |
|
|
222 |
static void test4 () throws Exception {
|
|
223 |
server = new ServerSocket (0);
|
|
224 |
int port = server.getLocalPort();
|
|
225 |
|
|
226 |
/* create an IPv4 mapped address corresponding to local host */
|
|
227 |
|
|
228 |
byte[] b = {0,0,0,0,0,0,0,0,0,0,(byte)0xff,(byte)0xff,0,0,0,0};
|
|
229 |
byte[] ia4 = ia4addr.getAddress();
|
|
230 |
b[12] = ia4[0];
|
|
231 |
b[13] = ia4[1];
|
|
232 |
b[14] = ia4[2];
|
|
233 |
b[15] = ia4[3];
|
|
234 |
|
|
235 |
InetAddress dest = InetAddress.getByAddress (b);
|
|
236 |
c1 = new Socket (dest, port);
|
|
237 |
s1 = server.accept ();
|
|
238 |
simpleDataExchange (c1,s1);
|
|
239 |
c1.close ();
|
|
240 |
s1.close ();
|
|
241 |
server.close ();
|
|
242 |
System.out.println ("Test4: OK");
|
|
243 |
}
|
|
244 |
}
|