< prev index next >

test/jdk/java/util/logging/LogManager/Configuration/updateConfiguration/SimpleUpdateConfigWithInputStreamTest.java

Print this page
rev 47439 : 8189291: Test policy should extend the default system policy
Reviewed-by:
   1 /*
   2  * Copyright (c) 2015, 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  */


  33 import java.security.Policy;
  34 import java.security.ProtectionDomain;
  35 import java.util.Arrays;
  36 import java.util.Enumeration;
  37 import java.util.Objects;
  38 import java.util.Properties;
  39 import java.util.concurrent.Callable;
  40 import java.util.concurrent.atomic.AtomicBoolean;
  41 import java.util.concurrent.atomic.AtomicLong;
  42 import java.util.function.BiFunction;
  43 import java.util.function.Function;
  44 import java.util.logging.Handler;
  45 import java.util.logging.Level;
  46 import java.util.logging.LogManager;
  47 import java.util.logging.LogRecord;
  48 import java.util.logging.Logger;
  49 import java.util.logging.LoggingPermission;
  50 
  51 /**
  52  * @test
  53  * @bug 8033661
  54  * @summary tests LogManager.updateConfiguration(InputStream, Function) method
  55  * @run main/othervm SimpleUpdateConfigWithInputStreamTest UNSECURE
  56  * @run main/othervm SimpleUpdateConfigWithInputStreamTest SECURE
  57  * @author danielfuchs
  58  */
  59 public class SimpleUpdateConfigWithInputStreamTest {

  60 
  61     /**
  62      * We will test updateConfiguration in
  63      * two configurations:
  64      * UNSECURE: No security manager.
  65      * SECURE: With the security manager present - and the required
  66      *         permissions granted.
  67      */
  68     public static enum TestCase {
  69         UNSECURE, SECURE;
  70         public void execute(Runnable run) {
  71             System.out.println("Running test case: " + name());
  72             try {
  73                Configure.setUp(this);
  74                Configure.doPrivileged(run, SimplePolicy.allowControl);
  75             } finally {
  76                Configure.doPrivileged(() -> {
  77                    try {
  78                        setSystemProperty("java.util.logging.config.file", null);
  79                        LogManager.getLogManager().readConfiguration();


 638         public final static ThreadLocal<AtomicBoolean> allowControl =
 639                 new ThreadLocal<AtomicBoolean>() {
 640             @Override
 641             protected AtomicBoolean initialValue() {
 642                 return new AtomicBoolean();
 643             }
 644         };
 645         public SimplePolicy(TestCase test) {
 646             basic = new Permissions();
 647             control = new Permissions();
 648             control.add(new LoggingPermission("control", null));
 649 
 650             // these are used for configuring the test itself...
 651             all = new Permissions();
 652             all.add(new java.security.AllPermission());
 653 
 654         }
 655 
 656         @Override
 657         public boolean implies(ProtectionDomain domain, Permission permission) {
 658             return getPermissions(domain).implies(permission);

 659         }
 660 
 661         public PermissionCollection permissions() {
 662             PermissionsBuilder builder = new PermissionsBuilder();
 663             if (allowAll.get().get()) {
 664                 builder.addAll(all);
 665             } else {
 666                 builder.addAll(basic);
 667                 if (allowControl.get().get()) {
 668                     builder.addAll(control);
 669                 }
 670             }
 671             return builder.toPermissions();
 672         }
 673 
 674         @Override
 675         public PermissionCollection getPermissions(CodeSource codesource) {
 676             return permissions();
 677         }
 678 
   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  */


  33 import java.security.Policy;
  34 import java.security.ProtectionDomain;
  35 import java.util.Arrays;
  36 import java.util.Enumeration;
  37 import java.util.Objects;
  38 import java.util.Properties;
  39 import java.util.concurrent.Callable;
  40 import java.util.concurrent.atomic.AtomicBoolean;
  41 import java.util.concurrent.atomic.AtomicLong;
  42 import java.util.function.BiFunction;
  43 import java.util.function.Function;
  44 import java.util.logging.Handler;
  45 import java.util.logging.Level;
  46 import java.util.logging.LogManager;
  47 import java.util.logging.LogRecord;
  48 import java.util.logging.Logger;
  49 import java.util.logging.LoggingPermission;
  50 
  51 /**
  52  * @test
  53  * @bug 8033661 8189291
  54  * @summary tests LogManager.updateConfiguration(InputStream, Function) method
  55  * @run main/othervm SimpleUpdateConfigWithInputStreamTest UNSECURE
  56  * @run main/othervm SimpleUpdateConfigWithInputStreamTest SECURE
  57  * @author danielfuchs
  58  */
  59 public class SimpleUpdateConfigWithInputStreamTest {
  60     static final Policy DEFAULT_POLICY = Policy.getPolicy();
  61 
  62     /**
  63      * We will test updateConfiguration in
  64      * two configurations:
  65      * UNSECURE: No security manager.
  66      * SECURE: With the security manager present - and the required
  67      *         permissions granted.
  68      */
  69     public static enum TestCase {
  70         UNSECURE, SECURE;
  71         public void execute(Runnable run) {
  72             System.out.println("Running test case: " + name());
  73             try {
  74                Configure.setUp(this);
  75                Configure.doPrivileged(run, SimplePolicy.allowControl);
  76             } finally {
  77                Configure.doPrivileged(() -> {
  78                    try {
  79                        setSystemProperty("java.util.logging.config.file", null);
  80                        LogManager.getLogManager().readConfiguration();


 639         public final static ThreadLocal<AtomicBoolean> allowControl =
 640                 new ThreadLocal<AtomicBoolean>() {
 641             @Override
 642             protected AtomicBoolean initialValue() {
 643                 return new AtomicBoolean();
 644             }
 645         };
 646         public SimplePolicy(TestCase test) {
 647             basic = new Permissions();
 648             control = new Permissions();
 649             control.add(new LoggingPermission("control", null));
 650 
 651             // these are used for configuring the test itself...
 652             all = new Permissions();
 653             all.add(new java.security.AllPermission());
 654 
 655         }
 656 
 657         @Override
 658         public boolean implies(ProtectionDomain domain, Permission permission) {
 659             return getPermissions(domain).implies(permission) ||
 660                    DEFAULT_POLICY.implies(domain, permission);
 661         }
 662 
 663         public PermissionCollection permissions() {
 664             PermissionsBuilder builder = new PermissionsBuilder();
 665             if (allowAll.get().get()) {
 666                 builder.addAll(all);
 667             } else {
 668                 builder.addAll(basic);
 669                 if (allowControl.get().get()) {
 670                     builder.addAll(control);
 671                 }
 672             }
 673             return builder.toPermissions();
 674         }
 675 
 676         @Override
 677         public PermissionCollection getPermissions(CodeSource codesource) {
 678             return permissions();
 679         }
 680 
< prev index next >