package mac import ( "context" "errors" "os" "os/exec" "path/filepath" "strings" "testing" ) // With a real `npm ci` of the shipped lockfile at hand // (EQUIPO_SRT_NODE_MODULES), the srt tree matches its pin. Skipped // otherwise: CI does not download packages. func TestSRTTreeRealInstall(t *testing.T) { dir := os.Getenv("EQUIPO_SRT_NODE_MODULES") if dir == "" { t.Skip("EQUIPO_SRT_NODE_MODULES not set") } sum, err := TreeSHA256(dir) if err != nil || sum != SRTTreeSHA256 { t.Fatalf("srt tree %s %v, pinned %s", sum, err, SRTTreeSHA256) } } // installedTools lays out an install dir as the installer leaves it, with // stand-in contents. func installedTools(t *testing.T) (string, Tools) { t.Helper() dir := t.TempDir() tools := PinnedTools(dir) pkg := filepath.Join(dir, "srt", "node_modules", "@anthropic-ai", "sandbox-runtime", "dist") for _, p := range []string{tools.Node, tools.RG, filepath.Join(pkg, "cli.js")} { if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(p, []byte("stand-in"), 0700); err != nil { t.Fatal(err) } } if err := os.MkdirAll(filepath.Dir(tools.SRT), 0700); err != nil { t.Fatal(err) } if err := os.Symlink(srtLink, tools.SRT); err != nil { t.Fatal(err) } return dir, tools } // Tools that are not exactly the pinned ones are never verified: stand-in // contents, another place, srt's link pointing elsewhere, a link inside // the package tree. func TestVerifyToolsRefuses(t *testing.T) { dir, tools := installedTools(t) if !errors.Is(VerifyTools(dir, tools), ErrUnpinned) { t.Fatal("stand-in tools verified") } other := tools other.RG = "/opt/homebrew/bin/rg" if !errors.Is(VerifyTools(dir, other), ErrUnpinned) { t.Fatal("ripgrep elsewhere verified") } if err := os.Remove(tools.SRT); err != nil { t.Fatal(err) } if err := os.Symlink("/tmp/fake-srt", tools.SRT); err != nil { t.Fatal(err) } if !errors.Is(VerifyTools(dir, tools), ErrUnpinned) { t.Fatal("srt link elsewhere verified") } if err := os.Symlink("/etc", filepath.Join(dir, "srt", "node_modules", "zod")); err != nil { t.Fatal(err) } if _, err := TreeSHA256(filepath.Join(dir, "srt", "node_modules")); !errors.Is(err, ErrUnpinned) { t.Fatalf("a link in the package tree: %v", err) } } // With the real archives and a real `npm ci` at hand, the installed // tools verify; a counterfeit srt (one byte of cli.js changed), Node or // ripgrep does not. func TestVerifyToolsReal(t *testing.T) { archives, modules := os.Getenv("EQUIPO_TOOL_ARCHIVES"), os.Getenv("EQUIPO_SRT_NODE_MODULES") if archives == "" || modules == "" { t.Skip("EQUIPO_TOOL_ARCHIVES and EQUIPO_SRT_NODE_MODULES not set") } dir := t.TempDir() for _, tool := range SRTTools { if _, err := Fetch(context.Background(), tool, filepath.Join(dir, "tools"), func(_ context.Context, url string, _ int64) ([]byte, error) { return os.ReadFile(filepath.Join(archives, url[strings.LastIndex(url, "/")+1:])) }); err != nil { t.Fatal(err) } } if err := os.MkdirAll(filepath.Join(dir, "srt"), 0700); err != nil { t.Fatal(err) } if out, err := exec.Command("cp", "-a", modules, filepath.Join(dir, "srt", "node_modules")).CombinedOutput(); err != nil { t.Fatalf("%v %s", err, out) } tools := PinnedTools(dir) if err := VerifyTools(dir, tools); err != nil { t.Fatal(err) } // The same ripgrep somewhere else, srt's link to the same file by // another path: not the installed tools. elsewhere := filepath.Join(t.TempDir(), "rg") if out, err := exec.Command("cp", tools.RG, elsewhere).CombinedOutput(); err != nil { t.Fatalf("%v %s", err, out) } moved := tools moved.RG = elsewhere if !errors.Is(VerifyTools(dir, moved), ErrUnpinned) { t.Fatal("ripgrep elsewhere verified") } cli := filepath.Join(dir, "srt", "node_modules", "@anthropic-ai", "sandbox-runtime", "dist", "cli.js") if err := os.Remove(tools.SRT); err != nil { t.Fatal(err) } if err := os.Symlink(cli, tools.SRT); err != nil { t.Fatal(err) } if !errors.Is(VerifyTools(dir, tools), ErrUnpinned) { t.Fatal("srt linked another way verified") } if err := os.Remove(tools.SRT); err != nil { t.Fatal(err) } if err := os.Symlink(srtLink, tools.SRT); err != nil { t.Fatal(err) } raw, _ := os.ReadFile(cli) raw[len(raw)/2] ^= 1 if err := os.WriteFile(cli, raw, 0600); err != nil { t.Fatal(err) } if !errors.Is(VerifyTools(dir, tools), ErrUnpinned) { t.Fatal("a counterfeit srt verified") } }