2014-11-19 21:23:01 +00:00
|
|
|
package digest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2015-01-06 00:53:13 +00:00
|
|
|
"github.com/docker/distribution/testutil"
|
2014-11-19 21:23:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDigestVerifier(t *testing.T) {
|
|
|
|
p := make([]byte, 1<<20)
|
|
|
|
rand.Read(p)
|
2014-11-19 22:59:05 +00:00
|
|
|
digest, err := FromBytes(p)
|
2014-11-19 21:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error digesting bytes: %#v", err)
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
tf, tarSum, err := testutil.CreateRandomTarFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating tarfile: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-14 05:35:42 +00:00
|
|
|
digest, err = FromTarArchive(tf)
|
2014-11-19 21:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error digesting tarsum: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if digest.String() != tarSum {
|
|
|
|
t.Fatalf("unexpected digest: %q != %q", digest.String(), tarSum)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedSize, _ := tf.Seek(0, os.SEEK_END) // Get tar file size
|
|
|
|
tf.Seek(0, os.SEEK_SET) // seek back
|
|
|
|
|
|
|
|
// This is the most relevant example for the registry application. It's
|
|
|
|
// effectively a read through pipeline, where the final sink is the digest
|
|
|
|
// verifier.
|
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 22:59:05 +00:00
|
|
|
lengthVerifier := NewLengthVerifier(expectedSize)
|
2014-11-19 21:23:01 +00:00
|
|
|
rd := io.TeeReader(tf, lengthVerifier)
|
|
|
|
io.Copy(verifier, rd)
|
|
|
|
|
|
|
|
if !lengthVerifier.Verified() {
|
|
|
|
t.Fatalf("verifier detected incorrect length")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !verifier.Verified() {
|
|
|
|
t.Fatalf("bytes not verified")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(stevvooe): Add benchmarks to measure bytes/second throughput for
|
|
|
|
// DigestVerifier. We should be tarsum/gzip limited for common cases but we
|
|
|
|
// want to verify this.
|
|
|
|
//
|
|
|
|
// The relevant benchmarks for comparison can be run with the following
|
|
|
|
// commands:
|
|
|
|
//
|
|
|
|
// go test -bench . crypto/sha1
|
|
|
|
// go test -bench . github.com/docker/docker/pkg/tarsum
|
|
|
|
//
|