| 1 | package main |
| 2 | |
| 3 | import ( |
| 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. |
| 14 | func 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, and asks git about them once, not once per ref. |
| 29 | func 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, whether the ref it names |
| 51 | // exists or not. A dangling one would create the ref it names. |
| 52 | r.git("", "update-ref", "refs/heads/real", h) |
| 53 | r.git("", "symbolic-ref", "refs/heads/alias", "refs/heads/real") |
| 54 | r.git("", "symbolic-ref", "refs/heads/dangling", "refs/heads/main") |
| 55 | for _, alias := range []string{"refs/heads/alias", "refs/heads/dangling"} { |
| 56 | lines := append(topics(maxUpdates-1), fmt.Sprintf("%s %s %s", zeroID, h, alias)) |
| 57 | code, says := pushTo(t, gitRepository{}, "alice", lines) |
| 58 | want := "valley: alice may not write " + alias + " — it is a symbolic ref in this repository, and a push to it would write the ref it points at instead" |
| 59 | if code == 0 || !strings.Contains(says, want) { |
| 60 | t.Errorf("%s among %d updates: exit %d, %q", alias, len(lines), code, says) |
| 61 | } |
| 62 | if strings.Count(says, "\n") != 1 { |
| 63 | t.Errorf("%s among %d updates: refused more than the symbolic ref: %q", alias, len(lines), says) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Names git would refuse as refnames are never read as paths. |
| 68 | symrefs, err := gitRepository{}.symbolic([]string{"refs/heads/../../HEAD", "refs/heads/real", "refs/heads/alias"}) |
| 69 | if err != nil || symrefs["refs/heads/../../HEAD"] || symrefs["refs/heads/real"] || !symrefs["refs/heads/alias"] { |
| 70 | t.Errorf("symbolic = %v, %v", symrefs, err) |
| 71 | } |
| 72 | } |