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