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