-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathexecutor_test.go
More file actions
751 lines (617 loc) · 25.6 KB
/
executor_test.go
File metadata and controls
751 lines (617 loc) · 25.6 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
package executor_test
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/gorcon/rcon"
"github.com/gorcon/rcon-cli/internal/config"
"github.com/gorcon/rcon-cli/internal/executor"
"github.com/gorcon/rcon/rcontest"
"github.com/gorcon/telnet"
"github.com/gorcon/telnet/telnettest"
"github.com/gorcon/websocket"
gorilla "github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
)
const ConfigLayoutJSON = `{"%s": {"address": "%s", "password": "%s", "log": "%s", "type": "%s"}}`
const ConfigLayoutYAML = "%s:\n address: %s\n password: %s\n log: %s\n type: %s"
func handlersRCON(c *rcontest.Context) {
switch c.Request().Body() {
case "help":
responseBody := "Can I help you?"
rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, responseBody).WriteTo(c.Conn())
default:
rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, "unknown command").WriteTo(c.Conn())
}
}
func handlersTELNET(c *telnettest.Context) {
switch c.Request() {
case "", "exit":
case "help":
c.Writer().WriteString(fmt.Sprintf("2020-11-14T23:09:20 31220.643 "+telnet.ResponseINFLayout, c.Request(), c.Conn().RemoteAddr()) + telnet.CRLF)
c.Writer().WriteString("Can I help you?" + telnet.CRLF)
default:
c.Writer().WriteString(fmt.Sprintf("*** ERROR: unknown command '%s'", c.Request()) + telnet.CRLF)
}
c.Writer().Flush()
}
const MockCommandStatusResponseTextWebRCON = `hostname: Rust Server [DOCKER]
version : 2260 secure (secure mode enabled, connected to Steam3)
map : Procedural Map
players : 0 (500 max) (0 queued) (0 joining)
id name ping connected addr owner violation kicks`
func handlersWebRCON() http.Handler {
server := http.NewServeMux()
var upgrader = gorilla.Upgrader{}
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
server.HandleFunc("/password", func(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("upgrade error: %v\n", err)
return
}
defer ws.Close()
var response websocket.Message
// Receive message.
_, p, err := ws.ReadMessage()
if err != nil {
if !strings.Contains(err.Error(), "websocket: close 1006 (abnormal closure): unexpected EOF") {
log.Printf("read message error: %v\n", err)
}
return
}
var message websocket.Message
if err := json.Unmarshal(p, &message); err != nil {
// TODO: What Rust responses on read message fail?
fmt.Println(string(p))
log.Printf("unmarshal message error: %v\n", err)
return
}
switch message.Message {
case "status":
response = websocket.Message{
Message: MockCommandStatusResponseTextWebRCON,
Identifier: message.Identifier,
Type: "Generic",
}
case "deadline":
time.Sleep(websocket.DefaultDeadline + 1*time.Second)
response = websocket.Message{
Message: fmt.Sprintf("sleep for %d secends", websocket.DefaultDeadline+1*time.Second),
Identifier: message.Identifier,
Type: "Generic",
}
default:
response = websocket.Message{
Message: fmt.Sprintf("Command '%s' not found", message.Message),
Identifier: message.Identifier,
Type: "Warning",
}
}
js, err := json.Marshal(response)
if err != nil {
log.Printf("marshal response error: %v\n", err)
return
}
if err := ws.WriteMessage(gorilla.TextMessage, js); err != nil {
log.Printf("write response error: %v\n", err)
return
}
})
return server
}
func TestExecute(t *testing.T) {
serverRCON := rcontest.NewServer(
rcontest.SetSettings(rcontest.Settings{Password: "password"}),
rcontest.SetCommandHandler(handlersRCON),
)
defer serverRCON.Close()
serverTELNET := telnettest.NewServer(
telnettest.SetSettings(telnettest.Settings{Password: "password"}),
telnettest.SetCommandHandler(handlersTELNET),
)
defer serverTELNET.Close()
serverWebRCON := httptest.NewServer(handlersWebRCON())
defer serverWebRCON.Close()
// Test empty address.
t.Run("empty address", func(t *testing.T) {
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: "", Password: "password"}, "help")
assert.Error(t, err)
})
// Test empty password.
t.Run("empty password", func(t *testing.T) {
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: serverRCON.Addr(), Password: ""}, "help")
assert.Error(t, err)
})
// Test wrong password.
t.Run("wrong password", func(t *testing.T) {
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: serverRCON.Addr(), Password: "wrong"}, "help")
assert.Error(t, err)
})
// Test empty command.
t.Run("empty command", func(t *testing.T) {
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: serverRCON.Addr(), Password: "password"}, "")
assert.Error(t, err)
})
// Test long command.
t.Run("long command", func(t *testing.T) {
w := bytes.Buffer{}
bigCommand := make([]byte, 1001)
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: serverRCON.Addr(), Password: "password"}, string(bigCommand))
assert.Error(t, err)
})
// Positive RCON test Execute func.
t.Run("no error rcon", func(t *testing.T) {
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: serverRCON.Addr(), Password: "password"}, "help", "unknown")
assert.NoError(t, err)
result := strings.TrimSuffix(w.String(), "\n")
assert.Equal(t, "Can I help you?\n"+executor.CommandsResponseSeparator+"\nunknown command", result)
})
// Positive TELNET test Execute func.
t.Run("no error telnet", func(t *testing.T) {
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: serverTELNET.Addr(), Password: "password", Type: config.ProtocolTELNET}, "help", "unknown")
assert.NoError(t, err)
result := strings.TrimSuffix(w.String(), "\n")
if !strings.Contains(result, "Can I help you?\n"+executor.CommandsResponseSeparator+"\n*** ERROR: unknown command 'unknown'") {
assert.Equal(t, "Can I help you?\n"+executor.CommandsResponseSeparator+"\n*** ERROR: unknown command 'unknown'", result)
}
})
// Positive WEB RCON test Execute func.
t.Run("no error web", func(t *testing.T) {
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: serverWebRCON.Listener.Addr().String(), Password: "password", Type: config.ProtocolWebRCON}, "status")
assert.NoError(t, err)
result := strings.TrimSuffix(w.String(), "\n")
assert.Equal(t, MockCommandStatusResponseTextWebRCON, result)
})
// Positive test Execute func with log.
t.Run("no error with log", func(t *testing.T) {
w := bytes.Buffer{}
logFileName := "tmpfile.log"
defer os.Remove(logFileName)
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: serverRCON.Addr(), Password: "password", Log: logFileName}, "help")
assert.NoError(t, err)
})
if run := getVar("TEST_PZ_SERVER", "false"); run == "true" {
addr := getVar("TEST_PZ_SERVER_ADDR", "127.0.0.1:16260")
password := getVar("TEST_PZ_SERVER_PASSWORD", "docker")
t.Run("pz server", func(t *testing.T) {
needle := func() string {
n := `List of server commands :
* addalltowhitelist : Add all the current users connected with a password in the whitelist, so their account is protected.
* additem : Add an item to a player, if no username is given the item will be added to you, count is optional, use /additem \"username\" \"module.item\" count, ex : /additem \"rj\" \"Base.Axe\" count
* adduser : Use this command to add a new user in a whitelisted server, use : /adduser \"username\" \"pwd\"
* addusertowhitelist : Add the user connected with a password in the whitelist, so his account is protected, use : /addusertowhitelist \"username\"
* addvehicle : Spawn a new vehicle, use: /addvehicle \"script\" \"user or x,y,z\", ex /addvehicle \"Base.VanAmbulance\" \"rj\"
* addxp : Add experience points to a player, use : /addxp \"playername\" perkname=xp, ex /addxp \"rj\" Woodwork=2
* alarm : Sound a building alarm at the admin's position. Must be in a room.
* banid : Ban a SteamID, use : /banid SteamID
* banuser : Ban a user, add a -ip to also ban his ip, add a -r \"reason\" to specify a reason for the ban, use : /banuser \"username\" -ip -r \"reason\", ex /banuser \"rj\" -ip -r \"spawn kill\"
* changeoption : Use this to change a server option, use : /changeoption optionName \"newValue\"
* chopper : Start the choppers (do noise on a random player)
* createhorde : Use this to spawn a horde near a player, use : /createhorde count \"username\", ex /createhorde 150 \"rj\", username is optional except from the server console.
* godmod : Set a player invincible, if no username set it make you invincible, if no value it toggle it, use : /godmode \"username\" -value, ex /godmode \"rj\" -true (could be -false)
* gunshot : Start a gunshot (do noise on a random player)
* help : Help
* invisible : Set a player invisible zombie will ignore him, if no username set it make you invisible, if no value it toggle it, use : /invisible \"username\" -value, ex /invisible \"rj\" -true (could be -false)
* kickuser : Kick a user, add a -r \"reason\" to specify a reason for the kick, use : /kickuser \"username\" -r \"reason\"
* noclip : A player with noclip won't collide on anything, if no value it toggle it, use : /noclip \"username\" -value, ex /noclip \"rj\" -true (could be -false)
* players : List the players connected
* quit : Quit the server (but save it before)
* releasesafehouse : Release a safehouse you are the owner of, use : /releasesafehouse
* reloadlua : Reload a Lua script, use : /reloadlua \"filename\"
* reloadoptions : Reload the options on the server (ServerOptions.ini) and send them to the clients
* removeuserfromwhitelist : Remove the user from the whitelist, use: /removeuserfromwhitelist \"username\"
* save : Save the current world
* sendpulse : Toggle sending server performance info to this client, use : /sendpulse
* servermsg : Use this to broadcast a message to all connected players, use : /servermsg my message !
* setaccesslevel : Use it to set new access level to a player, acces level: admin, moderator, overseer, gm, observer. use : /setaccesslevel \"username\" \"accesslevel\", ex: /setaccesslevel \"rj\" \"moderator\"
* showoptions : Show the list of current Server options with their values.
* startrain : Start rain on the server
* stoprain : Stop rain on the server
* teleport : Teleport to a player, once teleported, wait 2 seconds to show map, use : /teleport \"playername\" or /teleport \"player1\" \"player2\", ex /teleport \"rj\" or /teleport \"rj\" \"toUser\"
* teleportto : Teleport to coordinates, use: /teleportto x,y,z, ex /teleportto 100098,189980,0
* unbanid : Unban a SteamID, use : /unbanid SteamID
* unbanuser : Unban a player, use : /unbanuser \"username\"
* voiceban : Block voice from user \"username\", use : /voiceban \"username\" -value, ex /voiceban \"rj\" -true (could be -false)`
n = strings.Replace(n, "List of server commands :", "List of server commands : ", -1)
return n
}()
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: addr, Password: password}, "help")
assert.NoError(t, err)
result := strings.TrimSuffix(w.String(), "\n")
assert.Equal(t, needle, result)
})
}
if run := getVar("TEST_7DTD_SERVER", "false"); run == "true" {
addr := getVar("TEST_7DTD_SERVER_ADDR", "172.22.0.2:8081")
password := getVar("TEST_7DTD_SERVER_PASSWORD", "banana")
t.Run("7dtd server", func(t *testing.T) {
needle := func() string {
n := `*** Generic Console Help ***
To get further help on a specific topic or command type (without the brackets)
help <topic / command>
Generic notation of command parameters:
<param name> Required parameter
<entityId / player name> Possible types of parameter values
[param name] Optional parameter
*** List of Help Topics ***
None yet
*** List of Commands ***
admin => Manage user permission levels
aiddebug => Toggles AIDirector debug output.
audio => Watch audio stats
automove => Player auto movement
ban => Manage ban entries
bents => Switches block entities on/off
BiomeParticles => Debug
buff => Applies a buff to the local player
buffplayer => Apply a buff to a player
chunkcache cc => shows all loaded chunks in cache
chunkobserver co => Place a chunk observer on a given position.
chunkreset cr => resets the specified chunks
commandpermission cp => Manage command permission levels
creativemenu cm => enables/disables the creativemenu
debuff => Removes a buff from the local player
debuffplayer => Remove a buff from a player
debugmenu dm => enables/disables the debugmenu ` + `
debugshot dbs => Lets you make a screenshot that will have some generic info
on it and a custom text you can enter. Also stores a list
of your current perk levels in a CSV file next to it.
debugweather => Dumps internal weather state to the console.
decomgr => ` + `
dms => Gives control over Dynamic Music functionality.
dof => Control DOF
enablescope es => toggle debug scope
exhausted => Makes the player exhausted.
exportcurrentconfigs => Exports the current game config XMLs
exportprefab => Exports a prefab from a world area
floatingorigin fo => ` + `
fov => Camera field of view
gamestage => usage: gamestage - displays the gamestage of the local player.
getgamepref gg => Gets game preferences
getgamestat ggs => Gets game stats
getoptions => Gets game options
gettime gt => Get the current game time
gfx => Graphics commands
givequest => usage: givequest questname
giveself => usage: giveself itemName [qualityLevel=6] [count=1] [putInInventory=false] [spawnWithMods=true]
giveselfxp => usage: giveselfxp 10000
help => Help on console and specific commands
kick => Kicks user with optional reason. "kick playername reason"
kickall => Kicks all users with optional reason. "kickall reason"
kill => Kill a given entity
killall => Kill all entities
lgo listgameobjects => List all active game objects
lights => Debug views to optimize lights
listents le => lists all entities
listplayerids lpi => Lists all players with their IDs for ingame commands
listplayers lp => lists all players
listthreads lt => lists all threads
loggamestate lgs => Log the current state of the game
loglevel => Telnet/Web only: Select which types of log messages are shown
mem => Prints memory information and unloads resources or changes garbage collector
memcl => Prints memory information on client and calls garbage collector
occlusion => Control OcclusionManager
pirs => tbd
pois => Switches distant POIs on/off
pplist => Lists all PersistentPlayer data
prefab => ` + `
prefabupdater => ` + `
profilenetwork => Writes network profiling information
profiling => Enable Unity profiling for 300 frames
removequest => usage: removequest questname
repairchunkdensity rcd => check and optionally fix densities of a chunk
saveworld sa => Saves the world manually.
say => Sends a message to all connected clients
ScreenEffect => Sets a screen effect
setgamepref sg => sets a game pref
setgamestat sgs => sets a game stat
settargetfps => Set the target FPS the game should run at (upper limit)
settempunit stu => Set the current temperature units.
settime st => Set the current game time
show => Shows custom layers of rendering.
showalbedo albedo => enables/disables display of albedo in gBuffer
showchunkdata sc => shows some date of the current chunk
showClouds => Artist command to show one layer of clouds.
showhits => Show hit entity locations
shownexthordetime => Displays the wandering horde time
shownormals norms => enables/disables display of normal maps in gBuffer
showspecular spec => enables/disables display of specular values in gBuffer
showswings => Show melee swing arc rays
shutdown => shuts down the game
sleeper => Show sleeper info
smoothworldall swa => Applies some batched smoothing commands.
sounddebug => Toggles SoundManager debug output.
spawnairdrop => Spawns an air drop
spawnentity se => spawns an entity
spawnentityat sea => Spawns an entity at a give position
spawnscouts => Spawns zombie scouts
SpawnScreen => Display SpawnScreen
spawnsupplycrate => Spawns a supply crate where the player is
spawnwanderinghorde spawnwh => Spawns a wandering horde of zombies
spectator spectatormode sm => enables/disables spectator mode
spectrum => Force a particular lighting spectrum.
stab => stability
starve hungry food => Makes the player starve (optionally specify the amount of food you want to have in percent).
switchview sv => Switch between fpv and tpv
SystemInfo => List SystemInfo
teleport tp => Teleport the local player
teleportplayer tele => Teleport a given player
thirsty water => Makes the player thirsty (optionally specify the amount of water you want to have in percent).
traderarea => ...
trees => Switches trees on/off
updatelighton => Commands for UpdateLightOnAllMaterials and UpdateLightOnPlayers
version => Get the currently running version of the game and loaded mods
visitmap => Visit an given area of the map. Optionally run the density check on each visited chunk.
water => Control water settings
weather => Control weather settings
weathersurvival => Enables/disables weather survival
whitelist => Manage whitelist entries
wsmats workstationmaterials => Set material counts on workstations.
xui => Execute XUi operations
xuireload => Access xui related functions such as reinitializing a window group, opening a window group
zip => Control zipline settings`
n = strings.Replace(n, "\n", "\r\n", -1)
n = strings.Replace(n, "some generic info\r\n", "some generic info\n", -1)
n = strings.Replace(n, "Also stores a list\r\n", "Also stores a list\n", -1)
return n
}()
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: addr, Password: password, Type: config.ProtocolTELNET}, "help")
assert.NoError(t, err)
result := strings.TrimSuffix(w.String(), "\n")
if !strings.Contains(w.String(), needle) {
assert.Equal(t, needle, result)
}
})
}
if run := getVar("TEST_RUST_SERVER_RCON", "false"); run == "true" {
addr := getVar("TEST_RUST_SERVER_RCON_ADDR", "127.0.0.1:28016")
password := getVar("TEST_RUST_SERVER_RCON_PASSWORD", "docker")
t.Run("rust server rcon", func(t *testing.T) {
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: addr, Password: password}, "status")
assert.NoError(t, err)
assert.NotEmpty(t, w.String())
fmt.Println(w.String())
})
}
if run := getVar("TEST_RUST_SERVER_WEB", "false"); run == "true" {
addr := getVar("TEST_RUST_SERVER_WEB_ADDR", "127.0.0.1:28016")
password := getVar("TEST_RUST_SERVER_WEB_PASSWORD", "docker")
t.Run("rust server web", func(t *testing.T) {
w := bytes.Buffer{}
app := executor.NewExecutor(nil, &w, "")
defer app.Close()
err := app.Execute(&w, &config.Session{Address: addr, Password: password, Type: config.ProtocolWebRCON}, "status")
assert.NoError(t, err)
assert.NotEmpty(t, w.String())
fmt.Println(w.String())
})
}
}
func TestInteractive(t *testing.T) {
serverRCON := rcontest.NewServer(
rcontest.SetSettings(rcontest.Settings{Password: "password"}),
rcontest.SetCommandHandler(handlersRCON),
)
defer serverRCON.Close()
serverTELNET := telnettest.NewServer(
telnettest.SetSettings(telnettest.Settings{Password: "password"}),
telnettest.SetCommandHandler(handlersTELNET),
)
defer serverTELNET.Close()
serverWebRCON := httptest.NewServer(handlersWebRCON())
defer serverWebRCON.Close()
// TODO: Add results check.
// Test wrong password.
t.Run("wrong password", func(t *testing.T) {
var r bytes.Buffer
r.WriteString("\n")
r.WriteString(executor.CommandQuit + "\n")
w := bytes.Buffer{}
app := executor.NewExecutor(&r, &w, "")
defer app.Close()
err := app.Interactive(&r, &w, &config.Session{Address: serverRCON.Addr(), Password: "fake"})
assert.Error(t, err)
})
// Test get Interactive commands RCON.
t.Run("get commands rcon", func(t *testing.T) {
r := bytes.Buffer{}
r.WriteString(serverRCON.Addr() + "\n")
r.WriteString("password" + "\n")
r.WriteString(config.ProtocolRCON + "\n")
r.WriteString("help" + "\n")
r.WriteString("unknown command" + "\n")
r.WriteString(executor.CommandQuit + "\n")
w := bytes.Buffer{}
app := executor.NewExecutor(&r, &w, "")
defer app.Close()
err := app.Interactive(&r, &w, &config.Session{})
assert.NoError(t, err)
})
// Test get Interactive commands TELNET.
t.Run("get commands telnet", func(t *testing.T) {
r := bytes.Buffer{}
r.WriteString(serverTELNET.Addr() + "\n")
r.WriteString("password" + "\n")
r.WriteString(config.ProtocolTELNET + "\n")
r.WriteString("help" + "\n")
r.WriteString("unknown command" + "\n")
r.WriteString(executor.CommandQuit + "\n")
w := bytes.Buffer{}
app := executor.NewExecutor(&r, &w, "")
defer app.Close()
err := app.Interactive(&r, &w, &config.Session{})
assert.NoError(t, err)
})
// Test get Interactive commands WEB RCON.
t.Run("get commands web", func(t *testing.T) {
r := bytes.Buffer{}
r.WriteString(serverWebRCON.Listener.Addr().String() + "\n")
r.WriteString("password" + "\n")
r.WriteString(config.ProtocolWebRCON + "\n")
r.WriteString("status" + "\n")
r.WriteString("unknown command" + "\n")
r.WriteString(executor.CommandQuit + "\n")
w := bytes.Buffer{}
app := executor.NewExecutor(&r, &w, "")
defer app.Close()
err := app.Interactive(&r, &w, &config.Session{})
assert.NoError(t, err)
})
}
func TestNewExecutor(t *testing.T) {
serverRCON := rcontest.NewServer(
rcontest.SetSettings(rcontest.Settings{Password: "password"}),
rcontest.SetCommandHandler(handlersRCON),
)
defer serverRCON.Close()
// Test getting address and password from args. Config ang log are not used.
t.Run("getting address and password from args", func(t *testing.T) {
r := &bytes.Buffer{}
w := &bytes.Buffer{}
app := executor.NewExecutor(r, w, "")
defer app.Close()
args := os.Args[0:1]
args = append(args, "-a="+serverRCON.Addr())
args = append(args, "-p="+"password")
args = append(args, "help")
err := app.Run(args)
assert.NoError(t, err)
})
// Test getting address and password from config. Log is not used.
t.Run("getting address and password from args with log", func(t *testing.T) {
configFileName := "rcon-test-local.yaml"
logFileName := "rcon-test.log"
stringBody := fmt.Sprintf(ConfigLayoutYAML, config.DefaultConfigEnv, serverRCON.Addr(), "password", logFileName, "")
createFile(configFileName, stringBody)
defer func() {
os.Remove(logFileName)
os.Remove(configFileName)
}()
r := &bytes.Buffer{}
w := &bytes.Buffer{}
app := executor.NewExecutor(r, w, "")
defer app.Close()
args := os.Args[0:1]
args = append(args, "-c="+configFileName)
args = append(args, "help")
err := app.Run(args)
assert.NoError(t, err)
})
// Test empty address and password. Log is not used.
t.Run("empty address and password", func(t *testing.T) {
configFileName := "rcon-test-local.yaml"
logFileName := "rcon-test.log"
stringBody := fmt.Sprintf(ConfigLayoutYAML, config.DefaultConfigEnv, "", "", logFileName, "")
createFile(configFileName, stringBody)
defer func() {
os.Remove(logFileName)
os.Remove(configFileName)
}()
r := &bytes.Buffer{}
w := &bytes.Buffer{}
app := executor.NewExecutor(r, w, "")
defer app.Close()
args := os.Args[0:1]
// Hack to use os.Args[0] in go run
args[0] = ""
args = append(args, "-c="+configFileName)
args = append(args, "help")
err := app.Run(args)
assert.EqualError(t, err, "cli: address is not set: to set address add -a host:port")
})
// Test empty password. Log is not used.
t.Run("empty password", func(t *testing.T) {
configFileName := "rcon-test-local.yaml"
logFileName := "rcon-test.log"
stringBody := fmt.Sprintf(ConfigLayoutYAML, config.DefaultConfigEnv, serverRCON.Addr(), "", logFileName, "")
createFile(configFileName, stringBody)
defer func() {
os.Remove(logFileName)
os.Remove(configFileName)
}()
r := &bytes.Buffer{}
w := &bytes.Buffer{}
app := executor.NewExecutor(r, w, "")
defer app.Close()
args := os.Args[0:1]
// Hack to use os.Args[0] in go run
args[0] = ""
args = append(args, "-a="+serverRCON.Addr())
args = append(args, "-c="+configFileName)
args = append(args, "help")
err := app.Run(args)
assert.EqualError(t, err, "cli: password is not set: to set password add -p password")
})
// Positive test Interactive. Log is not used.
t.Run("no error", func(t *testing.T) {
r := &bytes.Buffer{}
w := &bytes.Buffer{}
app := executor.NewExecutor(r, w, "")
defer app.Close()
args := os.Args[0:1]
args = append(args, "-a="+serverRCON.Addr())
args = append(args, "-p="+"password")
r.WriteString("\n")
r.WriteString("help" + "\n")
r.WriteString(executor.CommandQuit + "\n")
err := app.Run(args)
assert.NoError(t, err)
})
}
// getVar returns environment variable or default value.
func getVar(key string, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
func createFile(name, stringBody string) error {
file, err := os.Create(name)
if err != nil {
return err
}
_, err = file.WriteString(stringBody)
return err
}