forked from pkg/sftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftp_test.go
78 lines (68 loc) · 2.32 KB
/
sftp_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
package sftp
import (
"errors"
"fmt"
"io"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
)
func TestErrFxCode(t *testing.T) {
table := []struct {
err error
fx fxerr
}{
{err: errors.New("random error"), fx: ErrSSHFxFailure},
{err: EBADF, fx: ErrSSHFxFailure},
{err: syscall.ENOENT, fx: ErrSSHFxNoSuchFile},
{err: syscall.EPERM, fx: ErrSSHFxPermissionDenied},
{err: io.EOF, fx: ErrSSHFxEOF},
{err: fmt.Errorf("wrapped permission denied error: %w", ErrSSHFxPermissionDenied), fx: ErrSSHFxPermissionDenied},
{err: fmt.Errorf("wrapped op unsupported error: %w", ErrSSHFxOpUnsupported), fx: ErrSSHFxOpUnsupported},
}
for _, tt := range table {
statusErr := statusFromError(1, tt.err).StatusError
assert.Equal(t, statusErr.FxCode(), tt.fx)
}
}
func TestSupportedExtensions(t *testing.T) {
for _, supportedExtension := range supportedSFTPExtensions {
_, err := getSupportedExtensionByName(supportedExtension.Name)
assert.NoError(t, err)
}
_, err := getSupportedExtensionByName("[email protected]")
assert.Error(t, err)
}
func TestExtensions(t *testing.T) {
var supportedExtensions []string
for _, supportedExtension := range supportedSFTPExtensions {
supportedExtensions = append(supportedExtensions, supportedExtension.Name)
}
testSFTPExtensions := []string{"[email protected]"}
expectedSFTPExtensions := []sshExtensionPair{
{"[email protected]", "1"},
}
err := SetSFTPExtensions(testSFTPExtensions...)
assert.NoError(t, err)
assert.Equal(t, expectedSFTPExtensions, sftpExtensions)
invalidSFTPExtensions := []string{"[email protected]"}
err = SetSFTPExtensions(invalidSFTPExtensions...)
assert.Error(t, err)
assert.Equal(t, expectedSFTPExtensions, sftpExtensions)
emptySFTPExtensions := []string{}
expectedSFTPExtensions = []sshExtensionPair{}
err = SetSFTPExtensions(emptySFTPExtensions...)
assert.NoError(t, err)
assert.Equal(t, expectedSFTPExtensions, sftpExtensions)
// if we only have an invalid extension nothing will be modified.
invalidSFTPExtensions = []string{
}
err = SetSFTPExtensions(invalidSFTPExtensions...)
assert.Error(t, err)
assert.Equal(t, expectedSFTPExtensions, sftpExtensions)
err = SetSFTPExtensions(supportedExtensions...)
assert.NoError(t, err)
assert.Equal(t, supportedSFTPExtensions, sftpExtensions)
}