aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2017-09-07 12:16:04 -0400
committerRuss Cox <rsc@golang.org>2017-09-22 20:08:08 +0000
commit0bc83d7aaa7165f256210358fdfcf3bccd6d2f8f (patch)
tree430483060e43457ea15ef646880bc3772dc53b3e
parentf2a2dbe422f526cb9973099f5603451b3a8b3835 (diff)
downloadgo-0bc83d7aaa7165f256210358fdfcf3bccd6d2f8f.tar.gz
go-0bc83d7aaa7165f256210358fdfcf3bccd6d2f8f.zip
[dev.boringcrypto.go1.8] crypto/hmac: add test for Write/Sum after Sum
This is documented to work (in hash.Hash's definition) and existing code assumes it works. Add a test. Change-Id: I63546f3b2d66222683a4f268a4eaff835fd836fe Reviewed-on: https://go-review.googlesource.com/63911 Reviewed-by: Adam Langley <agl@golang.org> Reviewed-on: https://go-review.googlesource.com/65482 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
-rw-r--r--src/crypto/hmac/hmac_test.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/crypto/hmac/hmac_test.go b/src/crypto/hmac/hmac_test.go
index 444978001c..cb5ea98d72 100644
--- a/src/crypto/hmac/hmac_test.go
+++ b/src/crypto/hmac/hmac_test.go
@@ -5,6 +5,7 @@
package hmac
import (
+ "bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
@@ -594,6 +595,42 @@ func TestEqual(t *testing.T) {
}
}
+func TestWriteAfterSum(t *testing.T) {
+ h := New(sha1.New, nil)
+ h.Write([]byte("hello"))
+ sumHello := h.Sum(nil)
+
+ h = New(sha1.New, nil)
+ h.Write([]byte("hello world"))
+ sumHelloWorld := h.Sum(nil)
+
+ // Test that Sum has no effect on future Sum or Write operations.
+ // This is a bit unusual as far as usage, but it's allowed
+ // by the definition of Go hash.Hash, and some clients expect it to work.
+ h = New(sha1.New, nil)
+ h.Write([]byte("hello"))
+ if sum := h.Sum(nil); !bytes.Equal(sum, sumHello) {
+ t.Fatalf("1st Sum after hello = %x, want %x", sum, sumHello)
+ }
+ if sum := h.Sum(nil); !bytes.Equal(sum, sumHello) {
+ t.Fatalf("2nd Sum after hello = %x, want %x", sum, sumHello)
+ }
+
+ h.Write([]byte(" world"))
+ if sum := h.Sum(nil); !bytes.Equal(sum, sumHelloWorld) {
+ t.Fatalf("1st Sum after hello world = %x, want %x", sum, sumHelloWorld)
+ }
+ if sum := h.Sum(nil); !bytes.Equal(sum, sumHelloWorld) {
+ t.Fatalf("2nd Sum after hello world = %x, want %x", sum, sumHelloWorld)
+ }
+
+ h.Reset()
+ h.Write([]byte("hello"))
+ if sum := h.Sum(nil); !bytes.Equal(sum, sumHello) {
+ t.Fatalf("Sum after Reset + hello = %x, want %x", sum, sumHello)
+ }
+}
+
func BenchmarkHMACSHA256_1K(b *testing.B) {
key := make([]byte, 32)
buf := make([]byte, 1024)