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