package release import ( "bytes" "crypto/sha256" "encoding/hex" "encoding/json" "fmt" "os" "os/exec" "path/filepath" "regexp" "slices" "strings" "testing" "amadan.net/oficina/equipo/internal/testci" ) // rendererPin is internal/release/renderer.json: the renderer's full // package closure for Linux amd64, data for the release build only. type rendererPin struct { Version string `json:"version"` Snapshots map[string]string `json:"snapshots"` Program string `json:"program"` AMD64 []rendererPackage `json:"amd64"` ARM64 json.RawMessage `json:"arm64,omitempty"` } type rendererPackage struct { Package string `json:"package"` Version string `json:"version"` Architecture string `json:"architecture"` URL string `json:"url"` SHA256 string `json:"sha256"` Size int64 `json:"size"` Base bool `json:"base"` } type ciChromiumPin struct { Version string `json:"version"` Snapshots map[string]string `json:"snapshots"` Program string `json:"program"` AMD64 []rendererPackage `json:"amd64"` } var pinDigest = regexp.MustCompile(`^[a-f0-9]{64}$`) // rendererPinProblems lists every way renderer disagrees with ci (both raw // JSON): the browser packages, those not marked base, must be exactly // ci-chromium.json's amd64 packages at the same versions, URLs and // digests; the rest are the Debian base the CI image already has, so they // are not in ci-chromium.json. Each entry is fetchable and checkable. func rendererPinProblems(ciRaw, rendererRaw []byte) []string { var ci ciChromiumPin var r rendererPin if err := json.Unmarshal(ciRaw, &ci); err != nil { return []string{"ci-chromium.json: " + err.Error()} } if err := json.Unmarshal(rendererRaw, &r); err != nil { return []string{"renderer.json: " + err.Error()} } var problems []string add := func(format string, args ...any) { problems = append(problems, fmt.Sprintf(format, args...)) } if r.Version != ci.Version || r.Program != ci.Program || fmt.Sprint(r.Snapshots) != fmt.Sprint(ci.Snapshots) { add("renderer pins %s %s %v, CI pins %s %s %v", r.Version, r.Program, r.Snapshots, ci.Version, ci.Program, ci.Snapshots) } if r.ARM64 != nil { add("renderer.json pins arm64; the renderer is Linux amd64 only") } ciByName := map[string]rendererPackage{} for _, p := range ci.AMD64 { ciByName[p.Package] = p } seen := map[string]bool{} var names []string base := false for _, p := range r.AMD64 { names = append(names, p.Package) if seen[p.Package] { add("%s pinned twice", p.Package) } seen[p.Package] = true if !strings.HasPrefix(p.URL, "https://snapshot.debian.org/archive/") || !strings.HasSuffix(p.URL, ".deb") || !pinDigest.MatchString(p.SHA256) || p.Size <= 0 || p.Version == "" || (p.Architecture != "amd64" && p.Architecture != "all") { add("%s: want a snapshot .deb URL, a SHA-256, a size, a version and amd64 or all, got %+v", p.Package, p) } want, browser := ciByName[p.Package] switch { case p.Base && browser: add("%s is a browser package in ci-chromium.json but marked base", p.Package) case !p.Base && !browser: add("%s %s is not in ci-chromium.json and not marked base", p.Package, p.Version) case browser && (p.Version != want.Version || p.URL != want.URL || p.SHA256 != want.SHA256 || p.Size != want.Size): add("%s: renderer pins %s %s %s, CI pins %s %s %s", p.Package, p.Version, p.URL, p.SHA256, want.Version, want.URL, want.SHA256) } base = base || p.Base } for _, p := range ci.AMD64 { if !seen[p.Package] { add("%s %s is in ci-chromium.json but not in renderer.json", p.Package, p.Version) } } if !slices.IsSorted(names) { add("renderer.json packages are not sorted by name") } if !base { add("renderer.json pins no base packages: the tree would need the host's") } return problems } // The renderer tree is the CI browser plus the base it needs: the same // browser packages at the same versions as scripts/ci-chromium.json // (one script writes both, so CI and runners move together), and the // loader and libc from the pin, not the host. func TestRendererPinMatchesCIChromium(t *testing.T) { ciRaw, err := os.ReadFile("../../scripts/ci-chromium.json") if err != nil { t.Fatal(err) } rendererRaw, err := os.ReadFile("renderer.json") if err != nil { t.Fatal(err) } for _, problem := range rendererPinProblems(ciRaw, rendererRaw) { t.Error(problem) } var r rendererPin if err = json.Unmarshal(rendererRaw, &r); err != nil { t.Fatal(err) } pinned := map[string]rendererPackage{} for _, p := range r.AMD64 { pinned[p.Package] = p } // No pruning of the base: the loader, libc and debconf come from the pin. for _, name := range []string{"libc6", "libgcc-s1", "debconf", "zlib1g"} { if p, ok := pinned[name]; !ok || !p.Base { t.Errorf("%s: want it pinned as base, got %+v (present %v)", name, p, ok) } } if p := pinned["chromium-headless-shell"]; p.Version != r.Version || p.Base { t.Errorf("chromium-headless-shell: want %s as a browser package, got %+v", r.Version, p) } // The check itself: an untouched copy passes, and each kind of drift // between the two files is reported as that drift. tamper := func(name, want string, edit func(r *rendererPin)) { t.Run(name, func(t *testing.T) { var copied rendererPin if err := json.Unmarshal(rendererRaw, &copied); err != nil { t.Fatal(err) } edit(&copied) raw, err := json.Marshal(copied) if err != nil { t.Fatal(err) } problems := rendererPinProblems(ciRaw, raw) if want == "" && len(problems) != 0 || want != "" && (len(problems) != 1 || !strings.Contains(problems[0], want)) { t.Errorf("got %q, want only a problem containing %q", problems, want) } }) } index := func(r *rendererPin, name string) int { i := slices.IndexFunc(r.AMD64, func(p rendererPackage) bool { return p.Package == name }) if i < 0 { panic("no " + name + " in renderer.json") } return i } tamper("untouched", "", func(r *rendererPin) {}) tamper("browser version differs", "libnss3: renderer pins 2:3.110-1+deb13u5+1", func(r *rendererPin) { r.AMD64[index(r, "libnss3")].Version += "+1" }) tamper("browser digest differs", "chromium-common: renderer pins", func(r *rendererPin) { r.AMD64[index(r, "chromium-common")].SHA256 = strings.Repeat("0", 64) }) tamper("browser package missing", "libnss3 2:3.110-1+deb13u5 is in ci-chromium.json but not in renderer.json", func(r *rendererPin) { r.AMD64 = slices.Delete(r.AMD64, index(r, "libnss3"), index(r, "libnss3")+1) }) tamper("browser package marked base", "libnss3 is a browser package", func(r *rendererPin) { r.AMD64[index(r, "libnss3")].Base = true }) tamper("unknown package not base", "libc6 2.41-12+deb13u4 is not in ci-chromium.json", func(r *rendererPin) { r.AMD64[index(r, "libc6")].Base = false }) tamper("shell version differs", "renderer pins 1.0-1", func(r *rendererPin) { r.Version = "1.0-1" }) tamper("no base", "pins no base packages", func(r *rendererPin) { r.AMD64 = slices.DeleteFunc(r.AMD64, func(p rendererPackage) bool { return p.Base }) }) tamper("architecture missing", "libc6: want a snapshot .deb URL", func(r *rendererPin) { r.AMD64[index(r, "libc6")].Architecture = "" }) tamper("arm64 pinned", "pins arm64", func(r *rendererPin) { r.ARM64 = json.RawMessage(`[]`) }) } // pinFixture is a tiny Debian archive: trixie and trixie-security Packages // indexes for both architectures, as the pin script reads them. func pinFixture() map[string]string { const debian, security = "20990101T000000Z", "20990102T000000Z" type pkg struct{ name, version, arch, priority, depends, provides string } release := []pkg{ {"chromium-headless-shell", "1.0-1", "", "optional", "chromium-common (= 1.0-1), libc6 (>= 2.38), libfoo1", ""}, {"chromium-common", "1.0-1", "", "optional", "libc6, x11-utils, libnope | virtual-thing", ""}, {"libfoo1", "1.0-1", "", "optional", "zlib1g", ""}, {"libvirt-impl", "2.0-1", "all", "optional", "", "virtual-thing (= 2.0)"}, {"x11-utils", "7.7-1", "", "optional", "libxt6", ""}, {"libxt6", "1.2-1", "", "optional", "", ""}, {"libc6", "2.41-1", "", "required", "libgcc-s1", ""}, {"libgcc-s1", "14.2-1", "", "required", "", ""}, {"zlib1g", "1:1.3-1", "", "required", "", ""}, {"dash", "0.5-1", "", "required", "", ""}, {"unrelated", "1.0-1", "", "optional", "libc6", ""}, } updates := []pkg{ {"chromium-headless-shell", "1.0-1~deb13u1", "", "optional", "chromium-common (= 1.0-1~deb13u1), libc6 (>= 2.38), libfoo1", ""}, {"chromium-common", "1.0-1~deb13u1", "", "optional", "libc6, x11-utils, libnope | virtual-thing", ""}, {"libfoo1", "1.0-1+deb13u1", "", "optional", "zlib1g", ""}, } write := func(archive, stamp string, packages []pkg, arch string) string { var b strings.Builder for _, p := range packages { a := arch if p.arch != "" { a = p.arch } deb := fmt.Sprintf("pool/main/%c/%s/%s_%s_%s.deb", p.name[0], p.name, p.name, p.version, a) sum := sha256.Sum256([]byte(archive + "/" + deb)) fmt.Fprintf(&b, "Package: %s\nVersion: %s\nArchitecture: %s\nPriority: %s\n", p.name, p.version, a, p.priority) if p.depends != "" { fmt.Fprintf(&b, "Depends: %s\n", p.depends) } if p.provides != "" { fmt.Fprintf(&b, "Provides: %s\n", p.provides) } fmt.Fprintf(&b, "Description: fixture\n multi-line field\nFilename: %s\nSize: %d\nSHA256: %s\n\n", deb, 1000+len(p.name), hex.EncodeToString(sum[:])) } return b.String() } fixture := map[string]string{} for _, arch := range []string{"amd64", "arm64"} { fixture[fmt.Sprintf("https://snapshot.debian.org/archive/debian/%s/dists/trixie/main/binary-%s/Packages.xz", debian, arch)] = write("debian", debian, release, arch) fixture[fmt.Sprintf("https://snapshot.debian.org/archive/debian-security/%s/dists/trixie-security/main/binary-%s/Packages.xz", security, arch)] = write("debian-security", security, updates, arch) } return fixture } // runPin runs scripts/ci-chromium-pin.py against the fixture archive, // writing its two files into dir. func runPin(t *testing.T, dir, version string) ([]byte, error) { t.Helper() fixture, err := json.Marshal(pinFixture()) if err != nil { t.Fatal(err) } const prelude = ` import importlib.util, json, lzma, os, sys spec = importlib.util.spec_from_file_location("pin", "../../scripts/ci-chromium-pin.py") m = importlib.util.module_from_spec(spec) spec.loader.exec_module(m) fixture, out = json.loads(sys.argv[1]), sys.argv[2] m.fetch = lambda url: lzma.compress(fixture[url].encode()) m.OUT = os.path.join(out, "ci-chromium.json") m.RENDERER_OUT = os.path.join(out, "renderer.json") sys.exit(m.main(sys.argv[3:])) ` cmd := exec.Command("python3", "-I", "-c", prelude, string(fixture), dir, version, "20990101T000000Z", "20990102T000000Z") cmd.Env = append(os.Environ(), "PYTHONDONTWRITEBYTECODE=1") return cmd.CombinedOutput() } // The pin script writes both files from one resolution: ci-chromium.json // without the Debian base (the CI image has it), renderer.json for amd64 // with it, the browser packages identical in both. Its output does not // depend on when it runs, and a snapshot without the wanted version // writes neither file. func TestRendererPinScriptWritesBoth(t *testing.T) { testci.Files(t, "../../scripts/ci-chromium-pin.py") testci.Tools(t, "python3") dir := t.TempDir() out, err := runPin(t, dir, "1.0-1~deb13u1") if err != nil { t.Fatalf("pin: %v\n%s", err, out) } ciRaw, err := os.ReadFile(filepath.Join(dir, "ci-chromium.json")) if err != nil { t.Fatal(err) } rendererRaw, err := os.ReadFile(filepath.Join(dir, "renderer.json")) if err != nil { t.Fatal(err) } for _, problem := range rendererPinProblems(ciRaw, rendererRaw) { t.Error(problem) } var ci struct { AMD64, ARM64 []rendererPackage } if err = json.Unmarshal(ciRaw, &ci); err != nil { t.Fatal(err) } var r rendererPin if err = json.Unmarshal(rendererRaw, &r); err != nil { t.Fatal(err) } names := func(packages []rendererPackage, base bool) []string { var out []string for _, p := range packages { if p.Base == base { out = append(out, p.Package) } } return out } browser := []string{"chromium-common", "chromium-headless-shell", "libfoo1", "libvirt-impl"} for arch, packages := range map[string][]rendererPackage{"amd64": ci.AMD64, "arm64": ci.ARM64} { if got := names(packages, false); !slices.Equal(got, browser) { t.Errorf("ci-chromium.json %s: got %v, want %v", arch, got, browser) } } if got := names(r.AMD64, false); !slices.Equal(got, browser) { t.Errorf("renderer.json browser packages: got %v, want %v", got, browser) } // The base the browser needs, transitively, and nothing else from it. if got, want := names(r.AMD64, true), []string{"libc6", "libgcc-s1", "zlib1g"}; !slices.Equal(got, want) { t.Errorf("renderer.json base packages: got %v, want %v", got, want) } byName := map[string]rendererPackage{} for _, p := range r.AMD64 { byName[p.Package] = p } if p := byName["libfoo1"]; p.Version != "1.0-1+deb13u1" || !strings.Contains(p.URL, "/debian-security/20990102T000000Z/pool/") || p.Architecture != "amd64" { t.Errorf("libfoo1: want the security update, got %+v", p) } if p := byName["libvirt-impl"]; p.Architecture != "all" || !strings.HasSuffix(p.URL, "_all.deb") { t.Errorf("libvirt-impl: want the arch-independent deb, got %+v", p) } if p := byName["zlib1g"]; p.Version != "1:1.3-1" || p.Size != 1006 || !pinDigest.MatchString(p.SHA256) { t.Errorf("zlib1g: got %+v", p) } // Same archive, same bytes. again := t.TempDir() if out, err = runPin(t, again, "1.0-1~deb13u1"); err != nil { t.Fatalf("pin again: %v\n%s", err, out) } for _, name := range []string{"ci-chromium.json", "renderer.json"} { first, _ := os.ReadFile(filepath.Join(dir, name)) second, _ := os.ReadFile(filepath.Join(again, name)) if !bytes.Equal(first, second) { t.Errorf("%s differs between two runs on the same archive", name) } } // A version the snapshots do not have: an error, and neither file. none := t.TempDir() if out, err = runPin(t, none, "2.0-1"); err == nil { t.Fatalf("pin of a missing version succeeded:\n%s", out) } if entries, _ := os.ReadDir(none); len(entries) != 0 { t.Errorf("a failed pin wrote %v", entries) } }