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