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