package runs import ( "strings" "testing" ) func TestValidKey(t *testing.T) { for _, k := range []string{ "a", "Z", "0", ".", "_", ":", "-", "fleet:import:acct-42:2026-10-24T12.00", strings.Repeat("k", 128), "AZaz09._:-", } { if !ValidKey(k) { t.Errorf("ValidKey(%q) = false, want true", k) } } for _, k := range []string{ "", strings.Repeat("k", 129), "a/b", "a b", "a\x00b", "a\nb", "café", "a,b", "a+b", } { if ValidKey(k) { t.Errorf("ValidKey(%q) = true, want false", k) } } } func TestScopedKeySeparatesCallers(t *testing.T) { svc := ScopedKey("svc:cred_1", "K") if len(svc) != 64 || strings.Trim(svc, "0123456789abcdef") != "" { t.Fatalf("ScopedKey = %q, want 64 lowercase hex digits", svc) } if ScopedKey("svc:cred_1", "K") != svc { t.Fatal("ScopedKey is not deterministic") } for _, other := range []string{ ScopedKey("svc:cred_2", "K"), ScopedKey("user:paul@keymail.dev", "K"), ScopedKey("svc:cred_1", "K2"), // The separator keeps the scope and the key apart. ScopedKey("svc:cred_", "1K"), ScopedKey("svc:cred_1K", ""), } { if other == svc { t.Errorf("two different callers or keys share the scoped key %s", svc) } } } func TestMetaDigestCoversEveryField(t *testing.T) { hash := strings.Repeat("a", 64) base := func() Meta { return Meta{ Argv: []string{"import", "x.csv"}, TimeoutS: 60, OnBehalfOf: "paul@example.com", Files: []MetaFile{{Name: "x.csv", Size: 5, SHA256: hash}}, } } want := MetaDigest(base()) if MetaDigest(base()) != want { t.Fatal("MetaDigest is not deterministic") } for name, change := range map[string]func(*Meta){ "argv": func(m *Meta) { m.Argv[1] = "y.csv" }, "argv split": func(m *Meta) { m.Argv = []string{"importx.csv"} }, "argv joined": func(m *Meta) { m.Argv = []string{"import", "x", ".csv"} }, "timeout": func(m *Meta) { m.TimeoutS = 61 }, "on_behalf_of": func(m *Meta) { m.OnBehalfOf = "bob@example.com" }, "no requester": func(m *Meta) { m.OnBehalfOf = "" }, "file name": func(m *Meta) { m.Files[0].Name = "y.csv" }, "file size": func(m *Meta) { m.Files[0].Size = 6 }, "file sha256": func(m *Meta) { m.Files[0].SHA256 = strings.Repeat("b", 64) }, "no files": func(m *Meta) { m.Files = nil }, "a second file": func(m *Meta) { m.Files = append(m.Files, MetaFile{Name: "y.csv", Size: 1, SHA256: hash}) }, } { m := base() change(&m) if MetaDigest(m) == want { t.Errorf("changing the %s left the digest unchanged", name) } } } func TestValidFileHash(t *testing.T) { lower := strings.Repeat("0123456789abcdef", 4) for _, h := range []string{lower, strings.ToUpper(lower), "ABCDEF" + lower[6:]} { if !ValidFileHash(h) { t.Errorf("ValidFileHash(%q) = false", h) } } for _, h := range []string{"", lower[:63], lower + "0", "g" + lower[1:], " " + lower[1:]} { if ValidFileHash(h) { t.Errorf("ValidFileHash(%q) = true", h) } } if NormalFileHash(strings.ToUpper(lower)) != lower { t.Error("NormalFileHash does not lowercase") } m := Meta{Argv: []string{"a"}, TimeoutS: 1, Files: []MetaFile{{Name: "x", Size: 1, SHA256: lower}}} u := m u.Files = []MetaFile{{Name: "x", Size: 1, SHA256: strings.ToUpper(lower)}} if MetaDigest(m) != MetaDigest(u) { t.Error("a declared sha256's case changes the digest") } } func TestCallerOfSeparatesCallers(t *testing.T) { if CallerOf("svc:a") == CallerOf("user:a") || CallerOf("svc:a") != CallerOf("svc:a") || len(CallerOf("x")) != 64 { t.Fatal("CallerOf does not name callers apart") } if CallerOf("svc:a") == ScopedKey("svc:a", "") { t.Fatal("CallerOf collides with a scoped key") } }