package equipotest import ( "encoding/json" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/runnerproto" ) func TestDirectContainmentReportBinding(t *testing.T) { for _, change := range []string{"valid", "lost-launch-ack", "unstarted-mismatch", "unstarted", "no-launch", "missing-namespace", "wrong-attempt", "wrong-fence", "wrong-manifest", "wrong-init", "wrong-boot", "wrong-namespace", "empty-flag", "inventory-only", "launch-no-manifest", "launch-no-namespace", "launch-no-policy", "launch-wrong-owner"} { t.Run(change, func(t *testing.T) { f := runnerFixtureNew(t, true, true) // Launch evidence uses migration 0015's tables, without a direct-launch table. if scalar(t, f.d, "SELECT count(*) FROM sqlite_master WHERE name='runner_namespace_launches'") != 0 { t.Fatal("unexpected launch table") } key := runnerproto.EventHash([]any{f.claim.Scope, f.claim.Handle.Attempt, f.claim.Handle.Fence, f.claim.Handle.Runner}) n := runnerproto.NamespaceLaunch{ID: strings.Repeat("a", 32), AttemptKey: key + "/model", OwnerKey: key, ManifestHash: strings.Repeat("b", 64), PolicyHash: strings.Repeat("c", 64), BootID: "boot", NamespaceID: "pid:[42]", HostPID: 42, StartTicks: 7} switch change { case "launch-no-manifest": n.ManifestHash = "" case "launch-no-namespace": n.NamespaceID = "" case "launch-no-policy": n.PolicyHash = "" case "launch-wrong-owner": n.OwnerKey = "other" } e := fixtureEvent(f, "launch", 0) e.Kind, e.Sequence, e.AgentID, e.SessionID = "namespace.launch", nil, n.AttemptKey, n.ID e.Payload, _ = json.Marshal(n) if strings.HasPrefix(change, "launch-") { if f.s.IngestRunnerEvent(connectionContext, f.principal, e) == nil { t.Fatal("invalid authenticated launch accepted") } return } if change == "inventory-only" { e.Kind = "namespace.owner" e.AgentID = key e.SessionID = key e.Payload = []byte(`{}`) } if change != "no-launch" && change != "unstarted" { executionMust(t, f.s.IngestRunnerEvent(connectionContext, f.principal, e)) } p := runnerproto.ContainmentProof{Version: 2, Scope: f.claim.Scope, Handle: f.claim.Handle, CheckedAt: time.Now().UTC(), Namespaces: []runnerproto.NamespaceExit{{ID: n.ID, AttemptKey: n.AttemptKey, ManifestHash: n.ManifestHash, BootID: n.BootID, NamespaceID: n.NamespaceID, HostPID: n.HostPID, StartTicks: n.StartTicks, ObservedAt: time.Now().UTC()}}} switch change { case "lost-launch-ack", "unstarted-mismatch": p.Unstarted = p.Namespaces p.Namespaces = nil if change == "unstarted-mismatch" { p.Unstarted[0].ManifestHash = strings.Repeat("e", 64) } case "unstarted": p.Namespaces = nil case "missing-namespace": p.Namespaces = nil case "wrong-attempt": p.Namespaces[0].AttemptKey += "other" case "wrong-fence": p.Handle.Fence++ case "wrong-manifest": p.Namespaces[0].ManifestHash = strings.Repeat("d", 64) case "wrong-init": p.Namespaces[0].StartTicks++ case "wrong-boot": p.Namespaces[0].BootID = "other" case "wrong-namespace": p.Namespaces[0].NamespaceID = "pid:[43]" case "empty-flag": p.Empty = true p.Namespaces = nil case "inventory-only": p.Version = 0 p.Empty = true p.Namespaces = nil p.Owner = runnerproto.ProcessOwner{LocalOnly: true, Attempt: f.claim.Handle.Attempt, Fence: f.claim.Handle.Fence, Marker: key} p.Inventory = runnerproto.ProcessInventory{CoverageKnown: true, BootID: "boot", CheckedAt: time.Now().UTC()} } q := f.request() q.Proof, _ = json.Marshal(p) _, err := f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", q) if (change == "valid" || change == "unstarted" || change == "lost-launch-ack") && err != nil { t.Fatalf("bound proof refused: %v", err) } if change != "valid" && change != "unstarted" && change != "lost-launch-ack" && err == nil { t.Fatal("unbound proof released capacity") } }) } } func TestDirectContainmentLaunchReplayAfterStop(t *testing.T) { f := runnerFixtureNew(t, true, true) key := runnerproto.EventHash([]any{f.claim.Scope, f.claim.Handle.Attempt, f.claim.Handle.Fence, f.claim.Handle.Runner}) n := runnerproto.NamespaceLaunch{ID: strings.Repeat("a", 32), AttemptKey: key + "/model", OwnerKey: key, ManifestHash: strings.Repeat("b", 64), PolicyHash: strings.Repeat("c", 64), BootID: "boot", NamespaceID: "pid:[42]", HostPID: 42, StartTicks: 7} e := fixtureEvent(f, "launch", 0) e.Kind = "namespace.launch" e.Sequence = nil e.AgentID = n.AttemptKey e.SessionID = n.ID e.Payload, _ = json.Marshal(n) executionMust(t, f.s.IngestRunnerEvent(connectionContext, f.principal, e)) _, err := f.x.Mutate(connectionContext, f.principal, "stop", f.request()) executionMust(t, err) executionMust(t, f.s.IngestRunnerEvent(connectionContext, f.principal, e)) n.ID = strings.Repeat("d", 32) e.ID = "new-launch" e.SessionID = n.ID e.Payload, _ = json.Marshal(n) if err = f.s.IngestRunnerEvent(connectionContext, f.principal, e); err == nil { t.Fatal("new launch admitted after stop") } }