2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-04-08 12:55:04 +00:00
|
|
|
"github.com/pkg/sftp"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-08-25 16:23:00 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/kms"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2020-07-24 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTransferUpdateQuota(t *testing.T) {
|
2021-07-24 18:11:17 +00:00
|
|
|
conn := NewBaseConnection("", ProtocolSFTP, "", "", dataprovider.User{})
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer := BaseTransfer{
|
2022-08-30 13:47:41 +00:00
|
|
|
Connection: conn,
|
|
|
|
transferType: TransferUpload,
|
2023-05-16 16:08:14 +00:00
|
|
|
Fs: vfs.NewOsFs("", os.TempDir(), "", nil),
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesReceived.Store(123)
|
2020-07-24 21:39:38 +00:00
|
|
|
errFake := errors.New("fake error")
|
|
|
|
transfer.TransferError(errFake)
|
|
|
|
err := transfer.Close()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualError(t, err, errFake.Error())
|
|
|
|
}
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "vdir")
|
|
|
|
vdirPath := "/vdir"
|
|
|
|
conn.User.VirtualFolders = append(conn.User.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
},
|
|
|
|
VirtualPath: vdirPath,
|
|
|
|
QuotaFiles: -1,
|
|
|
|
QuotaSize: -1,
|
|
|
|
})
|
|
|
|
transfer.ErrTransfer = nil
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesReceived.Store(1)
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.requestPath = "/vdir/file"
|
2020-08-22 08:12:00 +00:00
|
|
|
assert.True(t, transfer.updateQuota(1, 0))
|
2020-07-24 21:39:38 +00:00
|
|
|
err = transfer.Close()
|
|
|
|
assert.NoError(t, err)
|
2022-10-10 17:34:15 +00:00
|
|
|
|
|
|
|
transfer.ErrTransfer = errFake
|
2022-10-27 06:27:44 +00:00
|
|
|
transfer.Fs = newMockOsFs(true, "", "", "S3Fs fake", nil)
|
2022-10-10 17:34:15 +00:00
|
|
|
assert.False(t, transfer.updateQuota(1, 0))
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransferThrottling(t *testing.T) {
|
|
|
|
u := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "test",
|
|
|
|
UploadBandwidth: 50,
|
|
|
|
DownloadBandwidth: 40,
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2023-05-16 16:08:14 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "", nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
testFileSize := int64(131072)
|
2021-01-17 21:29:08 +00:00
|
|
|
wantedUploadElapsed := 1000 * (testFileSize / 1024) / u.UploadBandwidth
|
|
|
|
wantedDownloadElapsed := 1000 * (testFileSize / 1024) / u.DownloadBandwidth
|
2020-08-11 21:56:10 +00:00
|
|
|
// some tolerance
|
|
|
|
wantedUploadElapsed -= wantedDownloadElapsed / 10
|
|
|
|
wantedDownloadElapsed -= wantedDownloadElapsed / 10
|
2021-07-24 18:11:17 +00:00
|
|
|
conn := NewBaseConnection("id", ProtocolSCP, "", "", u)
|
2022-01-30 10:42:36 +00:00
|
|
|
transfer := NewBaseTransfer(nil, conn, nil, "", "", "", TransferUpload, 0, 0, 0, 0, true, fs, dataprovider.TransferQuota{})
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesReceived.Store(testFileSize)
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.Connection.UpdateLastActivity()
|
|
|
|
startTime := transfer.Connection.GetLastActivity()
|
|
|
|
transfer.HandleThrottle()
|
|
|
|
elapsed := time.Since(startTime).Nanoseconds() / 1000000
|
|
|
|
assert.GreaterOrEqual(t, elapsed, wantedUploadElapsed, "upload bandwidth throttling not respected")
|
|
|
|
err := transfer.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
transfer = NewBaseTransfer(nil, conn, nil, "", "", "", TransferDownload, 0, 0, 0, 0, true, fs, dataprovider.TransferQuota{})
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesSent.Store(testFileSize)
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.Connection.UpdateLastActivity()
|
|
|
|
startTime = transfer.Connection.GetLastActivity()
|
|
|
|
|
|
|
|
transfer.HandleThrottle()
|
|
|
|
elapsed = time.Since(startTime).Nanoseconds() / 1000000
|
|
|
|
assert.GreaterOrEqual(t, elapsed, wantedDownloadElapsed, "download bandwidth throttling not respected")
|
|
|
|
err = transfer.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-08-22 12:52:17 +00:00
|
|
|
func TestRealPath(t *testing.T) {
|
|
|
|
testFile := filepath.Join(os.TempDir(), "afile.txt")
|
2023-05-16 16:08:14 +00:00
|
|
|
fs := vfs.NewOsFs("123", os.TempDir(), "", nil)
|
2020-08-22 12:52:17 +00:00
|
|
|
u := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "user",
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2020-08-22 12:52:17 +00:00
|
|
|
}
|
|
|
|
u.Permissions = make(map[string][]string)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
file, err := os.Create(testFile)
|
2020-08-25 16:23:00 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-24 18:11:17 +00:00
|
|
|
conn := NewBaseConnection(fs.ConnectionID(), ProtocolSFTP, "", "", u)
|
2022-01-20 17:19:20 +00:00
|
|
|
transfer := NewBaseTransfer(file, conn, nil, testFile, testFile, "/transfer_test_file",
|
2022-01-30 10:42:36 +00:00
|
|
|
TransferUpload, 0, 0, 0, 0, true, fs, dataprovider.TransferQuota{})
|
2020-08-22 12:52:17 +00:00
|
|
|
rPath := transfer.GetRealFsPath(testFile)
|
|
|
|
assert.Equal(t, testFile, rPath)
|
|
|
|
rPath = conn.getRealFsPath(testFile)
|
|
|
|
assert.Equal(t, testFile, rPath)
|
|
|
|
err = transfer.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = file.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
transfer.File = nil
|
|
|
|
rPath = transfer.GetRealFsPath(testFile)
|
|
|
|
assert.Equal(t, testFile, rPath)
|
|
|
|
rPath = transfer.GetRealFsPath("")
|
|
|
|
assert.Empty(t, rPath)
|
2020-08-25 16:23:00 +00:00
|
|
|
err = os.Remove(testFile)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, conn.GetTransfers(), 0)
|
2020-08-22 12:52:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 11:54:36 +00:00
|
|
|
func TestTruncate(t *testing.T) {
|
|
|
|
testFile := filepath.Join(os.TempDir(), "transfer_test_file")
|
2023-05-16 16:08:14 +00:00
|
|
|
fs := vfs.NewOsFs("123", os.TempDir(), "", nil)
|
2020-08-20 11:54:36 +00:00
|
|
|
u := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "user",
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2020-08-20 11:54:36 +00:00
|
|
|
}
|
|
|
|
u.Permissions = make(map[string][]string)
|
|
|
|
u.Permissions["/"] = []string{dataprovider.PermAny}
|
|
|
|
file, err := os.Create(testFile)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
assert.FailNow(t, "unable to open test file")
|
|
|
|
}
|
|
|
|
_, err = file.Write([]byte("hello"))
|
|
|
|
assert.NoError(t, err)
|
2021-07-24 18:11:17 +00:00
|
|
|
conn := NewBaseConnection(fs.ConnectionID(), ProtocolSFTP, "", "", u)
|
2022-01-20 17:19:20 +00:00
|
|
|
transfer := NewBaseTransfer(file, conn, nil, testFile, testFile, "/transfer_test_file", TransferUpload, 0, 5,
|
2022-01-30 10:42:36 +00:00
|
|
|
100, 0, false, fs, dataprovider.TransferQuota{})
|
2020-08-20 11:54:36 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.SetStat("/transfer_test_file", &StatAttributes{
|
2020-08-20 11:54:36 +00:00
|
|
|
Size: 2,
|
|
|
|
Flags: StatAttrSize,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2020-08-22 12:52:17 +00:00
|
|
|
assert.Equal(t, int64(103), transfer.MaxWriteSize)
|
2020-08-20 11:54:36 +00:00
|
|
|
err = transfer.Close()
|
|
|
|
assert.NoError(t, err)
|
2020-08-20 12:44:38 +00:00
|
|
|
err = file.Close()
|
|
|
|
assert.NoError(t, err)
|
2020-08-20 11:54:36 +00:00
|
|
|
fi, err := os.Stat(testFile)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, int64(2), fi.Size())
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:19:20 +00:00
|
|
|
transfer = NewBaseTransfer(file, conn, nil, testFile, testFile, "/transfer_test_file", TransferUpload, 0, 0,
|
2022-01-30 10:42:36 +00:00
|
|
|
100, 0, true, fs, dataprovider.TransferQuota{})
|
2020-08-22 08:12:00 +00:00
|
|
|
// file.Stat will fail on a closed file
|
2021-03-21 18:15:47 +00:00
|
|
|
err = conn.SetStat("/transfer_test_file", &StatAttributes{
|
2020-08-22 08:12:00 +00:00
|
|
|
Size: 2,
|
|
|
|
Flags: StatAttrSize,
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = transfer.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
transfer = NewBaseTransfer(nil, conn, nil, testFile, testFile, "", TransferUpload, 0, 0, 0, 0, true,
|
|
|
|
fs, dataprovider.TransferQuota{})
|
2020-08-22 08:12:00 +00:00
|
|
|
_, err = transfer.Truncate("mismatch", 0)
|
2020-08-20 11:54:36 +00:00
|
|
|
assert.EqualError(t, err, errTransferMismatch.Error())
|
2020-08-22 08:12:00 +00:00
|
|
|
_, err = transfer.Truncate(testFile, 0)
|
2020-08-20 11:54:36 +00:00
|
|
|
assert.NoError(t, err)
|
2020-08-22 08:12:00 +00:00
|
|
|
_, err = transfer.Truncate(testFile, 1)
|
2021-04-03 14:00:55 +00:00
|
|
|
assert.EqualError(t, err, vfs.ErrVfsUnsupported.Error())
|
2020-08-20 11:54:36 +00:00
|
|
|
|
|
|
|
err = transfer.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.Remove(testFile)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, conn.GetTransfers(), 0)
|
|
|
|
}
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
func TestTransferErrors(t *testing.T) {
|
|
|
|
isCancelled := false
|
|
|
|
cancelFn := func() {
|
|
|
|
isCancelled = true
|
|
|
|
}
|
|
|
|
testFile := filepath.Join(os.TempDir(), "transfer_test_file")
|
2023-05-16 16:08:14 +00:00
|
|
|
fs := vfs.NewOsFs("id", os.TempDir(), "", nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
u := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "test",
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-02-25 20:53:04 +00:00
|
|
|
err := os.WriteFile(testFile, []byte("test data"), os.ModePerm)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
file, err := os.Open(testFile)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
assert.FailNow(t, "unable to open test file")
|
|
|
|
}
|
2021-07-24 18:11:17 +00:00
|
|
|
conn := NewBaseConnection("id", ProtocolSFTP, "", "", u)
|
2022-01-20 17:19:20 +00:00
|
|
|
transfer := NewBaseTransfer(file, conn, nil, testFile, testFile, "/transfer_test_file", TransferUpload,
|
2022-01-30 10:42:36 +00:00
|
|
|
0, 0, 0, 0, true, fs, dataprovider.TransferQuota{})
|
2023-04-08 12:55:04 +00:00
|
|
|
err = transfer.ConvertError(os.ErrNotExist)
|
|
|
|
assert.ErrorIs(t, err, sftp.ErrSSHFxNoSuchFile)
|
|
|
|
err = transfer.ConvertError(os.ErrPermission)
|
|
|
|
assert.ErrorIs(t, err, sftp.ErrSSHFxPermissionDenied)
|
2020-08-11 21:56:10 +00:00
|
|
|
assert.Nil(t, transfer.cancelFn)
|
|
|
|
assert.Equal(t, testFile, transfer.GetFsPath())
|
2023-08-12 16:51:47 +00:00
|
|
|
transfer.SetMetadata(map[string]string{"key": "val"})
|
2020-08-11 21:56:10 +00:00
|
|
|
transfer.SetCancelFn(cancelFn)
|
2020-07-24 21:39:38 +00:00
|
|
|
errFake := errors.New("err fake")
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesReceived.Store(9)
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.TransferError(ErrQuotaExceeded)
|
|
|
|
assert.True(t, isCancelled)
|
|
|
|
transfer.TransferError(errFake)
|
|
|
|
assert.Error(t, transfer.ErrTransfer, ErrQuotaExceeded.Error())
|
|
|
|
// the file is closed from the embedding struct before to call close
|
|
|
|
err = file.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = transfer.Close()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Error(t, err, ErrQuotaExceeded.Error())
|
|
|
|
}
|
|
|
|
assert.NoFileExists(t, testFile)
|
|
|
|
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testFile, []byte("test data"), os.ModePerm)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
file, err = os.Open(testFile)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
assert.FailNow(t, "unable to open test file")
|
|
|
|
}
|
|
|
|
fsPath := filepath.Join(os.TempDir(), "test_file")
|
2022-01-30 10:42:36 +00:00
|
|
|
transfer = NewBaseTransfer(file, conn, nil, fsPath, file.Name(), "/test_file", TransferUpload, 0, 0, 0, 0, true,
|
|
|
|
fs, dataprovider.TransferQuota{})
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesReceived.Store(9)
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.TransferError(errFake)
|
|
|
|
assert.Error(t, transfer.ErrTransfer, errFake.Error())
|
|
|
|
// the file is closed from the embedding struct before to call close
|
|
|
|
err = file.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = transfer.Close()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Error(t, err, errFake.Error())
|
|
|
|
}
|
|
|
|
assert.NoFileExists(t, testFile)
|
|
|
|
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testFile, []byte("test data"), os.ModePerm)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
file, err = os.Open(testFile)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
assert.FailNow(t, "unable to open test file")
|
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
transfer = NewBaseTransfer(file, conn, nil, fsPath, file.Name(), "/test_file", TransferUpload, 0, 0, 0, 0, true,
|
|
|
|
fs, dataprovider.TransferQuota{})
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesReceived.Store(9)
|
2020-07-24 21:39:38 +00:00
|
|
|
// the file is closed from the embedding struct before to call close
|
|
|
|
err = file.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = transfer.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoFileExists(t, testFile)
|
|
|
|
assert.FileExists(t, fsPath)
|
|
|
|
err = os.Remove(fsPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, conn.GetTransfers(), 0)
|
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
|
|
|
|
func TestRemovePartialCryptoFile(t *testing.T) {
|
|
|
|
testFile := filepath.Join(os.TempDir(), "transfer_test_file")
|
2022-01-06 09:11:47 +00:00
|
|
|
fs, err := vfs.NewCryptFs("id", os.TempDir(), "", vfs.CryptFsConfig{Passphrase: kms.NewPlainSecret("secret")})
|
2020-12-05 12:48:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
u := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "test",
|
|
|
|
HomeDir: os.TempDir(),
|
|
|
|
},
|
2020-12-05 12:48:13 +00:00
|
|
|
}
|
2021-07-24 18:11:17 +00:00
|
|
|
conn := NewBaseConnection(fs.ConnectionID(), ProtocolSFTP, "", "", u)
|
2022-01-20 17:19:20 +00:00
|
|
|
transfer := NewBaseTransfer(nil, conn, nil, testFile, testFile, "/transfer_test_file", TransferUpload,
|
2022-01-30 10:42:36 +00:00
|
|
|
0, 0, 0, 0, true, fs, dataprovider.TransferQuota{})
|
2020-12-05 12:48:13 +00:00
|
|
|
transfer.ErrTransfer = errors.New("test error")
|
2022-10-10 17:34:15 +00:00
|
|
|
_, _, err = transfer.getUploadFileSize()
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.Error(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testFile, []byte("test data"), os.ModePerm)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 17:34:15 +00:00
|
|
|
size, deletedFiles, err := transfer.getUploadFileSize()
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-10 17:34:15 +00:00
|
|
|
assert.Equal(t, int64(0), size)
|
|
|
|
assert.Equal(t, 1, deletedFiles)
|
2020-12-05 12:48:13 +00:00
|
|
|
assert.NoFileExists(t, testFile)
|
|
|
|
}
|
2021-08-10 11:07:38 +00:00
|
|
|
|
|
|
|
func TestFTPMode(t *testing.T) {
|
|
|
|
conn := NewBaseConnection("", ProtocolFTP, "", "", dataprovider.User{})
|
|
|
|
transfer := BaseTransfer{
|
2022-08-30 13:47:41 +00:00
|
|
|
Connection: conn,
|
|
|
|
transferType: TransferUpload,
|
2023-05-16 16:08:14 +00:00
|
|
|
Fs: vfs.NewOsFs("", os.TempDir(), "", nil),
|
2021-08-10 11:07:38 +00:00
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesReceived.Store(123)
|
2021-08-10 11:07:38 +00:00
|
|
|
assert.Empty(t, transfer.ftpMode)
|
|
|
|
transfer.SetFtpMode("active")
|
|
|
|
assert.Equal(t, "active", transfer.ftpMode)
|
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
|
|
|
|
func TestTransferQuota(t *testing.T) {
|
|
|
|
user := dataprovider.User{
|
|
|
|
BaseUser: sdk.BaseUser{
|
2023-06-16 19:06:21 +00:00
|
|
|
TotalDataTransfer: 3,
|
|
|
|
UploadDataTransfer: 2,
|
|
|
|
DownloadDataTransfer: 1,
|
2022-01-30 10:42:36 +00:00
|
|
|
},
|
|
|
|
}
|
2023-06-16 19:06:21 +00:00
|
|
|
ul, dl, total := user.GetDataTransferLimits()
|
|
|
|
assert.Equal(t, int64(2*1048576), ul)
|
|
|
|
assert.Equal(t, int64(1*1048576), dl)
|
|
|
|
assert.Equal(t, int64(3*1048576), total)
|
|
|
|
user.TotalDataTransfer = -1
|
|
|
|
user.UploadDataTransfer = -1
|
|
|
|
user.DownloadDataTransfer = -1
|
|
|
|
ul, dl, total = user.GetDataTransferLimits()
|
2022-01-30 10:42:36 +00:00
|
|
|
assert.Equal(t, int64(0), ul)
|
|
|
|
assert.Equal(t, int64(0), dl)
|
|
|
|
assert.Equal(t, int64(0), total)
|
|
|
|
transferQuota := dataprovider.TransferQuota{}
|
|
|
|
assert.True(t, transferQuota.HasDownloadSpace())
|
|
|
|
assert.True(t, transferQuota.HasUploadSpace())
|
|
|
|
transferQuota.TotalSize = -1
|
|
|
|
transferQuota.ULSize = -1
|
|
|
|
transferQuota.DLSize = -1
|
|
|
|
assert.True(t, transferQuota.HasDownloadSpace())
|
|
|
|
assert.True(t, transferQuota.HasUploadSpace())
|
|
|
|
transferQuota.TotalSize = 100
|
|
|
|
transferQuota.AllowedTotalSize = 10
|
|
|
|
assert.True(t, transferQuota.HasDownloadSpace())
|
|
|
|
assert.True(t, transferQuota.HasUploadSpace())
|
|
|
|
transferQuota.AllowedTotalSize = 0
|
|
|
|
assert.False(t, transferQuota.HasDownloadSpace())
|
|
|
|
assert.False(t, transferQuota.HasUploadSpace())
|
|
|
|
transferQuota.TotalSize = 0
|
|
|
|
transferQuota.DLSize = 100
|
|
|
|
transferQuota.ULSize = 50
|
|
|
|
transferQuota.AllowedTotalSize = 0
|
|
|
|
assert.False(t, transferQuota.HasDownloadSpace())
|
|
|
|
assert.False(t, transferQuota.HasUploadSpace())
|
|
|
|
transferQuota.AllowedDLSize = 1
|
|
|
|
transferQuota.AllowedULSize = 1
|
|
|
|
assert.True(t, transferQuota.HasDownloadSpace())
|
|
|
|
assert.True(t, transferQuota.HasUploadSpace())
|
|
|
|
transferQuota.AllowedDLSize = -10
|
|
|
|
transferQuota.AllowedULSize = -1
|
|
|
|
assert.False(t, transferQuota.HasDownloadSpace())
|
|
|
|
assert.False(t, transferQuota.HasUploadSpace())
|
|
|
|
|
|
|
|
conn := NewBaseConnection("", ProtocolSFTP, "", "", user)
|
|
|
|
transfer := NewBaseTransfer(nil, conn, nil, "file.txt", "file.txt", "/transfer_test_file", TransferUpload,
|
2023-05-16 16:08:14 +00:00
|
|
|
0, 0, 0, 0, true, vfs.NewOsFs("", os.TempDir(), "", nil), dataprovider.TransferQuota{})
|
2022-01-30 10:42:36 +00:00
|
|
|
err := transfer.CheckRead()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = transfer.CheckWrite()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
transfer.transferQuota = dataprovider.TransferQuota{
|
|
|
|
AllowedTotalSize: 10,
|
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesReceived.Store(5)
|
|
|
|
transfer.BytesSent.Store(4)
|
2022-01-30 10:42:36 +00:00
|
|
|
err = transfer.CheckRead()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = transfer.CheckWrite()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesSent.Store(6)
|
2022-01-30 10:42:36 +00:00
|
|
|
err = transfer.CheckRead()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), ErrReadQuotaExceeded.Error())
|
|
|
|
}
|
|
|
|
err = transfer.CheckWrite()
|
|
|
|
assert.True(t, conn.IsQuotaExceededError(err))
|
|
|
|
|
|
|
|
transferQuota = dataprovider.TransferQuota{
|
|
|
|
AllowedTotalSize: 0,
|
|
|
|
AllowedULSize: 10,
|
|
|
|
AllowedDLSize: 5,
|
|
|
|
}
|
|
|
|
transfer.transferQuota = transferQuota
|
|
|
|
assert.Equal(t, transferQuota, transfer.GetTransferQuota())
|
|
|
|
err = transfer.CheckRead()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), ErrReadQuotaExceeded.Error())
|
|
|
|
}
|
|
|
|
err = transfer.CheckWrite()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesReceived.Store(11)
|
2022-01-30 10:42:36 +00:00
|
|
|
err = transfer.CheckRead()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), ErrReadQuotaExceeded.Error())
|
|
|
|
}
|
|
|
|
err = transfer.CheckWrite()
|
|
|
|
assert.True(t, conn.IsQuotaExceededError(err))
|
|
|
|
}
|
2022-04-02 16:32:46 +00:00
|
|
|
|
|
|
|
func TestUploadOutsideHomeRenameError(t *testing.T) {
|
|
|
|
oldTempPath := Config.TempPath
|
|
|
|
|
|
|
|
conn := NewBaseConnection("", ProtocolSFTP, "", "", dataprovider.User{})
|
|
|
|
transfer := BaseTransfer{
|
2022-08-30 13:47:41 +00:00
|
|
|
Connection: conn,
|
|
|
|
transferType: TransferUpload,
|
2023-05-16 16:08:14 +00:00
|
|
|
Fs: vfs.NewOsFs("", filepath.Join(os.TempDir(), "home"), "", nil),
|
2022-04-02 16:32:46 +00:00
|
|
|
}
|
2022-08-30 13:47:41 +00:00
|
|
|
transfer.BytesReceived.Store(123)
|
2022-04-02 16:32:46 +00:00
|
|
|
|
|
|
|
fileName := filepath.Join(os.TempDir(), "_temp")
|
|
|
|
err := os.WriteFile(fileName, []byte(`data`), 0644)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
transfer.effectiveFsPath = fileName
|
|
|
|
res := transfer.checkUploadOutsideHomeDir(os.ErrPermission)
|
|
|
|
assert.Equal(t, 0, res)
|
|
|
|
|
|
|
|
Config.TempPath = filepath.Clean(os.TempDir())
|
|
|
|
res = transfer.checkUploadOutsideHomeDir(nil)
|
|
|
|
assert.Equal(t, 0, res)
|
2022-08-30 13:47:41 +00:00
|
|
|
assert.Greater(t, transfer.BytesReceived.Load(), int64(0))
|
2022-04-02 16:32:46 +00:00
|
|
|
res = transfer.checkUploadOutsideHomeDir(os.ErrPermission)
|
|
|
|
assert.Equal(t, 1, res)
|
2022-08-30 13:47:41 +00:00
|
|
|
assert.Equal(t, int64(0), transfer.BytesReceived.Load())
|
2022-04-02 16:32:46 +00:00
|
|
|
assert.NoFileExists(t, fileName)
|
|
|
|
|
|
|
|
Config.TempPath = oldTempPath
|
|
|
|
}
|