27033
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. 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 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.
|
|
22 |
*/
|
|
23 |
|
|
24 |
|
|
25 |
/*
|
|
26 |
* @test
|
|
27 |
* @bug 8048052
|
|
28 |
* @summary Test a series of methods which requires "setFactory" runtime permission
|
|
29 |
* @run main SetFactoryPermission success
|
|
30 |
* @run main/othervm/policy=policy.fail SetFactoryPermission fail
|
|
31 |
* @run main/othervm/policy=policy.success SetFactoryPermission success
|
|
32 |
*/
|
|
33 |
import java.net.ServerSocket;
|
|
34 |
import java.net.Socket;
|
|
35 |
import java.net.URL;
|
|
36 |
import java.net.URLConnection;
|
|
37 |
import java.rmi.server.RMISocketFactory;
|
|
38 |
import java.security.AccessControlException;
|
|
39 |
|
|
40 |
public class SetFactoryPermission {
|
|
41 |
static boolean success = false;
|
|
42 |
|
|
43 |
interface Runner {
|
|
44 |
public void run() throws Exception;
|
|
45 |
}
|
|
46 |
|
|
47 |
public static void main (String[] args) throws Exception {
|
|
48 |
if (args.length > 0) {
|
|
49 |
success = System.getSecurityManager() == null || args[0].equals("success");
|
|
50 |
}
|
|
51 |
|
|
52 |
doTest(()->{
|
|
53 |
System.out.println("Verify URLConnection.setContentHandlerFactor()");
|
|
54 |
URLConnection.setContentHandlerFactory(null);
|
|
55 |
});
|
|
56 |
doTest(()->{
|
|
57 |
System.out.println("Verify URL.setURLStreamHandlerFactory()");
|
|
58 |
URL.setURLStreamHandlerFactory(null);
|
|
59 |
});
|
|
60 |
doTest(()->{
|
|
61 |
System.out.println("Verify ServerSocket.setSocketFactory()");
|
|
62 |
ServerSocket.setSocketFactory(null);
|
|
63 |
});
|
|
64 |
doTest(()->{
|
|
65 |
System.out.println("Verify Socket.setSocketImplFactory()");
|
|
66 |
Socket.setSocketImplFactory(null);
|
|
67 |
});
|
|
68 |
doTest(()->{
|
|
69 |
System.out.println("Verify RMISocketFactory.setSocketFactory()");
|
|
70 |
RMISocketFactory.setSocketFactory(null);
|
|
71 |
});
|
|
72 |
}
|
|
73 |
|
|
74 |
static void doTest(Runner func) throws Exception {
|
|
75 |
try {
|
|
76 |
func.run();
|
|
77 |
if (!success) {
|
|
78 |
throw new RuntimeException("AccessControlException is not thrown. Test failed");
|
|
79 |
}
|
|
80 |
} catch (SecurityException e) {
|
|
81 |
if (success) {
|
|
82 |
e.printStackTrace();
|
|
83 |
throw new RuntimeException("AccessControlException is thrown unexpectedly. Test failed");
|
|
84 |
}
|
|
85 |
}
|
|
86 |
}
|
|
87 |
}
|