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