test/jdk/javax/management/Introspector/AnnotationSecurityTest.java
author kbarrett
Wed, 13 Nov 2019 18:00:30 -0500
changeset 59067 f080b08daace
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232588: G1 concurrent System.gc can return early or late 8233279: G1: GCLocker GC with +GCLockerInvokesConcurrent spins while cycle in progress Summary: Refactor G1CH::try_collect and fix bugs with concurrent collections. Reviewed-by: tschatzl, sjohanss
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
44423
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 33499
diff changeset
    30
 *
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 33499
diff changeset
    31
 * @modules java.desktop
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 33499
diff changeset
    32
 *          java.management
306c020eb154 8176176: fix @modules in jdk_svc tests
iignatyev
parents: 33499
diff changeset
    33
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run clean 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 build AnnotationSecurityTest Described UnDescribed DescribedMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *            UnDescribedMBean SqeDescriptorKey DescribedMX DescribedMXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @run main/othervm  AnnotationSecurityTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
// -Djava.security.debug=access,domain,policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Sun Microsystems, 2005 - All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class AnnotationSecurityTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** Creates a new instance of AnnotationSecurityTest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public AnnotationSecurityTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String[] argv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        AnnotationSecurityTest test = new AnnotationSecurityTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            final String testSrc = System.getProperty("test.src");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            final String codeBase = System.getProperty("test.classes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            final String policy = testSrc + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    "AnnotationSecurityTest.policy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            final File pf = new File(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (!pf.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                throw new IOException("policy file not found: " + policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (!pf.canRead())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                throw new IOException("policy file not readable: " + policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.out.println("Policy="+policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.setProperty("java.security.policy",policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.setSecurityManager(new SecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // We check that 6370080 is fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                final Method m1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        DescribedMBean.class.getMethod("getStringProp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                final Method m2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        DescribedMBean.class.getMethod("setStringProp",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                            String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                m1.getAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                m2.getAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                System.err.println("ERROR: 6370080 not fixed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                throw new IllegalStateException("ERROR: 6370080 not fixed.",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // Do the test: we should be able to register these 3 MBeans....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // We now test that the behaviour described in 6366543 does no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            // longer appears now that 6370080 is fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            final ObjectName name1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    new ObjectName("defaultDomain:class=UnDescribed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            UnDescribed unDescribedMBean = new UnDescribed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.out.println("\nWe register an MBean where DescriptorKey is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    "not used at all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            mbs.registerMBean(unDescribedMBean, name1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            System.out.println("\n\tOK - The MBean "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    + name1 + " is registered = " + mbs.isRegistered(name1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            final ObjectName name2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    new ObjectName("defaultDomain:class=Described");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            final Described describedMBean = new Described();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println("\nWe register an MBean with exactly the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    "same management"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    + " interface as above and where DescriptorKey is used.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            mbs.registerMBean(describedMBean, name2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println("\n\tOK - The MBean "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    + name2 + " is registered = " + mbs.isRegistered(name2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            final ObjectName name3 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    new ObjectName("defaultDomain:class=DescribedMX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            final DescribedMX describedMXBean = new DescribedMX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            System.out.println("\nWe register an MXBean with exactly the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    "same management"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    + " interface as above and where DescriptorKey is used.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            mbs.registerMBean(describedMXBean, name3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            System.out.println("\n\tOK - The MXBean "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    + name3 + " is registered = " + mbs.isRegistered(name3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.out.println("\nAll three MBeans correctly registered...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // We check that we don't have getAttribute() permission - as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // it's been voluntarily omitted from our policy file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // If we don't get the Security Exception there is probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // a security configuration pb...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                // We don't have getAttribute() permission, so this must fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                System.err.println("Trying getStringProp() - should fail");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                mbs.getAttribute(name1,"StringProp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                System.err.println("ERROR: didn't get expected SecurityException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        +"\n\t check security configuration & policy file: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                throw new RuntimeException("getStringProp() did not get a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        "SecurityException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } catch (SecurityException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                // OK!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                System.err.println("getStringProp() - failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         } catch (Exception t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (t instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                throw (RuntimeException)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            else throw new RuntimeException(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}