1 /*
2 * Copyright (c) 2015, 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 */
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Objects;
45 import java.util.Properties;
46 import java.util.Set;
47 import java.util.TreeSet;
48 import java.util.UUID;
49 import java.util.concurrent.Callable;
50 import java.util.concurrent.atomic.AtomicBoolean;
51 import java.util.function.BiFunction;
52 import java.util.function.Function;
53 import java.util.logging.FileHandler;
54 import java.util.logging.LogManager;
55 import java.util.logging.Logger;
56 import java.util.logging.LoggingPermission;
57 import java.util.stream.Collectors;
58 import java.util.stream.Stream;
59
60 /**
61 * @test
62 * @bug 8033661
63 * @summary tests LogManager.updateConfiguration(bin)
64 * @modules java.logging/java.util.logging:open
65 * @run main/othervm UpdateConfigurationTest UNSECURE
66 * @run main/othervm UpdateConfigurationTest SECURE
67 * @author danielfuchs
68 */
69 public class UpdateConfigurationTest {
70
71 /**
72 * We will test the handling of abstract logger nodes with file handlers in
73 * two configurations:
74 * UNSECURE: No security manager.
75 * SECURE: With the security manager present - and the required
76 * permissions granted.
77 */
78 public static enum TestCase {
79 UNSECURE, SECURE;
80 public void run(Properties propertyFile, boolean last) throws Exception {
81 System.out.println("Running test case: " + name());
82 Configure.setUp(this);
83 test(this.name() + " " + propertyFile.getProperty("test.name"),
84 propertyFile, last);
85 }
86 }
87
88
89 private static final String PREFIX =
90 "FileHandler-" + UUID.randomUUID() + ".log";
577
578 final Permissions permissions;
579 final Permissions allPermissions;
580 final ThreadLocal<AtomicBoolean> allowAll; // actually: this should be in a thread locale
581 public SimplePolicy(TestCase test, ThreadLocal<AtomicBoolean> allowAll) {
582 this.allowAll = allowAll;
583 permissions = new Permissions();
584 permissions.add(new LoggingPermission("control", null));
585 permissions.add(new FilePermission(PREFIX+".lck", "read,write,delete"));
586 permissions.add(new FilePermission(PREFIX, "read,write"));
587
588 // these are used for configuring the test itself...
589 allPermissions = new Permissions();
590 allPermissions.add(new java.security.AllPermission());
591
592 }
593
594 @Override
595 public boolean implies(ProtectionDomain domain, Permission permission) {
596 if (allowAll.get().get()) return allPermissions.implies(permission);
597 return permissions.implies(permission);
598 }
599
600 @Override
601 public PermissionCollection getPermissions(CodeSource codesource) {
602 return new PermissionsBuilder().addAll(allowAll.get().get()
603 ? allPermissions : permissions).toPermissions();
604 }
605
606 @Override
607 public PermissionCollection getPermissions(ProtectionDomain domain) {
608 return new PermissionsBuilder().addAll(allowAll.get().get()
609 ? allPermissions : permissions).toPermissions();
610 }
611 }
612
613 }
|
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 */
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Objects;
45 import java.util.Properties;
46 import java.util.Set;
47 import java.util.TreeSet;
48 import java.util.UUID;
49 import java.util.concurrent.Callable;
50 import java.util.concurrent.atomic.AtomicBoolean;
51 import java.util.function.BiFunction;
52 import java.util.function.Function;
53 import java.util.logging.FileHandler;
54 import java.util.logging.LogManager;
55 import java.util.logging.Logger;
56 import java.util.logging.LoggingPermission;
57 import java.util.stream.Collectors;
58 import java.util.stream.Stream;
59
60 /**
61 * @test
62 * @bug 8033661 8189291
63 * @summary tests LogManager.updateConfiguration(bin)
64 * @modules java.logging/java.util.logging:open
65 * @run main/othervm UpdateConfigurationTest UNSECURE
66 * @run main/othervm UpdateConfigurationTest SECURE
67 * @author danielfuchs
68 */
69 public class UpdateConfigurationTest {
70
71 static final Policy DEFAULT_POLICY = Policy.getPolicy();
72
73 /**
74 * We will test the handling of abstract logger nodes with file handlers in
75 * two configurations:
76 * UNSECURE: No security manager.
77 * SECURE: With the security manager present - and the required
78 * permissions granted.
79 */
80 public static enum TestCase {
81 UNSECURE, SECURE;
82 public void run(Properties propertyFile, boolean last) throws Exception {
83 System.out.println("Running test case: " + name());
84 Configure.setUp(this);
85 test(this.name() + " " + propertyFile.getProperty("test.name"),
86 propertyFile, last);
87 }
88 }
89
90
91 private static final String PREFIX =
92 "FileHandler-" + UUID.randomUUID() + ".log";
579
580 final Permissions permissions;
581 final Permissions allPermissions;
582 final ThreadLocal<AtomicBoolean> allowAll; // actually: this should be in a thread locale
583 public SimplePolicy(TestCase test, ThreadLocal<AtomicBoolean> allowAll) {
584 this.allowAll = allowAll;
585 permissions = new Permissions();
586 permissions.add(new LoggingPermission("control", null));
587 permissions.add(new FilePermission(PREFIX+".lck", "read,write,delete"));
588 permissions.add(new FilePermission(PREFIX, "read,write"));
589
590 // these are used for configuring the test itself...
591 allPermissions = new Permissions();
592 allPermissions.add(new java.security.AllPermission());
593
594 }
595
596 @Override
597 public boolean implies(ProtectionDomain domain, Permission permission) {
598 if (allowAll.get().get()) return allPermissions.implies(permission);
599 return permissions.implies(permission) ||
600 DEFAULT_POLICY.implies(domain, permission);
601 }
602
603 @Override
604 public PermissionCollection getPermissions(CodeSource codesource) {
605 return new PermissionsBuilder().addAll(allowAll.get().get()
606 ? allPermissions : permissions).toPermissions();
607 }
608
609 @Override
610 public PermissionCollection getPermissions(ProtectionDomain domain) {
611 return new PermissionsBuilder().addAll(allowAll.get().get()
612 ? allPermissions : permissions).toPermissions();
613 }
614 }
615
616 }
|