1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 8317678
* @modules java.base/java.util.zip:open
* @summary Fix up hashCode() for ZipFile.Source.Key
* @library /test/lib
* @run junit/othervm ZipSourceCache
*/
import java.io.*;
import java.lang.reflect.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.zip.*;
import jdk.test.lib.util.FileUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterAll;
import static org.junit.jupiter.api.Assertions.*;
public class ZipSourceCache {
private static final String ZIPFILE_NAME =
System.currentTimeMillis() + "-bug8317678.zip";
private static final String ZIPENTRY_NAME = "random.txt";
private static final String INVALID_LOC_EXCEPTION =
"ZipFile invalid LOC header (bad signature)";
private static final boolean DEBUG = false;
private static File relativeFile = new File(ZIPFILE_NAME);
private static File absoluteFile = new File(ZIPFILE_NAME).getAbsoluteFile();
private static boolean hasfileKeySupport;
@BeforeAll
public static void setup() throws Exception {
createZipFile("test1");
var attrs = Files.readAttributes(relativeFile.toPath(), BasicFileAttributes.class);
hasfileKeySupport = (attrs.fileKey() != null);
}
@AfterAll
public static void cleanup() throws IOException {
FileUtils.deleteFileIfExistsWithRetry(Path.of(ZIPFILE_NAME));
}
/*
* Monitor the internal "files" HashMap to ensure that we only
* create one <Key, Source> mapping per unique zip file.
*
* This test also ensures that a new <Key, Source> mapping is created
* if an update to an existing zip file is detected.
*/
@Test
public void testKeySourceMapping() throws Exception {
ZipFile absoluteZipFile;
HashMap internalMap;
int numSources;
try (ZipFile zipFile = new ZipFile(relativeFile)) {
Class source = Class.forName("java.util.zip.ZipFile$Source");
Field filesMap = source.getDeclaredField("files");
filesMap.setAccessible(true);
internalMap = (HashMap) filesMap.get(zipFile);
numSources = internalMap.size();
// opening of same zip file shouldn't cause new Source
// to be constructed on filesystems which support fileKey()
absoluteZipFile = new ZipFile(absoluteFile);
if (hasfileKeySupport) {
assertEquals(numSources, internalMap.size());
} else {
assertEquals(++numSources, internalMap.size());
}
// update the zip file, should expect a new Source Object
// ignore this part of test if file can't be updated (can't overwrite)
if (createZipFile("differentContent")) {
ZipFile z = new ZipFile(relativeFile);
// update of file should trigger new <Key, Source> mapping
assertEquals(++numSources, internalMap.size());
// new Source created, CEN structure should map fine
readZipFileContents(z);
// the old Source in use for old file, should no longer map correctly
IOException ioe = assertThrows(IOException.class, () -> readZipFileContents(absoluteZipFile));
assertEquals(INVALID_LOC_EXCEPTION, ioe.getMessage());
z.close();
assertEquals(--numSources, internalMap.size());
}
}
// with fileKey() support, the close() call shouldn't remove the
// Source entry just yet since we still have one reference to the file
if (hasfileKeySupport) {
assertEquals(numSources, internalMap.size());
} else {
assertEquals(--numSources, internalMap.size());
}
if (absoluteZipFile != null) {
absoluteZipFile.close();
}
// now, the Source entry key should be removed
assertEquals(--numSources, internalMap.size());
}
private static void readZipFileContents(ZipFile zf) throws IOException {
var e = zf.entries();
while (e.hasMoreElements()) {
InputStream is = zf.getInputStream(e.nextElement());
String s = new String(is.readAllBytes());
if (DEBUG) System.err.println(s);
}
}
private static boolean createZipFile(String content) {
CRC32 crc32 = new CRC32();
long t = System.currentTimeMillis();
// let's have at least 2 entries created to ensure
// that a bad zip structure is detected if file is updated
int numEntries = new Random().nextInt(10) + 2;
File zipFile = new File(ZIPFILE_NAME);
try (FileOutputStream fos = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos)) {
for (int i = 0; i < numEntries; i++) {
ZipEntry e = new ZipEntry(ZIPENTRY_NAME + i);
e.setMethod(ZipEntry.STORED);
byte[] toWrite = content.repeat(i+1).getBytes();
e.setTime(t);
e.setSize(toWrite.length);
crc32.reset();
crc32.update(toWrite);
e.setCrc(crc32.getValue());
zos.putNextEntry(e);
zos.write(toWrite);
}
} catch (IOException e) {
// some systems mightn't allow file to be updated while open
System.err.println("error updating file. " + e);
return false;
}
return true;
}
}
|