File indexing completed on 2025-01-05 04:58:17

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