package main import ( "crypto/sha256" "fmt" "os" "path/filepath" "testing" "amadan.net/oficina/equipo/internal/release" "amadan.net/oficina/equipo/internal/runner" ) // verify-installed is what staging and the production cutover run before // they start a runner: every program the runner will start, at the paths // in its own configuration, must be the bytes this release names. One // changed program, one missing, or a build that recorded no tool proxy // refuses. func TestVerifyInstalledHoldsEveryProgramToTheRelease(t *testing.T) { m, cfg, paths := installedFixture(t) if err := verifyInstalled(m, cfg); err != nil { t.Fatalf("the release's own programs refused: %v", err) } for name, path := range paths { if err := os.WriteFile(*path, []byte("another "+name), 0700); err != nil { t.Fatal(err) } if verifyInstalled(m, cfg) == nil { t.Fatal("verified with another", name) } if err := os.Remove(*path); err != nil { t.Fatal(err) } if verifyInstalled(m, cfg) == nil { t.Fatal("verified with no", name) } if err := os.WriteFile(*path, []byte(name), 0700); err != nil { t.Fatal(err) } } if err := verifyInstalled(m, cfg); err != nil { t.Fatal(err) } // An OpenCode root that does not verify refuses (slice S5); one that // does is EQUIPO_OPENCODE_ROOT's, where this machine has one. cfg.OpenCodeRoot = t.TempDir() if verifyInstalled(m, cfg) == nil { t.Fatal("verified with an empty OpenCode root") } cfg.OpenCodeRoot = os.Getenv("EQUIPO_OPENCODE_ROOT") if err := verifyInstalled(m, cfg); err != nil { t.Fatalf("the pinned OpenCode refused: %v", err) } // This test binary recorded no tool proxy, like any build but a release. if verifyInstalled(release.BuildManifest(), cfg) == nil { t.Fatal("a build that recorded no tool proxy verified an installation") } } // installedFixture is a release's programs, each its own bytes, and the // preview scanner beside the runner with the pin linked into this build. func installedFixture(t *testing.T) (release.Manifest, runner.Config, map[string]*string) { t.Helper() dir := t.TempDir() m := release.BuildManifest() m.Binaries = nil cfg := runner.Config{Executable: filepath.Join(dir, "equipo-runner")} paths := map[string]*string{"bwrap": &cfg.BwrapPath, "claude": &cfg.ClaudePath, "codex": &cfg.CodexPath, "codex-code-mode-host": &cfg.CodexCodeModeHostPath, "equipo-tool-proxy": &cfg.ToolProxyPath} for name, version := range map[string]string{"bwrap": m.BwrapVersion, "claude": m.Claude, "codex": m.Codex, "codex-code-mode-host": m.Codex, "equipo-tool-proxy": m.LauncherVersion} { *paths[name] = filepath.Join(dir, name) if err := os.WriteFile(*paths[name], []byte(name), 0700); err != nil { t.Fatal(err) } m.Binaries = append(m.Binaries, release.BinaryDigest{Name: name, Version: version, SHA256: fmt.Sprintf("%x", sha256.Sum256([]byte(name)))}) } if err := os.WriteFile(filepath.Join(dir, "gitleaks"), []byte("scanner"), 0700); err != nil { t.Fatal(err) } old := scannerPin scannerPin = func() string { return fmt.Sprintf("%x", sha256.Sum256([]byte("scanner"))) } t.Cleanup(func() { scannerPin = old }) installedRenderer(t, dir) return m, cfg, paths } // The runner scans every preview with the gitleaks beside its own // executable, held to the hash linked into its build; without it every // preview fails (preview_scanner). An installation without that scanner, // with another one, or one that cannot run, does not verify; nor does a // build that linked no scanner hash. func TestVerifyInstalledHoldsTheScannerBesideTheRunner(t *testing.T) { m, cfg, _ := installedFixture(t) scanner := filepath.Join(filepath.Dir(cfg.Executable), "gitleaks") if err := verifyInstalled(m, cfg); err != nil { t.Fatalf("the release's own scanner refused: %v", err) } for name, change := range map[string]func() error{ "no scanner": func() error { return os.Remove(scanner) }, "another scanner": func() error { return os.WriteFile(scanner, []byte("another scanner"), 0700) }, "not executable": func() error { return os.Chmod(scanner, 0600) }, } { if err := change(); err != nil { t.Fatal(err) } if verifyInstalled(m, cfg) == nil { t.Fatal("verified with", name) } _ = os.Remove(scanner) if err := os.WriteFile(scanner, []byte("scanner"), 0700); err != nil { t.Fatal(err) } if err := verifyInstalled(m, cfg); err != nil { t.Fatalf("after %s, the restored scanner refused: %v", name, err) } } scannerPin = func() string { return "" } if verifyInstalled(m, cfg) == nil { t.Fatal("a build that linked no scanner hash verified an installation") } }