oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1package bridge
2
3import (
4 "encoding/base64"
5 "net/http"
6 "net/http/httptest"
7 "sync/atomic"
8 "testing"
9 "time"
10)
11
12type discardEvents struct{}
13
14func (discardEvents) OnEvent(string) {}
15func TestBridgeRequiresHumanSessionAndSafeOrigin(t *testing.T) {
16 token := base64.RawURLEncoding.EncodeToString(make([]byte, 32))
17 cookie := `[{"name":"__Host-rastrillo_session","value":"` + token + `"}]`
18 for _, tc := range []struct{ origin, cookies string }{
19 {"https://calls.example.com", `[]`},
20 {"https://calls.example.com", `[{"name":"Authorization","value":"Bearer mt_fake"}]`},
21 {"https://calls.example.com", `[{"name":"__Host-rastrillo_session","value":"bad"}]`},
22 {"http://calls.example.com", cookie},
23 {"https://calls.example.com/path", cookie},
24 } {
25 if c, err := NewClient(tc.origin, "room", tc.cookies, discardEvents{}); err == nil {
26 c.Close()
27 t.Fatal("accepted invalid native session")
28 }
29 }
30 c, err := NewClient("https://calls.example.com", "room", cookie, discardEvents{})
31 if err != nil {
32 t.Fatal(err)
33 }
34 c.Close()
35}
36
37// loopbackSession is a session cookie as the handoff returns it for an
38// http://127.0.0.1 instance, which is the one place a test server can
39// be and the bridge will still send it.
40func loopbackSession() (string, string) {
41 token := base64.RawURLEncoding.EncodeToString(make([]byte, 32))
42 return token, `[{"name":"rastrillo_session","value":"` + token + `"}]`
43}
44
45func TestCheckSessionCarriesTheCookieAndAnswersAValue(t *testing.T) {
46 token, cookies := loopbackSession()
47 status := http.StatusOK
48 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
49 if c, err := r.Cookie("rastrillo_session"); err != nil || c.Value != token {
50 t.Error("session not sent")
51 }
52 if status == http.StatusSeeOther {
53 http.Redirect(w, r, "/signin", status)
54 return
55 }
56 w.WriteHeader(status)
57 }))
58 defer server.Close()
59 for s, want := range map[int]string{http.StatusOK: "member", http.StatusNotFound: "participant", http.StatusSeeOther: SignedOut} {
60 status = s
61 got, err := CheckSession(server.URL, cookies)
62 if err != nil || got != want {
63 t.Errorf("status %d gave %q, %v", s, got, err)
64 }
65 }
66 status = http.StatusBadGateway
67 if got, err := CheckSession(server.URL, cookies); err == nil || got == SignedOut {
68 t.Fatal("a bad gateway signed the app out", got, err)
69 }
70}
71
72func TestCheckSessionCallsAStoredCopyItCannotSendSignedOut(t *testing.T) {
73 var calls atomic.Int32
74 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { calls.Add(1) }))
75 defer server.Close()
76 for _, cookies := range []string{``, `[]`, `not json`, `[{"name":"rastrillo_session","value":"short"}]`} {
77 if got, err := CheckSession(server.URL, cookies); err != nil || got != SignedOut {
78 t.Errorf("%q gave %q, %v", cookies, got, err)
79 }
80 }
81 if calls.Load() != 0 {
82 t.Fatal("sent a session it should have refused", calls.Load())
83 }
84}
85
86func TestOpenLinkKeepsNoMeetingAndNoSessionApart(t *testing.T) {
87 _, cookies := loopbackSession()
88 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
89 switch r.URL.Path {
90 case "/j/live":
91 http.Redirect(w, r, "/m/abc123/join", http.StatusSeeOther)
92 case "/j/gone":
93 http.Redirect(w, r, "/signin", http.StatusSeeOther)
94 case "/m/xyz99/info":
95 w.Write([]byte(`{"title":"x"}`))
96 default:
97 http.NotFound(w, r)
98 }
99 }))
100 defer server.Close()
101 for address, want := range map[string]string{
102 server.URL: `{"room":""}`,
103 server.URL + "/j/live": `{"room":"abc123"}`,
104 server.URL + "/j/gone": `{"room":"","signedOut":true}`,
105 server.URL + "/m/xyz99": `{"room":"xyz99"}`,
106 // Signed in, as somebody this meeting is not for (2026-10-02).
107 server.URL + "/m/other1": `{"room":"","noAccess":true}`,
108 } {
109 got, err := OpenLink(server.URL, cookies, address)
110 if err != nil || got != want {
111 t.Errorf("%q gave %s, %v", address, got, err)
112 }
113 }
114 if _, err := OpenLink(server.URL, cookies, server.URL+"/j/replaced"); err == nil {
115 t.Fatal("a replaced link opened")
116 }
117}
118
119// TestReportsCarryTheHellosTokenAfterLeave is spec §15.26 through the
120// bridge: the token from the stream's hello goes on every report request
121// with the session, the answers come back as values the host can act on,
122// and all of them still work once the call has been left. The automatic
123// report (§15.29) is answered as Report a problem is, and its 204 is the
124// kill switch.
125func TestReportsCarryTheHellosTokenAfterLeave(t *testing.T) {
126 const report = "0123456789abcdef0123456789abcdef"
127 session, cookies := loopbackSession()
128 var quality, problem, auto atomic.Int32
129 hello := make(chan struct{})
130 var once atomic.Bool
131 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
132 if c, err := r.Cookie("rastrillo_session"); err != nil || c.Value != session {
133 t.Error("session not sent")
134 }
135 switch r.URL.Path {
136 case "/m/room/stream":
137 w.Header().Set("Content-Type", "text/event-stream")
138 w.Write([]byte("data: {\"type\":\"hello\",\"peer\":\"self\",\"report\":\"" + report + "\"}\n\n"))
139 w.(http.Flusher).Flush()
140 if once.CompareAndSwap(false, true) {
141 close(hello)
142 }
143 <-r.Context().Done()
144 case "/m/room/quality":
145 if r.Header.Get("X-Calls-Report") != report {
146 t.Error("quality without the token")
147 }
148 if quality.Add(1) == 1 {
149 w.WriteHeader(http.StatusAccepted)
150 } else {
151 w.WriteHeader(http.StatusNoContent)
152 }
153 case "/m/room/problem":
154 if r.Header.Get("X-Calls-Report") != report {
155 t.Error("problem without the token")
156 }
157 if problem.Add(1) == 1 {
158 w.Write([]byte(`{"ref":"K7Q2M9PD"}`))
159 } else {
160 w.WriteHeader(http.StatusTooManyRequests)
161 }
162 case "/m/room/auto-report":
163 if r.Header.Get("X-Calls-Report") != report {
164 t.Error("auto report without the token")
165 }
166 switch auto.Add(1) {
167 case 1:
168 w.Write([]byte(`{"ref":"AB12CD34"}`))
169 case 2:
170 w.WriteHeader(http.StatusTooManyRequests)
171 case 3:
172 w.WriteHeader(http.StatusNotFound)
173 default:
174 w.WriteHeader(http.StatusNoContent)
175 }
176 case "/m/room/info":
177 w.Write([]byte(`{"title":"x"}`))
178 default:
179 // The join, and the leave.
180 w.WriteHeader(http.StatusOK)
181 }
182 }))
183 defer server.Close()
184 c, err := NewClient(server.URL, "room", cookies, discardEvents{})
185 if err != nil {
186 t.Fatal(err)
187 }
188 if got := c.SendQuality(`{}`); got != "gone" {
189 t.Fatalf("before the hello: %q", got)
190 }
191 if got := c.SendAutoReport(`{}`); got != `{"outcome":"gone"}` {
192 t.Fatalf("auto report before the hello: %s", got)
193 }
194 if err := c.Start(); err != nil {
195 t.Fatal(err)
196 }
197 <-hello
198 // The hello is applied after it is read; give the core its turn.
199 for i := 0; i < 100 && c.SendQuality(`{"v":1}`) == "gone"; i++ {
200 time.Sleep(20 * time.Millisecond)
201 }
202 c.Leave()
203 if got := c.SendQuality(`{"v":1,"final":true}`); got != "stop" {
204 t.Fatalf("after leave, the kill switch: %q", got)
205 }
206 if got := c.SendProblem(`{"note":"x"}`); got != `{"ref":"K7Q2M9PD"}` {
207 t.Fatalf("problem: %s", got)
208 }
209 if got := c.SendProblem(`{"note":"x"}`); got != `{"outcome":"too-many"}` {
210 t.Fatalf("second problem: %s", got)
211 }
212 auto1 := `{"trigger":"freezes","diagnostics":"peer abc123","client":{"app":"mac"}}`
213 for i, want := range []string{
214 `{"ref":"AB12CD34"}`, `{"outcome":"too-many"}`, `{"outcome":"gone"}`, `{"outcome":"stop"}`,
215 } {
216 if got := c.SendAutoReport(auto1); got != want {
217 t.Fatalf("auto report %d: %s, want %s", i+1, got, want)
218 }
219 }
220 if got := c.SendAutoReport(`{not json`); got != `{"outcome":"failed"}` {
221 t.Fatalf("an invalid auto report: %s", got)
222 }
223}
224