This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
goneb_services_test.go
340 lines (302 loc) · 10.9 KB
/
goneb_services_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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/matrix-org/go-neb/clients"
"github.com/matrix-org/go-neb/database"
"maunium.net/go/mautrix/crypto"
"maunium.net/go/mautrix/crypto/olm"
mevt "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
func setupMockServer() (*http.ServeMux, *matrixTripper, *httptest.ResponseRecorder, chan string) {
mux := http.NewServeMux()
mxTripper := newMatrixTripper()
setup(envVars{
BaseURL: "http://go.neb",
DatabaseType: "sqlite3",
DatabaseURL: ":memory:",
}, mux, &http.Client{
Transport: mxTripper,
})
mxTripper.ClearHandlers()
mockWriter := httptest.NewRecorder()
reqChan := make(chan string)
mxTripper.HandlePOSTFilter("@link:hyrule")
mxTripper.Handle("GET", "/_matrix/client/r0/sync",
func(req *http.Request) (*http.Response, error) {
if _, ok := req.URL.Query()["since"]; !ok {
return newResponse(200, `{"next_batch":"11_22_33_44", "rooms": {}}`), nil
}
reqBody := <-reqChan
return newResponse(200, reqBody), nil
},
)
return mux, mxTripper, mockWriter, reqChan
}
func TestConfigureClient(t *testing.T) {
mux, _, mockWriter, _ := setupMockServer()
mockReq, _ := http.NewRequest("POST", "http://go.neb/admin/configureClient", bytes.NewBufferString(`
{
"UserID":"@link:hyrule",
"HomeserverURL":"http://hyrule.loz",
"AccessToken":"dangeroustogoalone",
"Sync":true,
"AutoJoinRooms":true
}`))
mux.ServeHTTP(mockWriter, mockReq)
expectCode := 200
if mockWriter.Code != expectCode {
t.Errorf("TestConfigureClient wanted HTTP status %d, got %d", expectCode, mockWriter.Code)
}
}
func TestRespondToEcho(t *testing.T) {
mux, mxTripper, mockWriter, reqChan := setupMockServer()
mxTripper.Handle("POST", "/_matrix/client/r0/keys/upload", func(req *http.Request) (*http.Response, error) {
return newResponse(200, `{}`), nil
})
var joinedRoom string
var joinedRoomBody []byte
mxTripper.Handle("POST", "/_matrix/client/r0/join/*", func(req *http.Request) (*http.Response, error) {
parts := strings.Split(req.URL.String(), "/")
joinedRoom = parts[len(parts)-1]
joinedRoomBody, _ = ioutil.ReadAll(req.Body)
return newResponse(200, `{}`), nil
})
var roomMsgBody []byte
mxTripper.Handle("PUT", "/_matrix/client/r0/rooms/!greatdekutree:hyrule/send/m.room.message/*", func(req *http.Request) (*http.Response, error) {
roomMsgBody, _ = ioutil.ReadAll(req.Body)
return newResponse(200, `{}`), nil
})
// configure the client
clientConfigReq, _ := http.NewRequest("POST", "http://go.neb/admin/configureClient", bytes.NewBufferString(`
{
"UserID":"@link:hyrule",
"HomeserverURL":"http://hyrule.loz",
"AccessToken":"dangeroustogoalone",
"Sync":true,
"AutoJoinRooms":true
}`))
mux.ServeHTTP(mockWriter, clientConfigReq)
// configure the echo service
serviceConfigReq, _ := http.NewRequest("POST", "http://go.neb/admin/configureService", bytes.NewBufferString(`
{
"Type": "echo",
"Id": "test_echo_service",
"UserID": "@link:hyrule",
"Config": {}
}`))
mux.ServeHTTP(mockWriter, serviceConfigReq)
// send neb an invite to a room
reqChan <- `{
"next_batch":"11_22_33_44",
"rooms": {
"invite": {
"!greatdekutree:hyrule": {"invite_state": {"events": [{
"type": "m.room.member",
"sender": "@navi:hyrule",
"content": {"membership": "invite"},
"state_key": "@link:hyrule",
"origin_server_ts": 10000,
"unsigned": {"age": 100},
"event_id": "evt123"
}]}}}
}
}`
// wait for it to be processed
reqChan <- `{"next_batch":"11_22_33_44", "rooms": {}}`
expectedRoom := "%21greatdekutree:hyrule"
if joinedRoom != expectedRoom {
t.Errorf("Expected join for room %v, got %v", expectedRoom, joinedRoom)
}
if expectedBody := `{"inviter":"@navi:hyrule"}`; string(joinedRoomBody) != expectedBody {
t.Errorf("Expected join message body to be %v, got %v", expectedBody, string(joinedRoomBody))
}
// send neb an !echo message
reqChan <- `{
"next_batch":"11_22_33_44",
"rooms": {
"join": {
"!greatdekutree:hyrule": {"timeline": {"events": [{
"type": "m.room.message",
"sender": "@navi:hyrule",
"content": {"body": "!echo save zelda", "msgtype": "m.text"},
"origin_server_ts": 10000,
"unsigned": {"age": 100},
"event_id": "evt124"
}]}}}
}
}`
// wait for it to be processed
reqChan <- `{"next_batch":"11_22_33_44", "rooms": {}}`
if expectedEchoResp := `{"msgtype":"m.notice","body":"save zelda"}`; string(roomMsgBody) != expectedEchoResp {
t.Errorf("Expected echo response to be `%v`, got `%v`", expectedEchoResp, string(roomMsgBody))
}
}
func TestEncryptedRespondToEcho(t *testing.T) {
mux, mxTripper, mockWriter, reqChan := setupMockServer()
// create the two accounts, inbound and outbound sessions, both the bot and mock ones
accountMock := olm.NewAccount()
accountBot := olm.NewAccount()
signingKeyMock, identityKeyMock := accountMock.IdentityKeys()
signingKeyBot, identityKeyBot := accountBot.IdentityKeys()
// encryptionEvtContent := &mevt.EncryptionEventContent{Algorithm: "m.megolm.v1.aes-sha2"}
ogsBot := crypto.NewOutboundGroupSession("!greatdekutree:hyrule", nil)
ogsBot.Shared = true
igsMock, err := crypto.NewInboundGroupSession(identityKeyBot, signingKeyBot, "!greatdekutree:hyrule", ogsBot.Internal.Key())
if err != nil {
t.Errorf("Error creating mock IGS: %v", err)
}
ogsMock := crypto.NewOutboundGroupSession("!greatdekutree:hyrule", nil)
ogsMock.Shared = true
igsBot, err := crypto.NewInboundGroupSession(identityKeyMock, signingKeyMock, "!greatdekutree:hyrule", ogsMock.Internal.Key())
if err != nil {
t.Errorf("Error creating bot IGS: %v", err)
}
mxTripper.Handle("POST", "/_matrix/client/r0/keys/upload", func(req *http.Request) (*http.Response, error) {
return newResponse(200, `{}`), nil
})
mxTripper.Handle("POST", "/_matrix/client/r0/keys/query", func(req *http.Request) (*http.Response, error) {
return newResponse(200, `{}`), nil
})
var joinedRoom string
var joinedRoomBody []byte
mxTripper.Handle("POST", "/_matrix/client/r0/join/*", func(req *http.Request) (*http.Response, error) {
parts := strings.Split(req.URL.String(), "/")
joinedRoom = parts[len(parts)-1]
joinedRoomBody, _ = ioutil.ReadAll(req.Body)
return newResponse(200, `{}`), nil
})
var decryptedMsg string
mxTripper.Handle("PUT", "/_matrix/client/r0/rooms/!greatdekutree:hyrule/send/m.room.encrypted/*", func(req *http.Request) (*http.Response, error) {
encryptedMsg, _ := ioutil.ReadAll(req.Body)
var encryptedContent mevt.EncryptedEventContent
encryptedContent.UnmarshalJSON(encryptedMsg)
decryptedMsgBytes, _, err := igsMock.Internal.Decrypt(encryptedContent.MegolmCiphertext)
if err != nil {
t.Errorf("Error decrypting message sent by bot: %v", err)
}
decryptedMsg = string(decryptedMsgBytes)
return newResponse(200, `{}`), nil
})
// configure the client
clientConfigReq, _ := http.NewRequest("POST", "http://go.neb/admin/configureClient", bytes.NewBufferString(`
{
"UserID":"@link:hyrule",
"DeviceID":"mastersword",
"HomeserverURL":"http://hyrule.loz",
"AccessToken":"dangeroustogoalone",
"Sync":true,
"AutoJoinRooms":true
}`))
mux.ServeHTTP(mockWriter, clientConfigReq)
// configure the echo service
serviceConfigReq, _ := http.NewRequest("POST", "http://go.neb/admin/configureService", bytes.NewBufferString(`
{
"Type": "echo",
"Id": "test_echo_service",
"UserID": "@link:hyrule",
"Config": {}
}`))
mux.ServeHTTP(mockWriter, serviceConfigReq)
// send neb an invite to a room
reqChan <- `{
"next_batch":"11_22_33_44",
"rooms": {
"invite": {
"!greatdekutree:hyrule": {"invite_state": {"events": [{
"type": "m.room.member",
"sender": "@navi:hyrule",
"content": {"membership": "invite"},
"state_key": "@link:hyrule",
"origin_server_ts": 10000,
"unsigned": {"age": 100},
"event_id": "evt123"
}]}}}
}
}`
// wait for it to be processed
reqChan <- `{"next_batch":"11_22_33_44", "rooms": {}}`
expectedRoom := "%21greatdekutree:hyrule"
if joinedRoom != expectedRoom {
t.Errorf("Expected join for room %v, got %v", expectedRoom, joinedRoom)
}
if expectedBody := `{"inviter":"@navi:hyrule"}`; string(joinedRoomBody) != expectedBody {
t.Errorf("Expected join message body to be %v, got %v", expectedBody, string(joinedRoomBody))
}
// send neb the room state: encrypted with one member
reqChan <- `{
"next_batch":"11_22_33_44",
"rooms": {
"join": {"!greatdekutree:hyrule": {"timeline": {"events": [{
"type": "m.room.encryption",
"state_key": "",
"content": {
"algorithm": "m.megolm.v1.aes-sha2"
}
}, {
"type": "m.room.member",
"sender": "@navi:hyrule",
"content": {"membership": "join", "displayname": "Navi"},
"state_key": "@navi:hyrule",
"origin_server_ts": 100,
"event_id": "evt124"
}]}}}
}
}`
// wait for it to be processed
reqChan <- `{"next_batch":"11_22_33_44", "rooms": {}}`
// DB is initialized, store the megolm sessions from before for the bot to be able to decrypt and encrypt
sqlDB, dialect := database.GetServiceDB().(*database.ServiceDB).GetSQLDb()
cryptoStore := crypto.NewSQLCryptoStore(sqlDB, dialect, "@link:hyrule-mastersword", "mastersword", []byte("masterswordpickle"), clients.CryptoMachineLogger{})
if err := cryptoStore.AddOutboundGroupSession(ogsBot); err != nil {
t.Errorf("Error storing bot OGS: %v", err)
}
if err := cryptoStore.PutGroupSession("!greatdekutree:hyrule", identityKeyMock, igsBot.ID(), igsBot); err != nil {
t.Errorf("Error storing bot IGS: %v", err)
}
cryptoStore.PutDevices("@navi:hyrule", map[id.DeviceID]*crypto.DeviceIdentity{
"NAVI": {
UserID: "@navi:hyrule",
DeviceID: "NAVI",
IdentityKey: identityKeyMock,
SigningKey: signingKeyMock,
},
})
plaintext := `{"room_id":"!greatdekutree:hyrule","type":"m.room.message","content":{"body":"!echo save zelda","msgtype":"m.text"}}`
ciphertext, err := ogsMock.Encrypt([]byte(plaintext))
if err != nil {
t.Errorf("Error encrypting bytes: %v", err)
}
// send neb an !echo message, encrypted with our mock OGS which it has an IGS for
reqChan <- fmt.Sprintf(`{
"next_batch":"11_22_33_44",
"rooms": {
"join": {"!greatdekutree:hyrule": {"timeline": {"events": [{
"type": "m.room.encrypted",
"sender": "@navi:hyrule",
"content": {
"algorithm":"m.megolm.v1.aes-sha2",
"sender_key":"%s",
"ciphertext":"%s",
"session_id":"%s",
"device_id": "NAVI"
},
"origin_server_ts": 10000,
"unsigned": {"age": 100},
"event_id": "evt125"
}]}}}
}
}`, identityKeyMock, string(ciphertext), ogsMock.ID())
// wait for it to be processed
reqChan <- `{"next_batch":"11_22_33_44", "rooms": {}}`
expectedDecryptedMsg := `{"room_id":"!greatdekutree:hyrule","type":"m.room.message","content":{"msgtype":"m.notice","body":"save zelda"}}`
if decryptedMsg != expectedDecryptedMsg {
t.Errorf("Expected decrypted message to be `%v`, got `%v`", expectedDecryptedMsg, decryptedMsg)
}
}