Path: blob/master/test/jdk/javax/management/Introspector/AnnotationSecurityTest.java
41149 views
/*1* Copyright (c) 2006, 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*/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*30* @modules java.desktop31* java.management32*33* @run clean AnnotationSecurityTest Described UnDescribed DescribedMBean34* UnDescribedMBean SqeDescriptorKey DescribedMX DescribedMXBean35* @run build AnnotationSecurityTest Described UnDescribed DescribedMBean36* UnDescribedMBean SqeDescriptorKey DescribedMX DescribedMXBean37* @run main/othervm -Djava.security.manager=allow AnnotationSecurityTest38*/39// -Djava.security.debug=access,domain,policy4041import java.io.File;42import java.io.IOException;4344import java.lang.management.ManagementFactory;45import java.lang.reflect.Method;46import javax.management.MBeanServer;47import javax.management.ObjectName;48/**49*50* @author Sun Microsystems, 2005 - All rights reserved.51*/5253public class AnnotationSecurityTest {5455/** Creates a new instance of AnnotationSecurityTest */56public AnnotationSecurityTest() {57}5859public static void main(String[] argv) {60AnnotationSecurityTest test = new AnnotationSecurityTest();61test.run();62}636465public void run() {66try {67final String testSrc = System.getProperty("test.src");68final String codeBase = System.getProperty("test.classes");69final String policy = testSrc + File.separator +70"AnnotationSecurityTest.policy";71final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();72final File pf = new File(policy);73if (!pf.exists())74throw new IOException("policy file not found: " + policy);75if (!pf.canRead())76throw new IOException("policy file not readable: " + policy);7778System.out.println("Policy="+policy);79System.setProperty("java.security.policy",policy);80System.setSecurityManager(new SecurityManager());8182// We check that 6370080 is fixed.83//84try {85final Method m1 =86DescribedMBean.class.getMethod("getStringProp");87final Method m2 =88DescribedMBean.class.getMethod("setStringProp",89String.class);90m1.getAnnotations();91m2.getAnnotations();92} catch (SecurityException x) {93System.err.println("ERROR: 6370080 not fixed.");94throw new IllegalStateException("ERROR: 6370080 not fixed.",x);95}9697// Do the test: we should be able to register these 3 MBeans....98// We now test that the behaviour described in 6366543 does no99// longer appears now that 6370080 is fixed.100//101102final ObjectName name1 =103new ObjectName("defaultDomain:class=UnDescribed");104UnDescribed unDescribedMBean = new UnDescribed();105System.out.println("\nWe register an MBean where DescriptorKey is " +106"not used at all");107mbs.registerMBean(unDescribedMBean, name1);108System.out.println("\n\tOK - The MBean "109+ name1 + " is registered = " + mbs.isRegistered(name1));110111final ObjectName name2 =112new ObjectName("defaultDomain:class=Described");113final Described describedMBean = new Described();114115System.out.println("\nWe register an MBean with exactly the " +116"same management"117+ " interface as above and where DescriptorKey is used.");118mbs.registerMBean(describedMBean, name2);119System.out.println("\n\tOK - The MBean "120+ name2 + " is registered = " + mbs.isRegistered(name2));121122final ObjectName name3 =123new ObjectName("defaultDomain:class=DescribedMX");124final DescribedMX describedMXBean = new DescribedMX();125126System.out.println("\nWe register an MXBean with exactly the " +127"same management"128+ " interface as above and where DescriptorKey is used.");129mbs.registerMBean(describedMXBean, name3);130System.out.println("\n\tOK - The MXBean "131+ name3 + " is registered = " + mbs.isRegistered(name3));132133System.out.println("\nAll three MBeans correctly registered...");134135136// We check that we don't have getAttribute() permission - as137// it's been voluntarily omitted from our policy file.138// If we don't get the Security Exception there is probably139// a security configuration pb...140//141try {142// We don't have getAttribute() permission, so this must fail.143System.err.println("Trying getStringProp() - should fail");144mbs.getAttribute(name1,"StringProp");145System.err.println("ERROR: didn't get expected SecurityException"146+"\n\t check security configuration & policy file: "+147policy);148throw new RuntimeException("getStringProp() did not get a " +149"SecurityException!");150} catch (SecurityException x) {151// OK!152System.err.println("getStringProp() - failed");153}154155} catch (Exception t) {156t.printStackTrace();157if (t instanceof RuntimeException)158throw (RuntimeException)t;159else throw new RuntimeException(t);160}161}162163}164165166