package kept import ( "errors" "strings" "testing" ) func TestAKeptValueIsReadOnlyAtItsVersionAndAKeyPartOnlyOfKeyCharacters(t *testing.T) { var v struct{ V int } for data, ok := range map[string]bool{`{"v":1}`: true, `{"v":2}`: false, `{}`: false} { err := Decode([]byte(data), &v) if (err == nil) != ok || (!ok && !errors.Is(err, ErrUnknownVersion)) { t.Errorf("Decode(%s) = %v", data, err) } } if Decode([]byte(`nope`), &v) == nil { t.Error("a value that is no JSON is read") } for name, ok := range map[string]bool{"a-b_c/d=": true, "": false, "a.b": false, "a b": false, "a*": false} { if (CheckName("module", name) == nil) != ok { t.Errorf("CheckName(%q) = %v", name, CheckName("module", name)) } } if !IsHex64(strings.Repeat("a", 64)) || IsHex64(strings.Repeat("A", 64)) || IsHex64("a") { t.Error("IsHex64 reads a wrong form") } if CheckDigest("sha256:"+strings.Repeat("0", 64), "x") != nil || CheckDigest("sha256:0", "x") == nil { t.Error("CheckDigest reads a wrong form") } }