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

fix: checksum format #1178

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -76,7 +76,7 @@ public int read(byte[] b, int off, int len) throws IOException {
endToEndChecksumHandler.update(b, off, i);
} else {
// no more payload to read. compute checksum and verify
if (!expectedChecksum.equalsIgnoreCase(endToEndChecksumHandler.hash())) {
if (!expectedChecksum.equals(endToEndChecksumHandler.hash())) {
jainsahab marked this conversation as resolved.
Show resolved Hide resolved
throw new IOException("possible memory corruption on payload detected");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ static String computeChecksum(byte[] bytes) {
return null;
}
HashCode hc = getNewCrc32cHasher().putBytes(bytes).hash();
return hc.toString();
return hashToString(hc);
}

private static String hashToString(HashCode hc) {
return String.valueOf(Integer.toUnsignedLong(hc.asInt()));
}

private static Hasher getNewCrc32cHasher() {
Expand All @@ -59,7 +63,7 @@ static boolean validateChecksum(String checksum, byte[] bytes) {
&& !checksum.isEmpty()
&& bytes != null
&& bytes.length > 0
&& checksum.equalsIgnoreCase(computeChecksum(bytes));
&& checksum.equals(computeChecksum(bytes));
}

static boolean hasChecksumHeader(HttpResponse response) {
Expand All @@ -76,6 +80,6 @@ void update(byte[] bytes, int off, int len) {
}

String hash() {
return hasher.hash().toString();
return hashToString(hasher.hash());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.datastore.v1.client;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -27,14 +28,22 @@
/** Test for {@link EndToEndChecksumHandler}. */
@RunWith(JUnit4.class)
public class EndToEndChecksumHandlerTest {
private byte[] payloadBytes = "This is a long string with numbers 1234, 134.56 ".getBytes(UTF_8);
private final byte[] payloadBytes = "This is a long string with numbers 1234, 134.56 ".getBytes(UTF_8);
private final byte[] payloadForUnsignedLongChecksum = "aaa".getBytes(UTF_8);
private final String unsignedLongChecksum = "3818383321";

@Test
public void validateChecksum_correctChecksum() {
String computed = EndToEndChecksumHandler.computeChecksum(payloadBytes);
assertTrue(EndToEndChecksumHandler.validateChecksum(computed, payloadBytes));
}

@Test
public void computeChecksum_returnsUnsignedLongAsStringValue() {
String computed = EndToEndChecksumHandler.computeChecksum(payloadForUnsignedLongChecksum);
assertEquals("computeChecksum return value", unsignedLongChecksum, computed);
}

@Test
public void validateChecksum_incorrectChecksum() {
String computed = EndToEndChecksumHandler.computeChecksum("random string".getBytes(UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void testHttpHeaders_expectE2eChecksumHeader() throws IOException {
httpRequest
.getHeaders()
.getFirstHeaderStringValue(EndToEndChecksumHandler.HTTP_REQUEST_CHECKSUM_HEADER);
assertEquals(8, header.length());
assertEquals(9, header.length());
}

@Test
Expand Down