-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_test.go
350 lines (289 loc) · 9.33 KB
/
client_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
// Copyright 2023 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package go_ftp_test
import (
"bytes"
"fmt"
"io"
"io/fs"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"time"
go_ftp "github.com/moov-io/go-ftp"
mhttptest "github.com/moov-io/go-ftp/internal/httptest"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
func TestClient(t *testing.T) {
client, err := go_ftp.NewClient(go_ftp.ClientConfig{
Hostname: "127.0.0.1:2121",
Username: "admin",
Password: "123456",
})
require.NotNil(t, client)
require.NoError(t, err)
require.NoError(t, client.Ping())
defer client.Close()
t.Run("open", func(t *testing.T) {
file, err := client.Open("first.txt")
require.NoError(t, err)
t.Cleanup(func() { file.Close() })
var buf bytes.Buffer
io.Copy(&buf, file)
require.Equal(t, "hello world", strings.TrimSpace(buf.String()))
})
t.Run("open larger files", func(t *testing.T) {
largerFileSize := size(t, filepath.Join("testdata", "ftp-server", "bigdata", "large.txt"))
const iterations = 10
var g errgroup.Group
for i := 0; i < iterations; i++ {
g.Go(func() error {
file, err := client.Open("/bigdata/large.txt")
if err != nil {
return err
}
var buf bytes.Buffer
_, err = io.Copy(&buf, file)
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
read := len(buf.Bytes())
if read != largerFileSize {
return fmt.Errorf("read %d bytes, expected %d", read, largerFileSize)
}
return nil
})
}
require.NoError(t, g.Wait())
})
t.Run("reader", func(t *testing.T) {
file, err := client.Reader("archive/old.txt")
require.NoError(t, err)
t.Cleanup(func() { file.Close() })
var buf bytes.Buffer
io.Copy(&buf, file)
require.Equal(t, "previous data", strings.TrimSpace(buf.String()))
})
t.Run("read larger files", func(t *testing.T) {
largerFileSize := size(t, filepath.Join("testdata", "ftp-server", "bigdata", "large.txt"))
// reader must process files in sequence
for i := 0; i < 10; i++ {
file, err := client.Reader("/bigdata/large.txt")
require.NoError(t, err)
var buf bytes.Buffer
_, err = io.Copy(&buf, file)
require.NoError(t, err)
require.NoError(t, file.Close())
require.Len(t, buf.Bytes(), largerFileSize)
}
})
t.Run("upload and delete", func(t *testing.T) {
body := io.NopCloser(strings.NewReader("example data"))
err := client.UploadFile("new.txt", body)
require.NoError(t, err)
file, err := client.Open("new.txt")
require.NoError(t, err)
var buf bytes.Buffer
io.Copy(&buf, file)
require.Equal(t, "example data", strings.TrimSpace(buf.String()))
require.NoError(t, file.Close())
err = client.Delete("new.txt")
require.NoError(t, err)
file, err = client.Open("new.txt")
require.Nil(t, file)
require.ErrorContains(t, err, "retrieving new.txt failed: 551 File not available")
})
t.Run("delete", func(t *testing.T) {
err := client.Delete("/missing.txt")
require.NoError(t, err)
err = client.Delete("/no-existing-dir/missing.txt")
require.NoError(t, err)
})
t.Run("list", func(t *testing.T) {
filenames, err := client.ListFiles(".")
require.NoError(t, err)
require.ElementsMatch(t, filenames, []string{"first.txt", "second.txt", "empty.txt"})
filenames, err = client.ListFiles("/")
require.NoError(t, err)
require.ElementsMatch(t, filenames, []string{"/first.txt", "/second.txt", "/empty.txt"})
})
t.Run("list subdir", func(t *testing.T) {
filenames, err := client.ListFiles("archive")
require.NoError(t, err)
require.ElementsMatch(t, filenames, []string{"archive/old.txt", "archive/empty2.txt"})
filenames, err = client.ListFiles("/archive")
require.NoError(t, err)
require.ElementsMatch(t, filenames, []string{"/archive/old.txt", "/archive/empty2.txt"})
filenames, err = client.ListFiles("/archive/")
require.NoError(t, err)
require.ElementsMatch(t, filenames, []string{"/archive/old.txt", "/archive/empty2.txt"})
})
t.Run("list and read", func(t *testing.T) {
filenames, err := client.ListFiles("/with-empty")
require.NoError(t, err)
// randomize filename order
rand.Shuffle(len(filenames), func(i, j int) {
filenames[i], filenames[j] = filenames[j], filenames[i]
})
require.ElementsMatch(t, filenames, []string{
"/with-empty/EMPTY1.txt", "/with-empty/empty_file2.txt",
"/with-empty/data.txt", "/with-empty/data2.txt",
})
// read each file and get back expected contents
var contents []string
for i := range filenames {
var file *go_ftp.File
if i/2 == 0 {
file, err = client.Open(filenames[i])
} else {
file, err = client.Reader(filenames[i])
}
require.NoError(t, err, "filenames[%d]", i)
require.NotNil(t, file, "filenames[%d]", i)
require.NotNil(t, file.Contents, "filenames[%d]", i)
bs, err := io.ReadAll(file.Contents)
require.NoError(t, err)
contents = append(contents, string(bs))
}
require.ElementsMatch(t, contents, []string{"", "", "also data\n", "has data\n"})
})
t.Run("list case testing", func(t *testing.T) {
files, err := client.ListFiles("/upper")
require.NoError(t, err)
require.ElementsMatch(t, files, []string{"/Upper/names.txt"})
files, err = client.ListFiles("ARCHIVE")
require.NoError(t, err)
require.ElementsMatch(t, files, []string{"archive/old.txt", "archive/empty2.txt"})
})
t.Run("walk", func(t *testing.T) {
var found []string
err := client.Walk(".", func(path string, d fs.DirEntry, err error) error {
found = append(found, path)
return nil
})
require.NoError(t, err)
require.ElementsMatch(t, found, []string{
"Upper", "Upper/names.txt", "bigdata", "bigdata/large.txt",
"first.txt", "second.txt", "empty.txt",
"archive", "archive/old.txt", "archive/empty2.txt",
"with-empty", "with-empty/EMPTY1.txt", "with-empty/empty_file2.txt",
"with-empty/data.txt", "with-empty/data2.txt",
})
})
t.Run("walk subdir", func(t *testing.T) {
var found []string
err := client.Walk("/archive", func(path string, d fs.DirEntry, err error) error {
found = append(found, path)
return nil
})
require.NoError(t, err)
require.ElementsMatch(t, found, []string{
"/archive/old.txt", "/archive/empty2.txt",
})
})
t.Run("walk skipdir", func(t *testing.T) {
var found []string
err := client.Walk(".", func(path string, d fs.DirEntry, err error) error {
found = append(found, path)
if strings.Contains(path, "with-empty") {
return fs.SkipDir
}
return nil
})
require.NoError(t, err)
require.ElementsMatch(t, found, []string{
"with-empty",
"second.txt", "first.txt", "empty.txt",
"bigdata", "bigdata/large.txt",
"archive", "archive/old.txt", "archive/empty2.txt",
"Upper", "Upper/names.txt",
})
})
require.NoError(t, client.Close())
}
func TestClientErrors(t *testing.T) {
client, err := go_ftp.NewClient(go_ftp.ClientConfig{
Hostname: "127.0.0.1:2121",
Username: "admin",
Password: "123456",
})
require.NotNil(t, client)
require.NoError(t, err)
require.NoError(t, client.Ping())
defer client.Close()
t.Run("open", func(t *testing.T) {
file, err := client.Open("not-found.txt")
require.ErrorContains(t, err, "551 File not available")
require.Nil(t, file)
})
t.Run("reader", func(t *testing.T) {
file, err := client.Reader("not-found.txt")
require.ErrorContains(t, err, "551 File not available")
require.Nil(t, file)
})
t.Run("upload", func(t *testing.T) {
body := io.NopCloser(strings.NewReader("no data"))
err := client.UploadFile("dir/does/not/exist.txt", body)
require.ErrorContains(t, err, "550 Directory change to /dir/does/not failed: lstat /data/dir/does/not: no such file or directory")
})
t.Run("list", func(t *testing.T) {
filenames, err := client.ListFiles("does/not/exist")
require.NoError(t, err)
require.Empty(t, filenames)
})
t.Run("walk", func(t *testing.T) {
var found []string
err := client.Walk("does/not/exist", func(path string, d fs.DirEntry, err error) error {
found = append(found, path)
return nil
})
require.ErrorContains(t, err, "550 Directory change to /does/not/exist failed: lstat /data/does/not/exist: no such file or directory")
require.Empty(t, found)
})
require.NoError(t, client.Close())
}
func TestClientFailure(t *testing.T) {
client, err := go_ftp.NewClient(go_ftp.ClientConfig{
Hostname: "127.0.0.1:2121",
Username: "incorrect",
Password: "wrong",
})
require.NotNil(t, client)
require.ErrorContains(t, err, "ftp connect: 530 Incorrect password, not logged in")
require.ErrorContains(t, client.Ping(), "530 Incorrect password, not logged in")
require.NoError(t, client.Close())
}
func TestClient__tlsDialOption(t *testing.T) {
if testing.Short() {
return // skip network calls
}
cafile, err := mhttptest.GrabConnectionCertificates(t, "google.com:443")
require.NoError(t, err)
defer os.Remove(cafile)
client, err := go_ftp.NewClient(go_ftp.ClientConfig{
Hostname: "127.0.0.1:2121",
Username: "admin",
Password: "123456",
Timeout: 5 * time.Second,
CAFile: cafile,
})
require.ErrorContains(t, err, "tls: first record does not look like a TLS handshake")
require.NotNil(t, client)
require.NoError(t, client.Close())
}
func size(t *testing.T, where string) int {
t.Helper()
fd, err := os.Open(where)
require.NoError(t, err)
info, err := fd.Stat()
require.NoError(t, err)
return int(info.Size())
}