2020-07-24 21:39:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
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
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
2022-01-06 09:11:47 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/kms"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/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{
|
|
|
|
Connection: conn,
|
|
|
|
transferType: TransferUpload,
|
|
|
|
BytesReceived: 123,
|
2021-03-21 18:15:47 +00:00
|
|
|
Fs: vfs.NewOsFs("", os.TempDir(), ""),
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
errFake := errors.New("fake error")
|
|
|
|
transfer.TransferError(errFake)
|
2020-08-22 08:12:00 +00:00
|
|
|
assert.False(t, transfer.updateQuota(1, 0))
|
2020-07-24 21:39:38 +00:00
|
|
|
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
|
|
|
|
transfer.BytesReceived = 1
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("", os.TempDir(), "")
|
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{})
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.BytesReceived = testFileSize
|
|
|
|
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{})
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.BytesSent = testFileSize
|
|
|
|
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")
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("123", os.TempDir(), "")
|
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")
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("123", os.TempDir(), "")
|
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")
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("id", os.TempDir(), "")
|
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{})
|
2020-08-11 21:56:10 +00:00
|
|
|
assert.Nil(t, transfer.cancelFn)
|
|
|
|
assert.Equal(t, testFile, transfer.GetFsPath())
|
|
|
|
transfer.SetCancelFn(cancelFn)
|
2020-07-24 21:39:38 +00:00
|
|
|
errFake := errors.New("err fake")
|
|
|
|
transfer.BytesReceived = 9
|
|
|
|
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{})
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.BytesReceived = 9
|
|
|
|
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{})
|
2020-07-24 21:39:38 +00:00
|
|
|
transfer.BytesReceived = 9
|
|
|
|
// 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")
|
|
|
|
_, err = transfer.getUploadFileSize()
|
|
|
|
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)
|
|
|
|
size, err := transfer.getUploadFileSize()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(9), size)
|
|
|
|
assert.NoFileExists(t, testFile)
|
|
|
|
}
|
2021-08-10 11:07:38 +00:00
|
|
|
|
|
|
|
func TestFTPMode(t *testing.T) {
|
|
|
|
conn := NewBaseConnection("", ProtocolFTP, "", "", dataprovider.User{})
|
|
|
|
transfer := BaseTransfer{
|
|
|
|
Connection: conn,
|
|
|
|
transferType: TransferUpload,
|
|
|
|
BytesReceived: 123,
|
|
|
|
Fs: vfs.NewOsFs("", os.TempDir(), ""),
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
TotalDataTransfer: -1,
|
|
|
|
UploadDataTransfer: -1,
|
|
|
|
DownloadDataTransfer: -1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user.Filters.DataTransferLimits = []sdk.DataTransferLimit{
|
|
|
|
{
|
|
|
|
Sources: []string{"127.0.0.1/32", "192.168.1.0/24"},
|
|
|
|
TotalDataTransfer: 100,
|
|
|
|
UploadDataTransfer: 0,
|
|
|
|
DownloadDataTransfer: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Sources: []string{"172.16.0.0/24"},
|
|
|
|
TotalDataTransfer: 0,
|
|
|
|
UploadDataTransfer: 120,
|
|
|
|
DownloadDataTransfer: 150,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ul, dl, total := user.GetDataTransferLimits("127.0.1.1")
|
|
|
|
assert.Equal(t, int64(0), ul)
|
|
|
|
assert.Equal(t, int64(0), dl)
|
|
|
|
assert.Equal(t, int64(0), total)
|
|
|
|
ul, dl, total = user.GetDataTransferLimits("127.0.0.1")
|
|
|
|
assert.Equal(t, int64(0), ul)
|
|
|
|
assert.Equal(t, int64(0), dl)
|
|
|
|
assert.Equal(t, int64(100*1048576), total)
|
|
|
|
ul, dl, total = user.GetDataTransferLimits("192.168.1.4")
|
|
|
|
assert.Equal(t, int64(0), ul)
|
|
|
|
assert.Equal(t, int64(0), dl)
|
|
|
|
assert.Equal(t, int64(100*1048576), total)
|
|
|
|
ul, dl, total = user.GetDataTransferLimits("172.16.0.2")
|
|
|
|
assert.Equal(t, int64(120*1048576), ul)
|
|
|
|
assert.Equal(t, int64(150*1048576), 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,
|
|
|
|
0, 0, 0, 0, true, vfs.NewOsFs("", os.TempDir(), ""), dataprovider.TransferQuota{})
|
|
|
|
err := transfer.CheckRead()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = transfer.CheckWrite()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
transfer.transferQuota = dataprovider.TransferQuota{
|
|
|
|
AllowedTotalSize: 10,
|
|
|
|
}
|
|
|
|
transfer.BytesReceived = 5
|
|
|
|
transfer.BytesSent = 4
|
|
|
|
err = transfer.CheckRead()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = transfer.CheckWrite()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
transfer.BytesSent = 6
|
|
|
|
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)
|
|
|
|
|
|
|
|
transfer.BytesReceived = 11
|
|
|
|
err = transfer.CheckRead()
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), ErrReadQuotaExceeded.Error())
|
|
|
|
}
|
|
|
|
err = transfer.CheckWrite()
|
|
|
|
assert.True(t, conn.IsQuotaExceededError(err))
|
|
|
|
}
|