// Package renderertest builds renderer archives for tests, in the layout // scripts/build-renderer-tree.py writes: MANIFEST first, then every entry // in path order, directories with a trailing slash, PAX headers, no // owner and no times. package renderertest import ( "archive/tar" "bytes" "compress/gzip" "crypto/sha256" "encoding/hex" "fmt" "sort" "strings" ) // Entry is one member of the tree: Kind is "dir", "file" or "link". // Mode is a file's (0644 or 0755); Body is a file's bytes, Target a // link's target. type Entry struct { Path, Kind string Mode int64 Body, Target string } // Small is a tree of a few entries of each kind, links among them. func Small() []Entry { return []Entry{ {Path: "etc", Kind: "dir"}, {Path: "etc/fonts", Kind: "dir"}, {Path: "etc/fonts/fonts.conf", Kind: "link", Target: "../../usr/share/fonts.conf"}, {Path: "usr", Kind: "dir"}, {Path: "usr/bin", Kind: "dir"}, {Path: "usr/bin/chromium", Kind: "link", Target: "../lib/chromium/chromium-headless-shell"}, {Path: "usr/lib", Kind: "dir"}, {Path: "usr/lib/chromium", Kind: "dir"}, {Path: "usr/lib/chromium/chromium-headless-shell", Kind: "file", Mode: 0o755, Body: "browser"}, {Path: "usr/share", Kind: "dir"}, {Path: "usr/share/fonts.conf", Kind: "file", Mode: 0o644, Body: ""}, } } // Manifest is the MANIFEST of entries, as the build writes it. func Manifest(entries []Entry) []byte { sorted := append([]Entry(nil), entries...) sort.Slice(sorted, func(i, j int) bool { return sorted[i].Path < sorted[j].Path }) var b strings.Builder for _, e := range sorted { switch e.Kind { case "dir": fmt.Fprintf(&b, "dir %s\n", e.Path) case "link": fmt.Fprintf(&b, "link %s %s\n", e.Path, e.Target) default: sum := sha256.Sum256([]byte(e.Body)) fmt.Fprintf(&b, "file %s %s %04o\n", e.Path, hex.EncodeToString(sum[:]), e.Mode) } } return []byte(b.String()) } // Pin is the SHA-256 of a MANIFEST: the renderer pin. func Pin(manifest []byte) string { sum := sha256.Sum256(manifest) return hex.EncodeToString(sum[:]) } // Archive is renderer.tar.gz for entries, with their MANIFEST, and its // pin. func Archive(entries []Entry) ([]byte, string) { manifest := Manifest(entries) return ArchiveOf(entries, manifest), Pin(manifest) } // ArchiveOf is renderer.tar.gz for entries with the given MANIFEST, which // may list other bytes than the entries hold. func ArchiveOf(entries []Entry, manifest []byte) []byte { headers := []*tar.Header{{Name: "MANIFEST", Typeflag: tar.TypeReg, Mode: 0o644, Size: int64(len(manifest))}} bodies := [][]byte{manifest} sorted := append([]Entry(nil), entries...) sort.Slice(sorted, func(i, j int) bool { return sorted[i].Path < sorted[j].Path }) for _, e := range sorted { switch e.Kind { case "dir": headers = append(headers, &tar.Header{Name: e.Path + "/", Typeflag: tar.TypeDir, Mode: 0o755}) bodies = append(bodies, nil) case "link": headers = append(headers, &tar.Header{Name: e.Path, Typeflag: tar.TypeSymlink, Mode: 0o777, Linkname: e.Target}) bodies = append(bodies, nil) default: headers = append(headers, &tar.Header{Name: e.Path, Typeflag: tar.TypeReg, Mode: e.Mode, Size: int64(len(e.Body))}) bodies = append(bodies, []byte(e.Body)) } } return Raw(headers, bodies) } // Raw is a tar.gz of exactly these headers and bodies, in order, so a // test can write any member the build never would. func Raw(headers []*tar.Header, bodies [][]byte) []byte { var b bytes.Buffer gz := gzip.NewWriter(&b) tw := tar.NewWriter(gz) for i, h := range headers { h.Format = tar.FormatPAX if err := tw.WriteHeader(h); err != nil { panic(err) } if len(bodies[i]) > 0 { if _, err := tw.Write(bodies[i]); err != nil { panic(err) } } } if err := tw.Close(); err != nil { panic(err) } if err := gz.Close(); err != nil { panic(err) } return b.Bytes() }