gunk-dev/the-valley

policy_test.go

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