1 /*
   2  * Copyright (c) 2014, 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.io.FilePermission;
  26 import java.security.AllPermission;
  27 import java.security.CodeSource;
  28 import java.security.Permission;
  29 import java.security.PermissionCollection;
  30 import java.security.Permissions;
  31 import java.security.Policy;
  32 import java.security.ProtectionDomain;
  33 import java.security.SecurityPermission;
  34 import java.util.Arrays;
  35 import java.util.Collections;
  36 import java.util.Enumeration;
  37 import java.util.PropertyPermission;
  38 import java.util.StringJoiner;
  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     /**
  50      * Constructor which sets the minimum permissions by default allowing testNG 
  51      * to work with a SecurityManager. 
  52      */
  53     public TestPolicy() {
  54         setMinimalPermissions(); 
  55     }
  56     
  57     /**
  58      * Constructor which sets the minimum permissions by default allowing testNG 
  59      * to work with a SecurityManager. 
  60      * @param ps permissions to be added.
  61      */
  62     public TestPolicy(Permissions ps) {
  63         setMinimalPermissions(); 
  64         addingPermissions(ps);
  65     }
  66 
  67     /**
  68      * Constructor which sets the minimum permissions by default allowing testNG 
  69      * to work with a SecurityManager. 
  70      * @param ps permission array to be added.
  71      */
  72     public TestPolicy(Permission... ps) {
  73         setMinimalPermissions(); 
  74         addingPermissions(ps);
  75     }
  76 
  77     /**
  78      * Defines the minimal permissions required by testNG when running these
  79      * tests
  80      */
  81     protected void setMinimalPermissions() {
  82         permissions.add(new SecurityPermission("getPolicy"));
  83         permissions.add(new SecurityPermission("setPolicy"));
  84         permissions.add(new RuntimePermission("getClassLoader"));
  85         permissions.add(new RuntimePermission("setSecurityManager"));
  86         permissions.add(new RuntimePermission("createSecurityManager"));
  87         permissions.add(new PropertyPermission("testng.show.stack.frames",
  88                 "read"));
  89         permissions.add(new PropertyPermission("test.classes", "read"));
  90         permissions.add(new PropertyPermission("test.src", "read"));
  91         permissions.add(new PropertyPermission("file.separator", "read"));
  92         permissions.add(new PropertyPermission("line.separator", "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     public void addingPermissions(Permissions ps) {
 102         setMinimalPermissions();
 103         Collections.list(ps.elements()).forEach(p -> permissions.add(p));
 104     }
 105     
 106     
 107     /*
 108      * Add permissions for your tests.
 109      * @param permissions to be added.
 110      */
 111     public void addingPermissions(Permission[] ps) {
 112         setMinimalPermissions();
 113         Arrays.stream(ps).forEach(p -> permissions.add(p));
 114     }
 115     
 116     /**
 117      * Set all permissions. Caution: this should not called carefully unless
 118      * it's really needed.
 119      */
 120     public void setAllPermissions() {
 121         permissions.add(new AllPermission());
 122     }
 123 
 124     /*
 125      * Overloaded methods from the Policy class.
 126      */
 127     @Override
 128     public String toString() {
 129         StringJoiner sj = new StringJoiner("\n", "policy: ", "");
 130         Enumeration<Permission> perms = permissions.elements();
 131         while (perms.hasMoreElements()) {
 132             sj.add(perms.nextElement().toString());
 133         }
 134         return sj.toString();
 135 
 136     }
 137 
 138     @Override
 139     public PermissionCollection getPermissions(ProtectionDomain domain) {
 140         return permissions;
 141     }
 142 
 143     @Override
 144     public PermissionCollection getPermissions(CodeSource codesource) {
 145         return permissions;
 146     }
 147 
 148     @Override
 149     public boolean implies(ProtectionDomain domain, Permission perm) {
 150         return permissions.implies(perm);
 151     }
 152 }