jdk/test/java/util/zip/TestExtraTime.java
author sherman
Wed, 22 Jul 2015 21:43:33 +0000
changeset 31819 ba7cde5a611a
parent 29815 a50c9d80a80f
child 31820 f8214072b376
permissions -rw-r--r--
8075526: Need a way to read and write ZipEntry timestamp using local date/time without tz conversion Summary: to add a pair of set/getTimeLocal() Reviewed-by: ksrini, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     1
/*
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     4
 *
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     8
 *
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    13
 * accompanied this code).
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    14
 *
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    18
 *
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    21
 * questions.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    22
 */
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    23
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    24
/**
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    25
 * @test
31819
ba7cde5a611a 8075526: Need a way to read and write ZipEntry timestamp using local date/time without tz conversion
sherman
parents: 29815
diff changeset
    26
 * @bug 4759491 6303183 7012868 8015666 8023713 8068790 8076641 8075526
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    27
 * @summary Test ZOS and ZIS timestamp in extra field correctly
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    28
 */
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    29
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    30
import java.io.*;
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    31
import java.nio.file.Files;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    32
import java.nio.file.Path;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    33
import java.nio.file.Paths;
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    34
import java.nio.file.attribute.FileTime;
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    35
import java.util.Arrays;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    36
import java.util.TimeZone;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    37
import java.util.concurrent.TimeUnit;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    38
import java.util.zip.ZipEntry;
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    39
import java.util.zip.ZipFile;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    40
import java.util.zip.ZipInputStream;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    41
import java.util.zip.ZipOutputStream;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    42
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    43
public class TestExtraTime {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    44
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    45
    public static void main(String[] args) throws Throwable{
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    46
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    47
        File src = new File(System.getProperty("test.src", "."), "TestExtraTime.java");
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    48
        if (src.exists()) {
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    49
            long time = src.lastModified();
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    50
            FileTime mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    51
            FileTime atime = FileTime.from(time + 300000, TimeUnit.MILLISECONDS);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    52
            FileTime ctime = FileTime.from(time - 300000, TimeUnit.MILLISECONDS);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    53
            TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    54
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    55
            for (byte[] extra : new byte[][] { null, new byte[] {1, 2, 3}}) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    56
                test(mtime, null, null, null, extra);
31819
ba7cde5a611a 8075526: Need a way to read and write ZipEntry timestamp using local date/time without tz conversion
sherman
parents: 29815
diff changeset
    57
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    58
                // ms-dos 1980 epoch problem
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    59
                test(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null, extra);
31819
ba7cde5a611a 8075526: Need a way to read and write ZipEntry timestamp using local date/time without tz conversion
sherman
parents: 29815
diff changeset
    60
                // negative epoch time
ba7cde5a611a 8075526: Need a way to read and write ZipEntry timestamp using local date/time without tz conversion
sherman
parents: 29815
diff changeset
    61
                test(FileTime.from(-100, TimeUnit.DAYS), null, null, null, extra);
ba7cde5a611a 8075526: Need a way to read and write ZipEntry timestamp using local date/time without tz conversion
sherman
parents: 29815
diff changeset
    62
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    63
                // non-default tz
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    64
                test(mtime, null, null, tz, extra);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    65
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    66
                test(mtime, atime, null, null, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    67
                test(mtime, null, ctime, null, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    68
                test(mtime, atime, ctime, null, extra);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    69
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    70
                test(mtime, atime, null, tz, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    71
                test(mtime, null, ctime, tz, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    72
                test(mtime, atime, ctime, tz, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    73
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    74
        }
29095
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
    75
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
    76
        testNullHandling();
29815
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
    77
        testTagOnlyHandling();
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
    78
        testTimeConversions();
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    79
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    80
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    81
    static void test(FileTime mtime, FileTime atime, FileTime ctime,
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    82
                     TimeZone tz, byte[] extra) throws Throwable {
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    83
        System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    84
                          mtime, atime, ctime);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    85
        TimeZone tz0 = TimeZone.getDefault();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    86
        if (tz != null) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    87
            TimeZone.setDefault(tz);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    88
        }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    89
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    90
        ZipOutputStream zos = new ZipOutputStream(baos);
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    91
        ZipEntry ze = new ZipEntry("TestExtraTime.java");
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    92
        ze.setExtra(extra);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    93
        ze.setLastModifiedTime(mtime);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    94
        if (atime != null)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    95
            ze.setLastAccessTime(atime);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    96
        if (ctime != null)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    97
            ze.setCreationTime(ctime);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    98
        zos.putNextEntry(ze);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    99
        zos.write(new byte[] { 1,2 ,3, 4});
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   100
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   101
        // append an extra entry to help check if the length and data
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   102
        // of the extra field are being correctly written (in previous
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   103
        // entry).
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   104
        if (extra != null) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   105
            ze = new ZipEntry("TestExtraEntry");
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   106
            zos.putNextEntry(ze);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   107
        }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   108
        zos.close();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   109
        if (tz != null) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   110
            TimeZone.setDefault(tz0);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   111
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   112
        // ZipInputStream
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   113
        ZipInputStream zis = new ZipInputStream(
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   114
                                 new ByteArrayInputStream(baos.toByteArray()));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   115
        ze = zis.getNextEntry();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   116
        zis.close();
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   117
        check(mtime, atime, ctime, ze, extra);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   118
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   119
        // ZipFile
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   120
        Path zpath = Paths.get(System.getProperty("test.dir", "."),
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   121
                               "TestExtraTime.zip");
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   122
        Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   123
        ZipFile zf = new ZipFile(zpath.toFile());
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   124
        ze = zf.getEntry("TestExtraTime.java");
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   125
        // ZipFile read entry from cen, which does not have a/ctime,
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   126
        // for now.
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   127
        check(mtime, null, null, ze, extra);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   128
        zf.close();
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   129
        Files.delete(zpath);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   130
    }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   131
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   132
    static void check(FileTime mtime, FileTime atime, FileTime ctime,
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   133
                      ZipEntry ze, byte[] extra) {
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   134
        /*
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   135
        System.out.printf("    mtime [%tc]: [%tc]/[%tc]%n",
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   136
                          mtime.to(TimeUnit.MILLISECONDS),
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   137
                          ze.getTime(),
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   138
                          ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   139
         */
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   140
        if (mtime.to(TimeUnit.SECONDS) !=
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   141
            ze.getLastModifiedTime().to(TimeUnit.SECONDS))
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   142
            throw new RuntimeException("Timestamp: storing mtime failed!");
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   143
        if (atime != null &&
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   144
            atime.to(TimeUnit.SECONDS) !=
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   145
            ze.getLastAccessTime().to(TimeUnit.SECONDS))
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   146
            throw new RuntimeException("Timestamp: storing atime failed!");
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   147
        if (ctime != null &&
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   148
            ctime.to(TimeUnit.SECONDS) !=
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   149
            ze.getCreationTime().to(TimeUnit.SECONDS))
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   150
            throw new RuntimeException("Timestamp: storing ctime failed!");
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   151
        if (extra != null) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   152
            // if extra data exists, the current implementation put it at
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   153
            // the end of the extra data array (implementation detail)
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   154
            byte[] extra1 = ze.getExtra();
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   155
            if (extra1 == null || extra1.length < extra.length ||
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   156
                !Arrays.equals(Arrays.copyOfRange(extra1,
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   157
                                                  extra1.length - extra.length,
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   158
                                                  extra1.length),
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   159
                               extra)) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   160
                throw new RuntimeException("Timestamp: storing extra field failed!");
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   161
            }
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   162
        }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   163
    }
29095
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   164
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   165
    static void testNullHandling() {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   166
        ZipEntry ze = new ZipEntry("TestExtraTime.java");
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   167
        try {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   168
            ze.setLastAccessTime(null);
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   169
            throw new RuntimeException("setLastAccessTime(null) should throw NPE");
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   170
        } catch (NullPointerException ignored) {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   171
            // pass
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   172
        }
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   173
        try {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   174
            ze.setCreationTime(null);
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   175
            throw new RuntimeException("setCreationTime(null) should throw NPE");
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   176
        } catch (NullPointerException ignored) {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   177
            // pass
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   178
        }
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   179
        try {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   180
            ze.setLastModifiedTime(null);
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   181
            throw new RuntimeException("setLastModifiedTime(null) should throw NPE");
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   182
        } catch (NullPointerException ignored) {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   183
            // pass
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   184
        }
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   185
    }
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   186
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   187
    // verify that setting and getting any time is possible as per the intent
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   188
    // of 4759491
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   189
    static void testTimeConversions() {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   190
        // Sample across the entire range
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   191
        long step = Long.MAX_VALUE / 100L;
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   192
        testTimeConversions(Long.MIN_VALUE, Long.MAX_VALUE - step, step);
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   193
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   194
        // Samples through the near future
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   195
        long currentTime = System.currentTimeMillis();
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   196
        testTimeConversions(currentTime, currentTime + 1_000_000, 10_000);
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   197
    }
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   198
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   199
    static void testTimeConversions(long from, long to, long step) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   200
        ZipEntry ze = new ZipEntry("TestExtraTime.java");
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   201
        for (long time = from; time <= to; time += step) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   202
            ze.setTime(time);
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   203
            FileTime lastModifiedTime = ze.getLastModifiedTime();
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   204
            if (lastModifiedTime.toMillis() != time) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   205
                throw new RuntimeException("setTime should make getLastModifiedTime " +
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   206
                        "return the specified instant: " + time +
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   207
                        " got: " + lastModifiedTime.toMillis());
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   208
            }
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   209
            if (ze.getTime() != time) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   210
                throw new RuntimeException("getTime after setTime, expected: " +
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   211
                        time + " got: " + ze.getTime());
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   212
            }
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   213
        }
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   214
    }
29815
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   215
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   216
    static void check(ZipEntry ze, byte[] extra) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   217
        if (extra != null) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   218
            byte[] extra1 = ze.getExtra();
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   219
            if (extra1 == null || extra1.length < extra.length ||
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   220
                !Arrays.equals(Arrays.copyOfRange(extra1,
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   221
                                                  extra1.length - extra.length,
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   222
                                                  extra1.length),
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   223
                               extra)) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   224
                throw new RuntimeException("Timestamp: storing extra field failed!");
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   225
            }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   226
        }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   227
    }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   228
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   229
    static void testTagOnlyHandling() throws Throwable {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   230
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   231
        byte[] extra = new byte[] { 0x0a, 0, 4, 0, 0, 0, 0, 0 };
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   232
        try (ZipOutputStream zos = new ZipOutputStream(baos)) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   233
            ZipEntry ze = new ZipEntry("TestExtraTime.java");
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   234
            ze.setExtra(extra);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   235
            zos.putNextEntry(ze);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   236
            zos.write(new byte[] { 1,2 ,3, 4});
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   237
        }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   238
        try (ZipInputStream zis = new ZipInputStream(
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   239
                 new ByteArrayInputStream(baos.toByteArray()))) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   240
            ZipEntry ze = zis.getNextEntry();
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   241
            check(ze, extra);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   242
        }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   243
        Path zpath = Paths.get(System.getProperty("test.dir", "."),
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   244
                               "TestExtraTime.zip");
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   245
        Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   246
        try (ZipFile zf = new ZipFile(zpath.toFile())) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   247
            ZipEntry ze = zf.getEntry("TestExtraTime.java");
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   248
            check(ze, extra);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   249
        } finally {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   250
            Files.delete(zpath);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   251
        }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   252
    }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   253
}