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