|
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.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 protected 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 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 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 * Add the specified permission(s) to the test policy. |
|
76 * Note there is no way to add permissions to current permissions. Reset |
|
77 * test policy by setting minimal permmisons in addition to specified |
|
78 * permissions when calling this method. |
|
79 */ |
|
80 protected static void setPermissions(Permission... ps) { |
|
81 Policy.setPolicy(new TestPolicy(ps)); |
|
82 } |
|
83 |
|
84 /* |
|
85 * Add the specified permission(s) to the test policy. |
|
86 * Note there is no way to add permissions to current permissions. Reset |
|
87 * test policy by setting minimal permmisons in addition to specified |
|
88 * permissions when calling this method. |
|
89 */ |
|
90 protected static void setPermissions(Permissions ps) { |
|
91 Policy.setPolicy(new TestPolicy(ps)); |
|
92 } |
|
93 |
|
94 /** |
|
95 * Backing up policy and security manager for restore when there is a |
|
96 * security manager. |
|
97 */ |
|
98 public JAXPBaseTest() { |
|
99 policy = Policy.getPolicy(); |
|
100 sm = System.getSecurityManager(); |
|
101 } |
|
102 |
|
103 /** |
|
104 * Safety acquire a system property. |
|
105 * Note invocation of this method will restore permission to limited |
|
106 * minimal permission of tests. If there is additional permission set |
|
107 * already, you need restore permission by yourself. |
|
108 * @param propName System property name to be acquired. |
|
109 * @return property value |
|
110 */ |
|
111 protected String getSystemProperty(final String propName) { |
|
112 setPermissions(new PropertyPermission(propName, "read")); |
|
113 try { |
|
114 return System.getProperty(propName); |
|
115 } finally { |
|
116 setPermissions(); |
|
117 } |
|
118 } |
|
119 |
|
120 /** |
|
121 * Safety set a system property by given system value. |
|
122 * |
|
123 * @param propName System property name to be set. |
|
124 * @param propValue System property value to be set. |
|
125 */ |
|
126 protected void setSystemProperty(final String propName, final String propValue) { |
|
127 setPermissions(new PropertyPermission(propName, "write")); |
|
128 try { |
|
129 if (propValue == null) { |
|
130 System.clearProperty(propName); |
|
131 } else { |
|
132 System.setProperty(propName, propValue); |
|
133 } |
|
134 } finally { |
|
135 setPermissions(); |
|
136 } |
|
137 } |
|
138 } |