gunk-dev/the-valley

policy_test.go

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