| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "strings" |
| 8 | "sync" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // warned records what P2 said to runner.log. |
| 15 | type warned struct { |
| 16 | mu sync.Mutex |
| 17 | calls [][]any |
| 18 | } |
| 19 | |
| 20 | func (w *warned) warn(msg string, args ...any) { |
| 21 | w.mu.Lock() |
| 22 | defer w.mu.Unlock() |
| 23 | w.calls = append(w.calls, append([]any{msg}, args...)) |
| 24 | } |
| 25 | |
| 26 | func (w *warned) attr(i int, key string) string { |
| 27 | w.mu.Lock() |
| 28 | defer w.mu.Unlock() |
| 29 | c := w.calls[i] |
| 30 | for j := 1; j+1 < len(c); j += 2 { |
| 31 | if c[j] == key { |
| 32 | return fmt.Sprint(c[j+1]) |
| 33 | } |
| 34 | } |
| 35 | return "" |
| 36 | } |
| 37 | |
| 38 | // A real build and test prints far more than the gate keeps (its last |
| 39 | // 200 lines), the script build's report lines and success line first: |
| 40 | // P2 reads the whole output as it comes, so a full run is still judged. |
| 41 | func TestP2ReadsTheWholeOutput(t *testing.T) { |
| 42 | f := newProbeFixture(t, allLines("denied"), "FAKE_NOISE=1") |
| 43 | var w warned |
| 44 | f.h.Warn = w.warn |
| 45 | p, ran, _ := probeP2(context.Background(), f.h, Capabilities(context.Background(), f.h.Exec, f.h.Tools)) |
| 46 | if p.Verdict != wire.ProbeDenied || !ran || len(w.calls) != 0 { |
| 47 | t.Fatalf("a long, complete run: %+v ran=%v warned=%v", p, ran, w.calls) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Each step of the srt run that fails is named in P2's detail with its |
| 52 | // exit and first error line, and once in runner.log with the bounded |
| 53 | // excerpt; P2 is never denied for it. |
| 54 | func TestP2SaysWhichStepFailed(t *testing.T) { |
| 55 | for _, c := range []struct{ env, phase, detail string }{ |
| 56 | {"FAKE_BUILD_FAIL=1", "script build", "srt_failed:script build exit 65"}, |
| 57 | {"FAKE_RESOLVE_FAIL=1", "package resolve", "srt_failed:package resolve exit 74"}, |
| 58 | {"FAKE_TEST_BUILD_FAIL=error: no such module 'XCTest'", "test build", "srt_failed:test build exit 65"}, |
| 59 | {"FAKE_BOOT_FAIL=1", "simulator boot", "srt_failed:simulator boot exit 149"}, |
| 60 | {"FAKE_RUN_FAIL=1", "test run", "srt_failed:test run exit 65"}, |
| 61 | } { |
| 62 | f := newProbeFixture(t, allLines("denied"), c.env) |
| 63 | var w warned |
| 64 | f.h.Warn = w.warn |
| 65 | p, ran, _ := probeP2(context.Background(), f.h, Capabilities(context.Background(), f.h.Exec, f.h.Tools)) |
| 66 | // The phase and its exit, never the output (whole-branch fix 1, |
| 67 | // #6: the excerpt goes only to the runner's log). |
| 68 | codedProbes(t, p) |
| 69 | if ran || p.Verdict != wire.ProbeFailed || !strings.HasSuffix(p.Detail, c.detail) { |
| 70 | t.Fatalf("%s: %+v ran=%v", c.phase, p, ran) |
| 71 | } |
| 72 | if err := wire.ValidCapabilities(wire.Capabilities{Probes: []wire.Probe{p}}); err != nil { |
| 73 | t.Fatalf("%s: detail outside the capability rules: %v", c.phase, err) |
| 74 | } |
| 75 | if len(w.calls) != 1 || w.calls[0][0] != "p2 srt run failed" || w.attr(0, "phase") != c.phase || w.attr(0, "excerpt") == "" || w.attr(0, "timed_out") != "false" { |
| 76 | t.Fatalf("%s: warned %v", c.phase, w.calls) |
| 77 | } |
| 78 | } |
| 79 | // No step reported at all: srt itself did not start the command. |
| 80 | f := newProbeFixture(t, allLines("denied")) |
| 81 | if err := os.WriteFile(f.h.Tools.SRT, []byte("#!/bin/bash\necho 'Error: Refusing to run with the built-in defaults' >&2\nexit 1\n"), 0700); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | var w warned |
| 85 | f.h.Warn = w.warn |
| 86 | p, _, _ := probeP2(context.Background(), f.h, Capabilities(context.Background(), f.h.Exec, f.h.Tools)) |
| 87 | if p.Detail != "not_shown_denied:script.* plugin.* test.*;srt_failed:srt start exit 1" || w.attr(0, "phase") != "srt start" { |
| 88 | t.Fatalf("srt did not start: %+v %v", p, w.calls) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // What P2 says about a failure never carries the sentinel secret, the |
| 93 | // Keychain service, the targets file's contents or a path under a home, |
| 94 | // even when the failing tool prints all of them. |
| 95 | func TestP2FailureIsRedacted(t *testing.T) { |
| 96 | leak := `t=../equipo-targets.txt; echo "error: cannot read $(sed -n 's/^state=//p' $t)"; echo "error: no item $(sed -n 's/^keychain=//p' $t) in $(sed -n 's/^keychain_file=//p' $t)"; echo "error: $(tr '\n' ' ' < $t)"; printf 'error: \033[31mred\033[0m and \001bell\n'` |
| 97 | f := newProbeFixture(t, allLines("denied")) |
| 98 | xb, _ := os.ReadFile(f.h.Tools.Xcodebuild) |
| 99 | patched := strings.Replace(string(xb), `[ -n "$FAKE_TEST_BUILD_FAIL" ] &&`, `{ `+leak+`; exit 65; }; [ -n "$FAKE_TEST_BUILD_FAIL" ] &&`, 1) |
| 100 | if err := os.WriteFile(f.h.Tools.Xcodebuild, []byte(patched), 0700); err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | var w warned |
| 104 | f.h.Warn = w.warn |
| 105 | f.h.Paths.OtherHomes = []string{"/Users/paul"} |
| 106 | p, _, _ := probeP2(context.Background(), f.h, Capabilities(context.Background(), f.h.Exec, f.h.Tools)) |
| 107 | if len(w.calls) != 1 || w.attr(0, "phase") != "test build" { |
| 108 | t.Fatalf("warned %v", w.calls) |
| 109 | } |
| 110 | excerpt := w.attr(0, "excerpt") |
| 111 | said := p.Detail + "\n" + fmt.Sprint(w.calls) |
| 112 | for _, bad := range []string{f.dir, "/Users/paul", "io.amadan.equipo.probe.", "probe-sentinel-", "login.keychain", "port_", "keychain=", "example.com", "\x1b", "\x01"} { |
| 113 | if strings.Contains(said, bad) { |
| 114 | t.Fatalf("%q left in %q", bad, said) |
| 115 | } |
| 116 | } |
| 117 | if !strings.Contains(excerpt, "error: cannot read [redacted] | error: no item [redacted] in [redacted]") || !strings.Contains(excerpt, "red and bell") || len(excerpt) > p2ExcerptBytes { |
| 118 | t.Fatalf("excerpt %q", excerpt) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // The judge of a failure from recorded gate outputs: the step a timeout |
| 123 | // stopped, srt or the gate not starting, every step passing yet no |
| 124 | // success line, and the excerpt's bounds (five lines, 400 bytes, no |
| 125 | // control characters). |
| 126 | func TestP2FailureFromGateOutput(t *testing.T) { |
| 127 | red := newRedactor([]string{"0123456789abcdef"}, []string{"/Users/equipo-paul"}) |
| 128 | feed := func(lines ...string) *p2Watch { |
| 129 | w := newP2Watch() |
| 130 | for _, l := range lines { |
| 131 | w.line(l) |
| 132 | } |
| 133 | return w |
| 134 | } |
| 135 | for name, c := range map[string]struct { |
| 136 | w *p2Watch |
| 137 | res Result |
| 138 | phase string |
| 139 | short string |
| 140 | timeup bool |
| 141 | }{ |
| 142 | "timed out in the test build": {feed("EQUIPO-PHASE script-build 0", "EQUIPO-PHASE package-resolve 0"), Result{Code: -1, Reason: "checks_timeout"}, "test build", "test build timed out", true}, |
| 143 | "a failure, then a timeout": {feed("EQUIPO-PHASE script-build 65", "EQUIPO-PHASE package-resolve 0"), Result{Code: -1, Reason: "checks_timeout"}, "script build", "script build exit 65, test build timed out", true}, |
| 144 | "stopped by the run's limit": {feed(), Result{Code: -1, Reason: "cancelled"}, "script build", "script build stopped", false}, |
| 145 | "srt refused its settings": {feed("Error: /Users/equipo-paul/.local/s.json does not exist."), Result{Code: 1}, "srt start", "srt start exit 1: Error: [home]...s.json does not exist.", false}, |
| 146 | "the gate could not start": {feed(), Result{Code: -1, Reason: "checks_unavailable"}, "gate start", "gate start failed", false}, |
| 147 | "no success line": {feed("EQUIPO-PHASE script-build 0", "EQUIPO-PHASE package-resolve 0", "EQUIPO-PHASE test-build 0", "EQUIPO-PHASE simulator-boot 0", "EQUIPO-PHASE test-run 0", "** BUILD SUCCEEDED **"), |
| 148 | Result{Code: 0}, "run end", "run end exit 0", false}, |
| 149 | } { |
| 150 | if c.w.ran(c.res) { |
| 151 | t.Fatalf("%s: taken as ran", name) |
| 152 | } |
| 153 | f := c.w.failure(c.res, red) |
| 154 | if f.Phase != c.phase || f.short() != c.short || f.TimedOut != c.timeup { |
| 155 | t.Fatalf("%s: %+v short %q", name, f, f.short()) |
| 156 | } |
| 157 | } |
| 158 | var lines []string |
| 159 | for i := 0; i < 9; i++ { |
| 160 | lines = append(lines, fmt.Sprintf("/Users/equipo-paul/x/f%d.swift:1:2: error: \x1b[1m%s\x07 0123456789abcdef", i, strings.Repeat("é", 60))) |
| 161 | } |
| 162 | lines = append(lines, lines[0]) |
| 163 | f := feed(lines...).failure(Result{Code: 65}, red) |
| 164 | if n := strings.Count(f.Excerpt, " | ") + 1; n > p2ErrorLines || len(f.Excerpt) > p2ExcerptBytes || len(f.First) > p2ShortBytes || !strings.HasPrefix(f.First, "[home]...f0.swift:1:2: error: é") { |
| 165 | t.Fatalf("bounds: %d lines, %d bytes, first %q", n, len(f.Excerpt), f.First) |
| 166 | } |
| 167 | for _, r := range f.Excerpt { |
| 168 | if r < 0x20 || r == 0x7f || r == '�' { |
| 169 | t.Fatalf("control or broken character in %q", f.Excerpt) |
| 170 | } |
| 171 | } |
| 172 | if strings.Contains(f.Excerpt, "0123456789abcdef") || strings.Contains(f.Excerpt, "equipo-paul") { |
| 173 | t.Fatalf("not redacted: %q", f.Excerpt) |
| 174 | } |
| 175 | // A run that did everything, by every account. |
| 176 | all := feed("EQUIPO-PHASE script-build 0", "EQUIPO-PHASE package-resolve 0", "EQUIPO-PHASE test-build 0", "EQUIPO-PHASE simulator-boot 0", "EQUIPO-PHASE test-run 0", "** BUILD SUCCEEDED **", "** TEST BUILD SUCCEEDED **", "** TEST EXECUTE SUCCEEDED **") |
| 177 | if !all.ran(Result{Code: 0}) || all.ran(Result{Code: 1}) { |
| 178 | t.Fatal("ran") |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // However long the reason, the detail keeps what decides trust (the |
| 183 | // end not proven) and stays a capability string: the reason is cut. |
| 184 | func TestP2DetailKeepsTheEnd(t *testing.T) { |
| 185 | unknown := []string{"script.state", "script.tcp", "script.dns", "script.keychain", "plugin.state", "plugin.tcp", "plugin.dns", "plugin.keychain", "test.state", "test.tcp", "test.dns", "test.keychain"} |
| 186 | d := p2Detail(map[string][]string{"unknown": unknown[:11], "allowed": unknown[11:]}, 12, false, false, "test build exit 65: "+strings.Repeat("error: x ", 60)) |
| 187 | if parts, ok := wire.ParseProbeDetail(d); !ok || !strings.Contains(d, ";end_unproven") || len(d) > wire.MaxCapabilityString || len(parts) < 3 { |
| 188 | t.Fatalf("detail %q", d) |
| 189 | } |
| 190 | if d := p2Detail(map[string][]string{"unknown": unknown}, 12, false, true, ""); d != wire.ProbeDetail("not_shown_denied", strings.Join(unknown, " "))+";srt_not_run" { |
| 191 | t.Fatalf("detail %q", d) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // P2's script runs every step it names, in order, under the gate's |
| 196 | // TMPDIR, with the package's plugins allowed to run unvalidated and |
| 197 | // nothing that reaches the network. |
| 198 | func TestP2Script(t *testing.T) { |
| 199 | s := P2Script(quote("/usr/bin/xcodebuild"), quote("/usr/bin/xcrun"), "ABCDEF01-2345-6789-ABCD-EF0123456789") |
| 200 | last := -1 |
| 201 | for _, p := range p2Phases { |
| 202 | i := strings.Index(s, "phase "+p.key+" ") |
| 203 | if i < last { |
| 204 | t.Fatalf("%s missing or out of order in %s", p.key, s) |
| 205 | } |
| 206 | last = i |
| 207 | } |
| 208 | if strings.Count(s, "-skipPackagePluginValidation") != 3 || strings.Count(s, XcodeWrites) != 5 || strings.Count(s, `-resultBundlePath "$TMPDIR/Results/`) != 4 || strings.Contains(s, "http") { |
| 209 | t.Fatalf("script %s", s) |
| 210 | } |
| 211 | } |
| 212 | |