922N/A/*
922N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
922N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
922N/A *
922N/A * This code is free software; you can redistribute it and/or modify it
922N/A * under the terms of the GNU General Public License version 2 only, as
922N/A * published by the Free Software Foundation.
922N/A *
922N/A * This code is distributed in the hope that it will be useful, but WITHOUT
922N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
922N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
922N/A * version 2 for more details (a copy is included in the LICENSE file that
922N/A * accompanied this code).
922N/A *
922N/A * You should have received a copy of the GNU General Public License version
922N/A * 2 along with this work; if not, write to the Free Software Foundation,
922N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
922N/A *
922N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
922N/A * or visit www.oracle.com if you need additional information or have any
922N/A * questions.
922N/A */
922N/A
922N/A/*
922N/A * @test
923N/A * @bug 6836682 7025988
935N/A * @summary JavacFileManager handling of zip64 archives (Scenario A and B)
922N/A * @compile -XDignore.symbol.file T6836682.java Utils.java
922N/A * @run main T6836682
922N/A */
935N/A/*
935N/A * This test consists of two scenarios:
935N/A *
935N/A * Scenario A: create a jar with entries exceeding 64K, and see if the javac
935N/A * can handle this large jar on the classpath. Generally this test completes
935N/A * within a minute
935N/A *
935N/A * Scenario B: create a jar with a large enough file exceeding 4GB, and
935N/A * similarly test javac. This test is known to be slow and problematic on
935N/A * certain operating systems, thus this test can be selected by passing a
935N/A * property through jtreg as follows:
935N/A * -javaoptions=-DT6836682.testScenarioB=true.
935N/A * Note this test will only run iff all the disk requirements are met at runtime.
935N/A */
935N/Aimport java.io.BufferedInputStream;
922N/Aimport java.io.BufferedOutputStream;
922N/Aimport java.io.File;
922N/Aimport java.io.FileInputStream;
922N/Aimport java.io.FileOutputStream;
922N/Aimport java.io.IOException;
935N/Aimport java.io.OutputStream;
922N/Aimport java.nio.file.Files;
922N/Aimport java.nio.file.Path;
935N/Aimport java.util.zip.CRC32;
922N/Aimport java.util.zip.ZipEntry;
935N/Aimport java.util.zip.ZipOutputStream;
922N/A
922N/Apublic class T6836682 {
922N/A
922N/A private static final long GIGA = 1024 * 1024 * 1024;
935N/A private static final int BUFFER_LEN = Short.MAX_VALUE * 2;
922N/A
935N/A static long getCount(long minlength) {
935N/A return (minlength / BUFFER_LEN) + 1;
935N/A }
935N/A
935N/A static long computeCRC(long minlength) {
935N/A CRC32 crc = new CRC32();
935N/A byte[] buffer = new byte[BUFFER_LEN];
935N/A long count = getCount(minlength);
935N/A for (long i = 0; i < count; i++) {
935N/A crc.update(buffer);
935N/A }
935N/A return crc.getValue();
935N/A }
935N/A
935N/A static long computeCRC(File inFile) throws IOException {
935N/A byte[] buffer = new byte[8192];
935N/A CRC32 crc = new CRC32();
935N/A FileInputStream fis = null;
935N/A BufferedInputStream bis = null;
922N/A try {
935N/A fis = new FileInputStream(inFile);
935N/A bis = new BufferedInputStream(fis);
935N/A int n = bis.read(buffer);
935N/A while (n > 0) {
935N/A crc.update(buffer, 0, n);
935N/A n = bis.read(buffer);
922N/A }
922N/A } finally {
935N/A Utils.close(bis);
935N/A Utils.close(fis);
922N/A }
935N/A return crc.getValue();
935N/A }
935N/A
935N/A static void createLargeFile(OutputStream os, long minlength) throws IOException {
935N/A byte[] buffer = new byte[BUFFER_LEN];
935N/A long count = getCount(minlength);
935N/A for (long i = 0; i < count; i++) {
935N/A os.write(buffer);
922N/A }
935N/A os.flush();
922N/A }
922N/A
922N/A static void createJarWithLargeFile(File jarFile, File javaFile,
922N/A long minlength) throws IOException {
922N/A Utils.createClassFile(javaFile, null, true);
935N/A File classFile = new File(Utils.getClassFileName(javaFile));
935N/A ZipOutputStream zos = null;
935N/A BufferedOutputStream bos = null;
935N/A FileInputStream fis = null;
935N/A try {
935N/A zos = new ZipOutputStream(new FileOutputStream(jarFile));
935N/A zos.setLevel(ZipOutputStream.STORED);
935N/A zos.setMethod(0);
935N/A bos = new BufferedOutputStream(zos);
935N/A
935N/A ZipEntry ze = new ZipEntry("large.data");
935N/A ze.setCompressedSize(getCount(minlength) * BUFFER_LEN);
935N/A ze.setSize(getCount(minlength) * BUFFER_LEN);
935N/A ze.setCrc(computeCRC(minlength));
935N/A ze.setMethod(ZipEntry.STORED);
935N/A zos.putNextEntry(ze);
935N/A createLargeFile(bos, minlength);
935N/A
935N/A ze = new ZipEntry(classFile.getName());
935N/A ze.setCompressedSize(classFile.length());
935N/A ze.setSize(classFile.length());
935N/A ze.setCrc(computeCRC(classFile));
935N/A ze.setMethod(ZipEntry.STORED);
935N/A zos.putNextEntry(ze);
935N/A fis = new FileInputStream(classFile);
935N/A Utils.copyStream(fis, bos);
935N/A bos.flush();
935N/A zos.closeEntry();
935N/A } finally {
935N/A Utils.close(bos);
935N/A Utils.close(zos);
935N/A Utils.close(fis);
935N/A }
922N/A // deleted to prevent accidental linkage
922N/A new File(Utils.getClassFileName(javaFile)).delete();
922N/A }
922N/A
922N/A static void createLargeJar(File jarFile, File javaFile) throws IOException {
922N/A File classFile = new File(Utils.getClassFileName(javaFile));
922N/A Utils.createClassFile(javaFile, null, true);
935N/A ZipOutputStream zos = null;
922N/A FileInputStream fis = null;
935N/A final int MAX = Short.MAX_VALUE * 2 + 10;
935N/A ZipEntry ze = null;
922N/A try {
935N/A zos = new ZipOutputStream(new FileOutputStream(jarFile));
935N/A zos.setLevel(ZipOutputStream.STORED);
935N/A zos.setMethod(ZipOutputStream.STORED);
935N/A for (int i = 0; i < MAX ; i++) {
935N/A ze = new ZipEntry("X" + i + ".txt");
935N/A ze.setSize(0);
935N/A ze.setCompressedSize(0);
935N/A ze.setCrc(0);
935N/A zos.putNextEntry(ze);
922N/A }
935N/A
935N/A // add a class file
935N/A ze = new ZipEntry(classFile.getName());
935N/A ze.setCompressedSize(classFile.length());
935N/A ze.setSize(classFile.length());
935N/A ze.setCrc(computeCRC(classFile));
935N/A zos.putNextEntry(ze);
922N/A fis = new FileInputStream(classFile);
935N/A Utils.copyStream(fis, zos);
922N/A } finally {
935N/A Utils.close(zos);
922N/A Utils.close(fis);
922N/A // deleted to prevent accidental linkage
922N/A new File(Utils.getClassFileName(javaFile)).delete();
922N/A }
935N/A }
922N/A
922N/A // a jar with entries exceeding 64k + a class file for the existential test
935N/A public static void testScenarioA(String... args) throws IOException {
922N/A File largeJar = new File("large.jar");
922N/A File javaFile = new File("Foo.java");
922N/A createLargeJar(largeJar, javaFile);
922N/A
922N/A File testFile = new File("Bar.java");
922N/A try {
922N/A Utils.createJavaFile(testFile, javaFile);
922N/A if (!Utils.compile("-doe", "-verbose", "-cp",
922N/A largeJar.getAbsolutePath(), testFile.getAbsolutePath())) {
922N/A throw new IOException("test failed");
922N/A }
922N/A } finally {
922N/A Utils.deleteFile(largeJar);
922N/A }
922N/A }
922N/A
922N/A // a jar with an enormous file + a class file for the existential test
935N/A public static void testScenarioB(String... args) throws IOException {
922N/A final File largeJar = new File("huge.jar");
922N/A final File javaFile = new File("Foo.java");
922N/A
922N/A final Path path = largeJar.getAbsoluteFile().getParentFile().toPath();
922N/A final long available = Files.getFileStore(path).getUsableSpace();
922N/A final long MAX_VALUE = 0xFFFF_FFFFL;
922N/A
922N/A final long absolute = MAX_VALUE + 1L;
922N/A final long required = (long)(absolute * 1.1); // pad for sundries
922N/A System.out.println("\tavailable: " + available / GIGA + " GB");
935N/A System.out.println("\trequired: " + required / GIGA + " GB");
922N/A
922N/A if (available > required) {
922N/A createJarWithLargeFile(largeJar, javaFile, absolute);
922N/A File testFile = new File("Bar.java");
922N/A Utils.createJavaFile(testFile, javaFile);
922N/A try {
922N/A if (!Utils.compile("-doe", "-verbose", "-cp",
922N/A largeJar.getAbsolutePath(), testFile.getAbsolutePath())) {
922N/A throw new IOException("test failed");
922N/A }
922N/A } finally {
922N/A Utils.deleteFile(largeJar);
922N/A }
922N/A } else {
935N/A System.out.println("Warning: testScenarioB passes vacuously," +
935N/A " requirements exceeds available space");
922N/A }
922N/A }
922N/A
922N/A public static void main(String... args) throws IOException {
935N/A testScenarioA();
935N/A System.out.println("testScenarioA: PASS");
935N/A if (Boolean.getBoolean("T6836682.testScenarioB")) {
935N/A testScenarioB();
935N/A System.out.println("testScenarioB: PASS");
935N/A } else {
935N/A System.out.println("Warning: testScenarioB, large file test skipped");
935N/A }
922N/A }
922N/A}