jdk/test/tools/jar/LeadingGarbage.java
author duke
Wed, 05 Jul 2017 20:24:54 +0200
changeset 29530 bfd405c818db
parent 27565 729f9700483a
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
     1
/*
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
     2
 * Copyright 2014 Google Inc.  All Rights Reserved.
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
     4
 *
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
     8
 *
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    13
 * accompanied this code).
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    14
 *
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    18
 *
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    21
 * questions.
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    22
 */
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    23
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    24
import static org.testng.Assert.assertEquals;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    25
import static org.testng.Assert.assertTrue;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    26
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    27
import java.io.File;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    28
import java.io.FileOutputStream;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    29
import java.io.IOException;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    30
import java.io.OutputStream;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    31
import java.nio.file.Files;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    32
import java.nio.file.Paths;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    33
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    34
import jdk.testlibrary.OutputAnalyzer;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    35
import jdk.testlibrary.ProcessTools;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    36
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    37
import org.testng.annotations.Test;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    38
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    39
/**
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    40
 * @test
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    41
 * @bug 8058520
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    42
 * @summary jar tf and jar xf should work on zip files with leading garbage
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    43
 * @library /lib/testlibrary
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    44
 * @run testng LeadingGarbage
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    45
 */
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    46
@Test
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    47
public class LeadingGarbage {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    48
    final String jar =
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27036
diff changeset
    49
        Paths.get(System.getProperty("java.home"), "bin", "jar").toString();
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    50
    final File[] files = { new File("a"), new File("b") };
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    51
    final File normalZip = new File("normal.zip");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    52
    final File leadingGarbageZip = new File("leadingGarbage.zip");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    53
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    54
    void createFile(File f) throws IOException {
27036
2bcadd860672 8058856: tools/jar/LeadingGarbage.java, introduced in JDK-8058520, fails on Windows
alanb
parents: 26714
diff changeset
    55
        try (OutputStream fos = new FileOutputStream(f)) {
2bcadd860672 8058856: tools/jar/LeadingGarbage.java, introduced in JDK-8058520, fails on Windows
alanb
parents: 26714
diff changeset
    56
            fos.write(f.getName().getBytes("UTF-8"));
2bcadd860672 8058856: tools/jar/LeadingGarbage.java, introduced in JDK-8058520, fails on Windows
alanb
parents: 26714
diff changeset
    57
        }
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    58
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    59
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    60
    void createFiles() throws IOException {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    61
        for (File file : files)
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    62
            createFile(file);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    63
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    64
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    65
    void deleteFiles() throws IOException {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    66
        for (File file : files)
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    67
            assertTrue(file.delete());
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    68
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    69
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    70
    void assertFilesExist() throws IOException {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    71
        for (File file : files)
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    72
            assertTrue(file.exists());
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    73
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    74
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    75
    void createNormalZip() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    76
        createFiles();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    77
        String[] cmd = { jar, "c0Mf", "normal.zip", "a", "b" };
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    78
        ProcessBuilder pb = new ProcessBuilder(cmd);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    79
        OutputAnalyzer a = ProcessTools.executeProcess(pb);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    80
        a.shouldHaveExitValue(0);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    81
        a.stdoutShouldMatch("\\A\\Z");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    82
        a.stderrShouldMatch("\\A\\Z");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    83
        deleteFiles();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    84
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    85
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    86
    void createZipWithLeadingGarbage() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    87
        createNormalZip();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    88
        createFile(leadingGarbageZip);
27036
2bcadd860672 8058856: tools/jar/LeadingGarbage.java, introduced in JDK-8058520, fails on Windows
alanb
parents: 26714
diff changeset
    89
        try (OutputStream fos = new FileOutputStream(leadingGarbageZip, true)) {
2bcadd860672 8058856: tools/jar/LeadingGarbage.java, introduced in JDK-8058520, fails on Windows
alanb
parents: 26714
diff changeset
    90
            Files.copy(normalZip.toPath(), fos);
2bcadd860672 8058856: tools/jar/LeadingGarbage.java, introduced in JDK-8058520, fails on Windows
alanb
parents: 26714
diff changeset
    91
        }
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    92
        assertTrue(normalZip.length() < leadingGarbageZip.length());
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    93
        assertTrue(normalZip.delete());
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    94
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    95
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    96
    public void test_canList() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    97
        createNormalZip();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    98
        assertCanList("normal.zip");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    99
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   100
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   101
    public void test_canListWithLeadingGarbage() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   102
        createZipWithLeadingGarbage();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   103
        assertCanList("leadingGarbage.zip");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   104
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   105
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   106
    void assertCanList(String zipFileName) throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   107
        String[] cmd = { jar, "tf", zipFileName };
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   108
        ProcessBuilder pb = new ProcessBuilder(cmd);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   109
        OutputAnalyzer a = ProcessTools.executeProcess(pb);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   110
        a.shouldHaveExitValue(0);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   111
        StringBuilder expected = new StringBuilder();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   112
        for (File file : files)
27036
2bcadd860672 8058856: tools/jar/LeadingGarbage.java, introduced in JDK-8058520, fails on Windows
alanb
parents: 26714
diff changeset
   113
            expected.append(file.getName()).append(System.lineSeparator());
26714
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   114
        a.stdoutShouldMatch(expected.toString());
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   115
        a.stderrShouldMatch("\\A\\Z");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   116
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   117
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   118
    public void test_canExtract() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   119
        createNormalZip();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   120
        assertCanExtract("normal.zip");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   121
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   122
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   123
    public void test_canExtractWithLeadingGarbage() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   124
        createZipWithLeadingGarbage();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   125
        assertCanExtract("leadingGarbage.zip");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   126
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   127
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   128
    void assertCanExtract(String zipFileName) throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   129
        String[] cmd = { jar, "xf", zipFileName };
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   130
        ProcessBuilder pb = new ProcessBuilder(cmd);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   131
        OutputAnalyzer a = ProcessTools.executeProcess(pb);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   132
        a.shouldHaveExitValue(0);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   133
        a.stdoutShouldMatch("\\A\\Z");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   134
        a.stderrShouldMatch("\\A\\Z");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   135
        assertFilesExist();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   136
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   137
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   138
}