sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 1 | /* |
| 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 |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 26 | * @bug 4759491 6303183 7012868 8015666 8023713 |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 27 | * @summary Test ZOS and ZIS timestamp in extra field correctly |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 31 | import java.nio.file.Files; |
| 32 | import java.nio.file.Path; |
| 33 | import java.nio.file.Paths; |
| 34 | import java.nio.file.attribute.FileTime; |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 35 | import java.util.Arrays; |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 36 | import java.util.TimeZone; |
| 37 | import java.util.concurrent.TimeUnit; |
| 38 | import java.util.zip.ZipEntry; |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 39 | import java.util.zip.ZipFile; |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 40 | import java.util.zip.ZipInputStream; |
| 41 | import java.util.zip.ZipOutputStream; |
| 42 | |
| 43 | |
| 44 | public 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()) { |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 50 | 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 | |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 56 | 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); |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 62 | |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 63 | test(mtime, atime, null, null, extra); |
| 64 | test(mtime, null, ctime, null, extra); |
| 65 | test(mtime, atime, ctime, null, extra); |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 66 | |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 67 | test(mtime, atime, null, tz, extra); |
| 68 | test(mtime, null, ctime, tz, extra); |
| 69 | test(mtime, atime, ctime, tz, extra); |
| 70 | } |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 74 | static void test(FileTime mtime, FileTime atime, FileTime ctime, |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 75 | TimeZone tz, byte[] extra) throws Throwable { |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 76 | System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n", |
| 77 | mtime, atime, ctime); |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 78 | TimeZone tz0 = TimeZone.getDefault(); |
| 79 | if (tz != null) { |
| 80 | TimeZone.setDefault(tz); |
| 81 | } |
| 82 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 83 | ZipOutputStream zos = new ZipOutputStream(baos); |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 84 | ZipEntry ze = new ZipEntry("TestExtraTime.java"); |
| 85 | ze.setExtra(extra); |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 86 | ze.setLastModifiedTime(mtime); |
| 87 | if (atime != null) |
| 88 | ze.setLastAccessTime(atime); |
| 89 | if (ctime != null) |
| 90 | ze.setCreationTime(ctime); |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 91 | zos.putNextEntry(ze); |
| 92 | zos.write(new byte[] { 1,2 ,3, 4}); |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 93 | |
| 94 | // append an extra entry to help check if the length and data |
| 95 | // of the extra field are being correctly written (in previous |
| 96 | // entry). |
| 97 | if (extra != null) { |
| 98 | ze = new ZipEntry("TestExtraEntry"); |
| 99 | zos.putNextEntry(ze); |
| 100 | } |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 101 | zos.close(); |
| 102 | if (tz != null) { |
| 103 | TimeZone.setDefault(tz0); |
| 104 | } |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 105 | // ZipInputStream |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 106 | ZipInputStream zis = new ZipInputStream( |
| 107 | new ByteArrayInputStream(baos.toByteArray())); |
| 108 | ze = zis.getNextEntry(); |
| 109 | zis.close(); |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 110 | check(mtime, atime, ctime, ze, extra); |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 111 | |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 112 | // ZipFile |
| 113 | Path zpath = Paths.get(System.getProperty("test.dir", "."), |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 114 | "TestExtraTime.zip"); |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 115 | Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath); |
| 116 | ZipFile zf = new ZipFile(zpath.toFile()); |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 117 | ze = zf.getEntry("TestExtraTime.java"); |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 118 | // ZipFile read entry from cen, which does not have a/ctime, |
| 119 | // for now. |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 120 | check(mtime, null, null, ze, extra); |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 121 | zf.close(); |
| 122 | Files.delete(zpath); |
| 123 | } |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 124 | |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 125 | static void check(FileTime mtime, FileTime atime, FileTime ctime, |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 126 | ZipEntry ze, byte[] extra) { |
sherman | 57eb381 | 2013-08-08 12:03:04 -0700 | [diff] [blame] | 127 | /* |
| 128 | System.out.printf(" mtime [%tc]: [%tc]/[%tc]%n", |
| 129 | mtime.to(TimeUnit.MILLISECONDS), |
| 130 | ze.getTime(), |
| 131 | ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS)); |
| 132 | */ |
| 133 | if (mtime.to(TimeUnit.SECONDS) != |
| 134 | ze.getLastModifiedTime().to(TimeUnit.SECONDS)) |
| 135 | throw new RuntimeException("Timestamp: storing mtime failed!"); |
| 136 | if (atime != null && |
| 137 | atime.to(TimeUnit.SECONDS) != |
| 138 | ze.getLastAccessTime().to(TimeUnit.SECONDS)) |
| 139 | throw new RuntimeException("Timestamp: storing atime failed!"); |
| 140 | if (ctime != null && |
| 141 | ctime.to(TimeUnit.SECONDS) != |
| 142 | ze.getCreationTime().to(TimeUnit.SECONDS)) |
| 143 | throw new RuntimeException("Timestamp: storing ctime failed!"); |
sherman | 45e3388 | 2013-08-28 09:46:55 -0700 | [diff] [blame^] | 144 | if (extra != null) { |
| 145 | // if extra data exists, the current implementation put it at |
| 146 | // the end of the extra data array (implementation detail) |
| 147 | byte[] extra1 = ze.getExtra(); |
| 148 | if (extra1 == null || extra1.length < extra.length || |
| 149 | !Arrays.equals(Arrays.copyOfRange(extra1, |
| 150 | extra1.length - extra.length, |
| 151 | extra1.length), |
| 152 | extra)) { |
| 153 | throw new RuntimeException("Timestamp: storing extra field failed!"); |
| 154 | } |
| 155 | } |
sherman | 21984b0 | 2013-05-29 19:50:47 -0700 | [diff] [blame] | 156 | } |
| 157 | } |