forked from soldank-plus-plus/spp-lobby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers_test.go
377 lines (323 loc) · 12.2 KB
/
servers_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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/nedik/spp-lobby/controllers"
"github.com/nedik/spp-lobby/routes"
"github.com/nedik/spp-lobby/types"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
type InvalidInput struct {
SomeValue string `json:"some_value"`
}
type TestEnvironment struct {
HTTPRecorder *httptest.ResponseRecorder
Context *gin.Context
Router *gin.Engine
}
func setupRouter() TestEnvironment {
var testEnvironment TestEnvironment
testEnvironment.HTTPRecorder = httptest.NewRecorder()
testEnvironment.Context, testEnvironment.Router = gin.CreateTestContext(testEnvironment.HTTPRecorder)
serverController := controllers.NewServerController()
serverRouteController := routes.NewServerRouteController(serverController)
routerGroup := testEnvironment.Router.Group("/")
serverRouteController.InitServerRoutes(routerGroup)
return testEnvironment
}
func TestGetServersEmptyList(t *testing.T) {
testEnvironment := setupRouter()
getServersAndAssert(t, testEnvironment, []types.Server{})
}
func TestRegisterNewServer(t *testing.T) {
testEnvironment := setupRouter()
registeredServer := registerServerAndAssert(t, testEnvironment, 23073, "Test Server")
getServersAndAssert(t, testEnvironment, []types.Server{registeredServer})
}
func TestRegisterNewServerTwice(t *testing.T) {
testEnvironment := setupRouter()
registerServerAndAssert(t, testEnvironment, 23073, "Test Server")
registeredServer := registerServerAndAssert(t, testEnvironment, 23073, "Test Server")
getServersAndAssert(t, testEnvironment, []types.Server{registeredServer})
}
func TestRegisterTwoNewServers(t *testing.T) {
testEnvironment := setupRouter()
registeredServer1 := registerServerAndAssert(t, testEnvironment, 23073, "Test Server")
registeredServer2 := registerServerAndAssert(t, testEnvironment, 23074, "Test Server 2")
getServersAndAssert(t, testEnvironment, []types.Server{registeredServer1, registeredServer2})
}
func TestRegisterTwoNewServersTwice(t *testing.T) {
testEnvironment := setupRouter()
registeredServer1 := registerServerAndAssert(t, testEnvironment, 23073, "Test Server")
registeredServer2 := registerServerAndAssert(t, testEnvironment, 23074, "Test Server 2")
registerServerAndAssert(t, testEnvironment, 23073, "Test Server")
registerServerAndAssert(t, testEnvironment, 23074, "Test Server 2")
getServersAndAssert(t, testEnvironment, []types.Server{registeredServer1, registeredServer2})
}
func TestRegisterServerAndThenUpdateIt(t *testing.T) {
testEnvironment := setupRouter()
registerServerInput := createRegisterServerInput(23073, "Test server")
registeredServer := registerServerInputAndAssert(t, testEnvironment, registerServerInput)
getServersAndAssert(t, testEnvironment, []types.Server{registeredServer})
advanced := !*registerServerInput.Advanced
registerServerInput.Advanced = &advanced
registeredServer = registerServerInputAndAssert(t, testEnvironment, registerServerInput)
getServersAndAssert(t, testEnvironment, []types.Server{registeredServer})
}
func TestFieldsWithDefaultValuesOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
advanced := new(bool)
antiCheatOn := new(bool)
bonusFrequency := new(uint16)
country := new(string)
info := new(string)
numBots := new(uint16)
private := new(bool)
realistic := new(bool)
respawn := new(uint32)
survival := new(bool)
wm := new(bool)
*advanced = false
*antiCheatOn = false
*bonusFrequency = 0
*country = ""
*info = ""
*numBots = 0
*private = false
*realistic = false
*respawn = 0
*survival = false
*wm = false
registerServerInput := types.RegisterServerInput{
Advanced: advanced,
AntiCheatOn: antiCheatOn,
BonusFrequency: bonusFrequency,
Country: country,
CurrentMap: "ctf_Ash",
GameStyle: "CTF",
Info: info,
MaxPlayers: 10,
Name: "Name",
NumBots: numBots,
OS: "Linux",
Players: []string{},
Port: 23073,
Private: private,
Realistic: realistic,
Respawn: respawn,
Survival: survival,
Version: "1.0",
WM: wm,
}
invalidInputJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusCreated, returnedCode)
}
func TestMissingFieldsOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
advanced := new(bool)
antiCheatOn := new(bool)
bonusFrequency := new(uint16)
info := new(string)
numBots := new(uint16)
realistic := new(bool)
respawn := new(uint32)
survival := new(bool)
wm := new(bool)
*advanced = true
*antiCheatOn = true
*bonusFrequency = 10
*info = "Test Server Info"
*numBots = 1
*realistic = true
*respawn = 1
*survival = true
*wm = true
newServerInputWithMissingFields := types.RegisterServerInput{
Advanced: advanced,
AntiCheatOn: antiCheatOn,
BonusFrequency: bonusFrequency,
CurrentMap: "ctf_Ash",
Info: info,
MaxPlayers: 32,
NumBots: numBots,
OS: "Linux",
Port: 23073,
Realistic: realistic,
Respawn: respawn,
Survival: survival,
Version: "1.0",
WM: wm,
}
invalidInputJson, _ := json.Marshal(newServerInputWithMissingFields)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}
func TestInvalidInputOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
invalidInput := InvalidInput{
SomeValue: "test",
}
invalidInputJson, _ := json.Marshal(invalidInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}
func TestTooLongCountryOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
registerServerInput := createRegisterServerInput(23073, "Test Name")
country := new(string)
*country = createLongString(types.MaxCountryLength + 1)
registerServerInput.Country = country
invalidInputJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}
func TestTooLongMapNameOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
registerServerInput := createRegisterServerInput(23073, "Test Name")
registerServerInput.CurrentMap = createLongString(types.MaxMapSize + 1)
invalidInputJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}
func TestTooLongGameStyleOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
registerServerInput := createRegisterServerInput(23073, "Test Name")
registerServerInput.GameStyle = createLongString(types.MaxGameStyleSize + 1)
invalidInputJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}
func TestTooLongInfoOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
registerServerInput := createRegisterServerInput(23073, "Test Name")
info := new(string)
*info = createLongString(types.MaxInfoSize + 1)
registerServerInput.Info = info
invalidInputJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}
func TestTooLongNameOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
registerServerInput := createRegisterServerInput(23073, createLongString(types.MaxNameSize+1))
invalidInputJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}
func TestTooLongOSOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
registerServerInput := createRegisterServerInput(23073, "Test Name")
registerServerInput.OS = createLongString(types.MaxOSSize + 1)
invalidInputJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}
func TestTooLongPlayerNameOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
registerServerInput := createRegisterServerInput(23073, "Test Name")
registerServerInput.Players[1] = createLongString(types.MaxPlayerNameSize + 1)
invalidInputJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}
func TestTooLongVersionOnRegisteringNewServer(t *testing.T) {
router := setupRouter()
registerServerInput := createRegisterServerInput(23073, "Test Name")
registerServerInput.Version = createLongString(types.MaxVersionSize + 1)
invalidInputJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(router, "/servers", invalidInputJson)
assert.Equal(t, http.StatusBadRequest, returnedCode)
}
func createLongString(size int) string {
var stringBuilder strings.Builder
stringBuilder.Grow(size)
for i := 0; i < size; i++ {
stringBuilder.WriteByte(0)
}
result := stringBuilder.String()
return result
}
func registerServerAndAssert(t *testing.T, env TestEnvironment, port uint16, name string) types.Server {
registerServerInput := createRegisterServerInput(port, name)
serverJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(env, "/servers", serverJson)
assert.Equal(t, http.StatusCreated, returnedCode)
registeredServer := types.ConvertRegisterServerInputToServer(registerServerInput)
return registeredServer
}
func registerServerInputAndAssert(t *testing.T, env TestEnvironment, registerServerInput types.RegisterServerInput) types.Server {
serverJson, _ := json.Marshal(registerServerInput)
returnedCode, _ := sendJsonToPostEndpoint(env, "/servers", serverJson)
assert.Equal(t, http.StatusCreated, returnedCode)
registeredServer := types.ConvertRegisterServerInputToServer(registerServerInput)
return registeredServer
}
func createRegisterServerInput(port uint16, name string) types.RegisterServerInput {
advanced := new(bool)
antiCheatOn := new(bool)
bonusFrequency := new(uint16)
country := new(string)
info := new(string)
numBots := new(uint16)
private := new(bool)
realistic := new(bool)
respawn := new(uint32)
survival := new(bool)
wm := new(bool)
*advanced = true
*antiCheatOn = true
*bonusFrequency = 10
*country = "PL"
*info = "Test Server Info"
*numBots = 1
*private = true
*realistic = true
*respawn = 1
*survival = true
*wm = true
registerServerInput := types.RegisterServerInput{
Advanced: advanced,
AntiCheatOn: antiCheatOn,
BonusFrequency: bonusFrequency,
Country: country,
CurrentMap: "ctf_Ash",
GameStyle: "CTF",
Info: info,
MaxPlayers: 32,
Name: name,
NumBots: numBots,
OS: "Linux",
Players: []string{"test_player_1", "another player"},
Port: port,
Private: private,
Realistic: realistic,
Respawn: respawn,
Survival: survival,
Version: "1.0",
WM: wm,
}
return registerServerInput
}
func getServersAndAssert(t *testing.T, env TestEnvironment, expected_servers []types.Server) {
code, body := getServers(env)
assert.Equal(t, http.StatusOK, code)
expected_servers_json, _ := json.Marshal(expected_servers)
assert.Equal(t, string(expected_servers_json), body)
}
func getServers(env TestEnvironment) (int, string) {
env.Context.Request, _ = http.NewRequest("GET", "/servers", nil)
httpRecorder := httptest.NewRecorder()
env.Router.ServeHTTP(httpRecorder, env.Context.Request)
return httpRecorder.Code, httpRecorder.Body.String()
}
func sendJsonToPostEndpoint(env TestEnvironment, endpoint string, dataJson []byte) (int, string) {
env.Context.Request, _ = http.NewRequest("POST", endpoint, strings.NewReader(string(dataJson)))
httpRecorder := httptest.NewRecorder()
env.Router.ServeHTTP(httpRecorder, env.Context.Request)
return httpRecorder.Code, httpRecorder.Body.String()
}