| 1 | package equipotest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 10 | ) |
| 11 | |
| 12 | func TestDirectContainmentReportBinding(t *testing.T) { |
| 13 | 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"} { |
| 14 | t.Run(change, func(t *testing.T) { |
| 15 | f := runnerFixtureNew(t, true, true) |
| 16 | // Launch evidence uses migration 0015's tables, without a direct-launch table. |
| 17 | if scalar(t, f.d, "SELECT count(*) FROM sqlite_master WHERE name='runner_namespace_launches'") != 0 { |
| 18 | t.Fatal("unexpected launch table") |
| 19 | } |
| 20 | key := runnerproto.EventHash([]any{f.claim.Scope, f.claim.Handle.Attempt, f.claim.Handle.Fence, f.claim.Handle.Runner}) |
| 21 | 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} |
| 22 | switch change { |
| 23 | case "launch-no-manifest": |
| 24 | n.ManifestHash = "" |
| 25 | case "launch-no-namespace": |
| 26 | n.NamespaceID = "" |
| 27 | case "launch-no-policy": |
| 28 | n.PolicyHash = "" |
| 29 | case "launch-wrong-owner": |
| 30 | n.OwnerKey = "other" |
| 31 | } |
| 32 | e := fixtureEvent(f, "launch", 0) |
| 33 | e.Kind, e.Sequence, e.AgentID, e.SessionID = "namespace.launch", nil, n.AttemptKey, n.ID |
| 34 | e.Payload, _ = json.Marshal(n) |
| 35 | if strings.HasPrefix(change, "launch-") { |
| 36 | if f.s.IngestRunnerEvent(connectionContext, f.principal, e) == nil { |
| 37 | t.Fatal("invalid authenticated launch accepted") |
| 38 | } |
| 39 | return |
| 40 | } |
| 41 | if change == "inventory-only" { |
| 42 | e.Kind = "namespace.owner" |
| 43 | e.AgentID = key |
| 44 | e.SessionID = key |
| 45 | e.Payload = []byte(`{}`) |
| 46 | } |
| 47 | if change != "no-launch" && change != "unstarted" { |
| 48 | executionMust(t, f.s.IngestRunnerEvent(connectionContext, f.principal, e)) |
| 49 | } |
| 50 | 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()}}} |
| 51 | switch change { |
| 52 | case "lost-launch-ack", "unstarted-mismatch": |
| 53 | p.Unstarted = p.Namespaces |
| 54 | p.Namespaces = nil |
| 55 | if change == "unstarted-mismatch" { |
| 56 | p.Unstarted[0].ManifestHash = strings.Repeat("e", 64) |
| 57 | } |
| 58 | case "unstarted": |
| 59 | p.Namespaces = nil |
| 60 | case "missing-namespace": |
| 61 | p.Namespaces = nil |
| 62 | case "wrong-attempt": |
| 63 | p.Namespaces[0].AttemptKey += "other" |
| 64 | case "wrong-fence": |
| 65 | p.Handle.Fence++ |
| 66 | case "wrong-manifest": |
| 67 | p.Namespaces[0].ManifestHash = strings.Repeat("d", 64) |
| 68 | case "wrong-init": |
| 69 | p.Namespaces[0].StartTicks++ |
| 70 | case "wrong-boot": |
| 71 | p.Namespaces[0].BootID = "other" |
| 72 | case "wrong-namespace": |
| 73 | p.Namespaces[0].NamespaceID = "pid:[43]" |
| 74 | case "empty-flag": |
| 75 | p.Empty = true |
| 76 | p.Namespaces = nil |
| 77 | case "inventory-only": |
| 78 | p.Version = 0 |
| 79 | p.Empty = true |
| 80 | p.Namespaces = nil |
| 81 | p.Owner = runnerproto.ProcessOwner{LocalOnly: true, Attempt: f.claim.Handle.Attempt, Fence: f.claim.Handle.Fence, Marker: key} |
| 82 | p.Inventory = runnerproto.ProcessInventory{CoverageKnown: true, BootID: "boot", CheckedAt: time.Now().UTC()} |
| 83 | } |
| 84 | q := f.request() |
| 85 | q.Proof, _ = json.Marshal(p) |
| 86 | _, err := f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", q) |
| 87 | if (change == "valid" || change == "unstarted" || change == "lost-launch-ack") && err != nil { |
| 88 | t.Fatalf("bound proof refused: %v", err) |
| 89 | } |
| 90 | if change != "valid" && change != "unstarted" && change != "lost-launch-ack" && err == nil { |
| 91 | t.Fatal("unbound proof released capacity") |
| 92 | } |
| 93 | }) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestDirectContainmentLaunchReplayAfterStop(t *testing.T) { |
| 98 | f := runnerFixtureNew(t, true, true) |
| 99 | key := runnerproto.EventHash([]any{f.claim.Scope, f.claim.Handle.Attempt, f.claim.Handle.Fence, f.claim.Handle.Runner}) |
| 100 | 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} |
| 101 | e := fixtureEvent(f, "launch", 0) |
| 102 | e.Kind = "namespace.launch" |
| 103 | e.Sequence = nil |
| 104 | e.AgentID = n.AttemptKey |
| 105 | e.SessionID = n.ID |
| 106 | e.Payload, _ = json.Marshal(n) |
| 107 | executionMust(t, f.s.IngestRunnerEvent(connectionContext, f.principal, e)) |
| 108 | _, err := f.x.Mutate(connectionContext, f.principal, "stop", f.request()) |
| 109 | executionMust(t, err) |
| 110 | executionMust(t, f.s.IngestRunnerEvent(connectionContext, f.principal, e)) |
| 111 | n.ID = strings.Repeat("d", 32) |
| 112 | e.ID = "new-launch" |
| 113 | e.SessionID = n.ID |
| 114 | e.Payload, _ = json.Marshal(n) |
| 115 | if err = f.s.IngestRunnerEvent(connectionContext, f.principal, e); err == nil { |
| 116 | t.Fatal("new launch admitted after stop") |
| 117 | } |
| 118 | } |
| 119 | |