From 7533fd68c68d1643853d8e41dafa5cc05de0bc84 Mon Sep 17 00:00:00 2001 From: Weijun Wang Date: Thu, 12 Sep 2024 11:28:37 -0400 Subject: [PATCH] test enhancements --- .../sun/security/tools/jarsigner/Main.java | 1 - .../security/tools/jarsigner/RemovedFiles.java | 12 ++++++++---- test/lib/jdk/test/lib/util/JarUtils.java | 18 +++++++++++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java b/src/jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java index 8fe0c1afc8989..014b420e1a237 100644 --- a/src/jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java +++ b/src/jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java @@ -1327,7 +1327,6 @@ private void displayMessagesAndResult(boolean isSigning) { if (hasNonexistentEntries) { warnings.add(rb.getString("nonexistent.entries.found")); } - if (externalFileAttributesDetected) { warnings.add(rb.getString("external.file.attributes.detected")); } diff --git a/test/jdk/sun/security/tools/jarsigner/RemovedFiles.java b/test/jdk/sun/security/tools/jarsigner/RemovedFiles.java index b093a8aa879a0..d0c905a1062fa 100644 --- a/test/jdk/sun/security/tools/jarsigner/RemovedFiles.java +++ b/test/jdk/sun/security/tools/jarsigner/RemovedFiles.java @@ -35,6 +35,10 @@ import java.nio.file.Path; public class RemovedFiles { + + private static final String NONEXISTENT_ENTRIES_FOUND + = "Nonexistent signed entries detected. See details in -verbose output."; + public static void main(String[] args) throws Exception { JarUtils.createJarFile( Path.of("a.jar"), @@ -46,19 +50,19 @@ public static void main(String[] args) throws Exception { // All is fine at the beginning. SecurityTools.jarsigner("-verify a.jar") - .shouldNotContain("Nonexistent signed entries detected. See details in -verbose output."); + .shouldNotContain(NONEXISTENT_ENTRIES_FOUND); // Remove an entry after signing. There will be a warning. JarUtils.deleteEntries(Path.of("a.jar"), "a"); SecurityTools.jarsigner("-verify a.jar") - .shouldContain("Nonexistent signed entries detected. See details in -verbose output."); + .shouldContain(NONEXISTENT_ENTRIES_FOUND); SecurityTools.jarsigner("-verify -verbose a.jar") - .shouldContain("Nonexistent signed entries detected. See details in -verbose output.") + .shouldContain(NONEXISTENT_ENTRIES_FOUND) .shouldContain("Warning: nonexistent signed entries: [a]"); // Re-sign will not clear the warning. SecurityTools.jarsigner("-storepass changeit -keystore ks a.jar x"); SecurityTools.jarsigner("-verify a.jar") - .shouldContain("Nonexistent signed entries detected. See details in -verbose output."); + .shouldContain(NONEXISTENT_ENTRIES_FOUND); } } diff --git a/test/lib/jdk/test/lib/util/JarUtils.java b/test/lib/jdk/test/lib/util/JarUtils.java index 4ac3e7a171177..3aa4ada5197ad 100644 --- a/test/lib/jdk/test/lib/util/JarUtils.java +++ b/test/lib/jdk/test/lib/util/JarUtils.java @@ -324,11 +324,14 @@ public static void updateManifest(String src, String dest, Manifest man) * Remove entries from a ZIP file. * * Each entry can be a name or a name ending with "*". + * + * @return number of removed entries + * @throws IOException if there is any I/O error */ - public static void deleteEntries(Path jarfile, String... patterns) - throws IOException - { + public static int deleteEntries(Path jarfile, String... patterns) + throws IOException { Path tmpfile = Files.createTempFile("jar", "jar"); + int count = 0; try (OutputStream out = Files.newOutputStream(tmpfile); JarOutputStream jos = new JarOutputStream(out)) { @@ -341,14 +344,21 @@ public static void deleteEntries(Path jarfile, String... patterns) if (pattern.endsWith("*")) { if (name.startsWith(pattern.substring( 0, pattern.length() - 1))) { + // Go directly to next entry. This + // one is not written into `jos` and + // therefore removed. + count++; continue top; } } else { if (name.equals(pattern)) { + // Same as above + count++; continue top; } } } + // No pattern matched, file retained jos.putNextEntry(copyEntry(jentry)); jf.getInputStream(jentry).transferTo(jos); } @@ -357,6 +367,8 @@ public static void deleteEntries(Path jarfile, String... patterns) // replace the original JAR file Files.move(tmpfile, jarfile, StandardCopyOption.REPLACE_EXISTING); + + return count; } private static void updateEntry(JarOutputStream jos, String name, Object content)