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  */
  23 package jaxp.library;
  24 
  25 import java.security.AllPermission;
  26 import java.security.CodeSource;
  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.security.SecurityPermission;
  33 import java.util.Arrays;
  34 import java.util.Collections;
  35 import java.util.Enumeration;
  36 import java.util.PropertyPermission;
  37 import java.util.StringJoiner;
  38 
  39 
  40 /*
  41  * Simple Policy class that supports the required Permissions to validate the
  42  * JAXP concrete classes.
  43  * Note: permission can only be added. You may want to create a new TestPolicy
  44  *       instance if you need remove permissions.
  45  */
  46 public class TestPolicy extends Policy {
  47     protected final PermissionCollection permissions = new Permissions();
  48 
  49     private static Policy defaultPolicy = Policy.getPolicy();
  50 
  51     /**
  52      * Constructor which sets the minimum permissions by default allowing testNG
  53      * to work with a SecurityManager.
  54      */
  55     public TestPolicy() {
  56         setMinimalPermissions();
  57     }
  58 
  59     /**
  60      * Construct an instance with the minimal permissions required by the test
  61      * environment and additional permission(s) as specified.
  62      * @param ps permissions to be added.
  63      */
  64     public TestPolicy(Permissions ps) {
  65         setMinimalPermissions();
  66         TestPolicy.this.addPermissions(ps);
  67     }
  68 
  69     /**
  70      * Construct an instance with the minimal permissions required by the test
  71      * environment and additional permission(s) as specified.
  72      * @param ps permission array to be added.
  73      */
  74     public TestPolicy(Permission... ps) {
  75         setMinimalPermissions();
  76         addPermissions(ps);
  77     }
  78 
  79     /**
  80      * Defines the minimal permissions required by testNG when running these
  81      * tests
  82      */
  83     protected void setMinimalPermissions() {
  84         permissions.add(new SecurityPermission("getPolicy"));
  85         permissions.add(new SecurityPermission("setPolicy"));
  86         permissions.add(new RuntimePermission("getClassLoader"));
  87         permissions.add(new RuntimePermission("setSecurityManager"));
  88         permissions.add(new RuntimePermission("createSecurityManager"));
  89         permissions.add(new PropertyPermission("testng.show.stack.frames",
  90                 "read"));
  91         permissions.add(new PropertyPermission("user.dir", "read"));
  92         permissions.add(new PropertyPermission("test.src", "read"));
  93         permissions.add(new PropertyPermission("fileStringBuffer", "read"));
  94         permissions.add(new PropertyPermission("dataproviderthreadcount", "read"));
  95     }
  96 
  97     /*
  98      * Add permissions for your tests.
  99      * @param permissions to be added.
 100      */
 101     private void addPermissions(Permissions ps) {
 102         Collections.list(ps.elements()).forEach(p -> permissions.add(p));
 103     }
 104 
 105 
 106     /*
 107      * Add permissions for your tests.
 108      * @param permissions to be added.
 109      */
 110     private void addPermissions(Permission[] ps) {
 111         Arrays.stream(ps).forEach(p -> permissions.add(p));
 112     }
 113 
 114     /**
 115      * Set all permissions. Caution: this should not called carefully unless
 116      * it's really needed.
 117      */
 118     private void setAllPermissions() {
 119         permissions.add(new AllPermission());
 120     }
 121 
 122     /*
 123      * Overloaded methods from the Policy class.
 124      */
 125     @Override
 126     public String toString() {
 127         StringJoiner sj = new StringJoiner("\n", "policy: ", "");
 128         Enumeration<Permission> perms = permissions.elements();
 129         while (perms.hasMoreElements()) {
 130             sj.add(perms.nextElement().toString());
 131         }
 132         return sj.toString();
 133 
 134     }
 135 
 136     @Override
 137     public PermissionCollection getPermissions(ProtectionDomain domain) {
 138         return permissions;
 139     }
 140 
 141     @Override
 142     public PermissionCollection getPermissions(CodeSource codesource) {
 143         return permissions;
 144     }
 145 
 146     @Override
 147     public boolean implies(ProtectionDomain domain, Permission perm) {
 148         if (defaultPolicy.implies(domain, perm))
 149             return true;
 150 
 151         return permissions.implies(perm);
 152     }
 153 }