author | darcy |
Thu, 13 Aug 2015 21:46:05 -0700 | |
changeset 32147 | 5f6d9917598c |
parent 30820 | 0d4717a011d3 |
permissions | -rw-r--r-- |
30355
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
1 |
/* |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
4 |
* |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
8 |
* |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
13 |
* accompanied this code). |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
14 |
* |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
18 |
* |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
21 |
* questions. |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
22 |
*/ |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
23 |
|
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
24 |
import java.security.AccessControlException; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
25 |
import java.security.Permission; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
26 |
import java.security.Policy; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
27 |
import java.security.ProtectionDomain; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
28 |
import java.util.List; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
29 |
|
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
30 |
/* |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
31 |
* @test |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
32 |
* @bug 8042901 |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
33 |
* @summary Check permission for PlatformMBeanProvider Constructor |
30820 | 34 |
* @modules java.management/sun.management.spi |
30355
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
35 |
* @author Shanliang Jiang |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
36 |
*/ |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
37 |
public class PlatformMBeanProviderConstructorCheck { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
38 |
public static void main(String[] args) throws Exception { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
39 |
Policy origPolicy = Policy.getPolicy(); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
40 |
SecurityManager origSM = System.getSecurityManager(); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
41 |
try { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
42 |
System.out.println("---PlatformMBeanProviderConstructorCheck starting..."); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
43 |
|
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
44 |
Policy.setPolicy(new MyPolicy()); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
45 |
System.setSecurityManager(new SecurityManager()); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
46 |
|
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
47 |
System.out.println("---PlatformMBeanProviderConstructorCheck Testing without permission..."); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
48 |
try { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
49 |
new MyProvider(); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
50 |
throw new RuntimeException("Does not get expected AccessControlException!"); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
51 |
} catch (AccessControlException ace) { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
52 |
System.out.println("---PlatformMBeanProviderConstructorCheck got the expected exception: " |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
53 |
+ ace); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
54 |
} |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
55 |
|
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
56 |
System.out.println("---PlatformMBeanProviderConstructorCheck Testing with permission..."); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
57 |
MyPolicy.allowed = true; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
58 |
new MyProvider(); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
59 |
|
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
60 |
System.out.println("---PlatformMBeanProviderConstructorCheck PASSED!"); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
61 |
} finally { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
62 |
System.setSecurityManager(origSM); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
63 |
Policy.setPolicy(origPolicy); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
64 |
} |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
65 |
} |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
66 |
|
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
67 |
private static class MyPolicy extends Policy { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
68 |
private static String permName = "sun.management.spi.PlatformMBeanProvider.subclass"; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
69 |
private static boolean allowed = false; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
70 |
|
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
71 |
@Override |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
72 |
public boolean implies(ProtectionDomain domain, Permission permission) { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
73 |
if (permName.equals(permission.getName())) { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
74 |
System.out.println("---MyPolicy-implies checks permission for " |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
75 |
+permName+" and returns "+allowed); |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
76 |
|
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
77 |
return allowed; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
78 |
} else { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
79 |
return true; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
80 |
} |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
81 |
} |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
82 |
} |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
83 |
|
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
84 |
private static class MyProvider extends sun.management.spi.PlatformMBeanProvider { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
85 |
@Override |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
86 |
public List<PlatformComponent<?>> getPlatformComponentList() { |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
87 |
return null; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
88 |
} |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
89 |
} |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
diff
changeset
|
90 |
} |