Skip to content
quarry Artifact [a40a275395]
Login

Artifact [a40a275395]

Artifact a40a2753956d80031f557cd31099aa439bfc9387d9244f01a2f3f9a3ba52864e:


package xfer

import (
	"bytes"
	"crypto/sha1"
	"encoding/hex"
	"net/http"
	"net/http/httptest"
	"os/exec"
	"strings"
	"testing"
	"time"
)

// ungated is onboardAuth on a repository that gates no pushes.
type ungated struct{ onboardAuth }

func (ungated) Gated(string) bool { return false }

// syncSigned sends a sync whose login card for user is signed with
// secret over covered ("" user sends no card) and returns the decoded
// reply.
func syncSigned(t *testing.T, f *Fronter, user, secret, covered string) string {
	t.Helper()
	body := covered
	if user != "" {
		nonce := sha1.Sum([]byte(covered))
		n := hex.EncodeToString(nonce[:])
		sig := sha1.Sum([]byte(n + secret))
		body = "login " + user + " " + n + " " + hex.EncodeToString(sig[:]) + "\n" + covered
	}
	req := httptest.NewRequest(http.MethodPost, "/r/alpha", bytes.NewReader(encodeContainer([]byte(body))))
	req.SetPathValue("repo", "alpha")
	req.Header.Set("Content-Type", "application/x-fossil")
	w := httptest.NewRecorder()
	f.ServeHTTP(w, req)
	return string(decodeContainer(w.Body.Bytes()))
}

// refusedByForge is a reply the forge itself wrote to turn the caller
// away: a login failure or a missing capability. An error from the store
// behind it (the test's made-up project code) means the forge let it in.
func refusedByForge(reply string) bool {
	return strings.Contains(reply, "login\\sfailed") || strings.Contains(reply, "not\\sauthorized")
}

func loginFailedFronter(t *testing.T, a Auth) *Fronter {
	t.Helper()
	if _, err := exec.LookPath("zit"); err != nil {
		t.Skip("zit not on PATH")
	}
	origin := t.TempDir()
	zrun(t, origin, "init")
	f := New()
	t.Cleanup(f.Close)
	f.Register("alpha", origin)
	f.SetAuth(a)
	return f
}

// A login card that is offered and does not verify is answered with
// fossil's own "login failed" error card, the same for a writer, a
// reader and a name nobody holds, on a pull and on a push, gated or not;
// a card that verifies, and no card at all, keep their answers.
func TestAnUnverifiedCardIsToldLoginFailed(t *testing.T) {
	pull := "pull PROJ PROJ\n"
	syncNoWrite := "push PROJ PROJ\npull PROJ PROJ\n"
	push := "push PROJ PROJ\n" + pushedFile([]byte("a pushed file\n"))
	for name, a := range map[string]Auth{
		"gated":   onboardAuth{secret: "right", patSecret: "pat-token"},
		"ungated": ungated{onboardAuth{secret: "right", patSecret: "pat-token"}},
	} {
		f := loginFailedFronter(t, a)
		for kind, covered := range map[string]string{"pull": pull, "sync": syncNoWrite, "push": push} {
			var first string
			for _, who := range []string{"jan", "lee", "zoot"} {
				got := strings.ReplaceAll(syncSigned(t, f, who, "not-their-secret", covered), who, "NAME")
				if !strings.HasPrefix(got, "error login\\sfailed") {
					t.Errorf("FAIL: %s %s as %s: not a login-failed error card: %q", name, kind, who, got)
				}
				if first == "" {
					first = got
				} else if got != first {
					t.Errorf("FAIL: %s %s: replies differ by name:\n  %q\n  %q", name, kind, first, got)
				}
			}
		}
		// A good sync token still pushes (an error card here would be a refusal).
		if got := syncSigned(t, f, "jan", "right", push); refusedByForge(got) {
			t.Errorf("FAIL: %s: a good token's push was refused: %q", name, got)
		}
		// A verified letterless token pulls, and is not told login failed.
		if got := syncSigned(t, f, "pat", "pat-token", syncNoWrite); refusedByForge(got) {
			t.Errorf("FAIL: %s: a verified letterless pull was refused: %q", name, got)
		}
		// No card: anonymous, as before.
		if got := syncSigned(t, f, "", "", syncNoWrite); refusedByForge(got) {
			t.Errorf("FAIL: %s: an anonymous pull was told login failed: %q", name, got)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The refusal counts the miss once, and a held account is told the hold
// rather than login failed.
func TestLoginFailedCountsTheMissOnce(t *testing.T) {
	f := loginFailedFronter(t, ungated{onboardAuth{secret: "right"}})
	lim := &countingLimit{max: 2, misses: map[string]int{}}
	f.SetLoginLimit(lim)
	pull := "pull PROJ PROJ\n"
	syncSigned(t, f, "jan", "wrong", pull)
	if lim.misses["jan"] != 1 {
		t.Errorf("FAIL: a bad card counted %d misses, want 1", lim.misses["jan"])
	}
	syncSigned(t, f, "jan", "wrong", pull)
	got := syncSigned(t, f, "jan", "wrong", pull)
	if lim.misses["jan"] != 2 || !strings.HasPrefix(got, "error ") || strings.HasPrefix(got, "error login\\sfailed") {
		t.Errorf("FAIL: a held account: misses %d, reply %q", lim.misses["jan"], got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

var _ = time.Second