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
|
/*
* Copyright (c) 2020, 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.
*/
import org.testng.annotations.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import static org.testng.Assert.*;
/* @test
* @bug 8246129
* @summary JDK add metadata to zip files with entries timestamped at the
* lower bound of the DOS time epoch, i.e., 1980-01-01T00:00:00Z
* @run testng/othervm ZipEntryTimeBounds
*/
public class ZipEntryTimeBounds {
@Test
public void testFilesWithEntryAtLowerTimeBoundAreEqual() throws Exception {
// Ensure that entries that end up being exactly at the start of the
// DOS epoch, java.util.zip.ZipEntry.DOSTIME_BEFORE_1980, or
// 1980-01-01 00:00:00 are written without any extra timestamp metadata
// being written
TimeZone.setDefault(TimeZone.getTimeZone("GMT-01"));
File f1 = createTempFile();
makeZip(f1, new GregorianCalendar(1980, Calendar.JANUARY, 1, 0, 0, 0).getTimeInMillis());
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
File f2 = createTempFile();
makeZip(f2, new GregorianCalendar(1980, Calendar.JANUARY, 1, 0, 0, 0).getTimeInMillis());
assertEquals(Files.mismatch(f1.toPath(), f2.toPath()), -1L);
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
File f3 = createTempFile();
makeZip(f3, new GregorianCalendar(1980, Calendar.JANUARY, 1, 0, 0, 0).getTimeInMillis());
assertEquals(Files.mismatch(f1.toPath(), f3.toPath()), -1L);
// Check that the milliseconds part of the time is exactly preserved
assertEquals(new ZipFile(f1).getEntry("entry.txt").getTime() % 60000, 0);
assertEquals(new ZipFile(f2).getEntry("entry.txt").getTime() % 60000, 0);
assertEquals(new ZipFile(f3).getEntry("entry.txt").getTime() % 60000, 0);
}
@Test
public void testFilesWithEntryAfterLowerTimeBoundAreEqual() throws Exception {
// Ensure files written using different timezone with entries set
// shortly after 1980-01-01 00:00:00 produce exactly identical files
// without metadata
TimeZone.setDefault(TimeZone.getTimeZone("GMT-01"));
File f1 = createTempFile();
makeZip(f1, new GregorianCalendar(1980, Calendar.JANUARY, 1, 0, 0, 1).getTimeInMillis());
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
File f2 = createTempFile();
makeZip(f2, new GregorianCalendar(1980, Calendar.JANUARY, 1, 0, 0, 1).getTimeInMillis());
assertEquals(Files.mismatch(f1.toPath(), f2.toPath()), -1L);
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
File f3 = createTempFile();
makeZip(f3, new GregorianCalendar(1980, Calendar.JANUARY, 1, 0, 0, 1).getTimeInMillis() + 999);
assertEquals(Files.mismatch(f1.toPath(), f3.toPath()), -1L);
// Check that the seconds part of the time is lossily preserved,
// rounding down to the previous 2s step since epoch
assertEquals(new ZipFile(f1).getEntry("entry.txt").getTime() % 60000, 0);
assertEquals(new ZipFile(f2).getEntry("entry.txt").getTime() % 60000, 0);
assertEquals(new ZipFile(f3).getEntry("entry.txt").getTime() % 60000, 0);
File f4 = createTempFile();
makeZip(f4, new GregorianCalendar(1980, Calendar.JANUARY, 1, 0, 0, 2).getTimeInMillis());
assertEquals(new ZipFile(f4).getEntry("entry.txt").getTime() % 60000, 2000);
}
@Test
public void testFilesWithEntryBeforeLowerTimeBoundAreNotEqual() throws Exception {
// Files written using different timezone with entries set shortly
// before 1980-01-01 00:00:00 will produce files which add timestamp
// metadata that make the files turn up not equal
TimeZone.setDefault(TimeZone.getTimeZone("GMT-01"));
File f1 = createTempFile();
makeZip(f1, new GregorianCalendar(1979, Calendar.DECEMBER, 31, 23, 59, 59).getTimeInMillis());
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
File f2 = createTempFile();
makeZip(f2, new GregorianCalendar(1979, Calendar.DECEMBER, 31, 23, 59, 59).getTimeInMillis());
assertNotEquals(Files.mismatch(f1.toPath(), f2.toPath()), -1L);
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01"));
File f3 = createTempFile();
makeZip(f3, new GregorianCalendar(1979, Calendar.DECEMBER, 31, 23, 59, 59).getTimeInMillis() + 500);
assertNotEquals(Files.mismatch(f1.toPath(), f3.toPath()), -1L);
// Check that the time is preserved at second precision, no rounding
// to 2s
assertEquals(new ZipFile(f1).getEntry("entry.txt").getTime() % 60000, 59000);
assertEquals(new ZipFile(f2).getEntry("entry.txt").getTime() % 60000, 59000);
// Milliseconds are discarded even when storing entries with extended
// time metadata
assertEquals(new ZipFile(f3).getEntry("entry.txt").getTime() % 60000, 59000);
}
private static void makeZip(File f, long time) throws Exception {
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f))) {
ZipEntry e = new ZipEntry("entry.txt");
e.setTime(time);
out.putNextEntry(e);
out.write(new byte[] { 0, 1, 2, 3 });
out.closeEntry();
}
}
private static File createTempFile() throws IOException {
File file = File.createTempFile("out", "zip");
file.deleteOnExit();
return file;
}
}
|