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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/*
* 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 8243254
* @summary Tests a simple set of operations on Zip files in various encodings
* focusing on ensuring metadata is properly encoded and read.
* @run testng TestZipFileEncodings
*/
import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import static org.testng.Assert.*;
public class TestZipFileEncodings {
private static int NUM_ENTRIES = 100;
private static int METAINF_ENTRIES = 5;
private static int ENTRY_SIZE = 100;
private static final AtomicInteger SEQUENCE = new AtomicInteger(0);
private static Set<Path> paths = new HashSet<>();
private static Random random() {
return ThreadLocalRandom.current();
}
@DataProvider(name = "non-unicode-charsets")
public Object[][] nonUnicodeCharsets() {
return new Object[][] {
{ "ISO-8859-1" },
{ "IBM01149" },
{ "IBM037" },
{ "IBM-Thai" }
};
}
@DataProvider(name = "unicode-charsets")
public Object[][] unicodeCharsets() {
return new Object[][] {
{ "UTF-8" },
{ "UTF-16" },
{ "UTF-16LE" },
{ "UTF-16BE" },
{ "UTF-32" }
};
}
@DataProvider(name = "all-charsets")
public Object[][] allCharsets() {
return Stream.concat(Stream.of(nonUnicodeCharsets()),
Stream.of(unicodeCharsets()))
.toArray(Object[][]::new);
}
@Test(dataProvider = "non-unicode-charsets")
public void testNonUnicode(String charsetName) throws Throwable {
test(NUM_ENTRIES, 100 + random().nextInt(ENTRY_SIZE), false, Charset.forName(charsetName));
}
@Test(dataProvider = "unicode-charsets")
public void testUnicode(String charsetName) throws Throwable {
test(NUM_ENTRIES, 100 + random().nextInt(ENTRY_SIZE), true, Charset.forName(charsetName));
}
@Test(dataProvider = "non-unicode-charsets")
public void testNonUnicodeManyEntries(String charsetName) throws Throwable {
test(70000, 10, false, Charset.forName(charsetName));
}
@Test(dataProvider = "unicode-charsets")
public void testUnicodeManyEntries(String charsetName) throws Throwable {
test(70000, 10, true, Charset.forName(charsetName));
}
/**
* This test was added to catch a regression where UTFZipCoder incorrectly
* treated latin1-encoded Strings as UTF8-compatible, while this actually only
* holds for ASCII strings.
*
* The implementation of UTFZipCoder.compare was later changed to not depend on
* the String's coder. Let's keep this test around anyway, since it provokes
* a corner case which could be easily missed.
*/
@Test
public void latin1NotAscii() throws IOException {
Path zip = Path.of("latin1-not-ascii.zip");
// latin1, but not ASCII
String entryName = "smörgåsbord";
try (ZipOutputStream z = new ZipOutputStream(Files.newOutputStream(zip))) {
z.putNextEntry(new ZipEntry(entryName));
}
try (ZipFile z = new ZipFile(zip.toFile())) {
assertNotNull(z.getEntry(entryName));
}
}
/**
* This test was added to catch a regression where ZipCoder.compare did not
* properly verify that the lookup name is a prefix of the entry name. Because of
* this regression, any candidate name with identical lengths and a trailing
* '/' would be incorrectly considered a "directory match".
*
* Since this regression depends on both a hash collision and that the length of names
* are equal, it is rarely found in the wild. Let's keep this test around
* since it explicity provokes this rare condition.
*
*/
@Test(dataProvider = "all-charsets")
public void sameHashAndLengthDirLookup(String charsetName) throws IOException {
// Two directory names with colliding hash codes and same length
// (found in a brute force search)
String one = "_____1637461950/";
String two = "_____-408231241/";
// Create a ZIP containing the two directories
Charset charset = Charset.forName(charsetName);
Path zip = Path.of("hash-collision-slashmatch-utf16.zip");
try (ZipOutputStream z = new ZipOutputStream(Files.newOutputStream(zip), charset)) {
// Give the names different comments so they we can distinguish them
ZipEntry first = new ZipEntry(one);
first.setComment("Entry one");
z.putNextEntry(first);
ZipEntry second = new ZipEntry(two);
second.setComment("Entry two");
z.putNextEntry(second);
}
// Assert that "slashless" lookups returns the correct entry even
// when the directory names have colliding hash codes and equal lengths
try (ZipFile z = new ZipFile(zip.toFile(), charset)) {
ZipEntry second = z.getEntry("_____-408231241");
assertEquals(second.getComment(), "Entry two");
ZipEntry first = z.getEntry("_____1637461950");
assertEquals(first.getComment(), "Entry one");
}
}
@AfterClass
public void tearDown() {
for (Path path : paths) {
path.toFile().deleteOnExit();
}
}
static void test(int numEntry, int szMax, boolean unicode, Charset cs) throws Throwable {
String name = "zfenc-" + SEQUENCE.incrementAndGet() + ".zip";
Zip zip = new Zip(name, numEntry, szMax, unicode, cs);
doTest(zip);
}
static void checkEqual(ZipEntry x, ZipEntry y) {
assertEquals(x.getName(), y.getName());
assertEquals(x.isDirectory(), y.isDirectory());
assertEquals(x.getMethod(), y.getMethod());
assertEquals((x.getTime() / 2000), y.getTime() / 2000);
assertEquals(x.getSize(), y.getSize());
assertEquals(x.getCompressedSize(), y.getCompressedSize());
assertEquals(x.getCrc(), y.getCrc());
assertEquals(x.getComment(), y.getComment());
}
static void doTest(Zip zip) throws Throwable {
try (ZipFile zf = new ZipFile(zip.name, zip.cs)) {
doTest0(zip, zf);
}
}
static void doTest0(Zip zip, ZipFile zf) throws Throwable {
// (0) check zero-length entry name, no AIOOBE
assertEquals(zf.getEntry(""), null);
List<ZipEntry> list = new ArrayList(zip.entries.keySet());
// check each entry and its bytes
for (ZipEntry ze : list) {
byte[] data = zip.entries.get(ze);
String name = ze.getName();
ZipEntry e = zf.getEntry(name);
checkEqual(e, ze);
if (!e.isDirectory()) {
// check with readAllBytes
try (InputStream is = zf.getInputStream(e)) {
assertEquals(data, is.readAllBytes());
}
int slash = name.indexOf('/');
if (slash > 0) {
ZipEntry dir1 = zf.getEntry(name.substring(0, slash));
ZipEntry dir2 = zf.getEntry(name.substring(0, slash + 1));
assertNotNull(dir1);
assertNotNull(dir2);
assertTrue(dir1.isDirectory());
assertTrue(dir2.isDirectory());
checkEqual(dir1, dir2);
}
} else {
ZipEntry unslashLookup = zf.getEntry(name.substring(0, name.length() - 1));
checkEqual(e, unslashLookup);
}
}
}
private static class Zip {
String name;
Charset cs;
Map<ZipEntry, byte[]> entries;
BasicFileAttributes attrs;
long lastModified;
Zip(String name, int num, int szMax, boolean unicode, Charset cs) {
this.cs = cs;
this.name = name;
entries = new LinkedHashMap<>(num);
try {
Path p = Paths.get(name);
Files.deleteIfExists(p);
paths.add(p);
} catch (Exception x) {
throw (RuntimeException)x;
}
try (FileOutputStream fos = new FileOutputStream(name);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos, cs))
{
CRC32 crc = new CRC32();
for (int i = 0; i < num; i++) {
String ename = "entry-" + i + "-name-" + random().nextLong();
if (unicode) {
// Provokes compatibility issue with slash handling for
// non-ASCII compatible Unicode encodings
ename = ename + '\u2F2F';
zos.putNextEntry(new ZipEntry(ename + '/'));
ename = ename + '/' + ename;
}
ZipEntry ze = new ZipEntry(ename);
assertTrue(!ze.isDirectory());
writeEntry(zos, crc, ze, ZipEntry.STORED, szMax);
}
// add some manifest entries
zos.putNextEntry(new ZipEntry("META-INF/"));
for (int i = 0; i < METAINF_ENTRIES; i++) {
String meta = "META-INF/" + "entry-" + i + "-metainf-" + random().nextLong();
ZipEntry ze = new ZipEntry(meta);
writeEntry(zos, crc, ze, ZipEntry.STORED, szMax);
}
} catch (Exception x) {
throw (RuntimeException)x;
}
try {
this.attrs = Files.readAttributes(Paths.get(name), BasicFileAttributes.class);
this.lastModified = new File(name).lastModified();
} catch (Exception x) {
throw (RuntimeException)x;
}
}
private void writeEntry(ZipOutputStream zos, CRC32 crc,
ZipEntry ze, int method, int szMax)
throws IOException
{
ze.setMethod(method);
byte[] data = new byte[random().nextInt(szMax + 1)];
random().nextBytes(data);
if (method == ZipEntry.STORED) { // must set size/csize/crc
ze.setSize(data.length);
ze.setCompressedSize(data.length);
crc.reset();
crc.update(data);
ze.setCrc(crc.getValue());
}
ze.setTime(System.currentTimeMillis());
ze.setComment(ze.getName());
zos.putNextEntry(ze);
zos.write(data);
zos.closeEntry();
entries.put(ze, data);
}
}
}
|