gunk-dev/the-valley

walk_test.go

source6063990989b7main's published tip history
2.9 KiB
1package main
2
3import (
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.
16func 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 // And what attest writes is read whole.
52 attested := mktree(t, dir, 1, "040000 tree "+leaf, "prose-format")
53 notes, err := gitRepository{}.notes(attested)
54 if err != nil || string(notes["prose-format/statement.note"]) != "a note\n" {
55 t.Errorf("an attestation's tree: notes = %q, error = %v", notes, err)
56 }
57}
58
59// mktree writes a tree of n entries, each "<entry>\t<name><index>".
60func mktree(t *testing.T, dir string, n int, entry, name string) string {
61 t.Helper()
62 var lines []string
63 for i := 0; i < n; i++ {
64 suffix := ""
65 if n > 1 {
66 suffix = fmt.Sprintf("%03d", i)
67 }
68 lines = append(lines, entry+"\t"+name+suffix)
69 }
70 return gitIn(t, dir, strings.Join(lines, "\n")+"\n", "mktree")
71}
72
73func gitIn(t *testing.T, dir, stdin string, args ...string) string {
74 t.Helper()
75 cmd := exec.Command("git", args...)
76 cmd.Dir = dir
77 cmd.Stdin = strings.NewReader(stdin)
78 out, err := cmd.CombinedOutput()
79 if err != nil {
80 t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
81 }
82 return strings.TrimSpace(string(out))
83}