File: PermissionTest.java

package info (click to toggle)
openjdk-11 11.0.28~3ea-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 780,896 kB
  • sloc: java: 5,208,133; xml: 1,192,267; cpp: 1,138,346; ansic: 461,925; javascript: 162,416; sh: 16,738; objc: 13,721; python: 4,757; asm: 3,570; makefile: 2,962; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (243 lines) | stat: -rw-r--r-- 8,059 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.FilePermission;
import java.io.IOException;
import java.lang.reflect.ReflectPermission;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import java.security.ProtectionDomain;
import java.security.SecurityPermission;
import java.util.Arrays;
import java.util.PropertyPermission;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

/*
 * @test
 * @run testng/othervm PermissionTest
 * @summary Test Permissions to access Info
 */

public class PermissionTest {
    /**
     * Backing up policy.
     */
    protected static Policy policy;

    /**
     * Backing up security manager.
     */
    private static SecurityManager sm;

    /**
     * Current process handle.
     */
    private final ProcessHandle currentHndl;

    PermissionTest() {
        policy = Policy.getPolicy();
        sm = System.getSecurityManager();
        currentHndl = ProcessHandle.current();
    }

    @Test
    public void descendantsWithPermission() {
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
        currentHndl.descendants();
    }

    @Test
    public void allProcessesWithPermission() {
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
        ProcessHandle.allProcesses();
    }

    @Test
    public void childrenWithPermission() {
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
        currentHndl.children();
    }

    @Test
    public void currentWithPermission() {
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
        ProcessHandle.current();
    }

    @Test
    public void ofWithPermission() {
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
        ProcessHandle.of(0);
    }

    @Test
    public void parentWithPermission() {
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
        currentHndl.parent();
    }

    @Test
    public void processToHandleWithPermission() throws IOException {
        Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
        Process p = null;
        try {
            ProcessBuilder pb = new ProcessBuilder("sleep", "30");
            p = pb.start();
            ProcessHandle ph = p.toHandle();
            Assert.assertNotNull(ph, "ProcessHandle expected from Process");
        } finally {
            if (p != null) {
                p.destroy();
            }
        }
    }

    /**
     * Setup a policy that would reject ProcessHandle requests without Permissions ManageProcess.
     */
    public void noPermissionsSetup(){
        Policy.setPolicy(new TestPolicy());
        SecurityManager sm = new SecurityManager();
        System.setSecurityManager(sm);
    }

    @Test(expectedExceptions = SecurityException.class)
    public void noPermissionAllChildren() {
        noPermissionsSetup();
        currentHndl.descendants();
    }

    @Test(expectedExceptions = SecurityException.class)
    public void noPermissionAllProcesses() {
        noPermissionsSetup();
        ProcessHandle.allProcesses();
    }

    @Test(expectedExceptions = SecurityException.class)
    public void noPermissionChildren() {
        noPermissionsSetup();
        currentHndl.children();
    }

    @Test(expectedExceptions = SecurityException.class)
    public void noPermissionCurrent() {
        noPermissionsSetup();
        ProcessHandle.current();
    }

    @Test(expectedExceptions = SecurityException.class)
    public void noPermissionOf() {
        noPermissionsSetup();
        ProcessHandle.of(0);
    }

    @Test(expectedExceptions = SecurityException.class)
    public void noPermissionParent() {
        noPermissionsSetup();
        currentHndl.parent();
    }

    @Test(expectedExceptions = SecurityException.class)
    public void noPermissionProcessToHandle() throws IOException {
        noPermissionsSetup();
        Process p = null;
        try {
            ProcessBuilder pb = new ProcessBuilder("sleep", "30");
            p = pb.start();
            ProcessHandle ph = p.toHandle();
            Assert.assertNotNull(ph, "ProcessHandle expected from Process");
        } finally {
            if (p != null) {
                p.destroy();
            }
        }
    }

    @AfterClass
    public void tearDownClass() throws Exception {
        System.setSecurityManager(sm);
        Policy.setPolicy(policy);
    }
}

class TestPolicy extends Policy {
    private final PermissionCollection permissions = new Permissions();

    public TestPolicy() {
        setBasicPermissions();
    }

    /*
     * Defines the minimal permissions required by testNG and set security
     * manager permission when running these tests.
     */
    public void setBasicPermissions() {
        permissions.add(new SecurityPermission("getPolicy"));
        permissions.add(new SecurityPermission("setPolicy"));
        permissions.add(new RuntimePermission("getClassLoader"));
        permissions.add(new RuntimePermission("setSecurityManager"));
        permissions.add(new RuntimePermission("createSecurityManager"));
        permissions.add(new PropertyPermission("user.dir", "read"));
        permissions.add(new PropertyPermission("test.src", "read"));
        permissions.add(new PropertyPermission("file.separator", "read"));
        permissions.add(new PropertyPermission("line.separator", "read"));
        permissions.add(new PropertyPermission("fileStringBuffer", "read"));
        permissions.add(new PropertyPermission("dataproviderthreadcount", "read"));
        permissions.add(new PropertyPermission("testng.show.stack.frames",
                "read"));
        permissions.add(new PropertyPermission("testng.thread.affinity", "read"));
        permissions.add(new PropertyPermission("testng.memory.friendly", "read"));
        permissions.add(new PropertyPermission("testng.mode.dryrun", "read"));
        permissions.add(new PropertyPermission("testng.report.xml.name", "read"));
        permissions.add(new PropertyPermission("testng.timezone", "read"));
        permissions.add(new PropertyPermission("testng.default.verbose", "read"));
        permissions.add(new ReflectPermission("suppressAccessChecks"));
        permissions.add(new FilePermission("<<ALL FILES>>", "execute"));
    }

    public TestPolicy(Permission... ps) {
        setBasicPermissions();
        Arrays.stream(ps).forEach(p -> permissions.add(p));
    }

    @Override
    public PermissionCollection getPermissions(ProtectionDomain domain) {
        return permissions;
    }

    @Override
    public PermissionCollection getPermissions(CodeSource codesource) {
        return permissions;
    }

    @Override
    public boolean implies(ProtectionDomain domain, Permission perm) {
        return permissions.implies(perm);
    }
}