Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/BigJar.java
38833 views
1
/*
2
* Copyright (c) 2012, 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 7194005
27
* @summary launcher handling of zip64 archives (Scenario A and B)
28
* @compile -XDignore.symbol.file BigJar.java
29
* @run main/timeout=600 BigJar
30
*/
31
/*
32
* This test consists of two scenarios:
33
*
34
* Scenario A: create a jar with entries exceeding 64K, add a main class and
35
* see if the launcher can handle it.
36
*
37
* Scenario A1: create a jar as in A, but add a zipfile comment as well.
38
*
39
* Scenario B: create a jar with a large enough file exceeding 4GB, and
40
* similarly test the launcher. This test can be run optionally by using the
41
* following jtreg option:
42
* "-javaoptions:-DBigJar_testScenarioB=true"
43
* or set
44
* "BigJar_testScenarioB" environment variable.
45
*
46
* Note this test will only run iff all the disk requirements are met at runtime.
47
*/
48
import java.io.BufferedInputStream;
49
import java.io.BufferedOutputStream;
50
import java.io.File;
51
import java.io.FileInputStream;
52
import java.io.FileOutputStream;
53
import java.io.IOException;
54
import java.io.OutputStream;
55
import java.nio.file.Files;
56
import java.nio.file.Path;
57
import java.util.ArrayList;
58
import java.util.List;
59
import java.util.jar.Attributes;
60
import java.util.jar.JarEntry;
61
import java.util.jar.JarOutputStream;
62
import java.util.jar.Manifest;
63
import java.util.zip.CRC32;
64
import java.util.zip.ZipEntry;
65
import java.util.zip.ZipOutputStream;
66
67
public class BigJar extends TestHelper {
68
69
private static final long GIGA = 1024 * 1024 * 1024;
70
private static final int BUFFER_LEN = Short.MAX_VALUE * 2;
71
72
long getCount(long minlength) {
73
return (minlength / BUFFER_LEN) + 1;
74
}
75
76
long computeCRC(long minlength) {
77
CRC32 crc = new CRC32();
78
byte[] buffer = new byte[BUFFER_LEN];
79
long count = getCount(minlength);
80
for (long i = 0; i < count; i++) {
81
crc.update(buffer);
82
}
83
return crc.getValue();
84
}
85
86
long computeCRC(File inFile) throws IOException {
87
byte[] buffer = new byte[8192];
88
CRC32 crc = new CRC32();
89
try (FileInputStream fis = new FileInputStream(inFile);
90
BufferedInputStream bis = new BufferedInputStream(fis)) {
91
int n = bis.read(buffer);
92
while (n > 0) {
93
crc.update(buffer, 0, n);
94
n = bis.read(buffer);
95
}
96
}
97
return crc.getValue();
98
}
99
100
void createLargeFile(OutputStream os, long minlength) throws IOException {
101
byte[] buffer = new byte[BUFFER_LEN];
102
long count = getCount(minlength);
103
for (long i = 0; i < count; i++) {
104
os.write(buffer);
105
}
106
os.flush();
107
}
108
109
Manifest createMainClass(File javaFile) throws IOException {
110
javaFile.delete();
111
List<String> content = new ArrayList<>();
112
content.add("public class " + baseName(javaFile) + "{");
113
content.add("public static void main(String... args) {");
114
content.add("System.out.println(\"Hello World\\n\");");
115
content.add("System.exit(0);");
116
content.add("}");
117
content.add("}");
118
createFile(javaFile, content);
119
compile(javaFile.getName());
120
Manifest manifest = new Manifest();
121
manifest.clear();
122
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
123
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, baseName(javaFile));
124
System.out.println(manifest.getMainAttributes().keySet());
125
System.out.println(manifest.getMainAttributes().values());
126
return manifest;
127
}
128
129
void createJarWithLargeFile(File jarFile, long minlength) throws IOException {
130
File javaFile = new File("Foo.java");
131
Manifest manifest = createMainClass(javaFile);
132
File classFile = getClassFile(javaFile);
133
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), manifest);
134
BufferedOutputStream bos = new BufferedOutputStream(jos);
135
FileInputStream fis = new FileInputStream(classFile);) {
136
jos.setLevel(ZipOutputStream.STORED);
137
jos.setMethod(0);
138
139
JarEntry je = new JarEntry("large.data");
140
je.setCompressedSize(getCount(minlength) * BUFFER_LEN);
141
je.setSize(getCount(minlength) * BUFFER_LEN);
142
je.setCrc(computeCRC(minlength));
143
je.setMethod(ZipEntry.STORED);
144
jos.putNextEntry(je);
145
createLargeFile(bos, minlength);
146
147
je = new JarEntry(classFile.getName());
148
je.setCompressedSize(classFile.length());
149
je.setSize(classFile.length());
150
je.setCrc(computeCRC(classFile));
151
je.setMethod(ZipEntry.STORED);
152
jos.putNextEntry(je);
153
copyStream(fis, bos);
154
bos.flush();
155
jos.closeEntry();
156
}
157
}
158
159
void createLargeJar(File jarFile, String comment) throws IOException {
160
final int MAX = Short.MAX_VALUE * 2 + 10;
161
JarEntry je = null;
162
File javaFile = new File("Foo.java");
163
File classFile = getClassFile(javaFile);
164
Manifest manifest = createMainClass(javaFile);
165
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), manifest);
166
FileInputStream fis = new FileInputStream(classFile)) {
167
jos.setLevel(JarOutputStream.STORED);
168
jos.setMethod(JarOutputStream.STORED);
169
for (int i = 0; i < MAX; i++) {
170
je = new JarEntry("X" + i + ".txt");
171
je.setSize(0);
172
je.setCompressedSize(0);
173
je.setCrc(0);
174
jos.putNextEntry(je);
175
}
176
177
// add a class file
178
je = new JarEntry(classFile.getName());
179
je.setCompressedSize(classFile.length());
180
je.setSize(classFile.length());
181
je.setCrc(computeCRC(classFile));
182
jos.putNextEntry(je);
183
copyStream(fis, jos);
184
jos.closeEntry();
185
if (comment != null) {
186
jos.setComment(comment);
187
}
188
}
189
}
190
191
void testTheJar(File theJar) throws Exception {
192
try {
193
TestResult tr = doExec(javaCmd, "-jar", theJar.getName());
194
tr.checkPositive();
195
if (!tr.testStatus) {
196
System.out.println(tr);
197
throw new Exception("Failed");
198
}
199
} finally {
200
theJar.delete();
201
}
202
}
203
204
// a jar with entries exceeding 64k + a class file for the existential test
205
@Test
206
void testScenarioA() throws Exception {
207
File largeJar = new File("large.jar");
208
createLargeJar(largeJar, null);
209
testTheJar(largeJar);
210
}
211
212
// a jar with entries exceeding 64k and zip comment
213
@Test
214
void testScenarioA1() throws Exception {
215
File largeJar = new File("largewithcomment.jar");
216
createLargeJar(largeJar, "A really large jar with a comment");
217
testTheJar(largeJar);
218
}
219
220
// a jar with an enormous file + a class file for the existential test
221
@Test
222
void testScenarioB() throws Exception {
223
final String testString = "BigJar_testScenarioB";
224
if (Boolean.getBoolean(testString) == false &&
225
System.getenv(testString) == null) {
226
System.out.println("Warning: testScenarioB passes vacuously");
227
return;
228
}
229
final File largeJar = new File("huge.jar");
230
231
final Path path = largeJar.getAbsoluteFile().getParentFile().toPath();
232
final long available = Files.getFileStore(path).getUsableSpace();
233
final long MAX_VALUE = 0xFFFF_FFFFL;
234
235
final long absolute = MAX_VALUE + 1L;
236
final long required = (long) (absolute * 1.1); // pad for sundries
237
System.out.println("\tavailable: " + available / GIGA + " GB");
238
System.out.println("\trequired: " + required / GIGA + " GB");
239
240
if (available > required) {
241
createJarWithLargeFile(largeJar, absolute);
242
testTheJar(largeJar);
243
} else {
244
System.out.println("Warning: testScenarioB passes vacuously,"
245
+ " requirements exceeds available space");
246
}
247
}
248
249
public static void main(String... args) throws Exception {
250
BigJar bj = new BigJar();
251
bj.run(args);
252
if (testExitValue > 0) {
253
System.out.println("Total of " + testExitValue + " failed");
254
System.exit(1);
255
} else {
256
System.out.println("All tests pass");
257
}
258
}
259
}
260
261