Path: blob/master/test/jdk/tools/launcher/BigJar.java
41144 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* @modules jdk.compiler28* jdk.zipfs29* @compile -XDignore.symbol.file BigJar.java30* @run main/timeout=600 BigJar31*/32/*33* This test consists of two scenarios:34*35* Scenario A: create a jar with entries exceeding 64K, add a main class and36* see if the launcher can handle it.37*38* Scenario A1: create a jar as in A, but add a zipfile comment as well.39*40* Scenario B: create a jar with a large enough file exceeding 4GB, and41* similarly test the launcher. This test can be run optionally by using the42* following jtreg option:43* "-javaoptions:-DBigJar_testScenarioB=true"44* or set45* "BigJar_testScenarioB" environment variable.46*47* Note this test will only run iff all the disk requirements are met at runtime.48*/49import java.io.BufferedInputStream;50import java.io.BufferedOutputStream;51import java.io.File;52import java.io.FileInputStream;53import java.io.FileOutputStream;54import java.io.IOException;55import java.io.OutputStream;56import java.nio.file.Files;57import java.nio.file.Path;58import java.util.ArrayList;59import java.util.List;60import java.util.jar.Attributes;61import java.util.jar.JarEntry;62import java.util.jar.JarOutputStream;63import java.util.jar.Manifest;64import java.util.zip.CRC32;65import java.util.zip.ZipEntry;66import java.util.zip.ZipOutputStream;6768public class BigJar extends TestHelper {6970private static final long GIGA = 1024 * 1024 * 1024;71private static final int BUFFER_LEN = Short.MAX_VALUE * 2;7273long getCount(long minlength) {74return (minlength / BUFFER_LEN) + 1;75}7677long computeCRC(long minlength) {78CRC32 crc = new CRC32();79byte[] buffer = new byte[BUFFER_LEN];80long count = getCount(minlength);81for (long i = 0; i < count; i++) {82crc.update(buffer);83}84return crc.getValue();85}8687long computeCRC(File inFile) throws IOException {88byte[] buffer = new byte[8192];89CRC32 crc = new CRC32();90try (FileInputStream fis = new FileInputStream(inFile);91BufferedInputStream bis = new BufferedInputStream(fis)) {92int n = bis.read(buffer);93while (n > 0) {94crc.update(buffer, 0, n);95n = bis.read(buffer);96}97}98return crc.getValue();99}100101void createLargeFile(OutputStream os, long minlength) throws IOException {102byte[] buffer = new byte[BUFFER_LEN];103long count = getCount(minlength);104for (long i = 0; i < count; i++) {105os.write(buffer);106}107os.flush();108}109110Manifest createMainClass(File javaFile) throws IOException {111javaFile.delete();112List<String> content = new ArrayList<>();113content.add("public class " + baseName(javaFile) + "{");114content.add("public static void main(String... args) {");115content.add("System.out.println(\"Hello World\\n\");");116content.add("System.exit(0);");117content.add("}");118content.add("}");119createFile(javaFile, content);120compile(javaFile.getName());121Manifest manifest = new Manifest();122manifest.clear();123manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");124manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, baseName(javaFile));125System.out.println(manifest.getMainAttributes().keySet());126System.out.println(manifest.getMainAttributes().values());127return manifest;128}129130void createJarWithLargeFile(File jarFile, long minlength) throws IOException {131File javaFile = new File("Foo.java");132Manifest manifest = createMainClass(javaFile);133File classFile = getClassFile(javaFile);134try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), manifest);135BufferedOutputStream bos = new BufferedOutputStream(jos);136FileInputStream fis = new FileInputStream(classFile);) {137jos.setLevel(ZipOutputStream.STORED);138jos.setMethod(0);139140JarEntry je = new JarEntry("large.data");141je.setCompressedSize(getCount(minlength) * BUFFER_LEN);142je.setSize(getCount(minlength) * BUFFER_LEN);143je.setCrc(computeCRC(minlength));144je.setMethod(ZipEntry.STORED);145jos.putNextEntry(je);146createLargeFile(bos, minlength);147148je = new JarEntry(classFile.getName());149je.setCompressedSize(classFile.length());150je.setSize(classFile.length());151je.setCrc(computeCRC(classFile));152je.setMethod(ZipEntry.STORED);153jos.putNextEntry(je);154copyStream(fis, bos);155bos.flush();156jos.closeEntry();157}158}159160void createLargeJar(File jarFile, String comment) throws IOException {161final int MAX = Short.MAX_VALUE * 2 + 10;162JarEntry je = null;163File javaFile = new File("Foo.java");164File classFile = getClassFile(javaFile);165Manifest manifest = createMainClass(javaFile);166try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), manifest);167FileInputStream fis = new FileInputStream(classFile)) {168jos.setLevel(JarOutputStream.STORED);169jos.setMethod(JarOutputStream.STORED);170for (int i = 0; i < MAX; i++) {171je = new JarEntry("X" + i + ".txt");172je.setSize(0);173je.setCompressedSize(0);174je.setCrc(0);175jos.putNextEntry(je);176}177178// add a class file179je = new JarEntry(classFile.getName());180je.setCompressedSize(classFile.length());181je.setSize(classFile.length());182je.setCrc(computeCRC(classFile));183jos.putNextEntry(je);184copyStream(fis, jos);185jos.closeEntry();186if (comment != null) {187jos.setComment(comment);188}189}190}191192void testTheJar(File theJar) throws Exception {193try {194TestResult tr = doExec(javaCmd, "-jar", theJar.getName());195tr.checkPositive();196if (!tr.testStatus) {197System.out.println(tr);198throw new Exception("Failed");199}200} finally {201theJar.delete();202}203}204205// a jar with entries exceeding 64k + a class file for the existential test206@Test207void testScenarioA() throws Exception {208File largeJar = new File("large.jar");209createLargeJar(largeJar, null);210testTheJar(largeJar);211}212213// a jar with entries exceeding 64k and zip comment214@Test215void testScenarioA1() throws Exception {216File largeJar = new File("largewithcomment.jar");217createLargeJar(largeJar, "A really large jar with a comment");218testTheJar(largeJar);219}220221// a jar with an enormous file + a class file for the existential test222@Test223void testScenarioB() throws Exception {224final String testString = "BigJar_testScenarioB";225if (Boolean.getBoolean(testString) == false &&226System.getenv(testString) == null) {227System.out.println("Warning: testScenarioB passes vacuously");228return;229}230final File largeJar = new File("huge.jar");231232final Path path = largeJar.getAbsoluteFile().getParentFile().toPath();233final long available = Files.getFileStore(path).getUsableSpace();234final long MAX_VALUE = 0xFFFF_FFFFL;235236final long absolute = MAX_VALUE + 1L;237final long required = (long) (absolute * 1.1); // pad for sundries238System.out.println("\tavailable: " + available / GIGA + " GB");239System.out.println("\trequired: " + required / GIGA + " GB");240241if (available > required) {242createJarWithLargeFile(largeJar, absolute);243testTheJar(largeJar);244} else {245System.out.println("Warning: testScenarioB passes vacuously,"246+ " requirements exceeds available space");247}248}249250public static void main(String... args) throws Exception {251BigJar bj = new BigJar();252bj.run(args);253if (testExitValue > 0) {254System.out.println("Total of " + testExitValue + " failed");255System.exit(1);256} else {257System.out.println("All tests pass");258}259}260}261262263