Path: blob/master/test/jdk/sun/management/PlatformMBeanProviderConstructorCheck.java
41144 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.security.AccessControlException;24import java.security.Permission;25import java.security.Policy;26import java.security.ProtectionDomain;27import java.util.List;2829/*30* @test31* @bug 804290132* @summary Check permission for PlatformMBeanProvider Constructor33* @modules java.management/sun.management.spi34* @author Shanliang Jiang35* @run main/othervm -Djava.security.manager=allow PlatformMBeanProviderConstructorCheck36*/37public class PlatformMBeanProviderConstructorCheck {38public static void main(String[] args) throws Exception {39Policy origPolicy = Policy.getPolicy();40SecurityManager origSM = System.getSecurityManager();41try {42System.out.println("---PlatformMBeanProviderConstructorCheck starting...");4344Policy.setPolicy(new MyPolicy());45System.setSecurityManager(new SecurityManager());4647System.out.println("---PlatformMBeanProviderConstructorCheck Testing without permission...");48try {49new MyProvider();50throw new RuntimeException("Does not get expected AccessControlException!");51} catch (AccessControlException ace) {52System.out.println("---PlatformMBeanProviderConstructorCheck got the expected exception: "53+ ace);54}5556System.out.println("---PlatformMBeanProviderConstructorCheck Testing with permission...");57MyPolicy.allowed = true;58new MyProvider();5960System.out.println("---PlatformMBeanProviderConstructorCheck PASSED!");61} finally {62System.setSecurityManager(origSM);63Policy.setPolicy(origPolicy);64}65}6667private static class MyPolicy extends Policy {68private static String permName = "sun.management.spi.PlatformMBeanProvider.subclass";69private static boolean allowed = false;7071@Override72public boolean implies(ProtectionDomain domain, Permission permission) {73if (permName.equals(permission.getName())) {74System.out.println("---MyPolicy-implies checks permission for "75+permName+" and returns "+allowed);7677return allowed;78} else {79return true;80}81}82}8384private static class MyProvider extends sun.management.spi.PlatformMBeanProvider {85@Override86public List<PlatformComponent<?>> getPlatformComponentList() {87return null;88}89}90}919293