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.security.Permission; 26 import java.security.Permissions; 27 import java.security.Policy; 28 import java.util.PropertyPermission; 29 import org.testng.annotations.AfterClass; 30 import org.testng.annotations.BeforeClass; 31 32 /** 33 * This is a base class that every test class must extend if it needs to be run 34 * with security mode. 35 */ 36 public class JAXPBaseTest { 37 /** 38 * Backing up policy. 39 */ 40 private static Policy policy; 41 42 /** 43 * Backing up security manager. 44 */ 45 private static SecurityManager sm; 46 47 /* 48 * Install a SecurityManager along with a base Policy to allow testNG to 49 * run when there is a security manager. 50 */ 51 @BeforeClass 52 public static void setUpClass() throws Exception { 53 setPolicy(new TestPolicy()); 54 System.setSecurityManager(new SecurityManager()); 55 } 56 57 /* 58 * Install the original Policy and SecurityManager when there is a security 59 * manager. 60 */ 61 @AfterClass 62 public static void tearDownClass() throws Exception { 63 System.setSecurityManager(sm); 64 setPolicy(policy); 65 } 66 67 /* 68 * Utility Method used to set the current Policy. 69 */ 70 protected static void setPolicy(Policy p) { 71 Policy.setPolicy(p); 72 } 73 74 /* 75 * Utility Method used to set minimal permission in addition to ps. 76 */ 77 protected static void setPermissions(Permission... ps) { 78 Policy.setPolicy(new TestPolicy(ps)); 79 } 80 81 /* 82 * Utility Method used to set minimal permission in addition to ps. 83 */ 84 protected static void setPermissions(Permissions ps) { 85 Policy.setPolicy(new TestPolicy(ps)); 86 } 87 88 /** 89 * Backing up policy and security manager for restore when there is a 90 * security manager. 91 */ 92 public JAXPBaseTest() { 93 policy = Policy.getPolicy(); 94 sm = System.getSecurityManager(); 95 } 96 97 /** 98 * Safety acquire a system property. 99 * Note invocation of this method will restore permission to limited 100 * minimal permission of tests. If there is additional permission set 101 * already, you need restore permission by yourself. 102 * @param propName System property name to be acquired. 103 * @return property value 104 */ 105 protected String getSystemProperty(final String propName) { 106 setPermissions(new PropertyPermission(propName, "read")); 107 try { 108 return System.getProperty(propName); 109 } finally { 110 setPermissions(); 111 } 112 } 113 114 /** 115 * Safety set a system property by given system value. 116 * 117 * @param propName System property name to be set. 118 * @param propValue System property value to be set. 119 */ 120 protected void setSystemProperty(final String propName, final String propValue) { 121 setPermissions(new PropertyPermission(propName, "write")); 122 try { 123 if (propValue == null) { 124 System.clearProperty(propName); 125 } else { 126 System.setProperty(propName, propValue); 127 } 128 } finally { 129 setPermissions(); 130 } 131 } 132 }