-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_test.go
565 lines (452 loc) · 12.6 KB
/
server_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
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
package main
import (
"bytes"
"fmt"
"github.com/jlaffaye/ftp"
"github.com/satori/go.uuid"
"gopkg.in/inconshreveable/log15.v2"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"time"
)
var U string
func init() {
// disable logging in log15 (used by the ftp server)
logger := log15.Root()
logger.SetHandler(log15.DiscardHandler())
// Use a uuid based on MAC address (should be unique in Travis/Docker)
U = uuid.NewV1().String()
// run the main bucketFTP server app in a goroutine
go main()
// allow time for the server to start
time.Sleep(time.Millisecond * 50)
}
// Integration style tests for the FTP server. This requires variables in
// env.list to be exported (eg: FTP_PORT, etc).
func getClient(doLogin bool) (*ftp.ServerConn, error) {
c, err := ftp.DialTimeout("localhost:"+os.Getenv("FTP_PORT"), time.Second)
if err != nil {
return nil, err
}
if doLogin {
err = c.Login(os.Getenv("FTP_USER"), os.Getenv("FTP_PASSWD"))
if err != nil {
return nil, err
}
}
return c, nil
}
// test that we can connect to the server
func TestLogin(t *testing.T) {
var err error
var c *ftp.ServerConn
if c, err = getClient(false); err != nil {
t.Fatal(err)
}
err = c.Login(os.Getenv("FTP_USER"), os.Getenv("FTP_PASSWD"))
if err != nil {
t.Fatal(err)
}
c.Quit()
if c, err = getClient(false); err != nil {
t.Fatal(err)
}
err = c.Login("bad_username", "bad_password")
if err == nil {
t.Fatal(err)
}
}
func checkUploadedFile(c *ftp.ServerConn, path string) error {
// Upload a file to the FTP server using PUT. GET it back and confirm it's contents
var err error
testString := "some text in a file like object"
data := bytes.NewBufferString(testString)
if err = c.Stor(path, data); err != nil {
return err
}
var reader io.ReadCloser
if reader, err = c.Retr(path); err != nil {
return err
}
defer reader.Close()
var dataRead []byte
if dataRead, err = ioutil.ReadAll(reader); err != nil {
return err
}
if testString != string(dataRead) {
return fmt.Errorf("Strings do not match, expected: [%s] but saw [%s]", testString, string(dataRead))
}
return nil
}
func TestPutAndGet(t *testing.T) {
testCases := []struct {
path string
errExpected bool
}{
{"/testfile1" + U + ".txt", false},
{"testfile2" + U + ".txt", false},
{"file with spaces" + U + ".txt", false},
// invalid put/gets, should fail. The ftp server package has a problem recovering after an error: TODO: fix upstream
//{"/invalid_directory" + U + "/testfile3" + U + ".txt", true}, // the parent key does not exist
}
var err error
var c *ftp.ServerConn
if c, err = getClient(true); err != nil {
t.Fatal(err)
}
tPrefix := "testprefix" + U + "/"
if err = c.MakeDir(tPrefix); err != nil {
t.Fatal(err)
}
if len(tPrefix) > 0 {
defer c.Delete(tPrefix)
}
for _, tc := range testCases {
for _, prefix := range []string{"", tPrefix} {
t.Run(fmt.Sprintf("putting %s, expecting %v", tc.path, tc.errExpected), func(t *testing.T) {
driver.rootPrefix = prefix
if err = checkUploadedFile(c, tc.path); (err != nil) != tc.errExpected {
t.Error(err)
}
if tc.errExpected {
return
}
defer c.Delete(tc.path)
// restore rootPrefix for other tests
driver.rootPrefix = ROOT_PREFIX
})
}
}
driver.rootPrefix = ROOT_PREFIX
}
func TestDirs(t *testing.T) {
// test mkdir, cd, pwd, del
testCases := []struct {
path string
expectedCwd string
errExpected bool
}{
{"testdir1" + U, "/testdir1" + U, false},
{"/testdir2" + U, "/testdir2" + U, false},
{"/testdir3" + U + "/", "/testdir3" + U, false},
{"test dir 4" + U, "/test dir 4" + U, false},
// failures, the ftp server package has a problem recovering after an error:
//{"/", "/", true}, // shouldn't be able to mkdir /
//{"testdir5" + U + "/testsubdir" + U, "", true}, // fails, can't create multiple directories (parent doesn't exist)
}
var err error
var c *ftp.ServerConn
if c, err = getClient(true); err != nil {
t.Fatal(err)
}
defer c.Quit()
tPrefix := "testprefix" + U + "/"
if err = c.MakeDir(tPrefix); err != nil {
t.Fatal(err)
}
if len(tPrefix) > 0 {
defer c.Delete(tPrefix)
}
defer func() {
driver.rootPrefix = ROOT_PREFIX
}()
for _, tc := range testCases {
for _, prefix := range []string{"", tPrefix} {
t.Run(fmt.Sprintf("%s:%v", tc.path, tc.errExpected), func(t *testing.T) {
driver.rootPrefix = prefix
if err = c.MakeDir(tc.path); (err != nil) != tc.errExpected {
t.Fatal(err)
}
defer rmDirs(c, []string{tc.path})
// all done if we expect the command to fail
if tc.errExpected {
return
}
if err = c.ChangeDir(tc.path); err != nil {
t.Fatal(err)
}
var cwd string
if cwd, err = c.CurrentDir(); err != nil {
t.Fatal(err)
}
if cwd != tc.expectedCwd {
t.Fatalf("cwd [%s] differs from expected path [%s]", cwd, tc.expectedCwd)
}
// test getting and putting a file from this directory
testFile := "testfile" + U
if err = checkUploadedFile(c, testFile); err != nil {
t.Fatal(err)
}
defer c.Delete(testFile)
// special case, we're done testing the root dir
if tc.path == "/" {
return
}
if err = c.ChangeDir("/"); err != nil {
t.Fatal(err)
}
if err = c.Delete(tc.path); err != nil {
t.Fatal(err)
}
// directory doesn't exist any more, err should be non nil
if err = c.ChangeDir(tc.path); err == nil {
t.Fatal("expected ChangeDir to fail but it worked")
}
driver.rootPrefix = ROOT_PREFIX
})
}
}
}
func TestRename(t *testing.T) {
// Test that renaming a file or directory works
testCases := []struct {
oldPath string
mkDirs []string
newPath string
isDir bool
errExpected bool
}{
// dirs
{"/", []string{}, "/", true, true}, // shouldn't be able to cp a dir to itself
{"testdir1" + U, []string{}, "newtestdir1" + U, true, false},
{"/testdir2" + U, []string{}, "/newtestdir2" + U, true, false},
// files
{"newfile1" + U, []string{}, "newfile2" + U, false, false},
{"/newfile3" + U, []string{}, "newfile4" + U, false, false},
{"/newfile3" + U, []string{}, "new file 4" + U, false, false},
{"/dir with spaces" + U + "/testfile2" + U + ".txt", []string{"dir with spaces" + U, "new dir 5" + U},
"new dir 5" + U + "/more spaces2" + U + ".txt", false, false}, // parent dir exists
// expected failures. The ftp server package has a problem recovering after an error:
//{"/testdir3" + U + "/", []string{}, "/testdir4/subdir" + U, true, true}, // parent key doesn't exist, should fail
//{"/dir with spaces" + U + "/testfile1" + U + ".txt", []string{"dir with spaces" + U},
// "new dir 4" + U + "/more spaces1" + U + ".txt", false, true}, // parent dir does not exist
}
var err error
var c *ftp.ServerConn
if c, err = getClient(true); err != nil {
t.Fatal(err)
}
defer c.Quit()
tPrefix := "testprefix" + U + "/"
if err = c.MakeDir(tPrefix); err != nil {
t.Fatal(err)
}
defer rmDirs(c, []string{tPrefix})
testString := "some more text in a file like object"
for _, tc := range testCases {
for _, prefix := range []string{"", tPrefix} {
t.Run(fmt.Sprintf("%s:%s,%v", tc.oldPath, tc.newPath, tc.errExpected), func(t *testing.T) {
driver.rootPrefix = prefix
// create any dirs required
if err = mkDirs(c, tc.mkDirs); err != nil {
t.Error(err)
}
defer rmDirs(c, tc.mkDirs)
if tc.isDir {
// don't need to mkdir on /
if tc.oldPath != "/" {
if err = c.MakeDir(tc.oldPath); err != nil {
t.Error(err)
}
defer rmDirs(c, []string{tc.oldPath})
}
if err = c.Rename(tc.oldPath, tc.newPath); (err != nil) != tc.errExpected {
t.Fatal(err)
}
// call had expected error so bail out
if tc.errExpected {
return
}
if err = c.ChangeDir(tc.newPath); err != nil {
t.Error(err)
}
// shouldn't work except on /
err = c.ChangeDir(tc.oldPath)
if tc.oldPath != "/" && err == nil {
t.Errorf("ChangeDir was expected to fail when cd-ing to: %s", tc.oldPath)
}
} else {
// write some info to the test file if we're renaming it
data := bytes.NewBufferString(testString)
if err = c.Stor(tc.oldPath, data); err != nil {
t.Error(err)
}
// move the file
if err = c.Rename(tc.oldPath, tc.newPath); (err != nil) != tc.errExpected {
t.Fatal(err)
}
if tc.errExpected {
return
}
var reader io.ReadCloser
if reader, err = c.Retr(tc.newPath); err != nil {
t.Error(err)
}
var dataRead []byte
if dataRead, err = ioutil.ReadAll(reader); err != nil {
t.Error(err)
}
reader.Close()
if testString != string(dataRead) {
t.Errorf("Strings do not match, expected: [%s] but saw [%s]", testString, string(dataRead))
}
//// TODO: the ftp client doesn't observe the error. Should fix this. Manual tests show the file is deleted.
//if _, err = c.Retr(tc.oldPath); err == nil {
// t.Errorf("file should not exist: %s", tc.oldPath)
//}
}
if err = c.ChangeDir("/"); err != nil {
t.Error(err)
}
if err = rmDirs(c, []string{tc.newPath}); (err != nil) != tc.errExpected {
t.Error(err)
}
if err = rmDirs(c, tc.mkDirs); err != nil {
t.Error(err)
}
driver.rootPrefix = ROOT_PREFIX
})
}
}
driver.rootPrefix = ROOT_PREFIX
}
func TestDirRenameDelete(t *testing.T) {
// Test that renaming a directory containing file(s) works as expected
var err error
var c *ftp.ServerConn
if c, err = getClient(true); err != nil {
t.Fatal(err)
}
dirA := "/dirA" + U
dirB := "/dirB" + U
newDirA := "/dirB" + U + "/subdir" + U
if err = c.MakeDir(dirA); err != nil {
t.Error(err)
}
if err = c.MakeDir(dirB); err != nil {
t.Error(err)
}
if err = c.ChangeDir(dirA); err != nil {
t.Error(err)
}
testFile := "testfile" + U
if err = checkUploadedFile(c, testFile); err != nil {
t.Error(err)
}
defer c.Delete(testFile)
if err = c.ChangeDirToParent(); err != nil {
t.Error(err)
}
if err = c.Rename(dirA, newDirA); err != nil {
t.Error(err)
}
if err = c.ChangeDir(newDirA); err != nil {
t.Error(err)
}
var entries []*ftp.Entry
if entries, err = c.List(newDirA); err != nil {
t.Error(err)
}
if len(entries) != 1 {
t.Errorf("Expected one file in dir %s, saw %d", newDirA, len(entries))
}
// test deleting the nested directory
if err = c.ChangeDir("/"); err != nil {
t.Error(err)
}
if err = c.Delete(dirB); err != nil {
t.Error(err)
}
// shouldn't be able to cd
if err = c.ChangeDir(dirB); err == nil {
t.Error("should not be able to cd into a deleted directory")
}
}
func TestListFiles(t *testing.T) {
// check the list of files (not their contents)
startDir := "/listfiles" + U
files := []string{"file1" + U, "file2" + U, "file3" + U}
dirs := []string{"subdir1" + U, "subdir2" + U, "subdir2" + U + "/deepdir" + U}
var err error
var c *ftp.ServerConn
if c, err = getClient(true); err != nil {
t.Fatal(err)
}
defer c.Quit()
if err = c.MakeDir(startDir); err != nil {
t.Fatal(err)
}
if err = c.ChangeDir(startDir); err != nil {
t.Fatal(err)
}
testString := "some text in a file like object"
data := bytes.NewBufferString(testString)
// store a bunch of files in the starting dir
for _, f := range files {
if err = c.Stor(f, data); err != nil {
t.Fatal(err)
}
}
// also store them in the subdirs
for _, dir := range dirs {
if err = c.ChangeDir(startDir); err != nil {
t.Fatal(err)
}
if err = c.MakeDir(dir); err != nil {
t.Fatal(err)
}
if err = c.ChangeDir(dir); err != nil {
t.Fatal(err)
}
for _, f := range files {
if err = c.Stor(f, data); err != nil {
t.Fatal(err)
}
}
}
// check the file listing is complete but not recursive
var entries []*ftp.Entry
if entries, err = c.List(startDir); err != nil {
t.Error(err)
}
// Arg, this ftp client doesn't list directories. At least we know the listing isn't recursive.
for i, e := range entries {
name := strings.TrimSpace(e.Name)
if name != files[i] {
t.Errorf("Expected file name '%s' but observed '%s'", name, files[i])
}
}
if err = c.ChangeDir("/"); err != nil {
t.Fatal(err)
}
if err = c.Delete(startDir); err != nil {
t.Fatal(err)
}
}
func mkDirs(c *ftp.ServerConn, dirs []string) error {
var err error
for _, d := range dirs {
if len(d) < 1 {
continue
}
if err = c.MakeDir(d); err != nil {
return err
}
}
return nil
}
func rmDirs(c *ftp.ServerConn, dirs []string) error {
var err error
for _, d := range dirs {
if len(d) < 1 || d == "/" || d == "" {
continue
}
if err = c.Delete(d); err != nil {
return err
}
}
return nil
}