Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions test/jdk/sun/security/tools/jarsigner/VerifyJarEntryName.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import jdk.test.lib.SecurityTools;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class VerifyJarEntryName {

Expand Down Expand Up @@ -85,7 +86,7 @@ void cleanup() throws Exception {
*/
@Test
void verifyManifestEntryName() throws Exception {
modifyJarEntryName(ORIGINAL_JAR, MODIFIED_JAR, "MANIFEST.MF");
modifyJarEntryName(ORIGINAL_JAR, MODIFIED_JAR, "META-INF/MANIFEST.MF");
SecurityTools.jarsigner("-verify -verbose " + MODIFIED_JAR)
.shouldContain("This JAR file contains internal " +
"inconsistencies that may result in different " +
Expand All @@ -95,6 +96,22 @@ void verifyManifestEntryName() throws Exception {
.shouldHaveExitValue(0);
}

/*
* Modify a single byte in signature filename in LOC, and
* validate that jarsigner -verify emits a warning message.
*/
@Test
void verifySignatureEntryName() throws Exception {
modifyJarEntryName(ORIGINAL_JAR, MODIFIED_JAR, "META-INF/MYKEY.SF");
SecurityTools.jarsigner("-verify -verbose " + MODIFIED_JAR)
.shouldContain("This JAR file contains internal " +
"inconsistencies that may result in different " +
"contents when reading via JarFile and JarInputStream:")
.shouldContain("- Entry XETA-INF/MYKEY.SF is present when reading " +
"via JarInputStream but missing when reading via JarFile")
.shouldHaveExitValue(0);
}

/*
* Validate that jarsigner -verify on a valid JAR works without
* emitting warnings about internal inconsistencies.
Expand All @@ -111,9 +128,14 @@ void verifyOriginalJar() throws Exception {
private void modifyJarEntryName(Path origJar, Path modifiedJar,
String entryName) throws Exception {
byte[] jarBytes = Files.readAllBytes(origJar);
var jarString = new String(jarBytes, StandardCharsets.UTF_8);
var pos = jarString.indexOf(entryName);
assertTrue(pos != -1, entryName + " is not present in the JAR");
byte[] entryNameBytes = entryName.getBytes(StandardCharsets.UTF_8);
int pos = 0;
try {
while (!Arrays.equals(jarBytes, pos, pos + entryNameBytes.length,
entryNameBytes, 0, entryNameBytes.length)) pos++;
} catch (ArrayIndexOutOfBoundsException ignore) {
fail(entryName + " is not present in the JAR");
}
jarBytes[pos] = 'X';
Files.write(modifiedJar, jarBytes);
}
Expand Down