Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/build/AbsPathsInImage.java
41140 views
1
/*
2
* Copyright (c) 2019, 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
import java.io.IOException;
25
import java.io.InputStream;
26
import java.nio.file.FileVisitResult;
27
import java.nio.file.Files;
28
import java.nio.file.Path;
29
import java.nio.file.Paths;
30
import java.nio.file.SimpleFileVisitor;
31
import java.nio.file.attribute.BasicFileAttributes;
32
import java.util.ArrayList;
33
import java.util.List;
34
import java.util.Properties;
35
import java.util.stream.Collectors;
36
import java.util.zip.ZipEntry;
37
import java.util.zip.ZipInputStream;
38
39
/*
40
* @test
41
* @bug 8226346
42
* @summary Check all output files for absolute path fragments
43
* @requires !vm.debug
44
* @run main AbsPathsInImage
45
*/
46
public class AbsPathsInImage {
47
48
// Set this property on command line to scan an alternate dir or file:
49
// JTREG=JAVA_OPTIONS=-Djdk.test.build.AbsPathInImage.dir=/path/to/dir
50
public static final String DIR_PROPERTY = "jdk.test.build.AbsPathsInImage.dir";
51
private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().contains("windows");
52
53
private boolean matchFound = false;
54
55
public static void main(String[] args) throws Exception {
56
String jdkPathString = System.getProperty("test.jdk");
57
Path jdkHome = Paths.get(jdkPathString);
58
59
Path dirToScan = jdkHome;
60
String overrideDir = System.getProperty(DIR_PROPERTY);
61
if (overrideDir != null) {
62
dirToScan = Paths.get(overrideDir);
63
}
64
65
String buildWorkspaceRoot = null;
66
String buildOutputRoot = null;
67
String testImageDirString = System.getenv("TEST_IMAGE_DIR");
68
if (testImageDirString != null) {
69
Path testImageDir = Paths.get(testImageDirString);
70
Path buildInfoPropertiesFile = testImageDir.resolve("build-info.properties");
71
System.out.println("Getting patterns from " + buildInfoPropertiesFile.toString());
72
Properties buildInfoProperties = new Properties();
73
try (InputStream inStream = Files.newInputStream(buildInfoPropertiesFile)) {
74
buildInfoProperties.load(inStream);
75
}
76
buildWorkspaceRoot = buildInfoProperties.getProperty("build.workspace.root");
77
buildOutputRoot = buildInfoProperties.getProperty("build.output.root");
78
} else {
79
System.out.println("Getting patterns from local environment");
80
// Try to resolve the workspace root based on the jtreg test root dir
81
String testRootDirString = System.getProperty("test.root");
82
if (testRootDirString != null) {
83
Path testRootDir = Paths.get(testRootDirString);
84
// Remove /test/jdk suffix
85
buildWorkspaceRoot = testRootDir.getParent().getParent().toString();
86
}
87
// Remove /jdk
88
Path buildOutputRootPath = jdkHome.getParent();
89
if (buildOutputRootPath.endsWith("images")) {
90
buildOutputRootPath = buildOutputRootPath.getParent();
91
}
92
buildOutputRoot = buildOutputRootPath.toString();
93
}
94
if (buildWorkspaceRoot == null) {
95
throw new Error("Could not find workspace root, test cannot run");
96
}
97
if (buildOutputRoot == null) {
98
throw new Error("Could not find build output root, test cannot run");
99
}
100
101
List<byte[]> searchPatterns = new ArrayList<>();
102
expandPatterns(searchPatterns, buildWorkspaceRoot);
103
expandPatterns(searchPatterns, buildOutputRoot);
104
105
System.out.println("Looking for:");
106
for (byte[] searchPattern : searchPatterns) {
107
System.out.println(new String(searchPattern));
108
}
109
System.out.println();
110
111
AbsPathsInImage absPathsInImage = new AbsPathsInImage();
112
absPathsInImage.scanFiles(dirToScan, searchPatterns);
113
114
if (absPathsInImage.matchFound) {
115
throw new Exception("Test failed");
116
}
117
}
118
119
/**
120
* Add path pattern to list of patterns to search for. Create all possible
121
* variants depending on platform.
122
*/
123
private static void expandPatterns(List<byte[]> searchPatterns, String pattern) {
124
if (IS_WINDOWS) {
125
String forward = pattern.replace('\\', '/');
126
String back = pattern.replace('/', '\\');
127
if (pattern.charAt(1) == ':') {
128
String forwardUpper = String.valueOf(pattern.charAt(0)).toUpperCase() + forward.substring(1);
129
String forwardLower = String.valueOf(pattern.charAt(0)).toLowerCase() + forward.substring(1);
130
String backUpper = String.valueOf(pattern.charAt(0)).toUpperCase() + back.substring(1);
131
String backLower = String.valueOf(pattern.charAt(0)).toLowerCase() + back.substring(1);
132
searchPatterns.add(forwardUpper.getBytes());
133
searchPatterns.add(forwardLower.getBytes());
134
searchPatterns.add(backUpper.getBytes());
135
searchPatterns.add(backLower.getBytes());
136
} else {
137
searchPatterns.add(forward.getBytes());
138
searchPatterns.add(back.getBytes());
139
}
140
} else {
141
searchPatterns.add(pattern.getBytes());
142
}
143
}
144
145
private void scanFiles(Path root, List<byte[]> searchPatterns) throws IOException {
146
Files.walkFileTree(root, new SimpleFileVisitor<>() {
147
@Override
148
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
149
String fileName = file.toString();
150
if (Files.isSymbolicLink(file)) {
151
return super.visitFile(file, attrs);
152
} else if (fileName.endsWith(".debuginfo") || fileName.endsWith(".pdb")) {
153
// Do nothing
154
} else if (fileName.endsWith(".zip")) {
155
scanZipFile(file, searchPatterns);
156
} else {
157
scanFile(file, searchPatterns);
158
}
159
return super.visitFile(file, attrs);
160
}
161
});
162
}
163
164
private void scanFile(Path file, List<byte[]> searchPatterns) throws IOException {
165
List<String> matches = scanBytes(Files.readAllBytes(file), searchPatterns);
166
if (matches.size() > 0) {
167
matchFound = true;
168
System.out.println(file + ":");
169
for (String match : matches) {
170
System.out.println(match);
171
}
172
System.out.println();
173
}
174
}
175
176
private void scanZipFile(Path zipFile, List<byte[]> searchPatterns) throws IOException {
177
try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(zipFile))) {
178
ZipEntry zipEntry;
179
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
180
List<String> matches = scanBytes(zipInputStream.readAllBytes(), searchPatterns);
181
if (matches.size() > 0) {
182
matchFound = true;
183
System.out.println(zipFile + ", " + zipEntry.getName() + ":");
184
for (String match : matches) {
185
System.out.println(match);
186
}
187
System.out.println();
188
}
189
}
190
}
191
}
192
193
private List<String> scanBytes(byte[] data, List<byte[]> searchPatterns) {
194
List<String> matches = new ArrayList<>();
195
for (int i = 0; i < data.length; i++) {
196
for (byte[] searchPattern : searchPatterns) {
197
boolean found = true;
198
for (int j = 0; j < searchPattern.length; j++) {
199
if ((i + j >= data.length || data[i + j] != searchPattern[j])) {
200
found = false;
201
break;
202
}
203
}
204
if (found) {
205
matches.add(new String(data, charsStart(data, i), charsOffset(data, i, searchPattern.length)));
206
// No need to search the same string for multiple patterns
207
break;
208
}
209
}
210
}
211
return matches;
212
}
213
214
private int charsStart(byte[] data, int startIndex) {
215
int index = startIndex;
216
while (--index > 0) {
217
byte datum = data[index];
218
if (datum < 32 || datum > 126) {
219
break;
220
}
221
}
222
return index + 1;
223
}
224
225
private int charsOffset(byte[] data, int startIndex, int startOffset) {
226
int offset = startOffset;
227
while (startIndex + ++offset < data.length) {
228
byte datum = data[startIndex + offset];
229
if (datum < 32 || datum > 126) {
230
break;
231
}
232
}
233
return offset;
234
}
235
}
236
237