Skip to content

Commit 410b939

Browse files
authored
fix: checksum format (#1178)
Fix checksum format to match the server's format -- unsigned long.
1 parent 76e3a71 commit 410b939

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

β€Ždatastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/ChecksumEnforcingInputStream.javaβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public int read(byte[] b, int off, int len) throws IOException {
7676
endToEndChecksumHandler.update(b, off, i);
7777
} else {
7878
// no more payload to read. compute checksum and verify
79-
if (!expectedChecksum.equalsIgnoreCase(endToEndChecksumHandler.hash())) {
79+
if (!expectedChecksum.equals(endToEndChecksumHandler.hash())) {
8080
throw new IOException("possible memory corruption on payload detected");
8181
}
8282
}

β€Ždatastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/EndToEndChecksumHandler.javaβ€Ž

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ static String computeChecksum(byte[] bytes) {
4040
return null;
4141
}
4242
HashCode hc = getNewCrc32cHasher().putBytes(bytes).hash();
43-
return hc.toString();
43+
return hashToString(hc);
44+
}
45+
46+
private static String hashToString(HashCode hc) {
47+
return String.valueOf(Integer.toUnsignedLong(hc.asInt()));
4448
}
4549

4650
private static Hasher getNewCrc32cHasher() {
@@ -59,7 +63,7 @@ static boolean validateChecksum(String checksum, byte[] bytes) {
5963
&& !checksum.isEmpty()
6064
&& bytes != null
6165
&& bytes.length > 0
62-
&& checksum.equalsIgnoreCase(computeChecksum(bytes));
66+
&& checksum.equals(computeChecksum(bytes));
6367
}
6468

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

7882
String hash() {
79-
return hasher.hash().toString();
83+
return hashToString(hasher.hash());
8084
}
8185
}

β€Ždatastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/EndToEndChecksumHandlerTest.javaβ€Ž

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.datastore.v1.client;
1717

1818
import static java.nio.charset.StandardCharsets.UTF_8;
19+
import static org.junit.Assert.assertEquals;
1920
import static org.junit.Assert.assertFalse;
2021
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertTrue;
@@ -27,14 +28,23 @@
2728
/** Test for {@link EndToEndChecksumHandler}. */
2829
@RunWith(JUnit4.class)
2930
public class EndToEndChecksumHandlerTest {
30-
private byte[] payloadBytes = "This is a long string with numbers 1234, 134.56 ".getBytes(UTF_8);
31+
private final byte[] payloadBytes =
32+
"This is a long string with numbers 1234, 134.56 ".getBytes(UTF_8);
33+
private final byte[] payloadForUnsignedLongChecksum = "aaa".getBytes(UTF_8);
34+
private final String unsignedLongChecksum = "3818383321";
3135

3236
@Test
3337
public void validateChecksum_correctChecksum() {
3438
String computed = EndToEndChecksumHandler.computeChecksum(payloadBytes);
3539
assertTrue(EndToEndChecksumHandler.validateChecksum(computed, payloadBytes));
3640
}
3741

42+
@Test
43+
public void computeChecksum_returnsUnsignedLongAsStringValue() {
44+
String computed = EndToEndChecksumHandler.computeChecksum(payloadForUnsignedLongChecksum);
45+
assertEquals("computeChecksum return value", unsignedLongChecksum, computed);
46+
}
47+
3848
@Test
3949
public void validateChecksum_incorrectChecksum() {
4050
String computed = EndToEndChecksumHandler.computeChecksum("random string".getBytes(UTF_8));

β€Ždatastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/RemoteRpcTest.javaβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void testHttpHeaders_expectE2eChecksumHeader() throws IOException {
176176
httpRequest
177177
.getHeaders()
178178
.getFirstHeaderStringValue(EndToEndChecksumHandler.HTTP_REQUEST_CHECKSUM_HEADER);
179-
assertEquals(8, header.length());
179+
assertEquals(9, header.length());
180180
}
181181

182182
@Test

0 commit comments

Comments
 (0)