1 /*
2  * Copyright (c) 2019, Oracle and/or its affiliates. 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 org.testng.annotations.AfterClass;
25 import org.testng.annotations.BeforeClass;
26 import org.testng.annotations.DataProvider;
27 import org.testng.annotations.Test;
28 
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.nio.file.FileSystem;
32 import java.nio.file.FileSystems;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.util.List;
36 import java.util.jar.JarEntry;
37 import java.util.jar.JarOutputStream;
38 import java.util.stream.Collectors;
39 
40 import static java.nio.file.Files.walk;
41 import static org.testng.Assert.*;
42 
43 /**
44  * @test
45  * @bug 8222807
46  * @summary Validate that you can iterate a ZIP file with invalid ZIP header entries
47  * @modules jdk.zipfs
48  * @compile InvalidZipHeaderTests.java
49  * @run testng InvalidZipHeaderTests
50  * @run testng/othervm/java.security.policy=test.policy  InvalidZipHeaderTests
51  */
52 public class InvalidZipHeaderTests {
53 
54 
55     // Name of Jar file used in tests
56     private static final String INVALID_JAR_FILE = "invalid.jar";
57 
58     /**
59      * Create the JAR files used by the tests
60      */
61     @BeforeClass
62     public void setUp() throws Exception {
63         createInvalidJarFile();
64     }
65 
66     /**
67      * Remove JAR files used by test as part of clean-up
68      */
69     @AfterClass
setUp()70     public void tearDown() throws Exception {
71         Files.deleteIfExists(Path.of(INVALID_JAR_FILE));
72     }
73 
74 
75     /**
76      * Validate that you can walk a ZIP archive with header entries
77      * such as "foo//"
78      */
79     @Test(dataProvider = "startPaths")
80     public void walkInvalidHeaderTest(String startPath, List<String> expectedPaths)
81             throws IOException {
82         try (FileSystem zipfs =
83                      FileSystems.newFileSystem(Path.of(INVALID_JAR_FILE))) {
84             List<String> result = walk(zipfs.getPath(startPath))
tearDown()85                     .map(f -> f.toString()).collect(Collectors.toList());
86             assertTrue(result.equals(expectedPaths),
87                     String.format("Error: Expected paths not found when walking"
88                                     + "%s,  starting at %s%n", INVALID_JAR_FILE,
89                             startPath));
90         }
91     }
92 
93 
94     /**
95      * Starting Path for walking the ZIP archive and the expected paths to be returned
test0000(String glob, boolean expectedResult, String errMsg)96      * when traversing the archive
97      */
98     @DataProvider(name = "startPaths")
99     public static Object[][] Name() {
100         return new Object[][]{
101 
102                 {"luckydog", List.of("luckydog", "luckydog/outfile.txt")},
103                 {"/luckydog", List.of("/luckydog", "/luckydog/outfile.txt")},
104                 {"./luckydog", List.of("./luckydog", "./luckydog/outfile.txt")},
105                 {"", List.of( "", "luckydog", "luckydog/outfile.txt")},
106                 {"/", List.of("/", "/luckydog", "/luckydog/outfile.txt")},
107                 {".", List.of(".", "./luckydog", "./luckydog/outfile.txt")},
108                 {"./", List.of(".", "./luckydog", "./luckydog/outfile.txt")}
109         };
110     }
111 
112     /**
113      * Create a jar file with invalid CEN and LOC headers
114      * @throws IOException
115      */
116     static void createInvalidJarFile() throws IOException {
117 
118         try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(INVALID_JAR_FILE))) {
test0001(String glob, boolean expectedResult, String errMsg)119             JarEntry je = new JarEntry("luckydog//");
120             jos.putNextEntry(je);
121             jos.closeEntry();
try(FileSystem zipfs = ZIPFS_PROVIDER.newFileSystem(Paths.get(R), UNZIPFS_MAP); DirectoryStream<Path> ds = Files.newDirectoryStream(zipfs.getPath(R), glob))122             je = new JarEntry("luckydog//outfile.txt");
123             jos.putNextEntry(je);
124             jos.write("Tennis Anyone!!".getBytes());
125             jos.closeEntry();
126         }
127     }
128 
129 }
130