Skip to content

Commit

Permalink
Throw a checked exception from Checksum instead of IllegalArgumentExc…
Browse files Browse the repository at this point in the history
…eption.

PiperOrigin-RevId: 358808320
  • Loading branch information
janakdr authored and copybara-github committed Feb 22, 2021
1 parent 4279f84 commit 3f2b1f3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@

/** The content checksum for an HTTP download, which knows its own type. */
public class Checksum {
/** Exception thrown to indicate that a string is not a valid checksum for that key type. */
public static final class InvalidChecksumException extends Exception {
private InvalidChecksumException(KeyType keyType, String hash) {
super("Invalid " + keyType + " checksum '" + hash + "'");
}
}

private final KeyType keyType;
private final HashCode hashCode;

Expand All @@ -29,15 +36,16 @@ private Checksum(KeyType keyType, HashCode hashCode) {
}

/** Constructs a new Checksum for a given key type and hash, in hex format. */
public static Checksum fromString(KeyType keyType, String hash) {
public static Checksum fromString(KeyType keyType, String hash) throws InvalidChecksumException {
if (!keyType.isValid(hash)) {
throw new IllegalArgumentException("Invalid " + keyType + " checksum '" + hash + "'");
throw new InvalidChecksumException(keyType, hash);
}
return new Checksum(keyType, HashCode.fromString(hash));
}

/** Constructs a new Checksum from a hash in Subresource Integrity format. */
public static Checksum fromSubresourceIntegrity(String integrity) {
public static Checksum fromSubresourceIntegrity(String integrity)
throws InvalidChecksumException {
Base64.Decoder decoder = Base64.getDecoder();
KeyType keyType = null;
byte[] hash = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,14 @@ private Checksum calculateChecksum(Optional<Checksum> originalChecksum, Path pat
// The checksum is checked on download, so if we got here, the user provided checksum is good
return originalChecksum.get();
}
return Checksum.fromString(KeyType.SHA256, RepositoryCache.getChecksum(KeyType.SHA256, path));
try {
return Checksum.fromString(KeyType.SHA256, RepositoryCache.getChecksum(KeyType.SHA256, path));
} catch (Checksum.InvalidChecksumException e) {
throw new IllegalStateException(
"Unexpected invalid checksum from internal computation of SHA-256 checksum on "
+ path.getPathString(),
e);
}
}

private Optional<Checksum> validateChecksum(String sha256, String integrity, List<URL> urls)
Expand All @@ -986,7 +993,7 @@ private Optional<Checksum> validateChecksum(String sha256, String integrity, Lis
}
try {
return Optional.of(Checksum.fromString(KeyType.SHA256, sha256));
} catch (IllegalArgumentException e) {
} catch (Checksum.InvalidChecksumException e) {
warnAboutChecksumError(urls, e.getMessage());
throw new RepositoryFunctionException(
Starlark.errorf(
Expand All @@ -1002,7 +1009,7 @@ private Optional<Checksum> validateChecksum(String sha256, String integrity, Lis

try {
return Optional.of(Checksum.fromSubresourceIntegrity(integrity));
} catch (IllegalArgumentException e) {
} catch (Checksum.InvalidChecksumException e) {
warnAboutChecksumError(urls, e.getMessage());
throw new RepositoryFunctionException(
Starlark.errorf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ public class HttpConnectorMultiplexerIntegrationTest {
private final HttpConnectorMultiplexer multiplexer =
new HttpConnectorMultiplexer(eventHandler, connector, httpStreamFactory, clock, sleeper);

private static Optional<Checksum> makeChecksum(String string) {
try {
return Optional.of(Checksum.fromString(KeyType.SHA256, string));
} catch (Checksum.InvalidChecksumException e) {
throw new IllegalStateException(e);
}
}

private static final Optional<Checksum> HELLO_SHA256 =
Optional.of(
Checksum.fromString(
KeyType.SHA256, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"));
makeChecksum("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");

@Before
public void before() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ public class HttpConnectorMultiplexerTest {
private static final byte[] data2 = "second".getBytes(UTF_8);
private static final byte[] data3 = "third".getBytes(UTF_8);

private static Optional<Checksum> makeChecksum(String string) {
try {
return Optional.of(Checksum.fromString(KeyType.SHA256, string));
} catch (Checksum.InvalidChecksumException e) {
throw new IllegalStateException(e);
}
}

private static final Optional<Checksum> DUMMY_CHECKSUM =
Optional.of(
Checksum.fromString(
KeyType.SHA256, "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"));
makeChecksum("abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd");

@Rule
public final ExpectedException thrown = ExpectedException.none();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,19 @@ public class HttpStreamTest {

private static final Random randoCalrissian = new Random();
private static final byte[] data = "hello".getBytes(UTF_8);

private static Optional<Checksum> makeChecksum(String string) {
try {
return Optional.of(Checksum.fromString(KeyType.SHA256, string));
} catch (Checksum.InvalidChecksumException e) {
throw new IllegalStateException(e);
}
}

private static final Optional<Checksum> GOOD_CHECKSUM =
Optional.of(
Checksum.fromString(
KeyType.SHA256, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"));
makeChecksum("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");
private static final Optional<Checksum> BAD_CHECKSUM =
Optional.of(
Checksum.fromString(
KeyType.SHA256, "0000000000000000000000000000000000000000000000000000000000000000"));
makeChecksum("0000000000000000000000000000000000000000000000000000000000000000");
private static final URL AURL = makeUrl("http://doodle.example");

@Rule
Expand Down Expand Up @@ -180,9 +185,7 @@ public void bigDataWithValidChecksum_readsOk() throws Exception {
streamFactory.create(
connection,
AURL,
Optional.of(
Checksum.fromString(
KeyType.SHA256, Hashing.sha256().hashBytes(bigData).toString())),
makeChecksum(Hashing.sha256().hashBytes(bigData).toString()),
reconnector)) {
assertThat(toByteArray(stream)).isEqualTo(bigData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void fetchBlob(
downloadBlob(
downloader,
new URL("http://example.com/content.txt"),
Optional.<Checksum>of(Checksum.fromString(KeyType.SHA256, contentDigest.getHash())));
Optional.of(Checksum.fromString(KeyType.SHA256, contentDigest.getHash())));

assertThat(downloaded).isEqualTo(content);
}
Expand Down Expand Up @@ -272,8 +272,7 @@ public void fetchBlob(
downloadBlob(
downloader,
new URL("http://example.com/content.txt"),
Optional.<Checksum>of(
Checksum.fromString(KeyType.SHA256, contentDigest.getHash()))));
Optional.of(Checksum.fromString(KeyType.SHA256, contentDigest.getHash()))));

assertThat(e).hasMessageThat().contains(contentDigest.getHash());
assertThat(e).hasMessageThat().contains(DIGEST_UTIL.computeAsUtf8("wrong content").getHash());
Expand Down

0 comments on commit 3f2b1f3

Please sign in to comment.