2020-07-24 21:39:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2020-07-24 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
2020-07-31 17:24:57 +00:00
|
|
|
// MockOsFs mockable OsFs
|
|
|
|
type MockOsFs struct {
|
|
|
|
vfs.Fs
|
|
|
|
hasVirtualFolders bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name for the Fs implementation
|
|
|
|
func (fs MockOsFs) Name() string {
|
|
|
|
return "mockOsFs"
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasVirtualFolders returns true if folders are emulated
|
|
|
|
func (fs MockOsFs) HasVirtualFolders() bool {
|
|
|
|
return fs.hasVirtualFolders
|
|
|
|
}
|
|
|
|
|
2020-08-16 18:17:02 +00:00
|
|
|
func (fs MockOsFs) IsUploadResumeSupported() bool {
|
|
|
|
return !fs.hasVirtualFolders
|
|
|
|
}
|
|
|
|
|
2020-07-31 17:24:57 +00:00
|
|
|
func newMockOsFs(hasVirtualFolders bool, connectionID, rootDir string) vfs.Fs {
|
|
|
|
return &MockOsFs{
|
2021-03-21 18:15:47 +00:00
|
|
|
Fs: vfs.NewOsFs(connectionID, rootDir, ""),
|
2020-07-31 17:24:57 +00:00
|
|
|
hasVirtualFolders: hasVirtualFolders,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func TestRemoveErrors(t *testing.T) {
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "map")
|
|
|
|
homePath := filepath.Join(os.TempDir(), "home")
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
user := dataprovider.User{
|
2021-03-21 18:15:47 +00:00
|
|
|
Username: "remove_errors_user",
|
|
|
|
HomeDir: homePath,
|
|
|
|
VirtualFolders: []vfs.VirtualFolder{
|
|
|
|
{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
Name: filepath.Base(mappedPath),
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
},
|
|
|
|
VirtualPath: "/virtualpath",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2021-06-01 20:28:43 +00:00
|
|
|
conn := NewBaseConnection("", ProtocolFTP, "", user)
|
2021-03-21 18:15:47 +00:00
|
|
|
err := conn.IsRemoveDirAllowed(fs, mappedPath, "/virtualpath1")
|
2020-07-24 21:39:38 +00:00
|
|
|
if assert.Error(t, err) {
|
2021-03-21 18:15:47 +00:00
|
|
|
assert.Contains(t, err.Error(), "permission denied")
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.RemoveFile(fs, filepath.Join(homePath, "missing_file"), "/missing_file",
|
|
|
|
vfs.NewFileInfo("info", false, 100, time.Now(), false))
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func TestSetStatMode(t *testing.T) {
|
2020-07-24 21:39:38 +00:00
|
|
|
oldSetStatMode := Config.SetstatMode
|
|
|
|
Config.SetstatMode = 1
|
2021-03-21 18:15:47 +00:00
|
|
|
|
|
|
|
fakePath := "fake path"
|
2020-07-24 21:39:38 +00:00
|
|
|
user := dataprovider.User{
|
2021-03-21 18:15:47 +00:00
|
|
|
HomeDir: os.TempDir(),
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := newMockOsFs(true, "", user.GetHomeDir())
|
2021-06-01 20:28:43 +00:00
|
|
|
conn := NewBaseConnection("", ProtocolWebDAV, "", user)
|
2021-03-21 18:15:47 +00:00
|
|
|
err := conn.handleChmod(fs, fakePath, fakePath, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.handleChown(fs, fakePath, fakePath, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.handleChtimes(fs, fakePath, fakePath, nil)
|
2020-11-12 09:39:46 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
Config.SetstatMode = 2
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.handleChmod(fs, fakePath, fakePath, nil)
|
2020-08-22 08:12:00 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
Config.SetstatMode = oldSetStatMode
|
|
|
|
}
|
2020-08-22 08:12:00 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func TestRecursiveRenameWalkError(t *testing.T) {
|
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2021-06-01 20:28:43 +00:00
|
|
|
conn := NewBaseConnection("", ProtocolWebDAV, "", dataprovider.User{})
|
2021-03-21 18:15:47 +00:00
|
|
|
err := conn.checkRecursiveRenameDirPermissions(fs, fs, "/source", "/target")
|
|
|
|
assert.ErrorIs(t, err, os.ErrNotExist)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func TestCrossRenameFsErrors(t *testing.T) {
|
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2021-06-01 20:28:43 +00:00
|
|
|
conn := NewBaseConnection("", ProtocolWebDAV, "", dataprovider.User{})
|
2021-03-21 18:15:47 +00:00
|
|
|
res := conn.hasSpaceForCrossRename(fs, vfs.QuotaCheckResult{}, 1, "missingsource")
|
|
|
|
assert.False(t, res)
|
2020-07-24 21:39:38 +00:00
|
|
|
if runtime.GOOS != osWindows {
|
2021-03-21 18:15:47 +00:00
|
|
|
dirPath := filepath.Join(os.TempDir(), "d")
|
|
|
|
err := os.Mkdir(dirPath, os.ModePerm)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
err = os.Chmod(dirPath, 0001)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
res = conn.hasSpaceForCrossRename(fs, vfs.QuotaCheckResult{}, 1, dirPath)
|
|
|
|
assert.False(t, res)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
err = os.Chmod(dirPath, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(dirPath)
|
|
|
|
assert.NoError(t, err)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func TestRenameVirtualFolders(t *testing.T) {
|
|
|
|
vdir := "/avdir"
|
|
|
|
u := dataprovider.User{}
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
2020-07-24 21:39:38 +00:00
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2021-03-21 18:15:47 +00:00
|
|
|
Name: "name",
|
|
|
|
MappedPath: "mappedPath",
|
2020-07-24 21:39:38 +00:00
|
|
|
},
|
2021-03-21 18:15:47 +00:00
|
|
|
VirtualPath: vdir,
|
2020-07-24 21:39:38 +00:00
|
|
|
})
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2021-06-01 20:28:43 +00:00
|
|
|
conn := NewBaseConnection("", ProtocolFTP, "", u)
|
2021-03-21 18:15:47 +00:00
|
|
|
res := conn.isRenamePermitted(fs, fs, "source", "target", vdir, "vdirtarget", nil)
|
|
|
|
assert.False(t, res)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateQuotaAfterRename(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
|
|
|
Username: userTestUsername,
|
|
|
|
HomeDir: filepath.Join(os.TempDir(), "home"),
|
|
|
|
}
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "vdir")
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
user.VirtualFolders = append(user.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir",
|
|
|
|
QuotaFiles: -1,
|
|
|
|
QuotaSize: -1,
|
|
|
|
})
|
|
|
|
user.VirtualFolders = append(user.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
},
|
|
|
|
VirtualPath: "/vdir1",
|
|
|
|
QuotaFiles: -1,
|
|
|
|
QuotaSize: -1,
|
|
|
|
})
|
|
|
|
err := os.MkdirAll(user.GetHomeDir(), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.MkdirAll(mappedPath, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
fs, err := user.GetFilesystem("id")
|
|
|
|
assert.NoError(t, err)
|
2021-06-01 20:28:43 +00:00
|
|
|
c := NewBaseConnection("", ProtocolSFTP, "", user)
|
2020-07-24 21:39:38 +00:00
|
|
|
request := sftp.NewRequest("Rename", "/testfile")
|
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
request.Filepath = "/dir"
|
|
|
|
request.Target = path.Join("/vdir", "dir")
|
|
|
|
testDirPath := filepath.Join(mappedPath, "dir")
|
|
|
|
err := os.MkdirAll(testDirPath, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Chmod(testDirPath, 0001)
|
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
err = c.updateQuotaAfterRename(fs, request.Filepath, request.Target, testDirPath, 0)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
err = os.Chmod(testDirPath, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
testFile1 := "/testfile1"
|
|
|
|
request.Target = testFile1
|
|
|
|
request.Filepath = path.Join("/vdir", "file")
|
2021-03-21 18:15:47 +00:00
|
|
|
err = c.updateQuotaAfterRename(fs, request.Filepath, request.Target, filepath.Join(mappedPath, "file"), 0)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Error(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(filepath.Join(mappedPath, "file"), []byte("test content"), os.ModePerm)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
request.Filepath = testFile1
|
|
|
|
request.Target = path.Join("/vdir", "file")
|
2021-03-21 18:15:47 +00:00
|
|
|
err = c.updateQuotaAfterRename(fs, request.Filepath, request.Target, filepath.Join(mappedPath, "file"), 12)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(filepath.Join(user.GetHomeDir(), "testfile1"), []byte("test content"), os.ModePerm)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
request.Target = testFile1
|
|
|
|
request.Filepath = path.Join("/vdir", "file")
|
2021-03-21 18:15:47 +00:00
|
|
|
err = c.updateQuotaAfterRename(fs, request.Filepath, request.Target, filepath.Join(mappedPath, "file"), 12)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
request.Target = path.Join("/vdir1", "file")
|
|
|
|
request.Filepath = path.Join("/vdir", "file")
|
2021-03-21 18:15:47 +00:00
|
|
|
err = c.updateQuotaAfterRename(fs, request.Filepath, request.Target, filepath.Join(mappedPath, "file"), 12)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.RemoveAll(mappedPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorsMapping(t *testing.T) {
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
2021-06-01 20:28:43 +00:00
|
|
|
conn := NewBaseConnection("", ProtocolSFTP, "", dataprovider.User{HomeDir: os.TempDir()})
|
2020-08-04 16:03:28 +00:00
|
|
|
for _, protocol := range supportedProtocols {
|
2020-07-24 21:39:38 +00:00
|
|
|
conn.SetProtocol(protocol)
|
2021-03-21 18:15:47 +00:00
|
|
|
err := conn.GetFsError(fs, os.ErrNotExist)
|
2020-07-24 21:39:38 +00:00
|
|
|
if protocol == ProtocolSFTP {
|
|
|
|
assert.EqualError(t, err, sftp.ErrSSHFxNoSuchFile.Error())
|
2021-05-06 19:35:43 +00:00
|
|
|
} else if protocol == ProtocolWebDAV || protocol == ProtocolFTP || protocol == ProtocolHTTP {
|
2020-08-11 21:56:10 +00:00
|
|
|
assert.EqualError(t, err, os.ErrNotExist.Error())
|
2020-07-24 21:39:38 +00:00
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, ErrNotExist.Error())
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.GetFsError(fs, os.ErrPermission)
|
2020-07-24 21:39:38 +00:00
|
|
|
if protocol == ProtocolSFTP {
|
|
|
|
assert.EqualError(t, err, sftp.ErrSSHFxPermissionDenied.Error())
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, ErrPermissionDenied.Error())
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.GetFsError(fs, os.ErrClosed)
|
2020-07-24 21:39:38 +00:00
|
|
|
if protocol == ProtocolSFTP {
|
|
|
|
assert.EqualError(t, err, sftp.ErrSSHFxFailure.Error())
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, ErrGenericFailure.Error())
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.GetFsError(fs, ErrPermissionDenied)
|
2020-09-19 08:14:30 +00:00
|
|
|
if protocol == ProtocolSFTP {
|
|
|
|
assert.EqualError(t, err, sftp.ErrSSHFxFailure.Error())
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, ErrPermissionDenied.Error())
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.GetFsError(fs, vfs.ErrVfsUnsupported)
|
2020-11-12 09:39:46 +00:00
|
|
|
if protocol == ProtocolSFTP {
|
|
|
|
assert.EqualError(t, err, sftp.ErrSSHFxOpUnsupported.Error())
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, ErrOpUnsupported.Error())
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.GetFsError(fs, vfs.ErrStorageSizeUnavailable)
|
2021-02-11 18:45:52 +00:00
|
|
|
if protocol == ProtocolSFTP {
|
|
|
|
assert.EqualError(t, err, sftp.ErrSSHFxOpUnsupported.Error())
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, vfs.ErrStorageSizeUnavailable.Error())
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.GetFsError(fs, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = conn.GetOpUnsupportedError()
|
|
|
|
if protocol == ProtocolSFTP {
|
|
|
|
assert.EqualError(t, err, sftp.ErrSSHFxOpUnsupported.Error())
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, ErrOpUnsupported.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 18:17:02 +00:00
|
|
|
|
|
|
|
func TestMaxWriteSize(t *testing.T) {
|
|
|
|
permissions := make(map[string][]string)
|
|
|
|
permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
user := dataprovider.User{
|
|
|
|
Username: userTestUsername,
|
|
|
|
Permissions: permissions,
|
|
|
|
HomeDir: filepath.Clean(os.TempDir()),
|
|
|
|
}
|
|
|
|
fs, err := user.GetFilesystem("123")
|
|
|
|
assert.NoError(t, err)
|
2021-06-01 20:28:43 +00:00
|
|
|
conn := NewBaseConnection("", ProtocolFTP, "", user)
|
2020-08-16 18:17:02 +00:00
|
|
|
quotaResult := vfs.QuotaCheckResult{
|
|
|
|
HasSpace: true,
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
size, err := conn.GetMaxWriteSize(quotaResult, false, 0, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(0), size)
|
|
|
|
|
|
|
|
conn.User.Filters.MaxUploadFileSize = 100
|
2021-03-21 18:15:47 +00:00
|
|
|
size, err = conn.GetMaxWriteSize(quotaResult, false, 0, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(100), size)
|
|
|
|
|
|
|
|
quotaResult.QuotaSize = 1000
|
2021-03-21 18:15:47 +00:00
|
|
|
size, err = conn.GetMaxWriteSize(quotaResult, false, 50, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(100), size)
|
|
|
|
|
|
|
|
quotaResult.QuotaSize = 1000
|
|
|
|
quotaResult.UsedSize = 990
|
2021-03-21 18:15:47 +00:00
|
|
|
size, err = conn.GetMaxWriteSize(quotaResult, false, 50, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(60), size)
|
|
|
|
|
|
|
|
quotaResult.QuotaSize = 0
|
|
|
|
quotaResult.UsedSize = 0
|
2021-03-21 18:15:47 +00:00
|
|
|
size, err = conn.GetMaxWriteSize(quotaResult, true, 100, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.EqualError(t, err, ErrQuotaExceeded.Error())
|
|
|
|
assert.Equal(t, int64(0), size)
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
size, err = conn.GetMaxWriteSize(quotaResult, true, 10, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(90), size)
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
fs = newMockOsFs(true, fs.ConnectionID(), user.GetHomeDir())
|
|
|
|
size, err = conn.GetMaxWriteSize(quotaResult, true, 100, fs.IsUploadResumeSupported())
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.EqualError(t, err, ErrOpUnsupported.Error())
|
|
|
|
assert.Equal(t, int64(0), size)
|
|
|
|
}
|