File: kacltest.cpp

package info (click to toggle)
kio 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,496 kB
  • sloc: cpp: 123,468; xml: 528; ansic: 466; ruby: 60; sh: 21; makefile: 13
file content (288 lines) | stat: -rw-r--r-- 7,853 bytes parent folder | download | duplicates (4)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
    This file is part of the KDE project
    SPDX-FileCopyrightText: 2005 Till Adam <adam@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "kacltest.h"

#include <QTest>
#include <config-kiocore.h>

#if HAVE_POSIX_ACL
#include <grp.h> // getgrnam()
#include <sys/types.h>
#endif

// The code comes partly from kdebase/kioslave/trash/testtrash.cpp

QTEST_MAIN(KACLTest)

KACLTest::KACLTest()
{
}

void KACLTest::initTestCase()
{
#if !HAVE_POSIX_ACL
    QSKIP("ACL support not compiled");
#endif
#ifdef Q_OS_FREEBSD
    QSKIP("The test is not adapted for FreeBSD yet");
#endif

    m_testACL = QStringLiteral(
        "user::rw-\n"
        "user:bin:rwx\n"
        "group::rw-\n"
        "mask::rwx\n"
        "other::r--\n");

    m_acl = KACL(m_testACL);

    // setACL call acl_from_text(), which seems to order the groups in the resulting ACL
    // according to group numeric Id, in ascending order. Find which group comes first
    // so that the tests pass regardless of which distro they're run on.
    auto *grpStruct = getgrnam("audio");
    m_audioGid = static_cast<int>(grpStruct->gr_gid);

    grpStruct = getgrnam("users");
    m_usersGid = static_cast<int>(grpStruct->gr_gid);

    QLatin1String orderedGroups;
    if (m_audioGid < m_usersGid) {
        orderedGroups = QLatin1String{
            "group:audio:--x\n"
            "group:users:r--\n"};
    } else {
        orderedGroups = QLatin1String{
            "group:users:r--\n"
            "group:audio:--x\n"};
    }

    m_testACL2 =
        QLatin1String{
            "user::rwx\n"
            "user:bin:rwx\n"
            "group::rw-\n"}
        + orderedGroups
        + QLatin1String{
            "mask::r-x\n"
            "other::r--\n"};

    m_testACLEffective =
        QLatin1String{
            "user::rwx\n"
            "user:bin:rwx    #effective:r-x\n"
            "group::rw-      #effective:r--\n"}
        + orderedGroups
        + QLatin1String{
            "mask::r-x\n"
            "other::r--\n"};

    QVERIFY(m_acl2.setACL(m_testACL2));
}

void KACLTest::testAsString()
{
    QCOMPARE(m_acl.asString(), m_testACL);
}

void KACLTest::testSetACL()
{
    QCOMPARE(m_acl2.asString().simplified(), m_testACLEffective.simplified());
}

void KACLTest::testGetOwnerPermissions()
{
    QCOMPARE(int(m_acl.ownerPermissions()), 6);
}

void KACLTest::testGetOwningGroupPermissions()
{
    QCOMPARE(int(m_acl.owningGroupPermissions()), 6);
}

void KACLTest::testGetOthersPermissions()
{
    QCOMPARE(int(m_acl.othersPermissions()), 4);
}

void KACLTest::testGetMaskPermissions()
{
    bool exists = false;
    int mask = m_acl.maskPermissions(exists);
    QVERIFY(exists);
    QCOMPARE(mask, 7);
}

void KACLTest::testGetAllUserPermissions()
{
    ACLUserPermissionsList list = m_acl.allUserPermissions();
    ACLUserPermissionsConstIterator it = list.constBegin();
    QString name;
    int permissions = 0;
    int count = 0;
    while (it != list.constEnd()) {
        name = (*it).first;
        permissions = (*it).second;
        ++it;
        ++count;
    }
    QCOMPARE(count, 1);
    QCOMPARE(name, QStringLiteral("bin"));
    QCOMPARE(permissions, 7);
}

void KACLTest::testGetAllGroupsPermissions()
{
    const ACLGroupPermissionsList list = m_acl2.allGroupPermissions();
    QCOMPARE(list.size(), 2);

    const bool isAudioFirst = m_audioGid < m_usersGid;

    QString name;
    int permissions;

    const auto acl1 = list.at(0);
    name = acl1.first;
    permissions = acl1.second;
    if (isAudioFirst) {
        QCOMPARE(name, QStringLiteral("audio"));
        QCOMPARE(permissions, 1);
    } else {
        QCOMPARE(name, QStringLiteral("users"));
        QCOMPARE(permissions, 4);
    }

    const auto acl2 = list.at(1);
    name = acl2.first;
    permissions = acl2.second;
    if (isAudioFirst) {
        QCOMPARE(name, QStringLiteral("users"));
        QCOMPARE(permissions, 4);
    } else {
        QCOMPARE(name, QStringLiteral("audio"));
        QCOMPARE(permissions, 1);
    }
}

void KACLTest::testIsExtended()
{
    KACL dukeOfMonmoth(m_testACL);
    QVERIFY(dukeOfMonmoth.isExtended());
    KACL earlOfUpnor(QStringLiteral("user::r--\ngroup::r--\nother::r--\n"));
    QVERIFY(!earlOfUpnor.isExtended());
}

void KACLTest::testOperators()
{
    KACL dukeOfMonmoth(m_testACL);
    KACL JamesScott(m_testACL);
    KACL earlOfUpnor(m_testACL2);
    QVERIFY(!(dukeOfMonmoth == earlOfUpnor));
    QVERIFY(dukeOfMonmoth != earlOfUpnor);
    QVERIFY(dukeOfMonmoth != earlOfUpnor);
    QVERIFY(!(dukeOfMonmoth != JamesScott));
}

void KACLTest::testSettingBasic()
{
    KACL CharlesII(m_testACL);
    CharlesII.setOwnerPermissions(7); // clearly
    CharlesII.setOwningGroupPermissions(0);
    CharlesII.setOthersPermissions(0);
    QCOMPARE(int(CharlesII.ownerPermissions()), 7);
    QCOMPARE(int(CharlesII.owningGroupPermissions()), 0);
    QCOMPARE(int(CharlesII.othersPermissions()), 0);
}

void KACLTest::testSettingExtended()
{
    KACL CharlesII(m_testACL);
    CharlesII.setMaskPermissions(7); // clearly
    bool dummy = false;
    QCOMPARE(int(CharlesII.maskPermissions(dummy)), 7);

    const QString expected(QStringLiteral("user::rw-\nuser:root:rwx\nuser:bin:r--\ngroup::rw-\nmask::rwx\nother::r--\n"));

    ACLUserPermissionsList users;
    ACLUserPermissions user = qMakePair(QStringLiteral("root"), (unsigned short)7);
    users.append(user);
    user = qMakePair(QStringLiteral("bin"), (unsigned short)4);
    users.append(user);
    CharlesII.setAllUserPermissions(users);
    QCOMPARE(CharlesII.asString(), expected);

    CharlesII.setACL(m_testACL); // reset
    // it already has an entry for bin, let's change it
    CharlesII.setNamedUserPermissions(QStringLiteral("bin"), 4);
    CharlesII.setNamedUserPermissions(QStringLiteral("root"), 7);
    QCOMPARE(CharlesII.asString(), expected);

    // groups, all and named

    QLatin1String orderedGroups;
    if (m_audioGid < m_usersGid) {
        orderedGroups = QLatin1String{
            "group:audio:-wx\n"
            "group:users:r--\n"};
    } else {
        orderedGroups = QLatin1String{
            "group:users:r--\n"
            "group:audio:-wx\n"};
    }

    const QString expected2 =
        QLatin1String{
            "user::rw-\n"
            "user:bin:rwx\n"
            "group::rw-\n"}
        + orderedGroups
        + QLatin1String{
            "mask::rwx\n"
            "other::r--\n"};

    CharlesII.setACL(m_testACL); // reset
    ACLGroupPermissionsList groups;
    ACLGroupPermissions group = qMakePair(QStringLiteral("audio"), (unsigned short)3);
    groups.append(group);
    group = qMakePair(QStringLiteral("users"), (unsigned short)4);
    groups.append(group);
    CharlesII.setAllGroupPermissions(groups);
    QCOMPARE(CharlesII.asString(), expected2);

    CharlesII.setACL(m_testACL); // reset
    CharlesII.setNamedGroupPermissions(QStringLiteral("audio"), 3);
    CharlesII.setNamedGroupPermissions(QStringLiteral("users"), 4);
    QCOMPARE(CharlesII.asString(), expected2);
}

void KACLTest::testSettingErrorHandling()
{
    KACL foo(m_testACL);
    bool v = foo.setNamedGroupPermissions(QStringLiteral("audio"), 7); // existing group
    QVERIFY(v);
    v = foo.setNamedGroupPermissions(QStringLiteral("jongel"), 7); // non-existing group
    QVERIFY(!v);

    v = foo.setNamedUserPermissions(QStringLiteral("bin"), 7); // existing user
    QVERIFY(v);
    v = foo.setNamedUserPermissions(QStringLiteral("jongel"), 7); // non-existing user
    QVERIFY(!v);
}

void KACLTest::testNewMask()
{
    KACL CharlesII(QStringLiteral("user::rw-\ngroup::rw-\nother::rw\n"));
    bool dummy = false;
    CharlesII.maskPermissions(dummy);
    QVERIFY(!dummy);

    CharlesII.setMaskPermissions(6);
    QCOMPARE(int(CharlesII.maskPermissions(dummy)), 6);
    QVERIFY(dummy); // mask exists now
}

#include "moc_kacltest.cpp"