Skip to content

Commit

Permalink
Merge 093ffb2 into f22c416
Browse files Browse the repository at this point in the history
  • Loading branch information
lazaroclapp committed Sep 19, 2018
2 parents f22c416 + 093ffb2 commit 5dbfdb5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -309,7 +310,12 @@ private static void writeModelJAR(String outPath) throws IOException {
outPath.endsWith(MODEL_JAR_SUFFIX), "invalid model file path! " + outPath);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outPath));
if (!map_result.isEmpty()) {
zos.putNextEntry(new ZipEntry(DEFAULT_ASTUBX_LOCATION));
ZipEntry entry = new ZipEntry(DEFAULT_ASTUBX_LOCATION);
// Set the modification/creation time to 0 to ensure that this jars always have the same
// checksum
entry.setTime(0);
entry.setCreationTime(FileTime.fromMillis(0));
zos.putNextEntry(entry);
writeModel(new DataOutputStream(zos));
zos.closeEntry();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import com.sun.tools.javac.main.Main;
import com.sun.tools.javac.main.Main.Result;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -254,4 +257,34 @@ public void toyNullTestAPI() throws Exception {
" }",
"}");
}

@Test
public void jarinferOutputJarIsBytePerByteDeterministic() throws Exception {
DefinitelyDerefedParamsDriver.reset();
String jarPath = "../test-java-lib-jarinfer/build/libs/test-java-lib-jarinfer.jar";
String pkg = "com.uber.nullaway.jarinfer.toys.unannotated";
DefinitelyDerefedParamsDriver.run(jarPath, "L" + pkg.replaceAll("\\.", "/"));
byte[] checksumBytes1 = sha1sum(DefinitelyDerefedParamsDriver.lastOutPath);
// Wait a second to ensure system time has changed
Thread.sleep(1);
DefinitelyDerefedParamsDriver.run(jarPath, "L" + pkg.replaceAll("\\.", "/"));
byte[] checksumBytes2 = sha1sum(DefinitelyDerefedParamsDriver.lastOutPath);
Assert.assertTrue(Arrays.equals(checksumBytes1, checksumBytes2));
}

public byte[] sha1sum(String path) throws Exception {
File file = new File(path);
MessageDigest digest = MessageDigest.getInstance("SHA-1");
InputStream fis = new FileInputStream(file);
int n = 0;
byte[] buffer = new byte[8192];
while (n != -1) {
n = fis.read(buffer);
if (n > 0) {
digest.update(buffer, 0, n);
}
}
fis.close();
return digest.digest();
}
}

0 comments on commit 5dbfdb5

Please sign in to comment.