Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/BigJar.java
38833 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 719400526* @summary launcher handling of zip64 archives (Scenario A and B)27* @compile -XDignore.symbol.file BigJar.java28* @run main/timeout=600 BigJar29*/30/*31* This test consists of two scenarios:32*33* Scenario A: create a jar with entries exceeding 64K, add a main class and34* see if the launcher can handle it.35*36* Scenario A1: create a jar as in A, but add a zipfile comment as well.37*38* Scenario B: create a jar with a large enough file exceeding 4GB, and39* similarly test the launcher. This test can be run optionally by using the40* following jtreg option:41* "-javaoptions:-DBigJar_testScenarioB=true"42* or set43* "BigJar_testScenarioB" environment variable.44*45* Note this test will only run iff all the disk requirements are met at runtime.46*/47import java.io.BufferedInputStream;48import java.io.BufferedOutputStream;49import java.io.File;50import java.io.FileInputStream;51import java.io.FileOutputStream;52import java.io.IOException;53import java.io.OutputStream;54import java.nio.file.Files;55import java.nio.file.Path;56import java.util.ArrayList;57import java.util.List;58import java.util.jar.Attributes;59import java.util.jar.JarEntry;60import java.util.jar.JarOutputStream;61import java.util.jar.Manifest;62import java.util.zip.CRC32;63import java.util.zip.ZipEntry;64import java.util.zip.ZipOutputStream;6566public class BigJar extends TestHelper {6768private static final long GIGA = 1024 * 1024 * 1024;69private static final int BUFFER_LEN = Short.MAX_VALUE * 2;7071long getCount(long minlength) {72return (minlength / BUFFER_LEN) + 1;73}7475long computeCRC(long minlength) {76CRC32 crc = new CRC32();77byte[] buffer = new byte[BUFFER_LEN];78long count = getCount(minlength);79for (long i = 0; i < count; i++) {80crc.update(buffer);81}82return crc.getValue();83}8485long computeCRC(File inFile) throws IOException {86byte[] buffer = new byte[8192];87CRC32 crc = new CRC32();88try (FileInputStream fis = new FileInputStream(inFile);89BufferedInputStream bis = new BufferedInputStream(fis)) {90int n = bis.read(buffer);91while (n > 0) {92crc.update(buffer, 0, n);93n = bis.read(buffer);94}95}96return crc.getValue();97}9899void createLargeFile(OutputStream os, long minlength) throws IOException {100byte[] buffer = new byte[BUFFER_LEN];101long count = getCount(minlength);102for (long i = 0; i < count; i++) {103os.write(buffer);104}105os.flush();106}107108Manifest createMainClass(File javaFile) throws IOException {109javaFile.delete();110List<String> content = new ArrayList<>();111content.add("public class " + baseName(javaFile) + "{");112content.add("public static void main(String... args) {");113content.add("System.out.println(\"Hello World\\n\");");114content.add("System.exit(0);");115content.add("}");116content.add("}");117createFile(javaFile, content);118compile(javaFile.getName());119Manifest manifest = new Manifest();120manifest.clear();121manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");122manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, baseName(javaFile));123System.out.println(manifest.getMainAttributes().keySet());124System.out.println(manifest.getMainAttributes().values());125return manifest;126}127128void createJarWithLargeFile(File jarFile, long minlength) throws IOException {129File javaFile = new File("Foo.java");130Manifest manifest = createMainClass(javaFile);131File classFile = getClassFile(javaFile);132try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), manifest);133BufferedOutputStream bos = new BufferedOutputStream(jos);134FileInputStream fis = new FileInputStream(classFile);) {135jos.setLevel(ZipOutputStream.STORED);136jos.setMethod(0);137138JarEntry je = new JarEntry("large.data");139je.setCompressedSize(getCount(minlength) * BUFFER_LEN);140je.setSize(getCount(minlength) * BUFFER_LEN);141je.setCrc(computeCRC(minlength));142je.setMethod(ZipEntry.STORED);143jos.putNextEntry(je);144createLargeFile(bos, minlength);145146je = new JarEntry(classFile.getName());147je.setCompressedSize(classFile.length());148je.setSize(classFile.length());149je.setCrc(computeCRC(classFile));150je.setMethod(ZipEntry.STORED);151jos.putNextEntry(je);152copyStream(fis, bos);153bos.flush();154jos.closeEntry();155}156}157158void createLargeJar(File jarFile, String comment) throws IOException {159final int MAX = Short.MAX_VALUE * 2 + 10;160JarEntry je = null;161File javaFile = new File("Foo.java");162File classFile = getClassFile(javaFile);163Manifest manifest = createMainClass(javaFile);164try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), manifest);165FileInputStream fis = new FileInputStream(classFile)) {166jos.setLevel(JarOutputStream.STORED);167jos.setMethod(JarOutputStream.STORED);168for (int i = 0; i < MAX; i++) {169je = new JarEntry("X" + i + ".txt");170je.setSize(0);171je.setCompressedSize(0);172je.setCrc(0);173jos.putNextEntry(je);174}175176// add a class file177je = new JarEntry(classFile.getName());178je.setCompressedSize(classFile.length());179je.setSize(classFile.length());180je.setCrc(computeCRC(classFile));181jos.putNextEntry(je);182copyStream(fis, jos);183jos.closeEntry();184if (comment != null) {185jos.setComment(comment);186}187}188}189190void testTheJar(File theJar) throws Exception {191try {192TestResult tr = doExec(javaCmd, "-jar", theJar.getName());193tr.checkPositive();194if (!tr.testStatus) {195System.out.println(tr);196throw new Exception("Failed");197}198} finally {199theJar.delete();200}201}202203// a jar with entries exceeding 64k + a class file for the existential test204@Test205void testScenarioA() throws Exception {206File largeJar = new File("large.jar");207createLargeJar(largeJar, null);208testTheJar(largeJar);209}210211// a jar with entries exceeding 64k and zip comment212@Test213void testScenarioA1() throws Exception {214File largeJar = new File("largewithcomment.jar");215createLargeJar(largeJar, "A really large jar with a comment");216testTheJar(largeJar);217}218219// a jar with an enormous file + a class file for the existential test220@Test221void testScenarioB() throws Exception {222final String testString = "BigJar_testScenarioB";223if (Boolean.getBoolean(testString) == false &&224System.getenv(testString) == null) {225System.out.println("Warning: testScenarioB passes vacuously");226return;227}228final File largeJar = new File("huge.jar");229230final Path path = largeJar.getAbsoluteFile().getParentFile().toPath();231final long available = Files.getFileStore(path).getUsableSpace();232final long MAX_VALUE = 0xFFFF_FFFFL;233234final long absolute = MAX_VALUE + 1L;235final long required = (long) (absolute * 1.1); // pad for sundries236System.out.println("\tavailable: " + available / GIGA + " GB");237System.out.println("\trequired: " + required / GIGA + " GB");238239if (available > required) {240createJarWithLargeFile(largeJar, absolute);241testTheJar(largeJar);242} else {243System.out.println("Warning: testScenarioB passes vacuously,"244+ " requirements exceeds available space");245}246}247248public static void main(String... args) throws Exception {249BigJar bj = new BigJar();250bj.run(args);251if (testExitValue > 0) {252System.out.println("Total of " + testExitValue + " failed");253System.exit(1);254} else {255System.out.println("All tests pass");256}257}258}259260261