gunk-dev/the-valley

checkrequest_test.go

source68d12192bbd3main's published tip history
6.1 KiB
1package main
2
3import (
4 "bytes"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "the-valley/change"
12)
13
14// requestRef is the ref of a check request filed by who, its attempt
15// numbered n.
16func requestRef(who string, n int) string {
17 return fmt.Sprintf("%s%s/0123456789abcdef/%016x", change.CheckRequestPrefix, who, n)
18}
19
20// A named principal files, moves and withdraws check requests under its
21// own name and no other's, and a holder of the approve grant withdraws
22// anyone's. A request points at a commit the repository holds, under a
23// name of exactly the documented shape, and the symbolic-ref, lookalike
24// and protection rules every ref meets still apply to it.
25func TestAPrincipalFilesCheckRequestsUnderItsOwnName(t *testing.T) {
26 r := newRecords(t)
27 head, other := r.source("one"), r.source("two")
28 tree := r.git("", "mktree")
29 blob := r.blob("not a commit\n")
30 mine, theirs := requestRef("alice", 1), requestRef("bob", 1)
31
32 r.expect("a create under its own name", "alice", mine, zeroID, head, "")
33 r.expect("a move under its own name", "alice", mine, head, other, "")
34 r.expect("a delete under its own name", "alice", mine, head, zeroID, "")
35 r.expect("a create under another's name", "alice", theirs, zeroID, head, "files check requests under its own name, refs/the-valley/check-requests/alice/")
36 r.expect("a move under another's name", "alice", theirs, head, other, "under its own name")
37 r.expect("a delete under another's name", "alice", theirs, head, zeroID, "deleted by the principal it is filed under or by a holder of the approve grant")
38 r.expect("an approve holder's delete", "operator", theirs, head, zeroID, "")
39 r.expect("an approve holder's create under another's name", "operator", theirs, zeroID, head, "under its own name")
40 r.expect("an approve holder's move under another's name", "operator", theirs, head, other, "under its own name")
41 r.expect("an untagged key", "", mine, zeroID, head, "a key that names no principal")
42 r.expect("a tree", "alice", mine, zeroID, tree, "is a tree, and a check request points at a commit")
43 r.expect("a blob", "alice", mine, head, blob, "is a blob, and a check request points at a commit")
44 r.expect("an object the repository lacks", "alice", mine, zeroID, strings.Repeat("1", 40), "does not hold")
45
46 const worktree, attempt = "0123456789abcdef", "fedcba9876543210"
47 for _, ref := range []string{
48 change.CheckRequestPrefix + "alice/" + worktree,
49 change.CheckRequestPrefix + "alice/" + worktree + "/" + attempt + "/x",
50 change.CheckRequestPrefix + "alice/" + worktree[:15] + "/" + attempt,
51 change.CheckRequestPrefix + "alice/" + worktree + "/" + attempt + "0",
52 change.CheckRequestPrefix + "alice/" + worktree + "/" + strings.ToUpper(attempt),
53 change.CheckRequestPrefix + "alice/" + worktree + "/" + attempt[:15] + "g",
54 change.CheckRequestPrefix + "-alice/" + worktree + "/" + attempt,
55 change.CheckRequestPrefix + worktree + "/" + attempt,
56 change.CheckRequestPrefix + "alice//" + attempt,
57 } {
58 r.expect("the malformed "+ref, "alice", ref, zeroID, head, "a check request's ref is refs/the-valley/check-requests/<principal>/<worktree>/<attempt>")
59 r.expect("a delete of the malformed "+ref, "operator", ref, head, zeroID, "a check request's ref is")
60 }
61
62 r.p.symrefs = map[string]bool{mine: true}
63 r.expect("a symbolic ref", "alice", mine, zeroID, head, "symbolic ref")
64 r.p.symrefs = nil
65 r.expect("a lookalike", "alice", "refs/"+mine, zeroID, head, "named refs/ followed by a full ref name")
66 r.p.push = pushPolicy{Refs: []string{change.CheckRequestPrefix + "*"}, Writers: []string{"operator"}}
67 r.expect("a protected request", "alice", mine, zeroID, head, "a protected ref of project")
68}
69
70// A principal holds at most maxCheckRequests requests in a repository,
71// counted with the ones the push creates. Moves and deletes create none,
72// and another principal's requests count for that principal alone.
73func TestAPrincipalHoldsABoundedNumberOfCheckRequests(t *testing.T) {
74 r := newRecords(t)
75 head, other := r.source("one"), r.source("two")
76 policyFile := filepath.Join(t.TempDir(), "policy.json")
77 if err := os.WriteFile(policyFile, []byte(`{"refs": [], "writers": [], "grants": {}}`), 0o644); err != nil {
78 t.Fatal(err)
79 }
80 push := func(who string, lines []string) (int, string) {
81 var stderr bytes.Buffer
82 code := preReceive([]string{"--project", "project", "--policy", policyFile, "--principal", who},
83 strings.NewReader(strings.Join(lines, "\n")+"\n"), &stderr, gitRepository{})
84 return code, stderr.String()
85 }
86 creates := func(who string, from, n int) []string {
87 var lines []string
88 for i := from; i < from+n; i++ {
89 lines = append(lines, zeroID+" "+head+" "+requestRef(who, i))
90 }
91 return lines
92 }
93 install := func(who string, from, n int) {
94 var refs strings.Builder
95 for i := from; i < from+n; i++ {
96 fmt.Fprintf(&refs, "create %s %s\n", requestRef(who, i), head)
97 }
98 r.git(refs.String(), "update-ref", "--stdin")
99 }
100
101 code, says := push("alice", creates("alice", 0, maxCheckRequests+1))
102 if code == 0 || !strings.Contains(says, "alice holds 0 check requests in project, and this push would create 33 more, past the 32 one principal may hold") {
103 t.Errorf("33 creates in one push: exit %d, %q", code, says)
104 }
105 if code, says := push("alice", creates("alice", 0, maxCheckRequests)); code != 0 {
106 t.Errorf("32 creates in one push: exit %d, %q", code, says)
107 }
108
109 install("alice", 0, 30)
110 install("bob", 0, 40)
111 code, says = push("alice", creates("alice", 30, 3))
112 if code == 0 || !strings.Contains(says, "alice holds 30 check requests in project, and this push would create 3 more") {
113 t.Errorf("three past 30: exit %d, %q", code, says)
114 }
115 if code, says := push("alice", creates("alice", 30, 2)); code != 0 {
116 t.Errorf("two past 30, beside another principal's 40: exit %d, %q", code, says)
117 }
118
119 install("alice", 30, 2)
120 moves := []string{head + " " + other + " " + requestRef("alice", 0), head + " " + zeroID + " " + requestRef("alice", 1)}
121 if code, says := push("alice", moves); code != 0 {
122 t.Errorf("a move and a delete at the bound: exit %d, %q", code, says)
123 }
124 code, says = push("alice", creates("alice", 32, 1))
125 if code == 0 || !strings.Contains(says, "alice holds 32 check requests") {
126 t.Errorf("a create at the bound: exit %d, %q", code, says)
127 }
128}