< prev index next >

test/jdk/java/lang/System/LoggerFinder/internal/BaseDefaultLoggerFinderTest/BaseDefaultLoggerFinderTest.java

Print this page
rev 47439 : 8189291: Test policy should extend the default system policy
Reviewed-by:
   1 /*
   2  * Copyright (c) 2015, 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  */


  41 import java.util.concurrent.atomic.AtomicBoolean;
  42 import java.util.concurrent.atomic.AtomicLong;
  43 import java.util.function.Supplier;
  44 import java.lang.System.LoggerFinder;
  45 import java.lang.System.Logger;
  46 import java.lang.System.Logger.Level;
  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
  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 RuntimePermission LOGGERFINDER_PERMISSION =
  79                 new RuntimePermission("loggerFinder");
  80     final static boolean VERBOSE = false;
  81     static final ThreadLocal<AtomicBoolean> allowControl = new ThreadLocal<AtomicBoolean>() {
  82         @Override
  83         protected AtomicBoolean initialValue() {
  84             return  new AtomicBoolean(false);
  85         }
  86     };
  87     static final ThreadLocal<AtomicBoolean> allowAccess = new ThreadLocal<AtomicBoolean>() {
  88         @Override
  89         protected AtomicBoolean initialValue() {
  90             return  new AtomicBoolean(false);
  91         }
  92     };
  93 
  94     final static AccessSystemLogger accessSystemLogger = new AccessSystemLogger();
  95     static final Class<?>[] providerClass;
  96     static {
  97         try {


 942             permissions.add(new RuntimePermission("setIO"));
 943         }
 944 
 945         Permissions getPermissions() {
 946             if (allowControl.get().get() || allowAccess.get().get()) {
 947                 PermissionsBuilder builder =  new PermissionsBuilder()
 948                         .addAll(permissions);
 949                 if (allowControl.get().get()) {
 950                     builder.add(CONTROL);
 951                 }
 952                 if (allowAccess.get().get()) {
 953                     builder.add(ACCESS);
 954                 }
 955                 return builder.toPermissions();
 956             }
 957             return permissions;
 958         }
 959 
 960         @Override
 961         public boolean implies(ProtectionDomain domain, Permission permission) {
 962             return getPermissions().implies(permission);

 963         }
 964 
 965         @Override
 966         public PermissionCollection getPermissions(CodeSource codesource) {
 967             return new PermissionsBuilder().addAll(getPermissions()).toPermissions();
 968         }
 969 
 970         @Override
 971         public PermissionCollection getPermissions(ProtectionDomain domain) {
 972             return new PermissionsBuilder().addAll(getPermissions()).toPermissions();
 973         }
 974     }
 975 }
   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  */


  41 import java.util.concurrent.atomic.AtomicBoolean;
  42 import java.util.concurrent.atomic.AtomicLong;
  43 import java.util.function.Supplier;
  44 import java.lang.System.LoggerFinder;
  45 import java.lang.System.Logger;
  46 import java.lang.System.Logger.Level;
  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 {


 943             permissions.add(new RuntimePermission("setIO"));
 944         }
 945 
 946         Permissions getPermissions() {
 947             if (allowControl.get().get() || allowAccess.get().get()) {
 948                 PermissionsBuilder builder =  new PermissionsBuilder()
 949                         .addAll(permissions);
 950                 if (allowControl.get().get()) {
 951                     builder.add(CONTROL);
 952                 }
 953                 if (allowAccess.get().get()) {
 954                     builder.add(ACCESS);
 955                 }
 956                 return builder.toPermissions();
 957             }
 958             return permissions;
 959         }
 960 
 961         @Override
 962         public boolean implies(ProtectionDomain domain, Permission permission) {
 963             return getPermissions().implies(permission) ||
 964                     DEFAULT_POLICY.implies(domain, permission);
 965         }
 966 
 967         @Override
 968         public PermissionCollection getPermissions(CodeSource codesource) {
 969             return new PermissionsBuilder().addAll(getPermissions()).toPermissions();
 970         }
 971 
 972         @Override
 973         public PermissionCollection getPermissions(ProtectionDomain domain) {
 974             return new PermissionsBuilder().addAll(getPermissions()).toPermissions();
 975         }
 976     }
 977 }
< prev index next >