Artifact dfbcff13fee4bed7842c0a0aec057ee91b44d79665aaf3f51dd1e69e629ba2c4:
- File
internal/xfer/xfer_test.go
— part of check-in
[bcef58408e]
at
2026-08-15 21:50:38
on branch trunk
— xfer: push gating with RFC §6 login verification; forge OLTP v1
quarry user add derives the shared secret via a clone handshake (project code, no .zit bytes) and stores it in oltp.db. Gated repos verify body or x-f-l-c cookie logins per request; pulls stay anonymous. Fossil pushes authenticate end-to-end; zit's client stops sending credentials after round 1 (zit#74) so its pushes fail closed until that lands. (user: dmestas, size: 7041) [annotate] [blame] [check-ins using]
package xfer import ( "net/http" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "testing" ) func zrun(t *testing.T, dir string, args ...string) string { t.Helper() cmd := exec.Command("zit", args...) cmd.Dir = dir out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("zit %v: %v\n%s", args, err, out) } return string(out) } // TestCloneAndPushThroughFronter is the 0001 §7 acceptance: `zit clone // <quarry>/r/<repo>` works, and a push through the same URL lands // artifacts in the fronted store. func TestCloneAndPushThroughFronter(t *testing.T) { if _, err := exec.LookPath("zit"); err != nil { t.Skip("zit not on PATH") } origin := t.TempDir() zrun(t, origin, "init") if err := os.WriteFile(filepath.Join(origin, "README.md"), []byte("# fronted\n"), 0o644); err != nil { t.Fatal(err) } zrun(t, origin, "add", "README.md") zrun(t, origin, "commit", "-m", "first") f := New() defer f.Close() f.Register("alpha", origin) mux := http.NewServeMux() mux.Handle("POST /r/{repo}", f) mux.Handle("POST /r/{repo}/xfer", f) srv := httptest.NewServer(mux) defer srv.Close() // Clone through quarry: the repo URL is used verbatim by zit. work := t.TempDir() clone := filepath.Join(work, "clone") zrun(t, work, "clone", srv.URL+"/r/alpha", clone) if tl := zrun(t, clone, "timeline", "-n", "3"); !strings.Contains(tl, "first") { t.Fatalf("clone missing history:\n%s", tl) } // Push back through the same URL (saved as the clone's remote). if err := os.WriteFile(filepath.Join(clone, "NOTES.md"), []byte("pushed\n"), 0o644); err != nil { t.Fatal(err) } zrun(t, clone, "add", "NOTES.md") zrun(t, clone, "commit", "-m", "pushed through quarry") zrun(t, clone, "push") if tl := zrun(t, origin, "timeline", "-n", "3"); !strings.Contains(tl, "pushed through quarry") { t.Fatalf("push did not land in origin:\n%s", tl) } // The /xfer alias answers too. zrun(t, work, "clone", srv.URL+"/r/alpha/xfer", filepath.Join(work, "clone2")) // Unknown repos 404 rather than 502. resp, err := http.Post(srv.URL+"/r/nope", "application/x-fossil", strings.NewReader("")) if err != nil { t.Fatal(err) } resp.Body.Close() if resp.StatusCode != http.StatusNotFound { t.Fatalf("unknown repo: status %d", resp.StatusCode) } } // TestBackendRestartAfterDeath: a dead child is restarted on the next // request instead of 502ing forever. func TestBackendRestartAfterDeath(t *testing.T) { if _, err := exec.LookPath("zit"); err != nil { t.Skip("zit not on PATH") } origin := t.TempDir() zrun(t, origin, "init") f := New() defer f.Close() f.Register("alpha", origin) mux := http.NewServeMux() mux.Handle("POST /r/{repo}", f) srv := httptest.NewServer(mux) defer srv.Close() work := t.TempDir() zrun(t, work, "clone", srv.URL+"/r/alpha", filepath.Join(work, "c1")) // Kill the child behind its back. f.mu.Lock() b := f.repos["alpha"] b.cmd.Process.Kill() f.mu.Unlock() zrun(t, work, "clone", srv.URL+"/r/alpha", filepath.Join(work, "c2")) } type mapAuth struct{ users map[string]string } // user -> secret, one repo func (m *mapAuth) Gated(string) bool { return len(m.users) > 0 } func (m *mapAuth) Secret(_, user string) (string, bool) { s, ok := m.users[user] return s, ok } // TestPushGating is 0001 §7 v1 auth: with a sync user configured, // anonymous pushes bounce with an error card and pulls stay anonymous. // The authenticated-push leg drives the fossil client: it sends a // credential on every round per sync RFC §6.5, where the zit client // stops after round 1 (zit#74) — until that lands, zit pushes to a // gated repo fail closed. func TestPushGating(t *testing.T) { if _, err := exec.LookPath("zit"); err != nil { t.Skip("zit not on PATH") } origin := t.TempDir() zrun(t, origin, "init") if err := os.WriteFile(filepath.Join(origin, "README.md"), []byte("# gated\n"), 0o644); err != nil { t.Fatal(err) } zrun(t, origin, "add", "README.md") zrun(t, origin, "commit", "-m", "first") code, err := ProjectCode(origin) if err != nil { t.Fatal(err) } if len(code) < 40 { t.Fatalf("suspicious project code %q", code) } f := New() defer f.Close() f.Register("alpha", origin) f.SetAuth(&mapAuth{users: map[string]string{ "dan": SharedSecret(code, "dan", "hunter2"), }}) mux := http.NewServeMux() mux.Handle("POST /r/{repo}", f) srv := httptest.NewServer(mux) defer srv.Close() // Pull stays anonymous (--once so the commit below cannot autosync). work := t.TempDir() clone := filepath.Join(work, "clone") zrun(t, work, "clone", srv.URL+"/r/alpha", clone, "--once") if err := os.WriteFile(filepath.Join(clone, "NOTES.md"), []byte("gated push\n"), 0o644); err != nil { t.Fatal(err) } zrun(t, clone, "add", "NOTES.md") zrun(t, clone, "commit", "-m", "wants in") // Anonymous push: refused, and nothing lands. cmd := exec.Command("zit", "push", srv.URL+"/r/alpha") cmd.Dir = clone if out, err := cmd.CombinedOutput(); err == nil { t.Fatalf("anonymous push succeeded against a gated repo:\n%s", out) } if tl := zrun(t, origin, "timeline", "-n", "2"); strings.Contains(tl, "wants in") { t.Fatalf("refused push still landed:\n%s", tl) } if _, err := exec.LookPath("fossil"); err != nil { t.Skip("fossil not on PATH for the authenticated-push leg") } frun := func(dir string, args ...string) (string, error) { cmd := exec.Command("fossil", args...) cmd.Dir = dir out, err := cmd.CombinedOutput() return string(out), err } // Wrong password: refused. fwork := t.TempDir() if out, err := frun(fwork, "clone", srv.URL+"/r/alpha", "bad.fossil"); err != nil { t.Fatalf("fossil clone (pull is anonymous) should succeed: %v\n%s", err, out) } co := filepath.Join(fwork, "co") if err := os.MkdirAll(co, 0o755); err != nil { t.Fatal(err) } if out, err := frun(co, "open", filepath.Join(fwork, "bad.fossil")); err != nil { t.Fatalf("fossil open: %v\n%s", err, out) } if err := os.WriteFile(filepath.Join(co, "F.md"), []byte("fossil push\n"), 0o644); err != nil { t.Fatal(err) } frun(co, "add", "F.md") if out, err := frun(co, "commit", "-m", "fossil wants in", "--no-warnings"); err != nil && !strings.Contains(out, "not authorized") { t.Fatalf("fossil commit: %v\n%s", err, out) } if out, _ := frun(co, "push", strings.Replace(srv.URL, "http://", "http://dan:wrong@", 1)+"/r/alpha", "--once"); !strings.Contains(strings.ToLower(out), "not authorized") { t.Fatalf("wrong-password fossil push not refused:\n%s", out) } if tl := zrun(t, origin, "timeline", "-n", "2"); strings.Contains(tl, "fossil wants in") { t.Fatalf("refused fossil push landed:\n%s", tl) } // Right credentials: lands. if out, err := frun(co, "push", strings.Replace(srv.URL, "http://", "http://dan:hunter2@", 1)+"/r/alpha", "--once"); err != nil { t.Fatalf("authenticated fossil push failed: %v\n%s", err, out) } if tl := zrun(t, origin, "timeline", "-n", "2"); !strings.Contains(tl, "fossil wants in") { t.Fatalf("authenticated fossil push missing from origin:\n%s", tl) } }