Skip to content

Commit

Permalink
Fix test, then fix code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lazaroclapp committed Aug 12, 2019
1 parent 872f051 commit b968f05
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
Expand Up @@ -61,7 +61,7 @@ private static void LOG(boolean cond, String tag, String msg) {
private static final String BASE64_PATTERN =
"(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?";
private static final String DIGEST_ENTRY_PATTERN =
"Name: [A-Za-z0-9/\\$\\n\\s]+\\.class\\nSHA-256-Digest: " + BASE64_PATTERN;
"Name: [A-Za-z0-9/\\$\\n\\s\\-\\.]+[A-Za-z0-9]\\nSHA-256-Digest: " + BASE64_PATTERN;

private static void addAnnotationIfNotPresent(
List<AnnotationNode> annotationList, String annotation) {
Expand Down Expand Up @@ -154,17 +154,17 @@ private static void copyAndAnnotateJarEntry(
MethodReturnAnnotations nullableReturns,
boolean stripJarSignatures)
throws IOException {
jarOS.putNextEntry(new ZipEntry(jarEntry.getName()));
String entryName = jarEntry.getName();
if (entryName.endsWith(".class")) {
jarOS.putNextEntry(new ZipEntry(jarEntry.getName()));
annotateBytecode(is, jarOS, nonnullParams, nullableReturns);
} else if (entryName.equals("META-INF/MANIFEST.SF")) {
} else if (entryName.equals("META-INF/MANIFEST.MF")) {
// Read full file
StringBuilder stringBuilder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String currentLine;
while ((currentLine = br.readLine()) != null) {
stringBuilder.append(currentLine);
stringBuilder.append(currentLine + "\n");
}
String manifestText = stringBuilder.toString();
// Check for evidence of jar signing, note that lines can be split if too long so regex
Expand All @@ -174,13 +174,17 @@ private static void copyAndAnnotateJarEntry(
if (!manifestText.equals(manifestMinusDigests) && !stripJarSignatures) {
throw new SignedJarException(SIGNED_JAR_ERROR_MESSAGE);
}
jarOS.putNextEntry(new ZipEntry(jarEntry.getName()));
jarOS.write(manifestMinusDigests.getBytes("UTF-8"));
} else if (entryName.startsWith("META-INF/")
&& (entryName.endsWith(".DSA") || entryName.endsWith(".RSA"))) {
&& (entryName.endsWith(".DSA")
|| entryName.endsWith(".RSA")
|| entryName.endsWith(".SF"))) {
if (!stripJarSignatures) {
throw new SignedJarException(SIGNED_JAR_ERROR_MESSAGE);
} // the case where stripJarSignatures==true is handled by default by skipping these files
} else {
jarOS.putNextEntry(new ZipEntry(jarEntry.getName()));
jarOS.write(IOUtils.toByteArray(is));
}
jarOS.closeEntry();
Expand Down
Expand Up @@ -109,9 +109,9 @@ public static boolean compareEntriesInAars(String aarFile1, String aarFile2) thr

private static String readManifestFromJar(String jarfile) throws IOException {
JarFile jar = new JarFile(jarfile);
ZipEntry manifestEntry = jar.getEntry("META-INF/MANIFEST.SF");
ZipEntry manifestEntry = jar.getEntry("META-INF/MANIFEST.MF");
if (manifestEntry == null) {
return "";
throw new IllegalArgumentException("Jar does not contain a manifest at META-INF/MANIFEST.MF");
}
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader =
Expand All @@ -120,19 +120,20 @@ private static String readManifestFromJar(String jarfile) throws IOException {
while ((currentLine = bufferedReader.readLine()) != null) {
// Ignore empty new lines
if (currentLine.trim().length() > 0) {
stringBuilder.append(currentLine);
stringBuilder.append(currentLine + "\n");
}
}
return stringBuilder.toString();
}

/**
* Compares the META-INF/MANIFEST.SF file in the given 2 jar files. We ignore empty newlines.
* Compares the META-INF/MANIFEST.MF file in the given 2 jar files. We ignore empty newlines.
*
* @param jarFile1 Path to the first jar file.
* @param jarFile2 Path to the second jar file.
* @return True iff the MANIFEST.SF files in the two jar files exist and are the same.
* @return True iff the MANIFEST.MF files in the two jar files exist and are the same.
* @throws IOException
* @throws IllegalArgumentException
*/
public static boolean compareManifestContents(String jarFile1, String jarFile2)
throws IOException {
Expand Down
Expand Up @@ -486,11 +486,11 @@ public void testSignedJars() throws Exception {
Assert.assertTrue(
"Annotated jar after signature stripping does not have all the entries present in the input "
+ "jar (before signing), or contains extra (e.g. META-INF) entries!",
EntriesComparator.compareEntriesInJars(outputJarPath, inputJarPath));
EntriesComparator.compareEntriesInJars(outputJarPath, baseJarPath));
// Check that META-INF/Manifest.MF is content-identical to the original unsigned jar
Assert.assertTrue(
"Annotated jar after signature stripping does not preserve the info inside META-INF/MANIFEST.SF",
EntriesComparator.compareManifestContents(outputJarPath, inputJarPath));
"Annotated jar after signature stripping does not preserve the info inside META-INF/MANIFEST.MF",
EntriesComparator.compareManifestContents(outputJarPath, baseJarPath));
}

private byte[] sha1sum(String path) throws Exception {
Expand Down
11 changes: 11 additions & 0 deletions jar-infer/test-java-lib-jarinfer/build.gradle
Expand Up @@ -25,6 +25,17 @@ targetCompatibility = "1.8"

def astubxPath = "META-INF/nullaway/jarinfer.astubx"

jar {
manifest {
attributes(
'Build-Timestamp': new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
'Created-By' : "Gradle ${gradle.gradleVersion}",
'Build-Jdk' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
'Build-OS' : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
)
}
}

jar.doLast {
javaexec {
classpath = files("${rootProject.projectDir}/jar-infer/jar-infer-cli/build/libs/jar-infer-cli-${VERSION_NAME}.jar")
Expand Down

0 comments on commit b968f05

Please sign in to comment.