2014-11-19 22:23:01 +01:00
|
|
|
package digest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
2016-12-16 01:30:53 +01:00
|
|
|
"reflect"
|
2014-11-19 22:23:01 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDigestVerifier(t *testing.T) {
|
|
|
|
p := make([]byte, 1<<20)
|
|
|
|
rand.Read(p)
|
2015-12-14 23:30:51 +01:00
|
|
|
digest := FromBytes(p)
|
2014-11-19 22:23:01 +01:00
|
|
|
|
2016-12-16 01:30:53 +01:00
|
|
|
verifier := digest.Verifier()
|
2015-03-10 22:40:58 +01:00
|
|
|
|
2014-11-19 22:23:01 +01:00
|
|
|
io.Copy(verifier, bytes.NewReader(p))
|
|
|
|
|
|
|
|
if !verifier.Verified() {
|
|
|
|
t.Fatalf("bytes not verified")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 02:26:09 +01:00
|
|
|
// TestVerifierUnsupportedDigest ensures that unsupported digest validation is
|
|
|
|
// flowing through verifier creation.
|
|
|
|
func TestVerifierUnsupportedDigest(t *testing.T) {
|
2016-12-16 01:30:53 +01:00
|
|
|
for _, testcase := range []struct {
|
|
|
|
Name string
|
|
|
|
Digest Digest
|
|
|
|
Expected interface{} // expected panic target
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "Empty",
|
|
|
|
Digest: "",
|
|
|
|
Expected: "no ':' separator in digest \"\"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "EmptyAlg",
|
|
|
|
Digest: ":",
|
|
|
|
Expected: "empty digest algorithm, validate before calling Algorithm.Hash()",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Unsupported",
|
|
|
|
Digest: Digest("bean:0123456789abcdef"),
|
|
|
|
Expected: "bean not available (make sure it is imported)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Garbage",
|
|
|
|
Digest: Digest("sha256-garbage:pure"),
|
|
|
|
Expected: "sha256-garbage not available (make sure it is imported)",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testcase.Name, func(t *testing.T) {
|
|
|
|
expected := testcase.Expected
|
|
|
|
defer func() {
|
|
|
|
recovered := recover()
|
|
|
|
if !reflect.DeepEqual(recovered, expected) {
|
|
|
|
t.Fatalf("unexpected recover: %v != %v", recovered, expected)
|
|
|
|
}
|
|
|
|
}()
|
2015-03-19 02:26:09 +01:00
|
|
|
|
2016-12-16 01:30:53 +01:00
|
|
|
_ = testcase.Digest.Verifier()
|
|
|
|
})
|
2015-03-19 02:26:09 +01:00
|
|
|
}
|
|
|
|
}
|