package webtest import ( "net/url" "sort" "strings" "testing" "time" "amadan.net/oficina/base/internal/equipo/equipotest" ) // Every change to a member's Ask link (the native set-up's save, the web's // Connect and its approval, Disconnect, a code that ends) reads the link as // it is at that moment, under the member's lock, changes it in that one // step, and only then revokes the device it displaced. Whichever of two // finishes first, Base ends with at most one live credential in Equipo, the // one it holds, and every credential it forgot is revoked. One test per // interleaving; the rest are elsewhere (see the commit that added this). // background runs call and returns where its answer will come. func background(call func() Resp) <-chan Resp { out := make(chan Resp, 1) go func() { out <- call() }() return out } // arrived waits for a held call to reach Equipo. func arrived(t *testing.T, entered <-chan struct{}, what string) { t.Helper() select { case <-entered: case <-time.After(5 * time.Second): t.Fatalf("%s never reached Equipo", what) } } // startWebConnect presses Connect on the web and returns the code shown. func (a *testApp) startWebConnect() string { a.t.Helper() r := a.post("/ask/connect", url.Values{"next": {"settings"}}) if r.Code != 303 { a.t.Fatalf("connect: %d %s", r.Code, r.Body) } m := codeRE.FindStringSubmatch(a.get(r.Location()).Body) if m == nil { a.t.Fatal("no code shown") } return m[1] } // approveAndCheck approves code in Equipo and presses Check again. func (a *testApp) approveAndCheck(f *equipotest.Fake, code string) Resp { a.t.Helper() if err := f.Approve(code); err != nil { a.t.Fatal(err) } a.pollable() return a.check("settings") } // wantLive fails unless exactly the devices in want hold a live credential. func wantLive(t *testing.T, f *equipotest.Fake, want ...string) { t.Helper() got := f.LiveCredentials() sort.Strings(got) sort.Strings(want) if strings.Join(got, ",") != strings.Join(want, ",") { t.Fatalf("live credentials %v, want %v", got, want) } } // Astra's (a): the web's Connect, paused at Equipo, saves after a native // renewal did. It replaces the renewal's link and revokes its device. func TestRaceWebConnectSavesAfterNativeRenewal(t *testing.T) { web, app, f := newNativeAskApp(t) f.LinkOficinaID("subject-owner", ownerAddress) if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { t.Fatalf("connect: %d %s", r.Code, r.Body) } f.RevokeAll() if s, _ := app.askState(); s.State != "reconnect" { t.Fatalf("state: %s", s.State) } entered, release := f.HoldNextBegin() started := background(func() Resp { return web.post("/ask/connect", url.Values{"next": {"settings"}}) }) arrived(t, entered, "the web's Connect") if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { t.Fatalf("renewal: %d %s", r.Code, r.Body) } renewed := f.LiveCredentials() release() if r := <-started; r.Code != 303 { t.Fatalf("web connect: %d %s", r.Code, r.Body) } if len(renewed) != 1 || !f.Revoked(renewed[0]) { t.Fatalf("the renewal's device %v is still live after the web replaced it", renewed) } m := codeRE.FindStringSubmatch(web.get("/ask/connect?next=settings").Body) if m == nil { t.Fatal("no code") } if r := web.approveAndCheck(f, m[1]); r.Code != 303 || r.Location() != "/settings" { t.Fatalf("check: %d %q", r.Code, r.Location()) } if live := f.LiveCredentials(); len(live) != 1 || live[0] == renewed[0] || web.equipoLinks() != 1 { t.Fatalf("live %v, links %d", live, web.equipoLinks()) } } // Astra's (b): Disconnect, paused revoking the old device, and a native // renewal released meanwhile. Disconnect removed the link there was when it // ran; the renewal, overtaken, saves nothing and revokes its own device. func TestRaceDisconnectWhileNativeRenewalSaves(t *testing.T) { _, app, f := newNativeAskApp(t) f.LinkOficinaID("subject-owner", ownerAddress) if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { t.Fatalf("connect: %d %s", r.Code, r.Body) } renewal, releaseRenewal := startHeldSetup(t, app, f) entered, releaseRevoke := f.HoldNextRevoke() disconnected := background(func() Resp { return app.post("/ask/disconnect", nil) }) arrived(t, entered, "Disconnect's revocation") releaseRenewal() wantBody(t, "renewal", <-renewal, 409, `{"state":"superseded"}`) releaseRevoke() if r := <-disconnected; r.Code != 303 { t.Fatalf("disconnect: %d", r.Code) } wantLive(t, f) if n := app.equipoLinks(); n != 0 { t.Fatalf("links: %d", n) } if hosts := f.Hosts(); len(hosts) != 1 { t.Errorf("hosts registered: %d", len(hosts)) } } // A native set-up saves while the web's approval waits at Equipo's // exchange: the approval finds its code replaced and gives its bearer up. func TestRaceNativeSavesWhileWebApprovalExchanges(t *testing.T) { web, app, f := newNativeAskApp(t) f.LinkOficinaID("subject-owner", ownerAddress) code := web.startWebConnect() if err := f.Approve(code); err != nil { t.Fatal(err) } web.pollable() entered, release := f.HoldNextExchange() checked := background(func() Resp { return web.check("settings") }) arrived(t, entered, "the web's exchange") if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { t.Fatalf("native: %d %s", r.Code, r.Body) } native := f.LiveCredentials() release() <-checked wantLive(t, f, native...) s, r := app.askState() if s.State != "ready" || strings.Contains(r.Body, `"renew_after":""`) { t.Fatalf("state: %s", r.Body) } // The old code ending later changes nothing. f.Expire() web.pollable() web.check("settings") wantLive(t, f, native...) } // Disconnect while the web's Connect waits at Equipo: the Connect, the // later of the two, saves its code; the connection Disconnect removed is // revoked, and nothing is live. func TestRaceDisconnectWhileWebConnectBegins(t *testing.T) { web, app, f := newNativeAskApp(t) f.LinkOficinaID("subject-owner", ownerAddress) if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { t.Fatalf("connect: %d %s", r.Code, r.Body) } entered, release := f.HoldNextBegin() started := background(func() Resp { return web.post("/ask/connect", url.Values{"next": {"settings"}}) }) arrived(t, entered, "the web's Connect") if r := app.post("/ask/disconnect", nil); r.Code != 303 { t.Fatalf("disconnect: %d", r.Code) } release() if r := <-started; r.Code != 303 { t.Fatalf("web connect: %d", r.Code) } wantLive(t, f) if s, _ := app.askState(); s.State != "pending" { t.Errorf("state: %s", s.State) } } // The web's code ends (expired) while a native set-up waits at Equipo: // the set-up, overtaken, saves nothing and revokes its device. func TestRaceCodeEndsWhileNativeSetupWaits(t *testing.T) { web, app, f := newNativeAskApp(t) f.LinkOficinaID("subject-owner", ownerAddress) web.startWebConnect() setup, release := startHeldSetup(t, app, f) f.Expire() web.pollable() web.check("settings") if n := web.equipoLinks(); n != 0 { t.Fatalf("the expired code's link stays: %d", n) } release() wantBody(t, "set-up", <-setup, 409, `{"state":"superseded"}`) wantLive(t, f) } // A second Connect while the first's approval waits at Equipo: the // first's bearer is given up, and only the newer code stands. func TestRaceReconnectWhileWebApprovalExchanges(t *testing.T) { web, _, f := newNativeAskApp(t) code := web.startWebConnect() if err := f.Approve(code); err != nil { t.Fatal(err) } web.pollable() entered, release := f.HoldNextExchange() checked := background(func() Resp { return web.check("settings") }) arrived(t, entered, "the web's exchange") web.startWebConnect() release() <-checked wantLive(t, f) if r := web.nativeGet("/ask/state"); !strings.Contains(r.Body, `"state":"pending"`) { t.Errorf("state: %s", r.Body) } }