author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57805 | e62f402c337f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55102 | 2 |
* Copyright (c) 2003, 2019, 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 |
|
36432
cb9e462e049a
8151260: Mark URLPermission/URLTest.java and ipv6tests/TcpTest.java as intermittently failing
amlu
parents:
34888
diff
changeset
|
27 |
* @key intermittent |
54746
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
49799
diff
changeset
|
28 |
* @summary IPv6 support for Windows XP and 2003 server. This test requires |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
49799
diff
changeset
|
29 |
* binding to the wildcard address, and as such is susceptible |
61049e91eae5
8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents:
49799
diff
changeset
|
30 |
* of intermittent failures caused by port reuse policy. |
49799
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
31 |
* @library /test/lib |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
32 |
* @build jdk.test.lib.NetworkConfiguration |
33dcb9c42f55
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents:
47216
diff
changeset
|
33 |
* jdk.test.lib.Platform |
47017 | 34 |
* @run main TcpTest -d |
55102 | 35 |
* @run main/othervm -Djdk.net.usePlainSocketImpl TcpTest -d |
2 | 36 |
*/ |
37 |
||
38 |
import java.net.*; |
|
39 |
import java.io.*; |
|
40 |
||
41 |
public class TcpTest extends Tests { |
|
42 |
static ServerSocket server, server1, server2; |
|
43 |
static Socket c1, c2, c3, s1, s2, s3; |
|
44 |
static InetAddress s1peer, s2peer; |
|
45 |
||
46 |
static InetAddress ia4any; |
|
47 |
static InetAddress ia6any; |
|
48 |
static Inet6Address ia6addr; |
|
49 |
static Inet4Address ia4addr; |
|
50 |
||
51 |
static { |
|
52 |
ia6addr = getFirstLocalIPv6Address (); |
|
53 |
ia4addr = getFirstLocalIPv4Address (); |
|
54 |
try { |
|
55 |
ia4any = InetAddress.getByName ("0.0.0.0"); |
|
56 |
ia6any = InetAddress.getByName ("::0"); |
|
57 |
} catch (Exception e) { |
|
58 |
e.printStackTrace(); |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
public static void main (String[] args) throws Exception { |
|
63 |
checkDebug(args); |
|
54903
5a211ee83bf1
8224018: test/jdk/java/net/ipv6tests/{Tcp,Udp}Test.java assume IPv4 is available
aeubanks
parents:
54746
diff
changeset
|
64 |
if (ia4addr == null) { |
5a211ee83bf1
8224018: test/jdk/java/net/ipv6tests/{Tcp,Udp}Test.java assume IPv4 is available
aeubanks
parents:
54746
diff
changeset
|
65 |
System.out.println ("No IPV4 addresses: exiting test"); |
5a211ee83bf1
8224018: test/jdk/java/net/ipv6tests/{Tcp,Udp}Test.java assume IPv4 is available
aeubanks
parents:
54746
diff
changeset
|
66 |
return; |
5a211ee83bf1
8224018: test/jdk/java/net/ipv6tests/{Tcp,Udp}Test.java assume IPv4 is available
aeubanks
parents:
54746
diff
changeset
|
67 |
} |
2 | 68 |
if (ia6addr == null) { |
69 |
System.out.println ("No IPV6 addresses: exiting test"); |
|
70 |
return; |
|
71 |
} |
|
72 |
dprintln ("Local Addresses"); |
|
73 |
dprintln (ia4addr.toString()); |
|
74 |
dprintln (ia6addr.toString()); |
|
34888
e3161752067f
8140472: java/net/ipv6tests/TcpTest.java failed intermittently with java.net.BindException: Address already in use: NET_Bind
xiaofeya
parents:
7668
diff
changeset
|
75 |
test1(); |
2 | 76 |
test2(); |
77 |
test3(); |
|
78 |
test4(); |
|
79 |
} |
|
80 |
||
81 |
/* basic TCP connectivity test using IPv6 only and IPv4/IPv6 together */ |
|
82 |
||
34888
e3161752067f
8140472: java/net/ipv6tests/TcpTest.java failed intermittently with java.net.BindException: Address already in use: NET_Bind
xiaofeya
parents:
7668
diff
changeset
|
83 |
static void test1 () throws Exception { |
e3161752067f
8140472: java/net/ipv6tests/TcpTest.java failed intermittently with java.net.BindException: Address already in use: NET_Bind
xiaofeya
parents:
7668
diff
changeset
|
84 |
server = new ServerSocket (0); |
e3161752067f
8140472: java/net/ipv6tests/TcpTest.java failed intermittently with java.net.BindException: Address already in use: NET_Bind
xiaofeya
parents:
7668
diff
changeset
|
85 |
int port = server.getLocalPort(); |
2 | 86 |
// try Ipv6 only |
87 |
c1 = new Socket ("::1", port); |
|
88 |
s1 = server.accept (); |
|
89 |
simpleDataExchange (c1, s1); |
|
90 |
s1.close (); |
|
91 |
c1.close(); |
|
92 |
// try with both IPv4 and Ipv6 |
|
93 |
c1 = new Socket ("127.0.0.1", port); |
|
94 |
c2 = new Socket ("::1", port); |
|
95 |
s1 = server.accept(); |
|
96 |
s2 = server.accept(); |
|
97 |
s1peer = s1.getInetAddress(); |
|
98 |
s2peer = s2.getInetAddress(); |
|
99 |
if (s1peer instanceof Inet6Address) { |
|
100 |
t_assert ((s2peer instanceof Inet4Address)); |
|
101 |
simpleDataExchange (c2, s1); |
|
102 |
simpleDataExchange (c1, s2); |
|
103 |
} else { |
|
104 |
t_assert ((s2peer instanceof Inet6Address)); |
|
105 |
simpleDataExchange (c1, s1); |
|
106 |
simpleDataExchange (c2, s2); |
|
107 |
} |
|
108 |
c1.close(); |
|
109 |
c2.close(); |
|
110 |
s1.close(); |
|
111 |
s2.close(); |
|
112 |
server.close (); |
|
113 |
System.out.println ("Test1: OK"); |
|
114 |
} |
|
115 |
||
116 |
/** bind tests: |
|
117 |
* 1. bind to specific address IPv4 only (any port) |
|
118 |
* 2. bind to specific address IPv6 only (any port) |
|
34888
e3161752067f
8140472: java/net/ipv6tests/TcpTest.java failed intermittently with java.net.BindException: Address already in use: NET_Bind
xiaofeya
parents:
7668
diff
changeset
|
119 |
* 3. bind to any address IPv4 (test collision) |
2 | 120 |
*/ |
121 |
||
122 |
static void test2 () throws Exception { |
|
123 |
||
124 |
server = new ServerSocket (); |
|
125 |
InetSocketAddress sadr = new InetSocketAddress (ia4addr, 0); |
|
126 |
server.bind (sadr); |
|
127 |
dprintln ("server bound to " + sadr); |
|
128 |
int port = server.getLocalPort(); |
|
129 |
InetSocketAddress sadr6 = new InetSocketAddress (ia6addr, port); |
|
130 |
||
131 |
c1 = new Socket (ia4addr, port); |
|
132 |
try { |
|
133 |
dprintln ("connecting to " + ia6addr); |
|
134 |
c2 = new Socket (); |
|
135 |
c2.connect (sadr6, 1000); |
|
57805
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
136 |
dprintln ("Unexpected successful connection: " + c2); |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
137 |
// Connect should fail with timeout exception. However, if something else is |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
138 |
// accepting connections, verify it is not our server. |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
139 |
server.setSoTimeout(500); |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
140 |
// Ok if accept() fails because of timeout |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
141 |
while (true) { |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
142 |
// acceptedSocket could be connected to c1, but not c2 |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
143 |
try (Socket acceptedSocket = server.accept()) { |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
144 |
dprintln("accepted socket: " + acceptedSocket); |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
145 |
if (acceptedSocket.getRemoteSocketAddress().equals(c2.getLocalSocketAddress())) |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
146 |
throw new RuntimeException("connect to IPv6 address should be refused"); |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
147 |
} |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
148 |
} |
2 | 149 |
} catch (IOException e) { } |
150 |
server.close (); |
|
151 |
c1.close (); |
|
47017 | 152 |
if (c2 != null) { |
153 |
c2.close(); |
|
154 |
} |
|
2 | 155 |
|
156 |
/* now try IPv6 only */ |
|
157 |
||
158 |
server = new ServerSocket (); |
|
159 |
sadr = new InetSocketAddress (ia6addr, 0); |
|
160 |
dprintln ("binding to " + sadr); |
|
161 |
server.bind (sadr); |
|
162 |
port = server.getLocalPort(); |
|
163 |
||
164 |
c1 = new Socket (ia6addr, port); |
|
165 |
try { |
|
47017 | 166 |
dprintln ("connecting to " + ia4addr); |
2 | 167 |
c2 = new Socket (ia4addr, port); |
57805
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
168 |
dprintln ("Unexpected successful connection: " + c2); |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
169 |
// Connect should fail with timeout exception. However, if something else is |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
170 |
// accepting connections, verify it is not our server. |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
171 |
server.setSoTimeout(500); |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
172 |
// Ok if accept() fails because of timeout |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
173 |
while (true) { |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
174 |
// acceptedSocket could be connected to c1, but not c2 |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
175 |
try (Socket acceptedSocket = server.accept()) { |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
176 |
dprintln("accepted socket: " + acceptedSocket); |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
177 |
if (acceptedSocket.getRemoteSocketAddress().equals(c2.getLocalSocketAddress())) |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
178 |
throw new RuntimeException("connect to IPv4 address should be refused"); |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
179 |
} |
e62f402c337f
8229421: The logic of java/net/ipv6tests/TcpTest.java is flawed
dfuchs
parents:
55102
diff
changeset
|
180 |
} |
2 | 181 |
} catch (IOException e) { } |
182 |
server.close (); |
|
183 |
c1.close (); |
|
47017 | 184 |
if (c2 != null) { |
185 |
c2.close(); |
|
186 |
} |
|
2 | 187 |
|
188 |
System.out.println ("Test2: OK"); |
|
189 |
} |
|
190 |
||
191 |
/* Test timeouts on accept(), connect() */ |
|
192 |
||
193 |
static void test3 () throws Exception { |
|
194 |
server = new ServerSocket (0); |
|
195 |
server.setSoTimeout (5000); |
|
196 |
int port = server.getLocalPort(); |
|
197 |
long t1 = System.currentTimeMillis(); |
|
198 |
try { |
|
199 |
server.accept (); |
|
200 |
throw new RuntimeException ("accept should not have returned"); |
|
201 |
} catch (SocketTimeoutException e) {} |
|
202 |
t1 = System.currentTimeMillis() - t1; |
|
203 |
checkTime (t1, 5000); |
|
204 |
||
205 |
c1 = new Socket (); |
|
206 |
c1.connect (new InetSocketAddress (ia4addr, port), 1000); |
|
207 |
s1 = server.accept (); |
|
208 |
simpleDataExchange (c1,s1); |
|
209 |
c2 = new Socket (); |
|
210 |
c2.connect (new InetSocketAddress (ia6addr, port), 1000); |
|
211 |
s2 = server.accept (); |
|
212 |
simpleDataExchange (c2,s2); |
|
213 |
c3 = new Socket (); |
|
214 |
c3.connect (new InetSocketAddress (ia6addr, port), 1000); |
|
215 |
s3 = server.accept (); |
|
216 |
c2.close (); s2.close(); |
|
217 |
server.close(); |
|
218 |
simpleDataExchange (c3,s3); |
|
219 |
c1.close (); c2.close(); |
|
220 |
s1.close (); s2.close(); |
|
221 |
||
222 |
System.out.println ("Test3: OK"); |
|
223 |
} |
|
224 |
||
225 |
/* Test: connect to IPv4 mapped address */ |
|
226 |
||
227 |
static void test4 () throws Exception { |
|
228 |
server = new ServerSocket (0); |
|
229 |
int port = server.getLocalPort(); |
|
230 |
||
231 |
/* create an IPv4 mapped address corresponding to local host */ |
|
232 |
||
233 |
byte[] b = {0,0,0,0,0,0,0,0,0,0,(byte)0xff,(byte)0xff,0,0,0,0}; |
|
234 |
byte[] ia4 = ia4addr.getAddress(); |
|
235 |
b[12] = ia4[0]; |
|
236 |
b[13] = ia4[1]; |
|
237 |
b[14] = ia4[2]; |
|
238 |
b[15] = ia4[3]; |
|
239 |
||
240 |
InetAddress dest = InetAddress.getByAddress (b); |
|
241 |
c1 = new Socket (dest, port); |
|
242 |
s1 = server.accept (); |
|
243 |
simpleDataExchange (c1,s1); |
|
244 |
c1.close (); |
|
245 |
s1.close (); |
|
246 |
server.close (); |
|
247 |
System.out.println ("Test4: OK"); |
|
248 |
} |
|
249 |
} |