blob: fcf75a153b0d559f9293de7e43d1db14de890ee3 [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
redestadb3be0a22015-02-21 13:46:24 +010026 * @bug 4759491 6303183 7012868 8015666 8023713 8068790
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;
sherman45e33882013-08-28 09:46:55 -070035import java.util.Arrays;
sherman21984b02013-05-29 19:50:47 -070036import java.util.TimeZone;
37import java.util.concurrent.TimeUnit;
38import java.util.zip.ZipEntry;
sherman57eb3812013-08-08 12:03:04 -070039import java.util.zip.ZipFile;
sherman21984b02013-05-29 19:50:47 -070040import java.util.zip.ZipInputStream;
41import java.util.zip.ZipOutputStream;
42
43
44public class TestExtraTime {
45
46 public static void main(String[] args) throws Throwable{
47
48 File src = new File(System.getProperty("test.src", "."), "TestExtraTime.java");
49 if (src.exists()) {
sherman57eb3812013-08-08 12:03:04 -070050 long time = src.lastModified();
51 FileTime mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
52 FileTime atime = FileTime.from(time + 300000, TimeUnit.MILLISECONDS);
53 FileTime ctime = FileTime.from(time - 300000, TimeUnit.MILLISECONDS);
54 TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
55
sherman45e33882013-08-28 09:46:55 -070056 for (byte[] extra : new byte[][] { null, new byte[] {1, 2, 3}}) {
57 test(mtime, null, null, null, extra);
58 // ms-dos 1980 epoch problem
59 test(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null, extra);
60 // non-default tz
61 test(mtime, null, null, tz, extra);
sherman57eb3812013-08-08 12:03:04 -070062
sherman45e33882013-08-28 09:46:55 -070063 test(mtime, atime, null, null, extra);
64 test(mtime, null, ctime, null, extra);
65 test(mtime, atime, ctime, null, extra);
sherman57eb3812013-08-08 12:03:04 -070066
sherman45e33882013-08-28 09:46:55 -070067 test(mtime, atime, null, tz, extra);
68 test(mtime, null, ctime, tz, extra);
69 test(mtime, atime, ctime, tz, extra);
70 }
sherman21984b02013-05-29 19:50:47 -070071 }
redestadb3be0a22015-02-21 13:46:24 +010072
73 testNullHandling();
sherman21984b02013-05-29 19:50:47 -070074 }
75
sherman57eb3812013-08-08 12:03:04 -070076 static void test(FileTime mtime, FileTime atime, FileTime ctime,
sherman45e33882013-08-28 09:46:55 -070077 TimeZone tz, byte[] extra) throws Throwable {
sherman57eb3812013-08-08 12:03:04 -070078 System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
79 mtime, atime, ctime);
sherman21984b02013-05-29 19:50:47 -070080 TimeZone tz0 = TimeZone.getDefault();
81 if (tz != null) {
82 TimeZone.setDefault(tz);
83 }
84 ByteArrayOutputStream baos = new ByteArrayOutputStream();
85 ZipOutputStream zos = new ZipOutputStream(baos);
sherman45e33882013-08-28 09:46:55 -070086 ZipEntry ze = new ZipEntry("TestExtraTime.java");
87 ze.setExtra(extra);
sherman57eb3812013-08-08 12:03:04 -070088 ze.setLastModifiedTime(mtime);
89 if (atime != null)
90 ze.setLastAccessTime(atime);
91 if (ctime != null)
92 ze.setCreationTime(ctime);
sherman21984b02013-05-29 19:50:47 -070093 zos.putNextEntry(ze);
94 zos.write(new byte[] { 1,2 ,3, 4});
sherman45e33882013-08-28 09:46:55 -070095
96 // append an extra entry to help check if the length and data
97 // of the extra field are being correctly written (in previous
98 // entry).
99 if (extra != null) {
100 ze = new ZipEntry("TestExtraEntry");
101 zos.putNextEntry(ze);
102 }
sherman21984b02013-05-29 19:50:47 -0700103 zos.close();
104 if (tz != null) {
105 TimeZone.setDefault(tz0);
106 }
sherman57eb3812013-08-08 12:03:04 -0700107 // ZipInputStream
sherman21984b02013-05-29 19:50:47 -0700108 ZipInputStream zis = new ZipInputStream(
109 new ByteArrayInputStream(baos.toByteArray()));
110 ze = zis.getNextEntry();
111 zis.close();
sherman45e33882013-08-28 09:46:55 -0700112 check(mtime, atime, ctime, ze, extra);
sherman21984b02013-05-29 19:50:47 -0700113
sherman57eb3812013-08-08 12:03:04 -0700114 // ZipFile
115 Path zpath = Paths.get(System.getProperty("test.dir", "."),
sherman45e33882013-08-28 09:46:55 -0700116 "TestExtraTime.zip");
sherman57eb3812013-08-08 12:03:04 -0700117 Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
118 ZipFile zf = new ZipFile(zpath.toFile());
sherman45e33882013-08-28 09:46:55 -0700119 ze = zf.getEntry("TestExtraTime.java");
sherman57eb3812013-08-08 12:03:04 -0700120 // ZipFile read entry from cen, which does not have a/ctime,
121 // for now.
sherman45e33882013-08-28 09:46:55 -0700122 check(mtime, null, null, ze, extra);
sherman57eb3812013-08-08 12:03:04 -0700123 zf.close();
124 Files.delete(zpath);
125 }
sherman21984b02013-05-29 19:50:47 -0700126
sherman57eb3812013-08-08 12:03:04 -0700127 static void check(FileTime mtime, FileTime atime, FileTime ctime,
sherman45e33882013-08-28 09:46:55 -0700128 ZipEntry ze, byte[] extra) {
sherman57eb3812013-08-08 12:03:04 -0700129 /*
130 System.out.printf(" mtime [%tc]: [%tc]/[%tc]%n",
131 mtime.to(TimeUnit.MILLISECONDS),
132 ze.getTime(),
133 ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
134 */
135 if (mtime.to(TimeUnit.SECONDS) !=
136 ze.getLastModifiedTime().to(TimeUnit.SECONDS))
137 throw new RuntimeException("Timestamp: storing mtime failed!");
138 if (atime != null &&
139 atime.to(TimeUnit.SECONDS) !=
140 ze.getLastAccessTime().to(TimeUnit.SECONDS))
141 throw new RuntimeException("Timestamp: storing atime failed!");
142 if (ctime != null &&
143 ctime.to(TimeUnit.SECONDS) !=
144 ze.getCreationTime().to(TimeUnit.SECONDS))
145 throw new RuntimeException("Timestamp: storing ctime failed!");
sherman45e33882013-08-28 09:46:55 -0700146 if (extra != null) {
147 // if extra data exists, the current implementation put it at
148 // the end of the extra data array (implementation detail)
149 byte[] extra1 = ze.getExtra();
150 if (extra1 == null || extra1.length < extra.length ||
151 !Arrays.equals(Arrays.copyOfRange(extra1,
152 extra1.length - extra.length,
153 extra1.length),
154 extra)) {
155 throw new RuntimeException("Timestamp: storing extra field failed!");
156 }
157 }
sherman21984b02013-05-29 19:50:47 -0700158 }
redestadb3be0a22015-02-21 13:46:24 +0100159
160 static void testNullHandling() {
161 ZipEntry ze = new ZipEntry("TestExtraTime.java");
162 try {
163 ze.setLastAccessTime(null);
164 throw new RuntimeException("setLastAccessTime(null) should throw NPE");
165 } catch (NullPointerException ignored) {
166 // pass
167 }
168 try {
169 ze.setCreationTime(null);
170 throw new RuntimeException("setCreationTime(null) should throw NPE");
171 } catch (NullPointerException ignored) {
172 // pass
173 }
174 try {
175 ze.setLastModifiedTime(null);
176 throw new RuntimeException("setLastModifiedTime(null) should throw NPE");
177 } catch (NullPointerException ignored) {
178 // pass
179 }
180 }
sherman21984b02013-05-29 19:50:47 -0700181}