1 /*
2 * Copyright (c) 2015, 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 */
47 import java.lang.reflect.InvocationTargetException;
48 import java.lang.reflect.Method;
49 import java.security.AccessController;
50 import java.security.PrivilegedAction;
51 import java.util.Locale;
52 import java.util.Objects;
53 import java.util.concurrent.atomic.AtomicReference;
54 import java.util.function.Function;
55 import jdk.internal.logger.DefaultLoggerFinder;
56 import jdk.internal.logger.SimpleConsoleLogger;
57 import sun.util.logging.PlatformLogger;
58
59 /**
60 * @test
61 * @bug 8140364 8145686 8189291
62 * @summary JDK implementation specific unit test for the base DefaultLoggerFinder.
63 * Tests the behavior of DefaultLoggerFinder and SimpleConsoleLogger
64 * implementation.
65 * @modules java.base/sun.util.logging
66 * java.base/jdk.internal.logger
67 * @build AccessSystemLogger BaseDefaultLoggerFinderTest CustomSystemClassLoader
68 * @run driver AccessSystemLogger
69 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader BaseDefaultLoggerFinderTest NOSECURITY
70 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader BaseDefaultLoggerFinderTest NOPERMISSIONS
71 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader BaseDefaultLoggerFinderTest WITHPERMISSIONS
72 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader BaseDefaultLoggerFinderTest WITHCUSTOMWRAPPERS
73 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader BaseDefaultLoggerFinderTest WITHREFLECTION
74 * @author danielfuchs
75 */
76 public class BaseDefaultLoggerFinderTest {
77
78 static final Policy DEFAULT_POLICY = Policy.getPolicy();
79 static final RuntimePermission LOGGERFINDER_PERMISSION =
80 new RuntimePermission("loggerFinder");
81 final static boolean VERBOSE = false;
82 static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
83 @Override
84 protected AtomicBoolean initialValue() {
85 return new AtomicBoolean(false);
86 }
87 };
88 static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
89 @Override
90 protected AtomicBoolean initialValue() {
91 return new AtomicBoolean(false);
92 }
93 };
94
95 final static AccessSystemLogger accessSystemLogger = new AccessSystemLogger();
96 static final Class<?>[] providerClass;
97 static {
98 try {
99 providerClass = new Class<?>[] {
100 ClassLoader.getSystemClassLoader().loadClass("BaseDefaultLoggerFinderTest$BaseLoggerFinder"),
101 };
102 } catch (ClassNotFoundException ex) {
103 throw new ExceptionInInitializerError(ex);
104 }
105 }
106
107 /**
108 * What our test provider needs to implement.
109 */
110 public static interface TestLoggerFinder {
111 public final static AtomicBoolean fails = new AtomicBoolean();
112 public final static AtomicReference<String> conf = new AtomicReference<>("");
113 public final static AtomicLong sequencer = new AtomicLong();
114
115
116 public Logger getLogger(String name, Module caller);
117 public Logger getLocalizedLogger(String name, ResourceBundle bundle, Module caller);
118 void setLevel(Logger logger, Level level, Module caller);
119 void setLevel(Logger logger, PlatformLogger.Level level, Module caller);
120 PlatformLogger.Bridge asPlatformLoggerBridge(Logger logger);
121 }
122
123 public static class BaseLoggerFinder extends DefaultLoggerFinder implements TestLoggerFinder {
124
125 static final RuntimePermission LOGGERFINDER_PERMISSION =
126 new RuntimePermission("loggerFinder");
127 public BaseLoggerFinder() {
128 if (fails.get()) {
129 throw new RuntimeException("Simulate exception while loading provider");
130 }
131 }
132
133 @Override
134 public void setLevel(Logger logger, Level level, Module caller) {
135 PrivilegedAction<Void> pa = () -> {
136 setLevel(logger, PlatformLogger.toPlatformLevel(level), caller);
137 return null;
138 };
139 AccessController.doPrivileged(pa);
140 }
141
142 @Override
143 public void setLevel(Logger logger, PlatformLogger.Level level, Module caller) {
144 PrivilegedAction<Logger> pa = () -> demandLoggerFor(logger.getName(), caller);
145 Logger impl = AccessController.doPrivileged(pa);
146 SimpleConsoleLogger.class.cast(impl)
147 .getLoggerConfiguration()
148 .setPlatformLevel(level);
149 }
150
151 @Override
152 public PlatformLogger.Bridge asPlatformLoggerBridge(Logger logger) {
153 PrivilegedAction<PlatformLogger.Bridge> pa = () ->
154 PlatformLogger.Bridge.convert(logger);
155 return AccessController.doPrivileged(pa);
156 }
157
158 }
159
160 public static class MyBundle extends ResourceBundle {
161
162 final ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>();
163
164 @Override
165 protected Object handleGetObject(String key) {
166 if (key.contains(" (translated)")) {
167 throw new RuntimeException("Unexpected key: " + key);
168 }
169 return map.computeIfAbsent(key, k -> k.toUpperCase(Locale.ROOT) + " (translated)");
170 }
171
172 @Override
173 public Enumeration<String> getKeys() {
174 return Collections.enumeration(map.keySet());
175 }
176
177 }
178 public static class MyLoggerBundle extends MyBundle {
179
460 }
461 }
462 }
463
464
465 public static void test(String[] args) {
466
467 final Class<?> expectedClass = jdk.internal.logger.DefaultLoggerFinder.class;
468
469 System.out.println("Declared provider class: " + providerClass[0]
470 + "[" + providerClass[0].getClassLoader() + "]");
471
472 Stream.of(args).map(TestCases::valueOf).forEach((testCase) -> {
473 TestLoggerFinder provider;
474 ErrorStream.errorStream.restore();
475 switch (testCase) {
476 case NOSECURITY:
477 System.out.println("\n*** Without Security Manager\n");
478 System.out.println(TestLoggerFinder.conf.get());
479 provider = getLoggerFinder(expectedClass);
480 if (!provider.getClass().getName().equals("BaseDefaultLoggerFinderTest$BaseLoggerFinder")) {
481 throw new RuntimeException("Unexpected provider: " + provider.getClass().getName());
482 }
483 test(provider, true);
484 System.out.println("Tetscase count: " + TestLoggerFinder.sequencer.get());
485 break;
486 case NOPERMISSIONS:
487 System.out.println("\n*** With Security Manager, without permissions\n");
488 System.out.println(TestLoggerFinder.conf.get());
489 setSecurityManager();
490 try {
491 provider = getLoggerFinder(expectedClass);
492 throw new RuntimeException("Expected exception not raised");
493 } catch (AccessControlException x) {
494 if (!LOGGERFINDER_PERMISSION.equals(x.getPermission())) {
495 throw new RuntimeException("Unexpected permission check", x);
496 }
497 final boolean control = allowControl.get().get();
498 try {
499 allowControl.get().set(true);
500 provider = getLoggerFinder(expectedClass);
501 if (!provider.getClass().getName().equals("BaseDefaultLoggerFinderTest$BaseLoggerFinder")) {
502 throw new RuntimeException("Unexpected provider: " + provider.getClass().getName());
503 }
504 } finally {
505 allowControl.get().set(control);
506 }
507 }
508 test(provider, false);
509 System.out.println("Tetscase count: " + TestLoggerFinder.sequencer.get());
510 break;
511 case WITHPERMISSIONS:
512 System.out.println("\n*** With Security Manager, with control permission\n");
513 System.out.println(TestLoggerFinder.conf.get());
514 setSecurityManager();
515 final boolean control = allowControl.get().get();
516 try {
517 allowControl.get().set(true);
518 provider = getLoggerFinder(expectedClass);
519 if (!provider.getClass().getName().equals("BaseDefaultLoggerFinderTest$BaseLoggerFinder")) {
520 throw new RuntimeException("Unexpected provider: " + provider.getClass().getName());
521 }
522 test(provider, true);
523 } finally {
524 allowControl.get().set(control);
525 }
526 break;
527 case WITHCUSTOMWRAPPERS:
528 System.out.println("\n*** With Security Manager, with control permission and custom Wrapper\n");
529 System.out.println(TestLoggerFinder.conf.get());
530 setSecurityManager();
531 final boolean previous = allowControl.get().get();
532 try {
533 allowControl.get().set(true);
534 provider = getLoggerFinder(expectedClass);
535 if (!provider.getClass().getName().equals("BaseDefaultLoggerFinderTest$BaseLoggerFinder")) {
536 throw new RuntimeException("Unexpected provider: " + provider.getClass().getName());
537 }
538 test(provider, CustomLoggerWrapper::new, true);
539 } finally {
540 allowControl.get().set(previous);
541 }
542 break;
543 case WITHREFLECTION:
544 System.out.println("\n*** With Security Manager,"
545 + " with control permission,"
546 + " using reflection while logging\n");
547 System.out.println(TestLoggerFinder.conf.get());
548 setSecurityManager();
549 final boolean before = allowControl.get().get();
550 try {
551 allowControl.get().set(true);
552 provider = getLoggerFinder(expectedClass);
553 if (!provider.getClass().getName().equals("BaseDefaultLoggerFinderTest$BaseLoggerFinder")) {
554 throw new RuntimeException("Unexpected provider: " + provider.getClass().getName());
555 }
556 test(provider, ReflectionLoggerWrapper::new, true);
557 } finally {
558 allowControl.get().set(before);
559 }
560 break;
561 default:
562 throw new RuntimeException("Unknown test case: " + testCase);
563 }
564 });
565 System.out.println("\nPASSED: Tested " + TestLoggerFinder.sequencer.get() + " cases.");
566 }
567
568 public static void test(TestLoggerFinder provider, boolean hasRequiredPermissions) {
569 test(provider, Function.identity(), hasRequiredPermissions);
570 }
571
572 public static void test(TestLoggerFinder provider, Function<Logger, Logger> wrapper, boolean hasRequiredPermissions) {
573
|
1 /*
2 * Copyright (c) 2015, 2018, 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 */
47 import java.lang.reflect.InvocationTargetException;
48 import java.lang.reflect.Method;
49 import java.security.AccessController;
50 import java.security.PrivilegedAction;
51 import java.util.Locale;
52 import java.util.Objects;
53 import java.util.concurrent.atomic.AtomicReference;
54 import java.util.function.Function;
55 import jdk.internal.logger.DefaultLoggerFinder;
56 import jdk.internal.logger.SimpleConsoleLogger;
57 import sun.util.logging.PlatformLogger;
58
59 /**
60 * @test
61 * @bug 8140364 8145686 8189291
62 * @summary JDK implementation specific unit test for the base DefaultLoggerFinder.
63 * Tests the behavior of DefaultLoggerFinder and SimpleConsoleLogger
64 * implementation.
65 * @modules java.base/sun.util.logging
66 * java.base/jdk.internal.logger
67 * @build AccessSystemLogger BaseDefaultLoggerFinderTest CustomSystemClassLoader BaseLoggerFinder
68 * @run driver AccessSystemLogger
69 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader BaseDefaultLoggerFinderTest NOSECURITY
70 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader BaseDefaultLoggerFinderTest NOPERMISSIONS
71 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader BaseDefaultLoggerFinderTest WITHPERMISSIONS
72 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader BaseDefaultLoggerFinderTest WITHCUSTOMWRAPPERS
73 * @run main/othervm -Xbootclasspath/a:boot -Djava.system.class.loader=CustomSystemClassLoader BaseDefaultLoggerFinderTest WITHREFLECTION
74 * @author danielfuchs
75 */
76 public class BaseDefaultLoggerFinderTest {
77
78 static final Policy DEFAULT_POLICY = Policy.getPolicy();
79 static final RuntimePermission LOGGERFINDER_PERMISSION =
80 new RuntimePermission("loggerFinder");
81 final static boolean VERBOSE = false;
82 static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
83 @Override
84 protected AtomicBoolean initialValue() {
85 return new AtomicBoolean(false);
86 }
87 };
88 static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
89 @Override
90 protected AtomicBoolean initialValue() {
91 return new AtomicBoolean(false);
92 }
93 };
94
95 final static AccessSystemLogger accessSystemLogger = new AccessSystemLogger();
96 static final Class<?>[] providerClass;
97 static {
98 try {
99 providerClass = new Class<?>[] {
100 ClassLoader.getSystemClassLoader().loadClass("BaseLoggerFinder"),
101 };
102 } catch (ClassNotFoundException ex) {
103 throw new ExceptionInInitializerError(ex);
104 }
105 }
106
107 /**
108 * What our test provider needs to implement.
109 */
110 public static interface TestLoggerFinder {
111 public final static AtomicBoolean fails = new AtomicBoolean();
112 public final static AtomicReference<String> conf = new AtomicReference<>("");
113 public final static AtomicLong sequencer = new AtomicLong();
114
115
116 public Logger getLogger(String name, Module caller);
117 public Logger getLocalizedLogger(String name, ResourceBundle bundle, Module caller);
118 void setLevel(Logger logger, Level level, Module caller);
119 void setLevel(Logger logger, PlatformLogger.Level level, Module caller);
120 PlatformLogger.Bridge asPlatformLoggerBridge(Logger logger);
121 }
122
123 public static class MyBundle extends ResourceBundle {
124
125 final ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>();
126
127 @Override
128 protected Object handleGetObject(String key) {
129 if (key.contains(" (translated)")) {
130 throw new RuntimeException("Unexpected key: " + key);
131 }
132 return map.computeIfAbsent(key, k -> k.toUpperCase(Locale.ROOT) + " (translated)");
133 }
134
135 @Override
136 public Enumeration<String> getKeys() {
137 return Collections.enumeration(map.keySet());
138 }
139
140 }
141 public static class MyLoggerBundle extends MyBundle {
142
423 }
424 }
425 }
426
427
428 public static void test(String[] args) {
429
430 final Class<?> expectedClass = jdk.internal.logger.DefaultLoggerFinder.class;
431
432 System.out.println("Declared provider class: " + providerClass[0]
433 + "[" + providerClass[0].getClassLoader() + "]");
434
435 Stream.of(args).map(TestCases::valueOf).forEach((testCase) -> {
436 TestLoggerFinder provider;
437 ErrorStream.errorStream.restore();
438 switch (testCase) {
439 case NOSECURITY:
440 System.out.println("\n*** Without Security Manager\n");
441 System.out.println(TestLoggerFinder.conf.get());
442 provider = getLoggerFinder(expectedClass);
443 if (!provider.getClass().getName().equals("BaseLoggerFinder")) {
444 throw new RuntimeException("Unexpected provider: " + provider.getClass().getName());
445 }
446 test(provider, true);
447 System.out.println("Tetscase count: " + TestLoggerFinder.sequencer.get());
448 break;
449 case NOPERMISSIONS:
450 System.out.println("\n*** With Security Manager, without permissions\n");
451 System.out.println(TestLoggerFinder.conf.get());
452 setSecurityManager();
453 try {
454 provider = getLoggerFinder(expectedClass);
455 throw new RuntimeException("Expected exception not raised");
456 } catch (AccessControlException x) {
457 if (!LOGGERFINDER_PERMISSION.equals(x.getPermission())) {
458 throw new RuntimeException("Unexpected permission check", x);
459 }
460 final boolean control = allowControl.get().get();
461 try {
462 allowControl.get().set(true);
463 provider = getLoggerFinder(expectedClass);
464 if (!provider.getClass().getName().equals("BaseLoggerFinder")) {
465 throw new RuntimeException("Unexpected provider: " + provider.getClass().getName());
466 }
467 } finally {
468 allowControl.get().set(control);
469 }
470 }
471 test(provider, false);
472 System.out.println("Tetscase count: " + TestLoggerFinder.sequencer.get());
473 break;
474 case WITHPERMISSIONS:
475 System.out.println("\n*** With Security Manager, with control permission\n");
476 System.out.println(TestLoggerFinder.conf.get());
477 setSecurityManager();
478 final boolean control = allowControl.get().get();
479 try {
480 allowControl.get().set(true);
481 provider = getLoggerFinder(expectedClass);
482 if (!provider.getClass().getName().equals("BaseLoggerFinder")) {
483 throw new RuntimeException("Unexpected provider: " + provider.getClass().getName());
484 }
485 test(provider, true);
486 } finally {
487 allowControl.get().set(control);
488 }
489 break;
490 case WITHCUSTOMWRAPPERS:
491 System.out.println("\n*** With Security Manager, with control permission and custom Wrapper\n");
492 System.out.println(TestLoggerFinder.conf.get());
493 setSecurityManager();
494 final boolean previous = allowControl.get().get();
495 try {
496 allowControl.get().set(true);
497 provider = getLoggerFinder(expectedClass);
498 if (!provider.getClass().getName().equals("BaseLoggerFinder")) {
499 throw new RuntimeException("Unexpected provider: " + provider.getClass().getName());
500 }
501 test(provider, CustomLoggerWrapper::new, true);
502 } finally {
503 allowControl.get().set(previous);
504 }
505 break;
506 case WITHREFLECTION:
507 System.out.println("\n*** With Security Manager,"
508 + " with control permission,"
509 + " using reflection while logging\n");
510 System.out.println(TestLoggerFinder.conf.get());
511 setSecurityManager();
512 final boolean before = allowControl.get().get();
513 try {
514 allowControl.get().set(true);
515 provider = getLoggerFinder(expectedClass);
516 if (!provider.getClass().getName().equals("BaseLoggerFinder")) {
517 throw new RuntimeException("Unexpected provider: " + provider.getClass().getName());
518 }
519 test(provider, ReflectionLoggerWrapper::new, true);
520 } finally {
521 allowControl.get().set(before);
522 }
523 break;
524 default:
525 throw new RuntimeException("Unknown test case: " + testCase);
526 }
527 });
528 System.out.println("\nPASSED: Tested " + TestLoggerFinder.sequencer.get() + " cases.");
529 }
530
531 public static void test(TestLoggerFinder provider, boolean hasRequiredPermissions) {
532 test(provider, Function.identity(), hasRequiredPermissions);
533 }
534
535 public static void test(TestLoggerFinder provider, Function<Logger, Logger> wrapper, boolean hasRequiredPermissions) {
536
|