2019-07-27 07:38:09 +00:00
|
|
|
package sftpd
|
|
|
|
|
|
|
|
import (
|
2019-08-04 07:37:58 +00:00
|
|
|
"os"
|
2019-07-27 19:19:30 +00:00
|
|
|
"runtime"
|
2019-07-27 07:38:09 +00:00
|
|
|
"testing"
|
2019-08-04 07:37:58 +00:00
|
|
|
|
2019-08-04 10:35:33 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2019-08-04 07:37:58 +00:00
|
|
|
"github.com/pkg/sftp"
|
2019-07-27 07:38:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWrongActions(t *testing.T) {
|
|
|
|
actionsCopy := actions
|
2019-07-27 19:19:30 +00:00
|
|
|
badCommand := "/bad/command"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
badCommand = "C:\\bad\\command"
|
|
|
|
}
|
2019-07-27 07:38:09 +00:00
|
|
|
actions = Actions{
|
|
|
|
ExecuteOn: []string{operationDownload},
|
2019-07-27 19:19:30 +00:00
|
|
|
Command: badCommand,
|
2019-07-27 07:38:09 +00:00
|
|
|
HTTPNotificationURL: "",
|
|
|
|
}
|
|
|
|
err := executeAction(operationDownload, "username", "path", "")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("action with bad command must fail")
|
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
err = executeAction(operationDelete, "username", "path", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("action not configured must silently fail")
|
|
|
|
}
|
2019-07-27 07:38:09 +00:00
|
|
|
actions.Command = ""
|
|
|
|
actions.HTTPNotificationURL = "http://foo\x7f.com/"
|
|
|
|
err = executeAction(operationDownload, "username", "path", "")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("action with bad url must fail")
|
|
|
|
}
|
|
|
|
actions = actionsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveNonexistentTransfer(t *testing.T) {
|
|
|
|
transfer := Transfer{}
|
|
|
|
err := removeTransfer(&transfer)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("remove nonexistent transfer must fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveNonexistentQuotaScan(t *testing.T) {
|
|
|
|
err := RemoveQuotaScan("username")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("remove nonexistent transfer must fail")
|
|
|
|
}
|
|
|
|
}
|
2019-08-04 07:37:58 +00:00
|
|
|
|
|
|
|
func TestGetOSOpenFlags(t *testing.T) {
|
|
|
|
var flags sftp.FileOpenFlags
|
|
|
|
flags.Write = true
|
|
|
|
flags.Append = true
|
|
|
|
flags.Excl = true
|
2019-08-04 09:02:38 +00:00
|
|
|
osFlags := getOSOpenFlags(flags)
|
2019-08-04 07:37:58 +00:00
|
|
|
if osFlags&os.O_WRONLY == 0 || osFlags&os.O_APPEND == 0 || osFlags&os.O_EXCL == 0 {
|
|
|
|
t.Errorf("error getting os flags from sftp file open flags")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUploadResume(t *testing.T) {
|
|
|
|
c := Connection{}
|
|
|
|
var flags sftp.FileOpenFlags
|
|
|
|
_, err := c.handleSFTPUploadToExistingFile(flags, "", "", 0)
|
|
|
|
if err != sftp.ErrSshFxOpUnsupported {
|
|
|
|
t.Errorf("file resume is not supported")
|
|
|
|
}
|
|
|
|
}
|
2019-08-04 10:35:33 +00:00
|
|
|
|
|
|
|
func TestUploadFiles(t *testing.T) {
|
|
|
|
oldUploadMode := uploadMode
|
|
|
|
uploadMode = uploadModeAtomic
|
|
|
|
c := Connection{}
|
|
|
|
var flags sftp.FileOpenFlags
|
|
|
|
flags.Write = true
|
|
|
|
flags.Trunc = true
|
|
|
|
_, err := c.handleSFTPUploadToExistingFile(flags, "missing_path", "other_missing_path", 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("upload to existing file must fail if one or both paths are invalid")
|
|
|
|
}
|
|
|
|
uploadMode = uploadModeStandard
|
|
|
|
_, err = c.handleSFTPUploadToExistingFile(flags, "missing_path", "other_missing_path", 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("upload to existing file must fail if one or both paths are invalid")
|
|
|
|
}
|
|
|
|
missingFile := "missing/relative/file.txt"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
missingFile = "missing\\relative\\file.txt"
|
|
|
|
}
|
|
|
|
_, err = c.handleSFTPUploadToNewFile(".", missingFile)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("upload new file in missing path must fail")
|
|
|
|
}
|
|
|
|
uploadMode = oldUploadMode
|
|
|
|
}
|
|
|
|
|
2019-08-11 12:53:37 +00:00
|
|
|
func TestWithInvalidHome(t *testing.T) {
|
2019-08-04 10:35:33 +00:00
|
|
|
u := dataprovider.User{}
|
|
|
|
u.HomeDir = "home_rel_path"
|
|
|
|
_, err := loginUser(u)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("login a user with an invalid home_dir must fail")
|
|
|
|
}
|
2019-08-11 12:53:37 +00:00
|
|
|
c := Connection{
|
|
|
|
User: u,
|
|
|
|
}
|
|
|
|
err = c.isSubDir("dir_rel_path")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("tested path is not a home subdir")
|
|
|
|
}
|
2019-08-04 10:35:33 +00:00
|
|
|
}
|