oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package checks
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "io"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "slices"
12 "strings"
13 "sync"
14 "testing"
15 "time"
16
17 "amadan.net/oficina/equipo/internal/equipo/team"
18 "amadan.net/oficina/equipo/internal/runner/provider"
19)
20
21func must(t *testing.T, err error) {
22 t.Helper()
23 if err != nil {
24 t.Fatal(err)
25 }
26}
27func put(t *testing.T, root, name, content string) {
28 t.Helper()
29 must(t, os.MkdirAll(filepath.Dir(filepath.Join(root, name)), 0700))
30 must(t, os.WriteFile(filepath.Join(root, name), []byte(content), 0600))
31}
32func raw(v any) []byte { b, _ := json.Marshal(v); return b }
33
34const baseID = "1111111111111111111111111111111111111111"
35const fixID = "2222222222222222222222222222222222222222"
36const repoID = "https://example.test/team/repo"
37const bugTest = `package fixture
38import "testing"
39func TestBug(t *testing.T) { if Value()!=2 {t.Fatal("wrong value")} }
40`
41const oldTest = `package fixture
42import "testing"
43func TestOld(t *testing.T) {t.Run("child",func(t *testing.T){})}
44`
45
46type localSandbox struct{}
47
48func (localSandbox) Run(ctx context.Context, c Command, w string, _ provider.ReadPolicy, out, errout io.Writer) (int, error) {
49 cmd := exec.CommandContext(ctx, c.Argv[0], c.Argv[1:]...)
50 cmd.Dir = filepath.Join(w, c.Dir)
51 cmd.Env = testEnv()
52 cmd.Stdout = out
53 cmd.Stderr = errout
54 err := cmd.Run()
55 if x, ok := err.(*exec.ExitError); ok {
56 return x.ExitCode(), nil
57 }
58 return 0, err
59}
60func testEnv() []string {
61 return append(os.Environ(), "CGO_ENABLED=0", "GOWORK=off", "GOFLAGS=", "GOTOOLCHAIN=local", "GOPROXY=off")
62}
63
64type fixture struct {
65 e *Executor
66 d Definition
67 r Reproduction
68 base, candidate string
69 c team.GitCandidate
70 hash string
71}
72
73var fixtureBaseline struct {
74 sync.Once
75 value TestBaseline
76 err error
77}
78
79func newFixture(t *testing.T) fixture {
80 t.Helper()
81 b := t.TempDir()
82 c := t.TempDir()
83 for _, root := range []string{b, c} {
84 put(t, root, "go.mod", "module fixture\n\ngo 1.26\n")
85 put(t, root, "old_test.go", oldTest)
86 }
87 put(t, b, "value.go", "package fixture\nfunc Value() int {return 1}\n")
88 put(t, c, "value.go", "package fixture\nfunc Value() int {return 2}\n")
89 command := Command{Argv: []string{"go", "test", "-json", "-count=1", "./..."}, Dir: ".", Parser: "go-test-json", TimeoutSeconds: 30}
90 d, err := ParseDefinition(context.Background(), raw(Definition{Version: 1, Repository: repoID, Base: baseID, Commands: []Command{command}, Reproduction: command}), repoID, baseID)
91 must(t, err)
92 d, err = FreezeMaterial(context.Background(), d, b)
93 must(t, err)
94 r := Reproduction{ExpectedSymptom: "wrong value", Files: []File{{Path: "bug_test.go", Bytes: []byte(bugTest)}}, TestNames: []TestIdentity{{Package: "fixture", Name: "TestBug"}}}
95 cand := team.GitCandidate{Repository: repoID, BaseCommit: baseID, Commit: fixID, Tree: strings.Repeat("3", 40)}
96 cfg := Config{ResolveCandidate: func(_ context.Context, hash string) (team.GitCandidate, error) {
97 if hash != CandidateHash(cand) {
98 return team.GitCandidate{}, provider.ErrIdentity
99 }
100 return cand, nil
101 }, Reproduction: func(context.Context, string) (Reproduction, error) { return r, nil }, ScratchRoot: t.TempDir(), Scope: team.Scope{Account: "team", Job: 1}, Handle: team.Handle{Attempt: 1, Fence: 1, Runner: "runner"}, Deadline: time.Now().Add(time.Minute), Policy: provider.ReadPolicy{ConfigHash: "policy", DeniedPaths: []string{t.TempDir()}}, Export: func(ctx context.Context, commit, dest string) error {
102 src := c
103 if commit == baseID {
104 src = b
105 }
106 return copyTree(src, dest)
107 }, Isolation: func(context.Context) (team.IsolationSnapshot, error) {
108 return team.IsolationSnapshot{Status: "confined", RunnerTainted: true, AcknowledgementIDs: []string{"ack"}, ObservedTuples: []team.IsolationTuple{{Provider: "codex"}}}, nil
109 }}
110 e, err := newExecutor(cfg, localSandbox{})
111 must(t, err)
112 e.SetAttestationStore(durableFixture{})
113 fixtureBaseline.Do(func() {
114 fixtureBaseline.value, fixtureBaseline.err = e.CaptureBaseline(context.Background(), d)
115 })
116 must(t, fixtureBaseline.err)
117 d.Baseline = fixtureBaseline.value
118 d.Baseline.Names = slices.Clone(fixtureBaseline.value.Names)
119 e.cfg.DefinitionHash = Digest(raw(d))
120 e.cfg.ReproductionHash = Digest(raw(r))
121 return fixture{e, d, r, b, c, cand, CandidateHash(cand)}
122}
123func TestFrozenReproduction(t *testing.T) {
124 f := newFixture(t)
125 base, err := f.e.Reproduce(context.Background(), f.d, f.r, baseID, "")
126 must(t, err)
127 if base.Outcome != team.CheckFail || base.SubjectHash != baseID || base.TestsRun < 1 || base.TestsFailed < 1 || !base.CollisionChecked {
128 t.Fatalf("bad base evidence: %+v", base)
129 }
130 log, err := f.e.Log(context.Background(), base.LogHash)
131 must(t, err)
132 if Digest(log) != base.LogHash {
133 t.Fatal("log digest mismatch")
134 }
135 pass, err := f.e.Reproduce(context.Background(), f.d, f.r, fixID, f.hash)
136 must(t, err)
137 if pass.Outcome != team.CheckPass || pass.SubjectHash != f.hash || pass.OverlayHash != base.OverlayHash {
138 t.Fatalf("bad candidate evidence: %+v", pass)
139 }
140 for _, change := range []string{"bytes", "symptom", "names", "command", "definition_command", "definition_bytes", "hash", "base"} {
141 t.Run(change, func(t *testing.T) {
142 d := f.d
143 r := f.r
144 r.Files = append([]File(nil), r.Files...)
145 subject := fixID
146 hash := f.hash
147 switch change {
148 case "bytes":
149 r.Files[0].Bytes = []byte("changed")
150 case "symptom":
151 r.ExpectedSymptom = "other"
152 case "names":
153 r.TestNames = nil
154 case "definition_command":
155 d.Commands = append([]Command(nil), d.Commands...)
156 d.Commands[0].TimeoutSeconds--
157 d.Reproduction.TimeoutSeconds--
158 case "definition_bytes":
159 d.Files = append([]File(nil), d.Files...)
160 d.Files[0].Bytes = []byte("changed frozen file")
161 case "command":
162 d.Reproduction.Argv = []string{"true"}
163 case "hash":
164 hash = ""
165 case "base":
166 subject = "bad"
167 }
168 if _, err := f.e.Reproduce(context.Background(), d, r, subject, hash); err == nil {
169 t.Fatal("changed frozen evidence accepted")
170 }
171 })
172 }
173 entries, err := os.ReadDir(f.e.cfg.ScratchRoot)
174 must(t, err)
175 if len(entries) != 0 {
176 t.Fatal("scratch retained")
177 }
178}
179func TestTrustedBaseCheckDefinitions(t *testing.T) {
180 f := newFixture(t)
181 for _, change := range []string{"wrong_base", "unknown", "duplicate", "no_test", "timeout", "parser", "traversal", "filter", "baseline", "platform", "tags"} {
182 t.Run(change, func(t *testing.T) {
183 d := f.d
184 d.Commands = append([]Command(nil), d.Commands...)
185 b := raw(d)
186 switch change {
187 case "baseline":
188 d.Baseline.Hash = strings.Repeat("a", 64)
189 case "platform":
190 d.GOOS = "other"
191 case "tags":
192 d.Reproduction.Argv = append(append([]string(nil), d.Reproduction.Argv...), "-tags=other")
193 case "wrong_base":
194 d.Base = fixID
195 case "unknown":
196 b = append([]byte(`{"bogus":1,`), b[1:]...)
197 case "duplicate":
198 b = append([]byte(`{"version":1,`), b[1:]...)
199 case "no_test":
200 d.Commands = nil
201 case "timeout":
202 d.Commands[0].TimeoutSeconds = 1201
203 case "parser":
204 d.Commands[0].Parser = "exit"
205 case "traversal":
206 d.Commands[0].Dir = "../"
207 case "filter":
208 d.Commands[0].Argv = []string{"go", "test", "-json", "-run=TestOld"}
209 }
210 if change != "unknown" && change != "duplicate" {
211 b = raw(d)
212 }
213 if _, err := ParseDefinition(context.Background(), b, repoID, baseID); err == nil {
214 t.Fatal("unsafe definition accepted")
215 }
216 })
217 }
218 put(t, f.base, "scripts/gate.sh", "echo trusted\n")
219 trusted := f.d
220 trusted.TrustedPaths = []string{"scripts"}
221 d, err := FreezeMaterial(context.Background(), trusted, f.base)
222 must(t, err)
223 if len(d.Files) < 2 {
224 t.Fatal("transitive material not frozen")
225 }
226}
227func TestReproductionOverlayCollision(t *testing.T) {
228 for _, kind := range []string{"edited", "traversal", "absolute", "symlink", "duplicate", "non_test", "unchanged", "frozen"} {
229 t.Run(kind, func(t *testing.T) {
230 f := newFixture(t)
231 r := f.r
232 switch kind {
233 case "edited":
234 put(t, f.candidate, "bug_test.go", "candidate fix")
235 case "traversal":
236 r.Files[0].Path = "../bad_test.go"
237 case "absolute":
238 r.Files[0].Path = "/bad_test.go"
239 case "symlink":
240 must(t, os.Symlink(f.base, filepath.Join(f.candidate, "link")))
241 r.Files[0].Path = "link/bug_test.go"
242 case "duplicate":
243 r.Files = append(r.Files, r.Files[0])
244 case "non_test":
245 r.Files[0].Path = "value.go"
246 case "unchanged":
247 put(t, f.base, "bug_test.go", "package fixture\n")
248 put(t, f.candidate, "bug_test.go", "package fixture\n")
249 case "frozen":
250 put(t, f.candidate, "bug_test.go", bugTest)
251 }
252 f.e.cfg.ReproductionHash = Digest(raw(r))
253 _, err := f.e.Reproduce(context.Background(), f.d, r, fixID, f.hash)
254 if kind == "unchanged" || kind == "frozen" {
255 must(t, err)
256 } else if err == nil {
257 t.Fatal("unsafe overlay accepted")
258 }
259 if kind == "edited" {
260 b, err := os.ReadFile(filepath.Join(f.candidate, "bug_test.go"))
261 must(t, err)
262 if string(b) != "candidate fix" {
263 t.Fatal("fix overwritten")
264 }
265 }
266 })
267 }
268}
269func TestRepositoryTestSetSuperset(t *testing.T) {
270 f := newFixture(t)
271 put(t, f.candidate, "extra_test.go", "package fixture\nimport \"testing\"\nfunc TestExtra(t *testing.T){}\n")
272 result, err := f.e.RepositoryChecks(context.Background(), f.d, f.c, f.hash)
273 must(t, err)
274 if result.Outcome != team.CheckPass || result.ObservedTestSetHash == result.BaselineTestSetHash || len(result.ObservedNames) < 4 {
275 t.Fatalf("extra tests rejected: %+v", result)
276 }
277 if !result.Isolation.RunnerTainted || result.Isolation.Status != "unconfined" {
278 t.Fatal("repository checks lost runner taint")
279 }
280 spec := team.ArtifactSpec{Role: team.RoleRepositoryChecks, Payload: raw(result)}
281 _, err = f.e.ValidateEvidence(context.Background(), nil, f.e.cfg.Scope, f.e.cfg.Handle, spec)
282 must(t, err)
283 result.Isolation.RunnerTainted = false
284 spec.Payload = raw(result)
285 if _, err = f.e.ValidateEvidence(context.Background(), nil, f.e.cfg.Scope, f.e.cfg.Handle, spec); err == nil {
286 t.Fatal("stripped repository taint registered")
287 }
288 put(t, f.candidate, "old_test.go", strings.ReplaceAll(oldTest, "TestOld", "TestRenamed"))
289 if _, err = f.e.RepositoryChecks(context.Background(), f.d, f.c, f.hash); err == nil {
290 t.Fatal("renamed baseline accepted")
291 }
292}
293func TestRepositoryDeletedTestFile(t *testing.T) {
294 f := newFixture(t)
295 must(t, os.Remove(filepath.Join(f.candidate, "old_test.go")))
296 put(t, f.candidate, "renamed_test.go", oldTest)
297 if _, err := f.e.RepositoryChecks(context.Background(), f.d, f.c, f.hash); err == nil {
298 t.Fatal("deleted test file accepted")
299 }
300}
301func TestRepositoryTSkipRejected(t *testing.T) {
302 f := newFixture(t)
303 put(t, f.candidate, "old_test.go", strings.ReplaceAll(oldTest, "func(t *testing.T){}", "func(t *testing.T){t.Skip(\"skip\")}"))
304 if _, err := f.e.RepositoryChecks(context.Background(), f.d, f.c, f.hash); err == nil {
305 t.Fatal("skipped subtest accepted")
306 }
307}
308func TestExecutorEvidenceOutcomes(t *testing.T) {
309 f := newFixture(t)
310 for _, kind := range []string{"compile", "skip", "zero", "symptom"} {
311 t.Run(kind, func(t *testing.T) {
312 r := f.r
313 r.Files = append([]File(nil), r.Files...)
314 switch kind {
315 case "compile":
316 r.Files[0].Bytes = []byte("bad go")
317 case "skip":
318 r.Files[0].Bytes = []byte(strings.ReplaceAll(bugTest, "t.Fatal(\"wrong value\")", "t.Skip(\"wrong value\")"))
319 case "zero":
320 r.Files[0].Bytes = []byte("package fixture")
321 case "symptom":
322 r.ExpectedSymptom = "different symptom"
323 }
324 f.e.cfg.ReproductionHash = Digest(raw(r))
325 if _, err := f.e.Reproduce(context.Background(), f.d, r, baseID, ""); err == nil {
326 t.Fatal("invalid evidence accepted")
327 }
328 })
329 }
330 f.e.cfg.ReproductionHash = Digest(raw(f.r))
331 r, err := f.e.Reproduce(context.Background(), f.d, f.r, baseID, "")
332 must(t, err)
333 if !r.Isolation.RunnerTainted || r.Isolation.Status != "unconfined" {
334 t.Fatal("executor erased concurrent runner exposure")
335 }
336 spec := team.ArtifactSpec{Role: team.RoleBaseResult, Payload: raw(r)}
337 _, err = f.e.ValidateEvidence(context.Background(), nil, f.e.cfg.Scope, f.e.cfg.Handle, spec)
338 must(t, err)
339 stale := f.e.cfg.Handle
340 stale.Fence++
341 if _, err = f.e.ValidateEvidence(context.Background(), nil, f.e.cfg.Scope, stale, spec); err == nil {
342 t.Fatal("executor attestation crossed current fence")
343 }
344 r.Isolation.RunnerTainted = false
345 r.Isolation.Status = "confined"
346 spec.Payload = raw(r)
347 if _, err = f.e.ValidateEvidence(context.Background(), nil, f.e.cfg.Scope, f.e.cfg.Handle, spec); err == nil {
348 t.Fatal("stripped taint registered")
349 }
350 spec.Payload = []byte(`{"outcome":"pass"}`)
351 if _, err = f.e.ValidateEvidence(context.Background(), nil, f.e.cfg.Scope, f.e.cfg.Handle, spec); err == nil {
352 t.Fatal("agent fabricated executor result")
353 }
354}
355func TestExecutorStreaming(t *testing.T) {
356 for _, kind := range []string{"complete", "incomplete", "truncated", "no_tests", "skip", "build", "timeout", "large"} {
357 t.Run(kind, func(t *testing.T) {
358 s := newGoResults()
359 data := `{"Action":"start","Package":"p"}` + "\n" + `{"Action":"run","Package":"p","Test":"TestOne"}` + "\n"
360 switch kind {
361 case "no_tests":
362 data = `{"Action":"start","Package":"p"}` + "\n"
363 case "skip":
364 data += `{"Action":"skip","Package":"p","Test":"TestOne"}` + "\n"
365 case "build":
366 data += `{"Action":"build-fail","Package":"p"}` + "\n"
367 case "large":
368 for range 1000 {
369 data += `{"Action":"output","Package":"p","Test":"TestOne","Output":"` + strings.Repeat("x", 100) + `"}` + "\n"
370 }
371 }
372 if kind == "incomplete" {
373 data += `{"Action":"pass","Package":"p"}` + "\n"
374 } else if kind == "no_tests" {
375 data += `{"Action":"pass","Package":"p"}` + "\n"
376 } else if kind != "incomplete" {
377 data += `{"Action":"pass","Package":"p","Test":"TestOne"}` + "\n" + `{"Action":"pass","Package":"p"}` + "\n"
378 }
379 if kind == "truncated" {
380 data += `{"Action":`
381 }
382 for _, chunk := range bytes.SplitAfter([]byte(data), []byte("\n")) {
383 _, err := s.Write(chunk)
384 must(t, err)
385 }
386 err := s.finish(0, nil)
387 if kind == "complete" || kind == "large" {
388 must(t, err)
389 if len(s.log.Bytes()) > 65536 {
390 t.Fatal("unbounded retained output")
391 }
392 } else if kind != "timeout" && err == nil {
393 t.Fatal("invalid stream accepted")
394 }
395 if kind == "timeout" && s.finish(0, context.DeadlineExceeded) == nil {
396 t.Fatal("timeout accepted")
397 }
398 })
399 }
400}
401
402func TestExecutorCandidateBinding(t *testing.T) {
403 f := newFixture(t)
404 if _, err := f.e.Reproduce(context.Background(), f.d, f.r, fixID, strings.Repeat("a", 64)); err == nil {
405 t.Fatal("unverified candidate hash accepted")
406 }
407 if _, err := f.e.Reproduce(context.Background(), f.d, f.r, baseID, f.hash); err == nil {
408 t.Fatal("candidate hash bound to wrong commit")
409 }
410}
411func TestTrustedGateAndBaseline(t *testing.T) {
412 f := newFixture(t)
413 put(t, f.base, "gate.sh", "#!/bin/sh\nexit 1\n")
414 put(t, f.candidate, "gate.sh", "#!/bin/sh\nexit 0\n")
415 trusted := f.d
416 trusted.TrustedPaths = []string{"gate.sh"}
417 d, err := FreezeMaterial(context.Background(), trusted, f.base)
418 must(t, err)
419 d.Commands = append(d.Commands, Command{Argv: []string{"/bin/sh", "gate.sh"}, Dir: ".", Parser: "exit", TimeoutSeconds: 10})
420 f.e.cfg.DefinitionHash = Digest(raw(d))
421 if _, err = f.e.RepositoryChecks(context.Background(), d, f.c, f.hash); err == nil {
422 t.Fatal("candidate replaced trusted gate")
423 }
424 put(t, f.base, "known_test.go", bugTest)
425 d, err = FreezeMaterial(context.Background(), f.d, f.base)
426 must(t, err)
427 baseline, err := f.e.CaptureBaseline(context.Background(), d)
428 must(t, err)
429 if !superset(baseline.Names, []TestIdentity{{Package: "fixture", Name: "TestBug"}}) {
430 t.Fatal("known failing test omitted from baseline")
431 }
432 put(t, f.base, "known_test.go", "invalid Go")
433 if _, err = f.e.CaptureBaseline(context.Background(), d); err == nil {
434 t.Fatal("incomplete baseline accepted")
435 }
436}
437
438func TestTrustedBaseImmutableDefinition(t *testing.T) {
439 root := t.TempDir()
440 cmd := Command{Argv: []string{"go", "test", "-json", "-count=1", "./..."}, Dir: ".", Parser: "go-test-json", TimeoutSeconds: 30}
441 put(t, root, ".equipo/checks.json", string(raw(map[string]any{"version": 1, "commands": []Command{cmd}, "reproduction": cmd})))
442 put(t, root, "go.mod", "module fixture\n\ngo 1.26\n")
443 put(t, root, "old_test.go", oldTest)
444 git := func(args ...string) string {
445 t.Helper()
446 c := exec.Command("git", args...)
447 c.Dir = root
448 c.Env = append(testEnv(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null")
449 b, err := c.CombinedOutput()
450 if err != nil {
451 t.Fatalf("git %v: %s", args, b)
452 }
453 return strings.TrimSpace(string(b))
454 }
455 git("init", "-q")
456 git("add", ".")
457 git("-c", "user.name=fixture", "-c", "user.email=fixture@example.test", "-c", "commit.gpgSign=false", "commit", "-qm", "fixture")
458 commit := git("rev-parse", "HEAD")
459 put(t, root, ".equipo/checks.json", "changed after base")
460 d, err := LoadBaseDefinition(context.Background(), root, repoID, commit, t.TempDir())
461 must(t, err)
462 if d.Base != commit || len(d.Commands) != 1 || len(d.TestFiles) != 1 {
463 t.Fatal("definition was not read from base")
464 }
465 if _, err = LoadBaseDefinition(context.Background(), root, repoID, fixID, t.TempDir()); err == nil {
466 t.Fatal("unknown base accepted")
467 }
468 must(t, os.Remove(filepath.Join(root, ".equipo/checks.json")))
469 put(t, root, ".amadan/ci", "go test ./...\n")
470 git("add", ".")
471 git("-c", "user.name=fixture", "-c", "user.email=fixture@example.test", "-c", "commit.gpgSign=false", "commit", "-qm", "no adapter")
472 commit = git("rev-parse", "HEAD")
473 if _, err = LoadBaseDefinition(context.Background(), root, repoID, commit, t.TempDir()); err == nil {
474 t.Fatal("unreviewed CI adapter accepted")
475 }
476}
477
478func TestTrustedGatesFreshCopies(t *testing.T) {
479 f := newFixture(t)
480 put(t, f.base, "gate.sh", "#!/bin/sh\ntest ! -f modified\n")
481 put(t, f.base, "touch.sh", "touch modified\n")
482 put(t, f.candidate, "gate.sh", "#!/bin/sh\nexit 0\n")
483 trusted := f.d
484 trusted.TrustedPaths = []string{"gate.sh", "touch.sh"}
485 d, err := FreezeMaterial(context.Background(), trusted, f.base)
486 must(t, err)
487 d.Commands = append(d.Commands, Command{Argv: []string{"/bin/sh", "touch.sh"}, Dir: ".", Parser: "exit", TimeoutSeconds: 10}, Command{Argv: []string{"/bin/sh", "gate.sh"}, Dir: ".", Parser: "exit", TimeoutSeconds: 10})
488 f.e.cfg.DefinitionHash = Digest(raw(d))
489 if _, err = f.e.RepositoryChecks(context.Background(), d, f.c, f.hash); err != nil {
490 t.Fatal("earlier command changed a later gate's workspace", err)
491 }
492}
493func TestExecutorHistoricalExposure(t *testing.T) {
494 f := newFixture(t)
495 r, err := f.e.Reproduce(context.Background(), f.d, f.r, baseID, "")
496 must(t, err)
497 f.e.cfg.Isolation = func(context.Context) (team.IsolationSnapshot, error) {
498 return team.IsolationSnapshot{Status: "confined", ObservedTuples: []team.IsolationTuple{{Provider: "claude"}}}, nil
499 }
500 again, err := f.e.Reproduce(context.Background(), f.d, f.r, baseID, "")
501 must(t, err)
502 if !again.Isolation.RunnerTainted || again.Isolation.Status != "unconfined" {
503 t.Fatal("historical runner taint erased")
504 }
505 h := f.e.cfg.Handle
506 h.Fence++
507 if _, err = f.e.ValidateEvidence(context.Background(), nil, f.e.cfg.Scope, h, team.ArtifactSpec{Role: team.RoleBaseResult, Payload: raw(r)}); err == nil {
508 t.Fatal("attestation crossed fence")
509 }
510}
511
512func TestReproductionOverlayPathValidation(t *testing.T) {
513 root := t.TempDir()
514 outside := t.TempDir()
515 must(t, os.Symlink(outside, filepath.Join(root, "link")))
516 for _, path := range []string{"../escape_test.go", "/escape_test.go", "link/escape_test.go"} {
517 if err := safePath(root, path, true); err == nil {
518 t.Fatal("unsafe path accepted", path)
519 }
520 }
521 if err := filesValid([]File{{Path: "same_test.go"}, {Path: "same_test.go"}}, true); err == nil {
522 t.Fatal("duplicate overlay accepted")
523 }
524}
525
526func TestExecutorCanonicalCandidateHash(t *testing.T) {
527 f := newFixture(t)
528 canonical := []byte(`{"base_commit":"1111111111111111111111111111111111111111","commit":"2222222222222222222222222222222222222222","repository":"https://example.test/team/repo","tree":"3333333333333333333333333333333333333333"}`)
529 hash := Digest(canonical)
530 f.e.cfg.ResolveCandidate = func(context.Context, string) (team.GitCandidate, error) { return f.c, nil }
531 r, err := f.e.Reproduce(context.Background(), f.d, f.r, fixID, hash)
532 must(t, err)
533 if r.SubjectHash != hash {
534 t.Fatal("candidate subject does not use canonical artifact hash")
535 }
536 if _, err = f.e.RepositoryChecks(context.Background(), f.d, f.c, hash); err != nil {
537 t.Fatal("repository checks rejected canonical artifact", err)
538 }
539}
540func TestExecutorProductionDependencies(t *testing.T) {
541 f := newFixture(t)
542 if _, err := New(f.e.cfg, &CodexSandbox{}); err == nil {
543 t.Fatal("empty production sandbox accepted")
544 }
545}
546
547func TestTrustedMaterialPreservesCandidateData(t *testing.T) {
548 f := newFixture(t)
549 put(t, f.base, "data.json", "old")
550 put(t, f.candidate, "data.json", "fixed")
551 put(t, f.candidate, "data_test.go", `package fixture
552import("testing";"os")
553func TestData(t *testing.T){b,e:=os.ReadFile("data.json");if e!=nil||string(b)!="fixed"{t.Fatal("candidate data was replaced")}}
554`)
555 d, err := FreezeMaterial(context.Background(), f.d, f.base)
556 must(t, err)
557 f.e.cfg.DefinitionHash = Digest(raw(d))
558 if _, err = f.e.RepositoryChecks(context.Background(), d, f.c, f.hash); err != nil {
559 t.Fatal("candidate data changed by trusted overlay", err)
560 }
561}
562
563func TestTrustedMaterialDirectory(t *testing.T) {
564 f := newFixture(t)
565 put(t, f.base, "controls/helper.sh", "value=trusted\n")
566 put(t, f.base, "controls/gate.sh", ". controls/helper.sh\ntest \"$value\" = trusted && test ! -e controls/extra.sh\n")
567 put(t, f.candidate, "controls/helper.sh", "value=changed\n")
568 put(t, f.candidate, "controls/extra.sh", "injected\n")
569 d := f.d
570 d.TrustedPaths = []string{"controls"}
571 d, err := FreezeMaterial(context.Background(), d, f.base)
572 must(t, err)
573 d.Commands = append(d.Commands, Command{Argv: []string{"/bin/sh", "controls/gate.sh"}, Dir: ".", Parser: "exit", TimeoutSeconds: 10})
574 f.e.cfg.DefinitionHash = Digest(raw(d))
575 if _, err = f.e.RepositoryChecks(context.Background(), d, f.c, f.hash); err != nil {
576 t.Fatal("trusted dependency tree was not restored", err)
577 }
578}
579
580func TestTrustedUndeclaredScript(t *testing.T) {
581 f := newFixture(t)
582 d := f.d
583 d.Commands = append(d.Commands, Command{Argv: []string{"/bin/sh", "gate.sh"}, Dir: ".", Parser: "exit", TimeoutSeconds: 10})
584 if _, err := ParseDefinition(context.Background(), raw(d), repoID, baseID); err == nil {
585 t.Fatal("undeclared command material accepted")
586 }
587}
588