forked from fclairamb/ftpserverlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_dirs_test.go
483 lines (369 loc) · 11.9 KB
/
handle_dirs_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
481
482
483
package ftpserver
import (
"crypto/tls"
"fmt"
"net"
"os"
"path"
"testing"
"time"
"github.com/secsy/goftp"
"github.com/stretchr/testify/require"
)
const DirKnown = "known"
func TestMain(m *testing.M) {
loc, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Printf("unable to set timezone: %v\n", err)
os.Exit(1)
}
time.Local = loc
os.Exit(m.Run())
}
func TestDirListing(t *testing.T) {
// MLSD is disabled we relies on LIST of files listing
s := NewTestServerWithDriver(t, &TestServerDriver{Debug: true, Settings: &Settings{DisableMLSD: true}})
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
dirName, err := c.Mkdir(DirKnown)
require.NoError(t, err, "Couldn't create dir")
require.Equal(t, path.Join("/", DirKnown), dirName)
contents, err := c.ReadDir("/")
require.NoError(t, err)
require.Len(t, contents, 1)
require.Equal(t, DirKnown, contents[0].Name())
}
func TestDirListingPathArg(t *testing.T) {
// MLSD is disabled we relies on LIST of files listing
s := NewTestServerWithDriver(t, &TestServerDriver{Debug: true, Settings: &Settings{DisableMLSD: true}})
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
for _, dir := range []string{"/" + DirKnown, "/" + DirKnown + "/1"} {
_, err = c.Mkdir(dir)
require.NoError(t, err, "Couldn't create dir")
}
contents, err := c.ReadDir(DirKnown)
require.NoError(t, err)
require.Len(t, contents, 1)
require.Equal(t, "1", contents[0].Name())
contents, err = c.ReadDir("")
require.NoError(t, err)
require.Len(t, contents, 1)
require.Equal(t, DirKnown, contents[0].Name())
}
func TestDirHandling(t *testing.T) {
s := NewTestServer(t, true)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
// Getwd will send a PWD command
p, err := c.Getwd()
require.NoError(t, err)
require.Equal(t, "/", p, "Bad path")
raw, err := c.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
rc, _, err := raw.SendCommand("CWD /unknown")
require.NoError(t, err)
require.Equal(t, StatusActionNotTaken, rc)
_, err = c.Mkdir("/" + DirKnown)
require.NoError(t, err)
contents, err := c.ReadDir("/")
require.NoError(t, err)
require.Len(t, contents, 1)
rc, _, err = raw.SendCommand("CWD /" + DirKnown)
require.NoError(t, err)
require.Equal(t, StatusFileOK, rc)
testSubdir := " strange\\ sub dìr"
rc, _, err = raw.SendCommand(fmt.Sprintf("MKD %v", testSubdir))
require.NoError(t, err)
require.Equal(t, StatusPathCreated, rc)
rc, response, err := raw.SendCommand(fmt.Sprintf("CWD %v", testSubdir))
require.NoError(t, err)
require.Equal(t, StatusFileOK, rc, response)
rc, response, err = raw.SendCommand("CDUP")
require.NoError(t, err)
require.Equal(t, StatusFileOK, rc, response)
err = c.Rmdir(path.Join("/", DirKnown, testSubdir))
require.NoError(t, err)
err = c.Rmdir(path.Join("/", DirKnown))
require.NoError(t, err)
err = c.Rmdir("/" + DirKnown)
require.Error(t, err, "We shouldn't have been able to ftpDelete known again")
}
func TestCWDToRegularFile(t *testing.T) {
s := NewTestServer(t, true)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
// Getwd will send a PWD command
p, err := c.Getwd()
require.NoError(t, err)
require.Equal(t, "/", p, "Bad path")
raw, err := c.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
// Creating a tiny file
ftpUpload(t, c, createTemporaryFile(t, 10), "file.txt")
rc, msg, err := raw.SendCommand("CWD /file.txt")
require.NoError(t, err)
require.Equal(t, `Can't change directory to /file.txt: Not a Directory`, msg)
require.Equal(t, StatusActionNotTaken, rc, "We shouldn't have been able to CWD to a regular file")
}
func TestMkdirRmDir(t *testing.T) {
s := NewTestServer(t, true)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
raw, err := c.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
t.Run("standard", func(t *testing.T) {
rc, _, err := raw.SendCommand("SITE MKDIR /dir1/dir2/dir3")
require.NoError(t, err)
require.Equal(t, StatusFileOK, rc)
for _, d := range []string{"/dir1", "/dir1/dir2", "/dir1/dir2/dir3"} {
stat, errStat := c.Stat(d)
require.NoError(t, errStat)
require.True(t, stat.IsDir())
}
rc, _, err = raw.SendCommand("SITE RMDIR /dir1")
require.NoError(t, err)
require.Equal(t, StatusFileOK, rc)
for _, d := range []string{"/dir1", "/dir1/dir2", "/dir1/dir2/dir3"} {
stat, errStat := c.Stat(d)
require.Error(t, errStat)
require.Nil(t, stat)
}
_, err = c.Mkdir("/missing/path")
require.Error(t, err)
})
t.Run("syntax error", func(t *testing.T) {
rc, _, err := raw.SendCommand("SITE MKDIR")
require.NoError(t, err)
require.Equal(t, StatusSyntaxErrorNotRecognised, rc)
rc, _, err = raw.SendCommand("SITE RMDIR")
require.NoError(t, err)
require.Equal(t, StatusSyntaxErrorNotRecognised, rc)
})
t.Run("spaces", func(t *testing.T) {
rc, _, err := raw.SendCommand("SITE MKDIR /dir1 /dir2")
require.NoError(t, err)
require.Equal(t, StatusFileOK, rc)
{
dir := "/dir1 /dir2"
stat, errStat := c.Stat(dir)
require.NoError(t, errStat)
require.True(t, stat.IsDir())
}
rc, _, err = raw.SendCommand("SITE RMDIR /dir1 /dir2")
require.NoError(t, err)
require.Equal(t, StatusFileOK, rc)
})
}
// TestDirListingWithSpace uses the MLSD for files listing
func TestDirListingWithSpace(t *testing.T) {
s := NewTestServer(t, true)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
dirName := " with spaces "
_, err = c.Mkdir(dirName)
require.NoError(t, err, "Couldn't create dir")
contents, err := c.ReadDir("/")
require.NoError(t, err)
require.Len(t, contents, 1)
require.Equal(t, dirName, contents[0].Name())
raw, err := c.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
rc, response, err := raw.SendCommand(fmt.Sprintf("CWD /%s", dirName))
require.NoError(t, err)
require.Equal(t, StatusFileOK, rc)
require.Equal(t, fmt.Sprintf("CD worked on /%s", dirName), response)
_, err = raw.PrepareDataConn()
require.NoError(t, err)
rc, response, err = raw.SendCommand("NLST /")
require.NoError(t, err)
require.Equal(t, StatusFileStatusOK, rc, response)
rc, _, err = raw.ReadResponse()
require.NoError(t, err)
require.Equal(t, StatusClosingDataConn, rc)
_, err = raw.PrepareDataConn()
require.NoError(t, err)
rc, response, err = raw.SendCommand("NLST /missingpath")
require.NoError(t, err)
require.Equal(t, StatusFileActionNotTaken, rc, response)
}
func TestCleanPath(t *testing.T) {
s := NewTestServer(t, true)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
raw, err := c.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
// various path purity tests
for _, dir := range []string{
"..",
"../..",
"/../..",
"////",
"/./",
"/././.",
} {
rc, response, err := raw.SendCommand(fmt.Sprintf("CWD %s", dir))
require.NoError(t, err)
require.Equal(t, StatusFileOK, rc)
require.Equal(t, "CD worked on /", response)
p, err := c.Getwd()
require.NoError(t, err)
require.Equal(t, "/", p)
}
}
func TestTLSTransfer(t *testing.T) {
s := NewTestServerWithDriver(t, &TestServerDriver{
Debug: true,
TLS: true,
})
s.settings.TLSRequired = MandatoryEncryption
conf := goftp.Config{
User: authUser,
Password: authPass,
TLSConfig: &tls.Config{
// nolint:gosec
InsecureSkipVerify: true,
},
TLSMode: goftp.TLSExplicit,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
contents, err := c.ReadDir("/")
require.NoError(t, err)
require.Len(t, contents, 0)
raw, err := c.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
rc, response, err := raw.SendCommand("PROT C")
require.NoError(t, err)
require.Equal(t, StatusOK, rc)
require.Equal(t, "OK", response)
rc, _, err = raw.SendCommand("PASV")
require.NoError(t, err)
require.Equal(t, StatusEnteringPASV, rc)
rc, response, err = raw.SendCommand("MLSD /")
require.NoError(t, err)
require.Equal(t, StatusServiceNotAvailable, rc)
require.Equal(t, "unable to open transfer: TLS is required", response)
}
func TestDirListingBeforeLogin(t *testing.T) {
s := NewTestServer(t, true)
conn, err := net.DialTimeout("tcp", s.Addr(), 5*time.Second)
require.NoError(t, err)
defer func() {
err = conn.Close()
require.NoError(t, err)
}()
buf := make([]byte, 1024)
n, err := conn.Read(buf)
require.NoError(t, err)
response := string(buf[:n])
require.Equal(t, "220 TEST Server\r\n", response)
_, err = conn.Write([]byte("LIST\r\n"))
require.NoError(t, err)
n, err = conn.Read(buf)
require.NoError(t, err)
response = string(buf[:n])
require.Equal(t, "530 Please login with USER and PASS\r\n", response)
}
func TestListArgs(t *testing.T) {
t.Run("with-mlsd", func(t *testing.T) {
testListDirArgs(t, NewTestServer(t, true))
})
t.Run("without-mlsd", func(t *testing.T) {
testListDirArgs(t, NewTestServerWithDriver(t, &TestServerDriver{Debug: true, Settings: &Settings{DisableMLSD: true}}))
})
}
func testListDirArgs(t *testing.T, s *FtpServer) {
conf := goftp.Config{
User: authUser,
Password: authPass,
}
testDir := "testdir"
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
for _, arg := range supportedlistArgs {
s.settings.DisableLISTArgs = true
_, err = c.ReadDir(arg)
require.Error(t, err, fmt.Sprintf("list args are disabled \"list %v\" must fail", arg))
s.settings.DisableLISTArgs = false
contents, err := c.ReadDir(arg)
require.NoError(t, err)
require.Len(t, contents, 0)
_, err = c.Mkdir(arg)
require.NoError(t, err)
_, err = c.Mkdir(fmt.Sprintf("%v/%v", arg, testDir))
require.NoError(t, err)
contents, err = c.ReadDir(arg)
require.NoError(t, err)
require.Len(t, contents, 1)
require.Equal(t, contents[0].Name(), testDir)
contents, err = c.ReadDir(fmt.Sprintf("%v %v", arg, arg))
require.NoError(t, err)
require.Len(t, contents, 1)
require.Equal(t, contents[0].Name(), testDir)
// cleanup
err = c.Rmdir(fmt.Sprintf("%v/%v", arg, testDir))
require.NoError(t, err)
err = c.Rmdir(arg)
require.NoError(t, err)
}
}
func TestMLSDTimezone(t *testing.T) {
s := NewTestServer(t, true)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, s.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
ftpUpload(t, c, createTemporaryFile(t, 10), "file")
contents, err := c.ReadDir("/")
require.NoError(t, err)
require.Len(t, contents, 1)
require.Equal(t, "file", contents[0].Name())
require.InDelta(t, time.Now().Unix(), contents[0].ModTime().Unix(), 5)
}