Skip to content

Commit

Permalink
test enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweij committed Sep 12, 2024
1 parent 8559b87 commit 7533fd6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
12 changes: 8 additions & 4 deletions test/jdk/sun/security/tools/jarsigner/RemovedFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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);
}
}
18 changes: 15 additions & 3 deletions test/lib/jdk/test/lib/util/JarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
Expand All @@ -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)
Expand Down

0 comments on commit 7533fd6

Please sign in to comment.