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