Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/management/DiagnosticCommandMBean/DcmdMBeanPermissionsTest.java
38855 views
/*1* Copyright (c) 2013, 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*/2223/*24* @test25* @bug 715025626* @summary Permissions Tests for the DiagnosticCommandMBean27* @author Frederic Parain28*29* @run main/othervm DcmdMBeanPermissionsTest30*/3132import java.lang.management.ManagementFactory;33import java.lang.reflect.Constructor;34import java.lang.reflect.InvocationTargetException;35import java.lang.reflect.ReflectPermission;36import java.security.Permission;37import java.util.HashSet;38import java.util.Iterator;39import javax.management.Descriptor;40import javax.management.InstanceNotFoundException;41import javax.management.IntrospectionException;42import javax.management.MBeanException;43import javax.management.MBeanInfo;44import javax.management.MBeanOperationInfo;45import javax.management.MBeanPermission;46import javax.management.MBeanServer;47import javax.management.MalformedObjectNameException;48import javax.management.ObjectName;49import javax.management.ReflectionException;50import javax.management.RuntimeMBeanException;5152/**53*54* @author fparain55*/56public class DcmdMBeanPermissionsTest {5758private static String HOTSPOT_DIAGNOSTIC_MXBEAN_NAME =59"com.sun.management:type=DiagnosticCommand";6061static public class CustomSecurityManager extends SecurityManager {6263private HashSet<Permission> grantedPermissions;6465public CustomSecurityManager() {66grantedPermissions = new HashSet<Permission>();67}6869public final void grantPermission(final Permission perm) {70grantedPermissions.add(perm);71}7273public final void denyPermission(final Permission perm) {74Iterator<Permission> it = grantedPermissions.iterator();75while (it.hasNext()) {76Permission p = it.next();77if (p.equals(perm)) {78it.remove();79}80}81}8283public final void checkPermission(final Permission perm) {84for (Permission p : grantedPermissions) {85if (p.implies(perm)) {86return;87}88}89throw new SecurityException(perm.toString());90}91};9293static Permission createPermission(String classname, String name,94String action) {95Permission permission = null;96try {97Class c = Class.forName(classname);98if (action == null) {99try {100Constructor constructor = c.getConstructor(String.class);101permission = (Permission) constructor.newInstance(name);102103} catch (InstantiationException | IllegalAccessException104| IllegalArgumentException | InvocationTargetException105| NoSuchMethodException | SecurityException ex) {106ex.printStackTrace();107throw new RuntimeException("TEST FAILED");108}109}110if (permission == null) {111try {112Constructor constructor = c.getConstructor(String.class,113String.class);114permission = (Permission) constructor.newInstance(115name,116action);117} catch (InstantiationException | IllegalAccessException118| IllegalArgumentException | InvocationTargetException119| NoSuchMethodException | SecurityException ex) {120ex.printStackTrace();121throw new RuntimeException("TEST FAILED");122}123}124} catch (ClassNotFoundException ex) {125ex.printStackTrace();126throw new RuntimeException("TEST FAILED");127}128if (permission == null) {129throw new RuntimeException("TEST FAILED");130}131return permission;132}133134// return true if invokation triggered a SecurityException135static boolean invokeOperation(MBeanServer mbs, ObjectName on,136MBeanOperationInfo opInfo) {137try {138if (opInfo.getSignature().length == 0) {139mbs.invoke(on, opInfo.getName(),140new Object[0], new String[0]);141} else {142mbs.invoke(on, opInfo.getName(),143new Object[1], new String[]{ String[].class.getName()});144}145} catch (SecurityException ex) {146ex.printStackTrace();147return true;148} catch (RuntimeMBeanException ex) {149if (ex.getCause() instanceof SecurityException) {150//ex.printStackTrace();151return true;152}153} catch (MBeanException | InstanceNotFoundException154| ReflectionException ex) {155throw new RuntimeException("TEST FAILED");156}157return false;158}159160static void testOperation(MBeanServer mbs, CustomSecurityManager sm,161ObjectName on, MBeanOperationInfo opInfo) {162System.out.println("Testing " + opInfo.getName());163Descriptor desc = opInfo.getDescriptor();164if (desc.getFieldValue("dcmd.permissionClass") == null) {165// No special permission required, execution should not trigger166// any security exception167if (invokeOperation(mbs, on, opInfo)) {168throw new RuntimeException("TEST FAILED");169}170} else {171// Building the required permission172Permission reqPerm = createPermission(173(String)desc.getFieldValue("dcmd.permissionClass"),174(String)desc.getFieldValue("dcmd.permissionName"),175(String)desc.getFieldValue("dcmd.permissionAction"));176// Paranoid mode: check that the SecurityManager has not already177// been granted the permission178sm.denyPermission(reqPerm);179// A special permission is required for this operation,180// invoking it without the permission granted must trigger181// a security exception182if(!invokeOperation(mbs, on, opInfo)) {183throw new RuntimeException("TEST FAILED");184}185// grant the permission and re-try invoking the operation186sm.grantPermission(reqPerm);187if(invokeOperation(mbs, on, opInfo)) {188throw new RuntimeException("TEST FAILED");189}190// Clean up191sm.denyPermission(reqPerm);192}193}194195public static void main(final String[] args) {196final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();197ObjectName on = null;198try {199on = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);200} catch (MalformedObjectNameException ex) {201ex.printStackTrace();202throw new RuntimeException("TEST FAILED");203}204MBeanInfo info = null;205try {206info = mbs.getMBeanInfo(on);207} catch (InstanceNotFoundException | IntrospectionException208| ReflectionException ex) {209ex.printStackTrace();210throw new RuntimeException("TEST FAILED");211}212CustomSecurityManager sm = new CustomSecurityManager();213System.setSecurityManager(sm);214// Set of permission required to run the test cleanly215// Some permissions are required by the MBeanServer and other216// platform services (RuntimePermission("createClassLoader"),217// ReflectPermission("suppressAccessChecks"),218// java.util.logging.LoggingPermission("control"),219// RuntimePermission("exitVM.97")).220// Other permissions are required by commands being invoked221// in the test (for instance, RuntimePermission("modifyThreadGroup")222// and RuntimePermission("modifyThread") are checked when223// runFinalization() is invoked by the gcRunFinalization command.224sm.grantPermission(new RuntimePermission("createClassLoader"));225sm.grantPermission(new ReflectPermission("suppressAccessChecks"));226sm.grantPermission(new java.util.logging.LoggingPermission("control", ""));227sm.grantPermission(new java.lang.RuntimePermission("exitVM.97"));228sm.grantPermission(new java.lang.RuntimePermission("modifyThreadGroup"));229sm.grantPermission(new java.lang.RuntimePermission("modifyThread"));230for(MBeanOperationInfo opInfo : info.getOperations()) {231Permission opPermission = new MBeanPermission(info.getClassName(),232opInfo.getName(),233on,234"invoke");235sm.grantPermission(opPermission);236testOperation(mbs, sm, on, opInfo);237sm.denyPermission(opPermission);238}239System.out.println("TEST PASSED");240}241}242243244