author | lancea |
Tue, 29 Oct 2019 14:22:18 -0400 | |
changeset 58845 | e492513d3630 |
parent 54944 | 9f714ef845d5 |
permissions | -rw-r--r-- |
54944
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
1 |
/* |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
4 |
* |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
8 |
* |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
13 |
* accompanied this code). |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
14 |
* |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
18 |
* |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
21 |
* questions. |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
22 |
*/ |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
23 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
24 |
import org.testng.annotations.AfterClass; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
25 |
import org.testng.annotations.BeforeClass; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
26 |
import org.testng.annotations.DataProvider; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
27 |
import org.testng.annotations.Test; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
28 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
29 |
import java.io.FileOutputStream; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
30 |
import java.io.IOException; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
31 |
import java.nio.file.FileSystem; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
32 |
import java.nio.file.FileSystems; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
33 |
import java.nio.file.Files; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
34 |
import java.nio.file.Path; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
35 |
import java.util.List; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
36 |
import java.util.jar.JarEntry; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
37 |
import java.util.jar.JarOutputStream; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
38 |
import java.util.stream.Collectors; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
39 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
40 |
import static java.nio.file.Files.walk; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
41 |
import static org.testng.Assert.*; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
42 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
43 |
/** |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
44 |
* @test |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
45 |
* @bug 8222807 |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
46 |
* @summary Validate that you can iterate a ZIP file with invalid ZIP header entries |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
47 |
* @modules jdk.zipfs |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
48 |
* @compile InvalidZipHeaderTests.java |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
49 |
* @run testng InvalidZipHeaderTests |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
50 |
* @run testng/othervm/java.security.policy=test.policy InvalidZipHeaderTests |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
51 |
*/ |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
52 |
public class InvalidZipHeaderTests { |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
53 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
54 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
55 |
// Name of Jar file used in tests |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
56 |
private static final String INVALID_JAR_FILE = "invalid.jar"; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
57 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
58 |
/** |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
59 |
* Create the JAR files used by the tests |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
60 |
*/ |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
61 |
@BeforeClass |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
62 |
public void setUp() throws Exception { |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
63 |
createInvalidJarFile(); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
64 |
} |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
65 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
66 |
/** |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
67 |
* Remove JAR files used by test as part of clean-up |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
68 |
*/ |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
69 |
@AfterClass |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
70 |
public void tearDown() throws Exception { |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
71 |
Files.deleteIfExists(Path.of(INVALID_JAR_FILE)); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
72 |
} |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
73 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
74 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
75 |
/** |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
76 |
* Validate that you can walk a ZIP archive with header entries |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
77 |
* such as "foo//" |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
78 |
*/ |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
79 |
@Test(dataProvider = "startPaths") |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
80 |
public void walkInvalidHeaderTest(String startPath, List<String> expectedPaths) |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
81 |
throws IOException { |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
82 |
try (FileSystem zipfs = |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
83 |
FileSystems.newFileSystem(Path.of(INVALID_JAR_FILE))) { |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
84 |
List<String> result = walk(zipfs.getPath(startPath)) |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
85 |
.map(f -> f.toString()).collect(Collectors.toList()); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
86 |
assertTrue(result.equals(expectedPaths), |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
87 |
String.format("Error: Expected paths not found when walking" |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
88 |
+ "%s, starting at %s%n", INVALID_JAR_FILE, |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
89 |
startPath)); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
90 |
} |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
91 |
} |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
92 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
93 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
94 |
/** |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
95 |
* Starting Path for walking the ZIP archive and the expected paths to be returned |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
96 |
* when traversing the archive |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
97 |
*/ |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
98 |
@DataProvider(name = "startPaths") |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
99 |
public static Object[][] Name() { |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
100 |
return new Object[][]{ |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
101 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
102 |
{"luckydog", List.of("luckydog", "luckydog/outfile.txt")}, |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
103 |
{"/luckydog", List.of("/luckydog", "/luckydog/outfile.txt")}, |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
104 |
{"./luckydog", List.of("./luckydog", "./luckydog/outfile.txt")}, |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
105 |
{"", List.of( "", "luckydog", "luckydog/outfile.txt")}, |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
106 |
{"/", List.of("/", "/luckydog", "/luckydog/outfile.txt")}, |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
107 |
{".", List.of(".", "./luckydog", "./luckydog/outfile.txt")}, |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
108 |
{"./", List.of(".", "./luckydog", "./luckydog/outfile.txt")} |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
109 |
}; |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
110 |
} |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
111 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
112 |
/** |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
113 |
* Create a jar file with invalid CEN and LOC headers |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
114 |
* @throws IOException |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
115 |
*/ |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
116 |
static void createInvalidJarFile() throws IOException { |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
117 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
118 |
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(INVALID_JAR_FILE))) { |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
119 |
JarEntry je = new JarEntry("luckydog//"); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
120 |
jos.putNextEntry(je); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
121 |
jos.closeEntry(); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
122 |
je = new JarEntry("luckydog//outfile.txt"); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
123 |
jos.putNextEntry(je); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
124 |
jos.write("Tennis Anyone!!".getBytes()); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
125 |
jos.closeEntry(); |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
126 |
} |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
127 |
} |
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
128 |
|
9f714ef845d5
8222807: Address iteration with invalid ZIP header entries
lancea
parents:
diff
changeset
|
129 |
} |