author | chegar |
Tue, 16 Mar 2010 10:05:51 +0000 | |
changeset 5151 | e62028f44cf6 |
parent 2 | 90ce3da70b43 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2003 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 |
/* |
|
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 InetAddress ia6bad; /* a global 6to4 IPv6 address, which cant be connected to */ |
|
42 |
static Inet4Address ia4addr; |
|
43 |
||
44 |
static { |
|
45 |
ia6addr = getFirstLocalIPv6Address (); |
|
46 |
ia4addr = getFirstLocalIPv4Address (); |
|
47 |
try { |
|
48 |
ia4any = InetAddress.getByName ("0.0.0.0"); |
|
49 |
ia6any = InetAddress.getByName ("::0"); |
|
5151
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
50 |
if (ia6addr != null) { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
51 |
int scope = ia6addr.getScopeId(); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
52 |
if (scope != 0) { |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
53 |
ia6bad = InetAddress.getByName ("fe80::1:2:3:4:5:6%"+scope); |
e62028f44cf6
6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents:
2
diff
changeset
|
54 |
} |
2 | 55 |
} else { |
56 |
ia6bad = InetAddress.getByName ("fe80::1:2:3:4:5:6"); |
|
57 |
} |
|
58 |
} catch (Exception e) { |
|
59 |
e.printStackTrace(); |
|
60 |
} |
|
61 |
} |
|
62 |
||
63 |
public static void main (String[] args) throws Exception { |
|
64 |
checkDebug(args); |
|
65 |
if (ia6addr == null) { |
|
66 |
System.out.println ("No IPV6 addresses: exiting test"); |
|
67 |
return; |
|
68 |
} |
|
69 |
dprintln ("Local Addresses"); |
|
70 |
dprintln (ia4addr.toString()); |
|
71 |
dprintln (ia6addr.toString()); |
|
72 |
dprintln ("Bad address: " + ia6bad); |
|
73 |
test1 (0); |
|
74 |
test1 (5100); |
|
75 |
test2(); |
|
76 |
test3(); |
|
77 |
test4(); |
|
78 |
} |
|
79 |
||
80 |
/* basic TCP connectivity test using IPv6 only and IPv4/IPv6 together */ |
|
81 |
||
82 |
static void test1 (int port) throws Exception { |
|
83 |
server = new ServerSocket (port); |
|
84 |
if (port == 0) { |
|
85 |
port = server.getLocalPort(); |
|
86 |
} |
|
87 |
// try Ipv6 only |
|
88 |
c1 = new Socket ("::1", port); |
|
89 |
s1 = server.accept (); |
|
90 |
simpleDataExchange (c1, s1); |
|
91 |
s1.close (); |
|
92 |
c1.close(); |
|
93 |
// try with both IPv4 and Ipv6 |
|
94 |
c1 = new Socket ("127.0.0.1", port); |
|
95 |
c2 = new Socket ("::1", port); |
|
96 |
s1 = server.accept(); |
|
97 |
s2 = server.accept(); |
|
98 |
s1peer = s1.getInetAddress(); |
|
99 |
s2peer = s2.getInetAddress(); |
|
100 |
if (s1peer instanceof Inet6Address) { |
|
101 |
t_assert ((s2peer instanceof Inet4Address)); |
|
102 |
simpleDataExchange (c2, s1); |
|
103 |
simpleDataExchange (c1, s2); |
|
104 |
} else { |
|
105 |
t_assert ((s2peer instanceof Inet6Address)); |
|
106 |
simpleDataExchange (c1, s1); |
|
107 |
simpleDataExchange (c2, s2); |
|
108 |
} |
|
109 |
c1.close(); |
|
110 |
c2.close(); |
|
111 |
s1.close(); |
|
112 |
s2.close(); |
|
113 |
server.close (); |
|
114 |
System.out.println ("Test1: OK"); |
|
115 |
} |
|
116 |
||
117 |
/** bind tests: |
|
118 |
* 1. bind to specific address IPv4 only (any port) |
|
119 |
* 2. bind to specific address IPv6 only (any port) |
|
120 |
* 3. bind to specific address IPv4 only (specific port) |
|
121 |
* 4. bind to specific address IPv4 only (specific port) |
|
122 |
* 5. bind to any address IPv4 (test collision) |
|
123 |
*/ |
|
124 |
||
125 |
static void test2 () throws Exception { |
|
126 |
||
127 |
server = new ServerSocket (); |
|
128 |
InetSocketAddress sadr = new InetSocketAddress (ia4addr, 0); |
|
129 |
server.bind (sadr); |
|
130 |
dprintln ("server bound to " + sadr); |
|
131 |
int port = server.getLocalPort(); |
|
132 |
InetSocketAddress sadr6 = new InetSocketAddress (ia6addr, port); |
|
133 |
||
134 |
c1 = new Socket (ia4addr, port); |
|
135 |
try { |
|
136 |
dprintln ("connecting to " + ia6addr); |
|
137 |
c2 = new Socket (); |
|
138 |
c2.connect (sadr6, 1000); |
|
139 |
throw new RuntimeException ("connect to IPv6 address should be refused"); |
|
140 |
} catch (IOException e) { } |
|
141 |
server.close (); |
|
142 |
c1.close (); |
|
143 |
||
144 |
/* now try IPv6 only */ |
|
145 |
||
146 |
server = new ServerSocket (); |
|
147 |
sadr = new InetSocketAddress (ia6addr, 0); |
|
148 |
dprintln ("binding to " + sadr); |
|
149 |
server.bind (sadr); |
|
150 |
port = server.getLocalPort(); |
|
151 |
||
152 |
c1 = new Socket (ia6addr, port); |
|
153 |
try { |
|
154 |
c2 = new Socket (ia4addr, port); |
|
155 |
throw new RuntimeException ("connect to IPv4 address should be refused"); |
|
156 |
} catch (IOException e) { } |
|
157 |
server.close (); |
|
158 |
c1.close (); |
|
159 |
||
160 |
/* now try IPv6 specific port only */ |
|
161 |
||
162 |
server = new ServerSocket (); |
|
163 |
sadr = new InetSocketAddress (ia6addr, 5200); |
|
164 |
server.bind (sadr); |
|
165 |
port = server.getLocalPort(); |
|
166 |
t_assert (port == 5200); |
|
167 |
||
168 |
c1 = new Socket (ia6addr, port); |
|
169 |
try { |
|
170 |
c2 = new Socket (ia4addr, port); |
|
171 |
throw new RuntimeException ("connect to IPv4 address should be refused"); |
|
172 |
} catch (IOException e) { } |
|
173 |
server.close (); |
|
174 |
c1.close (); |
|
175 |
||
176 |
/* now try IPv4 specific port only */ |
|
177 |
||
178 |
server = new ServerSocket (); |
|
179 |
sadr = new InetSocketAddress (ia4addr, 5200); |
|
180 |
server.bind (sadr); |
|
181 |
port = server.getLocalPort(); |
|
182 |
t_assert (port == 5200); |
|
183 |
||
184 |
c1 = new Socket (ia4addr, port); |
|
185 |
||
186 |
try { |
|
187 |
c2 = new Socket (ia6addr, port); |
|
188 |
throw new RuntimeException ("connect to IPv6 address should be refused"); |
|
189 |
} catch (IOException e) { } |
|
190 |
server.accept().close(); |
|
191 |
c1.close (); |
|
192 |
server.close(); |
|
193 |
System.out.println ("Test2: OK"); |
|
194 |
} |
|
195 |
||
196 |
/* Test timeouts on accept(), connect() */ |
|
197 |
||
198 |
static void test3 () throws Exception { |
|
199 |
server = new ServerSocket (0); |
|
200 |
server.setSoTimeout (5000); |
|
201 |
int port = server.getLocalPort(); |
|
202 |
long t1 = System.currentTimeMillis(); |
|
203 |
try { |
|
204 |
server.accept (); |
|
205 |
throw new RuntimeException ("accept should not have returned"); |
|
206 |
} catch (SocketTimeoutException e) {} |
|
207 |
t1 = System.currentTimeMillis() - t1; |
|
208 |
checkTime (t1, 5000); |
|
209 |
||
210 |
c1 = new Socket (); |
|
211 |
c1.connect (new InetSocketAddress (ia4addr, port), 1000); |
|
212 |
s1 = server.accept (); |
|
213 |
simpleDataExchange (c1,s1); |
|
214 |
c2 = new Socket (); |
|
215 |
c2.connect (new InetSocketAddress (ia6addr, port), 1000); |
|
216 |
s2 = server.accept (); |
|
217 |
simpleDataExchange (c2,s2); |
|
218 |
c3 = new Socket (); |
|
219 |
c3.connect (new InetSocketAddress (ia6addr, port), 1000); |
|
220 |
s3 = server.accept (); |
|
221 |
c2.close (); s2.close(); |
|
222 |
server.close(); |
|
223 |
simpleDataExchange (c3,s3); |
|
224 |
c1.close (); c2.close(); |
|
225 |
s1.close (); s2.close(); |
|
226 |
||
227 |
/* check if connect() timesout when connecting to unknown dest. */ |
|
228 |
||
229 |
c1 = new Socket(); |
|
230 |
t1 = System.currentTimeMillis(); |
|
231 |
InetSocketAddress ad1 = new InetSocketAddress (ia6bad, 2500); |
|
232 |
try { |
|
233 |
c1.connect (ad1, 5000); |
|
234 |
throw new RuntimeException ("timeout exception was expected"); |
|
235 |
} catch (SocketTimeoutException e) { |
|
236 |
t1 = System.currentTimeMillis() - t1; |
|
237 |
checkTime (t1, 5000); |
|
238 |
} catch (NoRouteToHostException e1) { |
|
239 |
} |
|
240 |
System.out.println ("Test3: OK"); |
|
241 |
} |
|
242 |
||
243 |
/* Test: connect to IPv4 mapped address */ |
|
244 |
||
245 |
static void test4 () throws Exception { |
|
246 |
server = new ServerSocket (0); |
|
247 |
int port = server.getLocalPort(); |
|
248 |
||
249 |
/* create an IPv4 mapped address corresponding to local host */ |
|
250 |
||
251 |
byte[] b = {0,0,0,0,0,0,0,0,0,0,(byte)0xff,(byte)0xff,0,0,0,0}; |
|
252 |
byte[] ia4 = ia4addr.getAddress(); |
|
253 |
b[12] = ia4[0]; |
|
254 |
b[13] = ia4[1]; |
|
255 |
b[14] = ia4[2]; |
|
256 |
b[15] = ia4[3]; |
|
257 |
||
258 |
InetAddress dest = InetAddress.getByAddress (b); |
|
259 |
c1 = new Socket (dest, port); |
|
260 |
s1 = server.accept (); |
|
261 |
simpleDataExchange (c1,s1); |
|
262 |
c1.close (); |
|
263 |
s1.close (); |
|
264 |
server.close (); |
|
265 |
System.out.println ("Test4: OK"); |
|
266 |
} |
|
267 |
} |