forked from fclairamb/ftpserverlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_misc_test.go
444 lines (345 loc) · 11.8 KB
/
handle_misc_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
package ftpserver
import (
"crypto/tls"
"fmt"
"io"
"path/filepath"
"strings"
"testing"
"time"
"github.com/secsy/goftp"
"github.com/stretchr/testify/require"
)
func TestSiteCommand(t *testing.T) {
server := NewTestServer(t, false)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
client, err := goftp.DialConfig(conf, server.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(client.Close()) }()
raw, err := client.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
rc, response, err := raw.SendCommand("SITE help")
require.NoError(t, err)
require.Equal(t, StatusSyntaxErrorNotRecognised, rc, "Are we supporting it now ?")
require.Equal(t, "Unknown SITE subcommand: HELP", response, "Are we supporting it now ?")
}
// florent(2018-01-14): #58: IDLE timeout: Testing timeout
// drakkan(2020-12-12): idle time is broken if you set timeout to 1 minute
// and a transfer requires more than 1 minutes any command issued at the transfer end
// will timeout. I handle idle timeout myself in SFTPGo but you could be
// interested to fix this bug
func TestIdleTimeout(t *testing.T) {
server := NewTestServerWithTestDriver(t, &TestServerDriver{Debug: false, Settings: &Settings{IdleTimeout: 2}})
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, server.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()) }()
time.Sleep(time.Second * 1) // < 2s : OK
returnCode, _, err := raw.SendCommand("NOOP")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
time.Sleep(time.Second * 3) // > 2s : Timeout
returnCode, _, err = raw.SendCommand("NOOP")
require.NoError(t, err)
require.Equal(t, StatusServiceNotAvailable, returnCode)
}
func TestStat(t *testing.T) {
server := NewTestServer(t, false)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
client, err := goftp.DialConfig(conf, server.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(client.Close()) }()
raw, err := client.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
returnCode, str, err := raw.SendCommand("STAT")
require.NoError(t, err)
require.Equal(t, StatusSystemStatus, returnCode)
count := strings.Count(str, "\n")
require.GreaterOrEqual(t, count, 4)
require.NotEqual(t, ' ', str[0])
server.settings.DisableSTAT = true
returnCode, str, err = raw.SendCommand("STAT")
require.NoError(t, err)
require.Equal(t, StatusCommandNotImplemented, returnCode, str)
}
func TestCLNT(t *testing.T) {
server := NewTestServer(t, false)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
c, err := goftp.DialConfig(conf, server.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()) }()
rc, _, err := raw.SendCommand("CLNT NcFTP 3.2.6 macosx10.15")
require.NoError(t, err)
require.Equal(t, StatusOK, rc)
}
func TestOPTSUTF8(t *testing.T) {
server := NewTestServer(t, false)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
client, err := goftp.DialConfig(conf, server.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(client.Close()) }()
raw, err := client.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
for _, cmd := range []string{"OPTS UTF8", "OPTS UTF8 ON"} {
rc, message, err := raw.SendCommand(cmd)
require.NoError(t, err)
require.Equal(t, StatusOK, rc)
require.Equal(t, "I'm in UTF8 only anyway", message)
}
}
func TestOPTSHASH(t *testing.T) {
server := NewTestServerWithTestDriver(
t,
&TestServerDriver{
Debug: false,
Settings: &Settings{
EnableHASH: true,
},
},
)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
client, err := goftp.DialConfig(conf, server.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(client.Close()) }()
raw, err := client.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
returnCode, message, err := raw.SendCommand("OPTS HASH")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode, message)
require.Equal(t, "SHA-256", message)
returnCode, message, err = raw.SendCommand("OPTS HASH MD5")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
require.Equal(t, "MD5", message)
returnCode, message, err = raw.SendCommand("OPTS HASH CRC-37")
require.NoError(t, err)
require.Equal(t, StatusSyntaxErrorParameters, returnCode)
require.Equal(t, "Unknown algorithm, current selection not changed", message)
returnCode, message, err = raw.SendCommand("OPTS HASH")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
require.Equal(t, "MD5", message)
// now disable hash support
server.settings.EnableHASH = false
returnCode, _, err = raw.SendCommand("OPTS HASH")
require.NoError(t, err)
require.Equal(t, StatusSyntaxErrorNotRecognised, returnCode)
}
func TestAVBL(t *testing.T) {
server := NewTestServer(t, false)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
client, err := goftp.DialConfig(conf, server.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(client.Close()) }()
raw, err := client.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
returnCode, response, err := raw.SendCommand("AVBL")
require.NoError(t, err)
require.Equal(t, StatusFileStatus, returnCode)
require.Equal(t, "123", response)
// a missing dir
returnCode, _, err = raw.SendCommand("AVBL missing")
require.NoError(t, err)
require.Equal(t, StatusActionNotTaken, returnCode)
// AVBL on a file path
ftpUpload(t, client, createTemporaryFile(t, 10), "file")
returnCode, response, err = raw.SendCommand("AVBL file")
require.NoError(t, err)
require.Equal(t, StatusActionNotTaken, returnCode)
require.Equal(t, "/file: is not a directory", response)
noavblDir, err := client.Mkdir("noavbl")
require.NoError(t, err)
returnCode, response, err = raw.SendCommand(fmt.Sprintf("AVBL %v", noavblDir))
require.NoError(t, err)
require.Equal(t, StatusActionNotTaken, returnCode)
require.Equal(t, fmt.Sprintf("Couldn't get space for path %v: %v", noavblDir, errAvblNotPermitted.Error()), response)
}
func TestQuit(t *testing.T) {
server := NewTestServerWithTestDriver(t, &TestServerDriver{
Debug: false,
TLS: true,
})
conf := goftp.Config{
User: authUser,
Password: authPass,
TLSConfig: &tls.Config{
//nolint:gosec
InsecureSkipVerify: true,
},
TLSMode: goftp.TLSExplicit,
}
client, err := goftp.DialConfig(conf, server.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(client.Close()) }()
raw, err := client.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
rc, _, err := raw.SendCommand("QUIT")
require.NoError(t, err)
require.Equal(t, StatusClosingControlConn, rc)
}
func TestQuitWithCustomMessage(t *testing.T) {
driver := &MesssageDriver{
TestServerDriver{
Debug: true,
TLS: true,
},
}
driver.Init()
server := NewTestServerWithDriver(t, driver)
req := require.New(t)
conf := goftp.Config{
User: authUser,
Password: authPass,
TLSConfig: &tls.Config{
//nolint:gosec
InsecureSkipVerify: true,
},
TLSMode: goftp.TLSExplicit,
}
c, err := goftp.DialConfig(conf, server.Addr())
req.NoError(err, "Couldn't connect")
defer func() { panicOnError(c.Close()) }()
raw, err := c.OpenRawConn()
req.NoError(err, "Couldn't open raw connection")
rc, msg, err := raw.SendCommand("QUIT")
req.NoError(err)
req.Equal(StatusClosingControlConn, rc)
req.Equal("Sayonara, bye bye!", msg)
}
func TestQuitWithTransferInProgress(t *testing.T) {
req := require.New(t)
server := NewTestServerWithTestDriver(t, &TestServerDriver{
Debug: false,
})
conf := goftp.Config{
User: authUser,
Password: authPass,
}
client, err := goftp.DialConfig(conf, server.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(client.Close()) }()
raw, err := client.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
syncChannel := make(chan struct{}, 1)
go
// I don't see a pragmatic/good way to test this without forwarding the errors to the channel,
// and thus losing the convenience of testify.
//nolint:testifylint
func() {
defer close(syncChannel)
dcGetter, err := raw.PrepareDataConn() //nolint:govet
req.NoError(err)
file := createTemporaryFile(t, 256*1024)
fileName := filepath.Base(file.Name())
rc, response, err := raw.SendCommand(fmt.Sprintf("%s %s", "STOR", fileName))
req.NoError(err)
req.Equal(StatusFileStatusOK, rc, response)
dataConn, err := dcGetter()
req.NoError(err)
syncChannel <- struct{}{}
// wait some more time to be sure we send the QUIT command before starting the file copy
time.Sleep(100 * time.Millisecond)
_, err = io.Copy(dataConn, file)
req.NoError(err)
err = dataConn.Close()
req.NoError(err)
}()
// wait for the transfer to start
<-syncChannel
// we send a QUIT command after sending STOR and before the transfer ends.
// We expect the transfer close response and then the QUIT response
returnCode, _, err := raw.SendCommand("QUIT")
req.NoError(err)
req.Equal(StatusClosingDataConn, returnCode)
returnCode, _, err = raw.ReadResponse()
req.NoError(err)
req.Equal(StatusClosingControlConn, returnCode)
}
func TestTYPE(t *testing.T) {
server := NewTestServer(t, false)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
client, err := goftp.DialConfig(conf, server.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(client.Close()) }()
raw, err := client.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
defer func() { require.NoError(t, raw.Close()) }()
returnCode, _, err := raw.SendCommand("TYPE I")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
returnCode, _, err = raw.SendCommand("TYPE A")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
returnCode, _, err = raw.SendCommand("TYPE A N")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
returnCode, _, err = raw.SendCommand("TYPE i")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
returnCode, _, err = raw.SendCommand("TYPE a")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
returnCode, _, err = raw.SendCommand("TYPE l 8")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
returnCode, _, err = raw.SendCommand("TYPE l 7")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
returnCode, _, err = raw.SendCommand("TYPE wrong")
require.NoError(t, err)
require.Equal(t, StatusNotImplementedParam, returnCode)
}
func TestMode(t *testing.T) {
server := NewTestServer(t, false)
conf := goftp.Config{
User: authUser,
Password: authPass,
}
client, err := goftp.DialConfig(conf, server.Addr())
require.NoError(t, err, "Couldn't connect")
defer func() { panicOnError(client.Close()) }()
raw, err := client.OpenRawConn()
require.NoError(t, err, "Couldn't open raw connection")
returnCode, _, err := raw.SendCommand("MODE S")
require.NoError(t, err)
require.Equal(t, StatusOK, returnCode)
returnCode, _, err = raw.SendCommand("MODE X")
require.NoError(t, err)
require.Equal(t, StatusNotImplementedParam, returnCode)
}