Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/CachePremissionsTest/CachePermissionsTest.java
41152 views
1
/*
2
* Copyright (c) 2009, 2010, 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 6684104
27
* @summary Test verifies that ImageIO checks all permissions required for
28
* the file cache usage:
29
*
30
* no policy file: No security restrictions.
31
* Expected result: ImageIO creates file-cached stream.
32
*
33
* w.policy: the case when we have read and write permissions
34
* for java.io.temp directory but have only write permission
35
* for a temp file.
36
* Expected result: ImageIO create a memory-cached stream
37
* image output stream.
38
*
39
* rw.policy: the case when we have read and write permissions
40
* for java.io.temp directory but have only read and write
41
* permission for a temp cache file.
42
* Expected result: ImageIO creates a memory-cached stream
43
* because temporary cache file can not be deleted.
44
*
45
* rwd.policy: the case when we have read and write permissions
46
* for java.io.temp directory and have all required permissions
47
* (read, write, and delete) for a temporary cache file.
48
* Expected result: ImageIO creates file-cached stream.
49
*
50
* -Djava.security.debug=access can be used to verify file permissions.
51
*
52
* @run main CachePermissionsTest true
53
* @run main/othervm -Djava.security.manager=allow CachePermissionsTest false w.policy
54
* @run main/othervm -Djava.security.manager=allow CachePermissionsTest false rw.policy
55
* @run main/othervm -Djava.security.manager=allow CachePermissionsTest true rwd.policy
56
*/
57
58
import java.io.File;
59
import java.io.IOException;
60
import java.io.ByteArrayOutputStream;
61
import javax.imageio.stream.ImageOutputStream;
62
63
import javax.imageio.ImageIO;
64
65
66
public class CachePermissionsTest {
67
public static void main(String[] args) {
68
boolean isFileCacheExpected =
69
Boolean.valueOf(args[0]).booleanValue();
70
System.out.println("Is file cache expected: " + isFileCacheExpected);
71
72
ImageIO.setUseCache(true);
73
74
System.out.println("java.io.tmpdir is " + System.getProperty("java.io.tmpdir"));
75
76
if (args.length > 1) {
77
String testsrc = System.getProperty("test.src", ".");
78
String policy = testsrc + File.separator + args[1];
79
80
System.out.println("Policy file: " + policy);
81
System.setProperty("java.security.policy", policy);
82
83
System.out.println("Install security manager...");
84
System.setSecurityManager(new SecurityManager());
85
}
86
87
ByteArrayOutputStream baos = new ByteArrayOutputStream();
88
89
try {
90
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
91
92
boolean isFileCache = ios.isCachedFile();
93
System.out.println("Is file cache used: " + isFileCache);
94
95
if (isFileCache !=isFileCacheExpected) {
96
System.out.println("WARNING: file chace usage is not as expected!");
97
}
98
99
System.out.println("Verify data writing...");
100
for (int i = 0; i < 8192; i++) {
101
ios.writeInt(i);
102
}
103
104
System.out.println("Verify data reading...");
105
ios.seek(0L);
106
107
for (int i = 0; i < 8192; i++) {
108
int j = ios.readInt();
109
if (i != j) {
110
throw new RuntimeException("Wrong data in the stream " + j + " instead of " + i);
111
}
112
}
113
114
System.out.println("Verify stream closing...");
115
ios.close();
116
} catch (IOException e) {
117
/*
118
* Something went wrong?
119
*/
120
throw new RuntimeException("Test FAILED.", e);
121
} catch (SecurityException e) {
122
/*
123
* We do not expect security execptions here:
124
* we there are any security restrition, ImageIO
125
* should swith to memory-cached streams, instead
126
* of using file cache.
127
*/
128
throw new RuntimeException("Test FAILED.", e);
129
}
130
}
131
}
132
133