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 24import java.io.FilePermission; 25import java.io.IOException; 26import java.security.CodeSource; 27import java.security.Permission; 28import java.security.PermissionCollection; 29import java.security.Permissions; 30import java.security.Policy; 31import java.security.ProtectionDomain; 32import java.security.SecurityPermission; 33import java.util.Arrays; 34import java.util.PropertyPermission; 35 36import org.testng.Assert; 37import org.testng.annotations.AfterClass; 38import org.testng.annotations.BeforeGroups; 39import org.testng.annotations.Test; 40 41/* 42 * @test 43 * @run testng/othervm PermissionTest 44 * @summary Test Permissions to access Info 45 */ 46 47public class PermissionTest { 48 /** 49 * Backing up policy. 50 */ 51 protected static Policy policy; 52 53 /** 54 * Backing up security manager. 55 */ 56 private static SecurityManager sm; 57 58 /** 59 * Current process handle. 60 */ 61 private final ProcessHandle currentHndl; 62 63 PermissionTest() { 64 policy = Policy.getPolicy(); 65 sm = System.getSecurityManager(); 66 currentHndl = ProcessHandle.current(); 67 } 68 69 @Test 70 public void descendantsWithPermission() { 71 Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); 72 currentHndl.descendants(); 73 } 74 75 @Test 76 public void allProcessesWithPermission() { 77 Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); 78 ProcessHandle.allProcesses(); 79 } 80 81 @Test 82 public void childrenWithPermission() { 83 Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); 84 currentHndl.children(); 85 } 86 87 @Test 88 public void currentWithPermission() { 89 Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); 90 ProcessHandle.current(); 91 } 92 93 @Test 94 public void ofWithPermission() { 95 Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); 96 ProcessHandle.of(0); 97 } 98 99 @Test 100 public void parentWithPermission() { 101 Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); 102 currentHndl.parent(); 103 } 104 105 @Test 106 public void processToHandleWithPermission() throws IOException { 107 Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); 108 Process p = null; 109 try { 110 ProcessBuilder pb = new ProcessBuilder("sleep", "30"); 111 p = pb.start(); 112 ProcessHandle ph = p.toHandle(); 113 Assert.assertNotNull(ph, "ProcessHandle expected from Process"); 114 } finally { 115 if (p != null) { 116 p.destroy(); 117 } 118 } 119 } 120 121 @BeforeGroups (groups = {"NoManageProcessPermission"}) 122 public void noPermissionsSetup(){ 123 Policy.setPolicy(new TestPolicy()); 124 SecurityManager sm = new SecurityManager(); 125 System.setSecurityManager(sm); 126 } 127 128 @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) 129 public void noPermissionAllChildren() { 130 currentHndl.descendants(); 131 } 132 133 @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) 134 public void noPermissionAllProcesses() { 135 ProcessHandle.allProcesses(); 136 } 137 138 @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) 139 public void noPermissionChildren() { 140 currentHndl.children(); 141 } 142 143 @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) 144 public void noPermissionCurrent() { 145 ProcessHandle.current(); 146 } 147 148 @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) 149 public void noPermissionOf() { 150 ProcessHandle.of(0); 151 } 152 153 @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) 154 public void noPermissionParent() { 155 currentHndl.parent(); 156 } 157 158 @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) 159 public void noPermissionProcessToHandle() throws IOException { 160 Process p = null; 161 try { 162 ProcessBuilder pb = new ProcessBuilder("sleep", "30"); 163 p = pb.start(); 164 ProcessHandle ph = p.toHandle(); 165 Assert.assertNotNull(ph, "ProcessHandle expected from Process"); 166 } finally { 167 if (p != null) { 168 p.destroy(); 169 } 170 } 171 } 172 173 @AfterClass 174 public void tearDownClass() throws Exception { 175 System.setSecurityManager(sm); 176 Policy.setPolicy(policy); 177 } 178} 179 180class TestPolicy extends Policy { 181 private final PermissionCollection permissions = new Permissions(); 182 183 public TestPolicy() { 184 setBasicPermissions(); 185 } 186 187 /* 188 * Defines the minimal permissions required by testNG and set security 189 * manager permission when running these tests. 190 */ 191 public void setBasicPermissions() { 192 permissions.add(new SecurityPermission("getPolicy")); 193 permissions.add(new SecurityPermission("setPolicy")); 194 permissions.add(new RuntimePermission("getClassLoader")); 195 permissions.add(new RuntimePermission("setSecurityManager")); 196 permissions.add(new RuntimePermission("createSecurityManager")); 197 permissions.add(new PropertyPermission("testng.show.stack.frames", 198 "read")); 199 permissions.add(new PropertyPermission("user.dir", "read")); 200 permissions.add(new PropertyPermission("test.src", "read")); 201 permissions.add(new PropertyPermission("file.separator", "read")); 202 permissions.add(new PropertyPermission("line.separator", "read")); 203 permissions.add(new PropertyPermission("fileStringBuffer", "read")); 204 permissions.add(new PropertyPermission("dataproviderthreadcount", "read")); 205 permissions.add(new FilePermission("<<ALL FILES>>", "execute")); 206 } 207 208 public TestPolicy(Permission... ps) { 209 setBasicPermissions(); 210 Arrays.stream(ps).forEach(p -> permissions.add(p)); 211 } 212 213 @Override 214 public PermissionCollection getPermissions(ProtectionDomain domain) { 215 return permissions; 216 } 217 218 @Override 219 public PermissionCollection getPermissions(CodeSource codesource) { 220 return permissions; 221 } 222 223 @Override 224 public boolean implies(ProtectionDomain domain, Permission perm) { 225 return permissions.implies(perm); 226 } 227} 228