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
  26  * @bug 4759491 6303183 7012868 8015666
  27  * @summary Test ZOS and ZIS timestamp in extra field correctly
  28  */
  29 
  30 import java.io.*;
  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;
  35 import java.util.TimeZone;
  36 import java.util.concurrent.TimeUnit;
  37 import java.util.zip.ZipEntry;
  38 import java.util.zip.ZipFile;
  39 import java.util.zip.ZipInputStream;
  40 import java.util.zip.ZipOutputStream;
  41 
  42 
  43 public 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()) {
  49             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);
  68         }
  69     }
  70 
  71     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);
  75         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 
  83         ze.setLastModifiedTime(mtime);
  84         if (atime != null)
  85             ze.setLastAccessTime(atime);
  86         if (ctime != null)
  87             ze.setCreationTime(ctime);
  88         zos.putNextEntry(ze);
  89         zos.write(new byte[] { 1,2 ,3, 4});
  90         zos.close();
  91         if (tz != null) {
  92             TimeZone.setDefault(tz0);
  93         }
  94         // ZipInputStream
  95         ZipInputStream zis = new ZipInputStream(
  96                                  new ByteArrayInputStream(baos.toByteArray()));
  97         ze = zis.getNextEntry();
  98         zis.close();
  99         check(mtime, atime, ctime, ze);
 100 
 101         // 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     }
 113 
 114     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!");
 133     }
 134 }