test/jdk/java/util/zip/ZipFile/ReadZip.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 39310 jdk/test/java/util/zip/ZipFile/ReadZip.java@fed59f4021c8
child 47223 723486922bfe
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8553
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4822
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4822
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4822
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
39310
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
    25
   @bug 4241361 4842702 4985614 6646605 5032358 6923692 6233323 8144977
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @summary Make sure we can read a zip file.
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 22948
diff changeset
    27
   @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    31
import java.nio.file.Files;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    32
import java.nio.file.Paths;
34686
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
    33
import java.nio.file.NoSuchFileException;
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    34
import java.nio.file.StandardCopyOption;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    35
import java.nio.file.StandardOpenOption;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class ReadZip {
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    39
    private static void unreached (Object o)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        // Should never get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        throw new Exception ("Expected exception was not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main(String args[]) throws Exception {
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    47
        try (ZipFile zf = new ZipFile(new File(System.getProperty("test.src", "."),
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    48
                                               "input.zip"))) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    49
            // Make sure we throw NPE on null objects
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    50
            try { unreached (zf.getEntry(null)); }
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    51
            catch (NullPointerException e) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    53
            try { unreached (zf.getInputStream(null)); }
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    54
            catch (NullPointerException e) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    56
            ZipEntry ze = zf.getEntry("ReadZip.java");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    57
            if (ze == null) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    58
                throw new Exception("cannot read from zip file");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    59
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    61
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    62
        // Make sure we can read the zip file that has some garbage
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    63
        // bytes padded at the end.
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    64
        File newZip = new File(System.getProperty("test.dir", "."), "input2.zip");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    65
        Files.copy(Paths.get(System.getProperty("test.src", ""), "input.zip"),
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    66
                   newZip.toPath(), StandardCopyOption.REPLACE_EXISTING);
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    67
22948
2cf7e7202386 7152892: some jtreg tests fail with permission denied
robm
parents: 9035
diff changeset
    68
        newZip.setWritable(true);
2cf7e7202386 7152892: some jtreg tests fail with permission denied
robm
parents: 9035
diff changeset
    69
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    70
        // pad some bytes
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    71
        try (OutputStream os = Files.newOutputStream(newZip.toPath(),
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    72
                                                     StandardOpenOption.APPEND)) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    73
            os.write(1); os.write(3); os.write(5); os.write(7);
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    74
        }
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    75
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    76
        try (ZipFile zf = new ZipFile(newZip)) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    77
            ZipEntry ze = zf.getEntry("ReadZip.java");
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    78
            if (ze == null) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    79
                throw new Exception("cannot read from zip file");
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    80
            }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    81
        } finally {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    82
            newZip.delete();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    83
        }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    84
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    85
        // Read zip file comment
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    86
        try {
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    87
            try (FileOutputStream fos = new FileOutputStream(newZip);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    88
                 ZipOutputStream zos = new ZipOutputStream(fos))
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    89
            {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    90
                ZipEntry ze = new ZipEntry("ZipEntry");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    91
                zos.putNextEntry(ze);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    92
                zos.write(1); zos.write(2); zos.write(3); zos.write(4);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    93
                zos.closeEntry();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    94
                zos.setComment("This is the comment for testing");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    95
            }
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
    96
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    97
            try (ZipFile zf = new ZipFile(newZip)) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    98
                ZipEntry ze = zf.getEntry("ZipEntry");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
    99
                if (ze == null)
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
   100
                    throw new Exception("cannot read entry from zip file");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
   101
                if (!"This is the comment for testing".equals(zf.getComment()))
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
   102
                    throw new Exception("cannot read comment from zip file");
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 7668
diff changeset
   103
            }
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   104
        } finally {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   105
            newZip.delete();
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   106
        }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   107
39310
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   108
        // Read directory entry
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   109
        try {
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   110
            try (FileOutputStream fos = new FileOutputStream(newZip);
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   111
                 ZipOutputStream zos = new ZipOutputStream(fos))
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   112
            {
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   113
                ZipEntry ze = new ZipEntry("directory/");
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   114
                zos.putNextEntry(ze);
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   115
                zos.closeEntry();
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   116
            }
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   117
            try (ZipFile zf = new ZipFile(newZip)) {
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   118
                ZipEntry ze = zf.getEntry("directory/");
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   119
                if (ze == null || !ze.isDirectory())
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   120
                    throw new RuntimeException("read entry \"directory/\" failed");
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   121
                try (InputStream is = zf.getInputStream(ze)) {
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   122
                    is.available();
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   123
                } catch (Exception x) {
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   124
                    x.printStackTrace();
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   125
                }
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   126
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   127
                ze = zf.getEntry("directory");
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   128
                if (ze == null || !ze.isDirectory())
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   129
                    throw new RuntimeException("read entry \"directory\" failed");
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   130
                try (InputStream is = zf.getInputStream(ze)) {
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   131
                    is.available();
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   132
                } catch (Exception x) {
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   133
                    x.printStackTrace();
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   134
                }
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   135
            }
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   136
        } finally {
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   137
            newZip.delete();
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   138
        }
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   139
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   140
fed59f4021c8 6233323: ZipEntry.isDirectory() may return false incorrectly
sherman
parents: 34686
diff changeset
   141
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   142
        // Throw a FNF exception when read a non-existing zip file
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   143
        try { unreached (new ZipFile(
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   144
                             new File(System.getProperty("test.src", "."),
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   145
                                     "input"
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   146
                                      + String.valueOf(new java.util.Random().nextInt())
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2
diff changeset
   147
                                      + ".zip")));
34686
29ea8310a27a 8145260: To bring j.u.z.ZipFile's native implementation to Java to remove the expensive jni cost and mmap crash risk [2]
sherman
parents: 34526
diff changeset
   148
        } catch (NoSuchFileException nsfe) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
}