| 1 | package checks |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "slices" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 9 | ) |
| 10 | |
| 11 | func TestFrozenReproductionOwnFailure(t *testing.T) { |
| 12 | for _, kind := range []string{"passing", "mixed", "unrelated_symptom", "counts"} { |
| 13 | t.Run(kind, func(t *testing.T) { |
| 14 | f := newFixture(t) |
| 15 | put(t, f.base, "unrelated_test.go", `package fixture |
| 16 | import "testing" |
| 17 | func TestUnrelated(t *testing.T){t.Fatal("wrong value")} |
| 18 | `) |
| 19 | switch kind { |
| 20 | case "passing": |
| 21 | f.r.Files[0].Bytes = []byte("package fixture\nimport \"testing\"\nfunc TestBug(t *testing.T){}\n") |
| 22 | case "mixed": |
| 23 | f.r.TestNames = append(f.r.TestNames, TestIdentity{Package: "fixture", Name: "TestOld"}) |
| 24 | case "unrelated_symptom": |
| 25 | f.r.Files[0].Bytes = []byte("package fixture\nimport \"testing\"\nfunc TestBug(t *testing.T){t.Fatal(\"different\")}\n") |
| 26 | } |
| 27 | f.e.cfg.ReproductionHash = Digest(raw(f.r)) |
| 28 | result, err := f.e.Reproduce(context.Background(), f.d, f.r, baseID, "") |
| 29 | if kind == "counts" { |
| 30 | must(t, err) |
| 31 | if result.TestsRun != 1 || result.TestsFailed != 1 { |
| 32 | t.Fatalf("counts include unrelated tests: %d/%d", result.TestsRun, result.TestsFailed) |
| 33 | } |
| 34 | } else if err == nil { |
| 35 | t.Fatal("unrelated failure accepted as reproduction") |
| 36 | } |
| 37 | }) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestRepositoryTestPackagesWithoutTests(t *testing.T) { |
| 42 | f := newFixture(t) |
| 43 | for _, root := range []string{f.base, f.candidate} { |
| 44 | put(t, root, "types/value.go", "package types\ntype Value int\n") |
| 45 | } |
| 46 | baseline, err := f.e.CaptureBaseline(context.Background(), f.d) |
| 47 | must(t, err) |
| 48 | if slices.Contains(baseline.Names, TestIdentity{Package: "fixture/types"}) { |
| 49 | t.Fatal("empty package entered baseline") |
| 50 | } |
| 51 | f.d.Baseline = baseline |
| 52 | f.e.cfg.DefinitionHash = Digest(raw(f.d)) |
| 53 | result, err := f.e.RepositoryChecks(context.Background(), f.d, f.c, f.hash) |
| 54 | must(t, err) |
| 55 | if slices.Contains(result.ObservedNames, TestIdentity{Package: "fixture/types"}) { |
| 56 | t.Fatal("empty package entered observed identities") |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestTrustedMakefilePrecedence(t *testing.T) { |
| 61 | for _, name := range []string{"GNUmakefile", "makefile"} { |
| 62 | t.Run(name, func(t *testing.T) { |
| 63 | f := newFixture(t) |
| 64 | put(t, f.base, "Makefile", "check:\n\texit 1\n") |
| 65 | put(t, f.candidate, name, "check:\n\texit 0\n") |
| 66 | d, err := FreezeMaterial(context.Background(), f.d, f.base) |
| 67 | must(t, err) |
| 68 | d.Commands = append(d.Commands, Command{Argv: []string{"make", "check"}, Dir: ".", Parser: "exit", TimeoutSeconds: 10}) |
| 69 | f.e.cfg.DefinitionHash = Digest(raw(d)) |
| 70 | if _, err := f.e.RepositoryChecks(context.Background(), d, f.c, f.hash); err == nil { |
| 71 | t.Fatal("candidate makefile replaced trusted gate") |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestTrustedExitAllowlist(t *testing.T) { |
| 78 | f := newFixture(t) |
| 79 | for _, argv := range [][]string{ |
| 80 | {"make", "-f", "other.mk"}, {"make", "--file=other.mk"}, {"make", "--makefile", "other.mk"}, |
| 81 | {"make", "-Csub"}, {"make", "--directory=sub"}, {"make", "-I", "sub"}, |
| 82 | {"perl", "gate.pl"}, {"ruby", "gate.rb"}, {"python3.12", "gate.py"}, {"env", "sh", "gate.sh"}, |
| 83 | {"unknown"}, {"go", "test", "./..."}, {"go", "vet", "./..."}, {"/candidate/sh", "gate.sh"}, {"sh", "gate.sh", "untrusted/file"}, |
| 84 | } { |
| 85 | t.Run(argv[0]+"_"+argv[len(argv)-1], func(t *testing.T) { |
| 86 | d := f.d |
| 87 | d.TrustedPaths = []string{"gate.sh", "other.mk", "sub"} |
| 88 | d.Commands = append(slices.Clone(d.Commands), Command{Argv: argv, Dir: ".", Parser: "exit", TimeoutSeconds: 10}) |
| 89 | if _, err := ParseDefinition(context.Background(), raw(d), repoID, baseID); err == nil { |
| 90 | t.Fatalf("unsafe exit gate accepted: %v", argv) |
| 91 | } |
| 92 | }) |
| 93 | } |
| 94 | d := f.d |
| 95 | d.Commands = append(slices.Clone(d.Commands), Command{Argv: []string{"go", "vet"}, Dir: ".", Parser: "exit", TimeoutSeconds: 10}) |
| 96 | _, err := ParseDefinition(context.Background(), raw(d), repoID, baseID) |
| 97 | must(t, err) |
| 98 | } |
| 99 | |
| 100 | func TestExecutorIsolationOrderAndOwnership(t *testing.T) { |
| 101 | f := newFixture(t) |
| 102 | iso := team.IsolationSnapshot{Status: "unconfined", RunnerTainted: true, AcknowledgementIDs: []string{"z", "a"}, ObservedTuples: []team.IsolationTuple{{Provider: "codex"}, {Provider: "claude"}}} |
| 103 | f.e.cfg.Isolation = func(context.Context) (team.IsolationSnapshot, error) { return iso, nil } |
| 104 | result, err := f.e.Reproduce(context.Background(), f.d, f.r, baseID, "") |
| 105 | must(t, err) |
| 106 | slices.Reverse(iso.AcknowledgementIDs) |
| 107 | slices.Reverse(iso.ObservedTuples) |
| 108 | _, err = f.e.ValidateEvidence(context.Background(), nil, f.e.cfg.Scope, f.e.cfg.Handle, team.ArtifactSpec{Role: team.RoleBaseResult, Payload: raw(result)}) |
| 109 | must(t, err) |
| 110 | snapshot, err := f.e.isolation(context.Background()) |
| 111 | must(t, err) |
| 112 | snapshot.AcknowledgementIDs[0] = "changed" |
| 113 | snapshot.ObservedTuples[0].Provider = "changed" |
| 114 | if slices.Contains(f.e.exposure.AcknowledgementIDs, "changed") || f.e.exposure.ObservedTuples[0].Provider == "changed" { |
| 115 | t.Fatal("historical exposure aliases returned snapshot") |
| 116 | } |
| 117 | if slices.Contains(iso.AcknowledgementIDs, "changed") { |
| 118 | t.Fatal("snapshot aliases callback") |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestExecutorNoTestPackageSkipGuard(t *testing.T) { |
| 123 | for _, kind := range []string{"valid", "missing_output", "test_events", "test_output", "output_after_skip"} { |
| 124 | t.Run(kind, func(t *testing.T) { |
| 125 | g := newGoResults() |
| 126 | event := func(action, pkg, test, output string) { |
| 127 | g.event(append(raw(map[string]string{"Action": action, "Package": pkg, "Test": test, "Output": output}), '\n')) |
| 128 | } |
| 129 | event("start", "tested", "", "") |
| 130 | event("run", "tested", "TestOne", "") |
| 131 | event("pass", "tested", "TestOne", "") |
| 132 | event("pass", "tested", "", "") |
| 133 | event("start", "empty", "", "") |
| 134 | if kind == "test_events" { |
| 135 | event("run", "empty", "TestGone", "") |
| 136 | event("pass", "empty", "TestGone", "") |
| 137 | } |
| 138 | if kind == "test_output" { |
| 139 | event("output", "empty", "TestGone", "orphan output") |
| 140 | } |
| 141 | if kind != "missing_output" && kind != "output_after_skip" { |
| 142 | event("output", "empty", "", "? \tempty\t[no test files]\n") |
| 143 | } |
| 144 | event("skip", "empty", "", "") |
| 145 | if kind == "output_after_skip" { |
| 146 | event("output", "empty", "", "? \tempty\t[no test files]\n") |
| 147 | } |
| 148 | err := g.finish(0, nil) |
| 149 | if kind == "valid" { |
| 150 | must(t, err) |
| 151 | } else if err == nil { |
| 152 | t.Fatal("invalid package skip accepted") |
| 153 | } |
| 154 | }) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestTrustedExitParser(t *testing.T) { |
| 159 | c := Command{Argv: []string{"go", "test", "./..."}, Dir: ".", Parser: "exit", TimeoutSeconds: 10} |
| 160 | if commandValid(c, false) { |
| 161 | t.Fatal("exit parser accepted go test") |
| 162 | } |
| 163 | c.Argv[1] = "vet" |
| 164 | if !commandValid(c, false) { |
| 165 | t.Fatal("exit parser refused go vet") |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func TestFrozenHashBoundary(t *testing.T) { |
| 170 | f := newFixture(t) |
| 171 | r := f.r |
| 172 | r.Files = slices.Clone(r.Files) |
| 173 | r.Files[0].Bytes = []byte(bugTest + "\n") |
| 174 | if _, err := f.e.Reproduce(context.Background(), f.d, r, fixID, f.hash); err == nil { |
| 175 | t.Fatal("changed valid frozen bytes accepted") |
| 176 | } |
| 177 | } |
| 178 | |