Skip to content

Commit 94c1deb

Browse files
authored
Fix 'Outbound federation will ignore a missing event with bad JSON for room version 6' (#152)
This test got blacklisted for the release of Synapse 1.37.1. We can't assume that homeservers will process inbound federation events synchronously. In this particular case we replace the check for errors in /send response with a check that if we send a new event that references the expected rejected event we'll see a request for that event come in. c.f. matrix-org/synapse#10275 and matrix-org/sytest#1061
1 parent a1f0ee6 commit 94c1deb

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

tests/federation_room_get_missing_events_test.go

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build !synapse_blacklist
2-
31
package tests
42

53
import (
@@ -28,8 +26,12 @@ import (
2826
// To test this we need to:
2927
// * Add an event with "bad" data into the room history, but don't send it.
3028
// * Add a "good" event into the room history and send it.
31-
// * The homeserver attempts to get the missing event (with the bad data).
32-
// * Ensure that fetching the event results in an error.
29+
// * wait for the homeserver to attempt to get the missing event (with the bad data).
30+
// (The homeserver should reject the "good" event.)
31+
// * To check the good event was rejected, send another valid event pointing at
32+
// the first "good" event, and wait for a call to `/get_missing_events` for
33+
// that event (thus proving that the homeserver rejected the good event).
34+
//
3335
// sytest: Outbound federation will ignore a missing event with bad JSON for room version 6
3436
func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *testing.T) {
3537
deployment := Deploy(t, b.BlueprintAlice)
@@ -43,6 +45,13 @@ func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *test
4345
cancel := srv.Listen()
4446
defer cancel()
4547

48+
// register a handler for /get_missing_events, via a shim so that we can
49+
// behave differently as the test progresses.
50+
var onGetMissingEvents func(w http.ResponseWriter, req *http.Request)
51+
srv.Mux().HandleFunc("/_matrix/federation/v1/get_missing_events/{roomID}", func(w http.ResponseWriter, req *http.Request) {
52+
onGetMissingEvents(w, req)
53+
}).Methods("POST")
54+
4655
ver := gomatrixserverlib.RoomVersionV6
4756
charlie := srv.UserID("charlie")
4857
room := srv.MustMakeRoom(t, ver, federation.InitialRoomEvents(ver, charlie))
@@ -87,6 +96,7 @@ func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *test
8796
}
8897
room.AddEvent(signedBadEvent)
8998

99+
// send the first "good" event, referencing the broken event as a prev_event
90100
sentEvent := srv.MustCreateEvent(t, room, b.Event{
91101
Type: "m.room.message",
92102
Sender: charlie,
@@ -97,7 +107,7 @@ func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *test
97107
room.AddEvent(sentEvent)
98108

99109
waiter := NewWaiter()
100-
srv.Mux().HandleFunc("/_matrix/federation/v1/get_missing_events/{roomID}", func(w http.ResponseWriter, req *http.Request) {
110+
onGetMissingEvents = func(w http.ResponseWriter, req *http.Request) {
101111
defer waiter.Finish()
102112
must.MatchRequest(t, req, match.HTTPRequest{
103113
JSON: []match.JSON{
@@ -116,7 +126,7 @@ func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *test
116126
responseBytes, err = json.Marshal(&res)
117127
must.NotError(t, "failed to marshal response", err)
118128
w.Write(responseBytes)
119-
}).Methods("POST")
129+
}
120130

121131
fedClient := srv.FederationClient(deployment)
122132
resp, err := fedClient.SendTransaction(context.Background(), gomatrixserverlib.Transaction{
@@ -131,9 +141,45 @@ func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *test
131141
if len(resp.PDUs) != 1 {
132142
t.Fatalf("got %d errors, want 1", len(resp.PDUs))
133143
}
134-
pduRes, ok := resp.PDUs[sentEvent.EventID()]
144+
_, ok := resp.PDUs[sentEvent.EventID()]
135145
if !ok {
136146
t.Fatalf("wrong PDU returned from send transaction, got %v want %s", resp.PDUs, sentEvent.EventID())
137147
}
138-
must.NotEqualStr(t, pduRes.Error, "", "wanted an error string for pdu but was blank")
148+
149+
// older versions of Synapse returned an error for the 'good' PDU; nowadays
150+
// it just ignores it, so we need to send another event referring to the
151+
// first one and check that we get a /get_missing_events request.
152+
153+
message3 := srv.MustCreateEvent(t, room, b.Event{
154+
Type: "m.room.message",
155+
Sender: charlie,
156+
Content: map[string]interface{}{
157+
"body": "Message 3",
158+
},
159+
})
160+
room.AddEvent(message3)
161+
162+
waiter = NewWaiter()
163+
onGetMissingEvents = func(w http.ResponseWriter, req *http.Request) {
164+
must.MatchRequest(t, req, match.HTTPRequest{
165+
JSON: []match.JSON{
166+
match.JSONKeyEqual("earliest_events", []interface{}{latestEvent.EventID()}),
167+
match.JSONKeyEqual("latest_events", []interface{}{message3.EventID()}),
168+
},
169+
})
170+
defer waiter.Finish()
171+
172+
// we don't really care what we return here, so just return an empty body.
173+
w.WriteHeader(200)
174+
w.Write([]byte("{}"))
175+
}
176+
177+
resp, err = fedClient.SendTransaction(context.Background(), gomatrixserverlib.Transaction{
178+
TransactionID: "t2",
179+
Destination: gomatrixserverlib.ServerName("hs1"),
180+
PDUs: []json.RawMessage{
181+
message3.JSON(),
182+
},
183+
})
184+
waiter.Wait(t, 5*time.Second)
139185
}

0 commit comments

Comments
 (0)