Path: blob/master/test/jdk/tools/jar/LeadingGarbage.java
41144 views
/*1* Copyright 2014 Google Inc. All Rights Reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import static org.testng.Assert.assertEquals;24import static org.testng.Assert.assertTrue;2526import java.io.File;27import java.io.FileOutputStream;28import java.io.IOException;29import java.io.OutputStream;30import java.nio.file.Files;31import java.nio.file.Paths;32import java.util.ArrayList;33import java.util.Arrays;34import java.util.List;35import java.util.stream.Stream;3637import jdk.test.lib.JDKToolFinder;38import jdk.test.lib.Utils;39import jdk.test.lib.process.OutputAnalyzer;40import jdk.test.lib.process.ProcessTools;4142import org.testng.annotations.Test;4344/**45* @test46* @bug 8058520 819674847* @summary jar tf and jar xf should work on zip files with leading garbage48* @library /test/lib49* @run testng LeadingGarbage50*/51@Test52public class LeadingGarbage {5354final File[] files = { new File("a"), new File("b") };55final File normalZip = new File("normal.zip");56final File leadingGarbageZip = new File("leadingGarbage.zip");5758void createFile(File f) throws IOException {59try (OutputStream fos = new FileOutputStream(f)) {60fos.write(f.getName().getBytes("UTF-8"));61}62}6364void createFiles() throws IOException {65for (File file : files)66createFile(file);67}6869void deleteFiles() throws IOException {70for (File file : files)71assertTrue(file.delete());72}7374void assertFilesExist() throws IOException {75for (File file : files)76assertTrue(file.exists());77}7879void createNormalZip() throws Throwable {80createFiles();81OutputAnalyzer a = jar("c0Mf", "normal.zip", "a", "b");82a.shouldHaveExitValue(0);83a.stdoutShouldMatch("\\A\\Z");84a.stderrShouldMatchIgnoreVMWarnings("\\A\\Z");85deleteFiles();86}8788void createZipWithLeadingGarbage() throws Throwable {89createNormalZip();90createFile(leadingGarbageZip);91try (OutputStream fos = new FileOutputStream(leadingGarbageZip, true)) {92Files.copy(normalZip.toPath(), fos);93}94assertTrue(normalZip.length() < leadingGarbageZip.length());95assertTrue(normalZip.delete());96}9798public void test_canList() throws Throwable {99createNormalZip();100assertCanList("normal.zip");101}102103public void test_canListWithLeadingGarbage() throws Throwable {104createZipWithLeadingGarbage();105assertCanList("leadingGarbage.zip");106}107108void assertCanList(String zipFileName) throws Throwable {109OutputAnalyzer a = jar("tf", zipFileName);110a.shouldHaveExitValue(0);111StringBuilder expected = new StringBuilder();112for (File file : files)113expected.append(file.getName()).append(System.lineSeparator());114a.stdoutShouldMatch(expected.toString());115a.stderrShouldMatchIgnoreVMWarnings("\\A\\Z");116}117118public void test_canExtract() throws Throwable {119createNormalZip();120assertCanExtract("normal.zip");121}122123public void test_canExtractWithLeadingGarbage() throws Throwable {124createZipWithLeadingGarbage();125assertCanExtract("leadingGarbage.zip");126}127128void assertCanExtract(String zipFileName) throws Throwable {129OutputAnalyzer a = jar("xf", zipFileName);130a.shouldHaveExitValue(0);131a.stdoutShouldMatch("\\A\\Z");132a.stderrShouldMatchIgnoreVMWarnings("\\A\\Z");133assertFilesExist();134}135136OutputAnalyzer jar(String... args) throws Throwable {137String jar = JDKToolFinder.getJDKTool("jar");138List<String> cmds = new ArrayList<>();139cmds.add(jar);140cmds.addAll(Utils.getForwardVmOptions());141Stream.of(args).forEach(x -> cmds.add(x));142ProcessBuilder p = new ProcessBuilder(cmds);143return ProcessTools.executeCommand(p);144}145}146147148