gunk-dev/the-valley

policy_test.go

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