1 /*
2 * Copyright (c) 2014, 2016, 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 */
29 import java.lang.reflect.Method;
30 import java.lang.System.Logger;
31 import java.lang.System.Logger.Level;
32 import java.security.AllPermission;
33 import java.security.CodeSource;
34 import java.security.Permission;
35 import java.security.PermissionCollection;
36 import java.security.Permissions;
37 import java.security.Policy;
38 import java.security.ProtectionDomain;
39 import java.util.concurrent.atomic.AtomicBoolean;
40 import java.util.Optional;
41 import java.util.Set;
42 import java.util.stream.Collectors;
43 import java.util.stream.Stream;
44 import jdk.internal.logger.BootstrapLogger;
45 import jdk.internal.logger.LazyLoggers;
46
47 /*
48 * @test
49 * @bug 8140364
50 * @author danielfuchs
51 * @summary JDK implementation specific unit test for JDK internal artifacts.
52 Tests the behavior of bootstrap loggers (and SimpleConsoleLoggers
53 * too).
54 * @modules java.base/jdk.internal.logger:+open
55 * java.logging
56 * @build BootstrapLoggerUtils LogStream
57 * @run main/othervm BootstrapLoggerTest NO_SECURITY
58 * @run main/othervm BootstrapLoggerTest SECURE
59 * @run main/othervm/timeout=120 BootstrapLoggerTest SECURE_AND_WAIT
60 */
61 public class BootstrapLoggerTest {
62
63 static final Method isAlive;
64 static final Field logManagerInitialized;
65 static {
66 try {
67 // private reflection hook that allows us to test whether
68 // the BootstrapExecutor is alive.
69 isAlive = BootstrapLogger.class
70 .getDeclaredMethod("isAlive");
71 isAlive.setAccessible(true);
72 // private reflection hook that allows us to test whether the LogManager
73 // has initialized and registered with the BootstrapLogger class
74 logManagerInitialized = BootstrapLogger.class
75 .getDeclaredField("logManagerConfigured");
76 logManagerInitialized.setAccessible(true);
77 } catch (Exception ex) {
78 throw new ExceptionInInitializerError(ex);
79 }
80 }
81
82 static enum TestCase {
348 }
349
350 final static class SimplePolicy extends Policy {
351 static final ThreadLocal<Boolean> allowAll = new ThreadLocal<Boolean>() {
352 @Override
353 protected Boolean initialValue() {
354 return Boolean.FALSE;
355 }
356 };
357
358 Permissions getPermissions() {
359 Permissions perms = new Permissions();
360 if (allowAll.get()) {
361 perms.add(new AllPermission());
362 }
363 return perms;
364 }
365
366 @Override
367 public boolean implies(ProtectionDomain domain, Permission permission) {
368 return getPermissions(domain).implies(permission);
369 }
370
371 @Override
372 public PermissionCollection getPermissions(CodeSource codesource) {
373 return getPermissions();
374 }
375
376 @Override
377 public PermissionCollection getPermissions(ProtectionDomain domain) {
378 return getPermissions();
379 }
380
381 }
382 }
|
1 /*
2 * Copyright (c) 2014, 2017, 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 */
29 import java.lang.reflect.Method;
30 import java.lang.System.Logger;
31 import java.lang.System.Logger.Level;
32 import java.security.AllPermission;
33 import java.security.CodeSource;
34 import java.security.Permission;
35 import java.security.PermissionCollection;
36 import java.security.Permissions;
37 import java.security.Policy;
38 import java.security.ProtectionDomain;
39 import java.util.concurrent.atomic.AtomicBoolean;
40 import java.util.Optional;
41 import java.util.Set;
42 import java.util.stream.Collectors;
43 import java.util.stream.Stream;
44 import jdk.internal.logger.BootstrapLogger;
45 import jdk.internal.logger.LazyLoggers;
46
47 /*
48 * @test
49 * @bug 8140364 8189291
50 * @author danielfuchs
51 * @summary JDK implementation specific unit test for JDK internal artifacts.
52 Tests the behavior of bootstrap loggers (and SimpleConsoleLoggers
53 * too).
54 * @modules java.base/jdk.internal.logger:+open
55 * java.logging
56 * @build BootstrapLoggerUtils LogStream
57 * @run main/othervm BootstrapLoggerTest NO_SECURITY
58 * @run main/othervm BootstrapLoggerTest SECURE
59 * @run main/othervm/timeout=120 BootstrapLoggerTest SECURE_AND_WAIT
60 */
61 public class BootstrapLoggerTest {
62
63 static final Policy DEFAULT_POLICY = Policy.getPolicy();
64 static final Method isAlive;
65 static final Field logManagerInitialized;
66 static {
67 try {
68 // private reflection hook that allows us to test whether
69 // the BootstrapExecutor is alive.
70 isAlive = BootstrapLogger.class
71 .getDeclaredMethod("isAlive");
72 isAlive.setAccessible(true);
73 // private reflection hook that allows us to test whether the LogManager
74 // has initialized and registered with the BootstrapLogger class
75 logManagerInitialized = BootstrapLogger.class
76 .getDeclaredField("logManagerConfigured");
77 logManagerInitialized.setAccessible(true);
78 } catch (Exception ex) {
79 throw new ExceptionInInitializerError(ex);
80 }
81 }
82
83 static enum TestCase {
349 }
350
351 final static class SimplePolicy extends Policy {
352 static final ThreadLocal<Boolean> allowAll = new ThreadLocal<Boolean>() {
353 @Override
354 protected Boolean initialValue() {
355 return Boolean.FALSE;
356 }
357 };
358
359 Permissions getPermissions() {
360 Permissions perms = new Permissions();
361 if (allowAll.get()) {
362 perms.add(new AllPermission());
363 }
364 return perms;
365 }
366
367 @Override
368 public boolean implies(ProtectionDomain domain, Permission permission) {
369 return getPermissions(domain).implies(permission) ||
370 DEFAULT_POLICY.implies(domain, permission);
371 }
372
373 @Override
374 public PermissionCollection getPermissions(CodeSource codesource) {
375 return getPermissions();
376 }
377
378 @Override
379 public PermissionCollection getPermissions(ProtectionDomain domain) {
380 return getPermissions();
381 }
382
383 }
384 }
|