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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/*
* Copyright (c) 2013, 2017, 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 4759491 6303183 7012868 8015666 8023713 8068790 8076641 8075526 8130914
* 8161942 8206389
* @summary Test ZOS and ZIS timestamp in extra field correctly
*/
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFilePermission;
import java.time.Instant;
import java.util.Arrays;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class TestExtraTime {
public static void main(String[] args) throws Throwable{
File src = new File(System.getProperty("test.src", "."), "TestExtraTime.java");
if (!src.exists()) {
return;
}
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
for (byte[] extra : new byte[][] { null, new byte[] {1, 2, 3}}) {
// ms-dos 1980 epoch problem
test0(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null, extra);
// negative epoch time
test0(FileTime.from(-100, TimeUnit.DAYS), null, null, null, extra);
long time = src.lastModified();
test(FileTime.from(time, TimeUnit.MILLISECONDS),
FileTime.from(time + 300000, TimeUnit.MILLISECONDS),
FileTime.from(time - 300000, TimeUnit.MILLISECONDS),
tz, extra);
// now
time = Instant.now().toEpochMilli();
test(FileTime.from(time, TimeUnit.MILLISECONDS),
FileTime.from(time + 300000, TimeUnit.MILLISECONDS),
FileTime.from(time - 300000, TimeUnit.MILLISECONDS),
tz, extra);
// unix 2038
time = 0x80000000L;
test(FileTime.from(time, TimeUnit.SECONDS),
FileTime.from(time, TimeUnit.SECONDS),
FileTime.from(time, TimeUnit.SECONDS),
tz, extra);
// mtime < unix 2038
time = 0x7fffffffL;
test(FileTime.from(time, TimeUnit.SECONDS),
FileTime.from(time + 30000, TimeUnit.SECONDS),
FileTime.from(time + 30000, TimeUnit.SECONDS),
tz, extra);
}
testNullHandling();
testTagOnlyHandling();
testTimeConversions();
testNullMtime();
}
static void test(FileTime mtime, FileTime atime, FileTime ctime,
TimeZone tz, byte[] extra) throws Throwable {
test0(mtime, null, null, null, extra);
test0(mtime, null, null, tz, extra); // non-default tz
test0(mtime, atime, null, null, extra);
test0(mtime, null, ctime, null, extra);
test0(mtime, atime, ctime, null, extra);
test0(mtime, atime, null, tz, extra);
test0(mtime, null, ctime, tz, extra);
test0(mtime, atime, ctime, tz, extra);
}
static void test0(FileTime mtime, FileTime atime, FileTime ctime,
TimeZone tz, byte[] extra) throws Throwable {
System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
mtime, atime, ctime);
TimeZone tz0 = TimeZone.getDefault();
if (tz != null) {
TimeZone.setDefault(tz);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry ze = new ZipEntry("TestExtraTime.java");
ze.setExtra(extra);
ze.setLastModifiedTime(mtime);
if (atime != null)
ze.setLastAccessTime(atime);
if (ctime != null)
ze.setCreationTime(ctime);
zos.putNextEntry(ze);
zos.write(new byte[] { 1,2 ,3, 4});
// append an extra entry to help check if the length and data
// of the extra field are being correctly written (in previous
// entry).
if (extra != null) {
ze = new ZipEntry("TestExtraEntry");
zos.putNextEntry(ze);
}
zos.close();
if (tz != null) {
TimeZone.setDefault(tz0);
}
// ZipInputStream
ZipInputStream zis = new ZipInputStream(
new ByteArrayInputStream(baos.toByteArray()));
ze = zis.getNextEntry();
zis.close();
check(mtime, atime, ctime, ze, extra);
// ZipFile
Path zpath = Paths.get(System.getProperty("test.dir", "."),
"TestExtraTime.zip");
Path zparent = zpath.getParent();
if (zparent != null && !Files.isWritable(zparent)) {
System.err.format("zpath %s parent %s is not writable%n",
zpath, zparent);
}
if (Files.exists(zpath)) {
System.err.format("zpath %s already exists%n", zpath);
if (Files.isDirectory(zpath)) {
System.err.format("%n%s contents:%n", zpath);
Files.list(zpath).forEach(System.err::println);
}
FileOwnerAttributeView foav = Files.getFileAttributeView(zpath,
FileOwnerAttributeView.class);
System.err.format("zpath %s owner: %s%n", zpath, foav.getOwner());
System.err.format("zpath %s permissions:%n", zpath);
Set<PosixFilePermission> perms =
Files.getPosixFilePermissions(zpath);
perms.stream().forEach(System.err::println);
}
if (Files.isSymbolicLink(zpath)) {
System.err.format("zpath %s is a symbolic link%n", zpath);
}
Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
try (ZipFile zf = new ZipFile(zpath.toFile())) {
ze = zf.getEntry("TestExtraTime.java");
// ZipFile read entry from cen, which does not have a/ctime,
// for now.
check(mtime, null, null, ze, extra);
} finally {
Files.delete(zpath);
}
}
static void check(FileTime mtime, FileTime atime, FileTime ctime,
ZipEntry ze, byte[] extra) {
/*
System.out.printf(" mtime [%tc]: [%tc]/[%tc]%n",
mtime.to(TimeUnit.MILLISECONDS),
ze.getTime(),
ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
*/
if (mtime != null &&
mtime.to(TimeUnit.SECONDS) !=
ze.getLastModifiedTime().to(TimeUnit.SECONDS)) {
throw new RuntimeException("Timestamp: storing mtime failed!");
}
if (atime != null &&
atime.to(TimeUnit.SECONDS) !=
ze.getLastAccessTime().to(TimeUnit.SECONDS))
throw new RuntimeException("Timestamp: storing atime failed!");
if (ctime != null &&
ctime.to(TimeUnit.SECONDS) !=
ze.getCreationTime().to(TimeUnit.SECONDS))
throw new RuntimeException("Timestamp: storing ctime failed!");
if (extra != null) {
// if extra data exists, the current implementation put it at
// the end of the extra data array (implementation detail)
byte[] extra1 = ze.getExtra();
if (extra1 == null || extra1.length < extra.length ||
!Arrays.equals(Arrays.copyOfRange(extra1,
extra1.length - extra.length,
extra1.length),
extra)) {
throw new RuntimeException("Timestamp: storing extra field failed!");
}
}
}
static void testNullHandling() {
ZipEntry ze = new ZipEntry("TestExtraTime.java");
try {
ze.setLastAccessTime(null);
throw new RuntimeException("setLastAccessTime(null) should throw NPE");
} catch (NullPointerException ignored) {
// pass
}
try {
ze.setCreationTime(null);
throw new RuntimeException("setCreationTime(null) should throw NPE");
} catch (NullPointerException ignored) {
// pass
}
try {
ze.setLastModifiedTime(null);
throw new RuntimeException("setLastModifiedTime(null) should throw NPE");
} catch (NullPointerException ignored) {
// pass
}
}
// verify that setting and getting any time is possible as per the intent
// of 4759491
static void testTimeConversions() {
// Sample across the entire range
long step = Long.MAX_VALUE / 100L;
testTimeConversions(Long.MIN_VALUE, Long.MAX_VALUE - step, step);
// Samples through the near future
long currentTime = System.currentTimeMillis();
testTimeConversions(currentTime, currentTime + 1_000_000, 10_000);
}
static void testTimeConversions(long from, long to, long step) {
ZipEntry ze = new ZipEntry("TestExtraTime.java");
for (long time = from; time <= to; time += step) {
ze.setTime(time);
FileTime lastModifiedTime = ze.getLastModifiedTime();
if (lastModifiedTime.toMillis() != time) {
throw new RuntimeException("setTime should make getLastModifiedTime " +
"return the specified instant: " + time +
" got: " + lastModifiedTime.toMillis());
}
if (ze.getTime() != time) {
throw new RuntimeException("getTime after setTime, expected: " +
time + " got: " + ze.getTime());
}
}
}
static void check(ZipEntry ze, byte[] extra) {
if (extra != null) {
byte[] extra1 = ze.getExtra();
if (extra1 == null || extra1.length < extra.length ||
!Arrays.equals(Arrays.copyOfRange(extra1,
extra1.length - extra.length,
extra1.length),
extra)) {
throw new RuntimeException("Timestamp: storing extra field failed!");
}
}
}
static void testTagOnlyHandling() throws Throwable {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] extra = new byte[] { 0x0a, 0, 4, 0, 0, 0, 0, 0 };
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
ZipEntry ze = new ZipEntry("TestExtraTime.java");
ze.setExtra(extra);
zos.putNextEntry(ze);
zos.write(new byte[] { 1,2 ,3, 4});
}
try (ZipInputStream zis = new ZipInputStream(
new ByteArrayInputStream(baos.toByteArray()))) {
ZipEntry ze = zis.getNextEntry();
check(ze, extra);
}
Path zpath = Paths.get(System.getProperty("test.dir", "."),
"TestExtraTime.zip");
Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
try (ZipFile zf = new ZipFile(zpath.toFile())) {
ZipEntry ze = zf.getEntry("TestExtraTime.java");
check(ze, extra);
} finally {
Files.delete(zpath);
}
}
static void checkLastModifiedTimeDOS(FileTime mtime, ZipEntry ze) {
FileTime lmt = ze.getLastModifiedTime();
if ((lmt.to(TimeUnit.SECONDS) >>> 1) != (mtime.to(TimeUnit.SECONDS) >>> 1) ||
lmt.to(TimeUnit.MILLISECONDS) != ze.getTime() ||
lmt.to(TimeUnit.MILLISECONDS) % 1000 != 0) {
throw new RuntimeException("Timestamp: storing mtime in dos format failed!");
}
}
static void testNullMtime() throws Throwable {
Instant now = Instant.now();
FileTime ctime = FileTime.from(now);
FileTime atime = FileTime.from(now.plusSeconds(7));
FileTime mtime = FileTime.from(now.plusSeconds(13));
System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
mtime, atime, ctime);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
ZipEntry ze = new ZipEntry("TestExtraTime.java");
ze.setCreationTime(ctime);
ze.setLastAccessTime(atime);
// ze.setLastModifiedTime(now);
ze.setTime(mtime.toMillis());
zos.putNextEntry(ze);
zos.write(new byte[] { 1,2 ,3, 4});
}
try (ZipInputStream zis = new ZipInputStream(
new ByteArrayInputStream(baos.toByteArray()))) {
ZipEntry ze = zis.getNextEntry();
// check LOC
check(null, atime, ctime, ze, null);
checkLastModifiedTimeDOS(mtime, ze);
}
Path zpath = Paths.get(System.getProperty("test.dir", "."),
"TestExtraTime.zip");
Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
try (ZipFile zf = new ZipFile(zpath.toFile())) {
ZipEntry ze = zf.getEntry("TestExtraTime.java");
// check CEN
checkLastModifiedTimeDOS(mtime, ze);
} finally {
Files.delete(zpath);
}
}
}
|