Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/AnnotationSecurityTest.java
38839 views
/*1* Copyright (c) 2006, 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* @test AnnotationSecurityTest.java25* @bug 6366543 637008026* @summary Test that having a security manager doesn't trigger a27* NotCompliantMBeanException28* @author Daniel Fuchs, Yves Joan29* @run clean AnnotationSecurityTest Described UnDescribed DescribedMBean30* UnDescribedMBean SqeDescriptorKey DescribedMX DescribedMXBean31* @run build AnnotationSecurityTest Described UnDescribed DescribedMBean32* UnDescribedMBean SqeDescriptorKey DescribedMX DescribedMXBean33* @run main/othervm AnnotationSecurityTest34*/35// -Djava.security.debug=access,domain,policy3637import java.io.File;38import java.io.IOException;3940import java.lang.annotation.Annotation;41import java.lang.management.ManagementFactory;42import java.lang.reflect.AnnotatedElement;43import java.lang.reflect.Method;44import java.lang.reflect.UndeclaredThrowableException;4546import javax.management.JMException;47import javax.management.MBeanServer;48import javax.management.ObjectName;49/**50*51* @author Sun Microsystems, 2005 - All rights reserved.52*/5354public class AnnotationSecurityTest {5556/** Creates a new instance of AnnotationSecurityTest */57public AnnotationSecurityTest() {58}5960public static void main(String[] argv) {61AnnotationSecurityTest test = new AnnotationSecurityTest();62test.run();63}646566public void run() {67try {68final String testSrc = System.getProperty("test.src");69final String codeBase = System.getProperty("test.classes");70final String policy = testSrc + File.separator +71"AnnotationSecurityTest.policy";72final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();73final File pf = new File(policy);74if (!pf.exists())75throw new IOException("policy file not found: " + policy);76if (!pf.canRead())77throw new IOException("policy file not readable: " + policy);7879System.out.println("Policy="+policy);80System.setProperty("java.security.policy",policy);81System.setSecurityManager(new SecurityManager());8283// We check that 6370080 is fixed.84//85try {86final Method m1 =87DescribedMBean.class.getMethod("getStringProp");88final Method m2 =89DescribedMBean.class.getMethod("setStringProp",90String.class);91m1.getAnnotations();92m2.getAnnotations();93} catch (SecurityException x) {94System.err.println("ERROR: 6370080 not fixed.");95throw new IllegalStateException("ERROR: 6370080 not fixed.",x);96}9798// Do the test: we should be able to register these 3 MBeans....99// We now test that the behaviour described in 6366543 does no100// longer appears now that 6370080 is fixed.101//102103final ObjectName name1 =104new ObjectName("defaultDomain:class=UnDescribed");105UnDescribed unDescribedMBean = new UnDescribed();106System.out.println("\nWe register an MBean where DescriptorKey is " +107"not used at all");108mbs.registerMBean(unDescribedMBean, name1);109System.out.println("\n\tOK - The MBean "110+ name1 + " is registered = " + mbs.isRegistered(name1));111112final ObjectName name2 =113new ObjectName("defaultDomain:class=Described");114final Described describedMBean = new Described();115116System.out.println("\nWe register an MBean with exactly the " +117"same management"118+ " interface as above and where DescriptorKey is used.");119mbs.registerMBean(describedMBean, name2);120System.out.println("\n\tOK - The MBean "121+ name2 + " is registered = " + mbs.isRegistered(name2));122123final ObjectName name3 =124new ObjectName("defaultDomain:class=DescribedMX");125final DescribedMX describedMXBean = new DescribedMX();126127System.out.println("\nWe register an MXBean with exactly the " +128"same management"129+ " interface as above and where DescriptorKey is used.");130mbs.registerMBean(describedMXBean, name3);131System.out.println("\n\tOK - The MXBean "132+ name3 + " is registered = " + mbs.isRegistered(name3));133134System.out.println("\nAll three MBeans correctly registered...");135136137// We check that we don't have getAttribute() permission - as138// it's been voluntarily omitted from our policy file.139// If we don't get the Security Exception there is probably140// a security configuration pb...141//142try {143// We don't have getAttribute() permission, so this must fail.144System.err.println("Trying getStringProp() - should fail");145mbs.getAttribute(name1,"StringProp");146System.err.println("ERROR: didn't get expected SecurityException"147+"\n\t check security configuration & policy file: "+148policy);149throw new RuntimeException("getStringProp() did not get a " +150"SecurityException!");151} catch (SecurityException x) {152// OK!153System.err.println("getStringProp() - failed");154}155156} catch (Exception t) {157t.printStackTrace();158if (t instanceof RuntimeException)159throw (RuntimeException)t;160else throw new RuntimeException(t);161}162}163164}165166167