forked from matrix-org/complement
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvalid_test.go
More file actions
230 lines (200 loc) · 5.43 KB
/
Copy pathinvalid_test.go
File metadata and controls
230 lines (200 loc) · 5.43 KB
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
package csapi_tests
import (
"strings"
"testing"
"github.com/matrix-org/complement"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/match"
"github.com/matrix-org/complement/must"
"github.com/matrix-org/complement/runtime"
)
func TestJson(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_opts": map[string]interface{}{
"room_version": "6",
},
})
t.Run("Parallel", func(t *testing.T) {
// sytest: Invalid JSON integers
// sytest: Invalid JSON floats
t.Run("Invalid numerical values", func(t *testing.T) {
t.Parallel()
testCases := [][]byte{
[]byte(`{"body": 9007199254740992}`),
[]byte(`{"body": -9007199254740992}`),
[]byte(`{"body": 1.1}`),
}
for _, testCase := range testCases {
res := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "complement.dummy", helpers.GetTxnID("TestJson-InvalidNum")}, client.WithJSONBody(t, testCase))
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 400,
JSON: []match.JSON{
match.JSONKeyEqual("errcode", "M_BAD_JSON"),
},
})
}
})
// sytest: Invalid JSON special values
t.Run("Invalid JSON special values", func(t *testing.T) {
t.Parallel()
testCases := [][]byte{
[]byte(`{"body": Infinity}`),
[]byte(`{"body": -Infinity}`),
[]byte(`{"body": NaN}`),
}
for _, testCase := range testCases {
res := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "complement.dummy", helpers.GetTxnID("TestJson-InvalidVal")}, client.WithJSONBody(t, testCase))
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 400,
})
}
})
})
}
// small helper function to not bloat the main one
// todo: this should be more exhaustive and up-to-date
// todo: this should be easier to construct
func getFilters() []map[string]interface{} {
const NAO = "not_an_object"
const NAL = "not_a_list"
return []map[string]interface{}{
{
"presence": NAO,
},
{
"room": map[string]interface{}{
"timeline": NAO,
},
},
{
"room": map[string]interface{}{
"state": NAO,
},
},
{
"room": map[string]interface{}{
"ephemeral": NAO,
},
},
{
"room": map[string]interface{}{
"account_data": NAO,
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"rooms": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"not_rooms": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"senders": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"not_senders": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"types": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"not_types": NAL,
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"types": []int{1},
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"rooms": []string{"not_a_room_id"},
},
},
},
{
"room": map[string]interface{}{
"timeline": map[string]interface{}{
"senders": []string{"not_a_sender_id"},
},
},
},
}
}
// sytest: Check creating invalid filters returns 4xx
func TestFilter(t *testing.T) {
runtime.SkipIf(t, runtime.Dendrite) // FIXME: https://github.com/matrix-org/dendrite/issues/2067
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
filters := getFilters()
for _, filter := range filters {
res := alice.Do(t, "POST", []string{"_matrix", "client", "v3", "user", alice.UserID, "filter"}, client.WithJSONBody(t, filter))
if res.StatusCode >= 500 || res.StatusCode < 400 {
t.Errorf("Expected 4XX status code, got %d for testing filter %s", res.StatusCode, filter)
}
}
}
// sytest: Event size limits
func TestEvent(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"room_opts": map[string]interface{}{
"room_version": "6",
},
})
t.Run("Parallel", func(t *testing.T) {
t.Run("Large Event", func(t *testing.T) {
t.Parallel()
event := map[string]interface{}{
"msgtype": "m.text",
"body": strings.Repeat("and they dont stop coming ", 2700), // 2700 * 26 == 70200
}
res := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "send", "m.room.message", "1"}, client.WithJSONBody(t, event))
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 413,
})
})
t.Run("Large State Event", func(t *testing.T) {
t.Parallel()
stateEvent := map[string]interface{}{
"body": strings.Repeat("Dormammu, I've Come To Bargain.\n", 2200), // 2200 * 32 == 70400
}
res := alice.Do(t, "PUT", []string{"_matrix", "client", "v3", "rooms", roomID, "state", "marvel.universe.fate"}, client.WithJSONBody(t, stateEvent))
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: 413,
})
})
})
}