| 1 | package delta |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/hex" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "sort" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | // repo is a scratch git repository the tests write objects into. |
| 16 | type repo struct { |
| 17 | t *testing.T |
| 18 | dir string |
| 19 | } |
| 20 | |
| 21 | func newRepo(t *testing.T) repo { |
| 22 | t.Helper() |
| 23 | r := repo{t: t, dir: t.TempDir()} |
| 24 | r.git(nil, "init", "--quiet") |
| 25 | return r |
| 26 | } |
| 27 | |
| 28 | func (r repo) git(stdin []byte, args ...string) string { |
| 29 | r.t.Helper() |
| 30 | cmd := exec.Command("git", args...) |
| 31 | cmd.Dir = r.dir |
| 32 | cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "HOME="+r.dir, |
| 33 | "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@localhost", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@localhost") |
| 34 | cmd.Stdin = bytes.NewReader(stdin) |
| 35 | var stderr bytes.Buffer |
| 36 | cmd.Stderr = &stderr |
| 37 | out, err := cmd.Output() |
| 38 | if err != nil { |
| 39 | r.t.Fatalf("git %s: %v: %s", strings.Join(args, " "), err, stderr.String()) |
| 40 | } |
| 41 | return strings.TrimSpace(string(out)) |
| 42 | } |
| 43 | |
| 44 | func (r repo) blob(content []byte) string { |
| 45 | return r.git(content, "hash-object", "-w", "--stdin") |
| 46 | } |
| 47 | |
| 48 | // rawTree writes a tree object exactly as given, past every check git makes. |
| 49 | func (r repo) rawTree(entries ...rawEntry) string { |
| 50 | var b bytes.Buffer |
| 51 | for _, e := range entries { |
| 52 | b.WriteString(e.mode + " " + e.name + "\x00") |
| 53 | raw, err := hex.DecodeString(e.oid) |
| 54 | if err != nil { |
| 55 | r.t.Fatal(err) |
| 56 | } |
| 57 | b.Write(raw) |
| 58 | } |
| 59 | return r.git(b.Bytes(), "hash-object", "-t", "tree", "--literally", "-w", "--stdin") |
| 60 | } |
| 61 | |
| 62 | type rawEntry struct{ mode, name, oid string } |
| 63 | |
| 64 | // file is one file of a vector's tree. |
| 65 | type file struct { |
| 66 | path, mode string |
| 67 | content []byte |
| 68 | } |
| 69 | |
| 70 | // tree writes the tree that holds exactly the files given, with git's own |
| 71 | // mktree, which orders each tree's entries. |
| 72 | func (r repo) tree(files []file) string { |
| 73 | type dir struct { |
| 74 | files []file |
| 75 | } |
| 76 | here := map[string]string{} |
| 77 | sub := map[string]*dir{} |
| 78 | for _, f := range files { |
| 79 | name, rest, nested := strings.Cut(f.path, "/") |
| 80 | if !nested { |
| 81 | here[name] = f.mode + " blob " + r.blob(f.content) |
| 82 | continue |
| 83 | } |
| 84 | if sub[name] == nil { |
| 85 | sub[name] = &dir{} |
| 86 | } |
| 87 | sub[name].files = append(sub[name].files, file{path: rest, mode: f.mode, content: f.content}) |
| 88 | } |
| 89 | for name, d := range sub { |
| 90 | here[name] = "040000 tree " + r.tree(d.files) |
| 91 | } |
| 92 | names := make([]string, 0, len(here)) |
| 93 | for name := range here { |
| 94 | names = append(names, name) |
| 95 | } |
| 96 | sort.Strings(names) |
| 97 | var input bytes.Buffer |
| 98 | for _, name := range names { |
| 99 | input.WriteString(here[name] + "\t" + name + "\x00") |
| 100 | } |
| 101 | return r.git(input.Bytes(), "mktree", "-z") |
| 102 | } |
| 103 | |
| 104 | func (r repo) commit(tree string) string { |
| 105 | return r.git(nil, "commit-tree", tree, "-m", "c") |
| 106 | } |
| 107 | |
| 108 | func (r repo) repo() Repo { return Repo{Dir: r.dir} } |
| 109 | |
| 110 | // specFile is one file of a vector's tree, as valid/<name>.json lists it. |
| 111 | type specFile struct { |
| 112 | Path string `json:"path"` |
| 113 | PathHex string `json:"path_hex"` |
| 114 | Mode string `json:"mode"` |
| 115 | Content string `json:"content"` |
| 116 | ContentHex string `json:"content_hex"` |
| 117 | } |
| 118 | |
| 119 | func readVector(t *testing.T, name string) (pre, post []file) { |
| 120 | t.Helper() |
| 121 | data, err := os.ReadFile(name) |
| 122 | if err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | var v struct { |
| 126 | Pre []specFile `json:"pre"` |
| 127 | Post []specFile `json:"post"` |
| 128 | } |
| 129 | if err := json.Unmarshal(data, &v); err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | decode := func(list []specFile) []file { |
| 133 | var out []file |
| 134 | for _, f := range list { |
| 135 | path, content := []byte(f.Path), []byte(f.Content) |
| 136 | if f.PathHex != "" { |
| 137 | path = mustHex(t, f.PathHex) |
| 138 | } |
| 139 | if f.ContentHex != "" { |
| 140 | content = mustHex(t, f.ContentHex) |
| 141 | } |
| 142 | out = append(out, file{path: string(path), mode: f.Mode, content: content}) |
| 143 | } |
| 144 | return out |
| 145 | } |
| 146 | return decode(v.Pre), decode(v.Post) |
| 147 | } |
| 148 | |
| 149 | func mustHex(t *testing.T, s string) []byte { |
| 150 | t.Helper() |
| 151 | b, err := hex.DecodeString(s) |
| 152 | if err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | return b |
| 156 | } |
| 157 | |
| 158 | // Each valid vector's trees, built with real git, give exactly the encoding |
| 159 | // and digest vectors.py computed from the specification, whether they are |
| 160 | // named as trees or as commits. The encoding parses, and writes back byte |
| 161 | // for byte. |
| 162 | func TestTheVectorsHold(t *testing.T) { |
| 163 | r := newRepo(t) |
| 164 | specs, err := filepath.Glob("testdata/valid/*.json") |
| 165 | if err != nil || len(specs) == 0 { |
| 166 | t.Fatalf("no vectors: %v", err) |
| 167 | } |
| 168 | for _, spec := range specs { |
| 169 | name := strings.TrimSuffix(filepath.Base(spec), ".json") |
| 170 | t.Run(name, func(t *testing.T) { |
| 171 | want, err := os.ReadFile(strings.TrimSuffix(spec, ".json") + ".delta") |
| 172 | if err != nil { |
| 173 | t.Fatal(err) |
| 174 | } |
| 175 | digest, err := os.ReadFile(strings.TrimSuffix(spec, ".json") + ".sha256") |
| 176 | if err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | pre, post := readVector(t, spec) |
| 180 | preTree, postTree := r.tree(pre), r.tree(post) |
| 181 | for _, ids := range [][2]string{{preTree, postTree}, {r.commit(preTree), r.commit(postTree)}} { |
| 182 | d, err := Between(r.repo(), ids[0], ids[1]) |
| 183 | if err != nil { |
| 184 | t.Fatalf("Between: %v", err) |
| 185 | } |
| 186 | if got := d.Encode(); !bytes.Equal(got, want) { |
| 187 | t.Fatalf("the encoding is\n%q\nwhere vectors.py has\n%q", got, want) |
| 188 | } |
| 189 | if got := d.Digest(); got != strings.TrimSpace(string(digest)) { |
| 190 | t.Fatalf("the digest is %s where vectors.py has %s", got, digest) |
| 191 | } |
| 192 | } |
| 193 | parsed, err := Parse(want) |
| 194 | if err != nil { |
| 195 | t.Fatalf("Parse: %v", err) |
| 196 | } |
| 197 | if !bytes.Equal(parsed.Encode(), want) { |
| 198 | t.Fatalf("the parsed encoding writes back as\n%q", parsed.Encode()) |
| 199 | } |
| 200 | }) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // The delta of a tree against itself is the header alone, and the delta |
| 205 | // back again swaps every pre-image with its post-image. |
| 206 | func TestADeltaReversesExactly(t *testing.T) { |
| 207 | r := newRepo(t) |
| 208 | pre, post := readVector(t, "testdata/valid/mixed.json") |
| 209 | a, b := r.tree(pre), r.tree(post) |
| 210 | same, err := Between(r.repo(), a, a) |
| 211 | if err != nil || string(same.Encode()) != Header { |
| 212 | t.Fatalf("a tree against itself: %q, %v", same.Encode(), err) |
| 213 | } |
| 214 | forth, err := Between(r.repo(), a, b) |
| 215 | if err != nil { |
| 216 | t.Fatal(err) |
| 217 | } |
| 218 | back, err := Between(r.repo(), b, a) |
| 219 | if err != nil { |
| 220 | t.Fatal(err) |
| 221 | } |
| 222 | if len(forth) != len(back) || len(forth) == 0 { |
| 223 | t.Fatalf("%d changes forth and %d back", len(forth), len(back)) |
| 224 | } |
| 225 | for i := range forth { |
| 226 | if forth[i].Path != back[i].Path || forth[i].Pre != back[i].Post || forth[i].Post != back[i].Pre { |
| 227 | t.Fatalf("%q does not reverse: %+v against %+v", forth[i].Path, forth[i], back[i]) |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // Every malformed vector is refused. |
| 233 | func TestMalformedEncodingsAreRefused(t *testing.T) { |
| 234 | vectors, err := filepath.Glob("testdata/malformed/*.delta") |
| 235 | if err != nil || len(vectors) == 0 { |
| 236 | t.Fatalf("no vectors: %v", err) |
| 237 | } |
| 238 | for _, name := range vectors { |
| 239 | data, err := os.ReadFile(name) |
| 240 | if err != nil { |
| 241 | t.Fatal(err) |
| 242 | } |
| 243 | if d, err := Parse(data); err == nil { |
| 244 | t.Errorf("%s parsed, as %d changes", filepath.Base(name), len(d)) |
| 245 | } |
| 246 | } |
| 247 | } |