oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package release
2
3import (
4 "errors"
5 "fmt"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/wire"
10)
11
12const manifestSum = "ab0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcd"
13
14// A MANIFEST the build wrote: dir, file and link lines sorted by path,
15// every parent listed before its children, links resolving inside the
16// tree (through other links too).
17func TestParseRendererManifest(t *testing.T) {
18 text := "dir etc\n" +
19 "dir etc/fonts\n" +
20 "link etc/fonts/f.conf ../../usr/share/f.conf\n" +
21 "dir usr\n" +
22 "dir usr/bin\n" +
23 "link usr/bin/browser ../lib/browser\n" +
24 "link usr/bin/via ./../bin/browser\n" +
25 "dir usr/lib\n" +
26 "file usr/lib-extra " + manifestSum + " 0644\n" +
27 "file usr/lib/browser " + manifestSum + " 0755\n" +
28 "link usr/lib/up ..\n" +
29 "link usr/lib/up2 ../\n" +
30 "link usr/lib/up3 ./../.\n" +
31 "link usr/lib/via-up up/lib/\n" +
32 "dir usr/share\n" +
33 "file usr/share/f.conf " + manifestSum + " 0644\n"
34 got, err := parseRendererManifest([]byte(text))
35 if err != nil {
36 t.Fatal(err)
37 }
38 want := map[string]rendererEntry{
39 "etc": {kind: "dir"},
40 "etc/fonts": {kind: "dir"},
41 "etc/fonts/f.conf": {kind: "link", target: "../../usr/share/f.conf"},
42 "usr": {kind: "dir"},
43 "usr/bin": {kind: "dir"},
44 "usr/bin/browser": {kind: "link", target: "../lib/browser"},
45 "usr/bin/via": {kind: "link", target: "./../bin/browser"},
46 "usr/lib": {kind: "dir"},
47 "usr/lib-extra": {kind: "file", sha256: manifestSum, mode: 0o644},
48 "usr/lib/browser": {kind: "file", sha256: manifestSum, mode: 0o755},
49 "usr/lib/up": {kind: "link", target: ".."},
50 "usr/lib/up2": {kind: "link", target: "../"},
51 "usr/lib/up3": {kind: "link", target: "./../."},
52 "usr/lib/via-up": {kind: "link", target: "up/lib/"},
53 "usr/share": {kind: "dir"},
54 "usr/share/f.conf": {kind: "file", sha256: manifestSum, mode: 0o644},
55 }
56 if fmt.Sprint(got) != fmt.Sprint(want) {
57 t.Fatalf("parsed\n%v\nwant\n%v", got, want)
58 }
59 if got, err := parseRendererManifest(nil); err != nil || len(got) != 0 {
60 t.Fatalf("empty MANIFEST: %v %v", got, err)
61 }
62}
63
64// Anything the build would not have written is refused: the MANIFEST is
65// read strictly, never guessed at.
66func TestParseRendererManifestRefuses(t *testing.T) {
67 file := func(path, mode string) string { return "file " + path + " " + manifestSum + " " + mode + "\n" }
68 for name, c := range map[string]struct{ text, why string }{
69 "unknown kind": {"dir usr\nfifo usr/p\n", "unknown kind"},
70 "no kind": {"usr\n", "not an entry"},
71 "dir with a field more": {"dir usr x\n", "wrong number of fields"},
72 "file with a field less": {"dir usr\nfile usr/a " + manifestSum + "\n", "wrong number of fields"},
73 "file with a field more": {"dir usr\nfile usr/a " + manifestSum + " 0644 x\n", "wrong number of fields"},
74 "empty link target": {"dir usr\nlink usr/a \n", "not a relative link target"},
75 "link with no target": {"dir usr\nlink usr/a\n", "wrong number of fields"},
76 "two spaces": {"dir usr\nfile usr/a " + manifestSum + " 0644\n", "wrong number of fields"},
77 "upper-case hash": {"dir usr\n" + file("usr/a", "0644")[:len("file usr/a ")] + strings.ToUpper(manifestSum) + " 0644\n", "not a SHA-256"},
78 "short hash": {"dir usr\nfile usr/a abcd 0644\n", "not a SHA-256"},
79 "three-digit mode": {"dir usr\n" + file("usr/a", "644"), "not a mode"},
80 "group-writable mode": {"dir usr\n" + file("usr/a", "0664"), "not a mode"},
81 "world-writable mode": {"dir usr\n" + file("usr/a", "0646"), "not a mode"},
82 "setuid mode": {"dir usr\n" + file("usr/a", "4755"), "not a mode"},
83 "not octal": {"dir usr\n" + file("usr/a", "0855"), "not a mode"},
84 "duplicate": {"dir usr\ndir usr\n", "not sorted"},
85 "unsorted": {"dir usr\ndir etc\n", "not sorted"},
86 "absolute path": {"dir /usr\n", "not a clean relative path"},
87 "dot-dot path": {"dir usr\ndir usr/..\n", "not a clean relative path"},
88 "dot-dot inside": {"dir usr\ndir usr/../etc\n", "not a clean relative path"},
89 "dot path": {"dir usr\ndir usr/.\n", "not a clean relative path"},
90 "leading dot": {"dir ./usr\n", "not a clean relative path"},
91 "empty component": {"dir usr\ndir usr//lib\n", "not a clean relative path"},
92 "trailing slash": {"dir usr/\n", "not a clean relative path"},
93 "empty path": {"dir \n", "not a clean relative path"},
94 "no-break space in a path": {"dir usr\u00a0lib\n", "not a clean relative path"},
95 "tab in a path": {"dir usr\tlib\n", "not a clean relative path"},
96 "control character": {"dir usr\x01\n", "not a clean relative path"},
97 "delete character": {"dir usr\x7f\n", "not a clean relative path"},
98 "not UTF-8": {"dir usr\xff\n", "not UTF-8"},
99 "carriage return": {"dir usr\r\n", "not a clean relative path"},
100 "no final newline": {"dir usr", "does not end in a newline"},
101 "empty line": {"dir usr\n\ndir usr/lib\n", "not an entry"},
102 "lists itself": {"file MANIFEST " + manifestSum + " 0644\n", "not a clean relative path"},
103 "parent not listed": {"dir usr/lib\n", "its directory is not listed"},
104 "parent is a file": {file("usr", "0644") + "dir usr/lib\n", "its directory is not listed"},
105 "parent is a link": {"dir etc\nlink usr etc\ndir usr/lib\n", "its directory is not listed"},
106 "absolute link target": {"dir usr\nlink usr/a /usr\n", "not a relative link target"},
107 "link climbing out": {"dir usr\nlink usr/a ../../etc\n", "climbs out of the tree"},
108 "link names nothing": {"dir usr\nlink usr/a nothing\n", "names nothing in the tree"},
109 "link through a file": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/x\n", "is not a directory"},
110 "link to a file with a slash": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/\n", "is not a directory"},
111 "link to a file with /.": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/.\n", "is not a directory"},
112 "link to a file with /..": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/..\n", "is not a directory"},
113 "link to a file with //": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f//\n", "is not a directory"},
114 "slash through another link": {"dir usr\nlink usr/a f\nlink usr/b a/\n" + file("usr/f", "0644"), "is not a directory"},
115 "slash-dot through another link": {"dir usr\nlink usr/a f\nlink usr/b a/.\n" + file("usr/f", "0644"), "is not a directory"},
116 "link to a slash link to a file": {"dir usr\nlink usr/a f/\nlink usr/b a\n" + file("usr/f", "0644"), "is not a directory"},
117 "link through a link out": {"dir usr\nlink usr/out up/../..\nlink usr/up ..\n", "climbs out of the tree"},
118 "link loop": {"dir usr\nlink usr/a b\nlink usr/b a\n", "too many links"},
119 "link target with a tab": {"dir usr\nlink usr/a \tusr\n", "not a relative link target"},
120 } {
121 t.Run(name, func(t *testing.T) {
122 got, err := parseRendererManifest([]byte(c.text))
123 if err == nil {
124 t.Fatalf("accepted %q: %v", c.text, got)
125 }
126 if !strings.Contains(err.Error(), c.why) {
127 t.Fatalf("refused %q for another reason: %v (want %q)", c.text, err, c.why)
128 }
129 })
130 }
131}
132
133// The runner maps a verification to a health state: ok, missing (no tree
134// installed), mismatch (a tree that is not the pinned one) or unpinned
135// (nothing to check against).
136func TestRendererHealth(t *testing.T) {
137 for _, c := range []struct {
138 err error
139 want string
140 is error
141 }{
142 {nil, wire.ToolHealthy, nil},
143 {&RendererError{Health: wire.ToolMissing}, wire.ToolMissing, ErrRendererMissing},
144 {&RendererError{Health: wire.ToolMismatch, Path: "usr"}, wire.ToolMismatch, ErrRendererMismatch},
145 {&RendererError{Health: wire.ToolUnpinned}, wire.ToolUnpinned, ErrRendererUnpinned},
146 {fmt.Errorf("wrapped: %w", &RendererError{Health: wire.ToolMissing}), wire.ToolMissing, ErrRendererMissing},
147 {errors.New("something else"), wire.ToolMismatch, nil},
148 } {
149 if got := RendererHealth(c.err); got != c.want {
150 t.Errorf("%v: health %q, want %q", c.err, got, c.want)
151 }
152 if c.is != nil && !errors.Is(c.err, c.is) {
153 t.Errorf("%v is not %v", c.err, c.is)
154 }
155 }
156 if errors.Is(&RendererError{Health: wire.ToolMismatch}, ErrRendererMissing) {
157 t.Error("a mismatch is not a missing tree")
158 }
159}
160
161// No pin, or one that is not a SHA-256, matches nothing: the answer is
162// unpinned, before the disk is looked at.
163func TestVerifyRendererUnpinned(t *testing.T) {
164 for _, pin := range []string{"", "not a digest", strings.ToUpper(manifestSum), manifestSum[:63]} {
165 err := VerifyRenderer("/nonexistent/renderer", pin)
166 if !errors.Is(err, ErrRendererUnpinned) || RendererHealth(err) != wire.ToolUnpinned {
167 t.Errorf("pin %q: %v", pin, err)
168 }
169 }
170}
171