59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|