Files
nextcloud-desktop/test/testfilesystem.cpp
Jyrki Gadinger d9a0a6eab3 fix(filesystem): only change folder permissions if required
On Linux changing the permissions causes inotify to create a IN_ATTRIB
event -- even if the permissions stays the same.

Such an event is passed to the filesystem watcher which lets the client
schedule a new sync run.  In certain conditions, this could happen
during every sync run...

Signed-off-by: Jyrki Gadinger <nilsding@nilsding.org>
2025-05-28 08:51:08 +02:00

134 lines
4.2 KiB
C++

/*
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: CC0-1.0
*
* This software is in the public domain, furnished "as is", without technical
* support, and with no warranty, express or implied, as to its usefulness for
* any purpose.
*/
#include <QtTest>
#include <QTemporaryDir>
#include "common/filesystembase.h"
#include "logger.h"
#include "libsync/filesystem.h"
using namespace OCC;
namespace std_fs = std::filesystem;
class TestFileSystem : public QObject
{
Q_OBJECT
private:
QTemporaryDir testDir;
private Q_SLOTS:
void initTestCase()
{
OCC::Logger::instance()->setLogFlush(true);
OCC::Logger::instance()->setLogDebug(true);
QStandardPaths::setTestModeEnabled(true);
QDir dir(testDir.path());
dir.mkdir("existingDirectory");
}
#ifndef Q_OS_WIN
void testSetFolderPermissionsExistingDirectory_data()
{
constexpr auto perms_0555 =
std_fs::perms::owner_read | std_fs::perms::owner_exec
| std_fs::perms::group_read | std_fs::perms::group_exec
| std_fs::perms::others_read | std_fs::perms::others_exec;
constexpr auto perms_0755 = perms_0555 | std_fs::perms::owner_write;
constexpr auto perms_0775 = perms_0755 | std_fs::perms::group_write;
QTest::addColumn<std_fs::perms>("originalPermissions");
QTest::addColumn<FileSystem::FolderPermissions>("folderPermissions");
QTest::addColumn<bool>("expectedResult");
QTest::addColumn<bool>("expectedPermissionsChanged");
QTest::addColumn<std_fs::perms>("expectedPermissions");
QTest::newRow("0777, readonly -> 0555, changed")
<< std_fs::perms::all
<< FileSystem::FolderPermissions::ReadOnly
<< true
<< true
<< perms_0555;
QTest::newRow("0555, readonly -> 0555, not changed")
<< perms_0555
<< FileSystem::FolderPermissions::ReadOnly
<< true
<< false
<< perms_0555;
QTest::newRow("0777, readwrite -> 0775, changed")
<< std_fs::perms::all
<< FileSystem::FolderPermissions::ReadWrite
<< true
<< true
<< perms_0775;
QTest::newRow("0775, readwrite -> 0775, not changed")
<< perms_0775
<< FileSystem::FolderPermissions::ReadWrite
<< true
<< false
<< perms_0775;
QTest::newRow("0755, readwrite -> 0755, not changed")
<< perms_0755
<< FileSystem::FolderPermissions::ReadWrite
<< true
<< false
<< perms_0755;
QTest::newRow("0555, readwrite -> 0755, changed")
<< perms_0555
<< FileSystem::FolderPermissions::ReadWrite
<< true
<< true
<< perms_0755;
}
void testSetFolderPermissionsExistingDirectory()
{
QFETCH(std_fs::perms, originalPermissions);
QFETCH(FileSystem::FolderPermissions, folderPermissions);
QFETCH(bool, expectedResult);
QFETCH(bool, expectedPermissionsChanged);
QFETCH(std_fs::perms, expectedPermissions);
bool permissionsDidChange = false;
QString fullPath = testDir.filePath("existingDirectory");
const auto stdStrPath = fullPath.toStdWString();
std_fs::permissions(stdStrPath, originalPermissions);
QCOMPARE(FileSystem::setFolderPermissions(fullPath, folderPermissions, &permissionsDidChange), expectedResult);
const auto newPermissions = std_fs::status(stdStrPath).permissions();
QCOMPARE(newPermissions, expectedPermissions);
QCOMPARE(permissionsDidChange, expectedPermissionsChanged);
}
void testSetFolderPermissionsNonexistentDirectory()
{
bool permissionsDidChange = false;
QString fullPath = testDir.filePath("nonexistentDirectory");
QCOMPARE(FileSystem::setFolderPermissions("nonexistentDirectory", FileSystem::FolderPermissions::ReadOnly, &permissionsDidChange), false);
QCOMPARE(permissionsDidChange, false);
}
#endif
};
QTEST_GUILESS_MAIN(TestFileSystem)
#include "testfilesystem.moc"