1
- // +build !synapse_blacklist
2
-
3
1
package tests
4
2
5
3
import (
@@ -28,8 +26,12 @@ import (
28
26
// To test this we need to:
29
27
// * Add an event with "bad" data into the room history, but don't send it.
30
28
// * 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
+ //
33
35
// sytest: Outbound federation will ignore a missing event with bad JSON for room version 6
34
36
func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6 (t * testing.T ) {
35
37
deployment := Deploy (t , b .BlueprintAlice )
@@ -43,6 +45,13 @@ func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *test
43
45
cancel := srv .Listen ()
44
46
defer cancel ()
45
47
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
+
46
55
ver := gomatrixserverlib .RoomVersionV6
47
56
charlie := srv .UserID ("charlie" )
48
57
room := srv .MustMakeRoom (t , ver , federation .InitialRoomEvents (ver , charlie ))
@@ -87,6 +96,7 @@ func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *test
87
96
}
88
97
room .AddEvent (signedBadEvent )
89
98
99
+ // send the first "good" event, referencing the broken event as a prev_event
90
100
sentEvent := srv .MustCreateEvent (t , room , b.Event {
91
101
Type : "m.room.message" ,
92
102
Sender : charlie ,
@@ -97,7 +107,7 @@ func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *test
97
107
room .AddEvent (sentEvent )
98
108
99
109
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 ) {
101
111
defer waiter .Finish ()
102
112
must .MatchRequest (t , req , match.HTTPRequest {
103
113
JSON : []match.JSON {
@@ -116,7 +126,7 @@ func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *test
116
126
responseBytes , err = json .Marshal (& res )
117
127
must .NotError (t , "failed to marshal response" , err )
118
128
w .Write (responseBytes )
119
- }). Methods ( "POST" )
129
+ }
120
130
121
131
fedClient := srv .FederationClient (deployment )
122
132
resp , err := fedClient .SendTransaction (context .Background (), gomatrixserverlib.Transaction {
@@ -131,9 +141,45 @@ func TestOutboundFederationIgnoresMissingEventWithBadJSONForRoomVersion6(t *test
131
141
if len (resp .PDUs ) != 1 {
132
142
t .Fatalf ("got %d errors, want 1" , len (resp .PDUs ))
133
143
}
134
- pduRes , ok := resp .PDUs [sentEvent .EventID ()]
144
+ _ , ok := resp .PDUs [sentEvent .EventID ()]
135
145
if ! ok {
136
146
t .Fatalf ("wrong PDU returned from send transaction, got %v want %s" , resp .PDUs , sentEvent .EventID ())
137
147
}
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 )
139
185
}
0 commit comments