1 /* 2 * Copyright (c) 2019, 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 package test.java.util.zip.ZipFile; 24 25 import java.nio.file.FileSystems; 26 import java.nio.file.Paths; 27 import org.testng.annotations.AfterMethod; 28 import org.testng.annotations.BeforeMethod; 29 import org.testng.annotations.Test; 30 31 import java.io.*; 32 import java.nio.file.Files; 33 import java.nio.file.Path; 34 import java.util.List; 35 import java.util.zip.ZipEntry; 36 import java.util.zip.ZipFile; 37 import java.util.zip.ZipOutputStream; 38 39 import static org.testng.Assert.assertTrue; 40 41 /** 42 * @test 43 * @bug 8226530 44 * @summary ZIP File System tests that leverage DirectoryStream 45 * @modules java.base 46 * @compile Zip64SizeTest.java 47 * @run testng Zip64SizeTest 48 */ 49 public class Zip64SizeTest { 50 51 // Android-changed: Speed up the test with a larger buffer 52 // private static final int BUFFER_SIZE = 2048; 53 private static final int BUFFER_SIZE = 1024 * 1024; // 1MB 54 // ZIP file to create 55 // Android-changed: Create file in a temp dir. 56 // private static final String ZIP_FILE_NAME = "Zip64SizeTest.zip"; 57 private static final String TMPDIR = System.getProperty("java.io.tmpdir"); 58 private static final String ZIP_FILE_NAME = Paths.get(TMPDIR, "Zip64SizeTest.zip").toString(); 59 private static final String LARGE_FILE = Paths.get(TMPDIR, "LargeZipEntry.txt").toString(); 60 private static final String SMALL_FILE = Paths.get(TMPDIR, "SmallZipEntry.txt").toString(); 61 // File that will be created with a size greater than 0xFFFFFFFF 62 private static final String LARGE_FILE_NAME = "LargeZipEntry.txt"; 63 // File that will be created with a size less than 0xFFFFFFFF 64 private static final String SMALL_FILE_NAME = "SmallZipEntry.txt"; 65 // List of files to be added to the ZIP file 66 // Android-changed: Create file in a temp dir. 67 // private static final List<String> ZIP_ENTRIES = List.of(LARGE_FILE_NAME, 68 // SMALL_FILE_NAME); 69 private static final List<String> ZIP_ENTRIES = List.of(LARGE_FILE, SMALL_FILE); 70 private static final long LARGE_FILE_SIZE = 5L * 1024L * 1024L * 1024L; // 5GB 71 private static final long SMALL_FILE_SIZE = 0x100000L; // 1024L x 1024L; 72 73 /** 74 * Validate that if the size of a ZIP entry exceeds 0xFFFFFFFF, that the 75 * correct size is returned from the ZIP64 Extended information. 76 * @throws IOException 77 */ 78 // Android-changed: Disabled the test as it's long-running for CTS. http://b/227925195 79 // TODO: Enable this test on host or non-CTS device environment. 80 // @Test 81 // Android-changed: The test method needs to be public. 82 // private static void validateZipEntrySizes() throws IOException { validateZipEntrySizes()83 public void validateZipEntrySizes() throws IOException { 84 createFiles(); 85 createZipFile(); 86 System.out.println("Validating Zip Entry Sizes"); 87 try (ZipFile zip = new ZipFile(ZIP_FILE_NAME)) { 88 ZipEntry ze = zip.getEntry(LARGE_FILE_NAME); 89 System.out.printf("Entry: %s, size= %s%n", ze.getName(), ze.getSize()); 90 assertTrue(ze.getSize() == LARGE_FILE_SIZE); 91 ze = zip.getEntry(SMALL_FILE_NAME); 92 System.out.printf("Entry: %s, size= %s%n", ze.getName(), ze.getSize()); 93 assertTrue(ze.getSize() == SMALL_FILE_SIZE); 94 95 } 96 } 97 98 /** 99 * Delete the files created for use by the test 100 * @throws IOException if an error occurs deleting the files 101 */ deleteFiles()102 private static void deleteFiles() throws IOException { 103 // Android-changed: Path.of() isn't supported yet. 104 // Files.deleteIfExists(Path.of(ZIP_FILE_NAME)); 105 // Files.deleteIfExists(Path.of(LARGE_FILE_NAME)); 106 // Files.deleteIfExists(Path.of(SMALL_FILE_NAME)); 107 Files.deleteIfExists(FileSystems.getDefault().getPath(ZIP_FILE_NAME)); 108 Files.deleteIfExists(FileSystems.getDefault().getPath(LARGE_FILE)); 109 Files.deleteIfExists(FileSystems.getDefault().getPath(SMALL_FILE)); 110 } 111 112 /** 113 * Create the ZIP file adding an entry whose size exceeds 0xFFFFFFFF 114 * @throws IOException if an error occurs creating the ZIP File 115 */ createZipFile()116 private static void createZipFile() throws IOException { 117 try (FileOutputStream fos = new FileOutputStream(ZIP_FILE_NAME); 118 ZipOutputStream zos = new ZipOutputStream(fos)) { 119 System.out.printf("Creating Zip file: %s%n", ZIP_FILE_NAME); 120 for (String srcFile : ZIP_ENTRIES) { 121 System.out.printf("...Adding Entry: %s%n", srcFile); 122 File fileToZip = new File(srcFile); 123 try (FileInputStream fis = new FileInputStream(fileToZip)) { 124 ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); 125 zipEntry.setSize(fileToZip.length()); 126 zos.putNextEntry(zipEntry); 127 byte[] bytes = new byte[BUFFER_SIZE]; 128 int length; 129 while ((length = fis.read(bytes)) >= 0) { 130 zos.write(bytes, 0, length); 131 } 132 } 133 } 134 } 135 } 136 137 /** 138 * Create the files that will be added to the ZIP file 139 * @throws IOException if there is a problem creating the files 140 */ createFiles()141 private static void createFiles() throws IOException { 142 // Android-changed: Create file in a temp dir. 143 // try (RandomAccessFile largeFile = new RandomAccessFile(LARGE_FILE_NAME, "rw"); 144 // RandomAccessFile smallFile = new RandomAccessFile(SMALL_FILE_NAME, "rw")) { 145 try (RandomAccessFile largeFile = new RandomAccessFile(LARGE_FILE, "rw"); 146 RandomAccessFile smallFile = new RandomAccessFile(SMALL_FILE, "rw")) { 147 System.out.printf("Creating %s%n", LARGE_FILE_NAME); 148 largeFile.setLength(LARGE_FILE_SIZE); 149 System.out.printf("Creating %s%n", SMALL_FILE_NAME); 150 smallFile.setLength(SMALL_FILE_SIZE); 151 } 152 } 153 154 /** 155 * Make sure the needed test files do not exist prior to executing the test 156 * @throws IOException 157 */ 158 @BeforeMethod setUp()159 public void setUp() throws IOException { 160 deleteFiles(); 161 } 162 163 /** 164 * Remove the files created for the test 165 * @throws IOException 166 */ 167 @AfterMethod tearDown()168 public void tearDown() throws IOException { 169 deleteFiles(); 170 } 171 }