Centralize digest validation in public bundle API
This commit is contained in:
58
pkg/bundle/digest_test.go
Normal file
58
pkg/bundle/digest_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package bundle
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidateDigest(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid",
|
||||
value: "sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
},
|
||||
{
|
||||
name: "uppercase hex",
|
||||
value: "sha256:0123456789ABCDEF0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "missing prefix",
|
||||
value: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "wrong algorithm",
|
||||
value: "sha512:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "short hex",
|
||||
value: "sha256:0123456789abcdef",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "long hex",
|
||||
value: "sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "non hex",
|
||||
value: "sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdeg",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := ValidateDigest(test.value)
|
||||
if test.wantErr && err == nil {
|
||||
t.Fatal("ValidateDigest() error = nil, want error")
|
||||
}
|
||||
if !test.wantErr && err != nil {
|
||||
t.Fatalf("ValidateDigest() error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user