Centralize digest validation in public bundle API

This commit is contained in:
2026-06-02 18:52:33 +00:00
parent d5e3aa7a44
commit eba4d6dd56
4 changed files with 64 additions and 15 deletions

58
pkg/bundle/digest_test.go Normal file
View 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)
}
})
}
}