Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output leading zeroes in maven export checksums #453

Merged
merged 2 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,18 @@ private static CompletableFuture<Void> upload(
}

private static String toSha1(byte[] toHash) {
return toHexS("SHA-1", toHash);
return toHexS("%040x", "SHA-1", toHash);
}

private static String toMd5(byte[] toHash) {
return toHexS("MD5", toHash);
return toHexS("%032x", "MD5", toHash);
}

private static String toHexS(String algorithm, byte[] toHash) {
private static String toHexS(String fmt, String algorithm, byte[] toHash) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
digest.update(toHash);
return new BigInteger(1, digest.digest()).toString(16);
return String.format(fmt, new BigInteger(1, digest.digest()));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
Expand Down
45 changes: 35 additions & 10 deletions tests/integration/java_export/PublishShapeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}