| 1 | // Package renderertest builds renderer archives for tests, in the layout |
| 2 | // scripts/build-renderer-tree.py writes: MANIFEST first, then every entry |
| 3 | // in path order, directories with a trailing slash, PAX headers, no |
| 4 | // owner and no times. |
| 5 | package renderertest |
| 6 | |
| 7 | import ( |
| 8 | "archive/tar" |
| 9 | "bytes" |
| 10 | "compress/gzip" |
| 11 | "crypto/sha256" |
| 12 | "encoding/hex" |
| 13 | "fmt" |
| 14 | "sort" |
| 15 | "strings" |
| 16 | ) |
| 17 | |
| 18 | // Entry is one member of the tree: Kind is "dir", "file" or "link". |
| 19 | // Mode is a file's (0644 or 0755); Body is a file's bytes, Target a |
| 20 | // link's target. |
| 21 | type Entry struct { |
| 22 | Path, Kind string |
| 23 | Mode int64 |
| 24 | Body, Target string |
| 25 | } |
| 26 | |
| 27 | // Small is a tree of a few entries of each kind, links among them. |
| 28 | func Small() []Entry { |
| 29 | return []Entry{ |
| 30 | {Path: "etc", Kind: "dir"}, |
| 31 | {Path: "etc/fonts", Kind: "dir"}, |
| 32 | {Path: "etc/fonts/fonts.conf", Kind: "link", Target: "../../usr/share/fonts.conf"}, |
| 33 | {Path: "usr", Kind: "dir"}, |
| 34 | {Path: "usr/bin", Kind: "dir"}, |
| 35 | {Path: "usr/bin/chromium", Kind: "link", Target: "../lib/chromium/chromium-headless-shell"}, |
| 36 | {Path: "usr/lib", Kind: "dir"}, |
| 37 | {Path: "usr/lib/chromium", Kind: "dir"}, |
| 38 | {Path: "usr/lib/chromium/chromium-headless-shell", Kind: "file", Mode: 0o755, Body: "browser"}, |
| 39 | {Path: "usr/share", Kind: "dir"}, |
| 40 | {Path: "usr/share/fonts.conf", Kind: "file", Mode: 0o644, Body: "<fontconfig/>"}, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Manifest is the MANIFEST of entries, as the build writes it. |
| 45 | func Manifest(entries []Entry) []byte { |
| 46 | sorted := append([]Entry(nil), entries...) |
| 47 | sort.Slice(sorted, func(i, j int) bool { return sorted[i].Path < sorted[j].Path }) |
| 48 | var b strings.Builder |
| 49 | for _, e := range sorted { |
| 50 | switch e.Kind { |
| 51 | case "dir": |
| 52 | fmt.Fprintf(&b, "dir %s\n", e.Path) |
| 53 | case "link": |
| 54 | fmt.Fprintf(&b, "link %s %s\n", e.Path, e.Target) |
| 55 | default: |
| 56 | sum := sha256.Sum256([]byte(e.Body)) |
| 57 | fmt.Fprintf(&b, "file %s %s %04o\n", e.Path, hex.EncodeToString(sum[:]), e.Mode) |
| 58 | } |
| 59 | } |
| 60 | return []byte(b.String()) |
| 61 | } |
| 62 | |
| 63 | // Pin is the SHA-256 of a MANIFEST: the renderer pin. |
| 64 | func Pin(manifest []byte) string { |
| 65 | sum := sha256.Sum256(manifest) |
| 66 | return hex.EncodeToString(sum[:]) |
| 67 | } |
| 68 | |
| 69 | // Archive is renderer.tar.gz for entries, with their MANIFEST, and its |
| 70 | // pin. |
| 71 | func Archive(entries []Entry) ([]byte, string) { |
| 72 | manifest := Manifest(entries) |
| 73 | return ArchiveOf(entries, manifest), Pin(manifest) |
| 74 | } |
| 75 | |
| 76 | // ArchiveOf is renderer.tar.gz for entries with the given MANIFEST, which |
| 77 | // may list other bytes than the entries hold. |
| 78 | func ArchiveOf(entries []Entry, manifest []byte) []byte { |
| 79 | headers := []*tar.Header{{Name: "MANIFEST", Typeflag: tar.TypeReg, Mode: 0o644, Size: int64(len(manifest))}} |
| 80 | bodies := [][]byte{manifest} |
| 81 | sorted := append([]Entry(nil), entries...) |
| 82 | sort.Slice(sorted, func(i, j int) bool { return sorted[i].Path < sorted[j].Path }) |
| 83 | for _, e := range sorted { |
| 84 | switch e.Kind { |
| 85 | case "dir": |
| 86 | headers = append(headers, &tar.Header{Name: e.Path + "/", Typeflag: tar.TypeDir, Mode: 0o755}) |
| 87 | bodies = append(bodies, nil) |
| 88 | case "link": |
| 89 | headers = append(headers, &tar.Header{Name: e.Path, Typeflag: tar.TypeSymlink, Mode: 0o777, Linkname: e.Target}) |
| 90 | bodies = append(bodies, nil) |
| 91 | default: |
| 92 | headers = append(headers, &tar.Header{Name: e.Path, Typeflag: tar.TypeReg, Mode: e.Mode, Size: int64(len(e.Body))}) |
| 93 | bodies = append(bodies, []byte(e.Body)) |
| 94 | } |
| 95 | } |
| 96 | return Raw(headers, bodies) |
| 97 | } |
| 98 | |
| 99 | // Raw is a tar.gz of exactly these headers and bodies, in order, so a |
| 100 | // test can write any member the build never would. |
| 101 | func Raw(headers []*tar.Header, bodies [][]byte) []byte { |
| 102 | var b bytes.Buffer |
| 103 | gz := gzip.NewWriter(&b) |
| 104 | tw := tar.NewWriter(gz) |
| 105 | for i, h := range headers { |
| 106 | h.Format = tar.FormatPAX |
| 107 | if err := tw.WriteHeader(h); err != nil { |
| 108 | panic(err) |
| 109 | } |
| 110 | if len(bodies[i]) > 0 { |
| 111 | if _, err := tw.Write(bodies[i]); err != nil { |
| 112 | panic(err) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | if err := tw.Close(); err != nil { |
| 117 | panic(err) |
| 118 | } |
| 119 | if err := gz.Close(); err != nil { |
| 120 | panic(err) |
| 121 | } |
| 122 | return b.Bytes() |
| 123 | } |
| 124 | |