gunk-dev/the-valley

attest_test.go

sourceb9d42e2a97e7main's published tip history
5.0 KiB
1package main
2
3import (
4 "os"
5 "os/exec"
6 "path/filepath"
7 "strings"
8 "testing"
9)
10
11// Canonical form and the tree digest are the two pieces of genuinely
12// tricky logic here: they decide which bytes get signed and what a subject
13// is, and both must agree byte for byte with any other implementation.
14// Everything else in this program is exercised end to end by the flake
15// checks against the real binary.
16
17func TestCanonicalJSONSortsAndTightens(t *testing.T) {
18 got, err := canonicalJSON(map[string]any{
19 "b": "two",
20 "a": map[string]any{"z": "last", "y": []any{"1", "2"}},
21 })
22 if err != nil {
23 t.Fatal(err)
24 }
25 want := `{"a":{"y":["1","2"],"z":"last"},"b":"two"}`
26 if string(got) != want {
27 t.Errorf("canonicalJSON =\n %s\nwant\n %s", got, want)
28 }
29}
30
31func TestCanonicalJSONEscaping(t *testing.T) {
32 // Go's encoding/json escapes <, > and & by default; RFC 8785 does not,
33 // and a signer that escaped them would not match one that did not.
34 got, err := canonicalJSON(map[string]any{"k": "a<b>c&d \"q\" \\ \n ü"})
35 if err != nil {
36 t.Fatal(err)
37 }
38 want := `{"k":"a<b>c&d \"q\" \\ \n ü"}`
39 if string(got) != want {
40 t.Errorf("canonicalJSON =\n %s\nwant\n %s", got, want)
41 }
42}
43
44func TestCanonicalJSONRejectsNumbers(t *testing.T) {
45 // A statement carries no numbers, so the canonical form of one has
46 // never been decided. Refusing beats guessing.
47 if _, err := canonicalize(map[string]any{"n": 1.5}); err == nil {
48 t.Fatal("canonicalize accepted a number")
49 }
50}
51
52func TestTreeManifestIsUnambiguous(t *testing.T) {
53 // A path holding the manifest's own separators must not be able to
54 // forge an entry boundary: the byte length is what delimits it.
55 entries := []entry{
56 {mode: modeFile, path: "b\n100644 x 1 c", sum: [32]byte{}},
57 {mode: modeExec, path: "a", sum: [32]byte{0xff}},
58 }
59 want := "valley-tree-v1\n" +
60 "100755 ff00000000000000000000000000000000000000000000000000000000000000 1 a\n" +
61 "100644 0000000000000000000000000000000000000000000000000000000000000000 14 b\n100644 x 1 c\n"
62 if got := string(treeManifest(entries)); got != want {
63 t.Errorf("treeManifest =\n%q\nwant\n%q", got, want)
64 }
65}
66
67func TestTreeDigestIsPinned(t *testing.T) {
68 // The digest of a fixed tree is a wire format: changing it invalidates
69 // every attestation ever made. This value may only change with the
70 // scheme's version.
71 entries := []entry{{mode: modeFile, path: "a.txt", sum: sha256of("hi\n")}}
72 const want = "74781d4ba499b9896e97f2fdd38815d5723cb7b12e448ef0ce5788a1e72c2220"
73 if got := treeDigest(entries); got != want {
74 t.Errorf("treeDigest = %s, want %s", got, want)
75 }
76}
77
78func TestInputsManifestSortsAndDeduplicates(t *testing.T) {
79 want := "valley-inputs-v1\n1 a\n1 b\n"
80 if got := string(inputsManifest([]string{"b", "a", "b"})); got != want {
81 t.Errorf("inputsManifest = %q, want %q", got, want)
82 }
83}
84
85// The digest of a committed tree must equal the digest of that tree
86// written to disk — the two enumerators are the same scheme or the check
87// output digest and the subject digest mean different things.
88func TestGitTreeAndFilesystemAgree(t *testing.T) {
89 repo := t.TempDir()
90 run := func(args ...string) string {
91 cmd := exec.Command("git", args...)
92 cmd.Dir = repo
93 cmd.Env = append(os.Environ(),
94 "GIT_CONFIG_NOSYSTEM=1", "HOME="+t.TempDir(),
95 "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@l",
96 "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@l")
97 out, err := cmd.CombinedOutput()
98 if err != nil {
99 t.Fatalf("git %s: %v: %s", strings.Join(args, " "), err, out)
100 }
101 return strings.TrimSpace(string(out))
102 }
103 write := func(name, content string, mode os.FileMode) {
104 p := filepath.Join(repo, name)
105 if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
106 t.Fatal(err)
107 }
108 if err := os.WriteFile(p, []byte(content), mode); err != nil {
109 t.Fatal(err)
110 }
111 }
112
113 run("init", "--quiet", "-b", "main")
114 write("plain.txt", "hello\n", 0o644)
115 write("nested/dir/script.sh", "#!/bin/sh\n", 0o755)
116 if err := os.Symlink("plain.txt", filepath.Join(repo, "link")); err != nil {
117 t.Fatal(err)
118 }
119 run("add", "-A")
120 run("commit", "--quiet", "-m", "seed")
121
122 entries, err := gitTreeEntries(repo, "HEAD")
123 if err != nil {
124 t.Fatal(err)
125 }
126 fromGit := treeDigest(entries)
127
128 export := t.TempDir()
129 if err := exportTree(repo, "HEAD", export); err != nil {
130 t.Fatal(err)
131 }
132 fsEnts, err := fsEntries(export)
133 if err != nil {
134 t.Fatal(err)
135 }
136 if fromFS := treeDigest(fsEnts); fromFS != fromGit {
137 t.Errorf("git tree digest %s != filesystem digest %s\n git: %s\n fs: %s",
138 fromGit, fromFS, treeManifest(entries), treeManifest(fsEnts))
139 }
140
141 // The exec bit and the symlink are the two entries a naive walk gets
142 // wrong, so pin that they are actually present.
143 modes := map[string]string{}
144 for _, e := range entries {
145 modes[e.path] = e.mode
146 }
147 for path, want := range map[string]string{
148 "plain.txt": modeFile,
149 "nested/dir/script.sh": modeExec,
150 "link": modeSymlink,
151 } {
152 if modes[path] != want {
153 t.Errorf("%s has mode %q, want %q", path, modes[path], want)
154 }
155 }
156}
157
158func sha256of(s string) [32]byte {
159 var out [32]byte
160 copy(out[:], sha256sum([]byte(s)))
161 return out
162}