author | igerasim |
Thu, 19 Apr 2018 09:36:06 -0700 | |
changeset 49833 | 06a6ae39d892 |
parent 49431 | 5812849b5027 |
child 54770 | 62b6e7587b1f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49431
5812849b5027
8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 2018, 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 4476378 |
|
27 |
* @summary Check the specific behaviour of the setReuseAddress(boolean) |
|
49833
06a6ae39d892
8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
49431
diff
changeset
|
28 |
* method. |
13043
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
5506
diff
changeset
|
29 |
* @run main Basic |
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
5506
diff
changeset
|
30 |
* @run main/othervm -Dsun.net.useExclusiveBind Basic |
49833
06a6ae39d892
8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
49431
diff
changeset
|
31 |
* @run main/othervm -Dsun.net.useExclusiveBind=true Basic |
49431
5812849b5027
8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents:
47216
diff
changeset
|
32 |
* @run main/othervm -Djava.net.preferIPv4Stack=true Basic |
49833
06a6ae39d892
8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
49431
diff
changeset
|
33 |
* @run main/othervm -Dsun.net.useExclusiveBind |
06a6ae39d892
8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
49431
diff
changeset
|
34 |
* -Djava.net.preferIPv4Stack=true Basic |
06a6ae39d892
8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
49431
diff
changeset
|
35 |
* @run main/othervm -Dsun.net.useExclusiveBind=true |
06a6ae39d892
8201510: Merge TwoStacksPlainSocketImpl into DualStackPlainSocketImpl [win]
igerasim
parents:
49431
diff
changeset
|
36 |
* -Djava.net.preferIPv4Stack=true Basic |
2 | 37 |
*/ |
38 |
import java.net.*; |
|
39 |
||
40 |
public class Basic { |
|
41 |
||
42 |
static int testCount = 0; |
|
43 |
static int failures = 0; |
|
44 |
||
45 |
void test(String msg) { |
|
46 |
testCount++; |
|
47 |
System.out.println("***************************************"); |
|
48 |
System.out.println("Test " + testCount + ": " + msg); |
|
49 |
} |
|
50 |
||
51 |
void passed() { |
|
52 |
System.out.println("Test passed."); |
|
53 |
} |
|
54 |
||
55 |
void failed() { |
|
56 |
failures++; |
|
57 |
System.out.println("Test failed."); |
|
58 |
} |
|
59 |
||
60 |
void check(boolean pass) { |
|
61 |
if (pass) { |
|
62 |
passed(); |
|
63 |
} else { |
|
64 |
failed(); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
void SocketTests() throws Exception { |
|
69 |
Socket s1 = new Socket(); |
|
70 |
||
71 |
test("Socket should be created with SO_REUSEADDR disabled"); |
|
72 |
check(!s1.getReuseAddress()); |
|
73 |
||
74 |
test("Socket.setReuseAddress(true)"); |
|
75 |
s1.setReuseAddress(true); |
|
76 |
check(s1.getReuseAddress()); |
|
77 |
||
78 |
test("Socket.setReuseAddress(false)"); |
|
79 |
s1.setReuseAddress(false); |
|
80 |
check(!s1.getReuseAddress() ); |
|
81 |
||
82 |
/* bind to any port */ |
|
83 |
s1.bind( new InetSocketAddress(0) ); |
|
84 |
||
85 |
test("Binding Socket to port already in use should throw " + |
|
86 |
"a BindException"); |
|
87 |
Socket s2 = new Socket(); |
|
88 |
try { |
|
89 |
s2.bind( new InetSocketAddress(s1.getLocalPort()) ); |
|
90 |
failed(); |
|
91 |
} catch (BindException e) { |
|
92 |
passed(); |
|
93 |
} |
|
94 |
s2.close(); |
|
95 |
||
96 |
s1.close(); |
|
97 |
} |
|
98 |
||
99 |
void ServerSocketTests() throws Exception { |
|
100 |
ServerSocket s1 = new ServerSocket(); |
|
101 |
||
102 |
test("ServerSocket.setReuseAddress(true)"); |
|
103 |
s1.setReuseAddress(true); |
|
104 |
check(s1.getReuseAddress()); |
|
105 |
||
106 |
test("Socket.setReuseAddress(false)"); |
|
107 |
s1.setReuseAddress(false); |
|
108 |
check(!s1.getReuseAddress() ); |
|
109 |
||
110 |
/* bind to any port */ |
|
111 |
s1.bind( new InetSocketAddress(0) ); |
|
112 |
||
113 |
test("Binding ServerSocket to port already in use should throw " + |
|
114 |
"a BindException"); |
|
115 |
ServerSocket s2 = new ServerSocket(); |
|
116 |
try { |
|
117 |
s2.bind( new InetSocketAddress(s1.getLocalPort()) ); |
|
118 |
failed(); |
|
119 |
} catch (BindException e) { |
|
120 |
passed(); |
|
121 |
} |
|
122 |
s2.close(); |
|
123 |
||
124 |
s1.close(); |
|
125 |
} |
|
126 |
||
127 |
void DatagramSocketTests() throws Exception { |
|
128 |
DatagramSocket s1 = new DatagramSocket(null); |
|
129 |
||
130 |
test("DatagramSocket should be created with SO_REUSEADDR disabled"); |
|
131 |
check(!s1.getReuseAddress()); |
|
132 |
||
133 |
test("DatagramSocket.setReuseAddress(true)"); |
|
134 |
s1.setReuseAddress(true); |
|
135 |
check(s1.getReuseAddress()); |
|
136 |
||
137 |
test("DatagramSocket.setReuseAddress(false)"); |
|
138 |
s1.setReuseAddress(false); |
|
139 |
check(!s1.getReuseAddress() ); |
|
140 |
||
141 |
/* bind to any port */ |
|
142 |
s1.bind( new InetSocketAddress(0) ); |
|
143 |
||
144 |
test("Binding datagram socket to port already in use should throw " + |
|
145 |
"a BindException"); |
|
146 |
DatagramSocket s2 = new DatagramSocket(null); |
|
147 |
try { |
|
148 |
s2.bind( new InetSocketAddress(s1.getLocalPort()) ); |
|
149 |
failed(); |
|
150 |
} catch (BindException e) { |
|
151 |
passed(); |
|
152 |
} |
|
153 |
s2.close(); |
|
154 |
s1.close(); |
|
155 |
||
156 |
// bind with SO_REUSEADDR enabled |
|
157 |
||
158 |
s1 = new DatagramSocket(null); |
|
159 |
s1.setReuseAddress(true); |
|
160 |
s1.bind( new InetSocketAddress(0) ); |
|
161 |
||
162 |
test("Bind 2 datagram sockets to the same port - second " + |
|
163 |
"bind doesn't have SO_REUSEADDR enabled"); |
|
164 |
s2 = new DatagramSocket(null); |
|
165 |
try { |
|
166 |
s2.bind( new InetSocketAddress(s1.getLocalPort()) ); |
|
167 |
failed(); |
|
168 |
} catch (BindException e) { |
|
169 |
passed(); |
|
170 |
} |
|
171 |
s2.close(); |
|
172 |
||
173 |
test("Bind 2 datagram sockets to the same port - both have " + |
|
174 |
"SO_REUSEADDR enabled"); |
|
175 |
s2 = new DatagramSocket(null); |
|
176 |
s2.setReuseAddress(true); |
|
177 |
try { |
|
178 |
s2.bind( new InetSocketAddress(s1.getLocalPort()) ); |
|
179 |
passed(); |
|
180 |
} catch (BindException e) { |
|
13043
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
5506
diff
changeset
|
181 |
if (System.getProperty("sun.net.useExclusiveBind") != null) { |
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
5506
diff
changeset
|
182 |
// exclusive bind enabled - expected result |
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
5506
diff
changeset
|
183 |
passed(); |
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
5506
diff
changeset
|
184 |
} else { |
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
5506
diff
changeset
|
185 |
failed(); |
10fa4f21ea0f
7123896: Unexpected behavior due to Solaris using separate IPv4 and IPv6 port spaces
coffeys
parents:
5506
diff
changeset
|
186 |
} |
2 | 187 |
} |
188 |
s2.close(); |
|
189 |
||
190 |
s1.close(); |
|
191 |
||
192 |
} |
|
193 |
||
194 |
void MulticastSocketTests() throws Exception { |
|
195 |
test("Check SO_REUSEADDR is enabled in MulticastSocket()"); |
|
196 |
MulticastSocket s1 = new MulticastSocket(); |
|
197 |
check(s1.getReuseAddress()); |
|
198 |
s1.close(); |
|
199 |
||
200 |
test("Check that SO_REUSEADDR is not disabled by " + |
|
201 |
"MulticastSocket.bind()"); |
|
202 |
||
203 |
s1 = new MulticastSocket(null); |
|
204 |
||
205 |
// bind to specific address |
|
206 |
InetSocketAddress isa = new InetSocketAddress( |
|
207 |
InetAddress.getLocalHost(), 0); |
|
208 |
s1.bind(isa); |
|
209 |
check(s1.getReuseAddress()); |
|
210 |
s1.close(); |
|
211 |
} |
|
212 |
||
213 |
Basic() throws Exception { |
|
214 |
||
215 |
SocketTests(); |
|
216 |
ServerSocketTests(); |
|
217 |
DatagramSocketTests(); |
|
218 |
MulticastSocketTests(); |
|
219 |
||
220 |
System.out.println("***************************************"); |
|
221 |
System.out.println(testCount + " test(s) executed, " + |
|
222 |
failures + " failure(s)."); |
|
223 |
if (failures > 0) { |
|
224 |
throw new Exception(failures + " test(s) failed"); |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
public static void main(String args[]) throws Exception { |
|
229 |
new Basic(); |
|
230 |
} |
|
231 |
||
232 |
} |