package slidestest import ( "context" "fmt" "net/http" "net/http/httptest" "net/url" "strings" "sync" "testing" "time" ) // blockingMailer holds every Send until release is closed — an SMTP // relay that takes as long as the test likes. type blockingMailer struct { release chan struct{} mu sync.Mutex to []string } func (m *blockingMailer) Send(_ context.Context, to, _, _ string) error { <-m.release m.mu.Lock() m.to = append(m.to, to) m.mu.Unlock() return nil } func (m *blockingMailer) sent() []string { m.mu.Lock() defer m.mu.Unlock() return append([]string(nil), m.to...) } // TestSigninNeverWaitsForTheMailer is the timing half of the no-oracle // guarantee (Astra on #20, P1). When the send ran inside the request, a // permitted address waited for SMTP before its ?sent=1 and a denied one // did not — 252 ms against 0.35 ms, the allowlist readable off a // stopwatch, and force=1 made Keymail addresses readable the same way. // // Here the mailer never answers until the test says so. Every POST — // permitted, denied, and both again with force=1 — must come back while // it is still blocked, and the permitted links must then go out. func TestSigninNeverWaitsForTheMailer(t *testing.T) { bm := &blockingMailer{release: make(chan struct{})} cfg, _ := newConfig(t) cfg.Mailer = bm app := newAppWith(t, cfg, nil) var once sync.Once free := func() { once.Do(func() { close(bm.release) }) } t.Cleanup(free) // before the app's own Close, which drains the queue posts := []url.Values{ {"address": {allowedAddress}}, {"address": {deniedAddress}}, {"address": {allowedAddress}, "force": {"1"}}, {"address": {deniedAddress}, "force": {"1"}}, } for _, form := range posts { done := make(chan *httptest.ResponseRecorder, 1) go func() { done <- post(t, app, "/signin", form) }() select { case rec := <-done: if rec.Code != 303 || rec.Header().Get("Location") != "/signin?sent=1" { t.Errorf("POST %v: %d %q, want 303 /signin?sent=1", form, rec.Code, rec.Header().Get("Location")) } case <-time.After(2 * time.Second): t.Errorf("POST %v waited on the mailer: the response time says the address is permitted", form) free() <-done } } free() deadline := time.Now().Add(5 * time.Second) for len(bm.sent()) < 2 && time.Now().Before(deadline) { time.Sleep(10 * time.Millisecond) } time.Sleep(50 * time.Millisecond) // and nothing more arrives got := bm.sent() if len(got) != 2 || got[0] != allowedAddress || got[1] != allowedAddress { t.Fatalf("mail sent = %v, want two links, both to %s", got, allowedAddress) } } // TestAFullMailQueueAnswersLikeAnEmptyOne: the queue holds 64 links. // With the mailer stuck on the first (permitted) link, enough POSTs to // overflow it — each from its own address and IP, so no budget stands // in — must every one be answered exactly as the first was, and at // once. Every address's link is queued, permitted or not, so a refused // address fills the queue as surely as a permitted one (Astra on #20, // round 2). func TestAFullMailQueueAnswersLikeAnEmptyOne(t *testing.T) { bm := &blockingMailer{release: make(chan struct{})} cfg, _ := newConfig(t) cfg.Mailer = bm app := newAppWith(t, cfg, nil) var once sync.Once free := func() { once.Do(func() { close(bm.release) }) } t.Cleanup(free) postFrom := func(i int, address string) *httptest.ResponseRecorder { req := httptest.NewRequest(http.MethodPost, "/signin", strings.NewReader(url.Values{"address": {address}}.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Origin", testOrigin) req.Header.Set("Sec-Fetch-Site", "same-origin") req.RemoteAddr = fmt.Sprintf("198.51.100.%d:1234", i%250+1) rec := httptest.NewRecorder() app.ServeHTTP(rec, req) return rec } first := postFrom(0, allowedAddress) for i := 1; i <= 80; i++ { start := time.Now() rec := postFrom(i, fmt.Sprintf("stranger%d@else.test", i)) if took := time.Since(start); took > time.Second { t.Fatalf("POST %d took %v with the queue full", i, took) } if rec.Code != first.Code || rec.Header().Get("Location") != first.Header().Get("Location") || rec.Body.String() != first.Body.String() { t.Fatalf("POST %d answered %d %q, want exactly the first answer %d %q", i, rec.Code, rec.Header().Get("Location"), first.Code, first.Header().Get("Location")) } } }