File: testfilesystem.cpp

package info (click to toggle)
nextcloud-desktop 4.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,740 kB
  • sloc: cpp: 119,301; objc: 752; python: 606; ansic: 389; sh: 377; makefile: 44; javascript: 32; xml: 6
file content (159 lines) | stat: -rw-r--r-- 5,151 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * 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 <QTemporaryFile>

#include "common/filesystembase.h"
#include "logger.h"

#include "libsync/filesystem.h"

using namespace OCC;
using namespace Qt::StringLiterals;
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

    void testSetFileReadOnlyLongPath()
    {
        // this should be enough to hit Windows' default MAX_PATH limitation (260 chars)
        QTemporaryFile longFile(u"test"_s.repeated(60));

        // QTemporaryFile::open actually creates the file
        QVERIFY(longFile.open());
        QVERIFY(!longFile.fileName().isEmpty());

        QFileInfo fileInfo(longFile);
        const auto path = fileInfo.absoluteFilePath();

        // ensure we start with a read/writeable directory
        QVERIFY(FileSystem::isWritable(path));

        FileSystem::setFileReadOnly(path, true);
        // path should now be readonly
        QVERIFY(!FileSystem::isWritable(path));

        FileSystem::setFileReadOnly(path, false);
        // path should now be writable aagain
        QVERIFY(FileSystem::isWritable(path));
    }
};

QTEST_GUILESS_MAIN(TestFileSystem)
#include "testfilesystem.moc"