From 971f85e8af310e2b2c1cae1bae20531ff8d3d3e6 Mon Sep 17 00:00:00 2001 From: Dmitry Ivankov Date: Fri, 2 Oct 2020 10:55:59 +0200 Subject: [PATCH] Test for checksum file format, covering the leading zero case --- .../java_export/PublishShapeTest.java | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/tests/integration/java_export/PublishShapeTest.java b/tests/integration/java_export/PublishShapeTest.java index 64722a549..08a860869 100644 --- a/tests/integration/java_export/PublishShapeTest.java +++ b/tests/integration/java_export/PublishShapeTest.java @@ -7,7 +7,9 @@ import org.junit.rules.TemporaryFolder; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -18,6 +20,12 @@ import static org.junit.Assume.assumeFalse; public class PublishShapeTest { + // Jar content is fake, but chosen to produce leading zero in the checksum + // to be sure leading zeroes aren't omitted + private static final String JAR_CONTENTS = "magic!"; + private static final String JAR_MD5 = "05427eba78c92912c86d004b9857d6a0"; + private static final String JAR_SHA1 = "cbb0126a346a4dd6694fc48e3a94174fd1c7fa93"; + @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -34,17 +42,25 @@ public void publishingToAFileEndPointLooksCorrect() throws IOException, Interrup Path repoRoot = publish("com.example:my-lib:1.0.0"); // Check that the files are where we expect them to be - assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0.jar"))); - assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0.jar.md5"))); - assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0.jar.sha1"))); + checkJarShape(repoRoot, "com/example/my-lib/1.0.0/my-lib-1.0.0.jar"); + checkJarShape(repoRoot, "com/example/my-lib/1.0.0/my-lib-1.0.0-sources.jar"); + checkJarShape(repoRoot, "com/example/my-lib/1.0.0/my-lib-1.0.0-javadoc.jar"); + } + + private void checkJarShape(Path repoRoot, String path) throws IOException { + assertTrue(Files.exists(repoRoot.resolve(path))); + assertTrue(Files.exists(repoRoot.resolve(path + ".md5"))); + assertTrue(Files.exists(repoRoot.resolve(path + ".sha1"))); - assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-sources.jar"))); - assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-sources.jar.md5"))); - assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-sources.jar.sha1"))); + // Basic checksum format check + String md5 = new String(Files.readAllBytes(repoRoot.resolve(path + ".md5"))); + assertEquals(32, md5.length()); + String sha1 = new String(Files.readAllBytes(repoRoot.resolve(path + ".sha1"))); + assertEquals(40, sha1.length()); - assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-javadoc.jar"))); - assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-javadoc.jar.md5"))); - assertTrue(Files.exists(repoRoot.resolve("com/example/my-lib/1.0.0/my-lib-1.0.0-javadoc.jar.sha1"))); + // Check checksum values + assertEquals(JAR_MD5, md5); + assertEquals(JAR_SHA1, sha1); } private Path publish(String coordinates) throws IOException, InterruptedException { @@ -56,7 +72,7 @@ private Path publish(String coordinates) throws IOException, InterruptedExceptio // The publisher doesn't validate inputs (though remote maven repos do) // so we'll stub out the bits we need. - File stubJar = temp.newFile("dummy.jar"); + File stubJar = writeFile("dummy.jar", JAR_CONTENTS); File pomXml = temp.newFile("pom.xml"); // We'd prefer to use `bazel run`, but this is a reasonable proxy for @@ -87,4 +103,13 @@ private Path publish(String coordinates) throws IOException, InterruptedExceptio return repoRoot.toPath(); } + + private File writeFile(String name, String contents) throws IOException { + File file = temp.newFile(name); + + try (PrintStream out = new PrintStream(new FileOutputStream(file))) { + out.print(contents); + } + return file; + } }