author | chegar |
Fri, 16 Feb 2018 15:06:29 +0000 | |
branch | http-client-branch |
changeset 56138 | 4f92b988600e |
parent 56089 | 42208b2f224e |
child 56167 | 96fa4f49a9ff |
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 |
|
27 |
* @run testng/othervm/java.security.policy=httpclient.policy FileProcessorPermissionTest |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.FilePermission; |
|
31 |
import java.nio.file.Path; |
|
32 |
import java.nio.file.Paths; |
|
33 |
import java.security.AccessControlContext; |
|
34 |
import java.security.AccessController; |
|
35 |
import java.security.Permission; |
|
36 |
import java.security.Permissions; |
|
37 |
import java.security.PrivilegedActionException; |
|
38 |
import java.security.PrivilegedExceptionAction; |
|
39 |
import java.security.ProtectionDomain; |
|
40 |
import java.util.List; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
48083
diff
changeset
|
41 |
import java.net.http.HttpRequest; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
48083
diff
changeset
|
42 |
import java.net.http.HttpResponse; |
48083 | 43 |
import org.testng.annotations.Test; |
44 |
import static java.nio.file.StandardOpenOption.*; |
|
45 |
import static org.testng.Assert.*; |
|
46 |
||
47 |
public class FileProcessorPermissionTest { |
|
48 |
||
49 |
static final String testSrc = System.getProperty("test.src", "."); |
|
50 |
static final Path fromFilePath = Paths.get(testSrc, "FileProcessorPermissionTest.java"); |
|
51 |
static final Path asFilePath = Paths.get(testSrc, "asFile.txt"); |
|
52 |
static final Path CWD = Paths.get("."); |
|
53 |
static final Class<SecurityException> SE = SecurityException.class; |
|
54 |
||
55 |
static AccessControlContext withPermissions(Permission... perms) { |
|
56 |
Permissions p = new Permissions(); |
|
57 |
for (Permission perm : perms) { |
|
58 |
p.add(perm); |
|
59 |
} |
|
60 |
ProtectionDomain pd = new ProtectionDomain(null, p); |
|
61 |
return new AccessControlContext(new ProtectionDomain[]{ pd }); |
|
62 |
} |
|
63 |
||
64 |
static AccessControlContext noPermissions() { |
|
65 |
return withPermissions(/*empty*/); |
|
66 |
} |
|
67 |
||
68 |
@Test |
|
69 |
public void test() throws Exception { |
|
70 |
List<PrivilegedExceptionAction<?>> list = List.of( |
|
71 |
() -> HttpRequest.BodyPublisher.fromFile(fromFilePath), |
|
72 |
||
73 |
() -> HttpResponse.BodyHandler.asFile(asFilePath), |
|
74 |
() -> HttpResponse.BodyHandler.asFile(asFilePath, CREATE), |
|
75 |
() -> HttpResponse.BodyHandler.asFile(asFilePath, CREATE, WRITE), |
|
76 |
||
77 |
() -> HttpResponse.BodyHandler.asFileDownload(CWD), |
|
78 |
() -> HttpResponse.BodyHandler.asFileDownload(CWD, CREATE), |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56089
diff
changeset
|
79 |
() -> HttpResponse.BodyHandler.asFileDownload(CWD, CREATE, WRITE) |
48083 | 80 |
); |
81 |
||
82 |
// sanity, just run http ( no security manager ) |
|
83 |
System.setSecurityManager(null); |
|
84 |
try { |
|
85 |
for (PrivilegedExceptionAction pa : list) { |
|
86 |
AccessController.doPrivileged(pa); |
|
87 |
} |
|
88 |
} finally { |
|
89 |
System.setSecurityManager(new SecurityManager()); |
|
90 |
} |
|
91 |
||
92 |
// Run with all permissions, i.e. no further restrictions than test's AllPermission |
|
93 |
for (PrivilegedExceptionAction pa : list) { |
|
94 |
try { |
|
95 |
assert System.getSecurityManager() != null; |
|
96 |
AccessController.doPrivileged(pa, null, new Permission[] { }); |
|
97 |
} catch (PrivilegedActionException pae) { |
|
98 |
fail("UNEXPECTED Exception:" + pae); |
|
99 |
pae.printStackTrace(); |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
// Run with limited permissions, i.e. just what is required |
|
104 |
AccessControlContext minimalACC = withPermissions( |
|
105 |
new FilePermission(fromFilePath.toString() , "read"), |
|
106 |
new FilePermission(asFilePath.toString(), "read,write,delete"), |
|
107 |
new FilePermission(CWD.toString(), "read,write,delete") |
|
108 |
); |
|
109 |
for (PrivilegedExceptionAction pa : list) { |
|
110 |
try { |
|
111 |
assert System.getSecurityManager() != null; |
|
112 |
AccessController.doPrivileged(pa, minimalACC); |
|
113 |
} catch (PrivilegedActionException pae) { |
|
114 |
fail("UNEXPECTED Exception:" + pae); |
|
115 |
pae.printStackTrace(); |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
// Run with NO permissions, i.e. expect SecurityException |
|
120 |
for (PrivilegedExceptionAction pa : list) { |
|
121 |
try { |
|
122 |
assert System.getSecurityManager() != null; |
|
123 |
AccessController.doPrivileged(pa, noPermissions()); |
|
124 |
fail("EXPECTED SecurityException"); |
|
125 |
} catch (SecurityException expected) { |
|
126 |
System.out.println("Caught expected SE:" + expected); |
|
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
} |