gunk-dev/the-valley

push_test.go

sourced040e13810bfmain's published tip history
3.1 KiB
1package main
2
3import (
4 "bytes"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10)
11
12// pushTo runs preReceive over the lines as git would hand them to the
13// hook, as who, against repo, under a policy that protects nothing.
14func pushTo(t *testing.T, repo repository, who string, lines []string) (int, string) {
15 t.Helper()
16 dir := t.TempDir()
17 policyFile := filepath.Join(dir, "policy.json")
18 if err := os.WriteFile(policyFile, []byte(`{"refs": [], "writers": [], "grants": {}}`), 0o644); err != nil {
19 t.Fatal(err)
20 }
21 var stderr bytes.Buffer
22 code := preReceive([]string{"--project", "project", "--policy", policyFile, "--principal", who},
23 strings.NewReader(strings.Join(lines, "\n")+"\n"), &stderr, repo)
24 return code, stderr.String()
25}
26
27// A push names as many refs as its sender likes. The hook keeps at most
28// maxUpdates of them, so it asks git about at most that many.
29func TestAPushIsBoundInTheRefsItUpdates(t *testing.T) {
30 r := newRecords(t)
31 h := r.source("one")
32 topics := func(n int) []string {
33 var lines []string
34 for i := 0; i < n; i++ {
35 lines = append(lines, fmt.Sprintf("%s %s refs/heads/topic/%04d", zeroID, h, i))
36 }
37 return lines
38 }
39
40 // One past the bound is refused before any ref is looked up: this
41 // repository fails the test if one is.
42 code, says := pushTo(t, unreadable{t}, "alice", topics(maxUpdates+1))
43 if code == 0 || !strings.Contains(says, "it updates more than 1000 refs, the most one push may") {
44 t.Errorf("1001 updates: exit %d, %q", code, says)
45 }
46 if code, says := pushTo(t, gitRepository{}, "alice", topics(maxUpdates)); code != 0 {
47 t.Errorf("1000 updates: exit %d, %q", code, says)
48 }
49
50 // A symbolic ref among many is still found, in every form git reads
51 // as one: written by git symbolic-ref, naming a ref that does not exist,
52 // "ref:" followed by a tab or by nothing, and a filesystem symlink.
53 // Each would have a push write the ref it names.
54 r.git("", "update-ref", "refs/heads/real", h)
55 r.git("", "symbolic-ref", "refs/heads/alias", "refs/heads/real")
56 r.git("", "symbolic-ref", "refs/heads/dangling", "refs/heads/main")
57 heads := filepath.Join(r.dir, "refs", "heads")
58 for name, content := range map[string]string{
59 "tabbed": "ref:\trefs/heads/real\n",
60 "tight": "ref:refs/heads/real\n",
61 } {
62 if err := os.WriteFile(filepath.Join(heads, name), []byte(content), 0o644); err != nil {
63 t.Fatal(err)
64 }
65 }
66 if err := os.Symlink("refs/heads/real", filepath.Join(heads, "linked")); err != nil {
67 t.Fatal(err)
68 }
69 for _, alias := range []string{"alias", "dangling", "tabbed", "tight", "linked"} {
70 ref := "refs/heads/" + alias
71 lines := append(topics(maxUpdates-1), fmt.Sprintf("%s %s %s", zeroID, h, ref))
72 code, says := pushTo(t, gitRepository{}, "alice", lines)
73 want := "valley: alice may not write " + ref + " — it is a symbolic ref in this repository, and a push to it would write the ref it points at instead"
74 if code == 0 || !strings.Contains(says, want) {
75 t.Errorf("%s among %d updates: exit %d, %q", ref, len(lines), code, says)
76 }
77 if strings.Count(says, "\n") != 1 {
78 t.Errorf("%s among %d updates: refused more than the symbolic ref: %q", ref, len(lines), says)
79 }
80 }
81}