forked from matrix-org/sliding-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregressions_test.go
465 lines (431 loc) · 14.7 KB
/
regressions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package syncv3
import (
"encoding/json"
"net/http"
"testing"
"time"
"github.com/matrix-org/sliding-sync/sync2"
"github.com/matrix-org/sliding-sync/sync3"
"github.com/matrix-org/sliding-sync/sync3/extensions"
"github.com/matrix-org/sliding-sync/testutils"
"github.com/matrix-org/sliding-sync/testutils/m"
)
// catch all file for any kind of regression test which doesn't fall into a unique category
// Regression test for https://github.com/matrix-org/sliding-sync/issues/192
// - Bob on his server invites Alice to a room.
// - Alice joins the room first over federation. Proxy does the right thing and sets her membership to join. There is no timeline though due to not having backfilled.
// - Alice's client backfills in the room which pulls in the invite event, but the SS proxy doesn't see it as it's backfill, not /sync.
// - Charlie joins the same room via SS, which makes the SS proxy see 50 timeline events, which includes the invite.
// As the proxy has never seen this invite event before, it assumes it is newer than the join event and inserts it, corrupting state.
//
// Manually confirmed this can happen with 3x Element clients. We need to make sure we drop those earlier events.
// The first join over federation presents itself as a single join event in the timeline, with the create event, etc in state.
func TestBackfillInviteDoesntCorruptState(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
fedBob := "@bob:over_federation"
charlie := "@charlie:localhost"
charlieToken := "CHARLIE_TOKEN"
joinEvent := testutils.NewJoinEvent(t, alice)
room := roomEvents{
roomID: "!TestBackfillInviteDoesntCorruptState:localhost",
events: []json.RawMessage{
joinEvent,
},
state: createRoomState(t, fedBob, time.Now()),
}
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(room),
},
})
// alice syncs and should see the room.
aliceRes := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"a": {
Ranges: sync3.SliceRanges{{0, 20}},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 5,
},
},
},
})
m.MatchResponse(t, aliceRes, m.MatchList("a", m.MatchV3Count(1), m.MatchV3Ops(m.MatchV3SyncOp(0, 0, []string{room.roomID}))))
// Alice's client "backfills" new data in, meaning the next user who joins is going to see a different set of timeline events
dummyMsg := testutils.NewMessageEvent(t, fedBob, "you didn't see this before joining")
charlieJoinEvent := testutils.NewJoinEvent(t, charlie)
backfilledTimelineEvents := append(
room.state, []json.RawMessage{
dummyMsg,
testutils.NewStateEvent(t, "m.room.member", alice, fedBob, map[string]interface{}{
"membership": "invite",
}),
joinEvent,
charlieJoinEvent,
}...,
)
// now charlie also joins the room, causing a different response from /sync v2
v2.addAccount(t, charlie, charlieToken)
v2.queueResponse(charlie, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: room.roomID,
events: backfilledTimelineEvents,
}),
},
})
// and now charlie hits SS, which might corrupt membership state for alice.
charlieRes := v3.mustDoV3Request(t, charlieToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"a": {
Ranges: sync3.SliceRanges{{0, 20}},
},
},
})
m.MatchResponse(t, charlieRes, m.MatchList("a", m.MatchV3Count(1), m.MatchV3Ops(m.MatchV3SyncOp(0, 0, []string{room.roomID}))))
// alice should not see dummyMsg or the invite
aliceRes = v3.mustDoV3RequestWithPos(t, aliceToken, aliceRes.Pos, sync3.Request{})
m.MatchResponse(t, aliceRes, m.MatchNoV3Ops(), m.LogResponse(t), m.MatchRoomSubscriptionsStrict(
map[string][]m.RoomMatcher{
room.roomID: {
m.MatchJoinCount(3), // alice, bob, charlie,
m.MatchNoInviteCount(),
m.MatchNumLive(1),
m.MatchRoomTimeline([]json.RawMessage{charlieJoinEvent}),
},
},
))
}
func TestMalformedEventsTimeline(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
// unusual events ARE VALID EVENTS and should be sent to the client, but are unusual for some reason.
unusualEvents := []json.RawMessage{
testutils.NewStateEvent(t, "", "", alice, map[string]interface{}{
"empty string": "for event type",
}),
}
// malformed events are INVALID and should be ignored by the proxy.
malformedEvents := []json.RawMessage{
json.RawMessage(`{}`), // empty object
json.RawMessage(`{"type":5}`), // type is an integer
json.RawMessage(`{"type":"foo","content":{},"event_id":""}`), // 0-length string as event ID
json.RawMessage(`{"type":"foo","content":{}}`), // missing event ID
}
room := roomEvents{
roomID: "!TestMalformedEventsTimeline:localhost",
// append malformed after unusual. All malformed events should be dropped,
// leaving only unusualEvents.
events: append(unusualEvents, malformedEvents...),
state: createRoomState(t, alice, time.Now()),
}
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(room),
},
})
// alice syncs and should see the room.
aliceRes := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"a": {
Ranges: sync3.SliceRanges{{0, 20}},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: int64(len(unusualEvents)),
},
},
},
})
m.MatchResponse(t, aliceRes, m.MatchList("a", m.MatchV3Count(1), m.MatchV3Ops(m.MatchV3SyncOp(0, 0, []string{room.roomID}))),
m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
room.roomID: {
m.MatchJoinCount(1),
m.MatchRoomTimeline(unusualEvents),
},
}))
}
func TestMalformedEventsState(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
// unusual events ARE VALID EVENTS and should be sent to the client, but are unusual for some reason.
unusualEvents := []json.RawMessage{
testutils.NewStateEvent(t, "", "", alice, map[string]interface{}{
"empty string": "for event type",
}),
}
// malformed events are INVALID and should be ignored by the proxy.
malformedEvents := []json.RawMessage{
json.RawMessage(`{}`), // empty object
json.RawMessage(`{"type":5,"content":{},"event_id":"f","state_key":""}`), // type is an integer
json.RawMessage(`{"type":"foo","content":{},"event_id":"","state_key":""}`), // 0-length string as event ID
json.RawMessage(`{"type":"foo","content":{},"state_key":""}`), // missing event ID
}
latestEvent := testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "hi"})
room := roomEvents{
roomID: "!TestMalformedEventsState:localhost",
events: []json.RawMessage{latestEvent},
// append malformed after unusual. All malformed events should be dropped,
// leaving only unusualEvents.
state: append(createRoomState(t, alice, time.Now()), append(unusualEvents, malformedEvents...)...),
}
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(room),
},
})
// alice syncs and should see the room.
aliceRes := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"a": {
Ranges: sync3.SliceRanges{{0, 20}},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: int64(len(unusualEvents)),
RequiredState: [][2]string{{"", ""}},
},
},
},
})
m.MatchResponse(t, aliceRes, m.MatchList("a", m.MatchV3Count(1), m.MatchV3Ops(m.MatchV3SyncOp(0, 0, []string{room.roomID}))),
m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
room.roomID: {
m.MatchJoinCount(1),
m.MatchRoomTimeline([]json.RawMessage{latestEvent}),
m.MatchRoomRequiredState([]json.RawMessage{
unusualEvents[0],
}),
},
}))
}
// Regression test for https://github.com/matrix-org/sliding-sync/issues/295
// This test:
// - injects a good room and a bad room in the v2 response
// - then injects a good room update if the since token advanced
// - checks we see all updates
// In the past, the bad room in the first v2 response caused the proxy to retry and never advance,
// wedging the poller.
func TestBadCreateInitialiseDoesntWedgePolling(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
goodRoom := "!good:localhost"
badRoom := "!bad:localhost"
v2.addAccount(t, alice, aliceToken)
// we should see the since token increment, if we see repeats it means
// we aren't returning DataErrors when we should be.
wantSinces := []string{"", "1", "2"}
ch := make(chan bool)
v2.checkRequest = func(token string, req *http.Request) {
if len(wantSinces) == 0 {
return
}
gotSince := req.URL.Query().Get("since")
t.Logf("checkRequest got since=%v", gotSince)
want := wantSinces[0]
wantSinces = wantSinces[1:]
if gotSince != want {
t.Errorf("v2.checkRequest since got '%v' want '%v'", gotSince, want)
}
if len(wantSinces) == 0 {
close(ch)
}
}
// initial sync, everything fine
v2.queueResponse(alice, sync2.SyncResponse{
NextBatch: "1",
Rooms: sync2.SyncRoomsResponse{
Join: map[string]sync2.SyncV2JoinResponse{
goodRoom: {
Timeline: sync2.TimelineResponse{
Events: createRoomState(t, alice, time.Now()),
},
},
},
},
})
aliceRes := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"a": {
Ranges: sync3.SliceRanges{{0, 20}},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 1,
},
},
},
})
// we should only see 1 room
m.MatchResponse(t, aliceRes, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
goodRoom: {
m.MatchJoinCount(1),
},
},
))
// now inject a bad room and some extra good event
extraGoodEvent := testutils.NewMessageEvent(t, alice, "Extra!", testutils.WithTimestamp(time.Now().Add(time.Second)))
v2.queueResponse(alice, sync2.SyncResponse{
NextBatch: "2",
Rooms: sync2.SyncRoomsResponse{
Join: map[string]sync2.SyncV2JoinResponse{
goodRoom: {
Timeline: sync2.TimelineResponse{
Events: []json.RawMessage{extraGoodEvent},
},
},
badRoom: {
State: sync2.EventsResponse{
// BAD: missing create event
Events: createRoomState(t, alice, time.Now())[1:],
},
Timeline: sync2.TimelineResponse{
Events: []json.RawMessage{
testutils.NewMessageEvent(t, alice, "Hello World"),
},
},
},
},
},
})
v2.waitUntilEmpty(t, alice)
// we should see the extra good event and not the bad room
aliceRes = v3.mustDoV3RequestWithPos(t, aliceToken, aliceRes.Pos, sync3.Request{})
// we should only see 1 room
m.MatchResponse(t, aliceRes, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
goodRoom: {
m.MatchRoomTimelineMostRecent(1, []json.RawMessage{extraGoodEvent}),
},
},
))
// make sure we've seen all the v2 requests
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timed out waiting for all v2 requests")
}
}
// Regression test for https://github.com/matrix-org/sliding-sync/issues/295
// This test:
// - injects to-device msgs and a bad room in the v2 response
// - checks we see the to-device msgs
// It's possible for the poller to bail early and skip over processing the remaining response
// which this test is trying to safeguard against.
func TestBadPollDataDoesntDropToDeviceMsgs(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
goodRoom := "!good:localhost"
badRoom := "!bad:localhost"
v2.addAccount(t, alice, aliceToken)
// we should see the since token increment, if we see repeats it means
// we aren't returning DataErrors when we should be.
wantSinces := []string{"", "1", "2"}
ch := make(chan bool)
v2.checkRequest = func(token string, req *http.Request) {
if len(wantSinces) == 0 {
return
}
gotSince := req.URL.Query().Get("since")
t.Logf("checkRequest got since=%v", gotSince)
want := wantSinces[0]
wantSinces = wantSinces[1:]
if gotSince != want {
t.Errorf("v2.checkRequest since got '%v' want '%v'", gotSince, want)
}
if len(wantSinces) == 0 {
close(ch)
}
}
// initial sync, everything fine
v2.queueResponse(alice, sync2.SyncResponse{
NextBatch: "1",
Rooms: sync2.SyncRoomsResponse{
Join: map[string]sync2.SyncV2JoinResponse{
goodRoom: {
Timeline: sync2.TimelineResponse{
Events: createRoomState(t, alice, time.Now()),
},
},
},
},
})
aliceRes := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Extensions: extensions.Request{
ToDevice: &extensions.ToDeviceRequest{
Core: extensions.Core{
Enabled: &boolTrue,
},
},
},
Lists: map[string]sync3.RequestList{
"a": {
Ranges: sync3.SliceRanges{{0, 20}},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 1,
},
},
},
})
// we should only see 1 room
m.MatchResponse(t, aliceRes, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
goodRoom: {
m.MatchJoinCount(1),
},
},
))
// now inject a bad room and some to-device events
toDeviceEvent := testutils.NewEvent(t, "m.todevice", alice, map[string]interface{}{"body": "testio"})
v2.queueResponse(alice, sync2.SyncResponse{
NextBatch: "2",
ToDevice: sync2.EventsResponse{
Events: []json.RawMessage{toDeviceEvent},
},
Rooms: sync2.SyncRoomsResponse{
Join: map[string]sync2.SyncV2JoinResponse{
badRoom: {
State: sync2.EventsResponse{
// BAD: missing create event
Events: createRoomState(t, alice, time.Now())[1:],
},
Timeline: sync2.TimelineResponse{
Events: []json.RawMessage{
testutils.NewMessageEvent(t, alice, "Hello World"),
},
},
},
},
},
})
v2.waitUntilEmpty(t, alice)
// we should see the to-device event and not the bad room
aliceRes = v3.mustDoV3RequestWithPos(t, aliceToken, aliceRes.Pos, sync3.Request{})
// we should only see the to-device event
m.MatchResponse(t, aliceRes,
m.LogResponse(t),
m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{}),
m.MatchToDeviceMessages([]json.RawMessage{toDeviceEvent}),
)
// make sure we've seen all the v2 requests
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timed out waiting for all v2 requests")
}
}