blob: 9923ea693e0698ff28e8918b90f37b635e97bd11 [file] [log] [blame]
sherman21984b02013-05-29 19:50:47 -07001/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
sherman57eb3812013-08-08 12:03:04 -070026 * @bug 4759491 6303183 7012868 8015666
sherman21984b02013-05-29 19:50:47 -070027 * @summary Test ZOS and ZIS timestamp in extra field correctly
28 */
29
30import java.io.*;
sherman57eb3812013-08-08 12:03:04 -070031import java.nio.file.Files;
32import java.nio.file.Path;
33import java.nio.file.Paths;
34import java.nio.file.attribute.FileTime;
sherman21984b02013-05-29 19:50:47 -070035import java.util.TimeZone;
36import java.util.concurrent.TimeUnit;
37import java.util.zip.ZipEntry;
sherman57eb3812013-08-08 12:03:04 -070038import java.util.zip.ZipFile;
sherman21984b02013-05-29 19:50:47 -070039import java.util.zip.ZipInputStream;
40import java.util.zip.ZipOutputStream;
41
42
43public class TestExtraTime {
44
45 public static void main(String[] args) throws Throwable{
46
47 File src = new File(System.getProperty("test.src", "."), "TestExtraTime.java");
48 if (src.exists()) {
sherman57eb3812013-08-08 12:03:04 -070049 long time = src.lastModified();
50 FileTime mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
51 FileTime atime = FileTime.from(time + 300000, TimeUnit.MILLISECONDS);
52 FileTime ctime = FileTime.from(time - 300000, TimeUnit.MILLISECONDS);
53 TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
54
55 test(mtime, null, null, null);
56 // ms-dos 1980 epoch problem
57 test(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null);
58 // non-default tz
59 test(mtime, null, null, tz);
60
61 test(mtime, atime, null, null);
62 test(mtime, null, ctime, null);
63 test(mtime, atime, ctime, null);
64
65 test(mtime, atime, null, tz);
66 test(mtime, null, ctime, tz);
67 test(mtime, atime, ctime, tz);
sherman21984b02013-05-29 19:50:47 -070068 }
69 }
70
sherman57eb3812013-08-08 12:03:04 -070071 static void test(FileTime mtime, FileTime atime, FileTime ctime,
72 TimeZone tz) throws Throwable {
73 System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
74 mtime, atime, ctime);
sherman21984b02013-05-29 19:50:47 -070075 TimeZone tz0 = TimeZone.getDefault();
76 if (tz != null) {
77 TimeZone.setDefault(tz);
78 }
79 ByteArrayOutputStream baos = new ByteArrayOutputStream();
80 ZipOutputStream zos = new ZipOutputStream(baos);
81 ZipEntry ze = new ZipEntry("TestExtreTime.java");
82
sherman57eb3812013-08-08 12:03:04 -070083 ze.setLastModifiedTime(mtime);
84 if (atime != null)
85 ze.setLastAccessTime(atime);
86 if (ctime != null)
87 ze.setCreationTime(ctime);
sherman21984b02013-05-29 19:50:47 -070088 zos.putNextEntry(ze);
89 zos.write(new byte[] { 1,2 ,3, 4});
90 zos.close();
91 if (tz != null) {
92 TimeZone.setDefault(tz0);
93 }
sherman57eb3812013-08-08 12:03:04 -070094 // ZipInputStream
sherman21984b02013-05-29 19:50:47 -070095 ZipInputStream zis = new ZipInputStream(
96 new ByteArrayInputStream(baos.toByteArray()));
97 ze = zis.getNextEntry();
98 zis.close();
sherman57eb3812013-08-08 12:03:04 -070099 check(mtime, atime, ctime, ze);
sherman21984b02013-05-29 19:50:47 -0700100
sherman57eb3812013-08-08 12:03:04 -0700101 // ZipFile
102 Path zpath = Paths.get(System.getProperty("test.dir", "."),
103 "TestExtraTimp.zip");
104 Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
105 ZipFile zf = new ZipFile(zpath.toFile());
106 ze = zf.getEntry("TestExtreTime.java");
107 // ZipFile read entry from cen, which does not have a/ctime,
108 // for now.
109 check(mtime, null, null, ze);
110 zf.close();
111 Files.delete(zpath);
112 }
sherman21984b02013-05-29 19:50:47 -0700113
sherman57eb3812013-08-08 12:03:04 -0700114 static void check(FileTime mtime, FileTime atime, FileTime ctime,
115 ZipEntry ze) {
116 /*
117 System.out.printf(" mtime [%tc]: [%tc]/[%tc]%n",
118 mtime.to(TimeUnit.MILLISECONDS),
119 ze.getTime(),
120 ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
121 */
122 if (mtime.to(TimeUnit.SECONDS) !=
123 ze.getLastModifiedTime().to(TimeUnit.SECONDS))
124 throw new RuntimeException("Timestamp: storing mtime failed!");
125 if (atime != null &&
126 atime.to(TimeUnit.SECONDS) !=
127 ze.getLastAccessTime().to(TimeUnit.SECONDS))
128 throw new RuntimeException("Timestamp: storing atime failed!");
129 if (ctime != null &&
130 ctime.to(TimeUnit.SECONDS) !=
131 ze.getCreationTime().to(TimeUnit.SECONDS))
132 throw new RuntimeException("Timestamp: storing ctime failed!");
sherman21984b02013-05-29 19:50:47 -0700133 }
134}