| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "archive/tar" |
| 5 | "bytes" |
| 6 | "compress/gzip" |
| 7 | "crypto/sha256" |
| 8 | "encoding/hex" |
| 9 | "encoding/json" |
| 10 | "fmt" |
| 11 | "io" |
| 12 | "os" |
| 13 | "os/exec" |
| 14 | "path/filepath" |
| 15 | "slices" |
| 16 | "strings" |
| 17 | "testing" |
| 18 | "time" |
| 19 | |
| 20 | "amadan.net/oficina/equipo/internal/testci" |
| 21 | "amadan.net/oficina/equipo/internal/wire" |
| 22 | ) |
| 23 | |
| 24 | // debMember is one entry of a fixture deb's data.tar: a file (body), a |
| 25 | // link (link) or a directory (dir). |
| 26 | type debMember struct { |
| 27 | name, body, link string |
| 28 | mode int64 |
| 29 | dir bool |
| 30 | } |
| 31 | |
| 32 | // fixtureDeb is a small Debian package as dpkg-deb builds one: an ar |
| 33 | // archive of debian-binary, control.tar.gz and data.tar.gz. Its members |
| 34 | // carry a build machine's owner and times, which the tree must not keep. |
| 35 | func fixtureDeb(t *testing.T, members []debMember, control string) []byte { |
| 36 | t.Helper() |
| 37 | targz := func(members []debMember) []byte { |
| 38 | var b bytes.Buffer |
| 39 | gz := gzip.NewWriter(&b) |
| 40 | tw := tar.NewWriter(gz) |
| 41 | for _, m := range members { |
| 42 | h := &tar.Header{Name: m.name, Mode: m.mode, Uid: 1000, Gid: 1000, Uname: "builder", Gname: "builder", ModTime: time.Unix(1764000000, 0)} |
| 43 | switch { |
| 44 | case m.dir: |
| 45 | h.Typeflag = tar.TypeDir |
| 46 | case m.link != "": |
| 47 | h.Typeflag, h.Linkname = tar.TypeSymlink, m.link |
| 48 | default: |
| 49 | h.Typeflag, h.Size = tar.TypeReg, int64(len(m.body)) |
| 50 | } |
| 51 | if err := tw.WriteHeader(h); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | if _, err := io.WriteString(tw, m.body); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | } |
| 58 | if err := tw.Close(); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if err := gz.Close(); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | return b.Bytes() |
| 65 | } |
| 66 | var ar bytes.Buffer |
| 67 | ar.WriteString("!<arch>\n") |
| 68 | member := func(name string, body []byte) { |
| 69 | fmt.Fprintf(&ar, "%-16s%-12d%-6d%-6d%-8o%-10d`\n", name, 1764000000, 0, 0, 0o100644, len(body)) |
| 70 | ar.Write(body) |
| 71 | if len(body)%2 == 1 { |
| 72 | ar.WriteByte('\n') |
| 73 | } |
| 74 | } |
| 75 | member("debian-binary", []byte("2.0\n")) |
| 76 | member("control.tar.gz", targz([]debMember{{name: "./control", body: control, mode: 0o644}})) |
| 77 | member("data.tar.gz", targz(members)) |
| 78 | return ar.Bytes() |
| 79 | } |
| 80 | |
| 81 | // rendererFixture is a pin of three small packages (in the pin's sorted |
| 82 | // order) and their debs by URL: the browser, a fonts config whose links |
| 83 | // are absolute as Debian writes them (one top-level directory to |
| 84 | // another), and a loader. The fonts link's target comes from a later |
| 85 | // package; two packages ship the same directory and one identical file. |
| 86 | type rendererFixture struct { |
| 87 | packages map[string][]debMember |
| 88 | order []string |
| 89 | edit func(p *rendererPackage) |
| 90 | control func(name, control string) string |
| 91 | } |
| 92 | |
| 93 | func newRendererFixture() *rendererFixture { |
| 94 | return &rendererFixture{ |
| 95 | order: []string{"chromium-headless-shell", "fontconfig-config", "fonts-fixture", "libc6"}, |
| 96 | packages: map[string][]debMember{ |
| 97 | "chromium-headless-shell": { |
| 98 | {name: "./", dir: true, mode: 0o755}, |
| 99 | {name: "./usr/", dir: true, mode: 0o755}, |
| 100 | {name: "./usr/lib/", dir: true, mode: 0o755}, |
| 101 | {name: "./usr/lib/chromium/", dir: true, mode: 0o755}, |
| 102 | {name: "./usr/lib/chromium/chromium-headless-shell", body: "browser", mode: 0o755}, |
| 103 | // No ./usr/bin/ member: its directory is implied. |
| 104 | {name: "./usr/bin/chromium-headless-shell", link: "../lib/chromium/chromium-headless-shell", mode: 0o777}, |
| 105 | {name: "./usr/share/doc/shared/copyright", body: "same words", mode: 0o644}, |
| 106 | }, |
| 107 | "fontconfig-config": { |
| 108 | {name: "./etc/", dir: true, mode: 0o755}, |
| 109 | {name: "./etc/fonts/", dir: true, mode: 0o755}, |
| 110 | {name: "./etc/fonts/conf.d/", dir: true, mode: 0o755}, |
| 111 | {name: "./etc/fonts/conf.d/10-fixture.conf", link: "/usr/share/fontconfig/conf.avail/10-fixture.conf", mode: 0o777}, |
| 112 | }, |
| 113 | "fonts-fixture": { |
| 114 | {name: "./usr/share/fontconfig/conf.avail/10-fixture.conf", body: "<fontconfig/>", mode: 0o664}, |
| 115 | {name: "./usr/share/doc/shared/copyright", body: "same words", mode: 0o644}, |
| 116 | }, |
| 117 | "libc6": { |
| 118 | {name: "./usr/", dir: true, mode: 0o755}, |
| 119 | {name: "./usr/lib64/", dir: true, mode: 0o755}, |
| 120 | {name: "./usr/lib64/ld-linux-x86-64.so.2", body: "loader", mode: 0o4775}, |
| 121 | }, |
| 122 | }, |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // write puts the pin and its debs in dir and returns the pin's path and |
| 127 | // the URL-to-file map the script's fetch reads. |
| 128 | func (f *rendererFixture) write(t *testing.T, dir string) (string, map[string]string) { |
| 129 | t.Helper() |
| 130 | pin := rendererPin{Version: "1.0-1", Snapshots: map[string]string{"debian": "20990101T000000Z"}, Program: "usr/lib/chromium/chromium-headless-shell"} |
| 131 | urls := map[string]string{} |
| 132 | for _, name := range f.order { |
| 133 | // Its control file, as Debian's: the browser is built from the |
| 134 | // chromium source package, the others from their own. |
| 135 | control := "Package: " + name + "\nVersion: 1.0-1\nArchitecture: amd64\n" |
| 136 | if name == "chromium-headless-shell" { |
| 137 | control += "Source: chromium\n" |
| 138 | } |
| 139 | if f.control != nil { |
| 140 | control = f.control(name, control) |
| 141 | } |
| 142 | deb := fixtureDeb(t, f.packages[name], control) |
| 143 | sum := sha256.Sum256(deb) |
| 144 | p := rendererPackage{Package: name, Version: "1.0-1", Architecture: "amd64", |
| 145 | URL: fmt.Sprintf("https://snapshot.debian.org/archive/debian/20990101T000000Z/pool/main/%c/%s/%s_1.0-1_amd64.deb", name[0], name, name), |
| 146 | SHA256: hex.EncodeToString(sum[:]), Size: int64(len(deb)), Base: name == "libc6"} |
| 147 | path := filepath.Join(dir, name+".deb") |
| 148 | if err := os.WriteFile(path, deb, 0o600); err != nil { |
| 149 | t.Fatal(err) |
| 150 | } |
| 151 | if f.edit != nil { |
| 152 | f.edit(&p) |
| 153 | } |
| 154 | urls[p.URL] = path |
| 155 | pin.AMD64 = append(pin.AMD64, p) |
| 156 | } |
| 157 | raw, err := json.Marshal(pin) |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | path := filepath.Join(dir, "renderer.json") |
| 162 | if err = os.WriteFile(path, raw, 0o600); err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | return path, urls |
| 166 | } |
| 167 | |
| 168 | // buildRendererTree runs scripts/build-renderer-tree.py on the fixture, |
| 169 | // its fetch reading the fixture debs instead of the network. clock, when |
| 170 | // set, is what the script's time.time returns: the output must not |
| 171 | // depend on it. |
| 172 | func buildRendererTree(t *testing.T, pin string, urls map[string]string, out string, clock float64, extra ...string) (string, string, error) { |
| 173 | t.Helper() |
| 174 | fixture, err := json.Marshal(urls) |
| 175 | if err != nil { |
| 176 | t.Fatal(err) |
| 177 | } |
| 178 | const prelude = ` |
| 179 | import json, os, sys, time, types |
| 180 | if float(sys.argv[2]): |
| 181 | time.time = lambda: float(sys.argv[2]) |
| 182 | os.umask(0o077) |
| 183 | # The script's source bytes, compiled here: never a cached .pyc, which |
| 184 | # could be stale (same size, same mtime second) after an edit. |
| 185 | path = "../../scripts/build-renderer-tree.py" |
| 186 | with open(path, "rb") as f: |
| 187 | code = compile(f.read(), path, "exec", dont_inherit=True) |
| 188 | m = types.ModuleType("tree") |
| 189 | m.__file__ = path |
| 190 | sys.modules["tree"] = m |
| 191 | exec(code, m.__dict__) |
| 192 | urls = json.loads(sys.argv[1]) |
| 193 | m.fetch = lambda url: open(urls[url], "rb").read() |
| 194 | sys.exit(m.main(sys.argv[3:])) |
| 195 | ` |
| 196 | // Under a group-writable umask, so nothing the script keeps may take |
| 197 | // its mode from the umask (the verifier refuses a group-writable tree). |
| 198 | cmd := exec.Command("sh", append([]string{"-c", `umask 002 && exec python3 "$@"`, "sh", "-I", "-B", "-c", prelude, string(fixture), fmt.Sprint(clock), pin, out}, extra...)...) |
| 199 | cmd.Env = append(os.Environ(), "PYTHONDONTWRITEBYTECODE=1") |
| 200 | var stdout, stderr bytes.Buffer |
| 201 | cmd.Stdout, cmd.Stderr = &stdout, &stderr |
| 202 | err = cmd.Run() |
| 203 | return stdout.String(), stderr.String(), err |
| 204 | } |
| 205 | |
| 206 | // The release's renderer: every pinned deb unpacked into one tree, listed |
| 207 | // by MANIFEST (one line per entry, sorted by path), both in a gzip tar |
| 208 | // whose bytes depend only on the pin. A Debian absolute link becomes the |
| 209 | // same path relative to the link, so no entry points out of the tree. |
| 210 | func TestRendererTreeIsDeterministic(t *testing.T) { |
| 211 | testci.Files(t, "../../scripts/build-renderer-tree.py") |
| 212 | testci.Tools(t, "python3") |
| 213 | dir := t.TempDir() |
| 214 | pin, urls := newRendererFixture().write(t, dir) |
| 215 | first, second := filepath.Join(dir, "a", "renderer.tar.gz"), filepath.Join(dir, "b", "renderer.tar.gz") |
| 216 | for _, d := range []string{filepath.Dir(first), filepath.Dir(second)} { |
| 217 | if err := os.Mkdir(d, 0o700); err != nil { |
| 218 | t.Fatal(err) |
| 219 | } |
| 220 | } |
| 221 | pinA, stderr, err := buildRendererTree(t, pin, urls, first, 0) |
| 222 | if err != nil { |
| 223 | t.Fatalf("build: %v\n%s", err, stderr) |
| 224 | } |
| 225 | // Another clock and umask: the same bytes. |
| 226 | pinB, stderr, err := buildRendererTree(t, pin, urls, second, 4102444800) |
| 227 | if err != nil { |
| 228 | t.Fatalf("build again: %v\n%s", err, stderr) |
| 229 | } |
| 230 | a, _ := os.ReadFile(first) |
| 231 | b, _ := os.ReadFile(second) |
| 232 | if len(a) == 0 || !bytes.Equal(a, b) || pinA != pinB { |
| 233 | t.Fatalf("two builds of one pin differ: %d and %d bytes, pins %q %q", len(a), len(b), pinA, pinB) |
| 234 | } |
| 235 | // gzip: no file name, no time (RFC 1952: FLG, then MTIME). |
| 236 | if a[0] != 0x1f || a[1] != 0x8b || a[3] != 0 || !bytes.Equal(a[4:8], []byte{0, 0, 0, 0}) { |
| 237 | t.Fatalf("gzip header % x", a[:10]) |
| 238 | } |
| 239 | sum := func(s string) string { h := sha256.Sum256([]byte(s)); return hex.EncodeToString(h[:]) } |
| 240 | want := strings.Join([]string{ |
| 241 | "dir etc", |
| 242 | "dir etc/fonts", |
| 243 | "dir etc/fonts/conf.d", |
| 244 | "link etc/fonts/conf.d/10-fixture.conf ../../../usr/share/fontconfig/conf.avail/10-fixture.conf", |
| 245 | "dir usr", |
| 246 | "dir usr/bin", |
| 247 | "link usr/bin/chromium-headless-shell ../lib/chromium/chromium-headless-shell", |
| 248 | "dir usr/lib", |
| 249 | "dir usr/lib/chromium", |
| 250 | "file usr/lib/chromium/chromium-headless-shell " + sum("browser") + " 0755", |
| 251 | "dir usr/lib64", |
| 252 | "file usr/lib64/ld-linux-x86-64.so.2 " + sum("loader") + " 0755", |
| 253 | "dir usr/share", |
| 254 | "dir usr/share/doc", |
| 255 | "dir usr/share/doc/shared", |
| 256 | "file usr/share/doc/shared/copyright " + sum("same words") + " 0644", |
| 257 | "dir usr/share/fontconfig", |
| 258 | "dir usr/share/fontconfig/conf.avail", |
| 259 | "file usr/share/fontconfig/conf.avail/10-fixture.conf " + sum("<fontconfig/>") + " 0644", |
| 260 | }, "\n") + "\n" |
| 261 | |
| 262 | gz, err := gzip.NewReader(bytes.NewReader(a)) |
| 263 | if err != nil { |
| 264 | t.Fatal(err) |
| 265 | } |
| 266 | if gz.Name != "" || !gz.ModTime.IsZero() { |
| 267 | t.Fatalf("gzip header names %q at %v", gz.Name, gz.ModTime) |
| 268 | } |
| 269 | tr := tar.NewReader(gz) |
| 270 | var names []string |
| 271 | var manifest string |
| 272 | for { |
| 273 | h, err := tr.Next() |
| 274 | if err == io.EOF { |
| 275 | break |
| 276 | } |
| 277 | if err != nil { |
| 278 | t.Fatal(err) |
| 279 | } |
| 280 | // A directory's tar name ends in a slash; its path does not. |
| 281 | name := strings.TrimSuffix(h.Name, "/") |
| 282 | names = append(names, name) |
| 283 | if h.Uid != 0 || h.Gid != 0 || h.Uname != "" || h.Gname != "" || h.ModTime.Unix() != 0 { |
| 284 | t.Errorf("%s: owner %d:%d %q:%q, time %v", h.Name, h.Uid, h.Gid, h.Uname, h.Gname, h.ModTime) |
| 285 | } |
| 286 | body, _ := io.ReadAll(tr) |
| 287 | var line string |
| 288 | switch h.Typeflag { |
| 289 | case tar.TypeDir: |
| 290 | line = "dir " + name |
| 291 | if h.Mode != 0o755 { |
| 292 | t.Errorf("%s: mode %o", h.Name, h.Mode) |
| 293 | } |
| 294 | case tar.TypeSymlink: |
| 295 | line = "link " + h.Name + " " + h.Linkname |
| 296 | case tar.TypeReg: |
| 297 | if h.Name == "MANIFEST" { |
| 298 | manifest = string(body) |
| 299 | if h.Mode != 0o644 { |
| 300 | t.Errorf("MANIFEST mode %o", h.Mode) |
| 301 | } |
| 302 | continue |
| 303 | } |
| 304 | line = fmt.Sprintf("file %s %s %04o", h.Name, sum(string(body)), h.Mode) |
| 305 | default: |
| 306 | t.Errorf("%s: type %c", h.Name, h.Typeflag) |
| 307 | } |
| 308 | // Every entry is the MANIFEST line that names it. |
| 309 | if !strings.Contains(want, line+"\n") { |
| 310 | t.Errorf("tar entry %q is not in the MANIFEST", line) |
| 311 | } |
| 312 | } |
| 313 | if manifest != want { |
| 314 | t.Fatalf("MANIFEST:\n%s\nwant:\n%s", manifest, want) |
| 315 | } |
| 316 | if !slices.IsSorted(names) || len(names) != strings.Count(want, "\n")+1 || names[0] != "MANIFEST" { |
| 317 | t.Fatalf("tar entries %v", names) |
| 318 | } |
| 319 | // The script prints the MANIFEST's SHA-256: the pin the runner is linked with. |
| 320 | if pinA != sum(manifest)+"\n" { |
| 321 | t.Fatalf("printed %q, MANIFEST is %s", pinA, sum(manifest)) |
| 322 | } |
| 323 | |
| 324 | // --tree keeps the unpacked tree, its MANIFEST at the root. |
| 325 | tree := filepath.Join(dir, "tree") |
| 326 | if _, stderr, err = buildRendererTree(t, pin, urls, filepath.Join(dir, "c.tar.gz"), 0, "--tree", tree); err != nil { |
| 327 | t.Fatalf("build with --tree: %v\n%s", err, stderr) |
| 328 | } |
| 329 | kept, _ := os.ReadFile(filepath.Join(tree, "MANIFEST")) |
| 330 | link, _ := os.Readlink(filepath.Join(tree, "etc/fonts/conf.d/10-fixture.conf")) |
| 331 | if string(kept) != want || link != "../../../usr/share/fontconfig/conf.avail/10-fixture.conf" { |
| 332 | t.Fatalf("kept tree: MANIFEST %q, link %q", kept, link) |
| 333 | } |
| 334 | if font, err := os.ReadFile(filepath.Join(tree, "etc/fonts/conf.d/10-fixture.conf")); err != nil || string(font) != "<fontconfig/>" { |
| 335 | t.Fatalf("the kept tree's link does not reach its target: %q %v", font, err) |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // A deb that is not the pinned one, or a tree that would point outside |
| 340 | // itself, builds nothing: no tar, no scratch tree left beside it. |
| 341 | func TestRendererTreeRefuses(t *testing.T) { |
| 342 | testci.Files(t, "../../scripts/build-renderer-tree.py") |
| 343 | testci.Tools(t, "python3") |
| 344 | add := func(pkg string, m debMember) func(f *rendererFixture) { |
| 345 | return func(f *rendererFixture) { f.packages[pkg] = append(f.packages[pkg], m) } |
| 346 | } |
| 347 | for _, c := range []struct { |
| 348 | name, want string |
| 349 | change func(f *rendererFixture) |
| 350 | }{ |
| 351 | {"bad deb hash", "SHA-256", func(f *rendererFixture) { |
| 352 | f.edit = func(p *rendererPackage) { |
| 353 | if p.Package == "libc6" { |
| 354 | p.SHA256 = strings.Repeat("0", 64) |
| 355 | } |
| 356 | } |
| 357 | }}, |
| 358 | {"wrong deb size", "bytes", func(f *rendererFixture) { |
| 359 | f.edit = func(p *rendererPackage) { |
| 360 | if p.Package == "fonts-fixture" { |
| 361 | p.Size++ |
| 362 | } |
| 363 | } |
| 364 | }}, |
| 365 | {"not a snapshot URL", "want a snapshot.debian.org .deb URL", func(f *rendererFixture) { |
| 366 | f.edit = func(p *rendererPackage) { |
| 367 | p.URL = strings.Replace(p.URL, "https://snapshot.debian.org/", "http://snapshot.debian.org/", 1) |
| 368 | } |
| 369 | }}, |
| 370 | {"escaping link", "climbs out of the tree", add("libc6", debMember{name: "./usr/lib64/passwd", link: "../../../etc/passwd", mode: 0o777})}, |
| 371 | {"escaping through a link", "climbs out of the tree", func(f *rendererFixture) { |
| 372 | add("libc6", debMember{name: "./usr/up", link: "..", mode: 0o777})(f) |
| 373 | add("libc6", debMember{name: "./usr/out", link: "up/..", mode: 0o777})(f) |
| 374 | }}, |
| 375 | {"a link to a file with a slash", "is not a directory", add("libc6", debMember{name: "./usr/lib64/ld", link: "ld-linux-x86-64.so.2/", mode: 0o777})}, |
| 376 | {"a link to a file with /.", "is not a directory", add("libc6", debMember{name: "./usr/lib64/ld", link: "ld-linux-x86-64.so.2/.", mode: 0o777})}, |
| 377 | {"a slash through another link", "is not a directory", func(f *rendererFixture) { |
| 378 | add("libc6", debMember{name: "./usr/lib64/ld", link: "ld-linux-x86-64.so.2", mode: 0o777})(f) |
| 379 | add("libc6", debMember{name: "./usr/lib64/via", link: "ld/", mode: 0o777})(f) |
| 380 | }}, |
| 381 | {"the browser's link with a slash", "is not a directory", func(f *rendererFixture) { |
| 382 | f.packages["chromium-headless-shell"][5].link = "../lib/chromium/chromium-headless-shell/." |
| 383 | }}, |
| 384 | {"absolute link outside the tree", "names nothing in the tree", add("libc6", debMember{name: "./usr/lib64/sh", link: "/bin/sh", mode: 0o777})}, |
| 385 | {"absolute link climbing out", "climbs out of the tree", add("libc6", debMember{name: "./usr/lib64/root", link: "/../../etc", mode: 0o777})}, |
| 386 | {"path climbing out", "not a path inside the tree", add("libc6", debMember{name: "./usr/../../evil", body: "x", mode: 0o644})}, |
| 387 | {"absolute path", "not a path inside the tree", add("libc6", debMember{name: "/etc/evil", body: "x", mode: 0o644})}, |
| 388 | {"a path through a link", "through a link", func(f *rendererFixture) { |
| 389 | add("libc6", debMember{name: "./usr/lib64/host", link: "../lib", mode: 0o777})(f) |
| 390 | add("libc6", debMember{name: "./usr/lib64/host/evil", body: "x", mode: 0o644})(f) |
| 391 | }}, |
| 392 | {"two packages disagree", "disagree", add("libc6", debMember{name: "./usr/share/doc/shared/copyright", body: "other words", mode: 0o644})}, |
| 393 | {"a name the MANIFEST cannot hold", "not a path inside the tree", add("libc6", debMember{name: "./usr/two words", body: "x", mode: 0o644})}, |
| 394 | {"the tree's own MANIFEST", "MANIFEST", add("libc6", debMember{name: "./MANIFEST", body: "file x", mode: 0o644})}, |
| 395 | {"no program", "usr/lib/chromium/chromium-headless-shell", func(f *rendererFixture) { |
| 396 | f.packages["chromium-headless-shell"][4].mode = 0o644 |
| 397 | }}, |
| 398 | } { |
| 399 | t.Run(c.name, func(t *testing.T) { |
| 400 | t.Parallel() |
| 401 | dir := t.TempDir() |
| 402 | f := newRendererFixture() |
| 403 | c.change(f) |
| 404 | pin, urls := f.write(t, dir) |
| 405 | out := filepath.Join(dir, "out") |
| 406 | if err := os.Mkdir(out, 0o700); err != nil { |
| 407 | t.Fatal(err) |
| 408 | } |
| 409 | stdout, stderr, err := buildRendererTree(t, pin, urls, filepath.Join(out, "renderer.tar.gz"), 0) |
| 410 | if err == nil || !strings.Contains(stderr, c.want) || stdout != "" { |
| 411 | t.Fatalf("got %v, stdout %q, stderr %q; want a refusal naming %q", err, stdout, stderr, c.want) |
| 412 | } |
| 413 | if left, _ := os.ReadDir(out); len(left) != 0 { |
| 414 | t.Fatalf("a refused build left %v", left) |
| 415 | } |
| 416 | }) |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // A runner built without a renderer pin linked reports its renderer |
| 421 | // unpinned, whatever is on disk; so does a malformed pin. |
| 422 | func TestRendererPinUnpinned(t *testing.T) { |
| 423 | saved := rendererSHA256 |
| 424 | t.Cleanup(func() { rendererSHA256 = saved }) |
| 425 | pin := strings.Repeat("ab", 32) |
| 426 | for linked, want := range map[string][2]string{ |
| 427 | "": {"", wire.ToolUnpinned}, |
| 428 | "not a digest": {"", wire.ToolUnpinned}, |
| 429 | strings.ToUpper(pin): {"", wire.ToolUnpinned}, |
| 430 | pin: {pin, ""}, |
| 431 | } { |
| 432 | rendererSHA256 = linked |
| 433 | if got, health := RendererPin(); got != want[0] || health != want[1] { |
| 434 | t.Errorf("linked %q: got %q %q, want %q %q", linked, got, health, want[0], want[1]) |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // writeRendererTar writes a renderer.tar.gz holding only a MANIFEST with |
| 440 | // the given text, as packaging fixtures need one, and returns its SHA-256. |
| 441 | func writeRendererTar(t *testing.T, path, manifest string) string { |
| 442 | t.Helper() |
| 443 | var b bytes.Buffer |
| 444 | gz := gzip.NewWriter(&b) |
| 445 | tw := tar.NewWriter(gz) |
| 446 | if err := tw.WriteHeader(&tar.Header{Name: "MANIFEST", Mode: 0o644, Size: int64(len(manifest))}); err != nil { |
| 447 | t.Fatal(err) |
| 448 | } |
| 449 | if _, err := io.WriteString(tw, manifest); err != nil { |
| 450 | t.Fatal(err) |
| 451 | } |
| 452 | if err := tw.Close(); err != nil { |
| 453 | t.Fatal(err) |
| 454 | } |
| 455 | if err := gz.Close(); err != nil { |
| 456 | t.Fatal(err) |
| 457 | } |
| 458 | if err := os.WriteFile(path, b.Bytes(), 0o600); err != nil { |
| 459 | t.Fatal(err) |
| 460 | } |
| 461 | sum := sha256.Sum256([]byte(manifest)) |
| 462 | return hex.EncodeToString(sum[:]) |
| 463 | } |
| 464 | |
| 465 | // The build writes SOURCES beside the archive, from the same checked |
| 466 | // debs: the Chromium version and its source, and per package its version, |
| 467 | // architecture, deb and source package on snapshot.debian.org. |
| 468 | // renderer-sources.py check accepts it, and refuses it changed. |
| 469 | func TestRendererTreeWritesSources(t *testing.T) { |
| 470 | testci.Files(t, "../../scripts/build-renderer-tree.py", "../../scripts/renderer-sources.py") |
| 471 | testci.Tools(t, "python3") |
| 472 | dir := t.TempDir() |
| 473 | pin, urls := newRendererFixture().write(t, dir) |
| 474 | out := filepath.Join(dir, "out", "renderer.tar.gz") |
| 475 | os.Mkdir(filepath.Dir(out), 0o755) |
| 476 | if _, stderr, err := buildRendererTree(t, pin, urls, out, 0); err != nil { |
| 477 | t.Fatalf("build: %v\n%s", err, stderr) |
| 478 | } |
| 479 | sources := filepath.Join(dir, "out", "SOURCES") |
| 480 | raw, err := os.ReadFile(sources) |
| 481 | if err != nil { |
| 482 | t.Fatal(err) |
| 483 | } |
| 484 | snap := "https://snapshot.debian.org/archive/debian/20990101T000000Z/pool/main/" |
| 485 | want := "The page renderer: Chromium headless shell 1.0-1, from Debian.\n" + |
| 486 | "Chromium source: https://snapshot.debian.org/package/chromium/1.0-1/\n\n" + |
| 487 | "chromium-headless-shell 1.0-1 amd64 " + snap + "c/chromium-headless-shell/chromium-headless-shell_1.0-1_amd64.deb https://snapshot.debian.org/package/chromium/1.0-1/\n" + |
| 488 | "fontconfig-config 1.0-1 amd64 " + snap + "f/fontconfig-config/fontconfig-config_1.0-1_amd64.deb https://snapshot.debian.org/package/fontconfig-config/1.0-1/\n" + |
| 489 | "fonts-fixture 1.0-1 amd64 " + snap + "f/fonts-fixture/fonts-fixture_1.0-1_amd64.deb https://snapshot.debian.org/package/fonts-fixture/1.0-1/\n" + |
| 490 | "libc6 1.0-1 amd64 " + snap + "l/libc6/libc6_1.0-1_amd64.deb https://snapshot.debian.org/package/libc6/1.0-1/\n" |
| 491 | if string(raw) != want { |
| 492 | t.Fatalf("SOURCES:\n%s\nwant:\n%s", raw, want) |
| 493 | } |
| 494 | check := func(path string) error { |
| 495 | return exec.Command("python3", "-I", "../../scripts/renderer-sources.py", "check", pin, path).Run() |
| 496 | } |
| 497 | if err := check(sources); err != nil { |
| 498 | t.Fatalf("check refused the build's SOURCES: %v", err) |
| 499 | } |
| 500 | for name, edit := range map[string]func(string) string{ |
| 501 | "a package dropped": func(s string) string { return strings.Replace(s, "libc6 1.0-1", "", 1) }, |
| 502 | "another version": func(s string) string { return strings.Replace(s, "libc6 1.0-1 amd64", "libc6 1.0-2 amd64", 1) }, |
| 503 | "another header": func(s string) string { return strings.Replace(s, "shell 1.0-1,", "shell 2.0-1,", 1) }, |
| 504 | } { |
| 505 | bad := filepath.Join(dir, name) |
| 506 | os.WriteFile(bad, []byte(edit(want)), 0o600) |
| 507 | if check(bad) == nil { |
| 508 | t.Errorf("check accepted SOURCES with %s", name) |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // A deb whose control file is not the pinned package and version, or |
| 514 | // names no chromium source for the browser, is refused: SOURCES is never |
| 515 | // written from a guess, and neither is the archive. |
| 516 | func TestRendererTreeRefusesAnotherControl(t *testing.T) { |
| 517 | testci.Files(t, "../../scripts/build-renderer-tree.py", "../../scripts/renderer-sources.py") |
| 518 | testci.Tools(t, "python3") |
| 519 | for name, edit := range map[string]func(pkg, control string) string{ |
| 520 | "another version": func(pkg, c string) string { |
| 521 | if pkg == "libc6" { |
| 522 | return strings.Replace(c, "1.0-1", "1.0-9", 1) |
| 523 | } |
| 524 | return c |
| 525 | }, |
| 526 | "the browser from another source": func(pkg, c string) string { return strings.Replace(c, "Source: chromium", "Source: other", 1) }, |
| 527 | } { |
| 528 | t.Run(name, func(t *testing.T) { |
| 529 | dir := t.TempDir() |
| 530 | f := newRendererFixture() |
| 531 | f.control = edit |
| 532 | pin, urls := f.write(t, dir) |
| 533 | out := filepath.Join(dir, "renderer.tar.gz") |
| 534 | if _, _, err := buildRendererTree(t, pin, urls, out, 0); err == nil { |
| 535 | t.Fatal("built") |
| 536 | } |
| 537 | for _, p := range []string{out, filepath.Join(dir, "SOURCES")} { |
| 538 | if _, err := os.Stat(p); !os.IsNotExist(err) { |
| 539 | t.Fatalf("%s written: %v", p, err) |
| 540 | } |
| 541 | } |
| 542 | }) |
| 543 | } |
| 544 | } |
| 545 | |