2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2007, 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 4932837 6582235
|
|
27 |
* @summary Test SslRMI[Client|Server]SocketFactory equals() and hashCode().
|
|
28 |
* @author Daniel Fuchs
|
|
29 |
* @run clean SocketFactoryTest
|
|
30 |
* @run build SocketFactoryTest
|
|
31 |
* @run main SocketFactoryTest
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.io.PrintWriter;
|
|
35 |
import java.io.IOException;
|
|
36 |
import java.io.Serializable;
|
|
37 |
import java.io.ObjectInputStream;
|
|
38 |
import java.io.ObjectOutputStream;
|
|
39 |
import java.io.ByteArrayInputStream;
|
|
40 |
import java.io.ByteArrayOutputStream;
|
|
41 |
import java.util.Map;
|
|
42 |
import java.util.HashMap;
|
|
43 |
import javax.net.ssl.SSLContext;
|
|
44 |
import javax.rmi.ssl.SslRMIClientSocketFactory;
|
|
45 |
import javax.rmi.ssl.SslRMIServerSocketFactory;
|
|
46 |
|
|
47 |
public class SocketFactoryTest {
|
|
48 |
|
|
49 |
public static final String[] ciphersuite =
|
|
50 |
new String[] { "SSL_RSA_WITH_NULL_MD5" };
|
|
51 |
|
|
52 |
public static final String[] protocol =
|
|
53 |
new String[] { "TLSv1" };
|
|
54 |
|
|
55 |
public static class MySslRMIClientSocketFactory
|
|
56 |
extends SslRMIClientSocketFactory {
|
|
57 |
}
|
|
58 |
|
|
59 |
public static class MySslRMIServerSocketFactory
|
|
60 |
extends SslRMIServerSocketFactory {
|
|
61 |
public MySslRMIServerSocketFactory() {
|
|
62 |
super();
|
|
63 |
}
|
|
64 |
public MySslRMIServerSocketFactory(String[] ciphers,
|
|
65 |
String[] protocols,
|
|
66 |
boolean need) {
|
|
67 |
super(ciphers, protocols, need);
|
|
68 |
}
|
|
69 |
public MySslRMIServerSocketFactory(SSLContext context,
|
|
70 |
String[] ciphers,
|
|
71 |
String[] protocols,
|
|
72 |
boolean need) {
|
|
73 |
super(context, ciphers, protocols, need);
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
public static Object serializeAndClone(Object o) throws Exception {
|
|
78 |
System.out.println("Serializing object: " + o);
|
|
79 |
final ByteArrayOutputStream obytes =
|
|
80 |
new ByteArrayOutputStream();
|
|
81 |
final ObjectOutputStream ostr =
|
|
82 |
new ObjectOutputStream(obytes);
|
|
83 |
ostr.writeObject(o);
|
|
84 |
ostr.flush();
|
|
85 |
|
|
86 |
System.out.println("Deserializing object");
|
|
87 |
final ByteArrayInputStream ibytes =
|
|
88 |
new ByteArrayInputStream(obytes.toByteArray());
|
|
89 |
final ObjectInputStream istr =
|
|
90 |
new ObjectInputStream(ibytes);
|
|
91 |
return istr.readObject();
|
|
92 |
}
|
|
93 |
|
|
94 |
public static void testEquals(Object a, Object b, boolean expected) {
|
|
95 |
final boolean found = a.equals(b);
|
|
96 |
if (found != expected)
|
|
97 |
throw new RuntimeException("testEquals failed: objects are " +
|
|
98 |
((found)?"equals":"not equals"));
|
|
99 |
if (found && a.hashCode()!=b.hashCode())
|
|
100 |
throw new RuntimeException("testEquals failed: objects are " +
|
|
101 |
"equals but their hashcode differ");
|
|
102 |
}
|
|
103 |
|
|
104 |
public static void main(String[] args) {
|
|
105 |
try {
|
|
106 |
System.out.println("SocketFactoryTest START.");
|
|
107 |
|
|
108 |
final SSLContext context = SSLContext.getInstance("SSL");
|
|
109 |
context.init(null, null, null);
|
|
110 |
|
|
111 |
final SslRMIClientSocketFactory client1 =
|
|
112 |
new SslRMIClientSocketFactory();
|
|
113 |
final SslRMIClientSocketFactory client2 =
|
|
114 |
new SslRMIClientSocketFactory();
|
|
115 |
final SslRMIClientSocketFactory client11 =
|
|
116 |
(SslRMIClientSocketFactory) serializeAndClone(client1);
|
|
117 |
final SslRMIClientSocketFactory client21 =
|
|
118 |
(SslRMIClientSocketFactory) serializeAndClone(client2);
|
|
119 |
final SslRMIServerSocketFactory server1 =
|
|
120 |
new SslRMIServerSocketFactory();
|
|
121 |
final SslRMIServerSocketFactory server2 =
|
|
122 |
new SslRMIServerSocketFactory(null,
|
|
123 |
null,
|
|
124 |
false);
|
|
125 |
final SslRMIServerSocketFactory server3 =
|
|
126 |
new SslRMIServerSocketFactory(ciphersuite,
|
|
127 |
null,
|
|
128 |
false);
|
|
129 |
final SslRMIServerSocketFactory server4 =
|
|
130 |
new SslRMIServerSocketFactory(null,
|
|
131 |
protocol,
|
|
132 |
false);
|
|
133 |
final SslRMIServerSocketFactory server5 =
|
|
134 |
new SslRMIServerSocketFactory(null,
|
|
135 |
null,
|
|
136 |
true);
|
|
137 |
final SslRMIServerSocketFactory server6 =
|
|
138 |
new SslRMIServerSocketFactory(null,
|
|
139 |
null,
|
|
140 |
null,
|
|
141 |
false);
|
|
142 |
final SslRMIServerSocketFactory server7 =
|
|
143 |
new SslRMIServerSocketFactory(SSLContext.getDefault(),
|
|
144 |
null,
|
|
145 |
null,
|
|
146 |
false);
|
|
147 |
final SslRMIServerSocketFactory server8 =
|
|
148 |
new SslRMIServerSocketFactory(null,
|
|
149 |
null,
|
|
150 |
null,
|
|
151 |
true);
|
|
152 |
final SslRMIServerSocketFactory server9 =
|
|
153 |
new SslRMIServerSocketFactory(SSLContext.getDefault(),
|
|
154 |
null,
|
|
155 |
null,
|
|
156 |
true);
|
|
157 |
final SslRMIServerSocketFactory server10 =
|
|
158 |
new SslRMIServerSocketFactory(context,
|
|
159 |
null,
|
|
160 |
null,
|
|
161 |
true);
|
|
162 |
final MySslRMIClientSocketFactory subclient1 =
|
|
163 |
new MySslRMIClientSocketFactory();
|
|
164 |
final MySslRMIClientSocketFactory subclient2 =
|
|
165 |
new MySslRMIClientSocketFactory();
|
|
166 |
final MySslRMIServerSocketFactory subserver1 =
|
|
167 |
new MySslRMIServerSocketFactory();
|
|
168 |
final MySslRMIServerSocketFactory subserver2 =
|
|
169 |
new MySslRMIServerSocketFactory(null,
|
|
170 |
null,
|
|
171 |
false);
|
|
172 |
final MySslRMIServerSocketFactory subserver3 =
|
|
173 |
new MySslRMIServerSocketFactory(ciphersuite,
|
|
174 |
null,
|
|
175 |
false);
|
|
176 |
final MySslRMIServerSocketFactory subserver4 =
|
|
177 |
new MySslRMIServerSocketFactory(null,
|
|
178 |
protocol,
|
|
179 |
false);
|
|
180 |
final MySslRMIServerSocketFactory subserver5 =
|
|
181 |
new MySslRMIServerSocketFactory(null,
|
|
182 |
null,
|
|
183 |
true);
|
|
184 |
final MySslRMIServerSocketFactory subserver6 =
|
|
185 |
new MySslRMIServerSocketFactory(null,
|
|
186 |
ciphersuite,
|
|
187 |
null,
|
|
188 |
false);
|
|
189 |
final MySslRMIServerSocketFactory subserver7 =
|
|
190 |
new MySslRMIServerSocketFactory(SSLContext.getDefault(),
|
|
191 |
ciphersuite,
|
|
192 |
null,
|
|
193 |
false);
|
|
194 |
final MySslRMIServerSocketFactory subserver8 =
|
|
195 |
new MySslRMIServerSocketFactory(context,
|
|
196 |
null,
|
|
197 |
null,
|
|
198 |
true);
|
|
199 |
|
|
200 |
// clients
|
|
201 |
System.out.println("testEquals(client1,client1,true)");
|
|
202 |
testEquals(client1,client1,true);
|
|
203 |
System.out.println("testEquals(client1,client2,true)");
|
|
204 |
testEquals(client1,client2,true);
|
|
205 |
System.out.println("testEquals(client1,client11,true)");
|
|
206 |
testEquals(client1,client11,true);
|
|
207 |
System.out.println("testEquals(client1,client21,true)");
|
|
208 |
testEquals(client1,client21,true);
|
|
209 |
System.out.println("testEquals(client11,client21,true)");
|
|
210 |
testEquals(client11,client21,true);
|
|
211 |
System.out.println("testEquals(client1,null,false)");
|
|
212 |
testEquals(client1,null,false);
|
|
213 |
System.out.println("testEquals(client1,server1,false)");
|
|
214 |
testEquals(client1,server1,false);
|
|
215 |
System.out.println("testEquals(client21,server2,false)");
|
|
216 |
testEquals(client21,server2,false);
|
|
217 |
System.out.println("testEquals(client1,new Object(),false)");
|
|
218 |
testEquals(client1,new Object(),false);
|
|
219 |
|
|
220 |
// servers
|
|
221 |
System.out.println("testEquals(server1,server1,true)");
|
|
222 |
testEquals(server1,server1,true);
|
|
223 |
System.out.println("testEquals(server2,server2,true)");
|
|
224 |
testEquals(server2,server2,true);
|
|
225 |
System.out.println("testEquals(server3,server3,true)");
|
|
226 |
testEquals(server3,server3,true);
|
|
227 |
System.out.println("testEquals(server4,server4,true)");
|
|
228 |
testEquals(server4,server4,true);
|
|
229 |
System.out.println("testEquals(server5,server5,true)");
|
|
230 |
testEquals(server5,server5,true);
|
|
231 |
System.out.println("testEquals(server1,server2,true)");
|
|
232 |
testEquals(server1,server2,true);
|
|
233 |
System.out.println("testEquals(server1,server3,false)");
|
|
234 |
testEquals(server1,server3,false);
|
|
235 |
System.out.println("testEquals(server2,server3,false)");
|
|
236 |
testEquals(server2,server3,false);
|
|
237 |
System.out.println("testEquals(server3,server4,false)");
|
|
238 |
testEquals(server3,server4,false);
|
|
239 |
System.out.println("testEquals(server4,server5,false)");
|
|
240 |
testEquals(server4,server5,false);
|
|
241 |
System.out.println("testEquals(server6,server7,false)");
|
|
242 |
testEquals(server6,server7,false);
|
|
243 |
System.out.println("testEquals(server8,server9,false)");
|
|
244 |
testEquals(server8,server9,false);
|
|
245 |
System.out.println("testEquals(server9,server10,false)");
|
|
246 |
testEquals(server9,server10,false);
|
|
247 |
System.out.println("testEquals(server2,server6,true)");
|
|
248 |
testEquals(server2,server6,true);
|
|
249 |
System.out.println("testEquals(server2,server7,false)");
|
|
250 |
testEquals(server2,server7,false);
|
|
251 |
System.out.println("testEquals(server5,server8,true)");
|
|
252 |
testEquals(server5,server8,true);
|
|
253 |
System.out.println("testEquals(server5,server9,false)");
|
|
254 |
testEquals(server5,server9,false);
|
|
255 |
System.out.println("testEquals(server1,null,false)");
|
|
256 |
testEquals(server1,null,false);
|
|
257 |
System.out.println("testEquals(server2,null,false)");
|
|
258 |
testEquals(server2,null,false);
|
|
259 |
System.out.println("testEquals(server3,null,false)");
|
|
260 |
testEquals(server3,null,false);
|
|
261 |
System.out.println("testEquals(server1,client2,false)");
|
|
262 |
testEquals(server1,client2,false);
|
|
263 |
System.out.println("testEquals(server2,client11,false)");
|
|
264 |
testEquals(server2,client11,false);
|
|
265 |
System.out.println("testEquals(server1,new Object(),false)");
|
|
266 |
testEquals(server1,new Object(),false);
|
|
267 |
|
|
268 |
// client subclass
|
|
269 |
System.out.println("testEquals(subclient1,subclient1,true)");
|
|
270 |
testEquals(subclient1,subclient1,true);
|
|
271 |
System.out.println("testEquals(subclient1,subclient2,true)");
|
|
272 |
testEquals(subclient1,subclient2,true);
|
|
273 |
System.out.println("testEquals(subclient1,client1,false)");
|
|
274 |
testEquals(subclient1,client1,false);
|
|
275 |
System.out.println("testEquals(client1,subclient1,false)");
|
|
276 |
testEquals(client1,subclient1,false);
|
|
277 |
System.out.println("testEquals(subclient1,null,false)");
|
|
278 |
testEquals(subclient1,null,false);
|
|
279 |
System.out.println("testEquals(subclient1,server1,false)");
|
|
280 |
testEquals(subclient1,server1,false);
|
|
281 |
System.out.println("testEquals(server1,subclient1,false)");
|
|
282 |
testEquals(server1,subclient1,false);
|
|
283 |
System.out.println("testEquals(subclient2,subserver2,false)");
|
|
284 |
testEquals(subclient1,subserver2,false);
|
|
285 |
System.out.println("testEquals(subclient1,new Object(),false)");
|
|
286 |
testEquals(subclient1,new Object(),false);
|
|
287 |
|
|
288 |
// server subclass
|
|
289 |
System.out.println("testEquals(subserver1,subserver1,true)");
|
|
290 |
testEquals(subserver1,subserver1,true);
|
|
291 |
System.out.println("testEquals(subserver2,subserver2,true)");
|
|
292 |
testEquals(subserver2,subserver2,true);
|
|
293 |
System.out.println("testEquals(subserver3,subserver3,true)");
|
|
294 |
testEquals(subserver3,subserver3,true);
|
|
295 |
System.out.println("testEquals(subserver4,subserver4,true)");
|
|
296 |
testEquals(subserver4,subserver4,true);
|
|
297 |
System.out.println("testEquals(subserver5,subserver5,true)");
|
|
298 |
testEquals(subserver5,subserver5,true);
|
|
299 |
System.out.println("testEquals(subserver1,subserver2,true)");
|
|
300 |
testEquals(subserver1,subserver2,true);
|
|
301 |
System.out.println("testEquals(subserver1,subserver3,false)");
|
|
302 |
testEquals(subserver1,subserver3,false);
|
|
303 |
System.out.println("testEquals(subserver2,subserver3,false)");
|
|
304 |
testEquals(subserver2,subserver3,false);
|
|
305 |
System.out.println("testEquals(subserver3,subserver4,false)");
|
|
306 |
testEquals(subserver3,subserver4,false);
|
|
307 |
System.out.println("testEquals(subserver4,subserver5,false)");
|
|
308 |
testEquals(subserver4,subserver5,false);
|
|
309 |
System.out.println("testEquals(subserver3,subserver6,true)");
|
|
310 |
testEquals(subserver3,subserver6,true);
|
|
311 |
System.out.println("testEquals(subserver3,subserver7,false)");
|
|
312 |
testEquals(subserver3,subserver7,false);
|
|
313 |
System.out.println("testEquals(subserver1,server1,false)");
|
|
314 |
testEquals(subserver1,server1,false);
|
|
315 |
System.out.println("testEquals(server1,subserver1,false)");
|
|
316 |
testEquals(server1,subserver1,false);
|
|
317 |
System.out.println("testEquals(subserver2,server2,false)");
|
|
318 |
testEquals(subserver2,server2,false);
|
|
319 |
System.out.println("testEquals(server2,subserver2,false)");
|
|
320 |
testEquals(server2,subserver2,false);
|
|
321 |
System.out.println("testEquals(subserver3,server3,false)");
|
|
322 |
testEquals(subserver3,server3,false);
|
|
323 |
System.out.println("testEquals(server3,subserver3,false)");
|
|
324 |
testEquals(server3,subserver3,false);
|
|
325 |
System.out.println("testEquals(subserver4,server4,false)");
|
|
326 |
testEquals(subserver4,server4,false);
|
|
327 |
System.out.println("testEquals(server4,subserver4,false)");
|
|
328 |
testEquals(server4,subserver4,false);
|
|
329 |
System.out.println("testEquals(subserver5,server5,false)");
|
|
330 |
testEquals(subserver5,server5,false);
|
|
331 |
System.out.println("testEquals(server5,subserver5,false)");
|
|
332 |
testEquals(server5,subserver5,false);
|
|
333 |
System.out.println("testEquals(server10,subserver8,false)");
|
|
334 |
testEquals(server10,subserver8,false);
|
|
335 |
System.out.println("testEquals(subserver8,server10,false)");
|
|
336 |
testEquals(subserver8,server10,false);
|
|
337 |
System.out.println("testEquals(subserver1,null,false)");
|
|
338 |
testEquals(subserver1,null,false);
|
|
339 |
System.out.println("testEquals(subserver1,client2,false)");
|
|
340 |
testEquals(subserver1,client2,false);
|
|
341 |
System.out.println("testEquals(subserver1,subclient2,false)");
|
|
342 |
testEquals(subserver1,subclient2,false);
|
|
343 |
System.out.println("testEquals(client1,subserver2,false)");
|
|
344 |
testEquals(client1,subserver2,false);
|
|
345 |
System.out.println("testEquals(subserver1,new Object(),false)");
|
|
346 |
testEquals(subserver1,new Object(),false);
|
|
347 |
|
|
348 |
System.out.println("SocketFactoryTest PASSED.");
|
|
349 |
} catch (Exception x) {
|
|
350 |
System.out.println("SocketFactoryTest FAILED: " + x);
|
|
351 |
x.printStackTrace();
|
|
352 |
System.exit(1);
|
|
353 |
}
|
|
354 |
}
|
|
355 |
}
|