|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 | + |
| 25 | +import org.testng.annotations.AfterTest; |
| 26 | +import org.testng.annotations.BeforeTest; |
| 27 | +import org.testng.annotations.Test; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.jar.JarFile; |
| 34 | +import java.util.zip.ZipEntry; |
| 35 | +import java.util.zip.ZipException; |
| 36 | +import java.util.zip.ZipFile; |
| 37 | + |
| 38 | +import static org.testng.Assert.*; |
| 39 | + |
| 40 | +/** |
| 41 | + * @test |
| 42 | + * @bug 8280404 |
| 43 | + * @summary Validate that Zip/JarFile will throw a ZipException when the CEN |
| 44 | + * comment length field contains an incorrect value |
| 45 | + * @run testng/othervm InvalidCommentLengthTest |
| 46 | + */ |
| 47 | +public class InvalidCommentLengthTest { |
| 48 | + |
| 49 | + // Name used to create a JAR with an invalid comment length |
| 50 | + public static final Path INVALID_CEN_COMMENT_LENGTH_JAR = |
| 51 | + Path.of("Invalid-CEN-Comment-Length.jar"); |
| 52 | + // Name used to create a JAR with a valid comment length |
| 53 | + public static final Path VALID_CEN_COMMENT_LENGTH_JAR = |
| 54 | + Path.of("Valid-CEN-Comment-Length.jar"); |
| 55 | + // Zip/Jar CEN file header entry that will be modified |
| 56 | + public static final String META_INF_MANIFEST_MF = "META-INF/MANIFEST.MF"; |
| 57 | + // Expected ZipException message when the comment length corrupts the |
| 58 | + // Zip/Jar file |
| 59 | + public static final String INVALID_CEN_HEADER_BAD_ENTRY_NAME_OR_COMMENT = |
| 60 | + "invalid CEN header (bad entry name or comment)"; |
| 61 | + |
| 62 | + /** |
| 63 | + * Byte array representing a valid jar file prior modifying the comment length |
| 64 | + * entry in a CEN file header. |
| 65 | + * The "Valid-CEN-Comment-Length.jar" jar file was created via: |
| 66 | + * <pre> |
| 67 | + * {@code |
| 68 | + * jar cvf Valid-CEN-Comment-Length.jar Hello.txt Tennis.txt BruceWayne.txt |
| 69 | + * added manifest |
| 70 | + * adding: Hello.txt(in = 12) (out= 14)(deflated -16%) |
| 71 | + * adding: Tennis.txt(in = 53) (out= 53)(deflated 0%) |
| 72 | + * adding: BruceWayne.txt(in = 12) (out= 14)(deflated -16%) |
| 73 | + * } |
| 74 | + * </pre> |
| 75 | + * Its contents are: |
| 76 | + * <pre> |
| 77 | + * {@code |
| 78 | + * jar tvf Valid-CEN-Comment-Length.jar |
| 79 | + * 0 Wed Mar 02 06:39:24 EST 2022 META-INF/ |
| 80 | + * 66 Wed Mar 02 06:39:24 EST 2022 META-INF/MANIFEST.MF |
| 81 | + * 12 Wed Mar 02 06:39:06 EST 2022 Hello.txt |
| 82 | + * 53 Wed Mar 02 13:04:48 EST 2022 Tennis.txt |
| 83 | + * 12 Wed Mar 02 15:15:34 EST 2022 BruceWayne.txt |
| 84 | + * } |
| 85 | + * </pre> |
| 86 | + * The ByteArray was created by: |
| 87 | + * <pre> |
| 88 | + * {@code |
| 89 | + * var jar = Files.readAllBytes("Valid-CEN-Comment-Length.jar"); |
| 90 | + * var validEntryName = createByteArray(fooJar, |
| 91 | + * "VALID_ZIP_WITH_NO_COMMENTS_BYTES"); |
| 92 | + * } |
| 93 | + * </pre> |
| 94 | + */ |
| 95 | + public static byte[] VALID_ZIP_WITH_NO_COMMENTS_BYTES = { |
| 96 | + (byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, (byte) 0x14, |
| 97 | + (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x8, (byte) 0x0, |
| 98 | + (byte) 0xec, (byte) 0x34, (byte) 0x62, (byte) 0x54, (byte) 0x0, |
| 99 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 100 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 101 | + (byte) 0x0, (byte) 0x9, (byte) 0x0, (byte) 0x4, (byte) 0x0, |
| 102 | + (byte) 0x4d, (byte) 0x45, (byte) 0x54, (byte) 0x41, (byte) 0x2d, |
| 103 | + (byte) 0x49, (byte) 0x4e, (byte) 0x46, (byte) 0x2f, (byte) 0xfe, |
| 104 | + (byte) 0xca, (byte) 0x0, (byte) 0x0, (byte) 0x3, (byte) 0x0, |
| 105 | + (byte) 0x50, (byte) 0x4b, (byte) 0x7, (byte) 0x8, (byte) 0x0, |
| 106 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x2, (byte) 0x0, |
| 107 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 108 | + (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, |
| 109 | + (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x8, |
| 110 | + (byte) 0x0, (byte) 0xec, (byte) 0x34, (byte) 0x62, (byte) 0x54, |
| 111 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 112 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 113 | + (byte) 0x0, (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x0, |
| 114 | + (byte) 0x0, (byte) 0x4d, (byte) 0x45, (byte) 0x54, (byte) 0x41, |
| 115 | + (byte) 0x2d, (byte) 0x49, (byte) 0x4e, (byte) 0x46, (byte) 0x2f, |
| 116 | + (byte) 0x4d, (byte) 0x41, (byte) 0x4e, (byte) 0x49, (byte) 0x46, |
| 117 | + (byte) 0x45, (byte) 0x53, (byte) 0x54, (byte) 0x2e, (byte) 0x4d, |
| 118 | + (byte) 0x46, (byte) 0xf3, (byte) 0x4d, (byte) 0xcc, (byte) 0xcb, |
| 119 | + (byte) 0x4c, (byte) 0x4b, (byte) 0x2d, (byte) 0x2e, (byte) 0xd1, |
| 120 | + (byte) 0xd, (byte) 0x4b, (byte) 0x2d, (byte) 0x2a, (byte) 0xce, |
| 121 | + (byte) 0xcc, (byte) 0xcf, (byte) 0xb3, (byte) 0x52, (byte) 0x30, |
| 122 | + (byte) 0xd4, (byte) 0x33, (byte) 0xe0, (byte) 0xe5, (byte) 0x72, |
| 123 | + (byte) 0x2e, (byte) 0x4a, (byte) 0x4d, (byte) 0x2c, (byte) 0x49, |
| 124 | + (byte) 0x4d, (byte) 0xd1, (byte) 0x75, (byte) 0xaa, (byte) 0x4, |
| 125 | + (byte) 0xa, (byte) 0x98, (byte) 0xe8, (byte) 0x19, (byte) 0xe8, |
| 126 | + (byte) 0x19, (byte) 0x2a, (byte) 0x68, (byte) 0xf8, (byte) 0x17, |
| 127 | + (byte) 0x25, (byte) 0x26, (byte) 0xe7, (byte) 0xa4, (byte) 0x2a, |
| 128 | + (byte) 0x38, (byte) 0xe7, (byte) 0x17, (byte) 0x15, (byte) 0xe4, |
| 129 | + (byte) 0x17, (byte) 0x25, (byte) 0x96, (byte) 0x0, (byte) 0x15, |
| 130 | + (byte) 0x6b, (byte) 0xf2, (byte) 0x72, (byte) 0xf1, (byte) 0x72, |
| 131 | + (byte) 0x1, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x7, |
| 132 | + (byte) 0x8, (byte) 0xf4, (byte) 0x59, (byte) 0xdc, (byte) 0xa6, |
| 133 | + (byte) 0x42, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x42, |
| 134 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b, |
| 135 | + (byte) 0x3, (byte) 0x4, (byte) 0x14, (byte) 0x0, (byte) 0x8, |
| 136 | + (byte) 0x8, (byte) 0x8, (byte) 0x0, (byte) 0xe3, (byte) 0x34, |
| 137 | + (byte) 0x62, (byte) 0x54, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 138 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 139 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x9, |
| 140 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65, |
| 141 | + (byte) 0x6c, (byte) 0x6c, (byte) 0x6f, (byte) 0x2e, (byte) 0x74, |
| 142 | + (byte) 0x78, (byte) 0x74, (byte) 0xf3, (byte) 0x48, (byte) 0xcd, |
| 143 | + (byte) 0xc9, (byte) 0xc9, (byte) 0x57, (byte) 0x28, (byte) 0xcf, |
| 144 | + (byte) 0x2f, (byte) 0xca, (byte) 0x49, (byte) 0xe1, (byte) 0x2, |
| 145 | + (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x7, (byte) 0x8, |
| 146 | + (byte) 0xd5, (byte) 0xe0, (byte) 0x39, (byte) 0xb7, (byte) 0xe, |
| 147 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xc, (byte) 0x0, |
| 148 | + (byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x3, |
| 149 | + (byte) 0x4, (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8, |
| 150 | + (byte) 0x8, (byte) 0x0, (byte) 0x98, (byte) 0x68, (byte) 0x62, |
| 151 | + (byte) 0x54, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 152 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 153 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xa, (byte) 0x0, |
| 154 | + (byte) 0x0, (byte) 0x0, (byte) 0x54, (byte) 0x65, (byte) 0x6e, |
| 155 | + (byte) 0x6e, (byte) 0x69, (byte) 0x73, (byte) 0x2e, (byte) 0x74, |
| 156 | + (byte) 0x78, (byte) 0x74, (byte) 0x73, (byte) 0xf2, (byte) 0xb, |
| 157 | + (byte) 0x50, (byte) 0x8, (byte) 0x48, (byte) 0x2c, (byte) 0xca, |
| 158 | + (byte) 0x4c, (byte) 0x4a, (byte) 0x2c, (byte) 0x56, (byte) 0xf0, |
| 159 | + (byte) 0x2f, (byte) 0x48, (byte) 0xcd, (byte) 0x53, (byte) 0xc8, |
| 160 | + (byte) 0x2c, (byte) 0x56, (byte) 0x48, (byte) 0x54, (byte) 0x48, |
| 161 | + (byte) 0x2b, (byte) 0xcd, (byte) 0x53, (byte) 0x8, (byte) 0x49, |
| 162 | + (byte) 0xcd, (byte) 0xcb, (byte) 0x3, (byte) 0x72, (byte) 0x42, |
| 163 | + (byte) 0xf2, (byte) 0x4b, (byte) 0x8b, (byte) 0xf2, (byte) 0x12, |
| 164 | + (byte) 0x73, (byte) 0x53, (byte) 0xf3, (byte) 0x4a, (byte) 0x14, |
| 165 | + (byte) 0x4a, (byte) 0xf2, (byte) 0x15, (byte) 0xca, (byte) 0x13, |
| 166 | + (byte) 0x4b, (byte) 0x92, (byte) 0x33, (byte) 0xb8, (byte) 0x0, |
| 167 | + (byte) 0x50, (byte) 0x4b, (byte) 0x7, (byte) 0x8, (byte) 0xaa, |
| 168 | + (byte) 0xad, (byte) 0x14, (byte) 0xd, (byte) 0x35, (byte) 0x0, |
| 169 | + (byte) 0x0, (byte) 0x0, (byte) 0x35, (byte) 0x0, (byte) 0x0, |
| 170 | + (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, |
| 171 | + (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x8, |
| 172 | + (byte) 0x0, (byte) 0xf1, (byte) 0x79, (byte) 0x62, (byte) 0x54, |
| 173 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 174 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 175 | + (byte) 0x0, (byte) 0x0, (byte) 0xe, (byte) 0x0, (byte) 0x0, |
| 176 | + (byte) 0x0, (byte) 0x42, (byte) 0x72, (byte) 0x75, (byte) 0x63, |
| 177 | + (byte) 0x65, (byte) 0x57, (byte) 0x61, (byte) 0x79, (byte) 0x6e, |
| 178 | + (byte) 0x65, (byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, |
| 179 | + (byte) 0xf3, (byte) 0x54, (byte) 0x48, (byte) 0xcc, (byte) 0x55, |
| 180 | + (byte) 0x70, (byte) 0x4a, (byte) 0x2c, (byte) 0xc9, (byte) 0x4d, |
| 181 | + (byte) 0xcc, (byte) 0xe3, (byte) 0x2, (byte) 0x0, (byte) 0x50, |
| 182 | + (byte) 0x4b, (byte) 0x7, (byte) 0x8, (byte) 0x6c, (byte) 0x70, |
| 183 | + (byte) 0x60, (byte) 0xbd, (byte) 0xe, (byte) 0x0, (byte) 0x0, |
| 184 | + (byte) 0x0, (byte) 0xc, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 185 | + (byte) 0x50, (byte) 0x4b, (byte) 0x1, (byte) 0x2, (byte) 0x14, |
| 186 | + (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8, |
| 187 | + (byte) 0x8, (byte) 0x0, (byte) 0xec, (byte) 0x34, (byte) 0x62, |
| 188 | + (byte) 0x54, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 189 | + (byte) 0x2, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 190 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x9, (byte) 0x0, |
| 191 | + (byte) 0x4, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 192 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 193 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 194 | + (byte) 0x0, (byte) 0x4d, (byte) 0x45, (byte) 0x54, (byte) 0x41, |
| 195 | + (byte) 0x2d, (byte) 0x49, (byte) 0x4e, (byte) 0x46, (byte) 0x2f, |
| 196 | + (byte) 0xfe, (byte) 0xca, (byte) 0x0, (byte) 0x0, (byte) 0x50, |
| 197 | + (byte) 0x4b, (byte) 0x1, (byte) 0x2, (byte) 0x14, (byte) 0x0, |
| 198 | + (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x8, |
| 199 | + (byte) 0x0, (byte) 0xec, (byte) 0x34, (byte) 0x62, (byte) 0x54, |
| 200 | + (byte) 0xf4, (byte) 0x59, (byte) 0xdc, (byte) 0xa6, (byte) 0x42, |
| 201 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x42, (byte) 0x0, |
| 202 | + (byte) 0x0, (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x0, |
| 203 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 204 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 205 | + (byte) 0x0, (byte) 0x3d, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 206 | + (byte) 0x4d, (byte) 0x45, (byte) 0x54, (byte) 0x41, (byte) 0x2d, |
| 207 | + (byte) 0x49, (byte) 0x4e, (byte) 0x46, (byte) 0x2f, (byte) 0x4d, |
| 208 | + (byte) 0x41, (byte) 0x4e, (byte) 0x49, (byte) 0x46, (byte) 0x45, |
| 209 | + (byte) 0x53, (byte) 0x54, (byte) 0x2e, (byte) 0x4d, (byte) 0x46, |
| 210 | + (byte) 0x50, (byte) 0x4b, (byte) 0x1, (byte) 0x2, (byte) 0x14, |
| 211 | + (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8, |
| 212 | + (byte) 0x8, (byte) 0x0, (byte) 0xe3, (byte) 0x34, (byte) 0x62, |
| 213 | + (byte) 0x54, (byte) 0xd5, (byte) 0xe0, (byte) 0x39, (byte) 0xb7, |
| 214 | + (byte) 0xe, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xc, |
| 215 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x9, (byte) 0x0, |
| 216 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 217 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 218 | + (byte) 0x0, (byte) 0x0, (byte) 0xc1, (byte) 0x0, (byte) 0x0, |
| 219 | + (byte) 0x0, (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, |
| 220 | + (byte) 0x6f, (byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, |
| 221 | + (byte) 0x50, (byte) 0x4b, (byte) 0x1, (byte) 0x2, (byte) 0x14, |
| 222 | + (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8, |
| 223 | + (byte) 0x8, (byte) 0x0, (byte) 0x98, (byte) 0x68, (byte) 0x62, |
| 224 | + (byte) 0x54, (byte) 0xaa, (byte) 0xad, (byte) 0x14, (byte) 0xd, |
| 225 | + (byte) 0x35, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x35, |
| 226 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xa, (byte) 0x0, |
| 227 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 228 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 229 | + (byte) 0x0, (byte) 0x0, (byte) 0x6, (byte) 0x1, (byte) 0x0, |
| 230 | + (byte) 0x0, (byte) 0x54, (byte) 0x65, (byte) 0x6e, (byte) 0x6e, |
| 231 | + (byte) 0x69, (byte) 0x73, (byte) 0x2e, (byte) 0x74, (byte) 0x78, |
| 232 | + (byte) 0x74, (byte) 0x50, (byte) 0x4b, (byte) 0x1, (byte) 0x2, |
| 233 | + (byte) 0x14, (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x8, |
| 234 | + (byte) 0x8, (byte) 0x8, (byte) 0x0, (byte) 0xf1, (byte) 0x79, |
| 235 | + (byte) 0x62, (byte) 0x54, (byte) 0x6c, (byte) 0x70, (byte) 0x60, |
| 236 | + (byte) 0xbd, (byte) 0xe, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 237 | + (byte) 0xc, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xe, |
| 238 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 239 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 240 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x73, (byte) 0x1, |
| 241 | + (byte) 0x0, (byte) 0x0, (byte) 0x42, (byte) 0x72, (byte) 0x75, |
| 242 | + (byte) 0x63, (byte) 0x65, (byte) 0x57, (byte) 0x61, (byte) 0x79, |
| 243 | + (byte) 0x6e, (byte) 0x65, (byte) 0x2e, (byte) 0x74, (byte) 0x78, |
| 244 | + (byte) 0x74, (byte) 0x50, (byte) 0x4b, (byte) 0x5, (byte) 0x6, |
| 245 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x5, |
| 246 | + (byte) 0x0, (byte) 0x5, (byte) 0x0, (byte) 0x28, (byte) 0x1, |
| 247 | + (byte) 0x0, (byte) 0x0, (byte) 0xbd, (byte) 0x1, (byte) 0x0, |
| 248 | + (byte) 0x0, (byte) 0x0, (byte) 0x0, |
| 249 | + }; |
| 250 | + |
| 251 | + /** |
| 252 | + * Create Jar files used by the tests. |
| 253 | + * The {@code byte} array {@code VALID_ZIP_WITH_NO_COMMENTS_BYTES} is written |
| 254 | + * to disk to create the jar file: {@code Valid-CEN-Comment-Length.jar}. |
| 255 | + * |
| 256 | + * The jar file {@code InValid-CEN-Comment-Length.jar} is created by copying |
| 257 | + * the {@code byte} array {@code VALID_ZIP_WITH_NO_COMMENTS_BYTES} and modifying |
| 258 | + * the CEN file header comment length entry for "META-INF/MANIFEST.MF" so that |
| 259 | + * new comment length will forward the CEN to a subsequent CEN file header |
| 260 | + * entry. |
| 261 | + * |
| 262 | + * For {@code InValid-CEN-Comment-Length.jar}, the comment length is changed |
| 263 | + * from {@code 0x0} to the {@code 0x37}. |
| 264 | + * |
| 265 | + * @throws IOException If an error occurs |
| 266 | + */ |
| 267 | + @BeforeTest |
| 268 | + public void setup() throws IOException { |
| 269 | + Files.deleteIfExists(VALID_CEN_COMMENT_LENGTH_JAR); |
| 270 | + Files.deleteIfExists(INVALID_CEN_COMMENT_LENGTH_JAR); |
| 271 | + // Create the valid jar |
| 272 | + Files.write(VALID_CEN_COMMENT_LENGTH_JAR, VALID_ZIP_WITH_NO_COMMENTS_BYTES); |
| 273 | + // Now create an invalid jar |
| 274 | + byte[] invalid_bytes = Arrays.copyOf(VALID_ZIP_WITH_NO_COMMENTS_BYTES, |
| 275 | + VALID_ZIP_WITH_NO_COMMENTS_BYTES.length); |
| 276 | + // Change CEN file Header comment length so that the length will |
| 277 | + // result in the offset pointing to a subsequent CEN file header |
| 278 | + // resulting in an invalid comment |
| 279 | + invalid_bytes[536] = 55; |
| 280 | + Files.write(INVALID_CEN_COMMENT_LENGTH_JAR, invalid_bytes); |
| 281 | + } |
| 282 | + |
| 283 | + /** |
| 284 | + * Clean up after the test run |
| 285 | + * |
| 286 | + * @throws IOException If an error occurs |
| 287 | + */ |
| 288 | + @AfterTest |
| 289 | + public static void cleanup() throws IOException { |
| 290 | + Files.deleteIfExists(VALID_CEN_COMMENT_LENGTH_JAR); |
| 291 | + Files.deleteIfExists(INVALID_CEN_COMMENT_LENGTH_JAR); |
| 292 | + } |
| 293 | + |
| 294 | + /** |
| 295 | + * Validate that the original(valid) Jar file can be opened by {@code ZipFile} |
| 296 | + * and the expected Zip entry can be found |
| 297 | + * @throws IOException If an error occurs |
| 298 | + */ |
| 299 | + @Test |
| 300 | + public static void ZipFileValidCommentLengthTest() throws IOException { |
| 301 | + try (ZipFile jf = new ZipFile(VALID_CEN_COMMENT_LENGTH_JAR.toFile())) { |
| 302 | + ZipEntry ze = jf.getEntry(META_INF_MANIFEST_MF); |
| 303 | + assertNotNull(ze); |
| 304 | + assertEquals(ze.getName(), META_INF_MANIFEST_MF); |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + /** |
| 309 | + * Validate that the original(valid) Jar file can be opened by {@code JarFile} |
| 310 | + * and the expected Zip entry can be found |
| 311 | + * @throws IOException If an error occurs |
| 312 | + */ |
| 313 | + @Test |
| 314 | + public static void JarFileValidCommentLengthTest() throws IOException { |
| 315 | + try (JarFile jf = new JarFile(VALID_CEN_COMMENT_LENGTH_JAR.toFile())) { |
| 316 | + ZipEntry ze = jf.getEntry(META_INF_MANIFEST_MF); |
| 317 | + assertNotNull(ze); |
| 318 | + assertEquals(ze.getName(), META_INF_MANIFEST_MF); |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * Validate that a ZipException is thrown when the CEN file header comment |
| 324 | + * length is non-zero and the CEN entry does not contain a comment when |
| 325 | + * the Jar file is opened by {@code ZipFile} |
| 326 | + */ |
| 327 | + @Test |
| 328 | + public static void ZipFileInValidCommentLengthTest() { |
| 329 | + var ex= expectThrows(ZipException.class, |
| 330 | + () -> new ZipFile(INVALID_CEN_COMMENT_LENGTH_JAR.toFile())); |
| 331 | + assertEquals(ex.getMessage(), INVALID_CEN_HEADER_BAD_ENTRY_NAME_OR_COMMENT); |
| 332 | + } |
| 333 | + |
| 334 | + /** |
| 335 | + * Validate that a ZipException is thrown when the CEN file header comment |
| 336 | + * length is non-zero and the CEN entry does not contain a comment when |
| 337 | + * the Jar file is opened by {@code JarFile} |
| 338 | + */ |
| 339 | + @Test |
| 340 | + public static void JarFileInValidCommentLengthTest() { |
| 341 | + var ex= expectThrows(ZipException.class, |
| 342 | + () -> new JarFile(INVALID_CEN_COMMENT_LENGTH_JAR.toFile())); |
| 343 | + assertEquals(ex.getMessage(), INVALID_CEN_HEADER_BAD_ENTRY_NAME_OR_COMMENT); |
| 344 | + } |
| 345 | +} |
0 commit comments