jdk/test/java/util/zip/TestExtraTime.java
author sherman
Mon, 06 Apr 2015 13:41:10 -0700
changeset 29815 a50c9d80a80f
parent 29226 b675016fabfd
child 31819 ba7cde5a611a
permissions -rw-r--r--
8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file Summary: to add extra sanity check for entry extra data Reviewed-by: alanb
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
29815
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
    26
 * @bug 4759491 6303183 7012868 8015666 8023713 8068790 8076641
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);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    57
                // ms-dos 1980 epoch problem
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    58
                test(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    59
                // non-default tz
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    60
                test(mtime, null, null, tz, extra);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    61
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    62
                test(mtime, atime, null, null, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    63
                test(mtime, null, ctime, null, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    64
                test(mtime, atime, ctime, null, 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, tz, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    67
                test(mtime, null, ctime, tz, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    68
                test(mtime, atime, ctime, tz, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    69
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    70
        }
29095
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
    71
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
    72
        testNullHandling();
29815
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
    73
        testTagOnlyHandling();
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
    74
        testTimeConversions();
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    75
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    76
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    77
    static void test(FileTime mtime, FileTime atime, FileTime ctime,
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    78
                     TimeZone tz, byte[] extra) throws Throwable {
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    79
        System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    80
                          mtime, atime, ctime);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    81
        TimeZone tz0 = TimeZone.getDefault();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    82
        if (tz != null) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    83
            TimeZone.setDefault(tz);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    84
        }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    85
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    86
        ZipOutputStream zos = new ZipOutputStream(baos);
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    87
        ZipEntry ze = new ZipEntry("TestExtraTime.java");
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    88
        ze.setExtra(extra);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    89
        ze.setLastModifiedTime(mtime);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    90
        if (atime != null)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    91
            ze.setLastAccessTime(atime);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    92
        if (ctime != null)
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    93
            ze.setCreationTime(ctime);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    94
        zos.putNextEntry(ze);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    95
        zos.write(new byte[] { 1,2 ,3, 4});
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    96
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    97
        // 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
    98
        // of the extra field are being correctly written (in previous
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    99
        // entry).
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   100
        if (extra != null) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   101
            ze = new ZipEntry("TestExtraEntry");
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   102
            zos.putNextEntry(ze);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   103
        }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   104
        zos.close();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   105
        if (tz != null) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   106
            TimeZone.setDefault(tz0);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   107
        }
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   108
        // ZipInputStream
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   109
        ZipInputStream zis = new ZipInputStream(
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   110
                                 new ByteArrayInputStream(baos.toByteArray()));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   111
        ze = zis.getNextEntry();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   112
        zis.close();
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   113
        check(mtime, atime, ctime, ze, extra);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   114
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   115
        // ZipFile
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   116
        Path zpath = Paths.get(System.getProperty("test.dir", "."),
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   117
                               "TestExtraTime.zip");
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   118
        Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   119
        ZipFile zf = new ZipFile(zpath.toFile());
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   120
        ze = zf.getEntry("TestExtraTime.java");
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   121
        // ZipFile read entry from cen, which does not have a/ctime,
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   122
        // for now.
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   123
        check(mtime, null, null, ze, extra);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   124
        zf.close();
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   125
        Files.delete(zpath);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   126
    }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   127
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   128
    static void check(FileTime mtime, FileTime atime, FileTime ctime,
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   129
                      ZipEntry ze, byte[] extra) {
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   130
        /*
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   131
        System.out.printf("    mtime [%tc]: [%tc]/[%tc]%n",
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   132
                          mtime.to(TimeUnit.MILLISECONDS),
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   133
                          ze.getTime(),
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   134
                          ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   135
         */
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   136
        if (mtime.to(TimeUnit.SECONDS) !=
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   137
            ze.getLastModifiedTime().to(TimeUnit.SECONDS))
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   138
            throw new RuntimeException("Timestamp: storing mtime failed!");
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   139
        if (atime != null &&
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   140
            atime.to(TimeUnit.SECONDS) !=
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   141
            ze.getLastAccessTime().to(TimeUnit.SECONDS))
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   142
            throw new RuntimeException("Timestamp: storing atime failed!");
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   143
        if (ctime != null &&
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   144
            ctime.to(TimeUnit.SECONDS) !=
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   145
            ze.getCreationTime().to(TimeUnit.SECONDS))
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
   146
            throw new RuntimeException("Timestamp: storing ctime failed!");
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   147
        if (extra != null) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   148
            // if extra data exists, the current implementation put it at
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   149
            // the end of the extra data array (implementation detail)
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   150
            byte[] extra1 = ze.getExtra();
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   151
            if (extra1 == null || extra1.length < extra.length ||
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   152
                !Arrays.equals(Arrays.copyOfRange(extra1,
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   153
                                                  extra1.length - extra.length,
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   154
                                                  extra1.length),
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   155
                               extra)) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   156
                throw new RuntimeException("Timestamp: storing extra field failed!");
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   157
            }
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
   158
        }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   159
    }
29095
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   160
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   161
    static void testNullHandling() {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   162
        ZipEntry ze = new ZipEntry("TestExtraTime.java");
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   163
        try {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   164
            ze.setLastAccessTime(null);
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   165
            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
   166
        } catch (NullPointerException ignored) {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   167
            // pass
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   168
        }
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   169
        try {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   170
            ze.setCreationTime(null);
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   171
            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
   172
        } catch (NullPointerException ignored) {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   173
            // pass
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   174
        }
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   175
        try {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   176
            ze.setLastModifiedTime(null);
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   177
            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
   178
        } catch (NullPointerException ignored) {
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   179
            // pass
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   180
        }
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
   181
    }
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   182
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   183
    // 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
   184
    // of 4759491
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   185
    static void testTimeConversions() {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   186
        // Sample across the entire range
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   187
        long step = Long.MAX_VALUE / 100L;
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   188
        testTimeConversions(Long.MIN_VALUE, Long.MAX_VALUE - step, step);
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   189
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   190
        // Samples through the near future
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   191
        long currentTime = System.currentTimeMillis();
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   192
        testTimeConversions(currentTime, currentTime + 1_000_000, 10_000);
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
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   195
    static void testTimeConversions(long from, long to, long step) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   196
        ZipEntry ze = new ZipEntry("TestExtraTime.java");
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   197
        for (long time = from; time <= to; time += step) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   198
            ze.setTime(time);
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   199
            FileTime lastModifiedTime = ze.getLastModifiedTime();
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   200
            if (lastModifiedTime.toMillis() != time) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   201
                throw new RuntimeException("setTime should make getLastModifiedTime " +
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   202
                        "return the specified instant: " + time +
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   203
                        " got: " + lastModifiedTime.toMillis());
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   204
            }
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   205
            if (ze.getTime() != time) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   206
                throw new RuntimeException("getTime after setTime, expected: " +
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   207
                        time + " got: " + ze.getTime());
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
        }
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 29095
diff changeset
   210
    }
29815
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   211
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   212
    static void check(ZipEntry ze, byte[] extra) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   213
        if (extra != null) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   214
            byte[] extra1 = ze.getExtra();
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   215
            if (extra1 == null || extra1.length < extra.length ||
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   216
                !Arrays.equals(Arrays.copyOfRange(extra1,
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   217
                                                  extra1.length - extra.length,
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   218
                                                  extra1.length),
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   219
                               extra)) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   220
                throw new RuntimeException("Timestamp: storing extra field failed!");
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   221
            }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   222
        }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   223
    }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   224
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   225
    static void testTagOnlyHandling() throws Throwable {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   226
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   227
        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
   228
        try (ZipOutputStream zos = new ZipOutputStream(baos)) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   229
            ZipEntry ze = new ZipEntry("TestExtraTime.java");
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   230
            ze.setExtra(extra);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   231
            zos.putNextEntry(ze);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   232
            zos.write(new byte[] { 1,2 ,3, 4});
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   233
        }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   234
        try (ZipInputStream zis = new ZipInputStream(
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   235
                 new ByteArrayInputStream(baos.toByteArray()))) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   236
            ZipEntry ze = zis.getNextEntry();
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   237
            check(ze, extra);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   238
        }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   239
        Path zpath = Paths.get(System.getProperty("test.dir", "."),
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   240
                               "TestExtraTime.zip");
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   241
        Files.copy(new ByteArrayInputStream(baos.toByteArray()), zpath);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   242
        try (ZipFile zf = new ZipFile(zpath.toFile())) {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   243
            ZipEntry ze = zf.getEntry("TestExtraTime.java");
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   244
            check(ze, extra);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   245
        } finally {
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   246
            Files.delete(zpath);
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   247
        }
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29226
diff changeset
   248
    }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   249
}