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.io.FilePermission;
25 import java.io.IOException;
26 import java.io.UncheckedIOException;
27 import java.lang.reflect.AccessibleObject;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.Modifier;
30 import java.lang.reflect.InaccessibleObjectException;
31 import java.lang.reflect.ReflectPermission;
32 import java.net.URI;
33 import java.nio.file.FileSystem;
34 import java.nio.file.FileSystems;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.security.CodeSource;
38 import java.security.Permission;
39 import java.security.PermissionCollection;
40 import java.security.Permissions;
41 import java.security.Policy;
42 import java.security.ProtectionDomain;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.Enumeration;
47 import java.util.Iterator;
48 import java.util.List;
49 import java.util.PropertyPermission;
50 import java.util.concurrent.atomic.AtomicBoolean;
51 import java.util.concurrent.atomic.AtomicLong;
52 import java.util.stream.Stream;
53
54 import jdk.internal.module.Modules;
55
56 /**
57 * @test
58 * @bug 8065552
59 * @summary test that all public fields returned by getDeclaredFields() can
60 * be set accessible if the right permission is granted; this test
61 * loads all classes and get their declared fields
62 * and call setAccessible(false) followed by setAccessible(true);
63 * @modules java.base/jdk.internal.module
64 * @run main/othervm --add-modules=ALL-SYSTEM FieldSetAccessibleTest UNSECURE
65 * @run main/othervm --add-modules=ALL-SYSTEM FieldSetAccessibleTest SECURE
66 *
67 * @author danielfuchs
68 */
69 public class FieldSetAccessibleTest {
70
71 static final List<String> cantread = new ArrayList<>();
240 systemClassLoader);
241 return test(c, addExports);
242 } catch (VerifyError ve) {
243 System.err.println("VerifyError for " + clsName);
244 ve.printStackTrace(System.err);
245 failed.add(s);
246 } catch (Exception t) {
247 t.printStackTrace(System.err);
248 failed.add(s);
249 } catch (NoClassDefFoundError e) {
250 e.printStackTrace(System.err);
251 failed.add(s);
252 }
253 return false;
254 }
255
256 static class ClassNameJrtStreamBuilder implements Iterable<String>{
257
258 final FileSystem jrt;
259 final Path root;
260 ClassNameJrtStreamBuilder() {
261 jrt = FileSystems.getFileSystem(URI.create("jrt:/"));
262 root = jrt.getPath("/modules");
263 }
264
265 @Override
266 public Iterator<String> iterator() {
267 try {
268 return Files.walk(root)
269 .filter(p -> p.getNameCount() > 2)
270 .filter(p -> ModuleLayer.boot().findModule(p.getName(1).toString()).isPresent())
271 .map(p -> p.subpath(2, p.getNameCount()))
272 .map(p -> p.toString())
273 .filter(s -> s.endsWith(".class") && !s.endsWith("module-info.class"))
274 .iterator();
275 } catch(IOException x) {
276 throw new UncheckedIOException("Unable to walk \"/modules\"", x);
277 }
278 }
279 }
280
281 // Test with or without a security manager
282 public static enum TestCase {
283 UNSECURE, SECURE;
284 public void run() throws Exception {
285 System.out.println("Running test case: " + name());
286 Configure.setUp(this);
287 FieldSetAccessibleTest.run(this);
288 }
289 }
290
291 // A helper class to configure the security manager for the test,
292 // and bypass it when needed.
293 static class Configure {
294 static Policy policy = null;
295 static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
296 @Override
297 protected AtomicBoolean initialValue() {
298 return new AtomicBoolean(false);
|
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.io.FilePermission;
25 import java.io.IOException;
26 import java.io.UncheckedIOException;
27 import java.lang.module.ModuleFinder;
28 import java.lang.reflect.AccessibleObject;
29 import java.lang.reflect.Field;
30 import java.lang.reflect.Modifier;
31 import java.lang.reflect.InaccessibleObjectException;
32 import java.lang.reflect.ReflectPermission;
33 import java.net.URI;
34 import java.nio.file.FileSystem;
35 import java.nio.file.FileSystems;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.security.CodeSource;
39 import java.security.Permission;
40 import java.security.PermissionCollection;
41 import java.security.Permissions;
42 import java.security.Policy;
43 import java.security.ProtectionDomain;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Collections;
47 import java.util.Enumeration;
48 import java.util.Iterator;
49 import java.util.List;
50 import java.util.Set;
51 import java.util.PropertyPermission;
52 import java.util.concurrent.atomic.AtomicBoolean;
53 import java.util.concurrent.atomic.AtomicLong;
54 import java.util.stream.Collectors;
55 import java.util.stream.Stream;
56
57 import jdk.internal.module.Modules;
58
59 /**
60 * @test
61 * @bug 8065552
62 * @summary test that all public fields returned by getDeclaredFields() can
63 * be set accessible if the right permission is granted; this test
64 * loads all classes and get their declared fields
65 * and call setAccessible(false) followed by setAccessible(true);
66 * @modules java.base/jdk.internal.module
67 * @run main/othervm --add-modules=ALL-SYSTEM FieldSetAccessibleTest UNSECURE
68 * @run main/othervm --add-modules=ALL-SYSTEM FieldSetAccessibleTest SECURE
69 *
70 * @author danielfuchs
71 */
72 public class FieldSetAccessibleTest {
73
74 static final List<String> cantread = new ArrayList<>();
243 systemClassLoader);
244 return test(c, addExports);
245 } catch (VerifyError ve) {
246 System.err.println("VerifyError for " + clsName);
247 ve.printStackTrace(System.err);
248 failed.add(s);
249 } catch (Exception t) {
250 t.printStackTrace(System.err);
251 failed.add(s);
252 } catch (NoClassDefFoundError e) {
253 e.printStackTrace(System.err);
254 failed.add(s);
255 }
256 return false;
257 }
258
259 static class ClassNameJrtStreamBuilder implements Iterable<String>{
260
261 final FileSystem jrt;
262 final Path root;
263 final Set<String> modules;
264 ClassNameJrtStreamBuilder() {
265 jrt = FileSystems.getFileSystem(URI.create("jrt:/"));
266 root = jrt.getPath("/modules");
267 modules = systemModules();
268 }
269
270 @Override
271 public Iterator<String> iterator() {
272 try {
273 return Files.walk(root)
274 .filter(p -> p.getNameCount() > 2)
275 .filter(p -> modules.contains(p.getName(1).toString()))
276 .map(p -> p.subpath(2, p.getNameCount()))
277 .map(p -> p.toString())
278 .filter(s -> s.endsWith(".class") && !s.endsWith("module-info.class"))
279 .iterator();
280 } catch(IOException x) {
281 throw new UncheckedIOException("Unable to walk \"/modules\"", x);
282 }
283 }
284
285 /*
286 * Filter deployment modules
287 */
288 static Set<String> systemModules() {
289 return ModuleFinder.ofSystem().findAll().stream()
290 .map(mref -> mref.descriptor().name())
291 .filter(mn -> !mn.contains("deploy") &&
292 !mn.contains("plugin") &&
293 !mn.contains("javaws"))
294 .collect(Collectors.toSet());
295 }
296 }
297
298 // Test with or without a security manager
299 public static enum TestCase {
300 UNSECURE, SECURE;
301 public void run() throws Exception {
302 System.out.println("Running test case: " + name());
303 Configure.setUp(this);
304 FieldSetAccessibleTest.run(this);
305 }
306 }
307
308 // A helper class to configure the security manager for the test,
309 // and bypass it when needed.
310 static class Configure {
311 static Policy policy = null;
312 static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
313 @Override
314 protected AtomicBoolean initialValue() {
315 return new AtomicBoolean(false);
|