author | kvn |
Fri, 21 Jun 2019 13:04:14 -0700 | |
changeset 55462 | 6dfdcd31463d |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
21304 | 1 |
/* |
55462
6dfdcd31463d
8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
21304 | 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.security.Permission; |
|
25 |
import java.security.Permissions; |
|
26 |
import java.security.Policy; |
|
27 |
import java.security.ProtectionDomain; |
|
28 |
import java.util.logging.Level; |
|
29 |
import java.util.logging.Logger; |
|
30 |
import java.util.logging.LoggingPermission; |
|
31 |
||
32 |
||
33 |
||
34 |
/** |
|
35 |
* @test |
|
21659
3b80f3e5b552
8026952: Test java/util/logging/LogManager/RootLogger/setLevel/TestRootLoggerLevel.java has wrong @bug id
dfuchs
parents:
21304
diff
changeset
|
36 |
* @bug 8026499 |
21304 | 37 |
* @summary checks that Logger.getLogger("").setLevel() is working correctly. |
30820 | 38 |
* @modules java.base/sun.util.logging |
35768
7066da300a08
8149391: Fix module dependences in java/util tests
shurailine
parents:
30820
diff
changeset
|
39 |
* java.logging |
21304 | 40 |
* @build TestRootLoggerLevel |
41 |
* @run main/othervm -Dtest.security=on TestRootLoggerLevel |
|
42 |
* @run main/othervm -Dtest.security=off TestRootLoggerLevel |
|
43 |
* @author danielfuchs |
|
44 |
*/ |
|
45 |
public class TestRootLoggerLevel { |
|
46 |
||
47 |
public static enum Loggers { |
|
48 |
ROOT("", "root"), |
|
49 |
GLOBAL(Logger.GLOBAL_LOGGER_NAME); |
|
50 |
||
51 |
private final String name; |
|
52 |
private final String displayName; |
|
53 |
Loggers(String name) { |
|
54 |
this(name, name); |
|
55 |
} |
|
56 |
Loggers(String name, String displayName) { |
|
57 |
this.name = name; |
|
58 |
this.displayName = displayName; |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
public static void main(String[] args) throws Exception { |
|
63 |
||
64 |
for (Loggers log : Loggers.values()) { |
|
65 |
System.out.println(log.displayName + ": " |
|
66 |
+ Logger.getLogger(log.name) |
|
67 |
+ ": " + Logger.getLogger(log.name).getLevel()); |
|
68 |
} |
|
69 |
||
70 |
if ("on".equals(System.getProperty("test.security","on"))) { |
|
71 |
System.out.println("*** SecurityManager is ON"); |
|
72 |
Policy.setPolicy(new SimplePolicy()); |
|
73 |
||
74 |
System.setSecurityManager(new SecurityManager()); |
|
75 |
} else { |
|
76 |
System.out.println("*** SecurityManager is OFF"); |
|
77 |
} |
|
78 |
||
79 |
// Before the fix, setting the root logger level here had only |
|
80 |
// a transient effect... |
|
81 |
for (Loggers log : Loggers.values()) { |
|
82 |
System.out.println("Logger.getLogger(\"" |
|
83 |
+ log.name |
|
84 |
+ "\").setLevel(Level.FINEST);"); |
|
85 |
Logger.getLogger(log.name).setLevel(Level.FINEST); |
|
86 |
System.out.println(log.displayName + ": " |
|
87 |
+ Logger.getLogger(log.name) |
|
88 |
+ ": " + Logger.getLogger(log.name).getLevel()); |
|
89 |
||
90 |
// At this point - the root logger should have a level which is |
|
91 |
// FINEST - just check it here for sanity. |
|
92 |
if (Logger.getLogger(log.name).getLevel() != Level.FINEST) { |
|
93 |
throw new RuntimeException("Logger " + log.displayName |
|
94 |
+ " level should be FINEST: " |
|
95 |
+ Logger.getLogger(log.name).getLevel()); |
|
96 |
} |
|
97 |
} |
|
98 |
// Initializing some PlatformLogger caused the root logger |
|
99 |
// level to be reset to INFO. |
|
100 |
// |
|
101 |
final Object platformLogger = |
|
102 |
sun.util.logging.PlatformLogger.getLogger("java.foo"); |
|
103 |
System.out.println("Got platform logger: " + platformLogger); |
|
104 |
||
105 |
for (Loggers log : Loggers.values()) { |
|
106 |
// Check that the root logger still has the FINEST value assigned |
|
107 |
// to it earlier. |
|
108 |
System.out.println(log.displayName + ": " |
|
109 |
+ Logger.getLogger(log.name) |
|
110 |
+ ": " + Logger.getLogger(log.name).getLevel()); |
|
111 |
||
112 |
if (Logger.getLogger(log.name).getLevel() != Level.FINEST) { |
|
113 |
throw new RuntimeException("Logger " + log.displayName |
|
114 |
+ " level should be FINEST: " |
|
115 |
+ Logger.getLogger(log.name).getLevel()); |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
} |
|
120 |
||
121 |
private static final class SimplePolicy extends Policy { |
|
55462
6dfdcd31463d
8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents:
47216
diff
changeset
|
122 |
|
6dfdcd31463d
8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents:
47216
diff
changeset
|
123 |
static final Policy DEFAULT_POLICY = Policy.getPolicy(); |
6dfdcd31463d
8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents:
47216
diff
changeset
|
124 |
|
21304 | 125 |
private final Permissions perms; |
126 |
||
127 |
private static final Permissions permissions(Permission... perms) { |
|
128 |
Permissions permissions = new Permissions(); |
|
129 |
for (Permission perm : perms) { |
|
130 |
permissions.add(perm); |
|
131 |
} |
|
132 |
return permissions; |
|
133 |
} |
|
134 |
||
135 |
SimplePolicy() { |
|
136 |
this(permissions(new LoggingPermission("control", null), |
|
137 |
new RuntimePermission("accessClassInPackage.sun.util.logging") |
|
138 |
)); |
|
139 |
} |
|
140 |
||
141 |
SimplePolicy(Permissions perms) { |
|
142 |
this.perms = perms; |
|
143 |
} |
|
144 |
||
145 |
@Override |
|
146 |
public boolean implies(ProtectionDomain domain, Permission permission) { |
|
55462
6dfdcd31463d
8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents:
47216
diff
changeset
|
147 |
return perms.implies(permission) || DEFAULT_POLICY.implies(domain, permission); |
21304 | 148 |
} |
149 |
||
150 |
} |
|
151 |
||
152 |
} |