8246129: ZIP entries created for DOS epoch include local timezone metadata

Reviewed-by: lancea
This commit is contained in:
Claes Redestad 2020-06-10 20:53:04 +02:00
parent e47b2bc8c3
commit 99136026b8
3 changed files with 170 additions and 23 deletions
src/java.base/share/classes/java/util/zip
test/jdk/java/util/zip/ZipFile

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 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
@ -133,11 +133,6 @@ public class ZipEntry implements ZipConstants, Cloneable {
comment = e.comment;
}
/**
* Creates a new un-initialized zip entry
*/
ZipEntry() {}
/**
* Returns the name of the entry.
* @return the name of the entry
@ -167,10 +162,15 @@ public class ZipEntry implements ZipConstants, Cloneable {
this.xdostime = javaToExtendedDosTime(time);
// Avoid setting the mtime field if time is in the valid
// range for a DOS time
if (xdostime != DOSTIME_BEFORE_1980 && time <= UPPER_DOSTIME_BOUND) {
if (this.xdostime != DOSTIME_BEFORE_1980 && time <= UPPER_DOSTIME_BOUND) {
this.mtime = null;
} else {
this.mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
int localYear = javaEpochToLocalDateTime(time).getYear();
if (localYear >= 1980 && localYear <= 2099) {
this.mtime = null;
} else {
this.mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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
@ -131,14 +131,8 @@ class ZipUtils {
/**
* Converts Java time to DOS time.
*/
private static long javaToDosTime(long time) {
Instant instant = Instant.ofEpochMilli(time);
LocalDateTime ldt = LocalDateTime.ofInstant(
instant, ZoneId.systemDefault());
private static long javaToDosTime(LocalDateTime ldt) {
int year = ldt.getYear() - 1980;
if (year < 0) {
return (1 << 21) | (1 << 16);
}
return (year << 25 |
ldt.getMonthValue() << 21 |
ldt.getDayOfMonth() << 16 |
@ -154,14 +148,17 @@ class ZipUtils {
* @param time milliseconds since epoch
* @return DOS time with 2s remainder encoded into upper half
*/
public static long javaToExtendedDosTime(long time) {
if (time < 0) {
return ZipEntry.DOSTIME_BEFORE_1980;
static long javaToExtendedDosTime(long time) {
LocalDateTime ldt = javaEpochToLocalDateTime(time);
if (ldt.getYear() >= 1980) {
return javaToDosTime(ldt) + ((time % 2000) << 32);
}
long dostime = javaToDosTime(time);
return (dostime != ZipEntry.DOSTIME_BEFORE_1980)
? dostime + ((time % 2000) << 32)
: ZipEntry.DOSTIME_BEFORE_1980;
return ZipEntry.DOSTIME_BEFORE_1980;
}
static LocalDateTime javaEpochToLocalDateTime(long time) {
Instant instant = Instant.ofEpochMilli(time);
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
}
/**

@ -0,0 +1,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;
}
}