From 075bbe2aef99d89549302e6098b088a5e22c25b2 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sun, 29 Mar 2020 11:10:03 +0200 Subject: [PATCH] added test case that checks quota for files inside virtual folders --- sftpd/sftpd_test.go | 68 +++++++++++++++++++++++++++++++++++++++++++++ vfs/osfs.go | 4 +-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/sftpd/sftpd_test.go b/sftpd/sftpd_test.go index b83195c1..5408862e 100644 --- a/sftpd/sftpd_test.go +++ b/sftpd/sftpd_test.go @@ -2316,6 +2316,74 @@ func TestVirtualFolders(t *testing.T) { os.RemoveAll(mappedPath) } +func TestVirtualFoldersQuota(t *testing.T) { + usePubKey := false + u := getTestUser(usePubKey) + u.QuotaFiles = 100 + mappedPath1 := filepath.Join(os.TempDir(), "vdir1") + vdirPath1 := "/vdir1" + mappedPath2 := filepath.Join(os.TempDir(), "vdir2") + vdirPath2 := "/vdir2" + u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{ + VirtualPath: vdirPath1, + MappedPath: mappedPath1, + }) + u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{ + VirtualPath: vdirPath2, + MappedPath: mappedPath2, + }) + os.MkdirAll(mappedPath1, 0777) + os.MkdirAll(mappedPath2, 0777) + user, _, err := httpd.AddUser(u, http.StatusOK) + if err != nil { + t.Errorf("unable to add user: %v", err) + } + client, err := getSftpClient(user, usePubKey) + if err != nil { + t.Errorf("unable to create sftp client: %v", err) + } else { + defer client.Close() + testFileName := "test_file.dat" + testFileSize := int64(131072) + testFilePath := filepath.Join(homeBasePath, testFileName) + err = createTestFile(testFilePath, testFileSize) + if err != nil { + t.Errorf("unable to create test file: %v", err) + } + err = sftpUploadFile(testFilePath, testFileName, testFileSize, client) + if err != nil { + t.Errorf("file upload error: %v", err) + } + err = sftpUploadFile(testFilePath, path.Join(vdirPath1, testFileName), testFileSize, client) + if err != nil { + t.Errorf("file upload error: %v", err) + } + err = sftpUploadFile(testFilePath, path.Join(vdirPath2, testFileName), testFileSize, client) + if err != nil { + t.Errorf("file upload error: %v", err) + } + expectedQuotaFiles := 3 + expectedQuotaSize := testFileSize * 3 + user, _, err = httpd.GetUserByID(user.ID, http.StatusOK) + if err != nil { + t.Errorf("error getting user: %v", err) + } + if expectedQuotaFiles != user.UsedQuotaFiles { + t.Errorf("quota files does not match, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles) + } + if expectedQuotaSize != user.UsedQuotaSize { + t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize) + } + } + _, err = httpd.RemoveUser(user, http.StatusOK) + if err != nil { + t.Errorf("unable to remove user: %v", err) + } + os.RemoveAll(user.GetHomeDir()) + os.RemoveAll(mappedPath1) + os.RemoveAll(mappedPath2) +} + func TestMissingFile(t *testing.T) { usePubKey := false u := getTestUser(usePubKey) diff --git a/vfs/osfs.go b/vfs/osfs.go index 4c3a07b9..657ebd35 100644 --- a/vfs/osfs.go +++ b/vfs/osfs.go @@ -178,7 +178,7 @@ func (fs OsFs) ScanRootDirContents() (int, int64, error) { num, s, err := fs.getDirSize(v.MappedPath) if err != nil { if fs.IsNotExist(err) { - fsLog(fs, logger.LevelWarn, "unable to scan contents for not existent mapped path: %#v", v.MappedPath) + fsLog(fs, logger.LevelWarn, "unable to scan contents for non-existent mapped path: %#v", v.MappedPath) continue } return numFiles, size, err @@ -237,7 +237,7 @@ func (fs OsFs) ResolvePath(sftpPath string) (string, error) { // path chain until we hit a directory that _does_ exist and can be validated. _, err = fs.findFirstExistingDir(r, basePath) if err != nil { - fsLog(fs, logger.LevelWarn, "error resolving not existent path: %#v", err) + fsLog(fs, logger.LevelWarn, "error resolving non-existent path: %#v", err) } return r, err }