Repository: googleapis/java-datastore
Pull request #548 (2021-09-27 21:53:10)

chore: re-generated to pick up changes from synthtool
This PR was generated using Autosynth. :rainbow:

Synth log will be available here:
https://source.cloud.google.com/results/invocations/d54bc33e-3d97-423d-807a-1d3265c1812e/targets

- [ ] To automatically regenerate this PR, check this box. (May take up to 24 hours.)

Source-Link: https://github.com/googleapis/synthtool/commit/a4be3384ccb92364795d981f2863f6986fcee620
Source-Link: https://github.com/googleapis/synthtool/commit/67ab4f9f4272ad13f4b809de47fd0dec05f425ad


Patch:
diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml
index 1a23ea42..2176b054 100644
--- a/.github/blunderbuss.yml
+++ b/.github/blunderbuss.yml
@@ -1,5 +1,5 @@
 # Configuration for the Blunderbuss GitHub app. For more info see
-# https://github.com/googleapis/repo-automation-bots/tree/master/packages/blunderbuss
+# https://github.com/googleapis/repo-automation-bots/tree/main/packages/blunderbuss
 assign_prs_by:
 - labels:
   - samples
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 3becb5c0..2425d723 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -1,7 +1,7 @@
 on:
   push:
     branches:
-    - master
+    - main
   pull_request:
 name: ci
 jobs:
@@ -9,14 +9,15 @@ jobs:
     runs-on: ubuntu-latest
     strategy:
       matrix:
-        java: [8, 11]
+        java: [8, 11, 17]
     steps:
     - uses: actions/checkout@v2
     - uses: stCarolas/setup-maven@v4
       with:
         maven-version: 3.8.1
-    - uses: actions/setup-java@v1
+    - uses: actions/setup-java@v2
       with:
+        distribution: zulu
         java-version: ${{matrix.java}}
     - run: java -version
     - run: .kokoro/build.sh
@@ -29,8 +30,9 @@ jobs:
     - uses: stCarolas/setup-maven@v4
       with:
         maven-version: 3.8.1
-    - uses: actions/setup-java@v1
+    - uses: actions/setup-java@v2
       with:
+        distribution: zulu
         java-version: 8
     - run: java -version
     - run: .kokoro/build.bat
@@ -40,14 +42,15 @@ jobs:
     runs-on: ubuntu-latest
     strategy:
       matrix:
-        java: [8, 11]
+        java: [8, 11, 17]
     steps:
     - uses: actions/checkout@v2
     - uses: stCarolas/setup-maven@v4
       with:
         maven-version: 3.8.1
-    - uses: actions/setup-java@v1
+    - uses: actions/setup-java@v2
       with:
+        distribution: zulu
         java-version: ${{matrix.java}}
     - run: java -version
     - run: .kokoro/dependencies.sh
@@ -58,8 +61,9 @@ jobs:
     - uses: stCarolas/setup-maven@v4
       with:
         maven-version: 3.8.1
-    - uses: actions/setup-java@v1
+    - uses: actions/setup-java@v2
       with:
+        distribution: zulu
         java-version: 8
     - run: java -version
     - run: .kokoro/build.sh
@@ -72,8 +76,9 @@ jobs:
     - uses: stCarolas/setup-maven@v4
       with:
         maven-version: 3.8.1
-    - uses: actions/setup-java@v1
+    - uses: actions/setup-java@v2
       with:
+        distribution: zulu
         java-version: 8
     - run: java -version
     - run: .kokoro/build.sh
diff --git a/.kokoro/dependencies.sh b/.kokoro/dependencies.sh
index 9030ba8f..9a5105d7 100755
--- a/.kokoro/dependencies.sh
+++ b/.kokoro/dependencies.sh
@@ -28,7 +28,28 @@ source ${scriptDir}/common.sh
 java -version
 echo $JOB_TYPE
 
-export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=128m"
+function determineMavenOpts() {
+  local javaVersion=$(
+    # filter down to the version line, then pull out the version between quotes,
+    # then trim the version number down to its minimal number (removing any
+    # update or suffix number).
+    java -version 2>&1 | grep "version" \
+      | sed -E 's/^.*"(.*?)".*$/\1/g' \
+      | sed -E 's/^(1\.[0-9]\.0).*$/\1/g'
+  )
+
+  case $javaVersion in
+    "17")
+      # MaxPermSize is no longer supported as of jdk 17
+      echo -n "-Xmx1024m"
+      ;;
+    *)
+      echo -n "-Xmx1024m -XX:MaxPermSize=128m"
+      ;;
+  esac
+}
+
+export MAVEN_OPTS=$(determineMavenOpts)
 
 # this should run maven enforcer
 retry_with_backoff 3 10 \
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 2b967491..0b91f18e 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
@@ -19,25 +19,22 @@ import com.google.api.client.http.HttpResponse;
 import com.google.common.annotations.VisibleForTesting;
 import java.io.IOException;
 import java.io.InputStream;
-import java.security.MessageDigest;
 
 /** This class provides End-to-End Checksum API for http protocol. */
 class ChecksumEnforcingInputStream extends InputStream {
   private final InputStream delegate;
-  private final MessageDigest messageDigest;
+  private final EndToEndChecksumHandler endToEndChecksumHandler;
   private final String expectedChecksum;
 
-  ChecksumEnforcingInputStream(
-      InputStream originalInputStream, HttpResponse response, MessageDigest digest) {
-    this(originalInputStream, EndToEndChecksumHandler.getChecksumHeader(response), digest);
+  ChecksumEnforcingInputStream(InputStream originalInputStream, HttpResponse response) {
+    this(originalInputStream, EndToEndChecksumHandler.getChecksumHeader(response));
   }
 
   @VisibleForTesting
-  ChecksumEnforcingInputStream(
-      InputStream originalInputStream, String checksum, MessageDigest digest) {
+  ChecksumEnforcingInputStream(InputStream originalInputStream, String checksum) {
     delegate = originalInputStream;
     expectedChecksum = checksum;
-    messageDigest = digest;
+    endToEndChecksumHandler = new EndToEndChecksumHandler();
   }
 
   @Override
@@ -76,11 +73,10 @@ class ChecksumEnforcingInputStream extends InputStream {
     if (len <= 0) return 0;
     int i = delegate.read(b, off, len);
     if (i > 0) {
-      messageDigest.update(b, off, i);
+      endToEndChecksumHandler.update(b, off, i);
     } else {
       // no more payload to read. compute checksum and verify
-      if (!expectedChecksum.equalsIgnoreCase(
-          com.google.common.io.BaseEncoding.base16().encode(messageDigest.digest()))) {
+      if (!expectedChecksum.equalsIgnoreCase(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 06d08d49..01aa303c 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
@@ -16,8 +16,9 @@
 package com.google.datastore.v1.client;
 
 import com.google.api.client.http.HttpResponse;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
+import com.google.common.hash.HashCode;
+import com.google.common.hash.Hasher;
+import com.google.common.hash.Hashing;
 
 /** This class provides End-to-End Checksum API for http protocol. */
 class EndToEndChecksumHandler {
@@ -25,22 +26,25 @@ class EndToEndChecksumHandler {
   static final String HTTP_REQUEST_CHECKSUM_HEADER = "x-request-checksum-348659783";
   /** The checksum http header on http responses */
   static final String HTTP_RESPONSE_CHECKSUM_HEADER = "x-response-checksum-348659783";
-  /** Algorithm used for checksum */
-  private static final String MD5 = "MD5";
+
+  final Hasher hasher = EndToEndChecksumHandler.getNewCrc32cHasher();
 
   /**
    * Create and return checksum as a string value for the input 'bytes'.
    *
    * @param bytes raw message for which the checksum is being computed
    * @return computed checksum as a hex string
-   * @throws RuntimeException if MD5 Algorithm is not found in the VM
    */
   static String computeChecksum(byte[] bytes) {
     if (bytes == null || (bytes.length == 0)) {
       return null;
     }
-    return com.google.common.io.BaseEncoding.base16()
-        .encode(getMessageDigestInstance().digest(bytes));
+    HashCode hc = getNewCrc32cHasher().putBytes(bytes).hash();
+    return hc.toString();
+  }
+
+  private static Hasher getNewCrc32cHasher() {
+    return Hashing.crc32c().newHasher();
   }
 
   /**
@@ -58,14 +62,6 @@ class EndToEndChecksumHandler {
         && checksum.equalsIgnoreCase(computeChecksum(bytes));
   }
 
-  static MessageDigest getMessageDigestInstance() {
-    try {
-      return MessageDigest.getInstance(MD5);
-    } catch (NoSuchAlgorithmException e) {
-      throw new RuntimeException("MD5 algorithm is not found when computing checksum!");
-    }
-  }
-
   static boolean hasChecksumHeader(HttpResponse response) {
     String checksum = getChecksumHeader(response);
     return checksum != null && !checksum.isEmpty();
@@ -74,4 +70,12 @@ class EndToEndChecksumHandler {
   static String getChecksumHeader(HttpResponse response) {
     return response.getHeaders().getFirstHeaderStringValue(HTTP_RESPONSE_CHECKSUM_HEADER);
   }
+
+  void update(byte[] bytes, int off, int len) {
+    hasher.putBytes(bytes, off, len);
+  }
+
+  String hash() {
+    return hasher.hash().toString();
+  }
 }
diff --git a/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/RemoteRpc.java b/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/RemoteRpc.java
index 2c9d4d34..321eea72 100644
--- a/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/RemoteRpc.java
+++ b/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/RemoteRpc.java
@@ -109,8 +109,7 @@ class RemoteRpc {
         }
         InputStream inputStream = httpResponse.getContent();
         return enableE2EChecksum && EndToEndChecksumHandler.hasChecksumHeader(httpResponse)
-            ? new ChecksumEnforcingInputStream(
-                inputStream, httpResponse, EndToEndChecksumHandler.getMessageDigestInstance())
+            ? new ChecksumEnforcingInputStream(inputStream, httpResponse)
             : inputStream;
       } catch (SocketTimeoutException e) {
         throw makeException(url, methodName, Code.DEADLINE_EXCEEDED, "Deadline exceeded", e);
diff --git a/synth.metadata b/synth.metadata
index 5872b771..09d17998 100644
--- a/synth.metadata
+++ b/synth.metadata
@@ -4,7 +4,7 @@
       "git": {
         "name": ".",
         "remote": "https://github.com/googleapis/java-datastore.git",
-        "sha": "bd22515ba6670dd9cf56bd82ff4cdd0b40b5b106"
+        "sha": "baddb6a311134d64b5e5894b661a7f7191c1b325"
       }
     },
     {
@@ -19,7 +19,7 @@
       "git": {
         "name": "synthtool",
         "remote": "https://github.com/googleapis/synthtool.git",
-        "sha": "0752ff727a19a467dffed335d5e59303689cf0d1"
+        "sha": "a4be3384ccb92364795d981f2863f6986fcee620"
       }
     }
   ],


Test patch:
diff --git a/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/ChecksumEnforcingInputStreamTest.java b/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/ChecksumEnforcingInputStreamTest.java
index 47592f98..dbe0e766 100644
--- a/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/ChecksumEnforcingInputStreamTest.java
+++ b/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/ChecksumEnforcingInputStreamTest.java
@@ -21,7 +21,6 @@ import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.security.MessageDigest;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
@@ -29,8 +28,6 @@ import org.junit.runners.JUnit4;
 /** Test for {@link ChecksumEnforcingInputStream}. */
 @RunWith(JUnit4.class)
 public class ChecksumEnforcingInputStreamTest {
-  private final MessageDigest digest = EndToEndChecksumHandler.getMessageDigestInstance();
-
   public void test(int payloadSize) throws Exception {
     // read 1000 bytes at a time
     // Since checksum should be correct, do not expect IOException
@@ -40,7 +37,7 @@ public class ChecksumEnforcingInputStreamTest {
         // do nothing with the bytes read
       }
     } catch (IOException e) {
-      fail("checksum verification failed!");
+      fail("checksum verification failed! " + e.getMessage());
     }
   }
 
@@ -66,9 +63,7 @@ public class ChecksumEnforcingInputStreamTest {
     // Since checksum should be correct, do not expect IOException
     try (ChecksumEnforcingInputStream instance =
         new ChecksumEnforcingInputStream(
-            new ByteArrayInputStream("hello there".getBytes(UTF_8)),
-            "this checksum is invalid",
-            digest)) {
+            new ByteArrayInputStream("hello there".getBytes(UTF_8)), "this checksum is invalid")) {
       byte[] buf = new byte[1000];
       while (instance.read(buf, 0, 1000) != -1) {
         // do nothing with the bytes read
@@ -103,7 +98,6 @@ public class ChecksumEnforcingInputStreamTest {
     }
     byte[] bytes = payload.getBytes(UTF_8);
     String expectedChecksum = EndToEndChecksumHandler.computeChecksum(bytes);
-    return new ChecksumEnforcingInputStream(
-        new ByteArrayInputStream(bytes), expectedChecksum, digest);
+    return new ChecksumEnforcingInputStream(new ByteArrayInputStream(bytes), expectedChecksum);
   }
 }
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 b5f82369..ebcb1239 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
@@ -174,7 +174,7 @@ public class RemoteRpcTest {
         httpRequest
             .getHeaders()
             .getFirstHeaderStringValue(EndToEndChecksumHandler.HTTP_REQUEST_CHECKSUM_HEADER);
-    assertEquals(32, header.length());
+    assertEquals(8, header.length());
   }
 
   @Test