| 1 | package export_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "syscall" |
| 14 | "testing" |
| 15 | |
| 16 | "the-valley/webui/export" |
| 17 | ) |
| 18 | |
| 19 | // git runs git for a test, with none of the caller's GIT_ variables and no |
| 20 | // system or global configuration, and returns its trimmed output. |
| 21 | func git(t *testing.T, dir string, stdin []byte, args ...string) string { |
| 22 | t.Helper() |
| 23 | if dir != "" { |
| 24 | args = append([]string{"-C", dir}, args...) |
| 25 | } |
| 26 | cmd := exec.Command("git", args...) |
| 27 | for _, kv := range os.Environ() { |
| 28 | if !strings.HasPrefix(kv, "GIT_") { |
| 29 | cmd.Env = append(cmd.Env, kv) |
| 30 | } |
| 31 | } |
| 32 | cmd.Env = append(cmd.Env, "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null", |
| 33 | "GIT_AUTHOR_NAME=Example Author", "GIT_AUTHOR_EMAIL=author@example.org", |
| 34 | "GIT_COMMITTER_NAME=Example Author", "GIT_COMMITTER_EMAIL=author@example.org", |
| 35 | "GIT_AUTHOR_DATE=2026-10-01T09:00:00Z", "GIT_COMMITTER_DATE=2026-10-01T09:00:00Z") |
| 36 | if stdin != nil { |
| 37 | cmd.Stdin = bytes.NewReader(stdin) |
| 38 | } |
| 39 | var stderr bytes.Buffer |
| 40 | cmd.Stderr = &stderr |
| 41 | out, err := cmd.Output() |
| 42 | if err != nil { |
| 43 | t.Fatalf("git %s: %v: %s", strings.Join(args, " "), err, stderr.String()) |
| 44 | } |
| 45 | return strings.TrimSpace(string(out)) |
| 46 | } |
| 47 | |
| 48 | // source is a bare repository whose main has two commits, first and tip, |
| 49 | // and whose side branch has a commit main does not reach. |
| 50 | type source struct { |
| 51 | repo, first, tip, side string |
| 52 | } |
| 53 | |
| 54 | func newSource(t *testing.T, format string) source { |
| 55 | t.Helper() |
| 56 | repo := filepath.Join(t.TempDir(), "source.git") |
| 57 | git(t, "", nil, "init", "--quiet", "--bare", "--initial-branch=main", "--object-format="+format, repo) |
| 58 | commit := func(parent, file, content string) string { |
| 59 | blob := git(t, repo, []byte(content), "hash-object", "-w", "--stdin") |
| 60 | tree := git(t, repo, []byte(fmt.Sprintf("100644 blob %s\t%s\n", blob, file)), "mktree") |
| 61 | args := []string{"commit-tree", tree, "-m", file} |
| 62 | if parent != "" { |
| 63 | args = append(args, "-p", parent) |
| 64 | } |
| 65 | return git(t, repo, nil, args...) |
| 66 | } |
| 67 | s := source{repo: repo} |
| 68 | s.first = commit("", "README.md", "# source\n") |
| 69 | s.tip = commit(s.first, "main.go", "package main\n") |
| 70 | s.side = commit(s.first, "side.txt", "not on main\n") |
| 71 | git(t, repo, nil, "update-ref", "refs/heads/main", s.tip) |
| 72 | git(t, repo, nil, "update-ref", "refs/heads/side", s.side) |
| 73 | return s |
| 74 | } |
| 75 | |
| 76 | func (s source) bundle(t *testing.T, args ...string) string { |
| 77 | t.Helper() |
| 78 | path := filepath.Join(t.TempDir(), "b.bundle") |
| 79 | git(t, s.repo, nil, append([]string{"bundle", "create", "--quiet", path}, args...)...) |
| 80 | return path |
| 81 | } |
| 82 | |
| 83 | // A full bundle of main at the declared tip is accepted into a fresh bare |
| 84 | // repository holding main alone. |
| 85 | func TestAcceptBundle(t *testing.T) { |
| 86 | ctx := context.Background() |
| 87 | for _, format := range []string{"sha1", "sha256"} { |
| 88 | t.Run(format, func(t *testing.T) { |
| 89 | s := newSource(t, format) |
| 90 | repo := filepath.Join(t.TempDir(), "app.git") |
| 91 | if err := export.AcceptBundle(ctx, s.bundle(t, "main"), repo, s.tip); err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | if refs := git(t, repo, nil, "for-each-ref", "--format=%(refname) %(objectname)"); refs != "refs/heads/main "+s.tip { |
| 95 | t.Errorf("the repository holds %q", refs) |
| 96 | } |
| 97 | if got := git(t, repo, nil, "rev-parse", "--show-object-format"); got != format { |
| 98 | t.Errorf("the repository is %s", got) |
| 99 | } |
| 100 | }) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // A bundle and a repository named relative to the working directory are |
| 105 | // read and written there. |
| 106 | func TestAcceptBundleTakesRelativePaths(t *testing.T) { |
| 107 | s := newSource(t, "sha1") |
| 108 | bundle := s.bundle(t, "main") |
| 109 | t.Chdir(filepath.Dir(bundle)) |
| 110 | if err := export.AcceptBundle(context.Background(), filepath.Base(bundle), "app.git", s.tip); err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | if _, err := os.Stat(filepath.Join(filepath.Dir(bundle), "app.git", "HEAD")); err != nil { |
| 114 | t.Error(err) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // What the acceptance procedure refuses, each leaving no repository. |
| 119 | func TestAcceptBundleRefuses(t *testing.T) { |
| 120 | ctx := context.Background() |
| 121 | s := newSource(t, "sha1") |
| 122 | good := s.bundle(t, "main") |
| 123 | corrupt := func() string { |
| 124 | data, err := os.ReadFile(good) |
| 125 | if err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | // Past the header, in the pack: flip one byte of the last object. |
| 129 | data[len(data)-30] ^= 0xff |
| 130 | path := filepath.Join(t.TempDir(), "corrupt.bundle") |
| 131 | if err := os.WriteFile(path, data, 0o644); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | return path |
| 135 | } |
| 136 | cases := []struct { |
| 137 | name, bundle, tip, says string |
| 138 | }{ |
| 139 | {"a wrong tip", good, s.first, "the manifest declares"}, |
| 140 | {"prerequisites", s.bundle(t, "main~1..main"), s.tip, "prerequisite"}, |
| 141 | {"an extra ref", s.bundle(t, "main", "side"), s.tip, "2 refs"}, |
| 142 | {"another ref than main", s.bundle(t, "side"), s.side, "refs/heads/side"}, |
| 143 | {"a corrupt pack", corrupt(), s.tip, "git fetch"}, |
| 144 | {"a malformed commit", malformedBundle(t), "", "fsck"}, |
| 145 | {"no bundle", filepath.Join(t.TempDir(), "missing.bundle"), s.tip, "no such file"}, |
| 146 | } |
| 147 | for _, c := range cases { |
| 148 | t.Run(c.name, func(t *testing.T) { |
| 149 | tip := c.tip |
| 150 | if tip == "" { |
| 151 | tip = bundleTip(t, c.bundle) |
| 152 | } |
| 153 | repo := filepath.Join(t.TempDir(), "app.git") |
| 154 | err := export.AcceptBundle(ctx, c.bundle, repo, tip) |
| 155 | if err == nil || !strings.Contains(err.Error(), c.says) { |
| 156 | t.Fatalf("AcceptBundle gave %v, want an error saying %q", err, c.says) |
| 157 | } |
| 158 | if c.name != "no bundle" { |
| 159 | wantCode(t, err, export.CodeBadBundle) |
| 160 | } |
| 161 | if _, err := os.Lstat(repo); !os.IsNotExist(err) { |
| 162 | t.Errorf("the refusal left %s behind", repo) |
| 163 | } |
| 164 | }) |
| 165 | } |
| 166 | t.Run("a repository that exists", func(t *testing.T) { |
| 167 | repo := t.TempDir() |
| 168 | if err := export.AcceptBundle(ctx, good, repo, s.tip); err == nil { |
| 169 | t.Fatal("AcceptBundle fetched into a directory that existed") |
| 170 | } |
| 171 | if _, err := os.Lstat(repo); err != nil { |
| 172 | t.Errorf("the refusal removed the directory that was there: %v", err) |
| 173 | } |
| 174 | }) |
| 175 | } |
| 176 | |
| 177 | // A bundle's header is read through a 64 KiB bound on the file. A header |
| 178 | // with no line end, a thousand times longer, is refused once the bound |
| 179 | // is reached: the receiver reads about 64 KiB of it, not the whole of it. |
| 180 | // The bundle is a named pipe, so the test counts what was read. |
| 181 | func TestAcceptBundleBoundsTheHeader(t *testing.T) { |
| 182 | path := filepath.Join(t.TempDir(), "endless.bundle") |
| 183 | if err := syscall.Mkfifo(path, 0o600); err != nil { |
| 184 | t.Fatal(err) |
| 185 | } |
| 186 | const offered = 64 << 20 |
| 187 | taken := make(chan int64, 1) |
| 188 | go func() { |
| 189 | w, err := os.OpenFile(path, os.O_WRONLY, 0) |
| 190 | if err != nil { |
| 191 | taken <- -1 |
| 192 | return |
| 193 | } |
| 194 | defer w.Close() |
| 195 | n, _ := w.Write([]byte("# v2 git bundle\n")) |
| 196 | total := int64(n) |
| 197 | chunk := bytes.Repeat([]byte("a"), 32<<10) |
| 198 | for total < offered { |
| 199 | n, err := w.Write(chunk) |
| 200 | total += int64(n) |
| 201 | if err != nil { |
| 202 | break // the receiver closed the bundle |
| 203 | } |
| 204 | } |
| 205 | taken <- total |
| 206 | }() |
| 207 | repo := filepath.Join(t.TempDir(), "app.git") |
| 208 | err := export.AcceptBundle(context.Background(), path, repo, strings.Repeat("1", 40)) |
| 209 | if err == nil || !strings.Contains(err.Error(), "longer than") { |
| 210 | t.Fatalf("AcceptBundle gave %v, want a refusal of the header's length", err) |
| 211 | } |
| 212 | wantCode(t, err, export.CodeBadBundle) |
| 213 | // What the pipe holds unread comes on top of what was read. |
| 214 | if n := <-taken; n < 0 || n > 4<<20 { |
| 215 | t.Errorf("the receiver took %d bytes of a header with no end, want about 64 KiB", n) |
| 216 | } |
| 217 | if _, err := os.Lstat(repo); !os.IsNotExist(err) { |
| 218 | t.Errorf("the refusal left %s behind", repo) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // bundleTip is the one ref a bundle's header names. |
| 223 | func bundleTip(t *testing.T, path string) string { |
| 224 | t.Helper() |
| 225 | return strings.Fields(git(t, "", nil, "bundle", "list-heads", path))[0] |
| 226 | } |
| 227 | |
| 228 | // malformedBundle is a bundle of a main whose tip is a commit fsck |
| 229 | // refuses: its author has no email and no date. |
| 230 | func malformedBundle(t *testing.T) string { |
| 231 | t.Helper() |
| 232 | repo := filepath.Join(t.TempDir(), "bad.git") |
| 233 | git(t, "", nil, "init", "--quiet", "--bare", "--initial-branch=main", repo) |
| 234 | tree := git(t, repo, nil, "mktree") |
| 235 | bad := git(t, repo, []byte("tree "+tree+"\nauthor nobody\ncommitter nobody\n\nmalformed\n"), |
| 236 | "hash-object", "-t", "commit", "--literally", "-w", "--stdin") |
| 237 | git(t, repo, nil, "update-ref", "refs/heads/main", bad) |
| 238 | path := filepath.Join(t.TempDir(), "bad.bundle") |
| 239 | git(t, repo, nil, "bundle", "create", "--quiet", path, "main") |
| 240 | return path |
| 241 | } |
| 242 | |
| 243 | // unreachableBundle is a bundle of main whose pack also carries a commit |
| 244 | // main does not reach, as a hostile publisher could write one. |
| 245 | func unreachableBundle(t *testing.T, s source) string { |
| 246 | t.Helper() |
| 247 | objects := git(t, s.repo, nil, "rev-list", "--objects", "main", "side") |
| 248 | pack := packObjects(t, s.repo, objects) |
| 249 | header := "# v2 git bundle\n" + s.tip + " refs/heads/main\n\n" |
| 250 | path := filepath.Join(t.TempDir(), "unreachable.bundle") |
| 251 | if err := os.WriteFile(path, append([]byte(header), pack...), 0o644); err != nil { |
| 252 | t.Fatal(err) |
| 253 | } |
| 254 | return path |
| 255 | } |
| 256 | |
| 257 | func packObjects(t *testing.T, repo, objects string) []byte { |
| 258 | t.Helper() |
| 259 | cmd := exec.Command("git", "-C", repo, "pack-objects", "--stdout", "--quiet") |
| 260 | cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null") |
| 261 | cmd.Stdin = strings.NewReader(objects + "\n") |
| 262 | out, err := cmd.Output() |
| 263 | if err != nil { |
| 264 | t.Fatal(err) |
| 265 | } |
| 266 | return out |
| 267 | } |
| 268 | |
| 269 | // Reachable admits only commits main's tip reaches. A bundle can carry |
| 270 | // other objects; they are in the repository and never reachable. |
| 271 | func TestReachable(t *testing.T) { |
| 272 | ctx := context.Background() |
| 273 | s := newSource(t, "sha1") |
| 274 | repo := filepath.Join(t.TempDir(), "app.git") |
| 275 | if err := export.AcceptBundle(ctx, unreachableBundle(t, s), repo, s.tip); err != nil { |
| 276 | t.Fatal(err) |
| 277 | } |
| 278 | if got := git(t, repo, nil, "cat-file", "-t", s.side); got != "commit" { |
| 279 | t.Fatalf("the unreachable commit is not in the repository: %q", got) |
| 280 | } |
| 281 | tree := git(t, repo, nil, "rev-parse", s.tip+"^{tree}") |
| 282 | for _, c := range []struct { |
| 283 | name, commit string |
| 284 | want bool |
| 285 | }{ |
| 286 | {"the tip", s.tip, true}, |
| 287 | {"its parent", s.first, true}, |
| 288 | {"a commit in the bundle that main does not reach", s.side, false}, |
| 289 | {"a tree", tree, false}, |
| 290 | {"an object the repository does not hold", strings.Repeat("1", 40), false}, |
| 291 | {"an abbreviated ID", s.tip[:12], false}, |
| 292 | {"a revision expression", s.tip + "~1", false}, |
| 293 | {"an option", "--all", false}, |
| 294 | } { |
| 295 | t.Run(c.name, func(t *testing.T) { |
| 296 | got, err := export.Reachable(ctx, repo, s.tip, c.commit) |
| 297 | if err != nil || got != c.want { |
| 298 | t.Errorf("Reachable gave %v, %v; want %v", got, err, c.want) |
| 299 | } |
| 300 | }) |
| 301 | } |
| 302 | if _, err := export.Reachable(ctx, repo, "main", s.tip); err == nil { |
| 303 | t.Error("Reachable took a tip that is no object ID") |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // A carried repository is fetched and checked as a bundle is, and holds |
| 308 | // main alone. |
| 309 | func TestCarryRepo(t *testing.T) { |
| 310 | ctx := context.Background() |
| 311 | s := newSource(t, "sha1") |
| 312 | held := filepath.Join(t.TempDir(), "held.git") |
| 313 | if err := export.AcceptBundle(ctx, s.bundle(t, "main"), held, s.tip); err != nil { |
| 314 | t.Fatal(err) |
| 315 | } |
| 316 | carried := filepath.Join(t.TempDir(), "carried.git") |
| 317 | if err := export.CarryRepo(ctx, held, carried, s.tip); err != nil { |
| 318 | t.Fatal(err) |
| 319 | } |
| 320 | if refs := git(t, carried, nil, "for-each-ref", "--format=%(refname) %(objectname)"); refs != "refs/heads/main "+s.tip { |
| 321 | t.Errorf("the carried repository holds %q", refs) |
| 322 | } |
| 323 | wrong := filepath.Join(t.TempDir(), "wrong.git") |
| 324 | if err := export.CarryRepo(ctx, held, wrong, s.first); err == nil { |
| 325 | t.Error("CarryRepo carried a repository at another tip") |
| 326 | } |
| 327 | if _, err := os.Lstat(wrong); !os.IsNotExist(err) { |
| 328 | t.Error("the refusal left a repository behind") |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // Git reads the repository it is given and no other: a directory that is |
| 333 | // not a repository fails, even when a repository holds it. |
| 334 | func TestGit(t *testing.T) { |
| 335 | ctx := context.Background() |
| 336 | s := newSource(t, "sha1") |
| 337 | repo := filepath.Join(t.TempDir(), "app.git") |
| 338 | if err := export.AcceptBundle(ctx, s.bundle(t, "main"), repo, s.tip); err != nil { |
| 339 | t.Fatal(err) |
| 340 | } |
| 341 | out, err := export.Git(ctx, repo, 1<<10, "rev-parse", export.MainRef) |
| 342 | if err != nil || strings.TrimSpace(string(out)) != s.tip { |
| 343 | t.Errorf("Git read main as %q, %v; want %s", out, err, s.tip) |
| 344 | } |
| 345 | if out, err := export.Git(ctx, filepath.Join(repo, "refs"), 1<<10, "rev-parse", export.MainRef); err == nil { |
| 346 | t.Errorf("Git read %q from a directory inside the repository", out) |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // Git reads no more output than its limit: git printing more is stopped |
| 351 | // and waited for, and the read fails. Output of exactly the limit is |
| 352 | // read whole. A reader that returns before the output ends stops git too, |
| 353 | // and is not failed for it. A git left writing to a pipe nobody reads |
| 354 | // would never exit, so each call returning shows git was stopped. |
| 355 | func TestGitOutputLimit(t *testing.T) { |
| 356 | ctx := context.Background() |
| 357 | repo := filepath.Join(t.TempDir(), "big.git") |
| 358 | git(t, "", nil, "init", "--quiet", "--bare", repo) |
| 359 | big := strings.Repeat("eight MiB of output\n", (8<<20)/20) |
| 360 | blob := git(t, repo, []byte(big), "hash-object", "-w", "--stdin") |
| 361 | |
| 362 | if out, err := export.Git(ctx, repo, 1<<20, "cat-file", "blob", blob); !errors.Is(err, export.ErrOutputLimit) { |
| 363 | t.Errorf("Git read %d bytes of an 8 MiB blob with a 1 MiB limit, and returned %v", len(out), err) |
| 364 | } |
| 365 | if out, err := export.Git(ctx, repo, int64(len(big)), "cat-file", "blob", blob); err != nil || string(out) != big { |
| 366 | t.Errorf("Git read %d bytes of a blob exactly its limit long: %v", len(out), err) |
| 367 | } |
| 368 | |
| 369 | var first []byte |
| 370 | err := export.GitStream(ctx, repo, 64<<20, func(r io.Reader) error { |
| 371 | first = make([]byte, 5) |
| 372 | _, err := io.ReadFull(r, first) |
| 373 | return err |
| 374 | }, "cat-file", "blob", blob) |
| 375 | if err != nil || string(first) != "eight" { |
| 376 | t.Errorf("a reader that stopped early read %q and got %v", first, err) |
| 377 | } |
| 378 | |
| 379 | // A git that fails is reported once its output is read to the end. |
| 380 | if err := export.GitStream(ctx, repo, 1<<10, func(r io.Reader) error { |
| 381 | _, err := io.ReadAll(r) |
| 382 | return err |
| 383 | }, "cat-file", "blob", strings.Repeat("0", 40)); err == nil { |
| 384 | t.Error("GitStream did not report git's failure") |
| 385 | } |
| 386 | } |