0N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test AnnotationSecurityTest.java
0N/A * @bug 6366543 6370080
0N/A * @summary Test that having a security manager doesn't trigger a
0N/A * NotCompliantMBeanException
0N/A * @author Daniel Fuchs, Yves Joan
0N/A * @run clean AnnotationSecurityTest Described UnDescribed DescribedMBean
0N/A * UnDescribedMBean SqeDescriptorKey DescribedMX DescribedMXBean
0N/A * @run build AnnotationSecurityTest Described UnDescribed DescribedMBean
0N/A * UnDescribedMBean SqeDescriptorKey DescribedMX DescribedMXBean
0N/A * @run main/othervm AnnotationSecurityTest
0N/A */
0N/A// -Djava.security.debug=access,domain,policy
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/A
0N/Aimport java.lang.annotation.Annotation;
0N/Aimport java.lang.management.ManagementFactory;
0N/Aimport java.lang.reflect.AnnotatedElement;
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.lang.reflect.UndeclaredThrowableException;
0N/A
0N/Aimport javax.management.JMException;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.ObjectName;
0N/A/**
0N/A *
0N/A * @author Sun Microsystems, 2005 - All rights reserved.
0N/A */
0N/A
0N/Apublic class AnnotationSecurityTest {
0N/A
0N/A /** Creates a new instance of AnnotationSecurityTest */
0N/A public AnnotationSecurityTest() {
0N/A }
0N/A
0N/A public static void main(String[] argv) {
0N/A AnnotationSecurityTest test = new AnnotationSecurityTest();
0N/A test.run();
0N/A }
0N/A
0N/A
0N/A public void run() {
0N/A try {
0N/A final String testSrc = System.getProperty("test.src");
0N/A final String codeBase = System.getProperty("test.classes");
0N/A final String policy = testSrc + File.separator +
0N/A "AnnotationSecurityTest.policy";
0N/A final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
0N/A final File pf = new File(policy);
0N/A if (!pf.exists())
0N/A throw new IOException("policy file not found: " + policy);
0N/A if (!pf.canRead())
0N/A throw new IOException("policy file not readable: " + policy);
0N/A
0N/A System.out.println("Policy="+policy);
0N/A System.setProperty("java.security.policy",policy);
0N/A System.setSecurityManager(new SecurityManager());
0N/A
0N/A // We check that 6370080 is fixed.
0N/A //
0N/A try {
0N/A final Method m1 =
0N/A DescribedMBean.class.getMethod("getStringProp");
0N/A final Method m2 =
0N/A DescribedMBean.class.getMethod("setStringProp",
0N/A String.class);
0N/A m1.getAnnotations();
0N/A m2.getAnnotations();
0N/A } catch (SecurityException x) {
0N/A System.err.println("ERROR: 6370080 not fixed.");
0N/A throw new IllegalStateException("ERROR: 6370080 not fixed.",x);
0N/A }
0N/A
0N/A // Do the test: we should be able to register these 3 MBeans....
0N/A // We now test that the behaviour described in 6366543 does no
0N/A // longer appears now that 6370080 is fixed.
0N/A //
0N/A
0N/A final ObjectName name1 =
0N/A new ObjectName("defaultDomain:class=UnDescribed");
0N/A UnDescribed unDescribedMBean = new UnDescribed();
0N/A System.out.println("\nWe register an MBean where DescriptorKey is " +
0N/A "not used at all");
0N/A mbs.registerMBean(unDescribedMBean, name1);
0N/A System.out.println("\n\tOK - The MBean "
0N/A + name1 + " is registered = " + mbs.isRegistered(name1));
0N/A
0N/A final ObjectName name2 =
0N/A new ObjectName("defaultDomain:class=Described");
0N/A final Described describedMBean = new Described();
0N/A
0N/A System.out.println("\nWe register an MBean with exactly the " +
0N/A "same management"
0N/A + " interface as above and where DescriptorKey is used.");
0N/A mbs.registerMBean(describedMBean, name2);
0N/A System.out.println("\n\tOK - The MBean "
0N/A + name2 + " is registered = " + mbs.isRegistered(name2));
0N/A
0N/A final ObjectName name3 =
0N/A new ObjectName("defaultDomain:class=DescribedMX");
0N/A final DescribedMX describedMXBean = new DescribedMX();
0N/A
0N/A System.out.println("\nWe register an MXBean with exactly the " +
0N/A "same management"
0N/A + " interface as above and where DescriptorKey is used.");
0N/A mbs.registerMBean(describedMXBean, name3);
0N/A System.out.println("\n\tOK - The MXBean "
0N/A + name3 + " is registered = " + mbs.isRegistered(name3));
0N/A
0N/A System.out.println("\nAll three MBeans correctly registered...");
0N/A
0N/A
0N/A // We check that we don't have getAttribute() permission - as
0N/A // it's been voluntarily omitted from our policy file.
0N/A // If we don't get the Security Exception there is probably
0N/A // a security configuration pb...
0N/A //
0N/A try {
0N/A // We don't have getAttribute() permission, so this must fail.
0N/A System.err.println("Trying getStringProp() - should fail");
0N/A mbs.getAttribute(name1,"StringProp");
0N/A System.err.println("ERROR: didn't get expected SecurityException"
0N/A +"\n\t check security configuration & policy file: "+
0N/A policy);
0N/A throw new RuntimeException("getStringProp() did not get a " +
0N/A "SecurityException!");
0N/A } catch (SecurityException x) {
0N/A // OK!
0N/A System.err.println("getStringProp() - failed");
0N/A }
0N/A
0N/A } catch (Exception t) {
0N/A t.printStackTrace();
0N/A if (t instanceof RuntimeException)
0N/A throw (RuntimeException)t;
0N/A else throw new RuntimeException(t);
0N/A }
0N/A }
0N/A
0N/A}