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