| 1 | package pushpolicy |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 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. |
| 13 | func 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/app/v1", true}, |
| 26 | {"refs/tags/release/*/v*", "refs/tags/release/app/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 | func write(t *testing.T, text string) string { |
| 39 | t.Helper() |
| 40 | path := filepath.Join(t.TempDir(), "push-policy.json") |
| 41 | if err := os.WriteFile(path, []byte(text), 0o644); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | return path |
| 45 | } |
| 46 | |
| 47 | // A policy reads as the host module writes it, and answers the two |
| 48 | // questions a reader of approvals asks of it: whether the project retains |
| 49 | // its change records, and whether a target is protected with no writer. |
| 50 | // Left out, retention is off. |
| 51 | func TestAPolicyReadsAsTheHostWritesIt(t *testing.T) { |
| 52 | for _, c := range []struct { |
| 53 | json string |
| 54 | retains bool |
| 55 | sealed, guards bool |
| 56 | }{ |
| 57 | {`{"grants":{},"refs":["refs/heads/main"],"writers":[]}`, false, true, true}, |
| 58 | {`{"grants":{},"refs":["refs/heads/main"],"retainChangeRecords":true,"writers":[]}`, true, true, true}, |
| 59 | {`{"grants":{},"refs":["refs/heads/*"],"writers":[]}`, false, true, true}, |
| 60 | {`{"grants":{},"refs":["refs/heads/main"],"writers":["integrator"]}`, false, false, true}, |
| 61 | {`{"grants":{},"refs":["refs/heads/release/*"],"writers":[]}`, false, false, false}, |
| 62 | {`{"grants":{"release-tags":{"refs":["refs/heads/main"],"writers":["integrator"]}},"refs":[],"writers":[]}`, false, false, false}, |
| 63 | } { |
| 64 | p, err := Read(write(t, c.json)) |
| 65 | if err != nil { |
| 66 | t.Errorf("%s: %v", c.json, err) |
| 67 | continue |
| 68 | } |
| 69 | if p.RetainChangeRecords != c.retains || p.Sealed("refs/heads/main") != c.sealed || p.Protects("refs/heads/main") != c.guards { |
| 70 | t.Errorf("%s: retains %v, seals main %v, protects main %v", c.json, p.RetainChangeRecords, p.Sealed("refs/heads/main"), p.Protects("refs/heads/main")) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // A policy holding a field this package does not know, a key spelled |
| 76 | // another way than its field, a key twice in one object, or a value of |
| 77 | // another kind does not read, and neither does a file that is not there. |
| 78 | // Each refusal names the file and says what is wrong in words, never in |
| 79 | // Go's type names, which the hook passes on to the pusher. |
| 80 | func TestAPolicyThatDoesNotReadIsRefused(t *testing.T) { |
| 81 | for _, c := range []struct{ what, text, says string }{ |
| 82 | {"an unknown field", `{"grants":{},"refs":[],"writers":[],"sealed":true}`, |
| 83 | `the document holds the key "sealed", which is none of its fields`}, |
| 84 | {"Writers beside writers", `{"grants":{},"refs":["refs/heads/main"],"writers":[],"Writers":["anyone"]}`, |
| 85 | `the document holds the key "Writers", which is its field "writers" spelled another way`}, |
| 86 | {"writers twice", `{"grants":{},"refs":["refs/heads/main"],"writers":[],"writers":["anyone"]}`, |
| 87 | `the document holds the key "writers" twice`}, |
| 88 | {"retention as a string", `{"grants":{},"refs":[],"writers":[],"retainChangeRecords":"true"}`, |
| 89 | `retainChangeRecords is a string, where a boolean belongs`}, |
| 90 | {"refs as a string", `{"grants":{},"refs":"refs/heads/main","writers":[]}`, |
| 91 | `refs is a string, where an array belongs`}, |
| 92 | {"grants as an array", `{"grants":[],"refs":[],"writers":[]}`, |
| 93 | `grants is an array, where an object belongs`}, |
| 94 | {"a grant's refs as an object", `{"grants":{"g":{"refs":{},"writers":[]}},"refs":[],"writers":[]}`, |
| 95 | `grants["g"].refs is an object, where an array belongs`}, |
| 96 | {"the document as an array", `[]`, `the document is an array, where an object belongs`}, |
| 97 | {"a grant's unknown field", `{"grants":{"g":{"refs":[],"writers":[],"readers":[]}},"refs":[],"writers":[]}`, |
| 98 | `grants["g"] holds the key "readers", which is none of its fields`}, |
| 99 | {"a second document after one", `{"grants":{},"refs":[],"writers":[]}{}`, |
| 100 | `the document holds more than one JSON value`}, |
| 101 | } { |
| 102 | path := write(t, c.text) |
| 103 | p, err := Read(path) |
| 104 | if err == nil || err.Error() != path+": "+c.says { |
| 105 | t.Errorf("%s read as %+v, %v, not %q", c.what, p, err, c.says) |
| 106 | } |
| 107 | } |
| 108 | if p, err := Read(filepath.Join(t.TempDir(), "absent.json")); err == nil { |
| 109 | t.Errorf("a missing policy read as %+v", p) |
| 110 | } |
| 111 | } |