| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/ed25519" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "the-valley/approval" |
| 14 | "the-valley/change" |
| 15 | "the-valley/note" |
| 16 | ) |
| 17 | |
| 18 | // TestMain lets the test binary stand in for the hook a real push runs: |
| 19 | // a repository whose hooks/pre-receive links to the binary runs |
| 20 | // preReceive with the arguments VALLEYHOOK_TEST_HOOK names. |
| 21 | func TestMain(m *testing.M) { |
| 22 | if args := os.Getenv("VALLEYHOOK_TEST_HOOK"); args != "" { |
| 23 | os.Exit(preReceive(strings.Split(args, "\n"), os.Stdin, os.Stderr, gitRepository{})) |
| 24 | } |
| 25 | os.Exit(m.Run()) |
| 26 | } |
| 27 | |
| 28 | var approvalKey = ed25519.NewKeyFromSeed(bytes.Repeat([]byte{7}, ed25519.SeedSize)) |
| 29 | |
| 30 | // approvalNote is a signed approval of version of the change id landing |
| 31 | // on target, by principal. |
| 32 | func approvalNote(t *testing.T, id, target, principal string, version int) string { |
| 33 | t.Helper() |
| 34 | n, err := approval.Sign(approvalStatement(id, target, principal, version), approval.NewKey(approvalKey)) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | return string(n) |
| 39 | } |
| 40 | |
| 41 | func approvalStatement(id, target, principal string, version int) approval.Statement { |
| 42 | return approval.Statement{ |
| 43 | Instance: strings.Repeat("5f", 32), Project: "project", Target: target, Change: id, |
| 44 | Version: version, Head: change.HeadPrefix + strings.Repeat("2", 40), |
| 45 | Record: change.HeadPrefix + strings.Repeat("3", 40), Principal: principal, |
| 46 | Issued: time.Date(2026, 11, 6, 0, 43, 26, 0, time.UTC), |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Only a holder of the approve grant, under either of its names, writes an |
| 51 | // approval: owning the change, the restack grant and a writer list confer |
| 52 | // nothing. An approval ref is named for a change and main, and points at |
| 53 | // one blob holding an approval note about that change and target, held to |
| 54 | // approval.MaxNote bytes before it is read. Whether its key may approve |
| 55 | // anything is not the hook's to judge. |
| 56 | func TestAnApprovalTakesTheApproveGrantAndHoldsOneNote(t *testing.T) { |
| 57 | r := newRecords(t) |
| 58 | r.p.grants["approver"] = map[string]bool{grantApprove: true} |
| 59 | r.p.grants["restacker"] = map[string]bool{grantRestack: true} |
| 60 | ref := approval.Ref("main", changeID) |
| 61 | first := r.blob(approvalNote(t, changeID, "main", "operator", 1)) |
| 62 | second := r.blob(approvalNote(t, changeID, "main", "approver", 2)) |
| 63 | if ref != "refs/the-valley/approvals/main/c-"+changeID { |
| 64 | t.Fatalf("the approval ref is %s", ref) |
| 65 | } |
| 66 | |
| 67 | r.expect("a create by a holder of request", "operator", ref, zeroID, first, "") |
| 68 | r.expect("a create by a holder of approve", "approver", ref, zeroID, second, "") |
| 69 | r.expect("a replacement", "operator", ref, first, second, "") |
| 70 | r.expect("a delete", "approver", ref, first, zeroID, "") |
| 71 | for _, who := range []string{"alice", "restacker"} { |
| 72 | says := "refs/the-valley/approvals/ takes writes only from a principal holding the approve grant (also accepted as request), and " + who + " holds none" |
| 73 | r.expect("a create by "+who, who, ref, zeroID, first, says) |
| 74 | r.expect("a replacement by "+who, who, ref, first, second, says) |
| 75 | r.expect("a delete by "+who, who, ref, first, zeroID, says) |
| 76 | } |
| 77 | r.expect("an untagged key", "", ref, zeroID, first, "a key that names no principal holds no grant") |
| 78 | r.p.push = pushPolicy{Writers: []string{"alice"}, Grants: map[string]refGrant{"all": {Refs: []string{"refs/*"}, Writers: []string{"alice"}}}} |
| 79 | r.expect("a declared writer opened every ref", "alice", ref, zeroID, first, "holds none") |
| 80 | r.p.push = pushPolicy{Refs: []string{approval.RefPrefix + "*"}, Writers: []string{"approver"}} |
| 81 | r.expect("a protected approval", "operator", ref, zeroID, first, "a protected ref of project") |
| 82 | r.expect("a protected approval by its writer", "approver", ref, zeroID, first, "") |
| 83 | r.p.push = pushPolicy{} |
| 84 | |
| 85 | for _, bad := range []string{ |
| 86 | approval.RefPrefix + "main/" + changeID, |
| 87 | approval.RefPrefix + "main/c-" + changeID[:31], |
| 88 | approval.RefPrefix + "main/c-" + strings.ToUpper(changeID), |
| 89 | approval.RefPrefix + "main/c-" + changeID + "/x", |
| 90 | approval.RefPrefix + "c-" + changeID, |
| 91 | approval.RefPrefix + "release/c-" + changeID, |
| 92 | approval.RefPrefix + "main", |
| 93 | } { |
| 94 | r.expect("the malformed "+bad, "operator", bad, zeroID, first, "an approval's ref is refs/the-valley/approvals/main/c-<change ID>") |
| 95 | r.expect("a delete of the malformed "+bad, "operator", bad, first, zeroID, "an approval's ref is") |
| 96 | } |
| 97 | r.p.symrefs = map[string]bool{ref: true} |
| 98 | r.expect("a symbolic ref", "operator", ref, zeroID, first, "symbolic ref") |
| 99 | r.p.symrefs = nil |
| 100 | r.expect("a lookalike", "operator", "refs/"+ref, zeroID, first, "named refs/ followed by a full ref name") |
| 101 | |
| 102 | signedAs := func(text []byte, name string) string { |
| 103 | n, err := note.Sign(text, name, approvalKey) |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | return string(n) |
| 108 | } |
| 109 | text, err := approvalStatement(changeID, "main", "operator", 1).Encode() |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | twice, err := note.AddSignature([]byte(approvalNote(t, changeID, "main", "operator", 1)), "approver", approvalKey) |
| 114 | if err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | for _, c := range []struct{ what, object, says string }{ |
| 118 | {"a tree", r.git("", "mktree"), "it points at a tree, where an approval holds a blob"}, |
| 119 | {"a commit", r.source("one"), "it points at a commit, where an approval holds a blob"}, |
| 120 | {"an object the push did not bring", strings.Repeat("1", 40), "it names an object the push did not bring"}, |
| 121 | {"an oversized blob", r.blob(strings.Repeat("x", approval.MaxNote+1)), "it is a blob of 2049 bytes, more than the 2048 an approval's may be"}, |
| 122 | {"text", r.blob("approved\n"), "it is not an approval note: this is not a note"}, |
| 123 | {"another kind of statement", r.blob(signedAs([]byte("the-valley/attestation/v1\n"), "operator")), "it is not an approval note: the signed text is not an approval"}, |
| 124 | {"a note signed twice", r.blob(string(twice)), "an approval note carries one signature, and this carries 2"}, |
| 125 | {"a signature under another name", r.blob(signedAs(text, "approver")), "signed with a key of approver, and names operator as its approver"}, |
| 126 | {"another change's approval", r.blob(approvalNote(t, strings.Repeat("k", 32), "main", "operator", 1)), "it approves the change " + strings.Repeat("k", 32) + ", and its ref names " + changeID}, |
| 127 | {"an approval for another target", r.blob(approvalNote(t, changeID, "release", "operator", 1)), "it approves landing on release, and its ref names main"}, |
| 128 | } { |
| 129 | r.expect(c.what, "operator", ref, zeroID, c.object, c.says) |
| 130 | r.expect(c.what+" as a replacement", "operator", ref, first, c.object, c.says) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // An approval is published beside the change's integration request in one |
| 135 | // atomic push, each ref leased: a create against no ref, a replacement or a |
| 136 | // delete against the value read. The push goes through the real hook, run |
| 137 | // by receive-pack. A stale lease on either ref, or an approval the hook |
| 138 | // refuses, leaves both refs as they were. |
| 139 | func TestAnApprovalIsPublishedWithItsRequestAtomicallyAndLeased(t *testing.T) { |
| 140 | hook, err := os.Executable() |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | dir := t.TempDir() |
| 145 | origin, client := filepath.Join(dir, "origin.git"), filepath.Join(dir, "client") |
| 146 | gitIn(t, dir, "", "init", "--quiet", "--bare", origin) |
| 147 | if err := os.Symlink(hook, filepath.Join(origin, "hooks", "pre-receive")); err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | policyFile, grantsFile := filepath.Join(dir, "policy.json"), filepath.Join(dir, "grants") |
| 151 | if err := os.WriteFile(policyFile, []byte(`{"refs": [], "writers": [], "grants": {}}`), 0o644); err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | if err := os.WriteFile(grantsFile, []byte("approve operator\n"), 0o644); err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | gitIn(t, dir, "", "init", "--quiet", client) |
| 158 | commit := func(subject string) string { |
| 159 | tree := gitIn(t, client, "", "mktree") |
| 160 | return gitIn(t, client, "", "-c", "user.name=t", "-c", "user.email=t@localhost", "commit-tree", tree, "-m", subject) |
| 161 | } |
| 162 | blob := func(content string) string { return gitIn(t, client, content, "hash-object", "-w", "--stdin") } |
| 163 | push := func(args ...string) (bool, string) { |
| 164 | cmd := exec.Command("git", append([]string{"push", "--atomic", "--porcelain", origin}, args...)...) |
| 165 | cmd.Dir = client |
| 166 | cmd.Env = append(os.Environ(), "VALLEYHOOK_TEST_HOOK="+strings.Join([]string{ |
| 167 | "--project", "project", "--policy", policyFile, "--grants", grantsFile, "--principal", "operator", |
| 168 | }, "\n")) |
| 169 | out, err := cmd.CombinedOutput() |
| 170 | return err == nil, string(out) |
| 171 | } |
| 172 | at := func(ref string) string { |
| 173 | cmd := exec.Command("git", "for-each-ref", "--format=%(objectname)", ref) |
| 174 | cmd.Dir = origin |
| 175 | out, err := cmd.Output() |
| 176 | if err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | return strings.TrimSpace(string(out)) |
| 180 | } |
| 181 | |
| 182 | req := requestPrefix + "main/" + change.RequestName(changeID) |
| 183 | ref := approval.Ref("main", changeID) |
| 184 | h1, h2 := commit("one"), commit("two") |
| 185 | a1 := blob(approvalNote(t, changeID, "main", "operator", 1)) |
| 186 | a2 := blob(approvalNote(t, changeID, "main", "operator", 2)) |
| 187 | elsewhere := blob(approvalNote(t, strings.Repeat("k", 32), "main", "operator", 2)) |
| 188 | |
| 189 | if ok, out := push("--force-with-lease="+req+":", "--force-with-lease="+ref+":", h1+":"+req, a1+":"+ref); !ok { |
| 190 | t.Fatalf("filing the request and its approval: %s", out) |
| 191 | } |
| 192 | if at(req) != h1 || at(ref) != a1 { |
| 193 | t.Fatalf("after filing: the request is at %q and the approval at %q", at(req), at(ref)) |
| 194 | } |
| 195 | |
| 196 | for _, c := range []struct { |
| 197 | what string |
| 198 | args []string |
| 199 | }{ |
| 200 | {"an approval leased against no ref", []string{"--force-with-lease=" + req + ":" + h1, "--force-with-lease=" + ref + ":", h2 + ":" + req, a2 + ":" + ref}}, |
| 201 | {"an approval leased against another value", []string{"--force-with-lease=" + req + ":" + h1, "--force-with-lease=" + ref + ":" + a2, h2 + ":" + req, a2 + ":" + ref}}, |
| 202 | {"a request leased against another value", []string{"--force-with-lease=" + req + ":" + h2, "--force-with-lease=" + ref + ":" + a1, h2 + ":" + req, a2 + ":" + ref}}, |
| 203 | {"a delete leased against another value", []string{"--force-with-lease=" + req + ":" + h1, "--force-with-lease=" + ref + ":" + a2, ":" + req, ":" + ref}}, |
| 204 | {"an approval the hook refuses", []string{"--force-with-lease=" + req + ":" + h1, "--force-with-lease=" + ref + ":" + a1, h2 + ":" + req, elsewhere + ":" + ref}}, |
| 205 | } { |
| 206 | if ok, out := push(c.args...); ok { |
| 207 | t.Errorf("%s was pushed: %s", c.what, out) |
| 208 | } |
| 209 | if at(req) != h1 || at(ref) != a1 { |
| 210 | t.Errorf("after %s: the request is at %q and the approval at %q", c.what, at(req), at(ref)) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if ok, out := push("--force-with-lease="+req+":"+h1, "--force-with-lease="+ref+":"+a1, h2+":"+req, a2+":"+ref); !ok { |
| 215 | t.Fatalf("replacing the request and its approval: %s", out) |
| 216 | } |
| 217 | if at(req) != h2 || at(ref) != a2 { |
| 218 | t.Fatalf("after replacing: the request is at %q and the approval at %q", at(req), at(ref)) |
| 219 | } |
| 220 | if ok, out := push("--force-with-lease="+req+":"+h2, "--force-with-lease="+ref+":"+a2, ":"+req, ":"+ref); !ok { |
| 221 | t.Fatalf("withdrawing the request and its approval: %s", out) |
| 222 | } |
| 223 | if at(req) != "" || at(ref) != "" { |
| 224 | t.Fatalf("after withdrawing: the request is at %q and the approval at %q", at(req), at(ref)) |
| 225 | } |
| 226 | } |