Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/FilePublisher/FilePublisherPermsTest.java
41153 views
1
/*
2
* Copyright (c) 2020, 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
/*
25
* @test
26
* @bug 8235459
27
* @summary Confirm that HttpRequest.BodyPublishers#ofFile(Path)
28
* works with changing permissions
29
* policy 1: no custom permission
30
* policy 2: custom permission for test classes
31
* policy 3: custom permission for test classes and httpclient
32
* @modules java.base/sun.net.www.http
33
* java.net.http/jdk.internal.net.http.common
34
* java.net.http/jdk.internal.net.http.frame
35
* java.net.http/jdk.internal.net.http.hpack
36
* jdk.httpserver
37
* @library /test/lib ../http2/server
38
* @compile ../HttpServerAdapters.java
39
* @build jdk.test.lib.net.SimpleSSLContext SecureZipFSProvider
40
* @run testng/othervm/java.security.policy=FilePublisherPermsTest1.policy FilePublisherPermsTest
41
* @run testng/othervm/java.security.policy=FilePublisherPermsTest2.policy FilePublisherPermsTest
42
* @run testng/othervm/java.security.policy=FilePublisherPermsTest3.policy FilePublisherPermsTest
43
*/
44
45
import com.sun.net.httpserver.HttpServer;
46
import com.sun.net.httpserver.HttpsConfigurator;
47
import com.sun.net.httpserver.HttpsServer;
48
import jdk.test.lib.net.SimpleSSLContext;
49
import org.testng.annotations.AfterTest;
50
import org.testng.annotations.BeforeTest;
51
import org.testng.annotations.DataProvider;
52
import org.testng.annotations.Test;
53
54
import javax.net.ssl.SSLContext;
55
import java.io.FileNotFoundException;
56
import java.io.FilePermission;
57
import java.io.IOException;
58
import java.io.InputStream;
59
import java.io.OutputStream;
60
import java.net.InetAddress;
61
import java.net.InetSocketAddress;
62
import java.net.URI;
63
import java.net.http.HttpClient;
64
import java.net.http.HttpRequest;
65
import java.net.http.HttpRequest.BodyPublisher;
66
import java.net.http.HttpRequest.BodyPublishers;
67
import java.net.http.HttpResponse;
68
import java.nio.file.FileSystem;
69
import java.nio.file.FileSystems;
70
import java.nio.file.Files;
71
import java.nio.file.Path;
72
import java.security.*;
73
import java.util.Map;
74
75
import static java.lang.System.out;
76
import static java.net.http.HttpClient.Builder.NO_PROXY;
77
import static org.testng.Assert.assertEquals;
78
import static org.testng.Assert.fail;
79
80
public class FilePublisherPermsTest implements HttpServerAdapters {
81
82
SSLContext sslContext;
83
HttpServerAdapters.HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ]
84
HttpServerAdapters.HttpTestServer httpsTestServer; // HTTPS/1.1
85
HttpServerAdapters.HttpTestServer http2TestServer; // HTTP/2 ( h2c )
86
HttpServerAdapters.HttpTestServer https2TestServer; // HTTP/2 ( h2 )
87
String httpURI;
88
String httpsURI;
89
String http2URI;
90
String https2URI;
91
92
FileSystem zipFs;
93
static Path zipFsPath;
94
static Path defaultFsPath;
95
96
String policyFile;
97
98
// Default file system set up
99
static final String DEFAULT_FS_MSG = "default fs";
100
101
private Path defaultFsFile() throws Exception {
102
var file = Path.of("defaultFile.txt");
103
if (Files.notExists(file)) {
104
Files.createFile(file);
105
Files.writeString(file, DEFAULT_FS_MSG);
106
}
107
assertEquals(Files.readString(file), DEFAULT_FS_MSG);
108
return file;
109
}
110
111
@DataProvider(name = "defaultFsData")
112
public Object[][] defaultFsData() {
113
return new Object[][]{
114
{ httpURI, defaultFsPath },
115
{ httpsURI, defaultFsPath },
116
{ http2URI, defaultFsPath },
117
{ https2URI, defaultFsPath },
118
{ httpURI, defaultFsPath },
119
{ httpsURI, defaultFsPath },
120
{ http2URI, defaultFsPath },
121
{ https2URI, defaultFsPath },
122
};
123
}
124
125
@Test(dataProvider = "defaultFsData")
126
public void testDefaultFs(String uriString, Path path)
127
throws Exception {
128
out.printf("\n\n--- testDefaultFs(%s, %s): starting\n",
129
uriString, path);
130
131
if (System.getSecurityManager() != null) {
132
changePerms(path.toString(), "read,write,delete");
133
// Should not throw
134
BodyPublisher bodyPublisher = BodyPublishers.ofFile(path);
135
// Restrict permissions
136
changePerms(path.toString(), "delete");
137
try {
138
BodyPublishers.ofFile(path);
139
fail();
140
} catch (SecurityException e) {
141
out.println("Caught expected: " + e);
142
}
143
try {
144
send(uriString, bodyPublisher);
145
fail();
146
} catch (SecurityException e) {
147
out.println("Caught expected: " + e);
148
}
149
}
150
}
151
152
// Zip File system set up
153
static final String ZIP_FS_MSG = "zip fs";
154
155
static FileSystem newZipFs(Path zipFile) throws Exception {
156
return FileSystems.newFileSystem(zipFile, Map.of("create", "true"));
157
}
158
159
static FileSystem newSecureZipFs(Path zipFile) throws Exception {
160
FileSystem fs = newZipFs(zipFile);
161
return new SecureZipFSProvider(fs.provider()).newFileSystem(fs);
162
}
163
164
static Path zipFsFile(FileSystem fs) throws Exception {
165
var file = fs.getPath("fileInZip.txt");
166
if (Files.notExists(file)) {
167
Files.createFile(file);
168
Files.writeString(file, ZIP_FS_MSG);
169
}
170
assertEquals(Files.readString(file), ZIP_FS_MSG);
171
return file;
172
}
173
174
@DataProvider(name = "zipFsData")
175
public Object[][] zipFsData() {
176
return new Object[][]{
177
{ httpURI, zipFsPath },
178
{ httpsURI, zipFsPath },
179
{ http2URI, zipFsPath },
180
{ https2URI, zipFsPath },
181
{ httpURI, zipFsPath },
182
{ httpsURI, zipFsPath },
183
{ http2URI, zipFsPath },
184
{ https2URI, zipFsPath },
185
};
186
}
187
188
@Test(dataProvider = "zipFsData")
189
public void testZipFs(String uriString, Path path) throws Exception {
190
out.printf("\n\n--- testZipFsCustomPerm(%s, %s): starting\n", uriString, path);
191
if (System.getSecurityManager() != null) {
192
changePerms(path.toString(), "read,write,delete");
193
194
// Custom permission not sufficiently granted, expected to fail
195
if (!policyFile.contains("FilePublisherPermsTest3")) {
196
try {
197
BodyPublishers.ofFile(path);
198
fail();
199
} catch (SecurityException e) {
200
out.println("Caught expected: " + e);
201
return;
202
}
203
} else {
204
BodyPublisher bodyPublisher = BodyPublishers.ofFile(path);
205
send(uriString, bodyPublisher);
206
// Restrict permissions
207
changePerms(path.toString(), "delete");
208
try {
209
BodyPublishers.ofFile(path);
210
fail();
211
} catch (SecurityException e) {
212
out.println("Caught expected: " + e);
213
}
214
try {
215
send(uriString, bodyPublisher);
216
fail();
217
} catch (SecurityException e) {
218
out.println("Caught expected: " + e);
219
}
220
}
221
}
222
}
223
224
@Test
225
public void testFileNotFound() throws Exception {
226
out.printf("\n\n--- testFileNotFound(): starting\n");
227
var zipPath = Path.of("fileNotFound.zip");
228
changePerms(zipPath.toString(), "read,write,delete");
229
try (FileSystem fs = newZipFs(zipPath)) {
230
Path fileInZip = zipFsFile(fs);
231
Files.deleteIfExists(fileInZip);
232
BodyPublishers.ofFile(fileInZip);
233
fail();
234
} catch (FileNotFoundException e) {
235
out.println("Caught expected: " + e);
236
}
237
var path = Path.of("fileNotFound.txt");
238
changePerms(path.toString(), "read,write,delete");
239
try {
240
Files.deleteIfExists(path);
241
BodyPublishers.ofFile(path);
242
fail();
243
} catch (FileNotFoundException e) {
244
out.println("Caught expected: " + e);
245
}
246
}
247
248
private void send(String uriString, BodyPublisher bodyPublisher)
249
throws Exception {
250
HttpClient client = HttpClient.newBuilder()
251
.proxy(NO_PROXY)
252
.sslContext(sslContext)
253
.build();
254
var req = HttpRequest.newBuilder(URI.create(uriString))
255
.POST(bodyPublisher)
256
.build();
257
client.send(req, HttpResponse.BodyHandlers.discarding());
258
}
259
260
private void changePerms(String path, String actions) {
261
Policy.setPolicy(new CustomPolicy(
262
new FilePermission(path, actions)
263
));
264
}
265
266
static class CustomPolicy extends Policy {
267
static final Policy DEFAULT_POLICY = Policy.getPolicy();
268
final PermissionCollection perms = new Permissions();
269
270
CustomPolicy(Permission... permissions) {
271
java.util.Arrays.stream(permissions).forEach(perms::add);
272
}
273
274
public PermissionCollection getPermissions(ProtectionDomain domain) {
275
return perms;
276
}
277
278
public PermissionCollection getPermissions(CodeSource codesource) {
279
return perms;
280
}
281
282
public boolean implies(ProtectionDomain domain, Permission perm) {
283
// Ignore any existing permissions for test files
284
return perm.getName().equals(defaultFsPath.toString())
285
|| perm.getName().equals(zipFsPath.toString())
286
? perms.implies(perm)
287
: perms.implies(perm) || DEFAULT_POLICY.implies(domain, perm);
288
}
289
}
290
291
static class HttpEchoHandler implements HttpServerAdapters.HttpTestHandler {
292
@Override
293
public void handle(HttpServerAdapters.HttpTestExchange t) throws IOException {
294
try (InputStream is = t.getRequestBody();
295
OutputStream os = t.getResponseBody()) {
296
byte[] bytes = is.readAllBytes();
297
t.sendResponseHeaders(200, bytes.length);
298
os.write(bytes);
299
}
300
}
301
}
302
303
@BeforeTest
304
public void setup() throws Exception {
305
policyFile = System.getProperty("java.security.policy");
306
out.println(policyFile);
307
308
sslContext = new SimpleSSLContext().get();
309
if (sslContext == null)
310
throw new AssertionError("Unexpected null sslContext");
311
312
zipFs = newSecureZipFs(Path.of("file.zip"));
313
zipFsPath = zipFsFile(zipFs);
314
defaultFsPath = defaultFsFile();
315
316
InetSocketAddress sa =
317
new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
318
319
httpTestServer = HttpServerAdapters.HttpTestServer.of(HttpServer.create(sa, 0));
320
httpTestServer.addHandler(
321
new FilePublisherPermsTest.HttpEchoHandler(), "/http1/echo");
322
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/echo";
323
324
HttpsServer httpsServer = HttpsServer.create(sa, 0);
325
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
326
httpsTestServer = HttpServerAdapters.HttpTestServer.of(httpsServer);
327
httpsTestServer.addHandler(
328
new FilePublisherPermsTest.HttpEchoHandler(), "/https1/echo");
329
httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/echo";
330
331
http2TestServer = HttpServerAdapters.HttpTestServer.of(
332
new Http2TestServer("localhost", false, 0));
333
http2TestServer.addHandler(
334
new FilePublisherPermsTest.HttpEchoHandler(), "/http2/echo");
335
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/echo";
336
337
https2TestServer = HttpServerAdapters.HttpTestServer.of(
338
new Http2TestServer("localhost", true, sslContext));
339
https2TestServer.addHandler(
340
new FilePublisherPermsTest.HttpEchoHandler(), "/https2/echo");
341
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/echo";
342
343
httpTestServer.start();
344
httpsTestServer.start();
345
http2TestServer.start();
346
https2TestServer.start();
347
}
348
349
@AfterTest
350
public void teardown() throws Exception {
351
httpTestServer.stop();
352
httpsTestServer.stop();
353
http2TestServer.stop();
354
https2TestServer.stop();
355
zipFs.close();
356
}
357
}
358
359