| 1 | package verdict |
| 2 | |
| 3 | // The commit rule, case by case. The verdict is a function of data, so |
| 4 | // every rule of dcr-439b771 is exercised here without a repository, a key, |
| 5 | // or a clock. What a repository does to produce these inputs is the refs |
| 6 | // transport's, and it is exercised end to end by the integrator-e2e check. |
| 7 | |
| 8 | import ( |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | attestedTree = "aaaa" |
| 16 | movedTree = "bbbb" |
| 17 | closureA = "1111" |
| 18 | closureB = "2222" |
| 19 | ) |
| 20 | |
| 21 | var observed = time.Date(2026, 8, 6, 12, 0, 0, 0, time.UTC) |
| 22 | |
| 23 | func pureCheck(name string) Required { |
| 24 | return Required{Name: name, Classes: []string{"code"}, Runner: "nix", Attribute: name} |
| 25 | } |
| 26 | |
| 27 | func effectfulCheck(name string, window time.Duration) Required { |
| 28 | return Required{Name: name, Classes: []string{"deploy"}, Runner: "command", Validity: window} |
| 29 | } |
| 30 | |
| 31 | func pureEvidence(name, inputs string) Evidence { |
| 32 | return Evidence{Check: name, Kind: Pure, Admissible: Admissible, Result: "passed", |
| 33 | Inputs: inputs, Derivation: "drv-" + inputs, TextDigest: "d" + name} |
| 34 | } |
| 35 | |
| 36 | // closures is what a caller recomputed over the tree that would land: the |
| 37 | // same derivation the attestation names, unless a case says otherwise. |
| 38 | func closures(pairs ...string) map[string]Closure { |
| 39 | out := map[string]Closure{} |
| 40 | for i := 0; i < len(pairs); i += 2 { |
| 41 | out[pairs[i]] = Closure{Inputs: pairs[i+1], Derivation: "drv-" + pairs[i+1]} |
| 42 | } |
| 43 | return out |
| 44 | } |
| 45 | |
| 46 | func effectfulEvidence(name string, at time.Time) Evidence { |
| 47 | return Evidence{Check: name, Kind: Effectful, Admissible: Admissible, Result: "passed", ObservedAt: at, TextDigest: "d" + name} |
| 48 | } |
| 49 | |
| 50 | func change(evidence ...Evidence) Change { |
| 51 | ch := Change{ID: "c1", Target: "refs/heads/main", Attested: attestedTree, Evidence: map[string]Evidence{}} |
| 52 | for _, e := range evidence { |
| 53 | ch.Evidence[e.Check] = e |
| 54 | } |
| 55 | return ch |
| 56 | } |
| 57 | |
| 58 | func TestUnmovedTreeTransfersEveryPureCheck(t *testing.T) { |
| 59 | // Nothing intervened, so the landed tree is the attested tree and |
| 60 | // there is nothing for a closure comparison to compare. |
| 61 | v := Decide( |
| 62 | change(pureEvidence("build", closureA), pureEvidence("lint", closureB)), |
| 63 | Snapshot{ |
| 64 | Apply: Applied{Clean: true, Tree: attestedTree}, |
| 65 | Required: []Required{pureCheck("build"), pureCheck("lint")}, |
| 66 | }) |
| 67 | if v.Outcome != Land { |
| 68 | t.Fatalf("outcome = %s (%s)", v.Outcome, v.Reason) |
| 69 | } |
| 70 | for _, cv := range v.Checks { |
| 71 | if cv.Rule != "unmoved" { |
| 72 | t.Errorf("%s transferred by %q, want unmoved", cv.Name, cv.Rule) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestClosureEqualityTransfersAndInequalityInvalidates(t *testing.T) { |
| 78 | // The base moved. build reads what the intervening landing changed; |
| 79 | // lint does not. Exactly one check is named, and the other is |
| 80 | // reported as having transferred. |
| 81 | v := Decide( |
| 82 | change(pureEvidence("build", closureA), pureEvidence("lint", closureB)), |
| 83 | Snapshot{ |
| 84 | Apply: Applied{Clean: true, Tree: movedTree}, |
| 85 | Required: []Required{pureCheck("build"), pureCheck("lint")}, |
| 86 | Closures: closures("build", "3333", "lint", closureB), |
| 87 | }) |
| 88 | if v.Outcome != Stale || v.StaleReason != "evidence" { |
| 89 | t.Fatalf("outcome = %s/%s (%s)", v.Outcome, v.StaleReason, v.Reason) |
| 90 | } |
| 91 | if len(v.Invalidated) != 1 || v.Invalidated[0] != "build" { |
| 92 | t.Fatalf("invalidated = %v, want exactly [build]", v.Invalidated) |
| 93 | } |
| 94 | for _, cv := range v.Checks { |
| 95 | if cv.Name == "lint" && (!cv.Transferred || cv.Rule != "closure") { |
| 96 | t.Errorf("lint = %+v, want transferred by closure equality", cv) |
| 97 | } |
| 98 | if cv.Name == "build" && !strings.Contains(cv.Reason, "input closure changed") { |
| 99 | t.Errorf("build reason = %q", cv.Reason) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestConflictDemandsEveryCheckAgain(t *testing.T) { |
| 105 | // A conflict is new authorship, so retry is not partial. |
| 106 | v := Decide( |
| 107 | change(pureEvidence("build", closureA), pureEvidence("lint", closureB)), |
| 108 | Snapshot{ |
| 109 | Apply: Applied{Clean: false, Conflict: "CONFLICT (content): src/main.txt"}, |
| 110 | Required: []Required{pureCheck("build"), pureCheck("lint")}, |
| 111 | }) |
| 112 | if v.Outcome != Stale || v.StaleReason != "conflict" { |
| 113 | t.Fatalf("outcome = %s/%s", v.Outcome, v.StaleReason) |
| 114 | } |
| 115 | if got := strings.Join(v.Invalidated, ","); got != "build,lint" { |
| 116 | t.Fatalf("invalidated = %q, want every required check", got) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestUnknownSignerDoesNotTransfer(t *testing.T) { |
| 121 | // Not a forgery: a well-formed statement by a signer the integrator |
| 122 | // does not hold. Deciding what to do about one belongs to the trust |
| 123 | // backstop, so here it simply fails to transfer. |
| 124 | ev := pureEvidence("build", closureA) |
| 125 | ev.Admissible = UnknownSigner |
| 126 | v := Decide(change(ev), Snapshot{ |
| 127 | Apply: Applied{Clean: true, Tree: attestedTree}, |
| 128 | Required: []Required{pureCheck("build")}, |
| 129 | }) |
| 130 | if v.Outcome != Stale { |
| 131 | t.Fatalf("outcome = %s, want stale", v.Outcome) |
| 132 | } |
| 133 | if !strings.Contains(v.Checks[0].Reason, "no signer the integrator holds") { |
| 134 | t.Fatalf("reason = %q", v.Checks[0].Reason) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestTamperedEvidenceIsRejectedNotStale(t *testing.T) { |
| 139 | ev := pureEvidence("build", closureA) |
| 140 | ev.Admissible = Tampered |
| 141 | v := Decide(change(ev), Snapshot{ |
| 142 | Apply: Applied{Clean: true, Tree: attestedTree}, |
| 143 | Required: []Required{pureCheck("build")}, |
| 144 | }) |
| 145 | if v.Outcome != Reject { |
| 146 | t.Fatalf("outcome = %s, want reject", v.Outcome) |
| 147 | } |
| 148 | if len(v.Invalidated) != 0 { |
| 149 | t.Fatalf("a rejection named %v as stale; re-attesting would not fix a forgery", v.Invalidated) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestMissingAndFailedEvidence(t *testing.T) { |
| 154 | failed := pureEvidence("lint", closureB) |
| 155 | failed.Result = "failed" |
| 156 | v := Decide(change(failed), Snapshot{ |
| 157 | Apply: Applied{Clean: true, Tree: attestedTree}, |
| 158 | Required: []Required{pureCheck("build"), pureCheck("lint")}, |
| 159 | }) |
| 160 | if v.Outcome != Stale { |
| 161 | t.Fatalf("outcome = %s", v.Outcome) |
| 162 | } |
| 163 | if got := strings.Join(v.Invalidated, ","); got != "build,lint" { |
| 164 | t.Fatalf("invalidated = %q", got) |
| 165 | } |
| 166 | if !strings.Contains(v.Checks[0].Reason, "no attestation") { |
| 167 | t.Errorf("build reason = %q", v.Checks[0].Reason) |
| 168 | } |
| 169 | if !strings.Contains(v.Checks[1].Reason, "records the check as failed") { |
| 170 | t.Errorf("lint reason = %q", v.Checks[1].Reason) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestEffectfulTransfersWhileScopeUntouchedAndObservationFresh(t *testing.T) { |
| 175 | v := Decide( |
| 176 | change(effectfulEvidence("reachable", observed)), |
| 177 | Snapshot{ |
| 178 | Apply: Applied{Clean: true, Tree: movedTree}, |
| 179 | Required: []Required{effectfulCheck("reachable", time.Hour)}, |
| 180 | Touched: map[string]bool{"prose": true}, |
| 181 | Now: observed.Add(30 * time.Minute), |
| 182 | }) |
| 183 | if v.Outcome != Land { |
| 184 | t.Fatalf("outcome = %s (%s)", v.Outcome, v.Reason) |
| 185 | } |
| 186 | if v.Checks[0].Rule != "untouched" { |
| 187 | t.Fatalf("rule = %q, want untouched", v.Checks[0].Rule) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func TestEffectfulFailsWhenItsClassWasTouched(t *testing.T) { |
| 192 | v := Decide( |
| 193 | change(effectfulEvidence("reachable", observed)), |
| 194 | Snapshot{ |
| 195 | Apply: Applied{Clean: true, Tree: movedTree}, |
| 196 | Required: []Required{effectfulCheck("reachable", time.Hour)}, |
| 197 | Touched: map[string]bool{"deploy": true}, |
| 198 | Now: observed.Add(time.Minute), |
| 199 | }) |
| 200 | if v.Outcome != Stale || !strings.Contains(v.Checks[0].Reason, "touched the deploy class") { |
| 201 | t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | func TestEffectfulExpiresWithItsWindow(t *testing.T) { |
| 206 | v := Decide( |
| 207 | change(effectfulEvidence("reachable", observed)), |
| 208 | Snapshot{ |
| 209 | Apply: Applied{Clean: true, Tree: attestedTree}, |
| 210 | Required: []Required{effectfulCheck("reachable", time.Hour)}, |
| 211 | Now: observed.Add(90 * time.Minute), |
| 212 | }) |
| 213 | if v.Outcome != Stale || !strings.Contains(v.Checks[0].Reason, "the declared window is 1h0m0s") { |
| 214 | t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func TestEffectfulWithNoDeclaredWindowIsReDemanded(t *testing.T) { |
| 219 | // Δ is declared policy. Absent a declaration there is no window, and |
| 220 | // the integrator may not invent one. |
| 221 | v := Decide( |
| 222 | change(effectfulEvidence("reachable", observed)), |
| 223 | Snapshot{ |
| 224 | Apply: Applied{Clean: true, Tree: attestedTree}, |
| 225 | Required: []Required{effectfulCheck("reachable", 0)}, |
| 226 | Now: observed.Add(time.Second), |
| 227 | }) |
| 228 | if v.Outcome != Stale || !strings.Contains(v.Checks[0].Reason, "declares no validity window") { |
| 229 | t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | func TestAnUnmovedTreeDoesNotExemptAnEffectfulCheckFromItsWindow(t *testing.T) { |
| 234 | // A pure claim carries no expiry because time is not among its |
| 235 | // inputs. An effectful one does, whatever happened to the tree. |
| 236 | v := Decide( |
| 237 | change(pureEvidence("build", closureA), effectfulEvidence("reachable", observed)), |
| 238 | Snapshot{ |
| 239 | Apply: Applied{Clean: true, Tree: attestedTree}, |
| 240 | Required: []Required{pureCheck("build"), effectfulCheck("reachable", time.Hour)}, |
| 241 | Now: observed.Add(2 * time.Hour), |
| 242 | }) |
| 243 | if v.Outcome != Stale { |
| 244 | t.Fatalf("outcome = %s", v.Outcome) |
| 245 | } |
| 246 | if got := strings.Join(v.Invalidated, ","); got != "reachable" { |
| 247 | t.Fatalf("invalidated = %q, want only the effectful check", got) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | func TestClosureThatCouldNotBeRecomputedIsNotATransfer(t *testing.T) { |
| 252 | v := Decide( |
| 253 | change(pureEvidence("build", closureA)), |
| 254 | Snapshot{ |
| 255 | Apply: Applied{Clean: true, Tree: movedTree}, |
| 256 | Required: []Required{pureCheck("build")}, |
| 257 | ClosureErrors: map[string]string{"build": "no such check attribute"}, |
| 258 | }) |
| 259 | if v.Outcome != Stale || !strings.Contains(v.Checks[0].Reason, "could not be recomputed") { |
| 260 | t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | func TestTheDerivationIsComparedToo(t *testing.T) { |
| 265 | // The closure held and the derivation moved. A pure attestation asserts |
| 266 | // both, so evidence whose derivation no longer matches is evidence |
| 267 | // about a computation this tree does not present. |
| 268 | moved := closures("build", closureA) |
| 269 | moved["build"] = Closure{Inputs: closureA, Derivation: "some other derivation"} |
| 270 | v := Decide(change(pureEvidence("build", closureA)), Snapshot{ |
| 271 | Apply: Applied{Clean: true, Tree: movedTree}, |
| 272 | Required: []Required{pureCheck("build")}, |
| 273 | Closures: moved, |
| 274 | }) |
| 275 | if v.Outcome != Stale || !strings.Contains(v.Checks[0].Reason, "its derivation changed") { |
| 276 | t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func TestThePolicyDecidesWhichRuleApplies(t *testing.T) { |
| 281 | // An attestation declaring a notarization for a check the policy runs |
| 282 | // with nix would otherwise dodge closure comparison entirely: no |
| 283 | // intervening landing touched its class, so the effectful rule would |
| 284 | // pass it straight through onto a base it was never produced over. |
| 285 | lying := effectfulEvidence("build", observed) |
| 286 | v := Decide(change(lying), Snapshot{ |
| 287 | Apply: Applied{Clean: true, Tree: movedTree}, |
| 288 | Required: []Required{pureCheck("build")}, |
| 289 | Closures: closures("build", "a completely different closure"), |
| 290 | Now: observed.Add(time.Minute), |
| 291 | }) |
| 292 | if v.Outcome != Reject { |
| 293 | t.Fatalf("outcome = %s, want reject", v.Outcome) |
| 294 | } |
| 295 | if !strings.Contains(v.Checks[0].Reason, "requires a pure check and the attestation claims an effectful check") { |
| 296 | t.Fatalf("reason = %q", v.Checks[0].Reason) |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | func TestAPureClaimForAnEffectfulCheckIsAlsoRefused(t *testing.T) { |
| 301 | // The mismatch is refused in both directions: the rule follows the |
| 302 | // policy, and a statement that contradicts it is not judged at all. |
| 303 | v := Decide(change(pureEvidence("reachable", closureA)), Snapshot{ |
| 304 | Apply: Applied{Clean: true, Tree: attestedTree}, |
| 305 | Required: []Required{effectfulCheck("reachable", time.Hour)}, |
| 306 | Now: observed, |
| 307 | }) |
| 308 | if v.Outcome != Reject || !strings.Contains(v.Checks[0].Reason, "requires an effectful check and the attestation claims a pure check") { |
| 309 | t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason) |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | func TestARunnerWithNoTransferRuleIsRefused(t *testing.T) { |
| 314 | v := Decide(change(pureEvidence("build", closureA)), Snapshot{ |
| 315 | Apply: Applied{Clean: true, Tree: attestedTree}, |
| 316 | Required: []Required{{Name: "build", Runner: "something-new"}}, |
| 317 | }) |
| 318 | if v.Outcome != Reject || !strings.Contains(v.Checks[0].Reason, "no transfer rule is defined") { |
| 319 | t.Fatalf("verdict = %s, reason = %q", v.Outcome, v.Checks[0].Reason) |
| 320 | } |
| 321 | } |