forked from strukturag/nextcloud-spreed-signaling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_signaling_test.go
348 lines (327 loc) · 8.87 KB
/
api_signaling_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
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2017 struktur AG
*
* @author Joachim Bauch <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package signaling
import (
"encoding/json"
"fmt"
"testing"
)
type testCheckValid interface {
CheckValid() error
}
func wrapMessage(messageType string, msg testCheckValid) *ClientMessage {
wrapped := &ClientMessage{
Type: messageType,
}
switch messageType {
case "hello":
wrapped.Hello = msg.(*HelloClientMessage)
case "message":
wrapped.Message = msg.(*MessageClientMessage)
case "bye":
wrapped.Bye = msg.(*ByeClientMessage)
case "room":
wrapped.Room = msg.(*RoomClientMessage)
default:
return nil
}
return wrapped
}
func testMessages(t *testing.T, messageType string, valid_messages []testCheckValid, invalid_messages []testCheckValid) {
for _, msg := range valid_messages {
if err := msg.CheckValid(); err != nil {
t.Errorf("Message %+v should be valid, got %s", msg, err)
}
// If the inner message is valid, it should also be valid in a wrapped
// ClientMessage.
if wrapped := wrapMessage(messageType, msg); wrapped == nil {
t.Errorf("Unknown message type: %s", messageType)
} else if err := wrapped.CheckValid(); err != nil {
t.Errorf("Message %+v should be valid, got %s", wrapped, err)
}
}
for _, msg := range invalid_messages {
if err := msg.CheckValid(); err == nil {
t.Errorf("Message %+v should not be valid", msg)
}
// If the inner message is invalid, it should also be invalid in a
// wrapped ClientMessage.
if wrapped := wrapMessage(messageType, msg); wrapped == nil {
t.Errorf("Unknown message type: %s", messageType)
} else if err := wrapped.CheckValid(); err == nil {
t.Errorf("Message %+v should not be valid", wrapped)
}
}
}
func TestClientMessage(t *testing.T) {
// The message needs a type.
msg := ClientMessage{}
if err := msg.CheckValid(); err == nil {
t.Errorf("Message %+v should not be valid", msg)
}
}
func TestHelloClientMessage(t *testing.T) {
internalAuthParams := []byte("{\"backend\":\"https://domain.invalid\"}")
valid_messages := []testCheckValid{
&HelloClientMessage{
Version: HelloVersion,
Auth: HelloClientMessageAuth{
Params: &json.RawMessage{'{', '}'},
Url: "https://domain.invalid",
},
},
&HelloClientMessage{
Version: HelloVersion,
Auth: HelloClientMessageAuth{
Type: "client",
Params: &json.RawMessage{'{', '}'},
Url: "https://domain.invalid",
},
},
&HelloClientMessage{
Version: HelloVersion,
Auth: HelloClientMessageAuth{
Type: "internal",
Params: (*json.RawMessage)(&internalAuthParams),
},
},
&HelloClientMessage{
Version: HelloVersion,
ResumeId: "the-resume-id",
},
}
invalid_messages := []testCheckValid{
&HelloClientMessage{},
&HelloClientMessage{Version: "0.0"},
&HelloClientMessage{Version: HelloVersion},
&HelloClientMessage{
Version: HelloVersion,
Auth: HelloClientMessageAuth{
Params: &json.RawMessage{'{', '}'},
Type: "invalid-type",
},
},
&HelloClientMessage{
Version: HelloVersion,
Auth: HelloClientMessageAuth{
Url: "https://domain.invalid",
},
},
&HelloClientMessage{
Version: HelloVersion,
Auth: HelloClientMessageAuth{
Params: &json.RawMessage{'{', '}'},
},
},
&HelloClientMessage{
Version: HelloVersion,
Auth: HelloClientMessageAuth{
Params: &json.RawMessage{'{', '}'},
Url: "invalid-url",
},
},
&HelloClientMessage{
Version: HelloVersion,
Auth: HelloClientMessageAuth{
Type: "internal",
Params: &json.RawMessage{'{', '}'},
},
},
&HelloClientMessage{
Version: HelloVersion,
Auth: HelloClientMessageAuth{
Type: "internal",
Params: &json.RawMessage{'x', 'y', 'z'}, // Invalid JSON.
},
},
}
testMessages(t, "hello", valid_messages, invalid_messages)
// A "hello" message must be present
msg := ClientMessage{
Type: "hello",
}
if err := msg.CheckValid(); err == nil {
t.Errorf("Message %+v should not be valid", msg)
}
}
func TestMessageClientMessage(t *testing.T) {
valid_messages := []testCheckValid{
&MessageClientMessage{
Recipient: MessageClientMessageRecipient{
Type: "session",
SessionId: "the-session-id",
},
Data: &json.RawMessage{'{', '}'},
},
&MessageClientMessage{
Recipient: MessageClientMessageRecipient{
Type: "user",
UserId: "the-user-id",
},
Data: &json.RawMessage{'{', '}'},
},
&MessageClientMessage{
Recipient: MessageClientMessageRecipient{
Type: "room",
},
Data: &json.RawMessage{'{', '}'},
},
}
invalid_messages := []testCheckValid{
&MessageClientMessage{},
&MessageClientMessage{
Recipient: MessageClientMessageRecipient{
Type: "session",
SessionId: "the-session-id",
},
},
&MessageClientMessage{
Recipient: MessageClientMessageRecipient{
Type: "session",
},
Data: &json.RawMessage{'{', '}'},
},
&MessageClientMessage{
Recipient: MessageClientMessageRecipient{
Type: "session",
UserId: "the-user-id",
},
Data: &json.RawMessage{'{', '}'},
},
&MessageClientMessage{
Recipient: MessageClientMessageRecipient{
Type: "user",
},
Data: &json.RawMessage{'{', '}'},
},
&MessageClientMessage{
Recipient: MessageClientMessageRecipient{
Type: "user",
UserId: "the-user-id",
},
},
&MessageClientMessage{
Recipient: MessageClientMessageRecipient{
Type: "user",
SessionId: "the-user-id",
},
Data: &json.RawMessage{'{', '}'},
},
&MessageClientMessage{
Recipient: MessageClientMessageRecipient{
Type: "unknown-type",
},
Data: &json.RawMessage{'{', '}'},
},
}
testMessages(t, "message", valid_messages, invalid_messages)
// A "message" message must be present
msg := ClientMessage{
Type: "message",
}
if err := msg.CheckValid(); err == nil {
t.Errorf("Message %+v should not be valid", msg)
}
}
func TestByeClientMessage(t *testing.T) {
// Any "bye" message is valid.
valid_messages := []testCheckValid{
&ByeClientMessage{},
}
invalid_messages := []testCheckValid{}
testMessages(t, "bye", valid_messages, invalid_messages)
// The "bye" message is optional.
msg := ClientMessage{
Type: "bye",
}
if err := msg.CheckValid(); err != nil {
t.Errorf("Message %+v should be valid, got %s", msg, err)
}
}
func TestRoomClientMessage(t *testing.T) {
// Any "room" message is valid.
valid_messages := []testCheckValid{
&RoomClientMessage{},
}
invalid_messages := []testCheckValid{}
testMessages(t, "room", valid_messages, invalid_messages)
// But a "room" message must be present
msg := ClientMessage{
Type: "room",
}
if err := msg.CheckValid(); err == nil {
t.Errorf("Message %+v should not be valid", msg)
}
}
func TestErrorMessages(t *testing.T) {
id := "request-id"
msg := ClientMessage{
Id: id,
}
err1 := msg.NewErrorServerMessage(&Error{})
if err1.Id != id {
t.Errorf("Expected id %s, got %+v", id, err1)
}
if err1.Type != "error" || err1.Error == nil {
t.Errorf("Expected type \"error\", got %+v", err1)
}
err2 := msg.NewWrappedErrorServerMessage(fmt.Errorf("test-error"))
if err2.Id != id {
t.Errorf("Expected id %s, got %+v", id, err2)
}
if err2.Type != "error" || err2.Error == nil {
t.Errorf("Expected type \"error\", got %+v", err2)
}
if err2.Error.Code != "internal_error" {
t.Errorf("Expected code \"internal_error\", got %+v", err2)
}
if err2.Error.Message != "test-error" {
t.Errorf("Expected message \"test-error\", got %+v", err2)
}
// Test "error" interface
if err2.Error.Error() != "test-error" {
t.Errorf("Expected error string \"test-error\", got %+v", err2)
}
}
func TestIsChatRefresh(t *testing.T) {
var msg ServerMessage
data_true := []byte("{\"type\":\"chat\",\"chat\":{\"refresh\":true}}")
msg = ServerMessage{
Type: "message",
Message: &MessageServerMessage{
Data: (*json.RawMessage)(&data_true),
},
}
if !msg.IsChatRefresh() {
t.Error("message should be detected as chat refresh")
}
data_false := []byte("{\"type\":\"chat\",\"chat\":{\"refresh\":false}}")
msg = ServerMessage{
Type: "message",
Message: &MessageServerMessage{
Data: (*json.RawMessage)(&data_false),
},
}
if msg.IsChatRefresh() {
t.Error("message should not be detected as chat refresh")
}
}