From 4747ecdcbf4f3495df4229b818d18cd8c1e6e16a Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 7 Jun 2022 17:53:55 -0700 Subject: [PATCH 1/4] test: add property testing to Crc32cCombineGoogle --- .../storage/Crc32cUtilityPropertyTest.java | 45 +++++++++++++++++++ .../storage/jqwik/StorageArbitraries.java | 2 + 2 files changed, 47 insertions(+) create mode 100644 google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java new file mode 100644 index 000000000..83a9bbe7c --- /dev/null +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.cloud.storage; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.hash.Hashing; +import net.jqwik.api.Arbitraries; +import net.jqwik.api.Arbitrary; +import net.jqwik.api.Example; +import net.jqwik.api.ForAll; +import net.jqwik.api.Provide; + +public class Crc32cUtilityPropertyTest { + @Example + public void testCrc32cCombinePropertyTest( + @ForAll("randomData") String firstObject, @ForAll("randomData") String secondObject) { + int firstPartHash = Hashing.crc32c().hashBytes(firstObject.getBytes()).asInt(); + int secondPartHash = Hashing.crc32c().hashBytes(secondObject.getBytes()).asInt(); + String mergedParts = firstObject + secondObject; + int combined = Hashing.crc32c().hashBytes(mergedParts.getBytes()).asInt(); + assertThat(combined) + .isEqualTo( + Crc32cUtility.crc32cCombineGoogle( + firstPartHash, secondPartHash, secondObject.getBytes().length)); + } + + @Provide("randomData") + Arbitrary stringArrays() { + return Arbitraries.strings().ofMinLength(0).ofMaxLength(1024 * 1024); + } +} diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/jqwik/StorageArbitraries.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/jqwik/StorageArbitraries.java index 842cec59a..68e9a3acb 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/jqwik/StorageArbitraries.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/jqwik/StorageArbitraries.java @@ -34,6 +34,7 @@ import net.jqwik.api.Arbitraries; import net.jqwik.api.Arbitrary; import net.jqwik.api.Combinators; +import net.jqwik.api.Provide; import net.jqwik.api.Tuple; import net.jqwik.api.arbitraries.ListArbitrary; import net.jqwik.api.providers.TypeUsage; @@ -304,6 +305,7 @@ public Arbitrary storageClass() { return Arbitraries.strings().all().ofMinLength(1).ofLength(1024); } + @Provide("objectChecksums") public Arbitrary objectChecksumsArbitrary() { return Combinators.combine( Arbitraries.integers().greaterOrEqual(1), From ffa9ed468aa5d974df6f41e34d46e5dfe2537594 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 8 Jun 2022 12:21:35 -0700 Subject: [PATCH 2/4] Update google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java Co-authored-by: BenWhitehead --- .../com/google/cloud/storage/Crc32cUtilityPropertyTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java index 83a9bbe7c..107165ae4 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java @@ -25,7 +25,7 @@ import net.jqwik.api.Provide; public class Crc32cUtilityPropertyTest { - @Example + @Property public void testCrc32cCombinePropertyTest( @ForAll("randomData") String firstObject, @ForAll("randomData") String secondObject) { int firstPartHash = Hashing.crc32c().hashBytes(firstObject.getBytes()).asInt(); From 4696cbeeab94859bbcd6534e6bdefbff35329d61 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 9 Jun 2022 15:19:20 -0700 Subject: [PATCH 3/4] address feedback --- .../storage/Crc32cUtilityPropertyTest.java | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java index 107165ae4..fe70c95a6 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java @@ -22,24 +22,17 @@ import net.jqwik.api.Arbitrary; import net.jqwik.api.Example; import net.jqwik.api.ForAll; +import net.jqwik.api.Property; import net.jqwik.api.Provide; public class Crc32cUtilityPropertyTest { @Property public void testCrc32cCombinePropertyTest( - @ForAll("randomData") String firstObject, @ForAll("randomData") String secondObject) { - int firstPartHash = Hashing.crc32c().hashBytes(firstObject.getBytes()).asInt(); - int secondPartHash = Hashing.crc32c().hashBytes(secondObject.getBytes()).asInt(); - String mergedParts = firstObject + secondObject; - int combined = Hashing.crc32c().hashBytes(mergedParts.getBytes()).asInt(); - assertThat(combined) - .isEqualTo( - Crc32cUtility.crc32cCombineGoogle( - firstPartHash, secondPartHash, secondObject.getBytes().length)); - } - - @Provide("randomData") - Arbitrary stringArrays() { - return Arbitraries.strings().ofMinLength(0).ofMaxLength(1024 * 1024); + @ForAll byte[] firstObject, @ForAll byte[] secondObject) { + int firstPartHash = Hashing.crc32c().hashBytes(firstObject).asInt(); + int secondPartHash = Hashing.crc32c().hashBytes(secondObject).asInt(); + int expected = Hashing.crc32c().newHasher().putBytes(firstObject).putBytes(secondObject).hash().asInt(); + int actual = Crc32cUtility.crc32cCombineGoogle(firstPartHash, secondPartHash, secondObject.length); + assertThat(actual).isEqualTo(expected); } } From 977f0b37677c74cb716ce72740d2bcd9c6c13e94 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Thu, 9 Jun 2022 15:20:58 -0700 Subject: [PATCH 4/4] address feedback --- .../cloud/storage/Crc32cUtilityPropertyTest.java | 10 ++++------ .../google/cloud/storage/jqwik/StorageArbitraries.java | 2 -- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java index fe70c95a6..c5c862306 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/Crc32cUtilityPropertyTest.java @@ -18,12 +18,8 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.hash.Hashing; -import net.jqwik.api.Arbitraries; -import net.jqwik.api.Arbitrary; -import net.jqwik.api.Example; import net.jqwik.api.ForAll; import net.jqwik.api.Property; -import net.jqwik.api.Provide; public class Crc32cUtilityPropertyTest { @Property @@ -31,8 +27,10 @@ public void testCrc32cCombinePropertyTest( @ForAll byte[] firstObject, @ForAll byte[] secondObject) { int firstPartHash = Hashing.crc32c().hashBytes(firstObject).asInt(); int secondPartHash = Hashing.crc32c().hashBytes(secondObject).asInt(); - int expected = Hashing.crc32c().newHasher().putBytes(firstObject).putBytes(secondObject).hash().asInt(); - int actual = Crc32cUtility.crc32cCombineGoogle(firstPartHash, secondPartHash, secondObject.length); + int expected = + Hashing.crc32c().newHasher().putBytes(firstObject).putBytes(secondObject).hash().asInt(); + int actual = + Crc32cUtility.crc32cCombineGoogle(firstPartHash, secondPartHash, secondObject.length); assertThat(actual).isEqualTo(expected); } } diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/jqwik/StorageArbitraries.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/jqwik/StorageArbitraries.java index 68e9a3acb..842cec59a 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/jqwik/StorageArbitraries.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/jqwik/StorageArbitraries.java @@ -34,7 +34,6 @@ import net.jqwik.api.Arbitraries; import net.jqwik.api.Arbitrary; import net.jqwik.api.Combinators; -import net.jqwik.api.Provide; import net.jqwik.api.Tuple; import net.jqwik.api.arbitraries.ListArbitrary; import net.jqwik.api.providers.TypeUsage; @@ -305,7 +304,6 @@ public Arbitrary storageClass() { return Arbitraries.strings().all().ofMinLength(1).ofLength(1024); } - @Provide("objectChecksums") public Arbitrary objectChecksumsArbitrary() { return Combinators.combine( Arbitraries.integers().greaterOrEqual(1),