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