| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os/exec" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "the-valley/change" |
| 11 | ) |
| 12 | |
| 13 | // A git tree can name the same subtree any number of times, so a handful |
| 14 | // of small objects can describe more paths than any walk could visit: the |
| 15 | // deep tree below is nine trees and one blob, and describes 64^9 paths. |
| 16 | // Each case is a tree like that pushed as an attestation, and each has to |
| 17 | // be refused at a bound, quickly, rather than walked. |
| 18 | func TestAWalkStopsAtItsBoundsBeforeReadingPastThem(t *testing.T) { |
| 19 | dir := t.TempDir() |
| 20 | gitIn(t, dir, "", "init", "--quiet", "--bare", dir) |
| 21 | t.Setenv("GIT_DIR", dir) |
| 22 | |
| 23 | note := gitIn(t, dir, "a note\n", "hash-object", "-w", "--stdin") |
| 24 | leaf := mktree(t, dir, 1, "100644 blob "+note, "statement.note") |
| 25 | sharing := func(child string, levels, width int) string { |
| 26 | id := child |
| 27 | for i := 0; i < levels; i++ { |
| 28 | id = mktree(t, dir, width, "040000 tree "+id, "t") |
| 29 | } |
| 30 | return id |
| 31 | } |
| 32 | big := gitIn(t, dir, strings.Repeat("x", maxNoteSize+1), "hash-object", "-w", "--stdin") |
| 33 | |
| 34 | for _, c := range []struct { |
| 35 | what, tree, says string |
| 36 | }{ |
| 37 | {"one subtree named 64 times over, nine levels deep", sharing(leaf, 9, 64), "deeper than"}, |
| 38 | {"one subtree named 64 times over, within the depth", sharing(leaf, 2, 64), "entries in all"}, |
| 39 | {"a tree wider than any attestation's", mktree(t, dir, maxEntriesInTree+1, "100644 blob "+note, "n"), "more than 64 entries"}, |
| 40 | {"a note larger than any statement", mktree(t, dir, 1, "100644 blob "+big, "statement.note"), "more than the 65536"}, |
| 41 | {"an executable where a note goes", mktree(t, dir, 1, "100755 blob "+note, "statement.note"), "mode 100755"}, |
| 42 | } { |
| 43 | start := time.Now() |
| 44 | _, err := gitRepository{}.notes(c.tree) |
| 45 | if err == nil || !strings.Contains(err.Error(), c.says) { |
| 46 | t.Errorf("%s: error = %v, want one saying %q", c.what, err, c.says) |
| 47 | } |
| 48 | if elapsed := time.Since(start); elapsed > 5*time.Second { |
| 49 | t.Errorf("%s: refused only after %s", c.what, elapsed) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // A tree past the size bound is refused on its type and size alone: |
| 54 | // its content is never asked for. |
| 55 | wide := mktree(t, dir, 400, "100644 blob "+note, "a-name-long-enough-to-make-the-tree-large-") |
| 56 | objects, err := openObjects() |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | _, err = walkNotes(objects, wide) |
| 61 | objects.close() |
| 62 | if err == nil || !strings.Contains(err.Error(), "more than the 16384") { |
| 63 | t.Errorf("an oversized tree: error = %v", err) |
| 64 | } |
| 65 | if got := strings.Join(objects.requests, "; "); got != "info "+wide { |
| 66 | t.Errorf("an oversized tree: requests = %s, want its info alone", got) |
| 67 | } |
| 68 | |
| 69 | // And what attest writes is read whole. |
| 70 | attested := mktree(t, dir, 1, "040000 tree "+leaf, "prose-format") |
| 71 | notes, err := gitRepository{}.notes(attested) |
| 72 | if err != nil || string(notes["prose-format/statement.note"]) != "a note\n" { |
| 73 | t.Errorf("an attestation's tree: notes = %q, error = %v", notes, err) |
| 74 | } |
| 75 | |
| 76 | // So is what the integrator writes: a transfer statement at |
| 77 | // change.TransferPath, beside a countersigned note. One level deeper |
| 78 | // is past the bound. |
| 79 | commit := strings.Repeat("ab", 20) |
| 80 | path := change.TransferPath(commit) |
| 81 | fanned := func(levels []string) string { |
| 82 | id := mktree(t, dir, 1, "100644 blob "+note, levels[len(levels)-1]) |
| 83 | for i := len(levels) - 2; i >= 0; i-- { |
| 84 | id = mktree(t, dir, 1, "040000 tree "+id, levels[i]) |
| 85 | } |
| 86 | return id |
| 87 | } |
| 88 | transfer := fanned(strings.Split(path, "/")[1:]) |
| 89 | landed := gitIn(t, dir, "040000 tree "+leaf+"\tprose-format\n040000 tree "+transfer+"\ttransfer.d\n", "mktree") |
| 90 | notes, err = gitRepository{}.notes(landed) |
| 91 | if err != nil || string(notes[path]) != "a note\n" || len(notes) != 2 { |
| 92 | t.Errorf("the integrator's tree: notes = %q, error = %v", notes, err) |
| 93 | } |
| 94 | deeper := fanned(append(strings.Split(path, "/")[:3], "ef", "statement.note")) |
| 95 | if _, err := (gitRepository{}).notes(deeper); err == nil || !strings.Contains(err.Error(), "deeper than the 4") { |
| 96 | t.Errorf("a tree one level deeper than the integrator's: error = %v", err) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // mktree writes a tree of n entries, each "<entry>\t<name><index>". |
| 101 | func mktree(t *testing.T, dir string, n int, entry, name string) string { |
| 102 | t.Helper() |
| 103 | var lines []string |
| 104 | for i := 0; i < n; i++ { |
| 105 | suffix := "" |
| 106 | if n > 1 { |
| 107 | suffix = fmt.Sprintf("%03d", i) |
| 108 | } |
| 109 | lines = append(lines, entry+"\t"+name+suffix) |
| 110 | } |
| 111 | return gitIn(t, dir, strings.Join(lines, "\n")+"\n", "mktree") |
| 112 | } |
| 113 | |
| 114 | func gitIn(t *testing.T, dir, stdin string, args ...string) string { |
| 115 | t.Helper() |
| 116 | cmd := exec.Command("git", args...) |
| 117 | cmd.Dir = dir |
| 118 | cmd.Stdin = strings.NewReader(stdin) |
| 119 | out, err := cmd.CombinedOutput() |
| 120 | if err != nil { |
| 121 | t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 122 | } |
| 123 | return strings.TrimSpace(string(out)) |
| 124 | } |