gunk-dev/the-valley

policy_test.go

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