| 1 | package callstest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // twoMembersInOneRoom is the shape every case below starts from: two |
| 11 | // signed-in members of the same meeting, both joined, both streams |
| 12 | // open, and each one's peer id. |
| 13 | func twoMembersInOneRoom(t *testing.T) (x *harness, sqid string, a, b *client, sa, sb *sseReader, pa, pb string) { |
| 14 | t.Helper() |
| 15 | x, sqid, _ = memberWithMeeting(t) |
| 16 | a = x.browser(t) |
| 17 | a.signIn("paul@example.com") |
| 18 | sa = a.stream("/m/" + sqid + "/stream") |
| 19 | t.Cleanup(sa.close) |
| 20 | pa, _ = sa.hello() |
| 21 | a.signal(sqid, pa, `{"type":"join"}`) |
| 22 | |
| 23 | b = newMemberBrowser(t, x, "paul@rslw.com") |
| 24 | sb = b.stream("/m/" + sqid + "/stream") |
| 25 | t.Cleanup(sb.close) |
| 26 | pb, _ = sb.hello() |
| 27 | b.signal(sqid, pb, `{"type":"join"}`) |
| 28 | if ev, _, ok := sa.nextOf("peer", 2*time.Second); !ok { |
| 29 | t.Fatalf("the two members never met (got %q)", ev) |
| 30 | } |
| 31 | return |
| 32 | } |
| 33 | |
| 34 | // gone reads the `gone` event the room is owed when a participation |
| 35 | // ends, and says whether it named this peer. |
| 36 | func sawGone(t *testing.T, s *sseReader, peer string) bool { |
| 37 | t.Helper() |
| 38 | ev, data, ok := s.nextOf("gone", 2*time.Second) |
| 39 | if !ok { |
| 40 | t.Logf("no gone event (got %q)", ev) |
| 41 | return false |
| 42 | } |
| 43 | var g struct{ From string } |
| 44 | json.Unmarshal(data, &g) |
| 45 | return g.From == peer |
| 46 | } |
| 47 | |
| 48 | // Spec §2.5's paragraph, corrected into the table by §15.12: a |
| 49 | // participation whose kind is `member` re-derives standing on every |
| 50 | // request, not only on the fifteen-second tick — "a member who has just |
| 51 | // been removed is dropped by the next thing they do". Codex S2-1 found |
| 52 | // every one of these accepting a removed member's traffic until the |
| 53 | // tick caught up. |
| 54 | // |
| 55 | // Each case revokes B's membership and then makes exactly one request, |
| 56 | // before any tick has run. |
| 57 | func TestAMemberWithNoStandingIsRefusedByTheNextRequestNotTheNextTick(t *testing.T) { |
| 58 | t.Run("relay", func(t *testing.T) { |
| 59 | x, sqid, _, b, sa, _, pa, pb := twoMembersInOneRoom(t) |
| 60 | // The answer a peer that never existed gets, read before |
| 61 | // anything is revoked: a dropped member must be told exactly |
| 62 | // this and nothing more (§2.5, "404, never 403"). |
| 63 | unknown, unknownBody := b.signal(sqid, pb, `{"type":"offer","to":"`+zeroPeer+`","seq":1,"gen":1,"payload":{}}`) |
| 64 | if unknown.StatusCode != http.StatusNotFound { |
| 65 | t.Fatalf("offer to an unknown peer: %d", unknown.StatusCode) |
| 66 | } |
| 67 | |
| 68 | x.ctl.SetAllowlist("paul@example.com") // B is off the team |
| 69 | resp, body := b.signal(sqid, pb, `{"type":"offer","to":"`+pa+`","seq":2,"gen":1,"payload":{}}`) |
| 70 | if resp.StatusCode != http.StatusNotFound { |
| 71 | t.Fatalf("relay by a removed member before the tick: %d %s", resp.StatusCode, body) |
| 72 | } |
| 73 | if string(body) != string(unknownBody) { |
| 74 | t.Fatalf("relay refusal body %s, want the unknown-peer body %s", body, unknownBody) |
| 75 | } |
| 76 | if !sawGone(t, sa, pb) { |
| 77 | t.Fatal("the room was not told the removed member had gone") |
| 78 | } |
| 79 | }) |
| 80 | |
| 81 | t.Run("reset", func(t *testing.T) { |
| 82 | x, sqid, _, b, sa, _, pa, pb := twoMembersInOneRoom(t) |
| 83 | x.ctl.SetAllowlist("paul@example.com") |
| 84 | resp, body := b.signal(sqid, pb, `{"type":"reset","to":"`+pa+`"}`) |
| 85 | if resp.StatusCode != http.StatusNotFound { |
| 86 | t.Fatalf("reset by a removed member before the tick: %d %s", resp.StatusCode, body) |
| 87 | } |
| 88 | if !sawGone(t, sa, pb) { |
| 89 | t.Fatal("the room was not told the removed member had gone") |
| 90 | } |
| 91 | }) |
| 92 | |
| 93 | t.Run("resume", func(t *testing.T) { |
| 94 | x, sqid, _, b, sa, sb, _, pb := twoMembersInOneRoom(t) |
| 95 | sb.close() |
| 96 | x.ctl.SetAllowlist("paul@example.com") |
| 97 | rec := b.getStream("/m/" + sqid + "/stream?peer=" + pb) |
| 98 | _, boot, reason := answer(t, rec) |
| 99 | if rec.Code != http.StatusNotFound || boot == "" || reason != "" { |
| 100 | t.Fatalf("resume by a removed member before the tick: %d boot=%q reason=%q, want 404 with a boot and no reason", |
| 101 | rec.Code, boot, reason) |
| 102 | } |
| 103 | if !sawGone(t, sa, pb) { |
| 104 | t.Fatal("the room was not told the removed member had gone") |
| 105 | } |
| 106 | }) |
| 107 | |
| 108 | // The probe answers the same 404 and ends nothing: §15.3 defines it |
| 109 | // as changing nothing at all, so the ending is the next mutating |
| 110 | // request's, or the tick's. |
| 111 | t.Run("probe", func(t *testing.T) { |
| 112 | x, sqid, _, b, sa, _, pa, pb := twoMembersInOneRoom(t) |
| 113 | x.ctl.SetAllowlist("paul@example.com") |
| 114 | rec := b.get("/m/" + sqid + "/stream?probe=1&peer=" + pb) |
| 115 | _, boot, reason := answer(t, rec) |
| 116 | if rec.Code != http.StatusNotFound || boot == "" || reason != "" { |
| 117 | t.Fatalf("probe by a removed member before the tick: %d boot=%q reason=%q, want 404 with a boot and no reason", |
| 118 | rec.Code, boot, reason) |
| 119 | } |
| 120 | if ev, _, ok := sa.nextOf("gone", 500*time.Millisecond); ok { |
| 121 | t.Fatalf("the probe ended the participation (%q reached the room)", ev) |
| 122 | } |
| 123 | // Still there, and still the room's to lose on the next thing |
| 124 | // this member actually does. |
| 125 | if resp, body := b.signal(sqid, pb, `{"type":"reset","to":"`+pa+`"}`); resp.StatusCode != http.StatusNotFound { |
| 126 | t.Fatalf("reset after the probe: %d %s", resp.StatusCode, body) |
| 127 | } |
| 128 | if !sawGone(t, sa, pb) { |
| 129 | t.Fatal("the room was not told the removed member had gone") |
| 130 | } |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | // The other half of §15.12, and ruling 14's guarantee: an admitted |
| 135 | // guest is untouched by any of this. Their grant has expired, which is |
| 136 | // exactly the state a member would be dropped for, and they go on |
| 137 | // relaying, resetting and resuming — the participation is the authority |
| 138 | // once someone is in. |
| 139 | func TestAnAdmittedGuestWithNoStandingKeepsRelayingResettingAndResuming(t *testing.T) { |
| 140 | x, sqid, tok := memberWithMeeting(t) |
| 141 | m := x.browser(t) |
| 142 | m.signIn("paul@example.com") |
| 143 | sm := m.stream("/m/" + sqid + "/stream") |
| 144 | defer sm.close() |
| 145 | pm, _ := sm.hello() |
| 146 | m.signal(sqid, pm, `{"type":"join"}`) |
| 147 | |
| 148 | g := x.browser(t) |
| 149 | g.get("/j/" + tok) |
| 150 | g.signIn("guest@example.com") |
| 151 | sg := g.stream("/m/" + sqid + "/stream") |
| 152 | defer sg.close() |
| 153 | pg, _ := sg.hello() |
| 154 | g.signal(sqid, pg, `{"type":"join"}`) |
| 155 | m.signal(sqid, pm, `{"type":"admit","payload":{"peer":"`+pg+`"}}`) |
| 156 | if ev, _, ok := sg.nextOf("roster", 2*time.Second); !ok { |
| 157 | t.Fatalf("the guest was never admitted (got %q)", ev) |
| 158 | } |
| 159 | |
| 160 | x.advance(2*time.Hour + time.Minute) // the grant is over |
| 161 | |
| 162 | if resp, body := g.signal(sqid, pg, `{"type":"offer","to":"`+pm+`","seq":1,"gen":1,"payload":{}}`); resp.StatusCode != http.StatusOK { |
| 163 | t.Fatalf("relay past grant expiry: %d %s", resp.StatusCode, body) |
| 164 | } |
| 165 | if resp, body := g.signal(sqid, pg, `{"type":"reset","to":"`+pm+`"}`); resp.StatusCode != http.StatusOK { |
| 166 | t.Fatalf("reset past grant expiry: %d %s", resp.StatusCode, body) |
| 167 | } |
| 168 | if rec := g.get("/m/" + sqid + "/stream?probe=1&peer=" + pg); rec.Code != http.StatusNoContent { |
| 169 | t.Fatalf("resume probe past grant expiry: %d, want 204", rec.Code) |
| 170 | } |
| 171 | sg.close() |
| 172 | sg2 := g.stream("/m/" + sqid + "/stream?peer=" + pg) |
| 173 | defer sg2.close() |
| 174 | if p, _ := sg2.hello(); p != pg { |
| 175 | t.Fatalf("resume past grant expiry: %s, want %s", p, pg) |
| 176 | } |
| 177 | if ev, _, ok := sm.nextOf("gone", 500*time.Millisecond); ok { |
| 178 | t.Fatalf("the expired guest was dropped by their own requests (%q reached the member)", ev) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // zeroPeer is a peer id of the right shape that no room ever issued. |
| 183 | const zeroPeer = "00000000000000000000000000000000" |
| 184 | |