aboutsummaryrefslogtreecommitdiff
path: root/src/internal
diff options
context:
space:
mode:
authorFilippo Valsorda <filippo@golang.org>2025-05-21 23:55:43 +0200
committerGopher Robot <gobot@golang.org>2025-05-21 16:39:55 -0700
commitedcde86990abd9d7336eee5115b63d8c0863a5dd (patch)
treeed93e44da48842556fa23ce7e7025bfc29cb4d64 /src/internal
parentde457fc4ea50cc3ac9dd967161b8bc31b79a26dd (diff)
downloadgo-edcde86990abd9d7336eee5115b63d8c0863a5dd.tar.xz
crypto,hash: add and implement hash.Cloner
Fixes #69521 Co-authored-by: qiulaidongfeng <2645477756@qq.com> Change-Id: I6a6a465652f5ab7e6c9054e826e17df2b8b34e41 Reviewed-on: https://go-review.googlesource.com/c/go/+/675197 Reviewed-by: Roland Shoemaker <roland@golang.org> Auto-Submit: Filippo Valsorda <filippo@golang.org> Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/internal')
-rw-r--r--src/internal/testhash/hash.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/internal/testhash/hash.go b/src/internal/testhash/hash.go
index d863408f55..3413d5c20d 100644
--- a/src/internal/testhash/hash.go
+++ b/src/internal/testhash/hash.go
@@ -18,7 +18,49 @@ type MakeHash func() hash.Hash
// TestHash performs a set of tests on hash.Hash implementations, checking the
// documented requirements of Write, Sum, Reset, Size, and BlockSize.
func TestHash(t *testing.T, mh MakeHash) {
+ TestHashWithoutClone(t, mh)
+ // Test whether the results after cloning are consistent.
+ t.Run("Clone", func(t *testing.T) {
+ h, ok := mh().(hash.Cloner)
+ if !ok {
+ t.Fatalf("%T does not implement hash.Cloner", mh)
+ }
+ h3, err := h.Clone()
+ if err != nil {
+ t.Fatalf("Clone failed: %v", err)
+ }
+ prefix := []byte("tmp")
+ writeToHash(t, h, prefix)
+ h2, err := h.Clone()
+ if err != nil {
+ t.Fatalf("Clone failed: %v", err)
+ }
+ prefixSum := h.Sum(nil)
+ if !bytes.Equal(prefixSum, h2.Sum(nil)) {
+ t.Fatalf("%T Clone results are inconsistent", h)
+ }
+ suffix := []byte("tmp2")
+ writeToHash(t, h, suffix)
+ writeToHash(t, h3, append(prefix, suffix...))
+ compositeSum := h3.Sum(nil)
+ if !bytes.Equal(h.Sum(nil), compositeSum) {
+ t.Fatalf("%T Clone results are inconsistent", h)
+ }
+ if !bytes.Equal(h2.Sum(nil), prefixSum) {
+ t.Fatalf("%T Clone results are inconsistent", h)
+ }
+ writeToHash(t, h2, suffix)
+ if !bytes.Equal(h.Sum(nil), compositeSum) {
+ t.Fatalf("%T Clone results are inconsistent", h)
+ }
+ if !bytes.Equal(h2.Sum(nil), compositeSum) {
+ t.Fatalf("%T Clone results are inconsistent", h)
+ }
+ })
+}
+
+func TestHashWithoutClone(t *testing.T, mh MakeHash) {
// Test that Sum returns an appended digest matching output of Size
t.Run("SumAppend", func(t *testing.T) {
h := mh()