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.io.FilePermission; 26 import java.security.Permission; 27 import java.security.Permissions; 28 import java.security.Policy; 29 import static jaxp.library.JAXPBaseTest.setPolicy; 30 import org.testng.annotations.BeforeClass; 31 32 /** 33 * This is a base class that every test class that need to access local XML 34 * files must extend if it needs to be run with security mode. 35 */ 36 public class JAXPFileBaseTest extends JAXPBaseTest { 37 /* 38 * Install a SecurityManager along with a base Policy to allow testNG to 39 * run when there is a security manager. 40 */ 41 @BeforeClass 42 @Override 43 public void setUpClass() throws Exception { 44 setPolicy(new FileTestPolicy()); 45 System.setSecurityManager(new SecurityManager()); 46 } 47 48 /* 49 * Add the specified permission(s) to the test policy. 50 * Note there is no way to add permissions to current permissions. Reset 51 * test policy by setting minimal permmisons in addition to specified 52 * permissions when calling this method. 53 */ 54 protected static void setPermissions(Permission... ps) { 55 Policy.setPolicy(new FileTestPolicy(ps)); 56 } 57 58 /* 59 * Add the specified permission(s) to the test policy. 60 * Note there is no way to add permissions to current permissions. Reset 61 * test policy by setting minimal permmisons in addition to specified 62 * permissions when calling this method. 63 */ 64 protected static void setPermissions(Permissions ps) { 65 Policy.setPolicy(new FileTestPolicy(ps)); 66 } 67 } 68 69 /** 70 * This policy is only given to tests that need access local files. Additional 71 * permissions for accessing local files have been granted by default. 72 * @author HaiboYan 73 */ 74 class FileTestPolicy extends TestPolicy { 75 /** 76 * Constructor which sets the minimum permissions by default allowing testNG 77 * to work with a SecurityManager. 78 * @param ps permissions to be added. 79 */ 80 public FileTestPolicy(Permissions ps) { 81 super(ps); 82 } 83 84 /** 85 * Constructor which sets the minimum permissions by default allowing testNG 86 * to work with a SecurityManager. 87 * @param ps permission array to be added. 88 */ 89 public FileTestPolicy(Permission... ps) { 90 super(ps); 91 } 92 93 /** 94 * Defines the minimal permissions required by testNG when running these 95 * tests 96 */ 97 @Override 98 protected void setMinimalPermissions() { 99 super.setMinimalPermissions(); 100 permissions.add(new FilePermission(System.getProperty("user.dir") + "/-", 101 "read, write")); 102 permissions.add(new FilePermission(System.getProperty("test.src") + "/-", 103 "read")); 104 } 105 }