test/jdk/java/util/zip/ZipFile/ZipFileInputStreamSkipTest.java
author lancea
Wed, 30 Oct 2019 15:54:41 -0400
changeset 58864 fba8635290df
child 59057 c8e15590c7cc
permissions -rw-r--r--
8231451: ZipFileInputStream::skip handling of negative values with STORED entries Reviewed-by: clanger, bpb, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58864
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
     1
/*
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
     4
 *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
     8
 *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    13
 * accompanied this code).
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    14
 *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    18
 *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    21
 * questions.
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    22
 *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    23
 */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    24
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    25
import org.testng.annotations.AfterClass;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    26
import org.testng.annotations.BeforeClass;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    27
import org.testng.annotations.Test;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    28
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    29
import java.io.IOException;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    30
import java.io.InputStream;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    31
import java.nio.charset.StandardCharsets;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    32
import java.nio.file.FileSystem;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    33
import java.nio.file.FileSystems;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    34
import java.nio.file.Files;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    35
import java.nio.file.Path;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    36
import java.util.Arrays;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    37
import java.util.HashMap;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    38
import java.util.Map;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    39
import java.util.zip.ZipEntry;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    40
import java.util.zip.ZipFile;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    41
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    42
import static org.testng.Assert.*;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    43
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    44
/**
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    45
 * @test
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    46
 * @bug 8231451
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    47
 * @summary Basic tests for ZipFileInputStream::skip
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    48
 * @modules jdk.zipfs
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    49
 * @run testng/othervm ZipFileInputStreamSkipTest
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    50
 */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    51
public class ZipFileInputStreamSkipTest {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    52
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    53
    // Stored and Deflated Zip File paths used by the tests
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    54
    private final Path STORED_ZIPFILE = Path.of("skipStoredEntries.zip");
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    55
    private final Path DEFLATED_ZIPFILE = Path.of("skipDeflatedEntries.zip");
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    56
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    57
    // Saved Entries added to the relevant Zip file
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    58
    private final HashMap<String, Entry> STORED_ZIP_ENTRIES = new HashMap<>();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    59
    private final HashMap<String, Entry> DEFLATED_ZIP_ENTRIES = new HashMap<>();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    60
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    61
    /**
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    62
     * Create the Zip Files used by the tests
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    63
     *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    64
     * @throws IOException If an error occurs creating the Zip Files
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    65
     */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    66
    @BeforeClass
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    67
    private void createZip() throws IOException {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    68
        Entry e0 = Entry.of("Entry-0", ZipEntry.STORED, "Tennis Pro");
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    69
        Entry e1 = Entry.of("Entry-1", ZipEntry.STORED,
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    70
                "United States Tennis Association");
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    71
        Entry e2 = Entry.of("Entry-2", ZipEntry.DEFLATED, "Cardio Tennis");
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    72
        Entry e3 = Entry.of("Entry-3", ZipEntry.DEFLATED, "USTA League Championships");
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    73
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    74
        // Add entries
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    75
        STORED_ZIP_ENTRIES.put(e0.name, e0);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    76
        STORED_ZIP_ENTRIES.put(e1.name, e1);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    77
        DEFLATED_ZIP_ENTRIES.put(e2.name, e2);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    78
        DEFLATED_ZIP_ENTRIES.put(e3.name, e3);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    79
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    80
        Files.deleteIfExists(STORED_ZIPFILE);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    81
        Files.deleteIfExists(DEFLATED_ZIPFILE);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    82
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    83
        createZipFile(STORED_ZIPFILE,
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    84
                Map.of("create", "true", "noCompression", "true"),
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    85
                e0, e1);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    86
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    87
        createZipFile(DEFLATED_ZIPFILE, Map.of("create", "true"), e2, e3);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    88
    }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    89
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    90
    /**
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    91
     * Delete Zip Files created for the test
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    92
     *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    93
     * @throws IOException If an error occurs during cleanup
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    94
     */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    95
    @AfterClass
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    96
    private void cleanUp() throws IOException {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    97
        Files.deleteIfExists(STORED_ZIPFILE);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    98
        Files.deleteIfExists(DEFLATED_ZIPFILE);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
    99
    }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   100
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   101
    /**
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   102
     * Validate that you can skip forward within a STORED entry
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   103
     * and then read the expected data for the entry
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   104
     *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   105
     * @throws Exception If an error occurs during the test
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   106
     */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   107
    @Test
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   108
    private void testStoredSkip() throws Exception {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   109
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   110
        try (ZipFile zf = new ZipFile(STORED_ZIPFILE.toFile())) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   111
            var entries = zf.entries();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   112
            while (entries.hasMoreElements()) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   113
                var entry = entries.nextElement();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   114
                var entrySize = entry.getSize();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   115
                long midpoint = entrySize / 2;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   116
                Entry expected = STORED_ZIP_ENTRIES.get(entry.getName());
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   117
                assertNotNull(expected);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   118
                try (InputStream in = zf.getInputStream(entry)) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   119
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   120
                    // Check that if we specify 0, that we return the correct
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   121
                    // skip value value
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   122
                    assertEquals(in.skip(0), 0);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   123
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   124
                    // Try to skip past EOF and should return remaining bytes
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   125
                    assertEquals(in.skip(entrySize + 100), entrySize);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   126
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   127
                    // Return to BOF and then specify a value which would
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   128
                    // overflow the projected skip value and return the
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   129
                    // number of bytes moved to reach EOF
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   130
                    assertEquals(in.skip(-entrySize), -entrySize);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   131
                    assertEquals(in.skip(Long.MAX_VALUE), entrySize);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   132
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   133
                    // From midpoint, try to skip past EOF and then skip back
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   134
                    // to BOF
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   135
                    assertEquals(in.skip(-entrySize), -entrySize);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   136
                    assertEquals(in.skip(midpoint), midpoint);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   137
                    assertEquals(in.skip(1000), entrySize - midpoint);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   138
                    assertEquals(in.skip(-entrySize), -entrySize);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   139
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   140
                    // Read remaining bytes and validate against expected bytes
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   141
                    byte[] bytes = in.readAllBytes();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   142
                    assertEquals(bytes, expected.bytes);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   143
                    assertEquals(bytes.length, expected.bytes.length);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   144
                }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   145
            }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   146
        }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   147
    }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   148
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   149
    /**
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   150
     * Validate that you can skip backwards within a STORED entry
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   151
     * and then read the expected data for the entry
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   152
     *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   153
     * @throws Exception If an error occurs during the test
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   154
     */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   155
    @Test
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   156
    private void testStoredNegativeSkip() throws Exception {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   157
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   158
        try (ZipFile zf = new ZipFile(STORED_ZIPFILE.toFile())) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   159
            var entries = zf.entries();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   160
            while (entries.hasMoreElements()) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   161
                var entry = entries.nextElement();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   162
                var entrySize = entry.getSize();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   163
                var midpoint = entrySize / 2;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   164
                Entry expected = STORED_ZIP_ENTRIES.get(entry.getName());
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   165
                assertNotNull(expected);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   166
                try (InputStream in = zf.getInputStream(entry)) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   167
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   168
                    // Check that if you try to move past BOF
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   169
                    // that we return the correct value
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   170
                    assertEquals(in.skip(-1), 0);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   171
                    assertEquals(in.skip(-100), 0);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   172
                    assertEquals(in.skip(Long.MIN_VALUE), 0);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   173
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   174
                    // Go to midpoint in file; then specify a value before
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   175
                    // BOF which should result in the number of
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   176
                    // bytes to BOF returned
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   177
                    assertEquals(in.skip(midpoint), midpoint);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   178
                    assertEquals(in.skip(-(midpoint + 10)), -midpoint);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   179
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   180
                    // From midpoint, move back a couple of bytes
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   181
                    assertEquals(in.skip(midpoint), midpoint);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   182
                    assertEquals(in.skip(-2), -2);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   183
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   184
                    // Read the remaining bytes and compare to the expected bytes
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   185
                    byte[] bytes = in.readAllBytes();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   186
                    assertEquals(bytes, Arrays.copyOfRange(expected.bytes,
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   187
                            (int)midpoint - 2, (int) entrySize));
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   188
                    assertEquals(bytes.length, entrySize - midpoint + 2);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   189
                }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   190
            }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   191
        }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   192
    }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   193
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   194
    /**
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   195
     * Validate that you can skip forward within a DEFLATED entry
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   196
     * and then read the expected data for the entry
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   197
     *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   198
     * @throws Exception If an error occurs during the test
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   199
     */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   200
    @Test
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   201
    private void testDeflatedSkip() throws Exception {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   202
        try (ZipFile zf = new ZipFile(DEFLATED_ZIPFILE.toFile())) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   203
            var toSkip = 5; // Bytes to Skip
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   204
            var entries = zf.entries();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   205
            while (entries.hasMoreElements()) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   206
                var entry = entries.nextElement();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   207
                Entry expected = DEFLATED_ZIP_ENTRIES.get(entry.getName());
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   208
                assertNotNull(expected);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   209
                try (InputStream in = zf.getInputStream(entry)) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   210
                    assertEquals(in.skip(toSkip), toSkip);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   211
                    byte[] bytes = in.readAllBytes();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   212
                    var ebytes = Arrays.copyOfRange(expected.bytes,
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   213
                            toSkip, expected.bytes.length);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   214
                    assertEquals(bytes, ebytes);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   215
                    assertEquals(bytes.length, expected.bytes.length - toSkip);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   216
                }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   217
            }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   218
        }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   219
    }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   220
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   221
    /**
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   222
     * Validate that an IllegalArgumentException is thrown if you specify
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   223
     * a negative skip value for a DEFLATED entry.
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   224
     *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   225
     * @throws Exception If an unexpected error occurs during the test
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   226
     */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   227
    @Test
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   228
    private void testDeflatedIOException() throws Exception {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   229
        try (ZipFile zf = new ZipFile(DEFLATED_ZIPFILE.toFile())) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   230
            var entries = zf.entries();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   231
            while (entries.hasMoreElements()) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   232
                var entry = entries.nextElement();
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   233
                assertNotNull(entry);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   234
                try (InputStream in = zf.getInputStream(entry)) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   235
                    // Cannot specify a negative value
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   236
                    assertThrows(IllegalArgumentException.class, () -> in.skip((-1)));
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   237
                }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   238
            }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   239
        }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   240
    }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   241
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   242
    /**
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   243
     * Create a Zip File System using the specified properties and a Zip file
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   244
     * with the specified number of entries
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   245
     *
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   246
     * @param zipFile Path to the Zip File to create
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   247
     * @param env     Properties used for creating the Zip Filesystem
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   248
     * @param entries The entries to add to the Zip File
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   249
     * @throws IOException If an error occurs while creating the Zip file
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   250
     */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   251
    private void createZipFile(Path zipFile, Map<String, String> env,
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   252
                               Entry... entries) throws IOException {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   253
        try (FileSystem zipfs =
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   254
                     FileSystems.newFileSystem(zipFile, env)) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   255
            for (Entry e : entries) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   256
                Files.writeString(zipfs.getPath(e.name), new String(e.bytes));
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   257
            }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   258
        }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   259
    }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   260
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   261
    /**
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   262
     * Represents an entry in a Zip file. An entry encapsulates a name, a
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   263
     * compression method, and its contents/data.
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   264
     */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   265
    static class Entry {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   266
        private final String name;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   267
        private final int method;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   268
        private final byte[] bytes;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   269
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   270
        Entry(String name, int method, String contents) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   271
            this.name = name;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   272
            this.method = method;
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   273
            this.bytes = contents.getBytes(StandardCharsets.UTF_8);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   274
        }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   275
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   276
        static Entry of(String name, int method, String contents) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   277
            return new Entry(name, method, contents);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   278
        }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   279
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   280
        /**
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   281
         * Returns a new Entry with the same name and compression method as this
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   282
         * Entry but with the given content.
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   283
         */
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   284
        Entry content(String contents) {
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   285
            return new Entry(name, method, contents);
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   286
        }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   287
    }
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   288
fba8635290df 8231451: ZipFileInputStream::skip handling of negative values with STORED entries
lancea
parents:
diff changeset
   289
}