test/jdk/java/lang/ProcessHandle/PermissionTest.java
author kvn
Fri, 21 Jun 2019 13:04:14 -0700
changeset 55462 6dfdcd31463d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8185139: [Graal] Tests which set too restrictive security manager fail with Graal Summary: tests should also check default policy Reviewed-by: mchung, dfuchs, alanb, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     1
/*
55462
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     4
 *
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     8
 *
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    13
 * accompanied this code).
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    14
 *
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    18
 *
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    21
 * questions.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    22
 */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    23
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    24
import java.io.FilePermission;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    25
import java.io.IOException;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    26
import java.security.CodeSource;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    27
import java.security.Permission;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    28
import java.security.PermissionCollection;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    29
import java.security.Permissions;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    30
import java.security.Policy;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    31
import java.security.ProtectionDomain;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    32
import java.security.SecurityPermission;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    33
import java.util.Arrays;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    34
import java.util.PropertyPermission;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    35
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    36
import org.testng.Assert;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    37
import org.testng.annotations.AfterClass;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    38
import org.testng.annotations.BeforeGroups;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    39
import org.testng.annotations.Test;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    40
34891
dc0cab2e79cd 8146574: (process) ProcessHandle test cleanup
rriggs
parents: 33648
diff changeset
    41
/*
dc0cab2e79cd 8146574: (process) ProcessHandle test cleanup
rriggs
parents: 33648
diff changeset
    42
 * @test
dc0cab2e79cd 8146574: (process) ProcessHandle test cleanup
rriggs
parents: 33648
diff changeset
    43
 * @run testng/othervm PermissionTest
dc0cab2e79cd 8146574: (process) ProcessHandle test cleanup
rriggs
parents: 33648
diff changeset
    44
 * @summary Test Permissions to access Info
dc0cab2e79cd 8146574: (process) ProcessHandle test cleanup
rriggs
parents: 33648
diff changeset
    45
 */
dc0cab2e79cd 8146574: (process) ProcessHandle test cleanup
rriggs
parents: 33648
diff changeset
    46
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    47
public class PermissionTest {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    48
    /**
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    49
     * Backing up policy.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    50
     */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    51
    protected static Policy policy;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    52
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    53
    /**
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    54
     * Backing up security manager.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    55
     */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    56
    private static SecurityManager sm;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    57
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    58
    /**
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    59
     * Current process handle.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    60
     */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    61
    private final ProcessHandle currentHndl;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    62
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    63
    PermissionTest() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    64
        policy = Policy.getPolicy();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    65
        sm = System.getSecurityManager();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    66
        currentHndl = ProcessHandle.current();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    67
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    68
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    69
    @Test
33648
9564031a20e0 8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents: 30899
diff changeset
    70
    public void descendantsWithPermission() {
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    71
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
33648
9564031a20e0 8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents: 30899
diff changeset
    72
        currentHndl.descendants();
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    73
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    74
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    75
    @Test
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    76
    public void allProcessesWithPermission() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    77
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    78
        ProcessHandle.allProcesses();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    79
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    80
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    81
    @Test
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    82
    public void childrenWithPermission() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    83
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    84
        currentHndl.children();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    85
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    86
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    87
    @Test
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    88
    public void currentWithPermission() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    89
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    90
        ProcessHandle.current();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    91
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    92
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    93
    @Test
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    94
    public void ofWithPermission() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    95
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    96
        ProcessHandle.of(0);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    97
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    98
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
    99
    @Test
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   100
    public void parentWithPermission() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   101
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   102
        currentHndl.parent();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   103
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   104
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   105
    @Test
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   106
    public void processToHandleWithPermission() throws IOException {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   107
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   108
        Process p = null;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   109
        try {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   110
            ProcessBuilder pb = new ProcessBuilder("sleep", "30");
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   111
            p = pb.start();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   112
            ProcessHandle ph = p.toHandle();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   113
            Assert.assertNotNull(ph, "ProcessHandle expected from Process");
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   114
        } finally {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   115
            if (p != null) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   116
                p.destroy();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   117
            }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   118
        }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   119
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   120
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   121
    @BeforeGroups (groups = {"NoManageProcessPermission"})
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   122
    public void noPermissionsSetup(){
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   123
        Policy.setPolicy(new TestPolicy());
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   124
        SecurityManager sm = new SecurityManager();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   125
        System.setSecurityManager(sm);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   126
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   127
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   128
    @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   129
    public void noPermissionAllChildren() {
33648
9564031a20e0 8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents: 30899
diff changeset
   130
        currentHndl.descendants();
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   131
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   132
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   133
    @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   134
    public void noPermissionAllProcesses() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   135
        ProcessHandle.allProcesses();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   136
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   137
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   138
    @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   139
    public void noPermissionChildren() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   140
        currentHndl.children();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   141
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   142
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   143
    @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   144
    public void noPermissionCurrent() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   145
        ProcessHandle.current();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   146
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   147
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   148
    @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   149
    public void noPermissionOf() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   150
        ProcessHandle.of(0);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   151
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   152
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   153
    @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   154
    public void noPermissionParent() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   155
        currentHndl.parent();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   156
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   157
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   158
    @Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class)
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   159
    public void noPermissionProcessToHandle() throws IOException {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   160
        Process p = null;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   161
        try {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   162
            ProcessBuilder pb = new ProcessBuilder("sleep", "30");
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   163
            p = pb.start();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   164
            ProcessHandle ph = p.toHandle();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   165
            Assert.assertNotNull(ph, "ProcessHandle expected from Process");
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   166
        } finally {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   167
            if (p != null) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   168
                p.destroy();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   169
            }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   170
        }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   171
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   172
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   173
    @AfterClass
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   174
    public void tearDownClass() throws Exception {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   175
        System.setSecurityManager(sm);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   176
        Policy.setPolicy(policy);
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   177
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   178
}
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   179
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   180
class TestPolicy extends Policy {
55462
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
   181
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
   182
    static final Policy DEFAULT_POLICY = Policy.getPolicy();
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
   183
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   184
    private final PermissionCollection permissions = new Permissions();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   185
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   186
    public TestPolicy() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   187
        setBasicPermissions();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   188
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   189
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   190
    /*
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   191
     * Defines the minimal permissions required by testNG and set security
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   192
     * manager permission when running these tests.
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   193
     */
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   194
    public void setBasicPermissions() {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   195
        permissions.add(new SecurityPermission("getPolicy"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   196
        permissions.add(new SecurityPermission("setPolicy"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   197
        permissions.add(new RuntimePermission("getClassLoader"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   198
        permissions.add(new RuntimePermission("setSecurityManager"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   199
        permissions.add(new RuntimePermission("createSecurityManager"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   200
        permissions.add(new PropertyPermission("testng.show.stack.frames",
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   201
                "read"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   202
        permissions.add(new PropertyPermission("user.dir", "read"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   203
        permissions.add(new PropertyPermission("test.src", "read"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   204
        permissions.add(new PropertyPermission("file.separator", "read"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   205
        permissions.add(new PropertyPermission("line.separator", "read"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   206
        permissions.add(new PropertyPermission("fileStringBuffer", "read"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   207
        permissions.add(new PropertyPermission("dataproviderthreadcount", "read"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   208
        permissions.add(new FilePermission("<<ALL FILES>>", "execute"));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   209
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   210
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   211
    public TestPolicy(Permission... ps) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   212
        setBasicPermissions();
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   213
        Arrays.stream(ps).forEach(p -> permissions.add(p));
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   214
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   215
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   216
    @Override
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   217
    public PermissionCollection getPermissions(ProtectionDomain domain) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   218
        return permissions;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   219
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   220
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   221
    @Override
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   222
    public PermissionCollection getPermissions(CodeSource codesource) {
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   223
        return permissions;
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   224
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   225
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   226
    @Override
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   227
    public boolean implies(ProtectionDomain domain, Permission perm) {
55462
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
   228
        return permissions.implies(perm) || DEFAULT_POLICY.implies(domain, perm);
30899
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   229
    }
d2408e757489 8077350: JEP 102 Process API Updates Implementation
rriggs
parents:
diff changeset
   230
}