package release import ( "errors" "fmt" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) const manifestSum = "ab0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcd" // A MANIFEST the build wrote: dir, file and link lines sorted by path, // every parent listed before its children, links resolving inside the // tree (through other links too). func TestParseRendererManifest(t *testing.T) { text := "dir etc\n" + "dir etc/fonts\n" + "link etc/fonts/f.conf ../../usr/share/f.conf\n" + "dir usr\n" + "dir usr/bin\n" + "link usr/bin/browser ../lib/browser\n" + "link usr/bin/via ./../bin/browser\n" + "dir usr/lib\n" + "file usr/lib-extra " + manifestSum + " 0644\n" + "file usr/lib/browser " + manifestSum + " 0755\n" + "link usr/lib/up ..\n" + "link usr/lib/up2 ../\n" + "link usr/lib/up3 ./../.\n" + "link usr/lib/via-up up/lib/\n" + "dir usr/share\n" + "file usr/share/f.conf " + manifestSum + " 0644\n" got, err := parseRendererManifest([]byte(text)) if err != nil { t.Fatal(err) } want := map[string]rendererEntry{ "etc": {kind: "dir"}, "etc/fonts": {kind: "dir"}, "etc/fonts/f.conf": {kind: "link", target: "../../usr/share/f.conf"}, "usr": {kind: "dir"}, "usr/bin": {kind: "dir"}, "usr/bin/browser": {kind: "link", target: "../lib/browser"}, "usr/bin/via": {kind: "link", target: "./../bin/browser"}, "usr/lib": {kind: "dir"}, "usr/lib-extra": {kind: "file", sha256: manifestSum, mode: 0o644}, "usr/lib/browser": {kind: "file", sha256: manifestSum, mode: 0o755}, "usr/lib/up": {kind: "link", target: ".."}, "usr/lib/up2": {kind: "link", target: "../"}, "usr/lib/up3": {kind: "link", target: "./../."}, "usr/lib/via-up": {kind: "link", target: "up/lib/"}, "usr/share": {kind: "dir"}, "usr/share/f.conf": {kind: "file", sha256: manifestSum, mode: 0o644}, } if fmt.Sprint(got) != fmt.Sprint(want) { t.Fatalf("parsed\n%v\nwant\n%v", got, want) } if got, err := parseRendererManifest(nil); err != nil || len(got) != 0 { t.Fatalf("empty MANIFEST: %v %v", got, err) } } // Anything the build would not have written is refused: the MANIFEST is // read strictly, never guessed at. func TestParseRendererManifestRefuses(t *testing.T) { file := func(path, mode string) string { return "file " + path + " " + manifestSum + " " + mode + "\n" } for name, c := range map[string]struct{ text, why string }{ "unknown kind": {"dir usr\nfifo usr/p\n", "unknown kind"}, "no kind": {"usr\n", "not an entry"}, "dir with a field more": {"dir usr x\n", "wrong number of fields"}, "file with a field less": {"dir usr\nfile usr/a " + manifestSum + "\n", "wrong number of fields"}, "file with a field more": {"dir usr\nfile usr/a " + manifestSum + " 0644 x\n", "wrong number of fields"}, "empty link target": {"dir usr\nlink usr/a \n", "not a relative link target"}, "link with no target": {"dir usr\nlink usr/a\n", "wrong number of fields"}, "two spaces": {"dir usr\nfile usr/a " + manifestSum + " 0644\n", "wrong number of fields"}, "upper-case hash": {"dir usr\n" + file("usr/a", "0644")[:len("file usr/a ")] + strings.ToUpper(manifestSum) + " 0644\n", "not a SHA-256"}, "short hash": {"dir usr\nfile usr/a abcd 0644\n", "not a SHA-256"}, "three-digit mode": {"dir usr\n" + file("usr/a", "644"), "not a mode"}, "group-writable mode": {"dir usr\n" + file("usr/a", "0664"), "not a mode"}, "world-writable mode": {"dir usr\n" + file("usr/a", "0646"), "not a mode"}, "setuid mode": {"dir usr\n" + file("usr/a", "4755"), "not a mode"}, "not octal": {"dir usr\n" + file("usr/a", "0855"), "not a mode"}, "duplicate": {"dir usr\ndir usr\n", "not sorted"}, "unsorted": {"dir usr\ndir etc\n", "not sorted"}, "absolute path": {"dir /usr\n", "not a clean relative path"}, "dot-dot path": {"dir usr\ndir usr/..\n", "not a clean relative path"}, "dot-dot inside": {"dir usr\ndir usr/../etc\n", "not a clean relative path"}, "dot path": {"dir usr\ndir usr/.\n", "not a clean relative path"}, "leading dot": {"dir ./usr\n", "not a clean relative path"}, "empty component": {"dir usr\ndir usr//lib\n", "not a clean relative path"}, "trailing slash": {"dir usr/\n", "not a clean relative path"}, "empty path": {"dir \n", "not a clean relative path"}, "no-break space in a path": {"dir usr\u00a0lib\n", "not a clean relative path"}, "tab in a path": {"dir usr\tlib\n", "not a clean relative path"}, "control character": {"dir usr\x01\n", "not a clean relative path"}, "delete character": {"dir usr\x7f\n", "not a clean relative path"}, "not UTF-8": {"dir usr\xff\n", "not UTF-8"}, "carriage return": {"dir usr\r\n", "not a clean relative path"}, "no final newline": {"dir usr", "does not end in a newline"}, "empty line": {"dir usr\n\ndir usr/lib\n", "not an entry"}, "lists itself": {"file MANIFEST " + manifestSum + " 0644\n", "not a clean relative path"}, "parent not listed": {"dir usr/lib\n", "its directory is not listed"}, "parent is a file": {file("usr", "0644") + "dir usr/lib\n", "its directory is not listed"}, "parent is a link": {"dir etc\nlink usr etc\ndir usr/lib\n", "its directory is not listed"}, "absolute link target": {"dir usr\nlink usr/a /usr\n", "not a relative link target"}, "link climbing out": {"dir usr\nlink usr/a ../../etc\n", "climbs out of the tree"}, "link names nothing": {"dir usr\nlink usr/a nothing\n", "names nothing in the tree"}, "link through a file": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/x\n", "is not a directory"}, "link to a file with a slash": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/\n", "is not a directory"}, "link to a file with /.": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/.\n", "is not a directory"}, "link to a file with /..": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/..\n", "is not a directory"}, "link to a file with //": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f//\n", "is not a directory"}, "slash through another link": {"dir usr\nlink usr/a f\nlink usr/b a/\n" + file("usr/f", "0644"), "is not a directory"}, "slash-dot through another link": {"dir usr\nlink usr/a f\nlink usr/b a/.\n" + file("usr/f", "0644"), "is not a directory"}, "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"}, "link through a link out": {"dir usr\nlink usr/out up/../..\nlink usr/up ..\n", "climbs out of the tree"}, "link loop": {"dir usr\nlink usr/a b\nlink usr/b a\n", "too many links"}, "link target with a tab": {"dir usr\nlink usr/a \tusr\n", "not a relative link target"}, } { t.Run(name, func(t *testing.T) { got, err := parseRendererManifest([]byte(c.text)) if err == nil { t.Fatalf("accepted %q: %v", c.text, got) } if !strings.Contains(err.Error(), c.why) { t.Fatalf("refused %q for another reason: %v (want %q)", c.text, err, c.why) } }) } } // The runner maps a verification to a health state: ok, missing (no tree // installed), mismatch (a tree that is not the pinned one) or unpinned // (nothing to check against). func TestRendererHealth(t *testing.T) { for _, c := range []struct { err error want string is error }{ {nil, wire.ToolHealthy, nil}, {&RendererError{Health: wire.ToolMissing}, wire.ToolMissing, ErrRendererMissing}, {&RendererError{Health: wire.ToolMismatch, Path: "usr"}, wire.ToolMismatch, ErrRendererMismatch}, {&RendererError{Health: wire.ToolUnpinned}, wire.ToolUnpinned, ErrRendererUnpinned}, {fmt.Errorf("wrapped: %w", &RendererError{Health: wire.ToolMissing}), wire.ToolMissing, ErrRendererMissing}, {errors.New("something else"), wire.ToolMismatch, nil}, } { if got := RendererHealth(c.err); got != c.want { t.Errorf("%v: health %q, want %q", c.err, got, c.want) } if c.is != nil && !errors.Is(c.err, c.is) { t.Errorf("%v is not %v", c.err, c.is) } } if errors.Is(&RendererError{Health: wire.ToolMismatch}, ErrRendererMissing) { t.Error("a mismatch is not a missing tree") } } // No pin, or one that is not a SHA-256, matches nothing: the answer is // unpinned, before the disk is looked at. func TestVerifyRendererUnpinned(t *testing.T) { for _, pin := range []string{"", "not a digest", strings.ToUpper(manifestSum), manifestSum[:63]} { err := VerifyRenderer("/nonexistent/renderer", pin) if !errors.Is(err, ErrRendererUnpinned) || RendererHealth(err) != wire.ToolUnpinned { t.Errorf("pin %q: %v", pin, err) } } }