gunk-dev/the-valley

policy_test.go

sourced4ea94f6bcd7main'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 the symbolic refs of a repository and no objects.
39type fakeRepository struct{ symrefs map[string]bool }
40
41func (f fakeRepository) symbolic(ref string) (bool, error) { return f.symrefs[ref], 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{symrefs: map[string]bool{"refs/heads/alias": true}},
72 }
73 for _, c := range []struct {
74 who, ref string
75 allowed bool
76 says string
77 }{
78 // Prohibited outright, to the writer the grant and the protection
79 // both name.
80 {"writer", "refs/replace/" + some, false, "refs/replace/ is closed to every push"},
81 {"writer", "refs/notes/commits", false, "refs/notes/ is closed to every push"},
82 {"writer", "refs/the-valley/integration-outcomes/main/c", false, "written only by the machinery"},
83 // A symbolic ref is refused whoever writes it.
84 {"writer", "refs/heads/alias", false, "symbolic ref"},
85 // The request grant is needed whatever else is declared, and
86 // protection is needed on top of it.
87 {"writer", "refs/the-valley/integration-requests/main/c", false, "request grant"},
88 {"requester", "refs/the-valley/integration-requests/main/c", false, "a protected ref"},
89 // A tag takes a grant, and protection on top: being opened the
90 // ref does not make a principal its writer.
91 {"writer", "refs/tags/v1", true, ""},
92 {"opened", "refs/tags/v1", false, "a protected ref"},
93 {"requester", "refs/tags/v1", false, "opens only through a named grant"},
94 // An attestation named for a digest alone is refused before
95 // anything is read: it would block every ref beneath it.
96 {"writer", "refs/the-valley/attestations/" + strings.Repeat("a", 64), false, "<digest>/<key hash>"},
97 } {
98 got := p.decide(c.who, update{old: zero, new: some, ref: c.ref})
99 if (got == "") != c.allowed || !strings.Contains(got, c.says) {
100 t.Errorf("%s writing %s: got %q, want allowed=%v saying %q", c.who, c.ref, got, c.allowed, c.says)
101 }
102 }
103}