1 /*
   2  * Copyright (c) 2013, 2015, 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 8023713 8068790 8076641 8075526
  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.Arrays;
  36 import java.util.TimeZone;
  37 import java.util.concurrent.TimeUnit;
  38 import java.util.zip.ZipEntry;
  39 import java.util.zip.ZipFile;
  40 import java.util.zip.ZipInputStream;
  41 import java.util.zip.ZipOutputStream;
  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             for (byte[] extra : new byte[][] { null, new byte[] {1, 2, 3}}) {
  56                 test(mtime, null, null, null, extra);
  57 
  58                 // ms-dos 1980 epoch problem
  59                 test(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null, extra);
  60                 // negative epoch time
  61                 test(FileTime.from(-100, TimeUnit.DAYS), null, null, null, extra);
  62 
  63                 // non-default tz
  64                 test(mtime, null, null, tz, extra);
  65 
  66                 test(mtime, atime, null, null, extra);
  67                 test(mtime, null, ctime, null, extra);
  68                 test(mtime, atime, ctime, null, extra);
  69 
  70                 test(mtime, atime, null, tz, extra);
  71                 test(mtime, null, ctime, tz, extra);
  72                 test(mtime, atime, ctime, tz, extra);
  73             }
  74         }
  75 
  76         testNullHandling();
  77         testTagOnlyHandling();
  78         testTimeConversions();
  79     }
  80 
  81     static void test(FileTime mtime, FileTime atime, FileTime ctime,
  82                      TimeZone tz, byte[] extra) throws Throwable {
  83         System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
  84                           mtime, atime, ctime);
  85         TimeZone tz0 = TimeZone.getDefault();
  86         if (tz != null) {
  87             TimeZone.setDefault(tz);
  88         }
  89         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  90         ZipOutputStream zos = new ZipOutputStream(baos);
  91         ZipEntry ze = new ZipEntry("TestExtraTime.java");
  92         ze.setExtra(extra);
  93         ze.setLastModifiedTime(mtime);
  94         if (atime != null)
  95             ze.setLastAccessTime(atime);
  96         if (ctime != null)
  97             ze.setCreationTime(ctime);
  98         zos.putNextEntry(ze);
  99         zos.write(new byte[] { 1,2 ,3, 4});
 100 
 101         // append an extra entry to help check if the length and data
 102         // of the extra field are being correctly written (in previous
 103         // entry).
 104         if (extra != null) {
 105             ze = new ZipEntry("TestExtraEntry");
 106             zos.putNextEntry(ze);
 107         }
 108         zos.close();
 109         if (tz != null) {
 110             TimeZone.setDefault(tz0);
 111         }
 112         // ZipInputStream
 113         ZipInputStream zis = new ZipInputStream(
 114                                  new ByteArrayInputStream(baos.toByteArray()));
 115         ze = zis.getNextEntry();
 116         zis.close();
 117         check(mtime, atime, ctime, ze, extra);
 118 
 119         // ZipFile
 120         Path zpath = Paths.get(System.getProperty("test.dir", "."),
 121                                "TestExtraTime.zip");
 122         Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
 123         ZipFile zf = new ZipFile(zpath.toFile());
 124         ze = zf.getEntry("TestExtraTime.java");
 125         // ZipFile read entry from cen, which does not have a/ctime,
 126         // for now.
 127         check(mtime, null, null, ze, extra);
 128         zf.close();
 129         Files.delete(zpath);
 130     }
 131 
 132     static void check(FileTime mtime, FileTime atime, FileTime ctime,
 133                       ZipEntry ze, byte[] extra) {
 134         /*
 135         System.out.printf("    mtime [%tc]: [%tc]/[%tc]%n",
 136                           mtime.to(TimeUnit.MILLISECONDS),
 137                           ze.getTime(),
 138                           ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
 139          */
 140         if (mtime.to(TimeUnit.SECONDS) !=
 141             ze.getLastModifiedTime().to(TimeUnit.SECONDS))
 142             throw new RuntimeException("Timestamp: storing mtime failed!");
 143         if (atime != null &&
 144             atime.to(TimeUnit.SECONDS) !=
 145             ze.getLastAccessTime().to(TimeUnit.SECONDS))
 146             throw new RuntimeException("Timestamp: storing atime failed!");
 147         if (ctime != null &&
 148             ctime.to(TimeUnit.SECONDS) !=
 149             ze.getCreationTime().to(TimeUnit.SECONDS))
 150             throw new RuntimeException("Timestamp: storing ctime failed!");
 151         if (extra != null) {
 152             // if extra data exists, the current implementation put it at
 153             // the end of the extra data array (implementation detail)
 154             byte[] extra1 = ze.getExtra();
 155             if (extra1 == null || extra1.length < extra.length ||
 156                 !Arrays.equals(Arrays.copyOfRange(extra1,
 157                                                   extra1.length - extra.length,
 158                                                   extra1.length),
 159                                extra)) {
 160                 throw new RuntimeException("Timestamp: storing extra field failed!");
 161             }
 162         }
 163     }
 164 
 165     static void testNullHandling() {
 166         ZipEntry ze = new ZipEntry("TestExtraTime.java");
 167         try {
 168             ze.setLastAccessTime(null);
 169             throw new RuntimeException("setLastAccessTime(null) should throw NPE");
 170         } catch (NullPointerException ignored) {
 171             // pass
 172         }
 173         try {
 174             ze.setCreationTime(null);
 175             throw new RuntimeException("setCreationTime(null) should throw NPE");
 176         } catch (NullPointerException ignored) {
 177             // pass
 178         }
 179         try {
 180             ze.setLastModifiedTime(null);
 181             throw new RuntimeException("setLastModifiedTime(null) should throw NPE");
 182         } catch (NullPointerException ignored) {
 183             // pass
 184         }
 185     }
 186 
 187     // verify that setting and getting any time is possible as per the intent
 188     // of 4759491
 189     static void testTimeConversions() {
 190         // Sample across the entire range
 191         long step = Long.MAX_VALUE / 100L;
 192         testTimeConversions(Long.MIN_VALUE, Long.MAX_VALUE - step, step);
 193 
 194         // Samples through the near future
 195         long currentTime = System.currentTimeMillis();
 196         testTimeConversions(currentTime, currentTime + 1_000_000, 10_000);
 197     }
 198 
 199     static void testTimeConversions(long from, long to, long step) {
 200         ZipEntry ze = new ZipEntry("TestExtraTime.java");
 201         for (long time = from; time <= to; time += step) {
 202             ze.setTime(time);
 203             FileTime lastModifiedTime = ze.getLastModifiedTime();
 204             if (lastModifiedTime.toMillis() != time) {
 205                 throw new RuntimeException("setTime should make getLastModifiedTime " +
 206                         "return the specified instant: " + time +
 207                         " got: " + lastModifiedTime.toMillis());
 208             }
 209             if (ze.getTime() != time) {
 210                 throw new RuntimeException("getTime after setTime, expected: " +
 211                         time + " got: " + ze.getTime());
 212             }
 213         }
 214     }
 215 
 216     static void check(ZipEntry ze, byte[] extra) {
 217         if (extra != null) {
 218             byte[] extra1 = ze.getExtra();
 219             if (extra1 == null || extra1.length < extra.length ||
 220                 !Arrays.equals(Arrays.copyOfRange(extra1,
 221                                                   extra1.length - extra.length,
 222                                                   extra1.length),
 223                                extra)) {
 224                 throw new RuntimeException("Timestamp: storing extra field failed!");
 225             }
 226         }
 227     }
 228 
 229     static void testTagOnlyHandling() throws Throwable {
 230         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 231         byte[] extra = new byte[] { 0x0a, 0, 4, 0, 0, 0, 0, 0 };
 232         try (ZipOutputStream zos = new ZipOutputStream(baos)) {
 233             ZipEntry ze = new ZipEntry("TestExtraTime.java");
 234             ze.setExtra(extra);
 235             zos.putNextEntry(ze);
 236             zos.write(new byte[] { 1,2 ,3, 4});
 237         }
 238         try (ZipInputStream zis = new ZipInputStream(
 239                  new ByteArrayInputStream(baos.toByteArray()))) {
 240             ZipEntry ze = zis.getNextEntry();
 241             check(ze, extra);
 242         }
 243         Path zpath = Paths.get(System.getProperty("test.dir", "."),
 244                                "TestExtraTime.zip");
 245         Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
 246         try (ZipFile zf = new ZipFile(zpath.toFile())) {
 247             ZipEntry ze = zf.getEntry("TestExtraTime.java");
 248             check(ze, extra);
 249         } finally {
 250             Files.delete(zpath);
 251         }
 252     }
 253 }