package main import ( "archive/tar" "bytes" "compress/gzip" "context" "crypto/sha256" "encoding/hex" "errors" "fmt" "io" "os" "path/filepath" "slices" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/mac" runnersetup "amadan.net/oficina/equipo/internal/runner/setup" "amadan.net/rastrillo/rastrillo" ) type macRig struct { h macInstallHost home string ids map[string]string calls []string enrolled *runner.Config readable map[string]bool // archives are what the fake download serves, by URL; fetched the // URLs asked for. archives map[string][]byte fetched []string // loaded is whether launchd has the agent; stuck keeps it loaded // through a bootout; bootoutErr is what bootout answers. loaded, stuck bool bootoutErr error // bootoutTakesDown is a bootout that answers bootoutErr after it // took the agent out; bootstrapErr is what bootstrap answers. bootoutTakesDown bool bootstrapErr error // dsclHome is the home the user directory records ("" for none); // loggedOut is the user having no login session; childHomes the // homes npm and xcodebuild were run with. dsclHome string loggedOut bool childHomes []string // cwd is the directory the installer changed into ("" before it did). cwd string } func newMacRig(t *testing.T) *macRig { t.Helper() old := rastrillo.BuildVersion rastrillo.BuildVersion = "abc1234" t.Cleanup(func() { rastrillo.BuildVersion = old }) root := t.TempDir() users := filepath.Join(root, "Users") x := &macRig{home: filepath.Join(users, "equipo-paul"), dsclHome: filepath.Join(users, "equipo-paul"), ids: map[string]string{"-un": "equipo-paul\n", "-u": "502\n", "-Gn": "staff everyone localaccounts _developer\n"}, readable: map[string]bool{}, archives: map[string][]byte{}} for _, d := range []string{x.home, filepath.Join(users, "paul"), filepath.Join(users, "Shared")} { if err := os.MkdirAll(d, 0700); err != nil { t.Fatal(err) } } self := filepath.Join(root, "downloaded-equipo-runner") if err := os.WriteFile(self, []byte("#!verified runner\n"), 0700); err != nil { t.Fatal(err) } x.h = macInstallHost{goos: "darwin", goarch: "arm64", users: users, self: self, scannerPin: fmt.Sprintf("%x", sha256.Sum256([]byte("scanner"))), exec: func(_ context.Context, name string, args ...string) (string, error) { switch name { case "/usr/bin/id": return x.ids[args[0]], nil case "/usr/bin/dscl": if x.dsclHome == "" || strings.Join(args, " ") != ". -read /Users/"+strings.TrimSpace(x.ids["-un"])+" NFSHomeDirectory" { return "", errors.New("eDSRecordNotFound") } return "NFSHomeDirectory: " + x.dsclHome + "\n", nil case "/usr/sbin/scutil": return "Tito Studio\n", nil } return "", errors.New("unexpected " + name) }, exposed: func(p string) bool { return x.readable[filepath.Base(p)] }, fetch: func(_ context.Context, url string, _ int64) ([]byte, error) { if strings.HasSuffix(url, "/gitleaks") { return []byte("scanner"), nil } x.fetched = append(x.fetched, url) if b, ok := x.archives[url]; ok { return b, nil } return nil, errors.New("404 " + url) }, npmCI: func(_ context.Context, npm, prefix, home string) error { x.calls = append(x.calls, "npm "+npm+" "+prefix) x.childHomes = append(x.childHomes, home) if _, err := os.Stat(filepath.Join(prefix, "package-lock.json")); err != nil { return err } return nil }, // As runner.Enrol tells it: the link notice, the link, waiting. enrol: func(_ context.Context, cfg runner.Config, out io.Writer) error { x.enrolled = &cfg cfg.EnrolNotify(runner.EnrolNotice{Link: true}) _, _ = io.WriteString(out, "[https://equipo.example/runners/enrol?code=ABCDEFGHIJ](https://equipo.example/runners/enrol?code=ABCDEFGHIJ)\nABCDEFGHIJ\n") cfg.EnrolNotify(runner.EnrolNotice{Waiting: true}) return nil }, chdir: func(dir string) error { x.cwd = dir; return nil }, launchctl: func(ctx context.Context, args ...string) error { // launchctl is a program: a cancelled context never starts it. if ctx.Err() != nil { return ctx.Err() } x.calls = append(x.calls, "launchctl "+strings.Join(args, " ")) switch args[0] { case "print": if len(args) == 2 && args[1] == "gui/"+strings.TrimSpace(x.ids["-u"]) { // The login session, not the agent: not a call. x.calls = x.calls[:len(x.calls)-1] if x.loggedOut { return errors.New("Could not find domain for user gui: 502") } return nil } if !x.loaded { return errors.New("could not find service") } case "bootout": if x.bootoutErr != nil { if x.bootoutTakesDown { x.loaded = false } return x.bootoutErr } x.loaded = x.stuck case "bootstrap": if x.bootstrapErr != nil { return x.bootstrapErr } x.loaded = true } return nil }, stopWait: 300 * time.Millisecond, verify: func(string) error { return nil }, firstLaunch: func(_ context.Context, home string) { x.calls = append(x.calls, "firstLaunch") x.childHomes = append(x.childHomes, home) }, } // Stand-ins for the pinned Node and ripgrep archives, pinned the same // way (their digests), served by the fake download. node, rg := toolArchive(t, "node-v1", "bin/node"), toolArchive(t, "rg-1", "rg") x.archives["https://nodejs.example/node.tar.gz"], x.archives["https://rg.example/rg.tar.gz"] = node, rg x.h.tools = []mac.Tool{ {Name: mac.Node.Name, URL: "https://nodejs.example/node.tar.gz", SHA256: digest(node), Root: "node-v1", Binary: "bin/node"}, {Name: mac.Ripgrep.Name, URL: "https://rg.example/rg.tar.gz", SHA256: digest(rg), Root: "rg-1", Binary: "rg"}, } return x } func digest(b []byte) string { s := sha256.Sum256(b); return hex.EncodeToString(s[:]) } // toolArchive is a .tar.gz holding one executable under root. func toolArchive(t *testing.T, root, bin string) []byte { t.Helper() var buf bytes.Buffer z := gzip.NewWriter(&buf) tw := tar.NewWriter(z) body := []byte("#!/bin/sh\n") if err := tw.WriteHeader(&tar.Header{Name: root + "/" + bin, Mode: 0755, Size: int64(len(body)), Typeflag: tar.TypeReg}); err != nil { t.Fatal(err) } _, _ = tw.Write(body) _ = tw.Close() _ = z.Close() return buf.Bytes() } func (x *macRig) install() (int, string, string) { var out, errout bytes.Buffer code := installMac(context.Background(), []string{"--release", "abc1234", "--origin", "https://equipo.example", "--instance", "team1"}, x.h, &out, &errout) return code, out.String(), errout.String() } // The installer refuses before touching anything: not a Mac, root, an // administrator, a user without equipo in its name, a home of another person // this user can open. func TestInstallMacRefusals(t *testing.T) { for name, c := range map[string]struct { set func(*macRig) want string }{ "linux": {func(x *macRig) { x.h.goos = "linux" }, "runner.mac.platform"}, "intel": {func(x *macRig) { x.h.goarch = "amd64" }, "runner.mac.platform"}, "root": {func(x *macRig) { x.ids["-u"], x.ids["-un"] = "0\n", "root\n" }, "runner.mac.root"}, "administrator": {func(x *macRig) { x.ids["-Gn"] = "staff admin _developer\n" }, "runner.mac.admin"}, "own account": {func(x *macRig) { x.ids["-un"] = "paul\n" }, "runner.mac.user"}, "readable home": {func(x *macRig) { x.readable["paul"] = true }, "runner.mac.home"}, "homes unknown": {func(x *macRig) { x.h.users = filepath.Join(x.home, "missing") }, "runner.mac.homes_unknown"}, "another's home": {func(x *macRig) { x.dsclHome = filepath.Join(filepath.Dir(x.home), "paul") }, "runner.install.destination"}, "no home": {func(x *macRig) { x.dsclHome = "" }, "runner.install.destination"}, "logged out": {func(x *macRig) { x.loggedOut = true }, "runner.mac.not_logged_in"}, } { x := newMacRig(t) c.set(x) code, _, errout := x.install() want := equipo.RunnerSetupCopy(c.want) switch c.want { case "runner.mac.home": want = fmt.Sprintf(want, filepath.Join(filepath.Dir(x.home), "paul"), "equipo-paul") case "runner.mac.not_logged_in": want = "Log in as equipo-paul on this Mac (fast user switching) and keep it logged in, then run this again.\n" } if code != 1 || !strings.Contains(errout, want) || x.enrolled != nil || len(x.calls) != 0 || len(x.fetched) != 0 { t.Fatalf("%s: code %d %q calls %v", name, code, errout, x.calls) } if _, err := os.Stat(filepath.Join(x.home, ".local")); !os.IsNotExist(err) { t.Fatalf("%s: installed something", name) } } } // One command: the verified runner copied into ~/.local/lib, srt // installed from the pinned lockfile, a private env file, the link // printed for Paul to tap (enrolled as a worker named for the Mac), // then the launchd agent written and loaded. func TestInstallMac(t *testing.T) { x := newMacRig(t) code, out, errout := x.install() if code != 0 { t.Fatalf("code %d: %s", code, errout) } lib := filepath.Join(x.home, ".local/lib/equipo-runner") if raw, _ := os.ReadFile(filepath.Join(lib, "equipo-runner")); string(raw) != "#!verified runner\n" { t.Fatal("runner not installed") } if !strings.Contains(out, "(https://equipo.example/runners/enrol?code=ABCDEFGHIJ)") || !strings.Contains(errout, equipo.RunnerSetupCopy("runner.mac.tap")) || !strings.Contains(errout, equipo.RunnerSetupCopy("runner.mac.installed")) { t.Fatalf("out %q err %q", out, errout) } env := filepath.Join(x.home, ".config/equipo-runner/runner.env") values, err := runnersetup.ReadEnvFile(env) if err != nil { t.Fatal(err) } share := filepath.Join(x.home, ".local/share/equipo-runner") for k, v := range map[string]string{"EQUIPO_ORIGIN": "https://equipo.example", "EQUIPO_INSTANCE_ID": "team1", "EQUIPO_RUNNER_STATE_DIR": share + "/state", "EQUIPO_SCRATCH_ROOT": share + "/scratch", "EQUIPO_WORKSPACE_ROOT": share + "/work", "EQUIPO_MACHINE_LABEL": "Tito Studio", "EQUIPO_SRT_PATH": lib + "/srt/node_modules/.bin/srt", "EQUIPO_NODE_PATH": lib + "/tools/node-v1/bin/node", "EQUIPO_RG_PATH": lib + "/tools/rg-1/rg", "EQUIPO_GIT_PATH": "/usr/bin/git"} { if values[k] != v { t.Fatalf("%s=%q, want %q", k, values[k], v) } } if x.enrolled == nil || !x.enrolled.WorkerOnly || x.enrolled.MachineLabel != "Tito Studio" || x.enrolled.StateDir != share+"/state" { t.Fatalf("enrolled %+v", x.enrolled) } plist := filepath.Join(x.home, "Library/LaunchAgents/io.amadan.equipo-runner.plist") raw, err := os.ReadFile(plist) if err != nil || !bytes.Equal(raw, mac.LaunchAgent(filepath.Join(lib, "equipo-runner"), env, filepath.Join(x.home, "Library/Logs/equipo-runner"))) { t.Fatalf("plist %v", err) } want := []string{"launchctl print gui/502/io.amadan.equipo-runner", "npm " + lib + "/tools/node-v1/bin/npm " + lib + "/srt", "firstLaunch", "launchctl bootstrap gui/502 " + plist} if !slices.Equal(x.calls, want) { t.Fatalf("calls %v", x.calls) } // A download whose digest is not the pin installs nothing of it: one // line, no srt, checks run outside the sandbox and say so. x = newMacRig(t) x.archives["https://rg.example/rg.tar.gz"] = toolArchive(t, "rg-1", "rg-tampered") if code, _, errout := x.install(); code != 0 || !strings.Contains(errout, equipo.RunnerSetupCopy("runner.mac.no_srt")) { t.Fatalf("tampered ripgrep: %d %q", code, errout) } values, _ = runnersetup.ReadEnvFile(filepath.Join(x.home, ".config/equipo-runner/runner.env")) if _, ok := values["EQUIPO_SRT_PATH"]; ok { t.Fatal("srt path without its ripgrep") } if _, err := os.Stat(filepath.Join(x.home, ".local/lib/equipo-runner/tools/rg-1")); !os.IsNotExist(err) { t.Fatal("an archive that is not the pin was unpacked") } } func TestEnvFileAndExits(t *testing.T) { for _, c := range []struct { in []string rest []string file string ok bool }{ {[]string{"run"}, []string{"run"}, "", true}, {[]string{"run", "--env-file", "/x/runner.env"}, []string{"run"}, "/x/runner.env", true}, {[]string{"probe", "--env-file", "/x/runner.env"}, []string{"probe"}, "/x/runner.env", true}, {[]string{"enrol", "--env-file", "/x/runner.env"}, nil, "", false}, {[]string{"run", "--env-file"}, nil, "", false}, {[]string{"run", "--env-file", "/a", "--env-file", "/b"}, nil, "", false}, } { rest, file, ok := cutEnvFile(c.in) if ok != c.ok || file != c.file || !slices.Equal(rest, c.rest) { t.Fatalf("%v: %v %q %v", c.in, rest, file, ok) } } if supersededExit("darwin") != 0 || supersededExit("linux") != 3 || mac.ExitUpdated == 0 { t.Fatal("exit statuses") } // The file is what the runner reads: a bad origin in it is reported. dir := t.TempDir() env := filepath.Join(dir, "runner.env") if err := os.WriteFile(env, []byte("EQUIPO_ORIGIN=\"http://example.com\"\n"), 0600); err != nil { t.Fatal(err) } var out, log bytes.Buffer if run(context.Background(), []string{"run", "--env-file", env}, strings.NewReader(""), &out, &log, func(string) string { return "" }) != 1 || !strings.Contains(log.String(), "EQUIPO_ORIGIN") { t.Fatalf("env file not read: %q", log.String()) } } // The line asking Paul to open the link comes only with a link, right // before it. A Mac whose enrolment is still valid keeps it and says it // is already on the team (Paul's Studio, 2026-11-11: the line came with // no link after it). func TestInstallMacLinkLine(t *testing.T) { tap, already, ready := equipo.RunnerSetupCopy("runner.mac.tap"), equipo.RunnerSetupCopy("runner.mac.already"), equipo.RunnerSetupCopy("runner.mac.installed") link := "[https://equipo.example/runners/enrol?code=ABCDEFGHIJ](https://equipo.example/runners/enrol?code=ABCDEFGHIJ)\nABCDEFGHIJ\n" install := func(x *macRig) string { var both bytes.Buffer if code := installMac(context.Background(), []string{"--release", "abc1234", "--origin", "https://equipo.example", "--instance", "team1"}, x.h, &both, &both); code != 0 { t.Fatalf("code %d: %s", code, both.String()) } return both.String() } kept := newMacRig(t) kept.h.enrol = func(_ context.Context, cfg runner.Config, _ io.Writer) error { cfg.EnrolNotify(runner.EnrolNotice{Kept: true}) return nil } if got := install(kept); strings.Contains(got, tap) || !strings.HasSuffix(got, already+"\n"+ready+"\n") { t.Fatalf("kept enrolment: %q", got) } if got := install(newMacRig(t)); strings.Count(got, tap) != 1 || !strings.Contains(got, tap+"\n"+link) || strings.Contains(got, already) { t.Fatalf("new enrolment: %q", got) } } // Installing again over a running runner stops it first: nothing is // replaced while it runs, and the enrolment check gets the state lock // the running runner held (here the test holds it until bootout). func TestInstallMacAgain(t *testing.T) { x := newMacRig(t) x.loaded = true state := filepath.Join(x.home, ".local/share/equipo-runner/state") held, err := runner.LockState(state) if err != nil { t.Fatal(err) } launchctl := x.h.launchctl x.h.launchctl = func(ctx context.Context, args ...string) error { if args[0] == "bootout" && held != nil { held.Close() held = nil } return launchctl(ctx, args...) } npmCI := x.h.npmCI x.h.npmCI = func(ctx context.Context, npm, prefix, home string) error { if held != nil { t.Error("dependencies replaced under a running runner") } return npmCI(ctx, npm, prefix, home) } x.h.enrol = func(_ context.Context, cfg runner.Config, _ io.Writer) error { lock, err := runner.LockState(cfg.StateDir) if err != nil { return err } return lock.Close() } if code, _, errout := x.install(); code != 0 { t.Fatalf("reinstall: %d %s", code, errout) } if held != nil { held.Close() } } // The instance's role decides: a Mac it enrolled as a primary stops the // installer, which says why and loads no agent. func TestInstallMacEnrolledAsPrimary(t *testing.T) { x := newMacRig(t) x.h.enrol = func(context.Context, runner.Config, io.Writer) error { return runner.ErrEnrolledAsPrimary } code, _, errout := x.install() if code != 1 || !strings.Contains(errout, equipo.RunnerSetupCopy("runner.needs_primary")) || slices.Contains(x.calls, "launchctl bootstrap gui/502 "+filepath.Join(x.home, "Library/LaunchAgents/io.amadan.equipo-runner.plist")) { t.Fatalf("%d %q %v", code, errout, x.calls) } } // A runner that does not stop changes nothing: a bootout that fails, an // agent launchd still has after the wait, or a state lock still held // (a runner started by hand) all stop the installer before it touches // anything, with words saying so. func TestInstallMacWhenTheRunnerWillNotStop(t *testing.T) { for name, set := range map[string]func(*macRig) error{ "bootout fails": func(x *macRig) error { x.loaded, x.bootoutErr = true, errors.New("Boot-out failed: 5"); return nil }, "agent stays": func(x *macRig) error { x.loaded, x.stuck = true, true; return nil }, "lock still held": func(x *macRig) error { x.loaded = true _, err := runner.LockState(filepath.Join(x.home, ".local/share/equipo-runner/state")) return err }, "held, not loaded": func(x *macRig) error { _, err := runner.LockState(filepath.Join(x.home, ".local/share/equipo-runner/state")) return err }, } { x := newMacRig(t) if err := set(x); err != nil { t.Fatal(err) } code, _, errout := x.install() if code != 1 || !strings.Contains(errout, equipo.RunnerSetupCopy("runner.mac.still_running")) { t.Fatalf("%s: %d %q", name, code, errout) } if _, err := os.Stat(filepath.Join(x.home, ".local/lib")); !os.IsNotExist(err) || len(x.fetched) != 0 || x.enrolled != nil { t.Fatalf("%s: changed something", name) } // A runner taken out of launchd that still holds its lock is // given back to launchd: nothing was changed. if x.loaded != (name != "held, not loaded") { t.Fatalf("%s: left unloaded: %v", name, x.calls) } // A refused bootout, the agent still there, is the answer: no // waiting on it. if name == "bootout fails" && strings.Join(x.calls[len(x.calls)-2:], ",") != "launchctl bootout gui/502/io.amadan.equipo-runner,launchctl print gui/502/io.amadan.equipo-runner" { t.Fatalf("waited after a failed bootout: %v", x.calls) } } } // What npm installed is checked against the pins compiled into the // runner: anything else is not the sandbox, said in one line, and no // srt path is written. func TestInstallMacVerifiesThePins(t *testing.T) { x := newMacRig(t) x.h.verify = func(string) error { return mac.ErrUnpinned } if code, _, errout := x.install(); code != 0 || !strings.Contains(errout, equipo.RunnerSetupCopy("runner.mac.no_srt")) { t.Fatalf("%d %q", code, errout) } values, _ := runnersetup.ReadEnvFile(filepath.Join(x.home, ".config/equipo-runner/runner.env")) if _, ok := values["EQUIPO_SRT_PATH"]; ok { t.Fatal("an unpinned srt configured") } } // The name macOS's Add User dialog gives "Paul Equipo" is accepted: its // home is /Users/paulequipo and the install goes ahead there. func TestInstallMacAddUserDialogName(t *testing.T) { x := newMacRig(t) home := filepath.Join(filepath.Dir(x.home), "paulequipo") if err := os.MkdirAll(home, 0700); err != nil { t.Fatal(err) } x.home, x.dsclHome, x.ids["-un"] = home, home, "paulequipo\n" if code, _, errout := x.install(); code != 0 { t.Fatalf("%d %q", code, errout) } if _, err := os.Stat(filepath.Join(home, ".local/lib/equipo-runner/equipo-runner")); err != nil { t.Fatal("not installed in paulequipo's home") } // The refusal says why in words. if got := equipo.RunnerSetupCopy("runner.mac.user"); got != "Equipo runs in its own Mac user so it can't reach your files. Create a Standard user with 'equipo' in its account name, then run this again." { t.Fatalf("refusal %q", got) } } // A readable home is closed for this user alone with an ACL, and the // message says how and how to undo it, with the same entry. func TestInstallMacHomeACLMessage(t *testing.T) { x := newMacRig(t) x.readable["paul"] = true _, _, errout := x.install() paul := filepath.Join(filepath.Dir(x.home), "paul") want := "This user can get into another person's home folder (" + paul + "). An administrator can close it with: sudo chmod +a \"equipo-paul deny list,search,readattr,readextattr,readsecurity\" " + paul + " (to undo it: sudo chmod -a \"equipo-paul deny list,search,readattr,readextattr,readsecurity\" " + paul + ")\n" if errout != want { t.Fatalf("message %q", errout) } } // Under su or sudo, HOME is the caller's: the installer uses the home the // user directory records, for itself and for npm and xcodebuild, and never // writes to the inherited one. func TestInstallMacIgnoresInheritedHome(t *testing.T) { x := newMacRig(t) caller := filepath.Join(filepath.Dir(x.home), "paul") t.Setenv("HOME", caller) t.Setenv("USER", "paul") if code, _, errout := x.install(); code != 0 { t.Fatalf("%d %q", code, errout) } if _, err := os.Stat(filepath.Join(x.home, ".local/lib/equipo-runner/equipo-runner")); err != nil { t.Fatal("not installed in the user's own home") } if entries, _ := os.ReadDir(caller); len(entries) != 0 { t.Fatalf("wrote to the caller's home: %v", entries) } if !slices.Equal(x.childHomes, []string{x.home, x.home}) { t.Fatalf("npm and xcodebuild ran with homes %v", x.childHomes) } env := userEnv([]string{"HOME=" + caller, "USER=paul", "LOGNAME=paul", "PATH=/opt/bin", "LANG=C"}, x.home, "equipo-paul", "PATH=/usr/bin") if !slices.Equal(env, []string{"LANG=C", "HOME=" + x.home, "USER=equipo-paul", "LOGNAME=equipo-paul", "PATH=/usr/bin"}) { t.Fatalf("env %v", env) } } // Enrolment says how it is going: a quiet line while it waits, each // failure it waits out, and at the end why it stopped, in words, on // stderr and in the install log. "Runner installation is unavailable." // never comes alone. func TestInstallMacEnrolmentInWords(t *testing.T) { unavailable := equipo.RunnerSetupCopy("runner.install.unavailable") for name, c := range map[string]struct { err error want []string bare bool }{ "expired": {runner.ErrEnrolmentExpired, []string{"The link expired before it was approved. Run the install command again for a new link."}, false}, "turned down": {runner.ErrEnrolmentRefused, []string{"Equipo turned this link down, so this machine was not added. Run the install command again for a new link."}, false}, "server": {&runner.EnrolAnswerError{Status: 503, Key: "service.unavailable"}, []string{"Adding this machine to Equipo failed: Equipo answered HTTP 503 (service.unavailable)", unavailable}, true}, } { t.Run(name, func(t *testing.T) { x := newMacRig(t) x.h.enrol = func(_ context.Context, cfg runner.Config, out io.Writer) error { _, _ = io.WriteString(out, "[https://equipo.example/runners/enrol?code=ABCDEFGHJK](https://equipo.example/runners/enrol?code=ABCDEFGHJK)\nABCDEFGHJK\n") cfg.EnrolNotify(runner.EnrolNotice{Waiting: true}) cfg.EnrolNotify(runner.EnrolNotice{Retry: &runner.EnrolAnswerError{Status: 502}}) return c.err } code, _, errout := x.install() want := "Waiting for you to approve…\nEquipo did not answer properly (Equipo answered HTTP 502). Trying again.\n" + strings.Join(c.want, "\n") + "\n" if code != 1 || !strings.HasSuffix(errout, want) { t.Fatalf("%d %q", code, errout) } if !c.bare && strings.Contains(errout, unavailable) { t.Fatal("the bare unavailable line") } log, _ := os.ReadFile(filepath.Join(x.home, "Library/Logs/equipo-runner/install.log")) for _, line := range append([]string{"Waiting for you to approve…", "Equipo answered HTTP 502"}, c.want...) { if !strings.Contains(string(log), line) { t.Fatalf("install log lacks %q:\n%s", line, log) } } }) } } func TestInstallMacScannerDigestPreservesExistingBinary(t *testing.T) { x := newMacRig(t) if code, _, errout := x.install(); code != 0 { t.Fatal("initial install", code, errout) } path := filepath.Join(x.home, ".local/lib/equipo-runner/gitleaks") if raw, err := os.ReadFile(path); err != nil || string(raw) != "scanner" { t.Fatal("scanner not bundled", err) } fetch := x.h.fetch x.h.fetch = func(ctx context.Context, url string, limit int64) ([]byte, error) { if strings.HasSuffix(url, "/gitleaks") { return []byte("tampered"), nil } return fetch(ctx, url, limit) } if code, _, _ := x.install(); code == 0 { t.Fatal("changed scanner installed") } if raw, err := os.ReadFile(path); err != nil || string(raw) != "scanner" { t.Fatal("bad scanner replaced installed bytes", err) } }