From 6da7f4bfd113223e2d55893b533dc6eaa2a1d3db Mon Sep 17 00:00:00 2001 From: Yuri Golobokov Date: Mon, 28 Aug 2023 22:39:17 +0000 Subject: [PATCH 1/3] fix: checksum format --- .../v1/client/ChecksumEnforcingInputStream.java | 2 +- .../datastore/v1/client/EndToEndChecksumHandler.java | 10 +++++++--- .../v1/client/EndToEndChecksumHandlerTest.java | 11 ++++++++++- .../com/google/datastore/v1/client/RemoteRpcTest.java | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/ChecksumEnforcingInputStream.java b/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/ChecksumEnforcingInputStream.java index 0b91f18e2..e8cc4163a 100644 --- a/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/ChecksumEnforcingInputStream.java +++ b/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/ChecksumEnforcingInputStream.java @@ -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"); } } diff --git a/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/EndToEndChecksumHandler.java b/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/EndToEndChecksumHandler.java index 01aa303c8..3f840a8ba 100644 --- a/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/EndToEndChecksumHandler.java +++ b/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/EndToEndChecksumHandler.java @@ -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() { @@ -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) { @@ -76,6 +80,6 @@ void update(byte[] bytes, int off, int len) { } String hash() { - return hasher.hash().toString(); + return hashToString(hasher.hash()); } } diff --git a/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/EndToEndChecksumHandlerTest.java b/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/EndToEndChecksumHandlerTest.java index b0a7e4e86..e76c975cb 100644 --- a/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/EndToEndChecksumHandlerTest.java +++ b/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/EndToEndChecksumHandlerTest.java @@ -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; @@ -27,7 +28,9 @@ /** 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() { @@ -35,6 +38,12 @@ public void validateChecksum_correctChecksum() { 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)); diff --git a/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/RemoteRpcTest.java b/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/RemoteRpcTest.java index 281e92f04..28e3f20b8 100644 --- a/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/RemoteRpcTest.java +++ b/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/RemoteRpcTest.java @@ -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 From 677e7e20bbdeda0e664eea825f090cb66bd39419 Mon Sep 17 00:00:00 2001 From: Yuri Golobokov Date: Wed, 13 Sep 2023 17:57:22 +0000 Subject: [PATCH 2/3] fix lint error --- .../datastore/v1/client/EndToEndChecksumHandlerTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/EndToEndChecksumHandlerTest.java b/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/EndToEndChecksumHandlerTest.java index e76c975cb..1fdc1906a 100644 --- a/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/EndToEndChecksumHandlerTest.java +++ b/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/EndToEndChecksumHandlerTest.java @@ -28,7 +28,8 @@ /** Test for {@link EndToEndChecksumHandler}. */ @RunWith(JUnit4.class) public class EndToEndChecksumHandlerTest { - private final 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"; From d87edce80f30ed2dbb3c0a17b360b4973b0299a1 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Wed, 13 Sep 2023 18:06:56 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 662c0b5e4..cdab31b8b 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,13 @@ implementation 'com.google.cloud:google-cloud-datastore' If you are using Gradle without BOM, add this to your dependencies: ```Groovy -implementation 'com.google.cloud:google-cloud-datastore:2.16.3' +implementation 'com.google.cloud:google-cloud-datastore:2.17.1' ``` If you are using SBT, add this to your dependencies: ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.16.3" +libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.17.1" ``` @@ -370,7 +370,7 @@ Java is a registered trademark of Oracle and/or its affiliates. [kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-datastore/java11.html [stability-image]: https://img.shields.io/badge/stability-stable-green [maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-datastore.svg -[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-datastore/2.16.3 +[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-datastore/2.17.1 [authentication]: https://github.com/googleapis/google-cloud-java#authentication [auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes [predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles