Skip to content

Commit

Permalink
fix: checksum format (#1178)
Browse files Browse the repository at this point in the history
Fix checksum format to match the server's format -- unsigned long.
  • Loading branch information
nimf committed Sep 13, 2023
1 parent 76e3a71 commit 410b939
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
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())) {
throw new IOException("possible memory corruption on payload detected");
}
}
Expand Down
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());
}
}
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,23 @@
/** 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
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

0 comments on commit 410b939

Please sign in to comment.