72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package digest
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNormalizeSHA256_StripPrefix(t *testing.T) {
|
|
h := strings.Repeat("a", 64)
|
|
got, err := NormalizeSHA256("sha256:" + h)
|
|
if err != nil || got != h {
|
|
t.Fatalf("got %q err %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeSHA256_LowerHex(t *testing.T) {
|
|
h := strings.Repeat("A", 64)
|
|
want := strings.Repeat("a", 64)
|
|
got, err := NormalizeSHA256(h)
|
|
if err != nil || got != want {
|
|
t.Fatalf("got %q err %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeSHA256_RejectsShort(t *testing.T) {
|
|
_, err := NormalizeSHA256("abcd")
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeSHA256_MixedCasePrefix(t *testing.T) {
|
|
h := strings.Repeat("b", 64)
|
|
got, err := NormalizeSHA256("SHA256:" + h)
|
|
if err != nil || got != h {
|
|
t.Fatalf("got %q err %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestAssertDigestOnly_RejectsContent(t *testing.T) {
|
|
err := AssertDigestOnly(map[string]any{"sha256": strings.Repeat("a", 64), "content": "x"})
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|
|
|
|
func TestAssertDigestOnly_CaseInsensitive(t *testing.T) {
|
|
err := AssertDigestOnly(map[string]any{"CONTENT": "x"})
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|
|
|
|
func TestAssertDigestOnly_OK(t *testing.T) {
|
|
err := AssertDigestOnly(map[string]any{"sha256": strings.Repeat("a", 64), "scope": "local"})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestForbiddenKeys_ContainsContent(t *testing.T) {
|
|
found := false
|
|
for _, k := range ForbiddenKeys {
|
|
if k == "content" {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("ForbiddenKeys missing content: %v", ForbiddenKeys)
|
|
}
|
|
}
|