oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "crypto/sha256"
5 "fmt"
6 "os"
7 "path/filepath"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/release"
11 "amadan.net/oficina/equipo/internal/runner"
12)
13
14// verify-installed is what staging and the production cutover run before
15// they start a runner: every program the runner will start, at the paths
16// in its own configuration, must be the bytes this release names. One
17// changed program, one missing, or a build that recorded no tool proxy
18// refuses.
19func TestVerifyInstalledHoldsEveryProgramToTheRelease(t *testing.T) {
20 m, cfg, paths := installedFixture(t)
21 if err := verifyInstalled(m, cfg); err != nil {
22 t.Fatalf("the release's own programs refused: %v", err)
23 }
24 for name, path := range paths {
25 if err := os.WriteFile(*path, []byte("another "+name), 0700); err != nil {
26 t.Fatal(err)
27 }
28 if verifyInstalled(m, cfg) == nil {
29 t.Fatal("verified with another", name)
30 }
31 if err := os.Remove(*path); err != nil {
32 t.Fatal(err)
33 }
34 if verifyInstalled(m, cfg) == nil {
35 t.Fatal("verified with no", name)
36 }
37 if err := os.WriteFile(*path, []byte(name), 0700); err != nil {
38 t.Fatal(err)
39 }
40 }
41 if err := verifyInstalled(m, cfg); err != nil {
42 t.Fatal(err)
43 }
44 // An OpenCode root that does not verify refuses (slice S5); one that
45 // does is EQUIPO_OPENCODE_ROOT's, where this machine has one.
46 cfg.OpenCodeRoot = t.TempDir()
47 if verifyInstalled(m, cfg) == nil {
48 t.Fatal("verified with an empty OpenCode root")
49 }
50 cfg.OpenCodeRoot = os.Getenv("EQUIPO_OPENCODE_ROOT")
51 if err := verifyInstalled(m, cfg); err != nil {
52 t.Fatalf("the pinned OpenCode refused: %v", err)
53 }
54 // This test binary recorded no tool proxy, like any build but a release.
55 if verifyInstalled(release.BuildManifest(), cfg) == nil {
56 t.Fatal("a build that recorded no tool proxy verified an installation")
57 }
58}
59
60// installedFixture is a release's programs, each its own bytes, and the
61// preview scanner beside the runner with the pin linked into this build.
62func installedFixture(t *testing.T) (release.Manifest, runner.Config, map[string]*string) {
63 t.Helper()
64 dir := t.TempDir()
65 m := release.BuildManifest()
66 m.Binaries = nil
67 cfg := runner.Config{Executable: filepath.Join(dir, "equipo-runner")}
68 paths := map[string]*string{"bwrap": &cfg.BwrapPath, "claude": &cfg.ClaudePath, "codex": &cfg.CodexPath, "codex-code-mode-host": &cfg.CodexCodeModeHostPath, "equipo-tool-proxy": &cfg.ToolProxyPath}
69 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} {
70 *paths[name] = filepath.Join(dir, name)
71 if err := os.WriteFile(*paths[name], []byte(name), 0700); err != nil {
72 t.Fatal(err)
73 }
74 m.Binaries = append(m.Binaries, release.BinaryDigest{Name: name, Version: version, SHA256: fmt.Sprintf("%x", sha256.Sum256([]byte(name)))})
75 }
76 if err := os.WriteFile(filepath.Join(dir, "gitleaks"), []byte("scanner"), 0700); err != nil {
77 t.Fatal(err)
78 }
79 old := scannerPin
80 scannerPin = func() string { return fmt.Sprintf("%x", sha256.Sum256([]byte("scanner"))) }
81 t.Cleanup(func() { scannerPin = old })
82 installedRenderer(t, dir)
83 return m, cfg, paths
84}
85
86// The runner scans every preview with the gitleaks beside its own
87// executable, held to the hash linked into its build; without it every
88// preview fails (preview_scanner). An installation without that scanner,
89// with another one, or one that cannot run, does not verify; nor does a
90// build that linked no scanner hash.
91func TestVerifyInstalledHoldsTheScannerBesideTheRunner(t *testing.T) {
92 m, cfg, _ := installedFixture(t)
93 scanner := filepath.Join(filepath.Dir(cfg.Executable), "gitleaks")
94 if err := verifyInstalled(m, cfg); err != nil {
95 t.Fatalf("the release's own scanner refused: %v", err)
96 }
97 for name, change := range map[string]func() error{
98 "no scanner": func() error { return os.Remove(scanner) },
99 "another scanner": func() error { return os.WriteFile(scanner, []byte("another scanner"), 0700) },
100 "not executable": func() error { return os.Chmod(scanner, 0600) },
101 } {
102 if err := change(); err != nil {
103 t.Fatal(err)
104 }
105 if verifyInstalled(m, cfg) == nil {
106 t.Fatal("verified with", name)
107 }
108 _ = os.Remove(scanner)
109 if err := os.WriteFile(scanner, []byte("scanner"), 0700); err != nil {
110 t.Fatal(err)
111 }
112 if err := verifyInstalled(m, cfg); err != nil {
113 t.Fatalf("after %s, the restored scanner refused: %v", name, err)
114 }
115 }
116 scannerPin = func() string { return "" }
117 if verifyInstalled(m, cfg) == nil {
118 t.Fatal("a build that linked no scanner hash verified an installation")
119 }
120}
121