< prev index next >

test/javax/xml/jaxp/libs/jaxp/library/JAXPPolicyManager.java

Print this page




   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 

  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.Enumeration;
  34 import java.util.HashMap;
  35 import java.util.Map;
  36 import java.util.PropertyPermission;

  37 import java.util.StringJoiner;
  38 
  39 
  40 /*
  41  * This is a base class that every test class must extend if it needs to be run
  42  * with security mode.
  43  */
  44 public class JAXPPolicyManager {
  45     /*
  46      * Backing up policy.
  47      */
  48     private Policy policyBackup;
  49 
  50     /*
  51      * Backing up security manager.
  52      */
  53     private SecurityManager smBackup;
  54 
  55     /*
  56      * Current policy.


 146     /*
 147      * Remove a temporary permission from current thread context.
 148      *
 149      * @param index to remove.
 150      *
 151      * @throws RuntimeException if no temporary permission list in current
 152      *             thread context or no permission correlated to the index.
 153      */
 154     void removeTmpPermission(int index) {
 155         policy.removeTmpPermission(index);
 156     }
 157 
 158 
 159 }
 160 
 161 /*
 162  * Simple Policy class that supports the required Permissions to validate the
 163  * JAXP concrete classes.
 164  */
 165 class TestPolicy extends Policy {


 166     private final PermissionCollection permissions = new Permissions();
 167 
 168     private ThreadLocal<Map<Integer, Permission>> transientPermissions = new ThreadLocal<>();
 169     private ThreadLocal<Boolean> allowAll = new ThreadLocal<>();
 170 
 171     private static Policy defaultPolicy = Policy.getPolicy();
 172 
 173     /*
 174      * Add permission to this policy.
 175      *
 176      * @param permission to be added.
 177      */
 178     void addPermission(Permission p) {
 179         permissions.add(p);
 180     }
 181 
 182     /*
 183      * Set all permissions. Caution: this should not called carefully unless
 184      * it's really needed.
 185      *


 194     public String toString() {
 195         StringJoiner sj = new StringJoiner("\n", "policy: ", "");
 196         Enumeration<Permission> perms = permissions.elements();
 197         while (perms.hasMoreElements()) {
 198             sj.add(perms.nextElement().toString());
 199         }
 200         return sj.toString();
 201 
 202     }
 203 
 204     @Override
 205     public PermissionCollection getPermissions(ProtectionDomain domain) {
 206         return permissions;
 207     }
 208 
 209     @Override
 210     public PermissionCollection getPermissions(CodeSource codesource) {
 211         return permissions;
 212     }
 213 











 214     @Override
 215     public boolean implies(ProtectionDomain domain, Permission perm) {
 216         if (allowAll())
 217             return true;
 218 
 219         if (defaultPolicy.implies(domain, perm))
 220             return true;
 221 
 222         if (permissions.implies(perm))
 223             return true;
 224         else



 225             return tmpImplies(perm);
 226     }
 227 



 228     /*
 229      * Add a temporary permission in current thread context. This won't impact
 230      * global policy and doesn't support permission combination.
 231      *
 232      * @param permission to add.
 233      * @return index of the added permission.
 234      */
 235     int addTmpPermission(Permission p) {
 236         Map<Integer, Permission> tmpPermissions = transientPermissions.get();
 237         if (tmpPermissions == null)
 238             tmpPermissions = new HashMap<>();
 239 
 240         int id = tmpPermissions.size();
 241         tmpPermissions.put(id, p);
 242         transientPermissions.set(tmpPermissions);
 243         return id;
 244     }
 245 
 246     /*
 247      * Remove a temporary permission from current thread context.




   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 
  26 import java.net.URL;
  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.Enumeration;
  35 import java.util.HashMap;
  36 import java.util.Map;
  37 import java.util.PropertyPermission;
  38 import java.util.Set;
  39 import java.util.StringJoiner;
  40 
  41 
  42 /*
  43  * This is a base class that every test class must extend if it needs to be run
  44  * with security mode.
  45  */
  46 public class JAXPPolicyManager {
  47     /*
  48      * Backing up policy.
  49      */
  50     private Policy policyBackup;
  51 
  52     /*
  53      * Backing up security manager.
  54      */
  55     private SecurityManager smBackup;
  56 
  57     /*
  58      * Current policy.


 148     /*
 149      * Remove a temporary permission from current thread context.
 150      *
 151      * @param index to remove.
 152      *
 153      * @throws RuntimeException if no temporary permission list in current
 154      *             thread context or no permission correlated to the index.
 155      */
 156     void removeTmpPermission(int index) {
 157         policy.removeTmpPermission(index);
 158     }
 159 
 160 
 161 }
 162 
 163 /*
 164  * Simple Policy class that supports the required Permissions to validate the
 165  * JAXP concrete classes.
 166  */
 167 class TestPolicy extends Policy {
 168     private final static Set<String> TEST_JARS =
 169          Set.of("jtreg.jar", "javatest.jar", "testng.jar");
 170     private final PermissionCollection permissions = new Permissions();
 171 
 172     private ThreadLocal<Map<Integer, Permission>> transientPermissions = new ThreadLocal<>();
 173     private ThreadLocal<Boolean> allowAll = new ThreadLocal<>();
 174 
 175     private static Policy defaultPolicy = Policy.getPolicy();
 176 
 177     /*
 178      * Add permission to this policy.
 179      *
 180      * @param permission to be added.
 181      */
 182     void addPermission(Permission p) {
 183         permissions.add(p);
 184     }
 185 
 186     /*
 187      * Set all permissions. Caution: this should not called carefully unless
 188      * it's really needed.
 189      *


 198     public String toString() {
 199         StringJoiner sj = new StringJoiner("\n", "policy: ", "");
 200         Enumeration<Permission> perms = permissions.elements();
 201         while (perms.hasMoreElements()) {
 202             sj.add(perms.nextElement().toString());
 203         }
 204         return sj.toString();
 205 
 206     }
 207 
 208     @Override
 209     public PermissionCollection getPermissions(ProtectionDomain domain) {
 210         return permissions;
 211     }
 212 
 213     @Override
 214     public PermissionCollection getPermissions(CodeSource codesource) {
 215         return permissions;
 216     }
 217 
 218 
 219     private boolean isTestMachineryDomain(ProtectionDomain domain) {
 220         CodeSource cs = (domain == null) ? null : domain.getCodeSource();
 221         URL loc = (cs == null) ? null : cs.getLocation();
 222         String path = (loc == null) ? null : loc.getPath();
 223         return path != null && TEST_JARS.stream()
 224                                 .filter(path::endsWith)
 225                                 .findAny()
 226                                 .isPresent();
 227     }
 228 
 229     @Override
 230     public boolean implies(ProtectionDomain domain, Permission perm) {
 231         if (allowAll())
 232             return true;
 233 
 234         if (defaultPolicy.implies(domain, perm))
 235             return true;
 236 
 237         if (permissions.implies(perm))
 238             return true;
 239 
 240         if (isTestMachineryDomain(domain))
 241             return true;
 242             
 243         return tmpImplies(perm);
 244     }
 245 
 246 
 247 
 248 
 249     /*
 250      * Add a temporary permission in current thread context. This won't impact
 251      * global policy and doesn't support permission combination.
 252      *
 253      * @param permission to add.
 254      * @return index of the added permission.
 255      */
 256     int addTmpPermission(Permission p) {
 257         Map<Integer, Permission> tmpPermissions = transientPermissions.get();
 258         if (tmpPermissions == null)
 259             tmpPermissions = new HashMap<>();
 260 
 261         int id = tmpPermissions.size();
 262         tmpPermissions.put(id, p);
 263         transientPermissions.set(tmpPermissions);
 264         return id;
 265     }
 266 
 267     /*
 268      * Remove a temporary permission from current thread context.


< prev index next >