2019-07-27 07:38:09 +00:00
|
|
|
package sftpd
|
|
|
|
|
|
|
|
import (
|
2019-08-24 12:41:15 +00:00
|
|
|
"bytes"
|
2020-01-12 07:25:08 +00:00
|
|
|
"errors"
|
2019-08-24 12:41:15 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-10-10 07:04:17 +00:00
|
|
|
"net"
|
2019-08-04 07:37:58 +00:00
|
|
|
"os"
|
2020-01-19 06:41:05 +00:00
|
|
|
"path/filepath"
|
2019-07-27 19:19:30 +00:00
|
|
|
"runtime"
|
2019-07-27 07:38:09 +00:00
|
|
|
"testing"
|
2019-08-24 12:41:15 +00:00
|
|
|
"time"
|
2019-08-04 07:37:58 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/eikenb/pipeat"
|
|
|
|
"github.com/pkg/sftp"
|
2022-04-14 17:07:41 +00:00
|
|
|
"github.com/rs/xid"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-08-31 04:45:22 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-05-15 18:08:53 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
2022-01-06 09:11:47 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/kms"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2019-07-27 07:38:09 +00:00
|
|
|
)
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
const osWindows = "windows"
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
type MockChannel struct {
|
2019-11-26 21:26:42 +00:00
|
|
|
Buffer *bytes.Buffer
|
|
|
|
StdErrBuffer *bytes.Buffer
|
|
|
|
ReadError error
|
|
|
|
WriteError error
|
|
|
|
ShortWriteErr bool
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockChannel) Read(data []byte) (int, error) {
|
|
|
|
if c.ReadError != nil {
|
|
|
|
return 0, c.ReadError
|
|
|
|
}
|
|
|
|
return c.Buffer.Read(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockChannel) Write(data []byte) (int, error) {
|
|
|
|
if c.WriteError != nil {
|
|
|
|
return 0, c.WriteError
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
if c.ShortWriteErr {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
return c.Buffer.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockChannel) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockChannel) CloseWrite() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockChannel) SendRequest(name string, wantReply bool, payload []byte) (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockChannel) Stderr() io.ReadWriter {
|
|
|
|
return c.StdErrBuffer
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
// MockOsFs mockable OsFs
|
|
|
|
type MockOsFs struct {
|
2020-01-19 12:58:55 +00:00
|
|
|
vfs.Fs
|
2020-01-19 06:41:05 +00:00
|
|
|
err error
|
|
|
|
statErr error
|
|
|
|
isAtomicUploadSupported bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name for the Fs implementation
|
|
|
|
func (fs MockOsFs) Name() string {
|
|
|
|
return "mockOsFs"
|
|
|
|
}
|
|
|
|
|
2021-04-03 14:00:55 +00:00
|
|
|
// IsUploadResumeSupported returns true if resuming uploads is supported
|
2020-01-19 06:41:05 +00:00
|
|
|
func (MockOsFs) IsUploadResumeSupported() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAtomicUploadSupported returns true if atomic upload is supported
|
|
|
|
func (fs MockOsFs) IsAtomicUploadSupported() bool {
|
|
|
|
return fs.isAtomicUploadSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns a FileInfo describing the named file
|
|
|
|
func (fs MockOsFs) Stat(name string) (os.FileInfo, error) {
|
|
|
|
if fs.statErr != nil {
|
|
|
|
return nil, fs.statErr
|
|
|
|
}
|
|
|
|
return os.Stat(name)
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
// Lstat returns a FileInfo describing the named file
|
|
|
|
func (fs MockOsFs) Lstat(name string) (os.FileInfo, error) {
|
|
|
|
if fs.statErr != nil {
|
|
|
|
return nil, fs.statErr
|
|
|
|
}
|
|
|
|
return os.Lstat(name)
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
// Remove removes the named file or (empty) directory.
|
|
|
|
func (fs MockOsFs) Remove(name string, isDir bool) error {
|
|
|
|
if fs.err != nil {
|
|
|
|
return fs.err
|
|
|
|
}
|
|
|
|
return os.Remove(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename renames (moves) source to target
|
|
|
|
func (fs MockOsFs) Rename(source, target string) error {
|
|
|
|
if fs.err != nil {
|
|
|
|
return fs.err
|
|
|
|
}
|
|
|
|
return os.Rename(source, target)
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:58:55 +00:00
|
|
|
func newMockOsFs(err, statErr error, atomicUpload bool, connectionID, rootDir string) vfs.Fs {
|
2020-01-19 06:41:05 +00:00
|
|
|
return &MockOsFs{
|
2021-03-21 18:15:47 +00:00
|
|
|
Fs: vfs.NewOsFs(connectionID, rootDir, ""),
|
2020-01-19 06:41:05 +00:00
|
|
|
err: err,
|
|
|
|
statErr: statErr,
|
|
|
|
isAtomicUploadSupported: atomicUpload,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 07:38:09 +00:00
|
|
|
func TestRemoveNonexistentQuotaScan(t *testing.T) {
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.False(t, common.QuotaScans.RemoveUserQuotaScan("username"))
|
2019-07-27 07:38:09 +00:00
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
|
|
|
|
func TestGetOSOpenFlags(t *testing.T) {
|
|
|
|
var flags sftp.FileOpenFlags
|
|
|
|
flags.Write = true
|
|
|
|
flags.Excl = true
|
2019-08-04 09:02:38 +00:00
|
|
|
osFlags := getOSOpenFlags(flags)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NotEqual(t, 0, osFlags&os.O_WRONLY)
|
|
|
|
assert.NotEqual(t, 0, osFlags&os.O_EXCL)
|
|
|
|
|
2019-10-09 15:33:30 +00:00
|
|
|
flags.Append = true
|
|
|
|
// append flag should be ignored to allow resume
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NotEqual(t, 0, osFlags&os.O_WRONLY)
|
|
|
|
assert.NotEqual(t, 0, osFlags&os.O_EXCL)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:33:30 +00:00
|
|
|
func TestUploadResumeInvalidOffset(t *testing.T) {
|
2020-05-06 17:36:34 +00:00
|
|
|
testfile := "testfile" //nolint:goconst
|
|
|
|
file, err := os.Create(testfile)
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "testuser",
|
|
|
|
},
|
2019-10-09 15:33:30 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2021-07-24 18:11:17 +00:00
|
|
|
conn := common.NewBaseConnection("", common.ProtocolSFTP, "", "", user)
|
2022-01-20 17:19:20 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, conn, nil, file.Name(), file.Name(), testfile,
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferUpload, 10, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer := newTransfer(baseTransfer, nil, nil, nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
_, err = transfer.WriteAt([]byte("test"), 0)
|
|
|
|
assert.Error(t, err, "upload with invalid offset must fail")
|
2020-07-24 21:39:38 +00:00
|
|
|
if assert.Error(t, transfer.ErrTransfer) {
|
|
|
|
assert.EqualError(t, err, transfer.ErrTransfer.Error())
|
2021-03-21 18:15:47 +00:00
|
|
|
assert.Contains(t, transfer.ErrTransfer.Error(), "invalid write offset")
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 07:25:08 +00:00
|
|
|
err = transfer.Close()
|
2020-05-06 17:36:34 +00:00
|
|
|
if assert.Error(t, err) {
|
2021-08-08 17:30:21 +00:00
|
|
|
assert.ErrorIs(t, err, sftp.ErrSSHFxFailure)
|
|
|
|
assert.Contains(t, err.Error(), "invalid write offset")
|
2020-01-12 07:25:08 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
err = os.Remove(testfile)
|
|
|
|
assert.NoError(t, err)
|
2019-08-04 07:37:58 +00:00
|
|
|
}
|
2019-08-04 10:35:33 +00:00
|
|
|
|
2020-01-10 18:20:22 +00:00
|
|
|
func TestReadWriteErrors(t *testing.T) {
|
|
|
|
testfile := "testfile"
|
2020-05-06 17:36:34 +00:00
|
|
|
file, err := os.Create(testfile)
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "testuser",
|
|
|
|
},
|
2020-01-10 18:20:22 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2021-07-24 18:11:17 +00:00
|
|
|
conn := common.NewBaseConnection("", common.ProtocolSFTP, "", "", user)
|
2022-01-20 17:19:20 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, conn, nil, file.Name(), file.Name(), testfile, common.TransferDownload,
|
2022-01-30 10:42:36 +00:00
|
|
|
0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer := newTransfer(baseTransfer, nil, nil, nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
err = file.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = transfer.WriteAt([]byte("test"), 0)
|
|
|
|
assert.Error(t, err, "writing to closed file must fail")
|
2020-01-10 18:20:22 +00:00
|
|
|
buf := make([]byte, 32768)
|
|
|
|
_, err = transfer.ReadAt(buf, 0)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "reading from a closed file must fail")
|
2020-01-10 18:20:22 +00:00
|
|
|
err = transfer.Close()
|
2020-05-20 18:17:59 +00:00
|
|
|
assert.Error(t, err)
|
2020-05-06 17:36:34 +00:00
|
|
|
|
|
|
|
r, _, err := pipeat.Pipe()
|
|
|
|
assert.NoError(t, err)
|
2022-01-20 17:19:20 +00:00
|
|
|
baseTransfer = common.NewBaseTransfer(nil, conn, nil, file.Name(), file.Name(), testfile, common.TransferDownload,
|
2022-01-30 10:42:36 +00:00
|
|
|
0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer = newTransfer(baseTransfer, nil, r, nil)
|
|
|
|
err = transfer.Close()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
_, err = transfer.ReadAt(buf, 0)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "reading from a closed pipe must fail")
|
|
|
|
|
|
|
|
r, w, err := pipeat.Pipe()
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
pipeWriter := vfs.NewPipeWriter(w)
|
2022-01-20 17:19:20 +00:00
|
|
|
baseTransfer = common.NewBaseTransfer(nil, conn, nil, file.Name(), file.Name(), testfile, common.TransferDownload,
|
2022-01-30 10:42:36 +00:00
|
|
|
0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer = newTransfer(baseTransfer, pipeWriter, nil, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
err = r.Close()
|
|
|
|
assert.NoError(t, err)
|
2020-05-19 17:17:43 +00:00
|
|
|
errFake := fmt.Errorf("fake upload error")
|
|
|
|
go func() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-07-24 21:39:38 +00:00
|
|
|
pipeWriter.Done(errFake)
|
2020-05-19 17:17:43 +00:00
|
|
|
}()
|
2020-05-06 17:36:34 +00:00
|
|
|
err = transfer.closeIO()
|
2020-05-19 17:17:43 +00:00
|
|
|
assert.EqualError(t, err, errFake.Error())
|
2020-01-19 06:41:05 +00:00
|
|
|
_, err = transfer.WriteAt([]byte("test"), 0)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "writing to closed pipe must fail")
|
2020-08-31 04:45:22 +00:00
|
|
|
err = transfer.BaseTransfer.Close()
|
|
|
|
assert.EqualError(t, err, errFake.Error())
|
2020-05-06 17:36:34 +00:00
|
|
|
|
|
|
|
err = os.Remove(testfile)
|
|
|
|
assert.NoError(t, err)
|
2020-08-31 04:45:22 +00:00
|
|
|
assert.Len(t, conn.GetTransfers(), 0)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:52:17 +00:00
|
|
|
func TestUnsupportedListOP(t *testing.T) {
|
2021-07-24 18:11:17 +00:00
|
|
|
conn := common.NewBaseConnection("", common.ProtocolSFTP, "", "", dataprovider.User{})
|
2020-08-22 12:52:17 +00:00
|
|
|
sftpConn := Connection{
|
|
|
|
BaseConnection: conn,
|
|
|
|
}
|
|
|
|
request := sftp.NewRequest("Unsupported", "")
|
|
|
|
_, err := sftpConn.Filelist(request)
|
|
|
|
assert.EqualError(t, err, sftp.ErrSSHFxOpUnsupported.Error())
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
func TestTransferCancelFn(t *testing.T) {
|
|
|
|
testfile := "testfile"
|
2020-05-06 17:36:34 +00:00
|
|
|
file, err := os.Create(testfile)
|
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
isCancelled := false
|
|
|
|
cancelFn := func() {
|
|
|
|
isCancelled = true
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "testuser",
|
|
|
|
},
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2021-07-24 18:11:17 +00:00
|
|
|
conn := common.NewBaseConnection("", common.ProtocolSFTP, "", "", user)
|
2022-01-20 17:19:20 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, conn, cancelFn, file.Name(), file.Name(), testfile, common.TransferDownload,
|
2022-01-30 10:42:36 +00:00
|
|
|
0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer := newTransfer(baseTransfer, nil, nil, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
errFake := errors.New("fake error, this will trigger cancelFn")
|
|
|
|
transfer.TransferError(errFake)
|
|
|
|
err = transfer.Close()
|
2020-07-24 21:39:38 +00:00
|
|
|
if assert.Error(t, err) {
|
2021-08-08 17:30:21 +00:00
|
|
|
assert.ErrorIs(t, err, sftp.ErrSSHFxFailure)
|
|
|
|
assert.Contains(t, err.Error(), errFake.Error())
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
if assert.Error(t, transfer.ErrTransfer) {
|
|
|
|
assert.EqualError(t, transfer.ErrTransfer, errFake.Error())
|
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.True(t, isCancelled, "cancelFn not called!")
|
|
|
|
|
|
|
|
err = os.Remove(testfile)
|
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 10:35:33 +00:00
|
|
|
func TestUploadFiles(t *testing.T) {
|
2020-07-24 21:39:38 +00:00
|
|
|
common.Config.UploadMode = common.UploadModeAtomic
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("123", os.TempDir(), "")
|
2020-07-24 21:39:38 +00:00
|
|
|
u := dataprovider.User{}
|
2020-01-19 06:41:05 +00:00
|
|
|
c := Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", u),
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2019-08-04 10:35:33 +00:00
|
|
|
var flags sftp.FileOpenFlags
|
|
|
|
flags.Write = true
|
|
|
|
flags.Trunc = true
|
2021-03-21 18:15:47 +00:00
|
|
|
_, err := c.handleSFTPUploadToExistingFile(fs, flags, "missing_path", "other_missing_path", 0, "/missing_path", nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "upload to existing file must fail if one or both paths are invalid")
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
common.Config.UploadMode = common.UploadModeStandard
|
2021-03-21 18:15:47 +00:00
|
|
|
_, err = c.handleSFTPUploadToExistingFile(fs, flags, "missing_path", "other_missing_path", 0, "/missing_path", nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "upload to existing file must fail if one or both paths are invalid")
|
|
|
|
|
2019-08-04 10:35:33 +00:00
|
|
|
missingFile := "missing/relative/file.txt"
|
2020-05-06 17:36:34 +00:00
|
|
|
if runtime.GOOS == osWindows {
|
2019-08-04 10:35:33 +00:00
|
|
|
missingFile = "missing\\relative\\file.txt"
|
|
|
|
}
|
2022-02-16 15:05:56 +00:00
|
|
|
_, err = c.handleSFTPUploadToNewFile(fs, flags, ".", missingFile, "/missing", nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "upload new file in missing path must fail")
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
fs = newMockOsFs(nil, nil, false, "123", os.TempDir())
|
2021-02-25 20:53:04 +00:00
|
|
|
f, err := os.CreateTemp("", "temp")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = f.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
tr, err := c.handleSFTPUploadToExistingFile(fs, flags, f.Name(), f.Name(), 123, f.Name(), nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
transfer := tr.(*transfer)
|
|
|
|
transfers := c.GetTransfers()
|
|
|
|
if assert.Equal(t, 1, len(transfers)) {
|
|
|
|
assert.Equal(t, transfers[0].ID, transfer.GetID())
|
|
|
|
assert.Equal(t, int64(123), transfer.InitialSize)
|
|
|
|
err = transfer.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(c.GetTransfers()))
|
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
}
|
|
|
|
err = os.Remove(f.Name())
|
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
common.Config.UploadMode = common.UploadModeAtomicWithResume
|
2019-08-04 10:35:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func TestWithInvalidHome(t *testing.T) {
|
2019-08-04 10:35:33 +00:00
|
|
|
u := dataprovider.User{}
|
2020-05-06 17:36:34 +00:00
|
|
|
u.HomeDir = "home_rel_path" //nolint:goconst
|
2021-02-16 18:11:36 +00:00
|
|
|
_, err := loginUser(&u, dataprovider.LoginMethodPassword, "", nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "login a user with an invalid home_dir must fail")
|
|
|
|
|
2020-01-19 12:58:55 +00:00
|
|
|
u.HomeDir = os.TempDir()
|
2020-05-06 17:36:34 +00:00
|
|
|
fs, err := u.GetFilesystem("123")
|
|
|
|
assert.NoError(t, err)
|
2019-08-11 12:53:37 +00:00
|
|
|
c := Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", u),
|
2019-08-11 12:53:37 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
_, err = fs.ResolvePath("../upper_path")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "tested path is not a home subdir")
|
2021-02-11 18:45:52 +00:00
|
|
|
_, err = c.StatVFS(&sftp.Request{
|
|
|
|
Method: "StatVFS",
|
|
|
|
Filepath: "../unresolvable-path",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2019-08-04 10:35:33 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
|
2021-04-25 12:36:29 +00:00
|
|
|
func TestResolveWithRootDir(t *testing.T) {
|
|
|
|
u := dataprovider.User{}
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
u.HomeDir = "C:\\"
|
|
|
|
} else {
|
|
|
|
u.HomeDir = "/"
|
|
|
|
}
|
|
|
|
fs, err := u.GetFilesystem("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rel, err := filepath.Rel(u.HomeDir, os.TempDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
p, err := fs.ResolvePath(rel)
|
|
|
|
assert.NoError(t, err, "path %v", p)
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestSFTPGetUsedQuota(t *testing.T) {
|
|
|
|
u := dataprovider.User{}
|
|
|
|
u.HomeDir = "home_rel_path"
|
|
|
|
u.Username = "test_invalid_user"
|
|
|
|
u.QuotaSize = 4096
|
|
|
|
u.QuotaFiles = 1
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions = make(map[string][]string)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
2019-08-24 12:41:15 +00:00
|
|
|
connection := Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", u),
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
quotaResult, _ := connection.HasSpace(false, false, "/")
|
2020-06-16 20:49:18 +00:00
|
|
|
assert.False(t, quotaResult.HasSpace)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
func TestSupportedSSHCommands(t *testing.T) {
|
|
|
|
cmds := GetSupportedSSHCommands()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, len(supportedSSHCommands), len(cmds))
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
for _, c := range cmds {
|
2021-07-11 13:26:51 +00:00
|
|
|
assert.True(t, util.IsStringInSlice(c, supportedSSHCommands))
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHCommandPath(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2022-03-03 11:44:56 +00:00
|
|
|
channel: &mockSSHChannel,
|
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSSH, "", "", dataprovider.User{}),
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
sshCommand := sshCommand{
|
|
|
|
command: "test",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{},
|
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "", sshCommand.getDestPath())
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand.args = []string{"-t", "/tmp/../path"}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "/path", sshCommand.getDestPath())
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand.args = []string{"-t", "/tmp/"}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "/tmp/", sshCommand.getDestPath())
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand.args = []string{"-t", "tmp/"}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "/tmp/", sshCommand.getDestPath())
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
sshCommand.args = []string{"-t", "/tmp/../../../path"}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "/path", sshCommand.getDestPath())
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
sshCommand.args = []string{"-t", ".."}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "/", sshCommand.getDestPath())
|
|
|
|
|
2020-01-05 10:41:25 +00:00
|
|
|
sshCommand.args = []string{"-t", "."}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "/", sshCommand.getDestPath())
|
|
|
|
|
2020-01-05 10:41:25 +00:00
|
|
|
sshCommand.args = []string{"-t", "//"}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "/", sshCommand.getDestPath())
|
|
|
|
|
2020-01-05 10:41:25 +00:00
|
|
|
sshCommand.args = []string{"-t", "../.."}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "/", sshCommand.getDestPath())
|
|
|
|
|
2020-01-05 10:41:25 +00:00
|
|
|
sshCommand.args = []string{"-t", "/.."}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "/", sshCommand.getDestPath())
|
|
|
|
|
2020-02-08 22:33:06 +00:00
|
|
|
sshCommand.args = []string{"-f", "/a space.txt"}
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "/a space.txt", sshCommand.getDestPath())
|
2020-02-08 22:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHParseCommandPayload(t *testing.T) {
|
2020-02-14 15:17:32 +00:00
|
|
|
cmd := "command -a -f /ab\\ à/some\\ spaces\\ \\ \\(\\).txt"
|
2020-02-08 22:33:06 +00:00
|
|
|
name, args, _ := parseCommandPayload(cmd)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "command", name)
|
|
|
|
assert.Equal(t, 3, len(args))
|
|
|
|
assert.Equal(t, "/ab à/some spaces ().txt", args[2])
|
|
|
|
|
2020-02-14 15:17:32 +00:00
|
|
|
_, _, err := parseCommandPayload("")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "parsing invalid command must fail")
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHCommandErrors(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
readErr := fmt.Errorf("test read error")
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
}
|
|
|
|
server, client := net.Pipe()
|
2020-05-06 17:36:34 +00:00
|
|
|
defer func() {
|
|
|
|
err := server.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := client.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
2019-12-25 17:20:19 +00:00
|
|
|
user := dataprovider.User{}
|
2022-04-02 16:32:46 +00:00
|
|
|
user.Permissions = map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
connection := Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSSH, "", "", user),
|
2020-07-24 21:39:38 +00:00
|
|
|
channel: &mockSSHChannel,
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
cmd := sshCommand{
|
|
|
|
command: "md5sum",
|
2020-07-24 21:39:38 +00:00
|
|
|
connection: &connection,
|
2019-11-18 22:30:37 +00:00
|
|
|
args: []string{},
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err := cmd.handle()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "ssh command must fail, we are sending a fake error")
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
cmd = sshCommand{
|
|
|
|
command: "md5sum",
|
2020-07-24 21:39:38 +00:00
|
|
|
connection: &connection,
|
2020-08-11 21:56:10 +00:00
|
|
|
args: []string{"/../../test_file_ftp.dat"},
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
err = cmd.handle()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "ssh command must fail, we are requesting an invalid path")
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
cmd = sshCommand{
|
|
|
|
command: "git-receive-pack",
|
2020-07-24 21:39:38 +00:00
|
|
|
connection: &connection,
|
2019-11-26 21:26:42 +00:00
|
|
|
args: []string{"/../../testrepo"},
|
|
|
|
}
|
|
|
|
err = cmd.handle()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "ssh command must fail, we are requesting an invalid path")
|
|
|
|
|
2022-04-02 16:32:46 +00:00
|
|
|
user = dataprovider.User{}
|
|
|
|
user.Permissions = map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
}
|
|
|
|
user.HomeDir = filepath.Clean(os.TempDir())
|
|
|
|
user.QuotaFiles = 1
|
|
|
|
user.UsedQuotaFiles = 2
|
|
|
|
cmd.connection.User = user
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, err := cmd.connection.User.GetFilesystem("123")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2019-11-26 21:26:42 +00:00
|
|
|
err = cmd.handle()
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.EqualError(t, err, common.ErrQuotaExceeded.Error())
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
cmd.connection.User.QuotaFiles = 0
|
|
|
|
cmd.connection.User.UsedQuotaFiles = 0
|
2019-12-25 17:20:19 +00:00
|
|
|
cmd.connection.User.Permissions = make(map[string][]string)
|
|
|
|
cmd.connection.User.Permissions["/"] = []string{dataprovider.PermListItems}
|
2019-11-26 21:26:42 +00:00
|
|
|
err = cmd.handle()
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.EqualError(t, err, common.ErrPermissionDenied.Error())
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
cmd.connection.User.Permissions["/"] = []string{dataprovider.PermAny}
|
2019-11-26 21:26:42 +00:00
|
|
|
cmd.command = "invalid_command"
|
|
|
|
command, err := cmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
err = cmd.executeSystemCommand(command)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "invalid command must fail")
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
command, err = cmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = command.cmd.StderrPipe()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
err = cmd.executeSystemCommand(command)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "command must fail, pipe was already assigned")
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
err = cmd.executeSystemCommand(command)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "command must fail, pipe was already assigned")
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
command, err = cmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = command.cmd.StdoutPipe()
|
|
|
|
assert.NoError(t, err)
|
2019-11-26 21:26:42 +00:00
|
|
|
err = cmd.executeSystemCommand(command)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "command must fail, pipe was already assigned")
|
2020-06-13 20:48:51 +00:00
|
|
|
|
|
|
|
cmd = sshCommand{
|
|
|
|
command: "sftpgo-remove",
|
2020-07-24 21:39:38 +00:00
|
|
|
connection: &connection,
|
2020-06-13 20:48:51 +00:00
|
|
|
args: []string{"/../../src"},
|
|
|
|
}
|
|
|
|
err = cmd.handle()
|
|
|
|
assert.Error(t, err, "ssh command must fail, we are requesting an invalid path")
|
|
|
|
|
|
|
|
cmd = sshCommand{
|
|
|
|
command: "sftpgo-copy",
|
2020-07-24 21:39:38 +00:00
|
|
|
connection: &connection,
|
2020-06-13 20:48:51 +00:00
|
|
|
args: []string{"/../../test_src", "."},
|
|
|
|
}
|
|
|
|
err = cmd.handle()
|
|
|
|
assert.Error(t, err, "ssh command must fail, we are requesting an invalid path")
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
cmd.connection.User.HomeDir = filepath.Clean(os.TempDir())
|
|
|
|
|
2020-06-13 20:48:51 +00:00
|
|
|
cmd = sshCommand{
|
|
|
|
command: "sftpgo-copy",
|
2020-07-24 21:39:38 +00:00
|
|
|
connection: &connection,
|
2020-06-13 20:48:51 +00:00
|
|
|
args: []string{"src", "dst"},
|
|
|
|
}
|
2020-06-26 21:38:29 +00:00
|
|
|
|
2020-06-13 20:48:51 +00:00
|
|
|
cmd.connection.User.Permissions = make(map[string][]string)
|
|
|
|
cmd.connection.User.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
aDir := filepath.Join(os.TempDir(), "adir")
|
|
|
|
err = os.MkdirAll(aDir, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tmpFile := filepath.Join(aDir, "testcopy")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(tmpFile, []byte("aaa"), os.ModePerm)
|
2020-06-13 20:48:51 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Chmod(aDir, 0001)
|
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
err = cmd.checkCopyDestination(fs, tmpFile)
|
2020-06-13 20:48:51 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
err = os.Chmod(aDir, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(tmpFile)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 21:10:29 +00:00
|
|
|
func TestCommandsWithExtensionsFilter(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
}
|
|
|
|
server, client := net.Pipe()
|
|
|
|
defer server.Close()
|
|
|
|
defer client.Close()
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "test",
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
Status: 1,
|
|
|
|
},
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
user.Filters.FilePatterns = []sdk.PatternsFilter{
|
2020-03-02 09:13:49 +00:00
|
|
|
{
|
2021-05-22 10:28:05 +00:00
|
|
|
Path: "/subdir",
|
|
|
|
AllowedPatterns: []string{".jpg"},
|
|
|
|
DeniedPatterns: []string{},
|
2020-03-01 21:10:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSSH, "", "", user),
|
2020-07-24 21:39:38 +00:00
|
|
|
channel: &mockSSHChannel,
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
|
|
|
cmd := sshCommand{
|
|
|
|
command: "md5sum",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"subdir/test.png"},
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err := cmd.handleHashCommands()
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.EqualError(t, err, common.ErrPermissionDenied.Error())
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2020-03-01 21:10:29 +00:00
|
|
|
cmd = sshCommand{
|
|
|
|
command: "rsync",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"--server", "-vlogDtprze.iLsfxC", ".", "/"},
|
|
|
|
}
|
|
|
|
_, err = cmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, errUnsupportedConfig.Error())
|
|
|
|
|
2020-03-01 21:10:29 +00:00
|
|
|
cmd = sshCommand{
|
|
|
|
command: "git-receive-pack",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"/subdir"},
|
|
|
|
}
|
|
|
|
_, err = cmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, errUnsupportedConfig.Error())
|
|
|
|
|
2020-03-01 21:10:29 +00:00
|
|
|
cmd = sshCommand{
|
|
|
|
command: "git-receive-pack",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"/subdir/dir"},
|
|
|
|
}
|
|
|
|
_, err = cmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, errUnsupportedConfig.Error())
|
|
|
|
|
2020-03-01 21:10:29 +00:00
|
|
|
cmd = sshCommand{
|
|
|
|
command: "git-receive-pack",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"/adir/subdir"},
|
|
|
|
}
|
|
|
|
_, err = cmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2020-03-01 21:10:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
func TestSSHCommandsRemoteFs(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
}
|
|
|
|
user := dataprovider.User{}
|
2021-03-21 18:15:47 +00:00
|
|
|
user.FsConfig = vfs.Filesystem{
|
2021-07-11 13:26:51 +00:00
|
|
|
Provider: sdk.S3FilesystemProvider,
|
2020-05-06 17:36:34 +00:00
|
|
|
S3Config: vfs.S3FsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseS3FsConfig: sdk.BaseS3FsConfig{
|
2021-07-11 13:26:51 +00:00
|
|
|
Bucket: "s3bucket",
|
|
|
|
Endpoint: "endpoint",
|
|
|
|
Region: "eu-west-1",
|
|
|
|
},
|
2020-05-06 17:36:34 +00:00
|
|
|
},
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2020-07-24 21:39:38 +00:00
|
|
|
channel: &mockSSHChannel,
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
cmd := sshCommand{
|
|
|
|
command: "md5sum",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{},
|
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
command, err := cmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
err = cmd.executeSystemCommand(command)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "command must fail for a non local filesystem")
|
2020-06-13 20:48:51 +00:00
|
|
|
cmd = sshCommand{
|
|
|
|
command: "sftpgo-copy",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{},
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = cmd.handleSFTPGoCopy()
|
2020-06-13 20:48:51 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
cmd = sshCommand{
|
|
|
|
command: "sftpgo-remove",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{},
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = cmd.handleSFTPGoRemove()
|
|
|
|
assert.Error(t, err)
|
|
|
|
// the user has no permissions
|
|
|
|
assert.False(t, cmd.hasCopyPermissions("", "", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHCmdGetFsErrors(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
}
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
HomeDir: "relative path",
|
|
|
|
},
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
user.Permissions = map[string][]string{}
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2021-03-21 18:15:47 +00:00
|
|
|
channel: &mockSSHChannel,
|
|
|
|
}
|
|
|
|
cmd := sshCommand{
|
|
|
|
command: "sftpgo-remove",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"path"},
|
|
|
|
}
|
|
|
|
err := cmd.handleSFTPGoRemove()
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
cmd = sshCommand{
|
|
|
|
command: "sftpgo-copy",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"path1", "path2"},
|
|
|
|
}
|
|
|
|
_, _, _, _, _, _, err = cmd.getFsAndCopyPaths() //nolint:dogsled
|
|
|
|
assert.Error(t, err)
|
|
|
|
user = dataprovider.User{}
|
|
|
|
user.HomeDir = filepath.Join(os.TempDir(), "home")
|
|
|
|
user.VirtualFolders = append(connection.User.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: "relative",
|
|
|
|
},
|
|
|
|
VirtualPath: "/vpath",
|
|
|
|
})
|
|
|
|
connection.User = user
|
|
|
|
|
|
|
|
err = os.MkdirAll(user.GetHomeDir(), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cmd = sshCommand{
|
|
|
|
command: "sftpgo-copy",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"path1", "/vpath/path2"},
|
|
|
|
}
|
|
|
|
_, _, _, _, _, _, err = cmd.getFsAndCopyPaths() //nolint:dogsled
|
2020-06-13 20:48:51 +00:00
|
|
|
assert.Error(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
|
|
|
|
err = os.Remove(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 17:54:35 +00:00
|
|
|
func TestGitVirtualFolders(t *testing.T) {
|
|
|
|
permissions := make(map[string][]string)
|
|
|
|
permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Permissions: permissions,
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2020-02-24 17:54:35 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
conn := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2020-02-24 17:54:35 +00:00
|
|
|
}
|
|
|
|
cmd := sshCommand{
|
|
|
|
command: "git-receive-pack",
|
|
|
|
connection: conn,
|
|
|
|
args: []string{"/vdir"},
|
|
|
|
}
|
|
|
|
cmd.connection.User.VirtualFolders = append(cmd.connection.User.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: os.TempDir(),
|
|
|
|
},
|
2020-02-24 17:54:35 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
2021-03-21 18:15:47 +00:00
|
|
|
_, err := cmd.getSystemCommand()
|
2020-06-15 21:32:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
cmd.args = []string{"/"}
|
|
|
|
_, err = cmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, errUnsupportedConfig.Error())
|
2020-06-15 21:32:12 +00:00
|
|
|
cmd.args = []string{"/vdir1"}
|
|
|
|
_, err = cmd.getSystemCommand()
|
|
|
|
assert.NoError(t, err)
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2020-02-24 17:54:35 +00:00
|
|
|
cmd.connection.User.VirtualFolders = nil
|
|
|
|
cmd.connection.User.VirtualFolders = append(cmd.connection.User.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: os.TempDir(),
|
|
|
|
},
|
2020-02-24 17:54:35 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
|
|
|
cmd.args = []string{"/vdir/subdir"}
|
|
|
|
_, err = cmd.getSystemCommand()
|
2020-06-15 21:32:12 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2020-02-24 17:54:35 +00:00
|
|
|
cmd.args = []string{"/adir/subdir"}
|
|
|
|
_, err = cmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2020-02-24 17:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 14:24:56 +00:00
|
|
|
func TestRsyncOptions(t *testing.T) {
|
2019-12-25 17:20:19 +00:00
|
|
|
permissions := make(map[string][]string)
|
|
|
|
permissions["/"] = []string{dataprovider.PermAny}
|
2020-01-19 06:41:05 +00:00
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Permissions: permissions,
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
conn := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2019-11-29 14:24:56 +00:00
|
|
|
}
|
|
|
|
sshCmd := sshCommand{
|
|
|
|
command: "rsync",
|
|
|
|
connection: conn,
|
|
|
|
args: []string{"--server", "-vlogDtprze.iLsfxC", ".", "/"},
|
|
|
|
}
|
|
|
|
cmd, err := sshCmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2021-07-11 13:26:51 +00:00
|
|
|
assert.True(t, util.IsStringInSlice("--safe-links", cmd.cmd.Args),
|
2020-05-06 17:36:34 +00:00
|
|
|
"--safe-links must be added if the user has the create symlinks permission")
|
|
|
|
|
2019-12-25 17:20:19 +00:00
|
|
|
permissions["/"] = []string{dataprovider.PermDownload, dataprovider.PermUpload, dataprovider.PermCreateDirs,
|
|
|
|
dataprovider.PermListItems, dataprovider.PermOverwrite, dataprovider.PermDelete, dataprovider.PermRename}
|
2020-01-19 06:41:05 +00:00
|
|
|
user.Permissions = permissions
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
conn = &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2019-11-29 14:24:56 +00:00
|
|
|
}
|
|
|
|
sshCmd = sshCommand{
|
|
|
|
command: "rsync",
|
|
|
|
connection: conn,
|
|
|
|
args: []string{"--server", "-vlogDtprze.iLsfxC", ".", "/"},
|
|
|
|
}
|
|
|
|
cmd, err = sshCmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2021-07-11 13:26:51 +00:00
|
|
|
assert.True(t, util.IsStringInSlice("--munge-links", cmd.cmd.Args),
|
2020-05-06 17:36:34 +00:00
|
|
|
"--munge-links must be added if the user has the create symlinks permission")
|
|
|
|
|
2020-02-24 17:54:35 +00:00
|
|
|
sshCmd.connection.User.VirtualFolders = append(sshCmd.connection.User.VirtualFolders, vfs.VirtualFolder{
|
2020-06-07 21:30:18 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: os.TempDir(),
|
|
|
|
},
|
2020-02-24 17:54:35 +00:00
|
|
|
VirtualPath: "/vdir",
|
|
|
|
})
|
|
|
|
_, err = sshCmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, errUnsupportedConfig.Error())
|
2019-11-29 14:24:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 21:32:12 +00:00
|
|
|
func TestSystemCommandSizeForPath(t *testing.T) {
|
|
|
|
permissions := make(map[string][]string)
|
|
|
|
permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Permissions: permissions,
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2020-06-15 21:32:12 +00:00
|
|
|
}
|
|
|
|
fs, err := user.GetFilesystem("123")
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
conn := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2020-06-15 21:32:12 +00:00
|
|
|
}
|
|
|
|
sshCmd := sshCommand{
|
|
|
|
command: "rsync",
|
|
|
|
connection: conn,
|
|
|
|
args: []string{"--server", "-vlogDtprze.iLsfxC", ".", "/"},
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
_, _, err = sshCmd.getSizeForPath(fs, "missing path")
|
2020-06-15 21:32:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
testDir := filepath.Join(os.TempDir(), "dir")
|
|
|
|
err = os.MkdirAll(testDir, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
testFile := filepath.Join(testDir, "testfile")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testFile, []byte("test content"), os.ModePerm)
|
2020-06-15 21:32:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Symlink(testFile, testFile+".link")
|
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
numFiles, size, err := sshCmd.getSizeForPath(fs, testFile+".link")
|
2020-06-15 21:32:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, numFiles)
|
|
|
|
assert.Equal(t, int64(0), size)
|
2021-03-21 18:15:47 +00:00
|
|
|
numFiles, size, err = sshCmd.getSizeForPath(fs, testFile)
|
2020-06-15 21:32:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, numFiles)
|
|
|
|
assert.Equal(t, int64(12), size)
|
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
err = os.Chmod(testDir, 0001)
|
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
_, _, err = sshCmd.getSizeForPath(fs, testFile)
|
2020-06-15 21:32:12 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
err = os.Chmod(testDir, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(testDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
func TestSystemCommandErrors(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
readErr := fmt.Errorf("test read error")
|
|
|
|
writeErr := fmt.Errorf("test write error")
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
permissions := make(map[string][]string)
|
|
|
|
permissions["/"] = []string{dataprovider.PermAny}
|
2020-06-15 21:32:12 +00:00
|
|
|
homeDir := filepath.Join(os.TempDir(), "adir")
|
|
|
|
err := os.MkdirAll(homeDir, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(filepath.Join(homeDir, "afile"), []byte("content"), os.ModePerm)
|
2020-06-15 21:32:12 +00:00
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Permissions: permissions,
|
|
|
|
HomeDir: homeDir,
|
|
|
|
},
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
fs, err := user.GetFilesystem("123")
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2020-07-24 21:39:38 +00:00
|
|
|
channel: &mockSSHChannel,
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
2020-06-15 21:32:12 +00:00
|
|
|
var sshCmd sshCommand
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
sshCmd = sshCommand{
|
|
|
|
command: "dir",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"/"},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sshCmd = sshCommand{
|
|
|
|
command: "ls",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"/"},
|
|
|
|
}
|
2019-11-26 21:26:42 +00:00
|
|
|
}
|
|
|
|
systemCmd, err := sshCmd.getSystemCommand()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
systemCmd.cmd.Dir = os.TempDir()
|
2020-06-15 21:32:12 +00:00
|
|
|
// FIXME: the command completes but the fake client is unable to read the response
|
2020-05-06 17:36:34 +00:00
|
|
|
// no error is reported in this case. We can see that the expected code is executed
|
|
|
|
// reading the test coverage
|
|
|
|
sshCmd.executeSystemCommand(systemCmd) //nolint:errcheck
|
2019-11-26 21:26:42 +00:00
|
|
|
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
|
|
|
sshCmd.connection.channel = &mockSSHChannel
|
2022-01-20 17:19:20 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(nil, sshCmd.connection.BaseConnection, nil, "", "", "",
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferUpload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer := newTransfer(baseTransfer, nil, nil, nil)
|
2019-11-26 21:26:42 +00:00
|
|
|
destBuff := make([]byte, 65535)
|
|
|
|
dst := bytes.NewBuffer(destBuff)
|
2020-06-18 20:38:03 +00:00
|
|
|
_, err = transfer.copyFromReaderToWriter(dst, sshCmd.connection.channel)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, readErr.Error())
|
2019-11-26 21:26:42 +00:00
|
|
|
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
|
|
|
sshCmd.connection.channel = &mockSSHChannel
|
2020-08-22 08:12:00 +00:00
|
|
|
transfer.MaxWriteSize = 1
|
2020-06-18 20:38:03 +00:00
|
|
|
_, err = transfer.copyFromReaderToWriter(dst, sshCmd.connection.channel)
|
2022-01-30 10:42:36 +00:00
|
|
|
assert.True(t, transfer.Connection.IsQuotaExceededError(err))
|
2019-11-26 21:26:42 +00:00
|
|
|
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: nil,
|
|
|
|
ShortWriteErr: true,
|
|
|
|
}
|
|
|
|
sshCmd.connection.channel = &mockSSHChannel
|
2020-06-18 20:38:03 +00:00
|
|
|
_, err = transfer.copyFromReaderToWriter(sshCmd.connection.channel, dst)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, io.ErrShortWrite.Error())
|
2020-08-22 08:12:00 +00:00
|
|
|
transfer.MaxWriteSize = -1
|
2020-06-18 20:38:03 +00:00
|
|
|
_, err = transfer.copyFromReaderToWriter(sshCmd.connection.channel, dst)
|
2022-01-30 10:42:36 +00:00
|
|
|
assert.True(t, transfer.Connection.IsQuotaExceededError(err))
|
|
|
|
|
|
|
|
baseTransfer = common.NewBaseTransfer(nil, sshCmd.connection.BaseConnection, nil, "", "", "",
|
|
|
|
common.TransferDownload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{
|
|
|
|
AllowedDLSize: 1,
|
|
|
|
})
|
|
|
|
transfer = newTransfer(baseTransfer, nil, nil, nil)
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
|
|
|
sshCmd.connection.channel = &mockSSHChannel
|
|
|
|
_, err = transfer.copyFromReaderToWriter(dst, sshCmd.connection.channel)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), common.ErrReadQuotaExceeded.Error())
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:32:12 +00:00
|
|
|
err = os.RemoveAll(homeDir)
|
|
|
|
assert.NoError(t, err)
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func TestCommandGetFsError(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
|
|
|
FsConfig: vfs.Filesystem{
|
2021-07-11 13:26:51 +00:00
|
|
|
Provider: sdk.CryptedFilesystemProvider,
|
2021-03-21 18:15:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
conn := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
sshCmd := sshCommand{
|
|
|
|
command: "rsync",
|
|
|
|
connection: conn,
|
|
|
|
args: []string{"--server", "-vlogDtprze.iLsfxC", ".", "/"},
|
|
|
|
}
|
|
|
|
_, err := sshCmd.getSystemCommand()
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
}
|
|
|
|
conn = &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", user),
|
2021-03-21 18:15:47 +00:00
|
|
|
channel: &mockSSHChannel,
|
|
|
|
}
|
|
|
|
scpCommand := scpCommand{
|
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: conn,
|
|
|
|
args: []string{"-t", "/tmp"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scpCommand.handleRecursiveUpload()
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = scpCommand.handleDownload("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
func TestGetConnectionInfo(t *testing.T) {
|
2020-07-24 21:39:38 +00:00
|
|
|
c := common.ConnectionStatus{
|
2019-11-18 22:30:37 +00:00
|
|
|
Username: "test_user",
|
|
|
|
ConnectionID: "123",
|
|
|
|
ClientVersion: "client",
|
|
|
|
RemoteAddress: "127.0.0.1:1234",
|
2020-07-24 21:39:38 +00:00
|
|
|
Protocol: common.ProtocolSSH,
|
2020-08-11 21:56:10 +00:00
|
|
|
Command: "sha1sum /test_file_ftp.dat",
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
info := c.GetConnectionInfo()
|
2020-08-11 21:56:10 +00:00
|
|
|
assert.Contains(t, info, "sha1sum /test_file_ftp.dat")
|
2019-11-18 22:30:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestSCPFileMode(t *testing.T) {
|
|
|
|
mode := getFileModeAsString(0, true)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "0755", mode)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
mode = getFileModeAsString(0700, true)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "0700", mode)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
mode = getFileModeAsString(0750, true)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "0750", mode)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
mode = getFileModeAsString(0777, true)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "0777", mode)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
mode = getFileModeAsString(0640, false)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "0640", mode)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
mode = getFileModeAsString(0600, false)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "0600", mode)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
mode = getFileModeAsString(0, false)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "0644", mode)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
fileMode := uint32(0777)
|
|
|
|
fileMode = fileMode | uint32(os.ModeSetgid)
|
|
|
|
fileMode = fileMode | uint32(os.ModeSetuid)
|
|
|
|
fileMode = fileMode | uint32(os.ModeSticky)
|
|
|
|
mode = getFileModeAsString(os.FileMode(fileMode), false)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "7777", mode)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
fileMode = uint32(0644)
|
|
|
|
fileMode = fileMode | uint32(os.ModeSetgid)
|
|
|
|
mode = getFileModeAsString(os.FileMode(fileMode), false)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "4644", mode)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
fileMode = uint32(0600)
|
|
|
|
fileMode = fileMode | uint32(os.ModeSetuid)
|
|
|
|
mode = getFileModeAsString(os.FileMode(fileMode), false)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "2600", mode)
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
fileMode = uint32(0044)
|
|
|
|
fileMode = fileMode | uint32(os.ModeSticky)
|
|
|
|
mode = getFileModeAsString(os.FileMode(fileMode), false)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Equal(t, "1044", mode)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:37:59 +00:00
|
|
|
func TestSCPUploadError(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
writeErr := fmt.Errorf("test write error")
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
HomeDir: filepath.Join(os.TempDir()),
|
|
|
|
Permissions: make(map[string][]string),
|
|
|
|
},
|
2021-01-20 21:37:59 +00:00
|
|
|
}
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2021-01-20 21:37:59 +00:00
|
|
|
channel: &mockSSHChannel,
|
|
|
|
}
|
|
|
|
scpCommand := scpCommand{
|
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-t", "/"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := scpCommand.handle()
|
|
|
|
assert.EqualError(t, err, writeErr.Error())
|
|
|
|
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer([]byte("D0755 0 testdir\n")),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
|
|
|
err = scpCommand.handleRecursiveUpload()
|
|
|
|
assert.EqualError(t, err, writeErr.Error())
|
|
|
|
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer([]byte("D0755 a testdir\n")),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
|
|
|
err = scpCommand.handleRecursiveUpload()
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPInvalidEndDir(t *testing.T) {
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer([]byte("E\n")),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
}
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2021-03-21 18:15:47 +00:00
|
|
|
}),
|
|
|
|
channel: &mockSSHChannel,
|
2021-01-20 21:37:59 +00:00
|
|
|
}
|
|
|
|
scpCommand := scpCommand{
|
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-t", "/tmp"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := scpCommand.handleRecursiveUpload()
|
|
|
|
assert.EqualError(t, err, "unacceptable end dir command")
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
func TestSCPParseUploadMessage(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2021-03-21 18:15:47 +00:00
|
|
|
}),
|
|
|
|
channel: &mockSSHChannel,
|
2019-09-11 14:29:56 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand := scpCommand{
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-t", "/tmp"},
|
|
|
|
},
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
_, _, err := scpCommand.parseUploadMessage(fs, "invalid")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "parsing invalid upload message must fail")
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
_, _, err = scpCommand.parseUploadMessage(fs, "D0755 0")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "parsing incomplete upload message must fail")
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
_, _, err = scpCommand.parseUploadMessage(fs, "D0755 invalidsize testdir")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "parsing upload message with invalid size must fail")
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
_, _, err = scpCommand.parseUploadMessage(fs, "D0755 0 ")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "parsing upload message with invalid name must fail")
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPProtocolMessages(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
readErr := fmt.Errorf("test read error")
|
|
|
|
writeErr := fmt.Errorf("test write error")
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", dataprovider.User{}),
|
2020-07-24 21:39:38 +00:00
|
|
|
channel: &mockSSHChannel,
|
2019-09-11 14:29:56 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand := scpCommand{
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-t", "/tmp"},
|
|
|
|
},
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
_, err := scpCommand.readProtocolMessage()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, readErr.Error())
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
err = scpCommand.sendConfirmationMessage()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, writeErr.Error())
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
err = scpCommand.sendProtocolMessage("E\n")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, writeErr.Error())
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
_, err = scpCommand.getNextUploadProtocolMessage()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, readErr.Error())
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer([]byte("T1183832947 0 1183833773 0\n")),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
2019-09-11 14:29:56 +00:00
|
|
|
scpCommand.connection.channel = &mockSSHChannel
|
2019-08-24 12:41:15 +00:00
|
|
|
_, err = scpCommand.getNextUploadProtocolMessage()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, writeErr.Error())
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
respBuffer := []byte{0x02}
|
|
|
|
protocolErrorMsg := "protocol error msg"
|
|
|
|
respBuffer = append(respBuffer, protocolErrorMsg...)
|
|
|
|
respBuffer = append(respBuffer, 0x0A)
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(respBuffer),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
2019-09-11 14:29:56 +00:00
|
|
|
scpCommand.connection.channel = &mockSSHChannel
|
2019-08-24 12:41:15 +00:00
|
|
|
err = scpCommand.readConfirmationMessage()
|
2020-05-06 17:36:34 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Equal(t, protocolErrorMsg, err.Error())
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPTestDownloadProtocolMessages(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
readErr := fmt.Errorf("test read error")
|
|
|
|
writeErr := fmt.Errorf("test write error")
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", dataprovider.User{}),
|
2020-07-24 21:39:38 +00:00
|
|
|
channel: &mockSSHChannel,
|
2019-09-11 14:29:56 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand := scpCommand{
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-f", "-p", "/tmp"},
|
|
|
|
},
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
path := "testDir"
|
2020-06-08 17:40:17 +00:00
|
|
|
err := os.Mkdir(path, os.ModePerm)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = scpCommand.sendDownloadProtocolMessages(path, stat)
|
|
|
|
assert.EqualError(t, err, writeErr.Error())
|
2019-08-24 12:41:15 +00:00
|
|
|
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scpCommand.sendDownloadProtocolMessages(path, stat)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, readErr.Error())
|
2019-08-24 12:41:15 +00:00
|
|
|
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
|
|
|
scpCommand.args = []string{"-f", "/tmp"}
|
2019-09-11 14:29:56 +00:00
|
|
|
scpCommand.connection.channel = &mockSSHChannel
|
2019-08-24 12:41:15 +00:00
|
|
|
err = scpCommand.sendDownloadProtocolMessages(path, stat)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, writeErr.Error())
|
2019-08-24 12:41:15 +00:00
|
|
|
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
2019-09-11 14:29:56 +00:00
|
|
|
scpCommand.connection.channel = &mockSSHChannel
|
2019-08-24 12:41:15 +00:00
|
|
|
err = scpCommand.sendDownloadProtocolMessages(path, stat)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, readErr.Error())
|
|
|
|
|
|
|
|
err = os.Remove(path)
|
|
|
|
assert.NoError(t, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPCommandHandleErrors(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
readErr := fmt.Errorf("test read error")
|
|
|
|
writeErr := fmt.Errorf("test write error")
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
2019-10-10 07:04:17 +00:00
|
|
|
server, client := net.Pipe()
|
2020-05-06 17:36:34 +00:00
|
|
|
defer func() {
|
|
|
|
err := server.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := client.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", dataprovider.User{}),
|
2020-07-24 21:39:38 +00:00
|
|
|
channel: &mockSSHChannel,
|
2019-09-11 14:29:56 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand := scpCommand{
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-f", "/tmp"},
|
|
|
|
},
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
err := scpCommand.handle()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, readErr.Error())
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand.args = []string{"-i", "/tmp"}
|
|
|
|
err = scpCommand.handle()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "invalid scp command must fail")
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
func TestSCPErrorsMockFs(t *testing.T) {
|
|
|
|
errFake := errors.New("fake error")
|
|
|
|
u := dataprovider.User{}
|
|
|
|
u.Username = "test"
|
|
|
|
u.Permissions = make(map[string][]string)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
u.HomeDir = os.TempDir()
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
}
|
|
|
|
server, client := net.Pipe()
|
2020-05-06 17:36:34 +00:00
|
|
|
defer func() {
|
|
|
|
err := server.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := client.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
|
|
|
channel: &mockSSHChannel,
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", u),
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
scpCommand := scpCommand{
|
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-r", "-t", "/tmp"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testfile := filepath.Join(u.HomeDir, "testfile")
|
2021-03-21 18:15:47 +00:00
|
|
|
err := os.WriteFile(testfile, []byte("test"), os.ModePerm)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := newMockOsFs(errFake, nil, true, "123", os.TempDir())
|
|
|
|
err = scpCommand.handleUploadFile(fs, testfile, testfile, 0, false, 4, "/testfile")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(testfile)
|
|
|
|
assert.NoError(t, err)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 20:44:01 +00:00
|
|
|
func TestSCPRecursiveDownloadErrors(t *testing.T) {
|
2019-08-24 12:41:15 +00:00
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
readErr := fmt.Errorf("test read error")
|
|
|
|
writeErr := fmt.Errorf("test write error")
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
2019-10-10 07:04:17 +00:00
|
|
|
server, client := net.Pipe()
|
2020-05-06 17:36:34 +00:00
|
|
|
defer func() {
|
|
|
|
err := server.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err := client.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("123", os.TempDir(), "")
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2021-03-21 18:15:47 +00:00
|
|
|
}),
|
|
|
|
channel: &mockSSHChannel,
|
2019-09-11 14:29:56 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand := scpCommand{
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-r", "-f", "/tmp"},
|
|
|
|
},
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
path := "testDir"
|
2020-06-08 17:40:17 +00:00
|
|
|
err := os.Mkdir(path, os.ModePerm)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
err = scpCommand.handleRecursiveDownload(fs, "invalid_dir", "invalid_dir", stat)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, writeErr.Error())
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
2019-09-11 14:29:56 +00:00
|
|
|
scpCommand.connection.channel = &mockSSHChannel
|
2021-03-21 18:15:47 +00:00
|
|
|
err = scpCommand.handleRecursiveDownload(fs, "invalid_dir", "invalid_dir", stat)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "recursive upload download must fail for a non existing dir")
|
2019-08-24 12:41:15 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
err = os.Remove(path)
|
|
|
|
assert.NoError(t, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 20:44:01 +00:00
|
|
|
func TestSCPRecursiveUploadErrors(t *testing.T) {
|
2019-08-24 12:41:15 +00:00
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
readErr := fmt.Errorf("test read error")
|
|
|
|
writeErr := fmt.Errorf("test write error")
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", dataprovider.User{}),
|
2020-07-24 21:39:38 +00:00
|
|
|
channel: &mockSSHChannel,
|
2019-09-11 14:29:56 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand := scpCommand{
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-r", "-t", "/tmp"},
|
|
|
|
},
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
err := scpCommand.handleRecursiveUpload()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "recursive upload must fail, we send a fake error message")
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
2019-09-11 14:29:56 +00:00
|
|
|
scpCommand.connection.channel = &mockSSHChannel
|
2019-08-24 12:41:15 +00:00
|
|
|
err = scpCommand.handleRecursiveUpload()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "recursive upload must fail, we send a fake error message")
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPCreateDirs(t *testing.T) {
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
u := dataprovider.User{}
|
|
|
|
u.HomeDir = "home_rel_path"
|
|
|
|
u.Username = "test"
|
2019-12-25 17:20:19 +00:00
|
|
|
u.Permissions = make(map[string][]string)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
2019-08-24 12:41:15 +00:00
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
fs, err := u.GetFilesystem("123")
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", u),
|
2020-07-24 21:39:38 +00:00
|
|
|
channel: &mockSSHChannel,
|
2019-09-11 14:29:56 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand := scpCommand{
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-r", "-t", "/tmp"},
|
|
|
|
},
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = scpCommand.handleCreateDir(fs, "invalid_dir")
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "create invalid dir must fail")
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPDownloadFileData(t *testing.T) {
|
|
|
|
testfile := "testfile"
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
readErr := fmt.Errorf("test read error")
|
|
|
|
writeErr := fmt.Errorf("test write error")
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
mockSSHChannelReadErr := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
|
|
|
mockSSHChannelWriteErr := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", dataprovider.User{BaseUser: sdk.BaseUser{HomeDir: os.TempDir()}}),
|
2021-03-21 18:15:47 +00:00
|
|
|
channel: &mockSSHChannelReadErr,
|
2019-09-11 14:29:56 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand := scpCommand{
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-r", "-f", "/tmp"},
|
|
|
|
},
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2021-02-25 20:53:04 +00:00
|
|
|
err := os.WriteFile(testfile, []byte("test"), os.ModePerm)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
stat, err := os.Stat(testfile)
|
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
err = scpCommand.sendDownloadFileData(fs, testfile, stat, nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, readErr.Error())
|
|
|
|
|
2019-09-11 14:29:56 +00:00
|
|
|
scpCommand.connection.channel = &mockSSHChannelWriteErr
|
2021-03-21 18:15:47 +00:00
|
|
|
err = scpCommand.sendDownloadFileData(fs, testfile, stat, nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, writeErr.Error())
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand.args = []string{"-r", "-p", "-f", "/tmp"}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = scpCommand.sendDownloadFileData(fs, testfile, stat, nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, writeErr.Error())
|
|
|
|
|
2019-09-11 14:29:56 +00:00
|
|
|
scpCommand.connection.channel = &mockSSHChannelReadErr
|
2021-03-21 18:15:47 +00:00
|
|
|
err = scpCommand.sendDownloadFileData(fs, testfile, stat, nil)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.EqualError(t, err, readErr.Error())
|
|
|
|
|
|
|
|
err = os.Remove(testfile)
|
|
|
|
assert.NoError(t, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPUploadFiledata(t *testing.T) {
|
|
|
|
testfile := "testfile"
|
|
|
|
buf := make([]byte, 65535)
|
|
|
|
stdErrBuf := make([]byte, 65535)
|
|
|
|
readErr := fmt.Errorf("test read error")
|
|
|
|
writeErr := fmt.Errorf("test write error")
|
|
|
|
mockSSHChannel := MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: writeErr,
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "testuser",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", user),
|
2020-07-24 21:39:38 +00:00
|
|
|
channel: &mockSSHChannel,
|
2019-09-11 14:29:56 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
scpCommand := scpCommand{
|
2019-11-18 22:30:37 +00:00
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
args: []string{"-r", "-t", "/tmp"},
|
|
|
|
},
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
file, err := os.Create(testfile)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, scpCommand.connection.BaseConnection, nil, file.Name(), file.Name(),
|
2022-01-30 10:42:36 +00:00
|
|
|
"/"+testfile, common.TransferDownload, 0, 0, 0, 0, true, fs, dataprovider.TransferQuota{})
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer := newTransfer(baseTransfer, nil, nil, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
err = scpCommand.getUploadFileData(2, transfer)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "upload must fail, we send a fake write error message")
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: readErr,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
2019-09-11 14:29:56 +00:00
|
|
|
scpCommand.connection.channel = &mockSSHChannel
|
2020-05-06 17:36:34 +00:00
|
|
|
file, err = os.Create(testfile)
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.File = file
|
2020-01-12 07:25:08 +00:00
|
|
|
transfer.isFinished = false
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.Connection.AddTransfer(transfer)
|
|
|
|
err = scpCommand.getUploadFileData(2, transfer)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "upload must fail, we send a fake read error message")
|
2019-08-24 12:41:15 +00:00
|
|
|
|
|
|
|
respBuffer := []byte("12")
|
|
|
|
respBuffer = append(respBuffer, 0x02)
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(respBuffer),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
2019-09-11 14:29:56 +00:00
|
|
|
scpCommand.connection.channel = &mockSSHChannel
|
2020-05-06 17:36:34 +00:00
|
|
|
file, err = os.Create(testfile)
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
baseTransfer.File = file
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer = newTransfer(baseTransfer, nil, nil, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.Connection.AddTransfer(transfer)
|
|
|
|
err = scpCommand.getUploadFileData(2, transfer)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.Error(t, err, "upload must fail, we have not enough data to read")
|
2019-08-24 12:41:15 +00:00
|
|
|
|
|
|
|
// the file is already closed so we have an error on trasfer closing
|
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
transfer.Connection.AddTransfer(transfer)
|
|
|
|
err = scpCommand.getUploadFileData(0, transfer)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualError(t, err, common.ErrTransferClosed.Error())
|
|
|
|
}
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2020-02-19 10:26:40 +00:00
|
|
|
mockSSHChannel = MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
ReadError: nil,
|
|
|
|
WriteError: nil,
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
transfer.Connection.AddTransfer(transfer)
|
|
|
|
err = scpCommand.getUploadFileData(2, transfer)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.True(t, errors.Is(err, os.ErrClosed))
|
|
|
|
|
2020-01-12 07:25:08 +00:00
|
|
|
err = os.Remove(testfile)
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NoError(t, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2019-09-10 16:47:21 +00:00
|
|
|
|
|
|
|
func TestUploadError(t *testing.T) {
|
2020-07-24 21:39:38 +00:00
|
|
|
common.Config.UploadMode = common.UploadModeAtomic
|
|
|
|
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "testuser",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2020-07-24 21:39:38 +00:00
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSCP, "", "", user),
|
2019-09-10 16:47:21 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2019-09-10 16:47:21 +00:00
|
|
|
testfile := "testfile"
|
|
|
|
fileTempName := "temptestfile"
|
2020-05-06 17:36:34 +00:00
|
|
|
file, err := os.Create(fileTempName)
|
|
|
|
assert.NoError(t, err)
|
2021-05-31 19:45:29 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(file, connection.BaseConnection, nil, testfile, file.Name(),
|
2022-01-30 10:42:36 +00:00
|
|
|
testfile, common.TransferUpload, 0, 0, 0, 0, true, fs, dataprovider.TransferQuota{})
|
2020-08-31 04:45:22 +00:00
|
|
|
transfer := newTransfer(baseTransfer, nil, nil, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2020-01-12 07:25:08 +00:00
|
|
|
errFake := errors.New("fake error")
|
|
|
|
transfer.TransferError(errFake)
|
2020-05-06 17:36:34 +00:00
|
|
|
err = transfer.Close()
|
2020-07-24 21:39:38 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualError(t, err, common.ErrGenericFailure.Error())
|
|
|
|
}
|
|
|
|
if assert.Error(t, transfer.ErrTransfer) {
|
|
|
|
assert.EqualError(t, transfer.ErrTransfer, errFake.Error())
|
|
|
|
}
|
|
|
|
assert.Equal(t, int64(0), transfer.BytesReceived)
|
2020-05-06 17:36:34 +00:00
|
|
|
|
|
|
|
assert.NoFileExists(t, testfile)
|
|
|
|
assert.NoFileExists(t, fileTempName)
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
common.Config.UploadMode = common.UploadModeAtomicWithResume
|
2019-09-10 16:47:21 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
|
2020-08-31 04:45:22 +00:00
|
|
|
func TestTransferFailingReader(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "testuser",
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2021-03-21 18:15:47 +00:00
|
|
|
FsConfig: vfs.Filesystem{
|
2021-07-11 13:26:51 +00:00
|
|
|
Provider: sdk.CryptedFilesystemProvider,
|
2021-03-21 18:15:47 +00:00
|
|
|
CryptConfig: vfs.CryptFsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
Passphrase: kms.NewPlainSecret("crypt secret"),
|
2021-03-21 18:15:47 +00:00
|
|
|
},
|
|
|
|
},
|
2020-08-31 04:45:22 +00:00
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
|
|
|
|
fs := newMockOsFs(nil, nil, true, "", os.TempDir())
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2020-08-31 04:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
request := sftp.NewRequest("Open", "afile.txt")
|
|
|
|
request.Flags = 27 // read,write,create,truncate
|
|
|
|
|
|
|
|
transfer, err := connection.handleFilewrite(request)
|
|
|
|
require.NoError(t, err)
|
|
|
|
buf := make([]byte, 32)
|
|
|
|
_, err = transfer.ReadAt(buf, 0)
|
|
|
|
assert.EqualError(t, err, sftp.ErrSSHFxOpUnsupported.Error())
|
|
|
|
if c, ok := transfer.(io.Closer); ok {
|
|
|
|
err = c.Close()
|
2020-08-31 17:25:17 +00:00
|
|
|
assert.NoError(t, err)
|
2020-08-31 04:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fsPath := filepath.Join(os.TempDir(), "afile.txt")
|
|
|
|
|
|
|
|
r, _, err := pipeat.Pipe()
|
|
|
|
assert.NoError(t, err)
|
2022-01-20 17:19:20 +00:00
|
|
|
baseTransfer := common.NewBaseTransfer(nil, connection.BaseConnection, nil, fsPath, fsPath, filepath.Base(fsPath),
|
2022-01-30 10:42:36 +00:00
|
|
|
common.TransferUpload, 0, 0, 0, 0, false, fs, dataprovider.TransferQuota{})
|
2020-08-31 04:45:22 +00:00
|
|
|
errRead := errors.New("read is not allowed")
|
|
|
|
tr := newTransfer(baseTransfer, nil, r, errRead)
|
|
|
|
_, err = tr.ReadAt(buf, 0)
|
|
|
|
assert.EqualError(t, err, errRead.Error())
|
|
|
|
|
|
|
|
err = tr.Close()
|
2020-08-31 17:25:17 +00:00
|
|
|
assert.NoError(t, err)
|
2020-08-31 04:45:22 +00:00
|
|
|
|
2020-11-09 20:01:56 +00:00
|
|
|
tr = newTransfer(baseTransfer, nil, nil, errRead)
|
|
|
|
_, err = tr.ReadAt(buf, 0)
|
|
|
|
assert.EqualError(t, err, errRead.Error())
|
|
|
|
|
|
|
|
err = tr.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-08-31 04:45:22 +00:00
|
|
|
err = os.Remove(fsPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, connection.GetTransfers(), 0)
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
func TestConnectionStatusStruct(t *testing.T) {
|
2020-07-24 21:39:38 +00:00
|
|
|
var transfers []common.ConnectionTransfer
|
|
|
|
transferUL := common.ConnectionTransfer{
|
|
|
|
OperationType: "upload",
|
2021-07-11 13:26:51 +00:00
|
|
|
StartTime: util.GetTimeAsMsSinceEpoch(time.Now()),
|
2019-10-07 16:19:01 +00:00
|
|
|
Size: 123,
|
2020-07-24 21:39:38 +00:00
|
|
|
VirtualPath: "/test.upload",
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
transferDL := common.ConnectionTransfer{
|
|
|
|
OperationType: "download",
|
2021-07-11 13:26:51 +00:00
|
|
|
StartTime: util.GetTimeAsMsSinceEpoch(time.Now()),
|
2019-10-07 16:19:01 +00:00
|
|
|
Size: 123,
|
2020-07-24 21:39:38 +00:00
|
|
|
VirtualPath: "/test.download",
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
transfers = append(transfers, transferUL)
|
|
|
|
transfers = append(transfers, transferDL)
|
2020-07-24 21:39:38 +00:00
|
|
|
c := common.ConnectionStatus{
|
2019-10-07 16:19:01 +00:00
|
|
|
Username: "test",
|
|
|
|
ConnectionID: "123",
|
|
|
|
ClientVersion: "fakeClient-1.0.0",
|
|
|
|
RemoteAddress: "127.0.0.1:1234",
|
2021-07-11 13:26:51 +00:00
|
|
|
ConnectionTime: util.GetTimeAsMsSinceEpoch(time.Now()),
|
|
|
|
LastActivity: util.GetTimeAsMsSinceEpoch(time.Now()),
|
2019-10-07 16:19:01 +00:00
|
|
|
Protocol: "SFTP",
|
|
|
|
Transfers: transfers,
|
|
|
|
}
|
|
|
|
durationString := c.GetConnectionDuration()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NotEqual(t, 0, len(durationString))
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
transfersString := c.GetTransfersAsString()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NotEqual(t, 0, len(transfersString))
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
connInfo := c.GetConnectionInfo()
|
2020-05-06 17:36:34 +00:00
|
|
|
assert.NotEqual(t, 0, len(connInfo))
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
2019-11-12 06:37:47 +00:00
|
|
|
|
2022-01-06 17:09:49 +00:00
|
|
|
func TestSupportedSecurityOptions(t *testing.T) {
|
|
|
|
c := Configuration{
|
|
|
|
KexAlgorithms: supportedKexAlgos,
|
|
|
|
MACs: supportedMACs,
|
|
|
|
Ciphers: supportedCiphers,
|
|
|
|
}
|
|
|
|
serverConfig := &ssh.ServerConfig{}
|
|
|
|
err := c.configureSecurityOptions(serverConfig)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, supportedCiphers, serverConfig.Ciphers)
|
|
|
|
assert.Equal(t, supportedMACs, serverConfig.MACs)
|
|
|
|
assert.Equal(t, supportedKexAlgos, serverConfig.KeyExchanges)
|
|
|
|
c.KexAlgorithms = append(c.KexAlgorithms, "not a kex")
|
|
|
|
err = c.configureSecurityOptions(serverConfig)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2020-05-15 18:08:53 +00:00
|
|
|
func TestLoadHostKeys(t *testing.T) {
|
2020-06-08 16:45:04 +00:00
|
|
|
configDir := ".."
|
|
|
|
serverConfig := &ssh.ServerConfig{}
|
2020-05-15 18:08:53 +00:00
|
|
|
c := Configuration{}
|
2020-05-16 21:26:44 +00:00
|
|
|
c.HostKeys = []string{".", "missing file"}
|
2020-06-08 16:45:04 +00:00
|
|
|
err := c.checkAndLoadHostKeys(configDir, serverConfig)
|
2020-05-15 18:08:53 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
testfile := filepath.Join(os.TempDir(), "invalidkey")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testfile, []byte("some bytes"), os.ModePerm)
|
2020-05-15 18:08:53 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-16 21:26:44 +00:00
|
|
|
c.HostKeys = []string{testfile}
|
2020-06-08 16:45:04 +00:00
|
|
|
err = c.checkAndLoadHostKeys(configDir, serverConfig)
|
2020-05-15 18:08:53 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
err = os.Remove(testfile)
|
|
|
|
assert.NoError(t, err)
|
2020-06-08 16:45:04 +00:00
|
|
|
keysDir := filepath.Join(os.TempDir(), "keys")
|
2020-06-08 17:40:17 +00:00
|
|
|
err = os.MkdirAll(keysDir, os.ModePerm)
|
2020-06-08 16:45:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
rsaKeyName := filepath.Join(keysDir, defaultPrivateRSAKeyName)
|
|
|
|
ecdsaKeyName := filepath.Join(keysDir, defaultPrivateECDSAKeyName)
|
2020-10-19 12:30:40 +00:00
|
|
|
ed25519KeyName := filepath.Join(keysDir, defaultPrivateEd25519KeyName)
|
2020-06-08 16:45:04 +00:00
|
|
|
nonDefaultKeyName := filepath.Join(keysDir, "akey")
|
2020-10-19 12:30:40 +00:00
|
|
|
c.HostKeys = []string{nonDefaultKeyName, rsaKeyName, ecdsaKeyName, ed25519KeyName}
|
2020-06-08 16:45:04 +00:00
|
|
|
err = c.checkAndLoadHostKeys(configDir, serverConfig)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.FileExists(t, rsaKeyName)
|
|
|
|
assert.FileExists(t, ecdsaKeyName)
|
2020-10-19 12:30:40 +00:00
|
|
|
assert.FileExists(t, ed25519KeyName)
|
2020-06-08 16:45:04 +00:00
|
|
|
assert.NoFileExists(t, nonDefaultKeyName)
|
|
|
|
err = os.Remove(rsaKeyName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(ecdsaKeyName)
|
|
|
|
assert.NoError(t, err)
|
2020-10-19 12:30:40 +00:00
|
|
|
err = os.Remove(ed25519KeyName)
|
|
|
|
assert.NoError(t, err)
|
2020-06-08 16:45:04 +00:00
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
err = os.Chmod(keysDir, 0551)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
c.HostKeys = nil
|
|
|
|
err = c.checkAndLoadHostKeys(keysDir, serverConfig)
|
|
|
|
assert.Error(t, err)
|
|
|
|
c.HostKeys = []string{rsaKeyName, ecdsaKeyName}
|
|
|
|
err = c.checkAndLoadHostKeys(configDir, serverConfig)
|
|
|
|
assert.Error(t, err)
|
|
|
|
c.HostKeys = []string{ecdsaKeyName, rsaKeyName}
|
|
|
|
err = c.checkAndLoadHostKeys(configDir, serverConfig)
|
|
|
|
assert.Error(t, err)
|
2020-10-19 12:30:40 +00:00
|
|
|
c.HostKeys = []string{ed25519KeyName}
|
|
|
|
err = c.checkAndLoadHostKeys(configDir, serverConfig)
|
|
|
|
assert.Error(t, err)
|
2020-06-08 16:45:04 +00:00
|
|
|
err = os.Chmod(keysDir, 0755)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(keysDir)
|
|
|
|
assert.NoError(t, err)
|
2020-05-15 18:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCertCheckerInitErrors(t *testing.T) {
|
|
|
|
c := Configuration{}
|
2020-05-16 13:15:32 +00:00
|
|
|
c.TrustedUserCAKeys = []string{".", "missing file"}
|
2020-05-15 18:08:53 +00:00
|
|
|
err := c.initializeCertChecker("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
testfile := filepath.Join(os.TempDir(), "invalidkey")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testfile, []byte("some bytes"), os.ModePerm)
|
2020-05-15 18:08:53 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
c.TrustedUserCAKeys = []string{testfile}
|
|
|
|
err = c.initializeCertChecker("")
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = os.Remove(testfile)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
|
2020-06-26 21:38:29 +00:00
|
|
|
func TestRecursiveCopyErrors(t *testing.T) {
|
|
|
|
permissions := make(map[string][]string)
|
|
|
|
permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Permissions: permissions,
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2020-06-26 21:38:29 +00:00
|
|
|
}
|
|
|
|
fs, err := user.GetFilesystem("123")
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
conn := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection("", common.ProtocolSFTP, "", "", user),
|
2020-06-26 21:38:29 +00:00
|
|
|
}
|
|
|
|
sshCmd := sshCommand{
|
|
|
|
command: "sftpgo-copy",
|
|
|
|
connection: conn,
|
|
|
|
args: []string{"adir", "another"},
|
|
|
|
}
|
|
|
|
// try to copy a missing directory
|
2021-03-21 18:15:47 +00:00
|
|
|
err = sshCmd.checkRecursiveCopyPermissions(fs, fs, "adir", "another", "/another")
|
2020-06-26 21:38:29 +00:00
|
|
|
assert.Error(t, err)
|
2020-06-13 21:49:28 +00:00
|
|
|
}
|
2020-10-29 18:23:33 +00:00
|
|
|
|
|
|
|
func TestSFTPSubSystem(t *testing.T) {
|
|
|
|
permissions := make(map[string][]string)
|
|
|
|
permissions["/"] = []string{dataprovider.PermAny}
|
2021-02-16 18:11:36 +00:00
|
|
|
user := &dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Permissions: permissions,
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2020-10-29 18:23:33 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.AzureBlobFilesystemProvider
|
2020-10-29 18:23:33 +00:00
|
|
|
err := ServeSubSystemConnection(user, "connID", nil, nil)
|
|
|
|
assert.Error(t, err)
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.LocalFilesystemProvider
|
2020-10-29 18:23:33 +00:00
|
|
|
|
|
|
|
buf := make([]byte, 0, 4096)
|
|
|
|
stdErrBuf := make([]byte, 0, 4096)
|
|
|
|
mockSSHChannel := &MockChannel{
|
|
|
|
Buffer: bytes.NewBuffer(buf),
|
|
|
|
StdErrBuffer: bytes.NewBuffer(stdErrBuf),
|
|
|
|
}
|
|
|
|
// this is 327680 and it will result in packet too long error
|
|
|
|
_, err = mockSSHChannel.Write([]byte{0x00, 0x05, 0x00, 0x00, 0x00, 0x00})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ServeSubSystemConnection(user, "id", mockSSHChannel, mockSSHChannel)
|
|
|
|
assert.EqualError(t, err, "packet too long")
|
|
|
|
|
|
|
|
subsystemChannel := newSubsystemChannel(mockSSHChannel, mockSSHChannel)
|
|
|
|
n, err := subsystemChannel.Write([]byte{0x00})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, n, 1)
|
|
|
|
err = subsystemChannel.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-10-31 10:02:04 +00:00
|
|
|
|
|
|
|
func TestRecoverer(t *testing.T) {
|
|
|
|
c := Configuration{}
|
|
|
|
c.AcceptInboundConnection(nil, nil)
|
|
|
|
connID := "connectionID"
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, common.ProtocolSFTP, "", "", dataprovider.User{}),
|
2020-10-31 10:02:04 +00:00
|
|
|
}
|
|
|
|
c.handleSftpConnection(nil, connection)
|
|
|
|
sshCmd := sshCommand{
|
|
|
|
command: "cd",
|
|
|
|
connection: connection,
|
|
|
|
}
|
|
|
|
err := sshCmd.handle()
|
|
|
|
assert.EqualError(t, err, common.ErrGenericFailure.Error())
|
|
|
|
scpCmd := scpCommand{
|
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = scpCmd.handle()
|
|
|
|
assert.EqualError(t, err, common.ErrGenericFailure.Error())
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 0)
|
|
|
|
}
|
2020-12-23 18:53:07 +00:00
|
|
|
|
|
|
|
func TestListernerAcceptErrors(t *testing.T) {
|
|
|
|
errFake := errors.New("a fake error")
|
|
|
|
listener := newFakeListener(errFake)
|
|
|
|
c := Configuration{}
|
|
|
|
err := c.serve(listener, nil)
|
|
|
|
require.EqualError(t, err, errFake.Error())
|
|
|
|
err = listener.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
errNetFake := &fakeNetError{error: errFake}
|
|
|
|
listener = newFakeListener(errNetFake)
|
|
|
|
err = c.serve(listener, nil)
|
|
|
|
require.EqualError(t, err, errFake.Error())
|
|
|
|
err = listener.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeNetError struct {
|
|
|
|
error
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *fakeNetError) Timeout() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *fakeNetError) Temporary() bool {
|
|
|
|
e.count++
|
|
|
|
return e.count < 10
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *fakeNetError) Error() string {
|
|
|
|
return e.error.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeListener struct {
|
|
|
|
server net.Conn
|
|
|
|
client net.Conn
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *fakeListener) Accept() (net.Conn, error) {
|
|
|
|
return l.client, l.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *fakeListener) Close() error {
|
|
|
|
errClient := l.client.Close()
|
|
|
|
errServer := l.server.Close()
|
|
|
|
if errServer != nil {
|
|
|
|
return errServer
|
|
|
|
}
|
|
|
|
return errClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *fakeListener) Addr() net.Addr {
|
|
|
|
return l.server.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFakeListener(err error) net.Listener {
|
|
|
|
server, client := net.Pipe()
|
|
|
|
|
|
|
|
return &fakeListener{
|
|
|
|
server: server,
|
|
|
|
client: client,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 18:12:23 +00:00
|
|
|
|
|
|
|
func TestFolderPrefix(t *testing.T) {
|
|
|
|
c := Configuration{
|
|
|
|
FolderPrefix: "files",
|
|
|
|
}
|
|
|
|
c.checkFolderPrefix()
|
|
|
|
assert.Equal(t, "/files", c.FolderPrefix)
|
|
|
|
c.FolderPrefix = ""
|
|
|
|
c.checkFolderPrefix()
|
|
|
|
assert.Empty(t, c.FolderPrefix)
|
|
|
|
c.FolderPrefix = "/"
|
|
|
|
c.checkFolderPrefix()
|
|
|
|
assert.Empty(t, c.FolderPrefix)
|
|
|
|
c.FolderPrefix = "/."
|
|
|
|
c.checkFolderPrefix()
|
|
|
|
assert.Empty(t, c.FolderPrefix)
|
|
|
|
c.FolderPrefix = "."
|
|
|
|
c.checkFolderPrefix()
|
|
|
|
assert.Empty(t, c.FolderPrefix)
|
|
|
|
c.FolderPrefix = ".."
|
|
|
|
c.checkFolderPrefix()
|
|
|
|
assert.Empty(t, c.FolderPrefix)
|
|
|
|
}
|
2022-03-31 19:49:06 +00:00
|
|
|
|
|
|
|
func TestLoadRevokedUserCertsFile(t *testing.T) {
|
|
|
|
r := revokedCertificates{
|
|
|
|
certs: map[string]bool{},
|
|
|
|
}
|
|
|
|
err := r.load()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.filePath = filepath.Join(os.TempDir(), "sub", "testrevoked")
|
|
|
|
err = os.MkdirAll(filepath.Dir(r.filePath), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.WriteFile(r.filePath, []byte(`no json`), 0644)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = r.load()
|
|
|
|
assert.Error(t, err)
|
|
|
|
r.filePath = filepath.Dir(r.filePath)
|
|
|
|
err = r.load()
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = os.RemoveAll(r.filePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
|
|
|
|
func TestMaxUserSessions(t *testing.T) {
|
|
|
|
connection := &Connection{
|
|
|
|
BaseConnection: common.NewBaseConnection(xid.New().String(), common.ProtocolSFTP, "", "", dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "user_max_sessions",
|
|
|
|
HomeDir: filepath.Clean(os.TempDir()),
|
|
|
|
MaxSessions: 1,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
err := common.Connections.Add(connection)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
c := Configuration{}
|
|
|
|
c.handleSftpConnection(nil, connection)
|
|
|
|
|
|
|
|
sshCmd := sshCommand{
|
|
|
|
command: "cd",
|
|
|
|
connection: connection,
|
|
|
|
}
|
|
|
|
err = sshCmd.handle()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "too many open sessions")
|
|
|
|
}
|
|
|
|
scpCmd := scpCommand{
|
|
|
|
sshCommand: sshCommand{
|
|
|
|
command: "scp",
|
|
|
|
connection: connection,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = scpCmd.handle()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "too many open sessions")
|
|
|
|
}
|
|
|
|
err = ServeSubSystemConnection(&connection.User, connection.ID, nil, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "too many open sessions")
|
|
|
|
}
|
|
|
|
common.Connections.Remove(connection.GetID())
|
|
|
|
assert.Len(t, common.Connections.GetStats(), 0)
|
|
|
|
}
|
2022-04-27 16:38:46 +00:00
|
|
|
|
|
|
|
func TestCanReadSymlink(t *testing.T) {
|
|
|
|
connection := &Connection{
|
|
|
|
BaseConnection: common.NewBaseConnection(xid.New().String(), common.ProtocolSFTP, "", "", dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "user_can_read_symlink",
|
|
|
|
HomeDir: filepath.Clean(os.TempDir()),
|
|
|
|
Permissions: map[string][]string{
|
|
|
|
"/": {dataprovider.PermAny},
|
|
|
|
"/sub": {dataprovider.PermUpload},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Filters: dataprovider.UserFilters{
|
|
|
|
BaseUserFilters: sdk.BaseUserFilters{
|
|
|
|
FilePatterns: []sdk.PatternsFilter{
|
|
|
|
{
|
|
|
|
Path: "/denied",
|
|
|
|
DeniedPatterns: []string{"*.txt"},
|
|
|
|
DenyPolicy: sdk.DenyPolicyHide,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
err := connection.canReadLink("/sub/link")
|
|
|
|
assert.ErrorIs(t, err, sftp.ErrSSHFxPermissionDenied)
|
|
|
|
|
|
|
|
err = connection.canReadLink("/denied/file.txt")
|
|
|
|
assert.ErrorIs(t, err, sftp.ErrSSHFxNoSuchFile)
|
|
|
|
}
|