1 /* 2 * Copyright (c) 2013, 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 import java.lang.reflect.Field; 25 import java.lang.reflect.ReflectPermission; 26 import java.security.CodeSource; 27 import java.security.Permission; 28 import java.security.PermissionCollection; 29 import java.security.Permissions; 30 import java.security.Policy; 31 import java.security.ProtectionDomain; 32 import java.util.Arrays; 33 import java.util.Enumeration; 34 import java.util.concurrent.atomic.AtomicBoolean; 35 36 /** 37 * @test 38 * @bug 8065552 39 * @summary test that all fields returned by getDeclaredFields() can be 40 * set accessible if the right permission is granted. 41 * @run main/othervm ClassDeclaredFieldsTest UNSECURE 42 * @run main/othervm ClassDeclaredFieldsTest SECURE 43 * 44 * @author danielfuchs 45 */ 46 public class ClassDeclaredFieldsTest { 47 48 // Test with or without a security manager 49 public static enum TestCase { 50 UNSECURE, SECURE; 51 public void run() throws Exception { 52 System.out.println("Running test case: " + name()); 53 Configure.setUp(this); 54 test(this); 55 } 56 } 57 /** 58 * @param args the command line arguments 59 */ 60 public static void main(String[] args) throws Exception { 61 System.out.println(System.getProperty("java.version")); 62 if (args == null || args.length == 0) { 63 args = new String[] { "SECURE" }; 64 } else if (args.length != 1) { 65 throw new IllegalArgumentException("Only one arg expected: " 66 + Arrays.asList(args)); 67 } 68 TestCase.valueOf(args[0]).run(); 69 } 70 71 static void test(TestCase test) { 72 for (Field f : Class.class.getDeclaredFields()) { 73 f.setAccessible(true); 74 System.out.println("Field "+f.getName()+" is now accessible."); 75 if (f.getName().equals("classLoader")) { 76 throw new RuntimeException("Found "+f.getName()+" field!"); 77 } 78 } 79 System.out.println("Passed "+test); 80 } 81 82 // A helper class to configure the security manager for the test, 83 // and bypass it when needed. 84 static class Configure { 85 static Policy policy = null; 86 static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() { 87 @Override 88 protected AtomicBoolean initialValue() { 89 return new AtomicBoolean(false); 90 } 91 }; 92 static void setUp(TestCase test) { 93 switch (test) { 94 case SECURE: 95 if (policy == null && System.getSecurityManager() != null) { 96 throw new IllegalStateException("SecurityManager already set"); 97 } else if (policy == null) { 98 policy = new SimplePolicy(TestCase.SECURE, allowAll); 99 Policy.setPolicy(policy); 100 System.setSecurityManager(new SecurityManager()); 101 } 102 if (System.getSecurityManager() == null) { 103 throw new IllegalStateException("No SecurityManager."); 104 } 105 if (policy == null) { 106 throw new IllegalStateException("policy not configured"); 107 } 108 break; 109 case UNSECURE: 110 if (System.getSecurityManager() != null) { 111 throw new IllegalStateException("SecurityManager already set"); 112 } 113 break; 114 default: 115 new InternalError("No such testcase: " + test); 116 } 117 } 118 static void doPrivileged(Runnable run) { 119 allowAll.get().set(true); 120 try { 121 run.run(); 122 } finally { 123 allowAll.get().set(false); 124 } 125 } 126 } 127 128 // A Helper class to build a set of permissions. 129 final static class PermissionsBuilder { 130 final Permissions perms; 131 public PermissionsBuilder() { 132 this(new Permissions()); 133 } 134 public PermissionsBuilder(Permissions perms) { 135 this.perms = perms; 136 } 137 public PermissionsBuilder add(Permission p) { 138 perms.add(p); 139 return this; 140 } 141 public PermissionsBuilder addAll(PermissionCollection col) { 142 if (col != null) { 143 for (Enumeration<Permission> e = col.elements(); e.hasMoreElements(); ) { 144 perms.add(e.nextElement()); 145 } 146 } 147 return this; 148 } 149 public Permissions toPermissions() { 150 final PermissionsBuilder builder = new PermissionsBuilder(); 151 builder.addAll(perms); 152 return builder.perms; 153 } 154 } 155 156 // Policy for the test... 157 public static class SimplePolicy extends Policy { 158 159 final Permissions permissions; 160 final Permissions allPermissions; 161 final ThreadLocal<AtomicBoolean> allowAll; // actually: this should be in a thread locale 162 public SimplePolicy(TestCase test, ThreadLocal<AtomicBoolean> allowAll) { 163 this.allowAll = allowAll; 164 // we don't actually need any permission to create our 165 // FileHandlers because we're passing invalid parameters 166 // which will make the creation fail... 167 permissions = new Permissions(); 168 permissions.add(new RuntimePermission("accessDeclaredMembers")); 169 permissions.add(new ReflectPermission("suppressAccessChecks")); 170 171 // these are used for configuring the test itself... 172 allPermissions = new Permissions(); 173 allPermissions.add(new java.security.AllPermission()); 174 175 } 176 177 @Override 178 public boolean implies(ProtectionDomain domain, Permission permission) { 179 if (allowAll.get().get()) return allPermissions.implies(permission); 180 return permissions.implies(permission); 181 } 182 183 @Override 184 public PermissionCollection getPermissions(CodeSource codesource) { 185 return new PermissionsBuilder().addAll(allowAll.get().get() 186 ? allPermissions : permissions).toPermissions(); 187 } 188 189 @Override 190 public PermissionCollection getPermissions(ProtectionDomain domain) { 191 return new PermissionsBuilder().addAll(allowAll.get().get() 192 ? allPermissions : permissions).toPermissions(); 193 } 194 } 195 196 }