jdk/test/javax/management/Introspector/AnnotationSecurityTest.java
author ohair
Tue, 12 Apr 2011 18:36:42 -0700
changeset 9064 63c8767f5915
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test AnnotationSecurityTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6366543 6370080
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that having a security manager doesn't trigger a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          NotCompliantMBeanException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Daniel Fuchs, Yves Joan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean AnnotationSecurityTest Described UnDescribed DescribedMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *            UnDescribedMBean SqeDescriptorKey DescribedMX DescribedMXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build AnnotationSecurityTest Described UnDescribed DescribedMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *            UnDescribedMBean SqeDescriptorKey DescribedMX DescribedMXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run main/othervm  AnnotationSecurityTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
// -Djava.security.debug=access,domain,policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.reflect.AnnotatedElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.lang.reflect.UndeclaredThrowableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.JMException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Sun Microsystems, 2005 - All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class AnnotationSecurityTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** Creates a new instance of AnnotationSecurityTest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public AnnotationSecurityTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static void main(String[] argv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        AnnotationSecurityTest test = new AnnotationSecurityTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            final String testSrc = System.getProperty("test.src");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            final String codeBase = System.getProperty("test.classes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            final String policy = testSrc + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    "AnnotationSecurityTest.policy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            final File pf = new File(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (!pf.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                throw new IOException("policy file not found: " + policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (!pf.canRead())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                throw new IOException("policy file not readable: " + policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.out.println("Policy="+policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.setProperty("java.security.policy",policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.setSecurityManager(new SecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            // We check that 6370080 is fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                final Method m1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        DescribedMBean.class.getMethod("getStringProp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                final Method m2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        DescribedMBean.class.getMethod("setStringProp",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                            String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                m1.getAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                m2.getAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                System.err.println("ERROR: 6370080 not fixed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                throw new IllegalStateException("ERROR: 6370080 not fixed.",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // Do the test: we should be able to register these 3 MBeans....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            // We now test that the behaviour described in 6366543 does no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // longer appears now that 6370080 is fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            final ObjectName name1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    new ObjectName("defaultDomain:class=UnDescribed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            UnDescribed unDescribedMBean = new UnDescribed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            System.out.println("\nWe register an MBean where DescriptorKey is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    "not used at all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            mbs.registerMBean(unDescribedMBean, name1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            System.out.println("\n\tOK - The MBean "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    + name1 + " is registered = " + mbs.isRegistered(name1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            final ObjectName name2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    new ObjectName("defaultDomain:class=Described");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            final Described describedMBean = new Described();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.out.println("\nWe register an MBean with exactly the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    "same management"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    + " interface as above and where DescriptorKey is used.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            mbs.registerMBean(describedMBean, name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.println("\n\tOK - The MBean "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    + name2 + " is registered = " + mbs.isRegistered(name2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            final ObjectName name3 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    new ObjectName("defaultDomain:class=DescribedMX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            final DescribedMX describedMXBean = new DescribedMX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            System.out.println("\nWe register an MXBean with exactly the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    "same management"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    + " interface as above and where DescriptorKey is used.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            mbs.registerMBean(describedMXBean, name3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            System.out.println("\n\tOK - The MXBean "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    + name3 + " is registered = " + mbs.isRegistered(name3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            System.out.println("\nAll three MBeans correctly registered...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // We check that we don't have getAttribute() permission - as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // it's been voluntarily omitted from our policy file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // If we don't get the Security Exception there is probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // a security configuration pb...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // We don't have getAttribute() permission, so this must fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                System.err.println("Trying getStringProp() - should fail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                mbs.getAttribute(name1,"StringProp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                System.err.println("ERROR: didn't get expected SecurityException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        +"\n\t check security configuration & policy file: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new RuntimeException("getStringProp() did not get a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        "SecurityException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                // OK!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                System.err.println("getStringProp() - failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         } catch (Exception t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (t instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                throw (RuntimeException)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            else throw new RuntimeException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}