Skip to content
quarry Artifact [13c106f057]
Login

Artifact [13c106f057]

Artifact 13c106f05747bc2f1bd9162d1bc66d23546f693b7f0748ea9a0723e3e6a9f8af:


package xfer

import (
	"bytes"
	"net/http"
	"net/http/httptest"
	"testing"
)

// A repository under a takedown answers a sync with the protocol's own
// error, carrying the gate's sentence, and never reaches the child; a
// repository the read gate hides is not asked about at all.
func TestATakedownRefusesASyncInWords(t *testing.T) {
	f := gated()
	defer f.Close()
	srv, _ := fakeChild(t, f, "alpha")
	reached := false
	srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		reached = true
		w.Header().Set("Content-Type", "application/x-fossil")
		w.Write([]byte("pragma ok\n"))
	})
	f.SetHoldGate(func(_ *http.Request, repo, _ string) string {
		return "this repository is unavailable for legal reasons"
	})
	// A caller who offered nothing is answered as Fossil answers a pull
	// it may not read, and told how to sign in; it is not told of the
	// takedown before it has said who it is.
	w, _, _ := postRaw(t, f, encodeContainer([]byte("pull SRV PRJ\n")))
	if body := decodeContainer(w.Body.Bytes()); w.Code != http.StatusOK || !bytes.HasPrefix(body, []byte(`error not\sauthorized\sto\sread:\s`)) {
		t.Errorf("FAIL: a caller who offered nothing answered %d %q, want Fossil's not-authorized card", w.Code, body)
	}
	w = postAs(t, f, encodeContainer([]byte("pull SRV PRJ\n")))
	if reached {
		t.Error("FAIL: the child served a repository under a takedown")
	}
	if body := decodeContainer(w.Body.Bytes()); !bytes.Contains(body, []byte(`error this\srepository\sis\sunavailable\sfor\slegal\sreasons`)) {
		t.Errorf("FAIL: the reply carries no error card: %q", body)
	}

	asked := false
	f.SetReadGate(func(*http.Request, string, string) bool { return false })
	f.SetHoldGate(func(*http.Request, string, string) string { asked = true; return "held" })
	postRaw(t, f, encodeContainer([]byte("pull SRV PRJ\n")))
	if asked {
		t.Error("FAIL: the hold gate was asked about a repository the read gate hides")
	}
}

// postAs is postRaw with a Basic credential, the second round a stock
// client sends after a challenge.
func postAs(t *testing.T, f *Fronter, raw []byte) *httptest.ResponseRecorder {
	t.Helper()
	req := httptest.NewRequest(http.MethodPost, "/r/alpha", bytes.NewReader(raw))
	req.SetPathValue("repo", "alpha")
	req.Header.Set("Content-Type", "application/x-fossil")
	req.SetBasicAuth("someone", "pw")
	w := httptest.NewRecorder()
	f.ServeHTTP(w, req)
	return w
}