2014-11-19 21:23:01 +00:00
|
|
|
package digest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDigestVerifier(t *testing.T) {
|
|
|
|
p := make([]byte, 1<<20)
|
|
|
|
rand.Read(p)
|
2015-12-14 22:30:51 +00:00
|
|
|
digest := FromBytes(p)
|
2014-11-19 21:23:01 +00:00
|
|
|
|
2015-03-10 21:40:58 +00:00
|
|
|
verifier, err := NewDigestVerifier(digest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error getting digest verifier: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-11-19 21:23:01 +00:00
|
|
|
io.Copy(verifier, bytes.NewReader(p))
|
|
|
|
|
|
|
|
if !verifier.Verified() {
|
|
|
|
t.Fatalf("bytes not verified")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 01:26:09 +00:00
|
|
|
// TestVerifierUnsupportedDigest ensures that unsupported digest validation is
|
|
|
|
// flowing through verifier creation.
|
|
|
|
func TestVerifierUnsupportedDigest(t *testing.T) {
|
|
|
|
unsupported := Digest("bean:0123456789abcdef")
|
|
|
|
|
|
|
|
_, err := NewDigestVerifier(unsupported)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error when creating verifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != ErrDigestUnsupported {
|
2015-03-19 02:02:32 +00:00
|
|
|
t.Fatalf("incorrect error for unsupported digest: %v", err)
|
2015-03-19 01:26:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-19 21:23:01 +00:00
|
|
|
// TODO(stevvooe): Add benchmarks to measure bytes/second throughput for
|
2015-12-16 01:18:13 +00:00
|
|
|
// DigestVerifier.
|
2014-11-19 21:23:01 +00:00
|
|
|
//
|
2015-12-16 01:18:13 +00:00
|
|
|
// The relevant benchmark for comparison can be run with the following
|
2014-11-19 21:23:01 +00:00
|
|
|
// commands:
|
|
|
|
//
|
|
|
|
// go test -bench . crypto/sha1
|
|
|
|
//
|