3838import java .nio .charset .StandardCharsets ;
3939import java .nio .file .Files ;
4040import java .nio .file .Path ;
41+ import java .util .Arrays ;
4142import java .util .jar .JarFile ;
4243import java .util .zip .ZipEntry ;
4344import java .util .zip .ZipOutputStream ;
4445
4546import jdk .test .lib .SecurityTools ;
46- import static org .junit .jupiter .api .Assertions .assertTrue ;
47+ import static org .junit .jupiter .api .Assertions .fail ;
4748
4849public class VerifyJarEntryName {
4950
@@ -85,7 +86,7 @@ void cleanup() throws Exception {
8586 */
8687 @ Test
8788 void verifyManifestEntryName () throws Exception {
88- modifyJarEntryName (ORIGINAL_JAR , MODIFIED_JAR , "MANIFEST.MF" );
89+ modifyJarEntryName (ORIGINAL_JAR , MODIFIED_JAR , "META-INF/ MANIFEST.MF" );
8990 SecurityTools .jarsigner ("-verify -verbose " + MODIFIED_JAR )
9091 .shouldContain ("This JAR file contains internal " +
9192 "inconsistencies that may result in different " +
@@ -95,6 +96,22 @@ void verifyManifestEntryName() throws Exception {
9596 .shouldHaveExitValue (0 );
9697 }
9798
99+ /*
100+ * Modify a single byte in signature filename in LOC, and
101+ * validate that jarsigner -verify emits a warning message.
102+ */
103+ @ Test
104+ void verifySignatureEntryName () throws Exception {
105+ modifyJarEntryName (ORIGINAL_JAR , MODIFIED_JAR , "META-INF/MYKEY.SF" );
106+ SecurityTools .jarsigner ("-verify -verbose " + MODIFIED_JAR )
107+ .shouldContain ("This JAR file contains internal " +
108+ "inconsistencies that may result in different " +
109+ "contents when reading via JarFile and JarInputStream:" )
110+ .shouldContain ("- Entry XETA-INF/MYKEY.SF is present when reading " +
111+ "via JarInputStream but missing when reading via JarFile" )
112+ .shouldHaveExitValue (0 );
113+ }
114+
98115 /*
99116 * Validate that jarsigner -verify on a valid JAR works without
100117 * emitting warnings about internal inconsistencies.
@@ -111,9 +128,14 @@ void verifyOriginalJar() throws Exception {
111128 private void modifyJarEntryName (Path origJar , Path modifiedJar ,
112129 String entryName ) throws Exception {
113130 byte [] jarBytes = Files .readAllBytes (origJar );
114- var jarString = new String (jarBytes , StandardCharsets .UTF_8 );
115- var pos = jarString .indexOf (entryName );
116- assertTrue (pos != -1 , entryName + " is not present in the JAR" );
131+ byte [] entryNameBytes = entryName .getBytes (StandardCharsets .UTF_8 );
132+ int pos = 0 ;
133+ try {
134+ while (!Arrays .equals (jarBytes , pos , pos + entryNameBytes .length ,
135+ entryNameBytes , 0 , entryNameBytes .length )) pos ++;
136+ } catch (ArrayIndexOutOfBoundsException ignore ) {
137+ fail (entryName + " is not present in the JAR" );
138+ }
117139 jarBytes [pos ] = 'X' ;
118140 Files .write (modifiedJar , jarBytes );
119141 }
0 commit comments