File indexing completed on 2024-12-01 04:48:10

0001 /*
0002     SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "../imapaclattribute.h"
0008 #include <QTest>
0009 
0010 using namespace Akonadi;
0011 
0012 using ImapAcl = QMap<QByteArray, KIMAP::Acl::Rights>;
0013 
0014 Q_DECLARE_METATYPE(ImapAcl)
0015 Q_DECLARE_METATYPE(KIMAP::Acl::Rights)
0016 
0017 class ImapAclAttributeTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 private Q_SLOTS:
0022     void shouldHaveDefaultValue()
0023     {
0024         ImapAclAttribute attr;
0025         QVERIFY(attr.oldRights().isEmpty());
0026         QVERIFY(attr.rights().isEmpty());
0027         QVERIFY(!attr.myRights());
0028     }
0029 
0030     void shouldBuildAttribute()
0031     {
0032         QMap<QByteArray, KIMAP::Acl::Rights> right;
0033         right.insert("test", KIMAP::Acl::Admin);
0034         right.insert("foo", KIMAP::Acl::Admin);
0035 
0036         QMap<QByteArray, KIMAP::Acl::Rights> oldright;
0037         right.insert("test", KIMAP::Acl::Delete);
0038         right.insert("foo", KIMAP::Acl::Delete);
0039         ImapAclAttribute attr(right, oldright);
0040         QCOMPARE(attr.oldRights(), oldright);
0041         QCOMPARE(attr.rights(), right);
0042     }
0043 
0044     void shouldAssignValue()
0045     {
0046         ImapAclAttribute attr;
0047         QMap<QByteArray, KIMAP::Acl::Rights> right;
0048         right.insert("test", KIMAP::Acl::Admin);
0049         right.insert("foo", KIMAP::Acl::Admin);
0050         attr.setRights(right);
0051         QCOMPARE(attr.rights(), right);
0052     }
0053 
0054     void shouldCloneAttr()
0055     {
0056         ImapAclAttribute attr;
0057         QMap<QByteArray, KIMAP::Acl::Rights> right;
0058         right.insert("test", KIMAP::Acl::Admin);
0059         right.insert("foo", KIMAP::Acl::Admin);
0060         attr.setRights(right);
0061         ImapAclAttribute *clone = attr.clone();
0062         QVERIFY(attr == *clone);
0063         delete clone;
0064     }
0065 
0066     void shouldSerializedAttribute()
0067     {
0068         QMap<QByteArray, KIMAP::Acl::Rights> right;
0069         right.insert("test", KIMAP::Acl::Admin);
0070         right.insert("foo", KIMAP::Acl::Admin);
0071 
0072         QMap<QByteArray, KIMAP::Acl::Rights> oldright;
0073         right.insert("test", KIMAP::Acl::Delete);
0074         right.insert("foo", KIMAP::Acl::Delete);
0075         ImapAclAttribute attr(right, oldright);
0076         const QByteArray ba = attr.serialized();
0077         ImapAclAttribute result;
0078         result.deserialize(ba);
0079         QVERIFY(attr == result);
0080     }
0081 
0082     void shouldHaveType()
0083     {
0084         ImapAclAttribute attr;
0085         QCOMPARE(attr.type(), QByteArray("imapacl"));
0086     }
0087 
0088     void testMyRights()
0089     {
0090         ImapAclAttribute attr;
0091         KIMAP::Acl::Rights myRight = KIMAP::Acl::Admin;
0092 
0093         attr.setMyRights(myRight);
0094         QCOMPARE(attr.myRights(), myRight);
0095 
0096         ImapAclAttribute *clone = attr.clone();
0097         QCOMPARE(clone->myRights(), myRight);
0098 
0099         QVERIFY(*clone == attr);
0100 
0101         clone->setMyRights(KIMAP::Acl::Custom0);
0102         QVERIFY(!(*clone == attr));
0103         delete clone;
0104     }
0105 
0106     void testDeserialize_data()
0107     {
0108         QTest::addColumn<ImapAcl>("rights");
0109         QTest::addColumn<KIMAP::Acl::Rights>("myRights");
0110         QTest::addColumn<QByteArray>("serialized");
0111 
0112         KIMAP::Acl::Rights rights = KIMAP::Acl::None;
0113 
0114         {
0115             ImapAcl acl;
0116             QTest::newRow("empty") << acl << KIMAP::Acl::Rights(KIMAP::Acl::None) << QByteArray(" %% ");
0117         }
0118 
0119         {
0120             ImapAcl acl;
0121             acl.insert("user@host", rights);
0122             QTest::newRow("none") << acl << KIMAP::Acl::Rights(KIMAP::Acl::None) << QByteArray("user@host  %% ");
0123         }
0124 
0125         {
0126             ImapAcl acl;
0127             acl.insert("user@host", KIMAP::Acl::Lookup);
0128             QTest::newRow("lookup") << acl << KIMAP::Acl::Rights(KIMAP::Acl::None) << QByteArray("user@host l %% ");
0129         }
0130 
0131         {
0132             ImapAcl acl;
0133             acl.insert("user@host", KIMAP::Acl::Lookup | KIMAP::Acl::Read);
0134             QTest::newRow("lookup/read") << acl << KIMAP::Acl::Rights(KIMAP::Acl::None) << QByteArray("user@host lr %% ");
0135         }
0136 
0137         {
0138             ImapAcl acl;
0139             acl.insert("user@host", KIMAP::Acl::Lookup | KIMAP::Acl::Read);
0140             acl.insert("otheruser@host", KIMAP::Acl::Lookup | KIMAP::Acl::Read);
0141             QTest::newRow("lookup/read") << acl << KIMAP::Acl::Rights(KIMAP::Acl::None) << QByteArray("otheruser@host lr % user@host lr %% ");
0142         }
0143 
0144         {
0145             QTest::newRow("myrights") << ImapAcl() << KIMAP::Acl::rightsFromString("lrswipckxtdaen") << QByteArray(" %%  %% lrswipckxtdaen");
0146         }
0147     }
0148 
0149     void testDeserialize()
0150     {
0151         QFETCH(ImapAcl, rights);
0152         QFETCH(KIMAP::Acl::Rights, myRights);
0153         QFETCH(QByteArray, serialized);
0154 
0155         ImapAclAttribute deserializeAttr;
0156         deserializeAttr.deserialize(serialized);
0157         QCOMPARE(deserializeAttr.rights(), rights);
0158         QCOMPARE(deserializeAttr.myRights(), myRights);
0159     }
0160 
0161     void testSerializeDeserialize_data()
0162     {
0163         QTest::addColumn<ImapAcl>("rights");
0164         QTest::addColumn<KIMAP::Acl::Rights>("myRights");
0165         QTest::addColumn<QByteArray>("serialized");
0166         QTest::addColumn<QByteArray>("oldSerialized");
0167 
0168         ImapAcl acl;
0169         QTest::newRow("empty") << acl << KIMAP::Acl::Rights(KIMAP::Acl::None) << QByteArray(" %% ") << QByteArray("testme@host l %% ");
0170 
0171         acl.insert("user@host", KIMAP::Acl::None);
0172         QTest::newRow("none") << acl << KIMAP::Acl::Rights(KIMAP::Acl::None) << QByteArray("user@host  %% ") << QByteArray("testme@host l %% user@host ");
0173 
0174         acl.insert("user@host", KIMAP::Acl::Lookup);
0175         QTest::newRow("lookup") << acl << KIMAP::Acl::Rights(KIMAP::Acl::None) << QByteArray("user@host l %% ") << QByteArray("testme@host l %% user@host l");
0176 
0177         acl.insert("user@host", KIMAP::Acl::Lookup | KIMAP::Acl::Read);
0178         QTest::newRow("lookup/read") << acl << KIMAP::Acl::Rights(KIMAP::Acl::None) << QByteArray("user@host lr %% ")
0179                                      << QByteArray("testme@host l %% user@host lr");
0180 
0181         acl.insert("otheruser@host", KIMAP::Acl::Lookup | KIMAP::Acl::Read);
0182         QTest::newRow("lookup/read") << acl << KIMAP::Acl::Rights(KIMAP::Acl::None) << QByteArray("otheruser@host lr % user@host lr %% ")
0183                                      << QByteArray("testme@host l %% otheruser@host lr % user@host lr");
0184 
0185         QTest::newRow("myrights") << acl << KIMAP::Acl::rightsFromString("lrswipckxtdaen")
0186                                   << QByteArray("otheruser@host lr % user@host lr %%  %% lrswipckxtdaen")
0187                                   << QByteArray("testme@host l %% otheruser@host lr % user@host lr %% lrswipckxtdaen");
0188     }
0189 
0190     void testSerializeDeserialize()
0191     {
0192         QFETCH(ImapAcl, rights);
0193         QFETCH(KIMAP::Acl::Rights, myRights);
0194         QFETCH(QByteArray, serialized);
0195         QFETCH(QByteArray, oldSerialized);
0196 
0197         auto attr = new ImapAclAttribute();
0198         attr->setRights(rights);
0199         attr->setMyRights(myRights);
0200         QCOMPARE(attr->serialized(), serialized);
0201 
0202         ImapAcl acl;
0203         acl.insert("testme@host", KIMAP::Acl::Lookup);
0204         attr->setRights(acl);
0205 
0206         QCOMPARE(attr->serialized(), oldSerialized);
0207 
0208         delete attr;
0209 
0210         ImapAclAttribute deserializeAttr;
0211         deserializeAttr.deserialize(serialized);
0212         QCOMPARE(deserializeAttr.rights(), rights);
0213         QCOMPARE(deserializeAttr.myRights(), myRights);
0214     }
0215 
0216     void testOldRights()
0217     {
0218         ImapAcl acls;
0219         acls.insert("first_user@host", KIMAP::Acl::Lookup | KIMAP::Acl::Read);
0220         acls.insert("second_user@host", KIMAP::Acl::Lookup | KIMAP::Acl::Read);
0221         acls.insert("third_user@host", KIMAP::Acl::Lookup | KIMAP::Acl::Read);
0222 
0223         auto attr = new ImapAclAttribute();
0224         attr->setRights(acls);
0225 
0226         ImapAcl oldAcls = acls;
0227 
0228         acls.remove("first_user@host");
0229         acls.remove("third_user@host");
0230 
0231         attr->setRights(acls);
0232 
0233         QCOMPARE(attr->oldRights(), oldAcls);
0234 
0235         attr->setRights(acls);
0236 
0237         QCOMPARE(attr->oldRights(), acls);
0238         delete attr;
0239     }
0240 };
0241 
0242 QTEST_MAIN(ImapAclAttributeTest)
0243 
0244 #include "imapaclattributetest.moc"