gunk-dev/the-valley

policy_test.go

source58e68be0d151main's published tip history
6.0 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) approval(string) ([]byte, error) {
54 return nil, errors.New("holds no objects")
55}
56func (f fakeRepository) hasRecord(string) (bool, error) {
57 return false, errors.New("holds no refs")
58}
59func (f fakeRepository) ref(string) (string, bool, error) { return "", false, nil }
60func (f fakeRepository) kind(string) (string, error) { return "", errors.New("holds no objects") }
61func (f fakeRepository) records() (int, error) { return 0, nil }
62func (f fakeRepository) checkRequests(string) (int, error) { return 0, nil }
63func (f fakeRepository) graph() (graphReader, error) { return nil, errors.New("holds no objects") }
64
65// Every rule that applies to a ref has to allow the write, and none of
66// them can be satisfied by another. Each case here is a declaration that
67// tries to stand in for a rule it is not: a writer list covering a
68// namespace whose rule is a grant or a prohibition, a grant covering one
69// whose rule is fixed, and a ref name standing for another ref.
70func TestNoRuleStandsInForAnother(t *testing.T) {
71 const zero = "0000000000000000000000000000000000000000"
72 const some = "1111111111111111111111111111111111111111"
73 p := policy{
74 project: "everything",
75 push: pushPolicy{
76 // Every ref the project has is protected, and the writer is
77 // also opened every ref by a grant.
78 Refs: []string{"refs/*"},
79 Writers: []string{"writer"},
80 Grants: map[string]refGrant{
81 "everything": {Refs: []string{"refs/*"}, Writers: []string{"writer", "opened"}},
82 },
83 },
84 grants: grants{"requester": {"request": true}},
85 repo: fakeRepository{},
86 symrefs: map[string]bool{"refs/heads/alias": true},
87 }
88 for _, c := range []struct {
89 who, ref string
90 allowed bool
91 says string
92 }{
93 // Prohibited outright, to the writer the grant and the protection
94 // both name.
95 {"writer", "refs/replace/" + some, false, "refs/replace/ is closed to every push"},
96 {"writer", "refs/notes/commits", false, "refs/notes/ is closed to every push"},
97 {"writer", "refs/the-valley/integration-outcomes/main/c", false, "written only by the machinery"},
98 {"writer", "refs/the-valley/integration-landings/main/" + some, false, "written only by the machinery"},
99 // A symbolic ref is refused whoever writes it.
100 {"writer", "refs/heads/alias", false, "symbolic ref"},
101 // The approve grant is needed whatever else is declared, and
102 // protection is needed on top of it.
103 {"writer", "refs/the-valley/integration-requests/main/c", false, "approve grant (also accepted as request)"},
104 {"requester", "refs/the-valley/integration-requests/main/c", false, "a protected ref"},
105 // A tag takes a grant, and protection on top: being opened the
106 // ref does not make a principal its writer.
107 {"writer", "refs/tags/v1", true, ""},
108 {"opened", "refs/tags/v1", false, "a protected ref"},
109 {"requester", "refs/tags/v1", false, "opens only through a named grant"},
110 // An attestation named for a digest alone is refused before
111 // anything is read: it would block every ref beneath it.
112 {"writer", "refs/the-valley/attestations/" + strings.Repeat("a", 64), false, "<digest>/<key hash>"},
113 // The plan graph is one ref, matched exactly. A ref that only
114 // begins with its name is in the closed namespace, and the graph
115 // takes no write from a key that names no principal, before
116 // anything is read.
117 {"writer", knot.Ref + "-x", false, "written only by the machinery"},
118 {"writer", knot.Ref + "/x", false, "written only by the machinery"},
119 {"", knot.Ref, false, "takes writes only from a named principal"},
120 } {
121 got := p.decide(c.who, update{old: zero, new: some, ref: c.ref})
122 if (got == "") != c.allowed || !strings.Contains(got, c.says) {
123 t.Errorf("%s writing %s: got %q, want allowed=%v saying %q", c.who, c.ref, got, c.allowed, c.says)
124 }
125 }
126}
127
128// The plan graph is never deleted, by anyone, before anything is read.
129func TestNoPushDeletesThePlanGraph(t *testing.T) {
130 p := policy{project: "p", repo: fakeRepository{}}
131 got := p.decide("writer", update{old: "1111111111111111111111111111111111111111", new: "0000000000000000000000000000000000000000", ref: knot.Ref})
132 if !strings.Contains(got, "may not delete refs/the-valley/knots") || !strings.Contains(got, "never deleted") {
133 t.Errorf("a delete of the graph: %q", got)
134 }
135}