github.com/npaton/distribution@v2.3.1-rc.0+incompatible/digest/verifiers_test.go (about) 1 package digest 2 3 import ( 4 "bytes" 5 "crypto/rand" 6 "io" 7 "testing" 8 ) 9 10 func TestDigestVerifier(t *testing.T) { 11 p := make([]byte, 1<<20) 12 rand.Read(p) 13 digest := FromBytes(p) 14 15 verifier, err := NewDigestVerifier(digest) 16 if err != nil { 17 t.Fatalf("unexpected error getting digest verifier: %s", err) 18 } 19 20 io.Copy(verifier, bytes.NewReader(p)) 21 22 if !verifier.Verified() { 23 t.Fatalf("bytes not verified") 24 } 25 } 26 27 // TestVerifierUnsupportedDigest ensures that unsupported digest validation is 28 // flowing through verifier creation. 29 func TestVerifierUnsupportedDigest(t *testing.T) { 30 unsupported := Digest("bean:0123456789abcdef") 31 32 _, err := NewDigestVerifier(unsupported) 33 if err == nil { 34 t.Fatalf("expected error when creating verifier") 35 } 36 37 if err != ErrDigestUnsupported { 38 t.Fatalf("incorrect error for unsupported digest: %v", err) 39 } 40 } 41 42 // TODO(stevvooe): Add benchmarks to measure bytes/second throughput for 43 // DigestVerifier. 44 // 45 // The relevant benchmark for comparison can be run with the following 46 // commands: 47 // 48 // go test -bench . crypto/sha1 49 //