author | chegar |
Wed, 07 Mar 2018 14:06:39 +0000 | |
branch | http-client-branch |
changeset 56257 | 82a9340bdda6 |
parent 56167 | 96fa4f49a9ff |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56089
diff
changeset
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
48083 | 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 |
/* |
|
25 |
* @test |
|
26 |
* @summary Basic checks for SecurityException from body processors APIs |
|
56257
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
27 |
* @run testng/othervm/java.security.policy=allpermissions.policy FileProcessorPermissionTest |
48083 | 28 |
*/ |
29 |
||
56257
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
30 |
import java.io.File; |
48083 | 31 |
import java.io.FilePermission; |
32 |
import java.nio.file.Path; |
|
33 |
import java.nio.file.Paths; |
|
34 |
import java.security.AccessControlContext; |
|
35 |
import java.security.AccessController; |
|
36 |
import java.security.Permission; |
|
37 |
import java.security.Permissions; |
|
38 |
import java.security.PrivilegedActionException; |
|
39 |
import java.security.PrivilegedExceptionAction; |
|
40 |
import java.security.ProtectionDomain; |
|
41 |
import java.util.List; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
48083
diff
changeset
|
42 |
import java.net.http.HttpRequest; |
56257
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
43 |
import java.net.http.HttpRequest.BodyPublishers; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
48083
diff
changeset
|
44 |
import java.net.http.HttpResponse; |
56257
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
45 |
import java.net.http.HttpResponse.BodyHandlers; |
48083 | 46 |
import org.testng.annotations.Test; |
47 |
import static java.nio.file.StandardOpenOption.*; |
|
48 |
import static org.testng.Assert.*; |
|
49 |
||
50 |
public class FileProcessorPermissionTest { |
|
51 |
||
52 |
static final String testSrc = System.getProperty("test.src", "."); |
|
53 |
static final Path fromFilePath = Paths.get(testSrc, "FileProcessorPermissionTest.java"); |
|
54 |
static final Path asFilePath = Paths.get(testSrc, "asFile.txt"); |
|
55 |
static final Path CWD = Paths.get("."); |
|
56 |
static final Class<SecurityException> SE = SecurityException.class; |
|
57 |
||
58 |
static AccessControlContext withPermissions(Permission... perms) { |
|
59 |
Permissions p = new Permissions(); |
|
60 |
for (Permission perm : perms) { |
|
61 |
p.add(perm); |
|
62 |
} |
|
63 |
ProtectionDomain pd = new ProtectionDomain(null, p); |
|
64 |
return new AccessControlContext(new ProtectionDomain[]{ pd }); |
|
65 |
} |
|
66 |
||
67 |
static AccessControlContext noPermissions() { |
|
68 |
return withPermissions(/*empty*/); |
|
69 |
} |
|
70 |
||
71 |
@Test |
|
72 |
public void test() throws Exception { |
|
73 |
List<PrivilegedExceptionAction<?>> list = List.of( |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56138
diff
changeset
|
74 |
() -> HttpRequest.BodyPublishers.ofFile(fromFilePath), |
48083 | 75 |
|
56257
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
76 |
() -> BodyHandlers.ofFile(asFilePath), |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
77 |
() -> BodyHandlers.ofFile(asFilePath, CREATE), |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
78 |
() -> BodyHandlers.ofFile(asFilePath, CREATE, WRITE), |
48083 | 79 |
|
56257
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
80 |
() -> BodyHandlers.ofFileDownload(CWD), |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
81 |
() -> BodyHandlers.ofFileDownload(CWD, CREATE), |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
82 |
() -> BodyHandlers.ofFileDownload(CWD, CREATE, WRITE) |
48083 | 83 |
); |
84 |
||
56257
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
85 |
// TEST 1 - sanity, just run ( no security manager ) |
48083 | 86 |
System.setSecurityManager(null); |
87 |
try { |
|
88 |
for (PrivilegedExceptionAction pa : list) { |
|
89 |
AccessController.doPrivileged(pa); |
|
90 |
} |
|
91 |
} finally { |
|
92 |
System.setSecurityManager(new SecurityManager()); |
|
93 |
} |
|
94 |
||
95 |
// Run with all permissions, i.e. no further restrictions than test's AllPermission |
|
96 |
for (PrivilegedExceptionAction pa : list) { |
|
97 |
try { |
|
98 |
assert System.getSecurityManager() != null; |
|
99 |
AccessController.doPrivileged(pa, null, new Permission[] { }); |
|
100 |
} catch (PrivilegedActionException pae) { |
|
101 |
fail("UNEXPECTED Exception:" + pae); |
|
102 |
pae.printStackTrace(); |
|
103 |
} |
|
104 |
} |
|
105 |
||
56257
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
106 |
// TEST 2 - with all file permissions |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
107 |
AccessControlContext allFilesACC = withPermissions( |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
108 |
new FilePermission("<<ALL FILES>>" , "read,write") |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
109 |
); |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
110 |
for (PrivilegedExceptionAction pa : list) { |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
111 |
try { |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
112 |
assert System.getSecurityManager() != null; |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
113 |
AccessController.doPrivileged(pa, allFilesACC); |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
114 |
} catch (PrivilegedActionException pae) { |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
115 |
fail("UNEXPECTED Exception:" + pae); |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
116 |
pae.printStackTrace(); |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
117 |
} |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
118 |
} |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
119 |
|
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
120 |
// TEST 3 - with limited permissions, i.e. just what is required |
48083 | 121 |
AccessControlContext minimalACC = withPermissions( |
122 |
new FilePermission(fromFilePath.toString() , "read"), |
|
56257
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
123 |
new FilePermission(asFilePath.toString(), "write"), |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
124 |
// ofFileDownload requires read and write to the dir |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
125 |
new FilePermission(CWD.toString(), "read,write"), |
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
126 |
new FilePermission(CWD.toString() + File.separator + "*", "read,write") |
48083 | 127 |
); |
128 |
for (PrivilegedExceptionAction pa : list) { |
|
129 |
try { |
|
130 |
assert System.getSecurityManager() != null; |
|
131 |
AccessController.doPrivileged(pa, minimalACC); |
|
132 |
} catch (PrivilegedActionException pae) { |
|
133 |
fail("UNEXPECTED Exception:" + pae); |
|
134 |
pae.printStackTrace(); |
|
135 |
} |
|
136 |
} |
|
137 |
||
56257
82a9340bdda6
http-client-branch: rework file permissions to use limited doPriv
chegar
parents:
56167
diff
changeset
|
138 |
// TEST 4 - with NO permissions, i.e. expect SecurityException |
48083 | 139 |
for (PrivilegedExceptionAction pa : list) { |
140 |
try { |
|
141 |
assert System.getSecurityManager() != null; |
|
142 |
AccessController.doPrivileged(pa, noPermissions()); |
|
143 |
fail("EXPECTED SecurityException"); |
|
144 |
} catch (SecurityException expected) { |
|
145 |
System.out.println("Caught expected SE:" + expected); |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
} |