Skip to content

Commit

Permalink
Add additional logging when validating fetched android-all Jars
Browse files Browse the repository at this point in the history
There is an ongoing issue where the SHA-512 checksums for fetched
android-all Jars do not match the expected checksum. Add additional
logging to provide a little more context if this error occurs.

Also, add 'flush' and 'fsync' calls to ensure the file contents are
written to disk.

Updates #8205

PiperOrigin-RevId: 532812643
  • Loading branch information
hoisie committed May 17, 2023
1 parent 8311039 commit f1e7b43
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
3 changes: 3 additions & 0 deletions plugins/maven-dependency-resolver/build.gradle
Expand Up @@ -49,8 +49,11 @@ afterEvaluate {
dependencies {
api project(":pluginapi")
api project(":utils")
api "com.google.auto.value:auto-value-annotations:1.10.1"
api "com.google.guava:guava:$guavaJREVersion"

annotationProcessor "com.google.auto.value:auto-value:1.10.1"

testImplementation "junit:junit:$junitVersion"
testImplementation "org.mockito:mockito-core:$mockitoVersion"
testImplementation "com.google.truth:truth:$truthVersion"
Expand Down
Expand Up @@ -2,6 +2,7 @@

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.auto.value.AutoValue;
import com.google.common.base.Strings;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
Expand All @@ -24,6 +25,7 @@
import java.util.Base64;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import javax.annotation.Nonnull;
import org.robolectric.util.Logger;

/**
Expand Down Expand Up @@ -82,15 +84,27 @@ public void fetchArtifact(MavenJarArtifact artifact) {
return Futures.immediateFuture(null);
}
createArtifactSubdirectory(artifact, localRepositoryDir);
boolean pomValid =
ValidationResult pomResult =
validateStagedFiles(artifact.pomPath(), artifact.pomSha512Path());
if (!pomValid) {
throw new AssertionError("SHA512 mismatch for POM file fetched in " + artifact);
if (!pomResult.isSuccess()) {
throw new AssertionError(
"SHA-512 mismatch for POM file for "
+ artifact
+ ", expected SHA-512="
+ pomResult.expectedHashCode()
+ ", actual SHA-512="
+ pomResult.calculatedHashCode());
}
boolean jarValid =
ValidationResult jarResult =
validateStagedFiles(artifact.jarPath(), artifact.jarSha512Path());
if (!jarValid) {
throw new AssertionError("SHA512 mismatch for JAR file fetched in " + artifact);
if (!jarResult.isSuccess()) {
throw new AssertionError(
"SHA-512 mismatch for POM file for "
+ artifact
+ ", expected SHA-512="
+ jarResult.expectedHashCode()
+ ", actual SHA-512="
+ jarResult.calculatedHashCode());
}
Logger.info(
String.format(
Expand Down Expand Up @@ -123,15 +137,33 @@ private void removeArtifactFiles(File repositoryDir, MavenJarArtifact artifact)
new File(repositoryDir, artifact.pomSha512Path()).delete();
}

private boolean validateStagedFiles(String filePath, String sha512Path) throws IOException {
private ValidationResult validateStagedFiles(String filePath, String sha512Path)
throws IOException {
File tempFile = new File(this.stagingRepositoryDir, filePath);
File sha512File = new File(this.stagingRepositoryDir, sha512Path);

HashCode expected =
HashCode.fromString(new String(Files.asByteSource(sha512File).read(), UTF_8));

HashCode actual = Files.asByteSource(tempFile).hash(Hashing.sha512());
return expected.equals(actual);
return ValidationResult.create(expected.equals(actual), expected.toString(), actual.toString());
}

@AutoValue
abstract static class ValidationResult {
abstract boolean isSuccess();

@Nonnull
abstract String expectedHashCode();

@Nonnull
abstract String calculatedHashCode();

static ValidationResult create(
boolean isSuccess, String expectedHashCode, String calculatedHashCode) {
return new AutoValue_MavenArtifactFetcher_ValidationResult(
isSuccess, expectedHashCode, calculatedHashCode);
}
}

private void createArtifactSubdirectory(MavenJarArtifact artifact, File repositoryDir)
Expand Down Expand Up @@ -218,6 +250,9 @@ public ListenableFuture<Void> call() throws Exception {
try (InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(localFile)) {
ByteStreams.copy(inputStream, outputStream);
// Ensure all contents are written to disk.
outputStream.flush();
outputStream.getFD().sync();
}
return Futures.immediateFuture(null);
}
Expand Down

0 comments on commit f1e7b43

Please sign in to comment.