Skip to content
quarry Artifact [da83117bca]
Login

Artifact [da83117bca]

Artifact da83117bca1ecf624d221d84c996bc467d0dae5edd0a4688f5b0add7b5865ffe:


package xfer

import (
	"net/http"
	"testing"
)

// basicProbe is a sync-protocol request carrying HTTP Basic for user.
func basicProbe(user string) *http.Request {
	req := probe(fossilUA)
	req.Header.Set("Content-Type", "application/x-fossil")
	req.SetBasicAuth(user, "whatever")
	return req
}

// basicFor resolves Basic for the named accounts to the given credential,
// as the forge's sync lane does for a live token of that account.
func basicFor(creds map[string]Credential) BasicLogin {
	return func(req *http.Request, _ string) (string, Credential, bool) {
		u, _, ok := req.BasicAuth()
		c, live := creds[u]
		if !ok || !live {
			return "", Credential{}, false
		}
		return u, c, true
	}
}

var fullTok = Credential{Token: "TOK1", CheckIn: true, Publish: true}

func newBasicFronter(creds map[string]Credential) (*Fronter, *countingLimit) {
	f := New()
	f.SetAuth(secrets{"jan": "right"})
	lim := &countingLimit{max: 2, misses: map[string]int{}}
	f.SetLoginLimit(lim)
	good := map[string]string{}
	for u := range creds {
		good[u] = "whatever"
	}
	calls := 0
	f.SetBasicSignIn(signInWith(lim, good, &calls))
	f.SetBasicLogin(basicFor(creds))
	return f, lim
}

// A request with no login card whose Basic credential resolves is logged
// in as that account through that token.
func TestBasicWithNoCardIsALoginThroughTheToken(t *testing.T) {
	f, _ := newBasicFronter(map[string]Credential{"jan": fullTok})
	st := f.verifiedLogin("alpha", basicProbe("jan"), []byte("pull aaaa bbbb\n"), pushRequest{})
	if st.user != "jan" || st.token != "TOK1" || st.withheld || st.from != 0 || st.basicCred == nil {
		t.Errorf("FAIL: no-card Basic did not log in through the token: %+v", st)
	}
	if st.basicCred != nil && (!st.basicCred.CheckIn || !st.basicCred.Publish || st.basicCred.Token != "TOK1") {
		t.Errorf("FAIL: the credential lost its letters: %+v", *st.basicCred)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The token's own limits survive: a narrowed token gains nothing.
func TestBasicLoginKeepsTheTokensLimits(t *testing.T) {
	for name, c := range map[string]Credential{
		"no check-in": {Token: "T2", CheckIn: false, Publish: true},
		"no publish":  {Token: "T3", CheckIn: true, Publish: false},
	} {
		f, _ := newBasicFronter(map[string]Credential{"jan": c})
		st := f.verifiedLogin("alpha", basicProbe("jan"), nil, pushRequest{})
		if st.user != "jan" || st.token != c.Token || st.withheld != !c.Publish ||
			st.basicCred == nil || st.basicCred.CheckIn != c.CheckIn || st.basicCred.Publish != c.Publish {
			t.Errorf("FAIL %s: %+v", name, st)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A card naming the Basic account that does not verify stands on the
// Basic token: a success for the limiter, no miss.
func TestBasicStandsInForAnUnverifiableCardOfTheSameAccount(t *testing.T) {
	f, lim := newBasicFronter(map[string]Credential{"jan": fullTok})
	lim.misses["jan"] = 1
	body := signedLogin("jan", "wrong")
	st := f.verifiedLogin("alpha", basicProbe("jan"), body, scanRequest(body))
	if st.user != "jan" || st.token != "TOK1" || st.basicCred == nil {
		t.Errorf("FAIL: not logged in on Basic: %+v", st)
	}
	if lim.misses["jan"] != 0 {
		t.Errorf("FAIL: limiter misses = %d, want a success to clear them", lim.misses["jan"])
	}
	// A name with no credential here, named by the card, also stands.
	f2, lim2 := newBasicFronter(map[string]Credential{"kim": fullTok})
	body = signedLogin("kim", "wrong")
	st = f2.verifiedLogin("alpha", basicProbe("kim"), body, scanRequest(body))
	if st.user != "kim" || lim2.misses["kim"] != 0 {
		t.Errorf("FAIL: credential-less name: %+v, misses %d", st, lim2.misses["kim"])
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// Basic for A with a card naming B is refused as today.
func TestBasicForAnotherAccountThanTheCardIsRefused(t *testing.T) {
	f, lim := newBasicFronter(map[string]Credential{"jan": fullTok, "kim": {Token: "K", CheckIn: true, Publish: true}})
	body := signedLogin("jan", "wrong")
	st := f.verifiedLogin("alpha", basicProbe("kim"), body, scanRequest(body))
	if st.user != "" || st.basicCred != nil || lim.misses["jan"] != 1 {
		t.Errorf("FAIL: A's Basic stood for B's card: %+v, misses %d", st, lim.misses["jan"])
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// Basic that does not resolve, plus a bad card of the same account, is
// one miss: Basic's. A request with Basic and no card is a sign-in too,
// and counts its miss.
func TestUnresolvedBasicWithABadCardCountsOneMiss(t *testing.T) {
	f, lim := newBasicFronter(map[string]Credential{})
	body := signedLogin("jan", "wrong")
	st := f.verifiedLogin("alpha", basicProbe("jan"), body, scanRequest(body))
	if st.user != "" || st.basicCred != nil || lim.misses["jan"] != 1 {
		t.Errorf("FAIL: %+v, misses %d", st, lim.misses["jan"])
	}
	st = f.verifiedLogin("alpha", basicProbe("jan"), nil, pushRequest{})
	if st.user != "" || lim.misses["jan"] != 2 {
		t.Errorf("FAIL: an unresolved no-card Basic was not counted: %+v, misses %d", st, lim.misses["jan"])
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A held account stays held, Basic or not.
func TestAHeldAccountIsStillHeldOnBasic(t *testing.T) {
	f, lim := newBasicFronter(map[string]Credential{"jan": fullTok})
	lim.misses["jan"] = lim.max
	body := signedLogin("jan", "wrong")
	st := f.verifiedLogin("alpha", basicProbe("jan"), body, scanRequest(body))
	if !st.held || st.user != "" || st.basicCred != nil {
		t.Errorf("FAIL: held account logged in: %+v", st)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A verified card is untouched by Basic: zit's path.
func TestAVerifiedCardIgnoresBasic(t *testing.T) {
	f, _ := newBasicFronter(map[string]Credential{"jan": {Token: "BASIC", CheckIn: true, Publish: true}})
	body := signedLogin("jan", "right")
	st := f.verifiedLogin("alpha", basicProbe("jan"), body, scanRequest(body))
	if st.user != "jan" || st.basicCred != nil || st.token != "" {
		t.Errorf("FAIL: %+v", st)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}