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