-
Notifications
You must be signed in to change notification settings - Fork 3
/
telnet_test.go
480 lines (414 loc) · 15.8 KB
/
telnet_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
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
package telnet_test
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/gorcon/telnet"
"github.com/gorcon/telnet/telnettest"
)
func authHandler(c *telnettest.Context) {
switch c.Request() {
case c.Server().Settings.Password:
c.Writer().WriteString(telnet.ResponseAuthSuccess + telnet.CRLF + telnet.CRLF + telnet.CRLF + telnet.CRLF)
c.Writer().WriteString(telnettest.AuthSuccessWelcomeMessage + telnet.CRLF + telnet.CRLF)
c.Auth.Success = true
c.Auth.Break = true
case "unexpect":
c.Writer().WriteString("My spoon is too big" + telnet.CRLF + telnet.CRLF)
c.Auth.Success = false
c.Auth.Break = true
default:
c.Writer().WriteString(telnet.ResponseAuthIncorrectPassword + telnet.CRLF)
}
}
func commandHandler(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("lorem ipsum dolor sit amet" + telnet.CRLF)
default:
c.Writer().WriteString(fmt.Sprintf("*** ERROR: unknown command '%s'", c.Request()) + telnet.CRLF)
}
c.Writer().Flush()
}
func TestDial(t *testing.T) {
server := telnettest.NewServer(
telnettest.SetSettings(telnettest.Settings{Password: "password"}),
telnettest.SetAuthHandler(authHandler),
)
defer server.Close()
t.Run("connection refused", func(t *testing.T) {
wantErrContains := "connect: connection refused"
_, err := telnet.Dial("127.0.0.2:12345", "password")
if err == nil || !strings.Contains(err.Error(), wantErrContains) {
t.Errorf("got err %q, want to contain %q", err, wantErrContains)
}
})
t.Run("incorrect password", func(t *testing.T) {
_, err := telnet.Dial(server.Addr(), string(make([]byte, 1001)))
if !errors.Is(err, telnet.ErrCommandTooLong) {
t.Errorf("got err %q, want %q", err, telnet.ErrCommandTooLong)
}
})
t.Run("authentication failed", func(t *testing.T) {
_, err := telnet.Dial(server.Addr(), "wrong")
if !errors.Is(err, telnet.ErrAuthFailed) {
t.Errorf("got err %q, want %q", err, telnet.ErrAuthFailed)
}
})
t.Run("unexpected auth response", func(t *testing.T) {
_, err := telnet.Dial(server.Addr(), "unexpect")
if !errors.Is(err, telnet.ErrAuthUnexpectedMessage) {
t.Errorf("got err %q, want %q", err, telnet.ErrAuthUnexpectedMessage)
}
})
t.Run("auth success", func(t *testing.T) {
conn, err := telnet.Dial(server.Addr(), "password", telnet.SetDialTimeout(5*time.Second))
if err != nil {
t.Errorf("got err %q, want %v", err, nil)
return
}
defer conn.Close()
if conn.Status() != telnettest.AuthSuccessWelcomeMessage {
t.Fatalf("got result %q, want %q", conn.Status(), telnettest.AuthSuccessWelcomeMessage)
}
})
}
func TestConn_Execute(t *testing.T) {
server := telnettest.NewServer(
telnettest.SetSettings(telnettest.Settings{Password: "password"}),
telnettest.SetAuthHandler(authHandler),
telnettest.SetCommandHandler(commandHandler),
)
defer server.Close()
t.Run("incorrect command", func(t *testing.T) {
conn, err := telnet.Dial(server.Addr(), "password")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
defer conn.Close()
result, err := conn.Execute("")
if !errors.Is(err, telnet.ErrCommandEmpty) {
t.Errorf("got err %q, want %q", err, telnet.ErrCommandEmpty)
}
if len(result) != 0 {
t.Fatalf("got result len %d, want %d", len(result), 0)
}
result, err = conn.Execute(string(make([]byte, 1001)))
if !errors.Is(err, telnet.ErrCommandTooLong) {
t.Errorf("got err %q, want %q", err, telnet.ErrCommandTooLong)
}
if len(result) != 0 {
t.Fatalf("got result len %d, want %d", len(result), 0)
}
})
t.Run("closed network connection", func(t *testing.T) {
conn, err := telnet.Dial(server.Addr(), "password")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
conn.Close()
result, err := conn.Execute("help")
wantErrMsg := fmt.Sprintf("write tcp %s->%s: use of closed network connection", conn.LocalAddr(), conn.RemoteAddr())
if err == nil || err.Error() != wantErrMsg {
t.Errorf("got err %q, want to contain %q", err, wantErrMsg)
}
if len(result) != 0 {
t.Fatalf("got result len %d, want %d", len(result), 0)
}
})
t.Run("unknown command", func(t *testing.T) {
conn, err := telnet.Dial(server.Addr(), "password")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
defer conn.Close()
result, err := conn.Execute("random")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
resultWant := "*** ERROR: unknown command 'random'"
if result != resultWant {
t.Fatalf("got result %q, want %q", result, resultWant)
}
})
t.Run("success help command", func(t *testing.T) {
conn, err := telnet.Dial(server.Addr(), "password", telnet.SetClearResponse(true))
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
defer conn.Close()
result, err := conn.Execute("help")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
resultWant := "lorem ipsum dolor sit amet"
if result != resultWant {
t.Fatalf("got result %q, want %q", result, resultWant)
}
})
t.Run("multiple commands", func(t *testing.T) {
conn, err := telnet.Dial(server.Addr(), "password", telnet.SetClearResponse(true))
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
defer conn.Close()
// Command 1
result, err := conn.Execute("help")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
resultWant := "lorem ipsum dolor sit amet"
if result != resultWant {
t.Fatalf("got result %q, want %q", result, resultWant)
}
// Command 2
result, err = conn.Execute("random")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
resultWant = "*** ERROR: unknown command 'random'"
if result != resultWant {
t.Fatalf("got result %q, want %q", result, resultWant)
}
// Command 3
result, err = conn.Execute("help")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
resultWant = "lorem ipsum dolor sit amet"
if result != resultWant {
t.Fatalf("got result %q, want %q", result, resultWant)
}
})
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) {
conn, err := telnet.Dial(addr, password, telnet.SetClearResponse(true))
if err != nil {
t.Fatal(err)
}
defer conn.Close()
// Command 1
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
}()
result, err := conn.Execute("help")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
if result != needle {
t.Fatalf("got result %q, want %q", result, needle)
}
// Command 2
needle = "*** ERROR: unknown command 'status'"
result, err = conn.Execute("status")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
if result != needle {
t.Fatalf("got result %q, want %q", result, needle)
}
// Command 3
needle = "INF Chat (from '-non-player-', entity id '-1', to 'Global'): 'Server': 10"
result, err = conn.Execute("say 10")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
if !strings.Contains(needle, needle) {
t.Fatalf("got result %q, want to contain %q", result, needle)
}
})
}
}
func TestConn_Interactive(t *testing.T) {
server := telnettest.NewUnstartedServer()
server.Settings.Password = "password"
server.SetAuthHandler(authHandler)
server.SetCommandHandler(commandHandler)
server.Start()
defer server.Close()
t.Run("connection refused", func(t *testing.T) {
wantErrContains := "connect: connection refused"
r, w := bytes.Buffer{}, bytes.Buffer{}
err := telnet.DialInteractive(&r, &w, "127.0.0.2:12345", "password")
if err == nil || !strings.Contains(err.Error(), wantErrContains) {
t.Errorf("got err %q, want to contain %q", err, wantErrContains)
}
})
t.Run("unknown command", func(t *testing.T) {
needle := telnet.ResponseEnterPassword + telnet.CRLF +
telnet.ResponseAuthSuccess + telnet.CRLF + telnet.CRLF + telnet.CRLF + telnet.CRLF +
telnettest.AuthSuccessWelcomeMessage + telnet.CRLF + telnet.CRLF +
"*** ERROR: unknown command 'random'" + telnet.CRLF
r, w := bytes.Buffer{}, bytes.Buffer{}
r.WriteString("random" + "\n")
r.WriteString(telnet.ForcedExitCommand + "\n")
err := telnet.DialInteractive(&r, &w, server.Addr(), "password")
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
if w.String() != needle {
t.Fatalf("got result %q, want %q", w.String(), needle)
}
})
t.Run("success help command", func(t *testing.T) {
// TODO: server.Addr() in needle must be client address.
// This is impossible to check in current TELNET implementation.
needle := telnet.ResponseEnterPassword + telnet.CRLF +
telnet.ResponseAuthSuccess + telnet.CRLF + telnet.CRLF + telnet.CRLF + telnet.CRLF +
telnettest.AuthSuccessWelcomeMessage + telnet.CRLF + telnet.CRLF +
fmt.Sprintf("2020-11-14T23:09:20 31220.643 "+telnet.ResponseINFLayout, "help", server.Addr()) + telnet.CRLF +
"lorem ipsum dolor sit amet" + telnet.CRLF
r, w := bytes.Buffer{}, bytes.Buffer{}
r.WriteString("help" + "\n")
r.WriteString(telnet.ForcedExitCommand + "\n")
err := telnet.DialInteractive(&r, &w, server.Addr(), "password", telnet.SetExitCommand("exit"))
if err != nil {
t.Fatalf("got err %q, want %v", err, nil)
}
if !strings.Contains(w.String(), "help") {
t.Fatalf("got result %q, want to contain %q", w.String(), needle)
}
})
}
// getVar returns environment variable or default value.
func getVar(key string, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}