package main
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"craftdesign.group/quarry/internal/pickupclient"
)
type memStore map[string]pickupclient.Record
func (m memStore) Put(host string, r pickupclient.Record) (string, error) {
m[host] = r
return "memory", nil
}
func (m memStore) Delete(host string) error { delete(m, host); return nil }
func (m memStore) Get(host string) (pickupclient.Record, error) {
if r, ok := m[host]; ok {
return r, nil
}
return pickupclient.Record{}, pickupclient.ErrNoLogin
}
// RFC 0033 step 5, against the real sync lane (wired as serve wires it)
// and the real zit and fossil: quarry repo clone of a private repository,
// then bare pull and push with no password and nothing in the
// environment, and no copy of the token left in the checkout.
func TestRepoCloneThenBareSyncNeedsNoPassword(t *testing.T) {
r := newFirstPushRig(t, "vault")
if _, err := exec.LookPath("fossil"); err != nil {
if os.Getenv("CI") == "true" {
t.Fatal("FAIL: fossil not on PATH under CI")
}
t.Skip("fossil not on PATH")
}
r.commit(t, r.dirs["vault"])
r.front.SetBasicLogin(r.rg.doors.basicSyncLogin)
r.front.SetBasicSignIn(r.rg.doors.syncSignIn)
token := r.token
mux := http.NewServeMux()
// The namespaced address the client clones from (RFC 0025).
mux.HandleFunc("POST /r/jan/vault", func(w http.ResponseWriter, req *http.Request) {
req.SetPathValue("repo", "vault")
r.front.ServeHTTP(w, req)
})
mux.HandleFunc("GET /api/v1/me", func(w http.ResponseWriter, req *http.Request) {
if req.Header.Get("Authorization") != "Bearer "+token {
w.WriteHeader(401)
return
}
w.Write([]byte(`{"name":"jan"}`))
})
mux.HandleFunc("GET /api/v1/r/jan/vault/info", func(w http.ResponseWriter, req *http.Request) {
if req.Header.Get("Authorization") != "Bearer "+token {
w.WriteHeader(404)
return
}
w.Write([]byte(`{"name":"vault","checkins":1}`))
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
u, _ := url.Parse(srv.URL)
t.Setenv("ZIT_PASSWORD", "")
t.Setenv("HOME", r.home)
t.Setenv("FOSSIL_HOME", r.home)
clone := func(extra ...string) (string, string) {
work := t.TempDir()
var out, errw bytes.Buffer
st := memStore{u.Host: {Host: u.Host, Account: "jan", Token: token}}
e := &pickupclient.Env{Client: &http.Client{}, Stdout: &out, Stderr: &errw, Keys: st, ConfigDir: t.TempDir(),
Getenv: func(string) string { return "" }}
args := append([]string{"jan/vault", filepath.Join(work, "vault"), "--host", srv.URL, "--json"}, extra...)
if code := pickupclient.Clone(context.Background(), args, e); code != 0 {
t.Fatalf("FAIL: clone exit %d out %q err %q", code, out.String(), errw.String())
}
if strings.Contains(out.String()+errw.String(), token) {
t.Errorf("FAIL: the token was printed")
}
return work, filepath.Join(work, "vault")
}
noTokenOnDisk := func(root string) {
filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
if err == nil && info.Mode().IsRegular() {
if b, _ := os.ReadFile(p); bytes.Contains(b, []byte(token)) {
t.Errorf("FAIL: the token is on disk in %s", p)
}
}
return nil
})
}
bare := func(dir, tool string, args ...string) {
t.Helper()
cmd := exec.Command(tool, args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "FOSSIL_USER=jan")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("FAIL: bare %s %v: %v\n%s", tool, args, err, out)
}
}
work, dir := clone()
bare(dir, "zit", "pull")
if err := os.WriteFile(filepath.Join(dir, "new.txt"), []byte("hello\n"), 0o644); err != nil {
t.Fatal(err)
}
bare(dir, "zit", "add", "new.txt")
bare(dir, "zit", "commit", "-m", "from a clone")
bare(dir, "zit", "push")
noTokenOnDisk(work)
work, dir = clone("--fossil")
bare(dir, "fossil", "pull")
if err := os.WriteFile(filepath.Join(dir, "more.txt"), []byte("hello\n"), 0o644); err != nil {
t.Fatal(err)
}
bare(dir, "fossil", "add", "more.txt")
bare(dir, "fossil", "commit", "-m", "from a fossil clone", "--no-warnings")
bare(dir, "fossil", "push")
if !t.Failed() {
t.Log("ok: zit and fossil clones pull and push with no password")
}
}