jdk/test/tools/jar/LeadingGarbage.java
author jwilhelm
Fri, 03 Oct 2014 00:21:49 +0200
changeset 26940 b5b002c2980b
parent 26714 3caa40031b3f
child 27036 2bcadd860672
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
import sun.tools.jar.Main;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    34
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    35
import jdk.testlibrary.OutputAnalyzer;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    36
import jdk.testlibrary.ProcessTools;
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    37
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    38
import org.testng.annotations.Test;
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
/**
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    41
 * @test
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    42
 * @bug 8058520
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    43
 * @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
    44
 * @library /lib/testlibrary
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    45
 * @run testng LeadingGarbage
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    46
 */
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    47
@Test
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    48
public class LeadingGarbage {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    49
    final String jar =
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    50
        Paths.get(new File(System.getProperty("java.home")).getParent(),
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    51
                  "bin", "jar").toString();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    52
    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
    53
    final File normalZip = new File("normal.zip");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    54
    final File leadingGarbageZip = new File("leadingGarbage.zip");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    55
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    56
    void createFile(File f) throws IOException {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    57
        OutputStream fos = new FileOutputStream(f);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    58
        fos.write(f.getName().getBytes("UTF-8"));
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    59
        fos.close();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    60
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    61
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    62
    void createFiles() throws IOException {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    63
        for (File file : files)
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    64
            createFile(file);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    65
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    66
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    67
    void deleteFiles() throws IOException {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    68
        for (File file : files)
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    69
            assertTrue(file.delete());
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    70
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    71
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    72
    void assertFilesExist() throws IOException {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    73
        for (File file : files)
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    74
            assertTrue(file.exists());
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    75
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    76
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    77
    void createNormalZip() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    78
        createFiles();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    79
        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
    80
        ProcessBuilder pb = new ProcessBuilder(cmd);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    81
        OutputAnalyzer a = ProcessTools.executeProcess(pb);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    82
        a.shouldHaveExitValue(0);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    83
        a.stdoutShouldMatch("\\A\\Z");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    84
        a.stderrShouldMatch("\\A\\Z");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    85
        deleteFiles();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    86
    }
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    87
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    88
    void createZipWithLeadingGarbage() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    89
        createNormalZip();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    90
        createFile(leadingGarbageZip);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    91
        OutputStream fos = new FileOutputStream(leadingGarbageZip, true);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    92
        Files.copy(normalZip.toPath(), fos);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    93
        assertTrue(normalZip.length() < leadingGarbageZip.length());
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    94
        assertTrue(normalZip.delete());
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
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    97
    public void test_canList() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    98
        createNormalZip();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
    99
        assertCanList("normal.zip");
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
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   102
    public void test_canListWithLeadingGarbage() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   103
        createZipWithLeadingGarbage();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   104
        assertCanList("leadingGarbage.zip");
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
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   107
    void assertCanList(String zipFileName) throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   108
        String[] cmd = { jar, "tf", zipFileName };
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   109
        ProcessBuilder pb = new ProcessBuilder(cmd);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   110
        OutputAnalyzer a = ProcessTools.executeProcess(pb);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   111
        a.shouldHaveExitValue(0);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   112
        StringBuilder expected = new StringBuilder();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   113
        for (File file : files)
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   114
            expected.append(file.getName()).append('\n');
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   115
        a.stdoutShouldMatch(expected.toString());
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   116
        a.stderrShouldMatch("\\A\\Z");
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
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   119
    public void test_canExtract() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   120
        createNormalZip();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   121
        assertCanExtract("normal.zip");
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
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   124
    public void test_canExtractWithLeadingGarbage() throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   125
        createZipWithLeadingGarbage();
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   126
        assertCanExtract("leadingGarbage.zip");
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
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   129
    void assertCanExtract(String zipFileName) throws Throwable {
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   130
        String[] cmd = { jar, "xf", zipFileName };
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   131
        ProcessBuilder pb = new ProcessBuilder(cmd);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   132
        OutputAnalyzer a = ProcessTools.executeProcess(pb);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   133
        a.shouldHaveExitValue(0);
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   134
        a.stdoutShouldMatch("\\A\\Z");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   135
        a.stderrShouldMatch("\\A\\Z");
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   136
        assertFilesExist();
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
3caa40031b3f 8058520: jar xf does not work on zip files with leading garbage
martin
parents:
diff changeset
   139
}