| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // With a real `npm ci` of the shipped lockfile at hand |
| 14 | // (EQUIPO_SRT_NODE_MODULES), the srt tree matches its pin. Skipped |
| 15 | // otherwise: CI does not download packages. |
| 16 | func TestSRTTreeRealInstall(t *testing.T) { |
| 17 | dir := os.Getenv("EQUIPO_SRT_NODE_MODULES") |
| 18 | if dir == "" { |
| 19 | t.Skip("EQUIPO_SRT_NODE_MODULES not set") |
| 20 | } |
| 21 | sum, err := TreeSHA256(dir) |
| 22 | if err != nil || sum != SRTTreeSHA256 { |
| 23 | t.Fatalf("srt tree %s %v, pinned %s", sum, err, SRTTreeSHA256) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // installedTools lays out an install dir as the installer leaves it, with |
| 28 | // stand-in contents. |
| 29 | func installedTools(t *testing.T) (string, Tools) { |
| 30 | t.Helper() |
| 31 | dir := t.TempDir() |
| 32 | tools := PinnedTools(dir) |
| 33 | pkg := filepath.Join(dir, "srt", "node_modules", "@anthropic-ai", "sandbox-runtime", "dist") |
| 34 | for _, p := range []string{tools.Node, tools.RG, filepath.Join(pkg, "cli.js")} { |
| 35 | if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if err := os.WriteFile(p, []byte("stand-in"), 0700); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | } |
| 42 | if err := os.MkdirAll(filepath.Dir(tools.SRT), 0700); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if err := os.Symlink(srtLink, tools.SRT); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | return dir, tools |
| 49 | } |
| 50 | |
| 51 | // Tools that are not exactly the pinned ones are never verified: stand-in |
| 52 | // contents, another place, srt's link pointing elsewhere, a link inside |
| 53 | // the package tree. |
| 54 | func TestVerifyToolsRefuses(t *testing.T) { |
| 55 | dir, tools := installedTools(t) |
| 56 | if !errors.Is(VerifyTools(dir, tools), ErrUnpinned) { |
| 57 | t.Fatal("stand-in tools verified") |
| 58 | } |
| 59 | other := tools |
| 60 | other.RG = "/opt/homebrew/bin/rg" |
| 61 | if !errors.Is(VerifyTools(dir, other), ErrUnpinned) { |
| 62 | t.Fatal("ripgrep elsewhere verified") |
| 63 | } |
| 64 | if err := os.Remove(tools.SRT); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | if err := os.Symlink("/tmp/fake-srt", tools.SRT); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | if !errors.Is(VerifyTools(dir, tools), ErrUnpinned) { |
| 71 | t.Fatal("srt link elsewhere verified") |
| 72 | } |
| 73 | if err := os.Symlink("/etc", filepath.Join(dir, "srt", "node_modules", "zod")); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if _, err := TreeSHA256(filepath.Join(dir, "srt", "node_modules")); !errors.Is(err, ErrUnpinned) { |
| 77 | t.Fatalf("a link in the package tree: %v", err) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // With the real archives and a real `npm ci` at hand, the installed |
| 82 | // tools verify; a counterfeit srt (one byte of cli.js changed), Node or |
| 83 | // ripgrep does not. |
| 84 | func TestVerifyToolsReal(t *testing.T) { |
| 85 | archives, modules := os.Getenv("EQUIPO_TOOL_ARCHIVES"), os.Getenv("EQUIPO_SRT_NODE_MODULES") |
| 86 | if archives == "" || modules == "" { |
| 87 | t.Skip("EQUIPO_TOOL_ARCHIVES and EQUIPO_SRT_NODE_MODULES not set") |
| 88 | } |
| 89 | dir := t.TempDir() |
| 90 | for _, tool := range SRTTools { |
| 91 | if _, err := Fetch(context.Background(), tool, filepath.Join(dir, "tools"), func(_ context.Context, url string, _ int64) ([]byte, error) { |
| 92 | return os.ReadFile(filepath.Join(archives, url[strings.LastIndex(url, "/")+1:])) |
| 93 | }); err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | } |
| 97 | if err := os.MkdirAll(filepath.Join(dir, "srt"), 0700); err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | if out, err := exec.Command("cp", "-a", modules, filepath.Join(dir, "srt", "node_modules")).CombinedOutput(); err != nil { |
| 101 | t.Fatalf("%v %s", err, out) |
| 102 | } |
| 103 | tools := PinnedTools(dir) |
| 104 | if err := VerifyTools(dir, tools); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | // The same ripgrep somewhere else, srt's link to the same file by |
| 108 | // another path: not the installed tools. |
| 109 | elsewhere := filepath.Join(t.TempDir(), "rg") |
| 110 | if out, err := exec.Command("cp", tools.RG, elsewhere).CombinedOutput(); err != nil { |
| 111 | t.Fatalf("%v %s", err, out) |
| 112 | } |
| 113 | moved := tools |
| 114 | moved.RG = elsewhere |
| 115 | if !errors.Is(VerifyTools(dir, moved), ErrUnpinned) { |
| 116 | t.Fatal("ripgrep elsewhere verified") |
| 117 | } |
| 118 | cli := filepath.Join(dir, "srt", "node_modules", "@anthropic-ai", "sandbox-runtime", "dist", "cli.js") |
| 119 | if err := os.Remove(tools.SRT); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | if err := os.Symlink(cli, tools.SRT); err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | if !errors.Is(VerifyTools(dir, tools), ErrUnpinned) { |
| 126 | t.Fatal("srt linked another way verified") |
| 127 | } |
| 128 | if err := os.Remove(tools.SRT); err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | if err := os.Symlink(srtLink, tools.SRT); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | raw, _ := os.ReadFile(cli) |
| 135 | raw[len(raw)/2] ^= 1 |
| 136 | if err := os.WriteFile(cli, raw, 0600); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | if !errors.Is(VerifyTools(dir, tools), ErrUnpinned) { |
| 140 | t.Fatal("a counterfeit srt verified") |
| 141 | } |
| 142 | } |
| 143 | |