jdk/test/javax/management/Introspector/AnnotationSecurityTest.java
author coleenp
Wed, 17 Jun 2015 21:44:48 +0000
changeset 31362 8957ccbb5821
parent 30376 2ccf2cf7ea48
child 33315 d2a313368ccb
permissions -rw-r--r--
8098821: Crash in system dictionary initialization with shared strings Summary: map string regions after the compressed class base is known Reviewed-by: iklam, dcubed Contributed-by: coleen.phillimore@oracle.com, mikhailo.seledtsov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2006, 2015, 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
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    30
 * @modules java.desktop
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    31
 *          java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run clean 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 build AnnotationSecurityTest Described UnDescribed DescribedMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *            UnDescribedMBean SqeDescriptorKey DescribedMX DescribedMXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @run main/othervm  AnnotationSecurityTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
// -Djava.security.debug=access,domain,policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.lang.reflect.AnnotatedElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.lang.reflect.UndeclaredThrowableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.JMException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Sun Microsystems, 2005 - All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class AnnotationSecurityTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** Creates a new instance of AnnotationSecurityTest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public AnnotationSecurityTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static void main(String[] argv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        AnnotationSecurityTest test = new AnnotationSecurityTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            final String testSrc = System.getProperty("test.src");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            final String codeBase = System.getProperty("test.classes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            final String policy = testSrc + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    "AnnotationSecurityTest.policy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            final File pf = new File(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (!pf.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                throw new IOException("policy file not found: " + policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (!pf.canRead())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                throw new IOException("policy file not readable: " + policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.out.println("Policy="+policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            System.setProperty("java.security.policy",policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.setSecurityManager(new SecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            // We check that 6370080 is fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                final Method m1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        DescribedMBean.class.getMethod("getStringProp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                final Method m2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        DescribedMBean.class.getMethod("setStringProp",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                            String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                m1.getAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                m2.getAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                System.err.println("ERROR: 6370080 not fixed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                throw new IllegalStateException("ERROR: 6370080 not fixed.",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // Do the test: we should be able to register these 3 MBeans....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // We now test that the behaviour described in 6366543 does no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // longer appears now that 6370080 is fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            final ObjectName name1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    new ObjectName("defaultDomain:class=UnDescribed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            UnDescribed unDescribedMBean = new UnDescribed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            System.out.println("\nWe register an MBean where DescriptorKey is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    "not used at all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            mbs.registerMBean(unDescribedMBean, name1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println("\n\tOK - The MBean "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    + name1 + " is registered = " + mbs.isRegistered(name1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            final ObjectName name2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    new ObjectName("defaultDomain:class=Described");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            final Described describedMBean = new Described();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.out.println("\nWe register an MBean with exactly the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    "same management"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    + " interface as above and where DescriptorKey is used.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            mbs.registerMBean(describedMBean, name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            System.out.println("\n\tOK - The MBean "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    + name2 + " is registered = " + mbs.isRegistered(name2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            final ObjectName name3 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    new ObjectName("defaultDomain:class=DescribedMX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            final DescribedMX describedMXBean = new DescribedMX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.out.println("\nWe register an MXBean with exactly the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    "same management"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    + " interface as above and where DescriptorKey is used.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            mbs.registerMBean(describedMXBean, name3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.out.println("\n\tOK - The MXBean "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    + name3 + " is registered = " + mbs.isRegistered(name3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.out.println("\nAll three MBeans correctly registered...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // We check that we don't have getAttribute() permission - as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // it's been voluntarily omitted from our policy file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // If we don't get the Security Exception there is probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // a security configuration pb...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                // We don't have getAttribute() permission, so this must fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                System.err.println("Trying getStringProp() - should fail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                mbs.getAttribute(name1,"StringProp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                System.err.println("ERROR: didn't get expected SecurityException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        +"\n\t check security configuration & policy file: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                throw new RuntimeException("getStringProp() did not get a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        "SecurityException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                // OK!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                System.err.println("getStringProp() - failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         } catch (Exception t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (t instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                throw (RuntimeException)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            else throw new RuntimeException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}