51 lines
1.7 KiB
Go
51 lines
1.7 KiB
Go
package keys
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNamesEscapeIntoKeyPartsAndBack(t *testing.T) {
|
|
for _, c := range []struct{ name, esc string }{
|
|
{"fix/1.2", "fix/1=2e2"}, {"a.b", "a=2eb"}, {"a=b", "a=3db"}, {"a+b", "a=2bb"}, {"plain-name_1/x", "plain-name_1/x"},
|
|
{"fix/ü", "fix/=c3=bc"}, {"x y", "x=20y"},
|
|
} {
|
|
esc := EscapeName(c.name)
|
|
if esc != c.esc || strings.ContainsAny(esc, ".+ ") {
|
|
t.Errorf("escape of %q = %q, want %q", c.name, esc, c.esc)
|
|
}
|
|
if back, err := UnescapeName(esc); err != nil || back != c.name {
|
|
t.Errorf("unescape(%q) = %q, %v; want %q", esc, back, err, c.name)
|
|
}
|
|
}
|
|
for _, bad := range []string{"a.b", "a=", "a=2", "a=zz", "a=2E", "a b"} {
|
|
if got, err := UnescapeName(bad); err == nil {
|
|
t.Errorf("unescape(%q) = %q, accepted", bad, got)
|
|
}
|
|
}
|
|
if Version != 1 {
|
|
t.Errorf("Version = %d: a kept value's readers read the one version", Version)
|
|
}
|
|
}
|
|
|
|
func TestAKeptValueIsReadOnlyAtItsVersionAndAKeyPartOnlyOfKeyCharacters(t *testing.T) {
|
|
for data, ok := range map[string]bool{`{"v":1}`: true, `{"v":2}`: false, `{}`: false} {
|
|
err := CheckVersion([]byte(data))
|
|
if (err == nil) != ok || (!ok && !errors.Is(err, ErrUnknownVersion)) {
|
|
t.Errorf("CheckVersion(%s) = %v", data, err)
|
|
}
|
|
}
|
|
if CheckVersion([]byte(`nope`)) == 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")
|
|
}
|
|
}
|