Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/FilePermission/Correctness.java
41149 views
1
/*
2
* Copyright (c) 2016, 2017, 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
*
26
* @test
27
* @bug 8164705
28
* @modules java.base/java.io:open
29
* @summary Remove pathname canonicalization from FilePermission
30
*/
31
32
import java.io.FilePermission;
33
import java.lang.reflect.Method;
34
import java.nio.file.Path;
35
import java.nio.file.Paths;
36
37
public class Correctness {
38
39
static boolean err = false;
40
static Method containsMethod;
41
static boolean isWindows =
42
System.getProperty("os.name").contains("Windows");
43
public static void main(String args[]) throws Exception {
44
check("/", "/");
45
checkNo("/", "/x");
46
checkNo("/", "/../x");
47
48
checkNo("/", "x");
49
50
check("/-", "/*");
51
checkNo("/*", "/-");
52
53
check("/*", "/x");
54
check("/-", "/x");
55
check("/-", "/x/*");
56
check("/-", "/x/-");
57
check("/-", "/x/y");
58
checkNo("/*", "/x/y");
59
check("/x/*", "/x/x");
60
checkNo("/x/-", "/x");
61
checkNo("/x/*", "/x");
62
check("/x/-", "/x/x");
63
check("/x/-", "/x/x/y");
64
checkNo("/x/*", "/x/x/y");
65
checkNo("/x/*", "/x");
66
67
check("*", "x");
68
checkNo("", "x");
69
check("-", "x");
70
check("-", "*");
71
check("-", "a/-");
72
check("-", "a/*");
73
checkNo("*", "a/b");
74
check("a/*", "a/b");
75
check("a/-", "a/*");
76
check("a/-", "a/b/c");
77
checkNo("a/*", "a/b/c");
78
79
check("../", "../");
80
check("../-", "../*");
81
check("../../*", "../../a");
82
83
// If we allow .. and abs/rel checks
84
check("../-", "a");
85
check("../../-", "-");
86
checkNo("../../*", "a");
87
//check("/-", "a");
88
//checkNo("/*", "a");
89
//check("/-", "-");
90
91
try {
92
containsMethod = FilePermission.class.getDeclaredMethod(
93
"containsPath", Path.class, Path.class);
94
containsMethod.setAccessible(true);
95
System.out.println();
96
97
// The 1st 2 args of contains() must be normalized paths.
98
// When FilePermission::containsPath is called by implies,
99
// paths have already been normalized.
100
contains("x", "x", 0);
101
contains("x", "x/y", 1);
102
contains("x", "x/y/z", 2);
103
contains("x", "y", -1);
104
contains("x", "", -1);
105
contains("", "", 0);
106
contains("", "x", 1);
107
contains("", "x/y", 2);
108
contains("/", "/", 0);
109
contains("/", "/x", 1);
110
contains("/", "/x/y", 2);
111
contains("/x", "/x/y", 1);
112
contains("/x", "/y", -1);
113
//contains("/", "..", Integer.MAX_VALUE);
114
//contains("/", "x", Integer.MAX_VALUE);
115
//contains("/", "x/y", Integer.MAX_VALUE);
116
//contains("/", "../x", Integer.MAX_VALUE);
117
contains("/x", "y", -1);
118
contains("x", "/y", -1);
119
120
contains("", "..", -1);
121
contains("", "../x", -1);
122
contains("..", "", 1);
123
contains("..", "x", 2);
124
contains("..", "x/y", 3);
125
contains("../x", "x", -1);
126
contains("../x", "y", -1);
127
contains("../x", "../x/y", 1);
128
contains("../../x", "../../x/y", 1);
129
contains("../../../x", "../../../x/y", 1);
130
contains("../x", "../y", -1);
131
} catch (NoSuchMethodException e) {
132
// Ignored
133
}
134
if (err) throw new Exception("Failed.");
135
}
136
137
// Checks if s2 is inside s1 and depth is expected.
138
static void contains(String s1, String s2, int expected) throws Exception {
139
contains0(s1, s2, expected);
140
if (isWindows) {
141
contains0("C:" + s1, s2, -1);
142
contains0(s1, "C:" + s2, -1);
143
contains0("C:" + s1, "D:" + s2, -1);
144
contains0("C:" + s1, "C:" + s2, expected);
145
}
146
}
147
148
static void contains0(String s1, String s2, int expected) throws Exception {
149
Path p1 = Paths.get(s1);
150
Path p2 = Paths.get(s2);
151
int d = (int)containsMethod.invoke(null, p1, p2);
152
Path p;
153
try {
154
p = p2.relativize(p1);
155
} catch (Exception e) {
156
p = null;
157
}
158
System.out.printf("%-20s -> %-20s: %20s %5d %5d %s\n", s1, s2, p,
159
d, expected, d==expected?"":" WRONG");
160
if (d != expected) {
161
err = true;
162
}
163
}
164
165
static void check0(String s1, String s2, boolean expected) {
166
FilePermission fp1 = new FilePermission(s1, "read");
167
FilePermission fp2 = new FilePermission(s2, "read");
168
boolean b = fp1.implies(fp2);
169
System.out.printf("%-30s -> %-30s: %5b %s\n",
170
s1, s2, b, b==expected?"":" WRONG");
171
if (b != expected) {
172
err = true;
173
System.out.println(fp1);
174
System.out.println(fp2);
175
}
176
}
177
178
static void check(String s1, String s2, boolean expected) {
179
check0(s1, s2, expected);
180
if (isWindows) {
181
check0("C:" + s1, s2, false);
182
check0(s1, "C:" + s2, false);
183
check0("C:" + s1, "D:" + s2, false);
184
check0("C:" + s1, "C:" + s2, expected);
185
}
186
}
187
188
static void check(String s1, String s2) {
189
check(s1, s2, true);
190
}
191
192
static void checkNo(String s1, String s2) {
193
check(s1, s2, false);
194
}
195
}
196
197