| 1 | package webtest |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/base/internal/equipo/equipotest" |
| 11 | ) |
| 12 | |
| 13 | // Every change to a member's Ask link (the native set-up's save, the web's |
| 14 | // Connect and its approval, Disconnect, a code that ends) reads the link as |
| 15 | // it is at that moment, under the member's lock, changes it in that one |
| 16 | // step, and only then revokes the device it displaced. Whichever of two |
| 17 | // finishes first, Base ends with at most one live credential in Equipo, the |
| 18 | // one it holds, and every credential it forgot is revoked. One test per |
| 19 | // interleaving; the rest are elsewhere (see the commit that added this). |
| 20 | |
| 21 | // background runs call and returns where its answer will come. |
| 22 | func background(call func() Resp) <-chan Resp { |
| 23 | out := make(chan Resp, 1) |
| 24 | go func() { out <- call() }() |
| 25 | return out |
| 26 | } |
| 27 | |
| 28 | // arrived waits for a held call to reach Equipo. |
| 29 | func arrived(t *testing.T, entered <-chan struct{}, what string) { |
| 30 | t.Helper() |
| 31 | select { |
| 32 | case <-entered: |
| 33 | case <-time.After(5 * time.Second): |
| 34 | t.Fatalf("%s never reached Equipo", what) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // startWebConnect presses Connect on the web and returns the code shown. |
| 39 | func (a *testApp) startWebConnect() string { |
| 40 | a.t.Helper() |
| 41 | r := a.post("/ask/connect", url.Values{"next": {"settings"}}) |
| 42 | if r.Code != 303 { |
| 43 | a.t.Fatalf("connect: %d %s", r.Code, r.Body) |
| 44 | } |
| 45 | m := codeRE.FindStringSubmatch(a.get(r.Location()).Body) |
| 46 | if m == nil { |
| 47 | a.t.Fatal("no code shown") |
| 48 | } |
| 49 | return m[1] |
| 50 | } |
| 51 | |
| 52 | // approveAndCheck approves code in Equipo and presses Check again. |
| 53 | func (a *testApp) approveAndCheck(f *equipotest.Fake, code string) Resp { |
| 54 | a.t.Helper() |
| 55 | if err := f.Approve(code); err != nil { |
| 56 | a.t.Fatal(err) |
| 57 | } |
| 58 | a.pollable() |
| 59 | return a.check("settings") |
| 60 | } |
| 61 | |
| 62 | // wantLive fails unless exactly the devices in want hold a live credential. |
| 63 | func wantLive(t *testing.T, f *equipotest.Fake, want ...string) { |
| 64 | t.Helper() |
| 65 | got := f.LiveCredentials() |
| 66 | sort.Strings(got) |
| 67 | sort.Strings(want) |
| 68 | if strings.Join(got, ",") != strings.Join(want, ",") { |
| 69 | t.Fatalf("live credentials %v, want %v", got, want) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Astra's (a): the web's Connect, paused at Equipo, saves after a native |
| 74 | // renewal did. It replaces the renewal's link and revokes its device. |
| 75 | func TestRaceWebConnectSavesAfterNativeRenewal(t *testing.T) { |
| 76 | web, app, f := newNativeAskApp(t) |
| 77 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 78 | if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { |
| 79 | t.Fatalf("connect: %d %s", r.Code, r.Body) |
| 80 | } |
| 81 | f.RevokeAll() |
| 82 | if s, _ := app.askState(); s.State != "reconnect" { |
| 83 | t.Fatalf("state: %s", s.State) |
| 84 | } |
| 85 | entered, release := f.HoldNextBegin() |
| 86 | started := background(func() Resp { return web.post("/ask/connect", url.Values{"next": {"settings"}}) }) |
| 87 | arrived(t, entered, "the web's Connect") |
| 88 | if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { |
| 89 | t.Fatalf("renewal: %d %s", r.Code, r.Body) |
| 90 | } |
| 91 | renewed := f.LiveCredentials() |
| 92 | release() |
| 93 | if r := <-started; r.Code != 303 { |
| 94 | t.Fatalf("web connect: %d %s", r.Code, r.Body) |
| 95 | } |
| 96 | if len(renewed) != 1 || !f.Revoked(renewed[0]) { |
| 97 | t.Fatalf("the renewal's device %v is still live after the web replaced it", renewed) |
| 98 | } |
| 99 | m := codeRE.FindStringSubmatch(web.get("/ask/connect?next=settings").Body) |
| 100 | if m == nil { |
| 101 | t.Fatal("no code") |
| 102 | } |
| 103 | if r := web.approveAndCheck(f, m[1]); r.Code != 303 || r.Location() != "/settings" { |
| 104 | t.Fatalf("check: %d %q", r.Code, r.Location()) |
| 105 | } |
| 106 | if live := f.LiveCredentials(); len(live) != 1 || live[0] == renewed[0] || web.equipoLinks() != 1 { |
| 107 | t.Fatalf("live %v, links %d", live, web.equipoLinks()) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Astra's (b): Disconnect, paused revoking the old device, and a native |
| 112 | // renewal released meanwhile. Disconnect removed the link there was when it |
| 113 | // ran; the renewal, overtaken, saves nothing and revokes its own device. |
| 114 | func TestRaceDisconnectWhileNativeRenewalSaves(t *testing.T) { |
| 115 | _, app, f := newNativeAskApp(t) |
| 116 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 117 | if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { |
| 118 | t.Fatalf("connect: %d %s", r.Code, r.Body) |
| 119 | } |
| 120 | renewal, releaseRenewal := startHeldSetup(t, app, f) |
| 121 | entered, releaseRevoke := f.HoldNextRevoke() |
| 122 | disconnected := background(func() Resp { return app.post("/ask/disconnect", nil) }) |
| 123 | arrived(t, entered, "Disconnect's revocation") |
| 124 | releaseRenewal() |
| 125 | wantBody(t, "renewal", <-renewal, 409, `{"state":"superseded"}`) |
| 126 | releaseRevoke() |
| 127 | if r := <-disconnected; r.Code != 303 { |
| 128 | t.Fatalf("disconnect: %d", r.Code) |
| 129 | } |
| 130 | wantLive(t, f) |
| 131 | if n := app.equipoLinks(); n != 0 { |
| 132 | t.Fatalf("links: %d", n) |
| 133 | } |
| 134 | if hosts := f.Hosts(); len(hosts) != 1 { |
| 135 | t.Errorf("hosts registered: %d", len(hosts)) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // A native set-up saves while the web's approval waits at Equipo's |
| 140 | // exchange: the approval finds its code replaced and gives its bearer up. |
| 141 | func TestRaceNativeSavesWhileWebApprovalExchanges(t *testing.T) { |
| 142 | web, app, f := newNativeAskApp(t) |
| 143 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 144 | code := web.startWebConnect() |
| 145 | if err := f.Approve(code); err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | web.pollable() |
| 149 | entered, release := f.HoldNextExchange() |
| 150 | checked := background(func() Resp { return web.check("settings") }) |
| 151 | arrived(t, entered, "the web's exchange") |
| 152 | if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { |
| 153 | t.Fatalf("native: %d %s", r.Code, r.Body) |
| 154 | } |
| 155 | native := f.LiveCredentials() |
| 156 | release() |
| 157 | <-checked |
| 158 | wantLive(t, f, native...) |
| 159 | s, r := app.askState() |
| 160 | if s.State != "ready" || strings.Contains(r.Body, `"renew_after":""`) { |
| 161 | t.Fatalf("state: %s", r.Body) |
| 162 | } |
| 163 | // The old code ending later changes nothing. |
| 164 | f.Expire() |
| 165 | web.pollable() |
| 166 | web.check("settings") |
| 167 | wantLive(t, f, native...) |
| 168 | } |
| 169 | |
| 170 | // Disconnect while the web's Connect waits at Equipo: the Connect, the |
| 171 | // later of the two, saves its code; the connection Disconnect removed is |
| 172 | // revoked, and nothing is live. |
| 173 | func TestRaceDisconnectWhileWebConnectBegins(t *testing.T) { |
| 174 | web, app, f := newNativeAskApp(t) |
| 175 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 176 | if r := app.connectWithOficina(f, "subject-owner"); r.Code != 200 { |
| 177 | t.Fatalf("connect: %d %s", r.Code, r.Body) |
| 178 | } |
| 179 | entered, release := f.HoldNextBegin() |
| 180 | started := background(func() Resp { return web.post("/ask/connect", url.Values{"next": {"settings"}}) }) |
| 181 | arrived(t, entered, "the web's Connect") |
| 182 | if r := app.post("/ask/disconnect", nil); r.Code != 303 { |
| 183 | t.Fatalf("disconnect: %d", r.Code) |
| 184 | } |
| 185 | release() |
| 186 | if r := <-started; r.Code != 303 { |
| 187 | t.Fatalf("web connect: %d", r.Code) |
| 188 | } |
| 189 | wantLive(t, f) |
| 190 | if s, _ := app.askState(); s.State != "pending" { |
| 191 | t.Errorf("state: %s", s.State) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // The web's code ends (expired) while a native set-up waits at Equipo: |
| 196 | // the set-up, overtaken, saves nothing and revokes its device. |
| 197 | func TestRaceCodeEndsWhileNativeSetupWaits(t *testing.T) { |
| 198 | web, app, f := newNativeAskApp(t) |
| 199 | f.LinkOficinaID("subject-owner", ownerAddress) |
| 200 | web.startWebConnect() |
| 201 | setup, release := startHeldSetup(t, app, f) |
| 202 | f.Expire() |
| 203 | web.pollable() |
| 204 | web.check("settings") |
| 205 | if n := web.equipoLinks(); n != 0 { |
| 206 | t.Fatalf("the expired code's link stays: %d", n) |
| 207 | } |
| 208 | release() |
| 209 | wantBody(t, "set-up", <-setup, 409, `{"state":"superseded"}`) |
| 210 | wantLive(t, f) |
| 211 | } |
| 212 | |
| 213 | // A second Connect while the first's approval waits at Equipo: the |
| 214 | // first's bearer is given up, and only the newer code stands. |
| 215 | func TestRaceReconnectWhileWebApprovalExchanges(t *testing.T) { |
| 216 | web, _, f := newNativeAskApp(t) |
| 217 | code := web.startWebConnect() |
| 218 | if err := f.Approve(code); err != nil { |
| 219 | t.Fatal(err) |
| 220 | } |
| 221 | web.pollable() |
| 222 | entered, release := f.HoldNextExchange() |
| 223 | checked := background(func() Resp { return web.check("settings") }) |
| 224 | arrived(t, entered, "the web's exchange") |
| 225 | web.startWebConnect() |
| 226 | release() |
| 227 | <-checked |
| 228 | wantLive(t, f) |
| 229 | if r := web.nativeGet("/ask/state"); !strings.Contains(r.Body, `"state":"pending"`) { |
| 230 | t.Errorf("state: %s", r.Body) |
| 231 | } |
| 232 | } |
| 233 | |