jdk/test/java/util/zip/TestExtraTime.java
author redestad
Sat, 28 Feb 2015 13:17:13 +0100
changeset 29226 b675016fabfd
parent 29095 cb98c9bc4e6d
child 29815 a50c9d80a80f
permissions -rw-r--r--
8073497: Lazy conversion of ZipEntry time Reviewed-by: sherman, plevart
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
29095
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
    26
 * @bug 4759491 6303183 7012868 8015666 8023713 8068790
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
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    44
public class TestExtraTime {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    45
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    46
    public static void main(String[] args) throws Throwable{
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    47
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    48
        File src = new File(System.getProperty("test.src", "."), "TestExtraTime.java");
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    49
        if (src.exists()) {
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    50
            long time = src.lastModified();
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    51
            FileTime mtime = FileTime.from(time, TimeUnit.MILLISECONDS);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    52
            FileTime atime = FileTime.from(time + 300000, TimeUnit.MILLISECONDS);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    53
            FileTime ctime = FileTime.from(time - 300000, TimeUnit.MILLISECONDS);
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    54
            TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    55
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    56
            for (byte[] extra : new byte[][] { null, new byte[] {1, 2, 3}}) {
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    57
                test(mtime, null, null, null, extra);
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);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    60
                // non-default tz
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    61
                test(mtime, null, null, tz, extra);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    62
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    63
                test(mtime, atime, null, null, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    64
                test(mtime, null, ctime, null, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    65
                test(mtime, atime, ctime, null, extra);
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    66
19608
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    67
                test(mtime, atime, null, tz, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    68
                test(mtime, null, ctime, tz, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    69
                test(mtime, atime, ctime, tz, extra);
3a4407bc36d7 8023713: ZipFileSystem crashes on old zip file
sherman
parents: 19374
diff changeset
    70
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    71
        }
29095
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
    72
cb98c9bc4e6d 8068790: ZipEntry/JarEntry.setCreation/LastAccessTime(null) don't throw NPE as specified
redestad
parents: 19608
diff changeset
    73
        testNullHandling();
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
    }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   211
}