1 /*
   2  * Copyright (c) 2013, 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 
  24 import java.io.File;
  25 import java.io.FilePermission;
  26 import java.io.IOException;
  27 import java.security.Permission;
  28 import java.security.PermissionCollection;
  29 import java.security.Permissions;
  30 import java.security.Policy;
  31 import java.security.ProtectionDomain;
  32 import java.util.PropertyPermission;
  33 import java.util.logging.Level;
  34 import java.util.logging.LogManager;
  35 import java.util.logging.Logger;
  36 import java.util.logging.LoggingPermission;
  37 import jdk.internal.misc.JavaAWTAccess;
  38 import jdk.internal.misc.SharedSecrets;
  39 
  40 /**
  41  * @test
  42  * @bug 8030850
  43  * @summary Tests that setting .level=FINEST for the root logger in logging
  44  *      configuration file does work.
  45  * @modules java.base/jdk.internal.misc
  46  * @run main/othervm RootLevelInConfigFile
  47  *
  48  * @author danielfuchs
  49  */
  50 public class RootLevelInConfigFile {
  51 
  52     public final static String CONFIG_FILE_KEY = "java.util.logging.config.file";
  53 
  54     public static void main(String[] args) throws IOException {
  55         System.setProperty(CONFIG_FILE_KEY,
  56                 new File(System.getProperty("test.src", "."),
  57                         "rootlogger.properties").getAbsolutePath());
  58         System.out.println(CONFIG_FILE_KEY + "="
  59                 + System.getProperty(CONFIG_FILE_KEY));
  60         if (! new File(System.getProperty(CONFIG_FILE_KEY)).canRead()) {
  61             throw new RuntimeException("can't read config file: "
  62                     + System.getProperty(CONFIG_FILE_KEY));
  63         }
  64 
  65         final String configFile = System.getProperty(CONFIG_FILE_KEY);
  66 
  67         test("no security");
  68 
  69         LogManager.getLogManager().readConfiguration();
  70 
  71         Policy.setPolicy(new SimplePolicy(configFile));
  72         System.setSecurityManager(new SecurityManager());
  73 
  74         test("security");
  75 
  76         LogManager.getLogManager().readConfiguration();
  77 
  78         final JavaAWTAccessStub access = new JavaAWTAccessStub();
  79         SharedSecrets.setJavaAWTAccess(access);
  80 
  81         test("security and no context");
  82 
  83         for (Context ctx : Context.values()) {
  84 
  85             LogManager.getLogManager().readConfiguration();
  86 
  87             access.setContext(ctx);
  88 
  89             test("security and context " + ctx);
  90         }
  91     }
  92 
  93     public static void test(String conf) throws IOException {
  94 
  95         System.out.println("Testing with " + conf);
  96 
  97         testLoggableLevels();
  98 
  99         LogManager.getLogManager().readConfiguration();
 100 
 101         testLoggableLevels();
 102 
 103     }
 104 
 105     private static void testLoggableLevels() {
 106 
 107         Logger foobar = Logger.getLogger("foo.bar");
 108         if (!foobar.isLoggable(Level.FINEST)) {
 109             throw new RuntimeException("Expected FINEST to be loggable in "
 110                     + foobar.getName());
 111         }
 112         if (!foobar.getParent().isLoggable(Level.FINEST)) {
 113             throw new RuntimeException("Expected FINEST to be loggable in "
 114                     + foobar.getName());
 115         }
 116 
 117         Logger global = Logger.getGlobal();
 118         if (!global.isLoggable(Level.FINEST)) {
 119             throw new RuntimeException("Expected FINEST to be loggable in "
 120                     + global.getName());
 121         }
 122         if (!global.getParent().isLoggable(Level.FINEST)) {
 123             throw new RuntimeException("Expected FINEST to be loggable in "
 124                     + global.getName());
 125         }
 126 
 127         Logger root = Logger.getLogger("");
 128         if (!global.isLoggable(Level.FINEST)) {
 129             throw new RuntimeException("Expected FINEST to be loggable in "
 130                     + root.getName());
 131         }
 132         if (!global.getParent().isLoggable(Level.FINEST)) {
 133             throw new RuntimeException("Expected FINEST to be loggable in "
 134                     + root.getName());
 135         }
 136 
 137         root.setLevel(Level.FINER);
 138 
 139         if (foobar.isLoggable(Level.FINEST)) {
 140             throw new RuntimeException("Didn't expect FINEST to be loggable in "
 141                     + foobar.getName());
 142         }
 143         if (foobar.getParent().isLoggable(Level.FINEST)) {
 144             throw new RuntimeException("Didn't expect FINEST to be loggable in "
 145                     + foobar.getName());
 146         }
 147         if (global.isLoggable(Level.FINEST)) {
 148             throw new RuntimeException("Didn't expect FINEST to be loggable in "
 149                     + global.getName());
 150         }
 151         if (global.getParent().isLoggable(Level.FINEST)) {
 152             throw new RuntimeException("Didn't expect FINEST to be loggable in "
 153                     + global.getName());
 154         }
 155 
 156         if (!foobar.isLoggable(Level.FINER)) {
 157             throw new RuntimeException("Expected FINER to be loggable in "
 158                     + foobar.getName());
 159         }
 160         if (!foobar.getParent().isLoggable(Level.FINER)) {
 161             throw new RuntimeException("Expected FINER to be loggable in "
 162                     + foobar.getName());
 163         }
 164 
 165         if (!global.isLoggable(Level.FINER)) {
 166             throw new RuntimeException("Expected FINER to be loggable in "
 167                     + global.getName());
 168         }
 169         if (!global.getParent().isLoggable(Level.FINER)) {
 170             throw new RuntimeException("Expected FINER to be loggable in "
 171                     + global.getName());
 172         }
 173 
 174     }
 175 
 176     static final class SimplePolicy extends Policy {
 177 
 178         final PermissionCollection perms = new Permissions();
 179         public SimplePolicy(String configFile) {
 180             perms.add(new LoggingPermission("control", null));
 181             perms.add(new PropertyPermission("java.util.logging.config.class","read"));
 182             perms.add(new PropertyPermission("java.util.logging.config.file","read"));
 183             perms.add(new FilePermission(configFile, "read"));
 184             perms.add(new RuntimePermission("accessClassInPackage.jdk.internal.misc"));
 185         }
 186 
 187         @Override
 188         public boolean implies(ProtectionDomain domain, Permission permission) {
 189             return perms.implies(permission);
 190         }
 191     }
 192 
 193     static enum Context { ONE, TWO };
 194 
 195     static final class JavaAWTAccessStub implements JavaAWTAccess {
 196         private Context context;
 197 
 198         public void setContext(Context context) {
 199             this.context = context;
 200         }
 201 
 202         @Override
 203         public Object getAppletContext() {
 204             return context;
 205         }
 206     }
 207 
 208 }