gunk-dev/the-valley

policy_test.go

source8bf5a934d291main's published tip history
5.8 KiB
1package main
2
3import (
4 "errors"
5 "strings"
6 "testing"
7
8 "the-valley/knot"
9)
10
11// The patterns are documented as matching the full refname with `*`
12// crossing path separators, and the hook they replace matched them as
13// shell case patterns. A matcher is wrong exactly at the stars, so the
14// stars are what is pinned.
15func TestAPatternMatchesTheWholeRefnameAndItsStarsCrossSeparators(t *testing.T) {
16 for _, c := range []struct {
17 pattern, ref string
18 want bool
19 }{
20 {"refs/heads/main", "refs/heads/main", true},
21 {"refs/heads/main", "refs/heads/main2", false},
22 {"refs/heads/main", "refs/heads/mai", false},
23 {"refs/heads/release/*", "refs/heads/release/1.0", true},
24 {"refs/heads/release/*", "refs/heads/release/1/hotfix", true},
25 {"refs/heads/release/*", "refs/heads/release/", true},
26 {"refs/heads/release/*", "refs/heads/releases/1", false},
27 {"refs/tags/release/*/v*", "refs/tags/release/app/v1", true},
28 {"refs/tags/release/*/v*", "refs/tags/release/app/nightly", false},
29 {"refs/*/main", "refs/heads/x/main", true},
30 {"refs/*/main", "refs/heads/x/main/y", false},
31 {"refs/heads/*a*b", "refs/heads/aab", true},
32 {"refs/heads/*a*b", "refs/heads/aaba", false},
33 } {
34 if got := match(c.pattern, c.ref); got != c.want {
35 t.Errorf("match(%q, %q) = %v, want %v", c.pattern, c.ref, got, c.want)
36 }
37 }
38}
39
40// fakeRepository holds no objects.
41type fakeRepository struct{}
42
43func (f fakeRepository) symbolic([]string) (map[string]bool, error) { return nil, nil }
44func (f fakeRepository) notes(string) (map[string][]byte, error) {
45 return nil, errors.New("holds no objects")
46}
47func (f fakeRepository) record(string) (fileCommit, error) {
48 return fileCommit{}, errors.New("holds no objects")
49}
50func (f fakeRepository) report(string) (fileCommit, error) {
51 return fileCommit{}, errors.New("holds no objects")
52}
53func (f fakeRepository) hasRecord(string) (bool, error) {
54 return false, errors.New("holds no refs")
55}
56func (f fakeRepository) kind(string) (string, error) { return "", errors.New("holds no objects") }
57func (f fakeRepository) records() (int, error) { return 0, nil }
58func (f fakeRepository) checkRequests(string) (int, error) { return 0, nil }
59func (f fakeRepository) graph() (graphReader, error) { return nil, errors.New("holds no objects") }
60
61// Every rule that applies to a ref has to allow the write, and none of
62// them can be satisfied by another. Each case here is a declaration that
63// tries to stand in for a rule it is not: a writer list covering a
64// namespace whose rule is a grant or a prohibition, a grant covering one
65// whose rule is fixed, and a ref name standing for another ref.
66func TestNoRuleStandsInForAnother(t *testing.T) {
67 const zero = "0000000000000000000000000000000000000000"
68 const some = "1111111111111111111111111111111111111111"
69 p := policy{
70 project: "everything",
71 push: pushPolicy{
72 // Every ref the project has is protected, and the writer is
73 // also opened every ref by a grant.
74 Refs: []string{"refs/*"},
75 Writers: []string{"writer"},
76 Grants: map[string]refGrant{
77 "everything": {Refs: []string{"refs/*"}, Writers: []string{"writer", "opened"}},
78 },
79 },
80 grants: grants{"requester": {"request": true}},
81 repo: fakeRepository{},
82 symrefs: map[string]bool{"refs/heads/alias": true},
83 }
84 for _, c := range []struct {
85 who, ref string
86 allowed bool
87 says string
88 }{
89 // Prohibited outright, to the writer the grant and the protection
90 // both name.
91 {"writer", "refs/replace/" + some, false, "refs/replace/ is closed to every push"},
92 {"writer", "refs/notes/commits", false, "refs/notes/ is closed to every push"},
93 {"writer", "refs/the-valley/integration-outcomes/main/c", false, "written only by the machinery"},
94 {"writer", "refs/the-valley/integration-landings/main/" + some, false, "written only by the machinery"},
95 // A symbolic ref is refused whoever writes it.
96 {"writer", "refs/heads/alias", false, "symbolic ref"},
97 // The approve grant is needed whatever else is declared, and
98 // protection is needed on top of it.
99 {"writer", "refs/the-valley/integration-requests/main/c", false, "approve grant (also accepted as request)"},
100 {"requester", "refs/the-valley/integration-requests/main/c", false, "a protected ref"},
101 // A tag takes a grant, and protection on top: being opened the
102 // ref does not make a principal its writer.
103 {"writer", "refs/tags/v1", true, ""},
104 {"opened", "refs/tags/v1", false, "a protected ref"},
105 {"requester", "refs/tags/v1", false, "opens only through a named grant"},
106 // An attestation named for a digest alone is refused before
107 // anything is read: it would block every ref beneath it.
108 {"writer", "refs/the-valley/attestations/" + strings.Repeat("a", 64), false, "<digest>/<key hash>"},
109 // The plan graph is one ref, matched exactly. A ref that only
110 // begins with its name is in the closed namespace, and the graph
111 // takes no write from a key that names no principal, before
112 // anything is read.
113 {"writer", knot.Ref + "-x", false, "written only by the machinery"},
114 {"writer", knot.Ref + "/x", false, "written only by the machinery"},
115 {"", knot.Ref, false, "takes writes only from a named principal"},
116 } {
117 got := p.decide(c.who, update{old: zero, new: some, ref: c.ref})
118 if (got == "") != c.allowed || !strings.Contains(got, c.says) {
119 t.Errorf("%s writing %s: got %q, want allowed=%v saying %q", c.who, c.ref, got, c.allowed, c.says)
120 }
121 }
122}
123
124// The plan graph is never deleted, by anyone, before anything is read.
125func TestNoPushDeletesThePlanGraph(t *testing.T) {
126 p := policy{project: "p", repo: fakeRepository{}}
127 got := p.decide("writer", update{old: "1111111111111111111111111111111111111111", new: "0000000000000000000000000000000000000000", ref: knot.Ref})
128 if !strings.Contains(got, "may not delete refs/the-valley/knots") || !strings.Contains(got, "never deleted") {
129 t.Errorf("a delete of the graph: %q", got)
130 }
131}