oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package opencode
2
3import (
4 "archive/tar"
5 "bytes"
6 "compress/gzip"
7 "context"
8 "crypto/sha256"
9 "crypto/sha512"
10 "encoding/base64"
11 "encoding/hex"
12 "encoding/json"
13 "errors"
14 "os"
15 "path/filepath"
16 "testing"
17
18 "amadan.net/oficina/equipo/internal/runner/provider"
19)
20
21// The committed package.json asks for @opencode/cli alone; the lockfile
22// pins it at Version with the integrity npm published, and every
23// platform package it lists (the supported ones among them) carries an
24// integrity and the registry's URL.
25func TestLockfilePinsOpenCode(t *testing.T) {
26 pkg, _ := Package()
27 var p struct {
28 Dependencies map[string]string `json:"dependencies"`
29 }
30 if err := json.Unmarshal(pkg, &p); err != nil || p.Dependencies[PackageName] != Version || len(p.Dependencies) != 1 {
31 t.Fatalf("package.json: %v %v", p.Dependencies, err)
32 }
33 if !Pinned() {
34 t.Fatal("the lockfile does not pin OpenCode")
35 }
36 for name, l := range lockfile() {
37 if name == "" {
38 continue
39 }
40 if len(l.Integrity) <= len("sha512-") || l.Resolved == "" || l.Version != Version {
41 t.Errorf("%s is not pinned: %+v", name, l)
42 }
43 }
44 for _, goarch := range []string{"amd64", "arm64"} {
45 tree, platform, ok := Pins(goarch)
46 if !ok || len(tree) != 64 || len(platform.BinarySHA256) != 64 {
47 t.Fatalf("%s pins: %q %+v", goarch, tree, platform)
48 }
49 if _, ok := Lock(platform.Package); !ok {
50 t.Fatalf("%s's package %s is not in the lockfile", goarch, platform.Package)
51 }
52 if _, ok := CommittedPins(goarch); !ok {
53 t.Fatalf("%s: no committed pins", goarch)
54 }
55 }
56 if _, _, ok := Pins("riscv64"); ok {
57 t.Fatal("an unsupported platform has pins")
58 }
59}
60
61type entry struct {
62 name, body string
63 mode int64
64 kind byte
65 link string
66}
67
68func tarball(t *testing.T, entries []entry) []byte {
69 t.Helper()
70 var buf bytes.Buffer
71 gz := gzip.NewWriter(&buf)
72 tw := tar.NewWriter(gz)
73 for _, e := range entries {
74 h := &tar.Header{Name: e.name, Mode: e.mode, Size: int64(len(e.body)), Typeflag: e.kind, Linkname: e.link}
75 if e.kind == 0 {
76 h.Typeflag = tar.TypeReg
77 }
78 if h.Typeflag != tar.TypeReg {
79 h.Size = 0
80 }
81 if err := tw.WriteHeader(h); err != nil {
82 t.Fatal(err)
83 }
84 if h.Typeflag == tar.TypeReg {
85 tw.Write([]byte(e.body))
86 }
87 }
88 tw.Close()
89 gz.Close()
90 return buf.Bytes()
91}
92
93func integrity(raw []byte) string {
94 s := sha512.Sum512(raw)
95 return "sha512-" + base64.StdEncoding.EncodeToString(s[:])
96}
97
98// fakePins are pins for two synthetic packages, served by fetch.
99func fakePins(t *testing.T, cli, bin []byte) (InstallPins, Fetch, *int) {
100 t.Helper()
101 calls := 0
102 served := map[string][]byte{"https://registry.example/cli.tgz": cli, "https://registry.example/bin.tgz": bin}
103 fetch := func(_ context.Context, url string, limit int64) ([]byte, error) {
104 calls++
105 raw, ok := served[url]
106 if !ok || int64(len(raw)) > limit {
107 return nil, errors.New("no such file")
108 }
109 return raw, nil
110 }
111 sum := sha256.Sum256([]byte("a binary"))
112 pins := InstallPins{Platform: Platform{Package: "@opencode/cli-test", BinarySHA256: hex.EncodeToString(sum[:])}, Packages: map[string]Locked{
113 PackageName: {Version: Version, Resolved: "https://registry.example/cli.tgz", Integrity: integrity(cli)},
114 "@opencode/cli-test": {Version: Version, Resolved: "https://registry.example/bin.tgz", Integrity: integrity(bin)},
115 }}
116 return pins, fetch, &calls
117}
118
119var goodCLI = []entry{{name: "package/package.json", body: `{"name":"@opencode/cli"}`, mode: 0644}, {name: "package/bin/opencode.exe", body: "#!/bin/sh\nexit 1\n", mode: 0755}}
120var goodBin = []entry{{name: "package/package.json", body: `{"name":"@opencode/cli-test"}`, mode: 0644}, {name: "package/bin/", kind: tar.TypeDir, mode: 0755}, {name: "package/bin/opencode", body: "a binary", mode: 0755}}
121
122// The installer fetches exactly the lockfile's two packages, refuses one
123// whose bytes are not the lockfile's, unpacks regular files and
124// directories only, never a link or a path out of the package, seals the
125// tree and holds it and the binary to their digests, keeps a verified
126// install, and removes one that does not verify.
127func TestInstallFromArchives(t *testing.T) {
128 ctx := context.Background()
129 cli, bin := tarball(t, goodCLI), tarball(t, goodBin)
130 pins, fetch, calls := fakePins(t, cli, bin)
131 // The digest of what the install makes, then the install held to it.
132 probe := filepath.Join(t.TempDir(), "probe")
133 if err := InstallWith(ctx, probe, pins, fetch); !errors.Is(err, ErrUnpinned) {
134 t.Fatalf("an install with no tree pin verified: %v", err)
135 }
136 if _, err := os.Stat(filepath.Join(probe, "opencode", "node_modules")); !os.IsNotExist(err) {
137 t.Fatalf("an install that did not verify was left: %v", err)
138 }
139 scratch := filepath.Join(t.TempDir(), "node_modules")
140 for name, raw := range map[string][]byte{PackageName: cli, "@opencode/cli-test": bin} {
141 if err := unpack(raw, filepath.Join(scratch, filepath.FromSlash(name))); err != nil {
142 t.Fatal(err)
143 }
144 }
145 t.Cleanup(func() { removeTree(scratch) })
146 if err := provider.SealTree(scratch); err != nil {
147 t.Fatal(err)
148 }
149 pins.Tree, _ = provider.TreeSHA256(scratch)
150 root := filepath.Join(t.TempDir(), "root")
151 t.Cleanup(func() { removeTree(root) })
152 *calls = 0
153 if err := InstallWith(ctx, root, pins, fetch); err != nil {
154 t.Fatal(err)
155 }
156 if *calls != 2 {
157 t.Fatalf("%d fetches", *calls)
158 }
159 tree, binary := Paths(root, pins.Platform.Package)
160 if info, err := os.Stat(binary); err != nil || info.Mode().Perm()&0222 != 0 || info.Mode().Perm()&0111 == 0 {
161 t.Fatalf("binary %v %v", info, err)
162 }
163 if VerifyWith(root, pins) != nil {
164 t.Fatal("the install does not verify")
165 }
166 // Installed and verified: kept, nothing fetched.
167 *calls = 0
168 if err := InstallWith(ctx, root, pins, fetch); err != nil || *calls != 0 {
169 t.Fatalf("reinstall: %v, %d fetches", err, *calls)
170 }
171 // Another binary digest, or another tree, does not verify.
172 other := pins
173 other.Platform.BinarySHA256 = pins.Tree
174 if VerifyWith(root, other) == nil {
175 t.Fatal("another binary pin verified")
176 }
177 other = pins
178 other.Tree = pins.Platform.BinarySHA256
179 if VerifyWith(root, other) == nil {
180 t.Fatal("another tree pin verified")
181 }
182 // A file changed in the tree after the install.
183 os.Chmod(filepath.Dir(binary), 0755)
184 os.Chmod(binary, 0755)
185 os.WriteFile(filepath.Join(tree, "@opencode", "cli", "package.json"), []byte("{}"), 0644)
186 if VerifyWith(root, pins) == nil {
187 t.Fatal("a changed tree verified")
188 }
189 for name, bad := range map[string]struct {
190 cli, bin []byte
191 tamper bool
192 }{
193 "bytes that are not the lockfile's": {cli, bin, true},
194 "a symlink": {cli, tarball(t, append(goodBin, entry{name: "package/bin/link", kind: tar.TypeSymlink, link: "/etc/passwd"})), false},
195 "a hard link": {cli, tarball(t, append(goodBin, entry{name: "package/bin/hard", kind: tar.TypeLink, link: "package/bin/opencode"})), false},
196 "a path out of the package": {cli, tarball(t, append(goodBin, entry{name: "package/../../evil", body: "x", mode: 0644})), false},
197 "an absolute path": {cli, tarball(t, append(goodBin, entry{name: "/etc/evil", body: "x", mode: 0644})), false},
198 "a path outside package/": {cli, tarball(t, append(goodBin, entry{name: "other/evil", body: "x", mode: 0644})), false},
199 "a device": {cli, tarball(t, append(goodBin, entry{name: "package/dev", kind: tar.TypeChar})), false},
200 } {
201 p, f, _ := fakePins(t, bad.cli, bad.bin)
202 p.Tree = pins.Tree
203 if bad.tamper {
204 l := p.Packages["@opencode/cli-test"]
205 l.Integrity = integrity([]byte("other bytes"))
206 p.Packages["@opencode/cli-test"] = l
207 }
208 dir := filepath.Join(t.TempDir(), "root")
209 if err := InstallWith(ctx, dir, p, f); err == nil {
210 t.Errorf("%s: installed", name)
211 }
212 if _, err := os.Stat(filepath.Join(dir, "evil")); err == nil {
213 t.Errorf("%s: wrote outside the package", name)
214 }
215 if tr, _ := Paths(dir, p.Platform.Package); func() bool { _, err := os.Stat(tr); return err == nil }() {
216 t.Errorf("%s: a tree was left", name)
217 }
218 removeTree(dir)
219 }
220}
221
222// The real pinned install, where a runner (or CI's box) has one: it
223// verifies against this platform's committed pins.
224func TestPinnedInstallVerifies(t *testing.T) {
225 root := os.Getenv("EQUIPO_OPENCODE_ROOT")
226 if root == "" {
227 t.Skip("no pinned OpenCode install (EQUIPO_OPENCODE_ROOT)")
228 }
229 if err := Verify(root); err != nil {
230 t.Fatalf("the pinned install does not verify: %v", err)
231 }
232}
233