|
1 /* |
|
2 * Copyright (c) 2015, 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 import java.security.AccessControlException; |
|
25 import java.security.Permission; |
|
26 import java.security.Policy; |
|
27 import java.security.ProtectionDomain; |
|
28 import java.util.List; |
|
29 |
|
30 /* |
|
31 * @test |
|
32 * @bug 8042901 |
|
33 * @summary Check permission for PlatformMBeanProvider Constructor |
|
34 * @author Shanliang Jiang |
|
35 */ |
|
36 public class PlatformMBeanProviderConstructorCheck { |
|
37 public static void main(String[] args) throws Exception { |
|
38 Policy origPolicy = Policy.getPolicy(); |
|
39 SecurityManager origSM = System.getSecurityManager(); |
|
40 try { |
|
41 System.out.println("---PlatformMBeanProviderConstructorCheck starting..."); |
|
42 |
|
43 Policy.setPolicy(new MyPolicy()); |
|
44 System.setSecurityManager(new SecurityManager()); |
|
45 |
|
46 System.out.println("---PlatformMBeanProviderConstructorCheck Testing without permission..."); |
|
47 try { |
|
48 new MyProvider(); |
|
49 throw new RuntimeException("Does not get expected AccessControlException!"); |
|
50 } catch (AccessControlException ace) { |
|
51 System.out.println("---PlatformMBeanProviderConstructorCheck got the expected exception: " |
|
52 + ace); |
|
53 } |
|
54 |
|
55 System.out.println("---PlatformMBeanProviderConstructorCheck Testing with permission..."); |
|
56 MyPolicy.allowed = true; |
|
57 new MyProvider(); |
|
58 |
|
59 System.out.println("---PlatformMBeanProviderConstructorCheck PASSED!"); |
|
60 } finally { |
|
61 System.setSecurityManager(origSM); |
|
62 Policy.setPolicy(origPolicy); |
|
63 } |
|
64 } |
|
65 |
|
66 private static class MyPolicy extends Policy { |
|
67 private static String permName = "sun.management.spi.PlatformMBeanProvider.subclass"; |
|
68 private static boolean allowed = false; |
|
69 |
|
70 @Override |
|
71 public boolean implies(ProtectionDomain domain, Permission permission) { |
|
72 if (permName.equals(permission.getName())) { |
|
73 System.out.println("---MyPolicy-implies checks permission for " |
|
74 +permName+" and returns "+allowed); |
|
75 |
|
76 return allowed; |
|
77 } else { |
|
78 return true; |
|
79 } |
|
80 } |
|
81 } |
|
82 |
|
83 private static class MyProvider extends sun.management.spi.PlatformMBeanProvider { |
|
84 @Override |
|
85 public List<PlatformComponent<?>> getPlatformComponentList() { |
|
86 return null; |
|
87 } |
|
88 } |
|
89 } |