File indexing completed on 2024-05-05 05:17:16

0001 /*
0002     SPDX-FileCopyrightText: 2008 Thomas McGuire <mcguire@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "identitytest.h"
0007 #include "identity.h"
0008 #include "identitymanager.h"
0009 #include <QTest>
0010 
0011 #include <KConfig>
0012 #include <KConfigGroup>
0013 
0014 #include <QDataStream>
0015 #include <QMimeData>
0016 #include <QStandardPaths>
0017 
0018 using namespace KIdentityManagementCore;
0019 
0020 QTEST_GUILESS_MAIN(IdentityTester)
0021 
0022 void IdentityTester::initTestCase()
0023 {
0024     QStandardPaths::setTestModeEnabled(true);
0025 }
0026 
0027 bool IdentityTester::compareIdentities(const Identity &actual, const Identity &expected)
0028 {
0029     bool ok = false;
0030     [&]() {
0031         QCOMPARE(actual.uoid(), expected.uoid());
0032         // Don't compare isDefault - only one copy can be default, so this property
0033         // is not copied! It does not affect result of operator==() either.
0034         // QCOMPARE(actual.isDefault(), expected.isDefault());
0035         QCOMPARE(actual.identityName(), expected.identityName());
0036         QCOMPARE(actual.fullName(), expected.fullName());
0037         QCOMPARE(actual.organization(), expected.organization());
0038         QCOMPARE(actual.pgpEncryptionKey(), expected.pgpEncryptionKey());
0039         QCOMPARE(actual.pgpSigningKey(), expected.pgpSigningKey());
0040         QCOMPARE(actual.smimeEncryptionKey(), expected.smimeEncryptionKey());
0041         QCOMPARE(actual.smimeSigningKey(), expected.smimeSigningKey());
0042         QCOMPARE(actual.preferredCryptoMessageFormat(), expected.preferredCryptoMessageFormat());
0043         QCOMPARE(actual.autocryptEnabled(), expected.autocryptEnabled());
0044         QCOMPARE(actual.autocryptPrefer(), expected.autocryptPrefer());
0045         QCOMPARE(actual.encryptionOverride(), expected.encryptionOverride());
0046         QCOMPARE(actual.warnNotEncrypt(), expected.warnNotEncrypt());
0047         QCOMPARE(actual.warnNotSign(), expected.warnNotSign());
0048         QCOMPARE(actual.emailAliases(), expected.emailAliases());
0049         QCOMPARE(actual.primaryEmailAddress(), expected.primaryEmailAddress());
0050         QCOMPARE(actual.vCardFile(), expected.vCardFile());
0051         QCOMPARE(actual.replyToAddr(), expected.replyToAddr());
0052         QCOMPARE(actual.bcc(), expected.bcc());
0053         QCOMPARE(actual.cc(), expected.cc());
0054         QCOMPARE(actual.attachVcard(), expected.attachVcard());
0055         QCOMPARE(actual.autocorrectionLanguage(), expected.autocorrectionLanguage());
0056         QCOMPARE(actual.disabledFcc(), expected.disabledFcc());
0057         QCOMPARE(actual.pgpAutoSign(), expected.pgpAutoSign());
0058         QCOMPARE(actual.pgpAutoEncrypt(), expected.pgpAutoEncrypt());
0059         QCOMPARE(actual.defaultDomainName(), expected.defaultDomainName());
0060         QCOMPARE(actual.signatureText(), expected.signatureText());
0061         QCOMPARE(const_cast<Identity &>(actual).signature(), const_cast<Identity &>(expected).signature());
0062         QCOMPARE(actual.transport(), expected.transport());
0063         QCOMPARE(actual.fcc(), expected.fcc());
0064         QCOMPARE(actual.drafts(), expected.drafts());
0065         QCOMPARE(actual.templates(), expected.templates());
0066         QCOMPARE(actual.dictionary(), expected.dictionary());
0067         QCOMPARE(actual.isXFaceEnabled(), expected.isXFaceEnabled());
0068         QCOMPARE(actual.xface(), expected.xface());
0069         QCOMPARE(actual.isFaceEnabled(), expected.isFaceEnabled());
0070         QCOMPARE(actual.face(), expected.face());
0071         ok = true;
0072     }();
0073 
0074     return ok;
0075 }
0076 
0077 void IdentityTester::test_Identity()
0078 {
0079     Identity identity;
0080     identity.setUoid(42);
0081     QCOMPARE(identity.uoid(), 42u);
0082     identity.setIsDefault(true);
0083     QCOMPARE(identity.isDefault(), true);
0084     identity.setIdentityName(QStringLiteral("01234"));
0085     QCOMPARE(identity.identityName(), QStringLiteral("01234"));
0086     identity.setFullName(QStringLiteral("Daniel Vrátil"));
0087     QCOMPARE(identity.fullName(), QStringLiteral("Daniel Vrátil"));
0088     identity.setOrganization(QStringLiteral("KDE"));
0089     QCOMPARE(identity.organization(), QStringLiteral("KDE"));
0090     identity.setPGPEncryptionKey("0x0123456789ABCDEF");
0091     QCOMPARE(identity.pgpEncryptionKey(), QByteArray("0x0123456789ABCDEF"));
0092     identity.setPGPSigningKey("0xFEDCBA9876543210");
0093     QCOMPARE(identity.pgpSigningKey(), QByteArray("0xFEDCBA9876543210"));
0094     identity.setSMIMEEncryptionKey("0xABCDEF0123456789");
0095     QCOMPARE(identity.smimeEncryptionKey(), QByteArray("0xABCDEF0123456789"));
0096     identity.setSMIMESigningKey("0xFEDCBA9876543210");
0097     QCOMPARE(identity.smimeSigningKey(), QByteArray("0xFEDCBA9876543210"));
0098     identity.setPreferredCryptoMessageFormat(QStringLiteral("PGP"));
0099     QCOMPARE(identity.preferredCryptoMessageFormat(), QStringLiteral("PGP"));
0100     identity.setPrimaryEmailAddress(QStringLiteral("dvratil@kde.org"));
0101     const QStringList aliases = {QStringLiteral("dvratil1@kde.org"), QStringLiteral("example@example.org")};
0102     identity.setEmailAliases(aliases);
0103     QCOMPARE(identity.emailAliases(), aliases);
0104     QVERIFY(identity.matchesEmailAddress(QStringLiteral("dvratil@kde.org")));
0105     QVERIFY(identity.matchesEmailAddress(aliases[0]));
0106     QVERIFY(identity.matchesEmailAddress(aliases[1]));
0107 
0108     QCOMPARE(identity.primaryEmailAddress(), QStringLiteral("dvratil@kde.org"));
0109     const auto vcardFile = QStringLiteral(
0110         "BEGIN:VCARD\n"
0111         "VERSION:2.1\n"
0112         "N:Vrátil;Daniel;;\n"
0113         "END:VCARD");
0114     identity.setVCardFile(vcardFile);
0115     QCOMPARE(identity.vCardFile(), vcardFile);
0116     identity.setReplyToAddr(QStringLiteral("dvratil+reply@kde.org"));
0117     QCOMPARE(identity.replyToAddr(), QStringLiteral("dvratil+reply@kde.org"));
0118     identity.setBcc(QStringLiteral("dvratil+bcc@kde.org"));
0119     QCOMPARE(identity.bcc(), QStringLiteral("dvratil+bcc@kde.org"));
0120     identity.setCc(QStringLiteral("dvratil+cc@kde.org"));
0121     QCOMPARE(identity.cc(), QStringLiteral("dvratil+cc@kde.org"));
0122     identity.setAttachVcard(true);
0123     QCOMPARE(identity.attachVcard(), true);
0124     identity.setAutocorrectionLanguage(QStringLiteral("cs_CZ"));
0125     QCOMPARE(identity.autocorrectionLanguage(), QStringLiteral("cs_CZ"));
0126     identity.setDisabledFcc(true);
0127     QVERIFY(identity.disabledFcc());
0128     identity.setPgpAutoSign(true);
0129     QVERIFY(identity.pgpAutoSign());
0130     identity.setPgpAutoEncrypt(true);
0131     QVERIFY(identity.pgpAutoEncrypt());
0132     QVERIFY(!identity.autocryptEnabled());
0133     identity.setAutocryptEnabled(true);
0134     QVERIFY(identity.autocryptEnabled());
0135 
0136     QVERIFY(!identity.autocryptPrefer());
0137     identity.setAutocryptPrefer(true);
0138     QVERIFY(identity.autocryptPrefer());
0139 
0140     QVERIFY(!identity.encryptionOverride());
0141     identity.setEncryptionOverride(true);
0142     QVERIFY(identity.encryptionOverride());
0143 
0144     QVERIFY(!identity.warnNotEncrypt());
0145     identity.setWarnNotEncrypt(true);
0146     QVERIFY(identity.warnNotEncrypt());
0147 
0148     QVERIFY(!identity.warnNotSign());
0149     identity.setWarnNotSign(true);
0150     QVERIFY(identity.warnNotSign());
0151 
0152     identity.setDefaultDomainName(QStringLiteral("kde.org"));
0153     QCOMPARE(identity.defaultDomainName(), QStringLiteral("kde.org"));
0154     Signature sig;
0155     sig.setEnabledSignature(true);
0156     sig.setText(QStringLiteral("Regards,\nDaniel"));
0157     identity.setSignature(sig);
0158     QCOMPARE(identity.signature(), sig);
0159     identity.setTransport(QStringLiteral("smtp"));
0160     QCOMPARE(identity.transport(), QStringLiteral("smtp"));
0161     identity.setFcc(QStringLiteral("123")); // must be an Akonadi::Collection::Id
0162     QCOMPARE(identity.fcc(), QStringLiteral("123"));
0163     identity.setDrafts(QStringLiteral("124"));
0164     QCOMPARE(identity.drafts(), QStringLiteral("124"));
0165     identity.setTemplates(QStringLiteral("125"));
0166     QCOMPARE(identity.templates(), QStringLiteral("125"));
0167     identity.setDictionary(QStringLiteral("Čeština"));
0168     QCOMPARE(identity.dictionary(), QStringLiteral("Čeština"));
0169     identity.setXFaceEnabled(true);
0170     QVERIFY(identity.isXFaceEnabled());
0171     identity.setXFace(QStringLiteral(":-P"));
0172     QCOMPARE(identity.xface(), QStringLiteral(":-P"));
0173     identity.setFaceEnabled(true);
0174     QVERIFY(identity.isFaceEnabled());
0175     identity.setFace(QStringLiteral(";-)"));
0176     QCOMPARE(identity.face(), QStringLiteral(";-)"));
0177 
0178     // Test copy
0179     {
0180         const Identity copy = identity;
0181         QCOMPARE(copy, identity);
0182         // Test that the operator==() actually works
0183         QVERIFY(compareIdentities(copy, identity));
0184 
0185         identity.setXFace(QStringLiteral(":-("));
0186         QVERIFY(copy != identity);
0187 
0188         identity.setFace(QStringLiteral(">:("));
0189         QVERIFY(copy != identity);
0190     }
0191 
0192     // Test serialization
0193     {
0194         QByteArray ba;
0195         QDataStream inStream(&ba, QIODevice::WriteOnly);
0196         inStream << identity;
0197 
0198         Identity copy;
0199         QDataStream outStream(&ba, QIODevice::ReadOnly);
0200         outStream >> copy;
0201 
0202         // We already verified that operator==() works, so this should be enough
0203         QVERIFY(compareIdentities(copy, identity));
0204     }
0205 }
0206 
0207 void IdentityTester::test_NullIdentity()
0208 {
0209     IdentityManager manager;
0210     QVERIFY(manager.identityForAddress(QStringLiteral("thisaddressforsuredoesnotexist@kde.org")).isNull());
0211 }
0212 
0213 void IdentityTester::test_Aliases()
0214 {
0215     IdentityManager manager;
0216 
0217     // It is picking up identities from older tests sometimes, so cleanup
0218     while (manager.identities().size() > 1) {
0219         QVERIFY(manager.removeIdentity(manager.identities().at(0)));
0220         manager.commit();
0221     }
0222 
0223     Identity &i1 = manager.newFromScratch(QStringLiteral("Test1"));
0224     i1.setPrimaryEmailAddress(QStringLiteral("firstname.lastname@example.com"));
0225     i1.setEmailAliases(QStringList() << QStringLiteral("firstname@example.com") << QStringLiteral("lastname@example.com"));
0226     QVERIFY(i1.matchesEmailAddress(QStringLiteral("\"Lastname, Firstname\" <firstname@example.com>")));
0227     QVERIFY(i1.matchesEmailAddress(QStringLiteral("\"Lastname, Firstname\" <firstname.lastname@example.com>")));
0228     QCOMPARE(i1.emailAliases().size(), 2);
0229 
0230     KConfig testConfig(QStringLiteral("test"));
0231     KConfigGroup testGroup(&testConfig, QStringLiteral("testGroup"));
0232     i1.writeConfig(testGroup);
0233     i1.readConfig(testGroup);
0234     QCOMPARE(i1.emailAliases().size(), 2);
0235 
0236     manager.commit();
0237 
0238     Identity &i2 = manager.newFromScratch(QStringLiteral("Test2"));
0239     i2.setPrimaryEmailAddress(QStringLiteral("test@test.de"));
0240     QVERIFY(i2.emailAliases().isEmpty());
0241     manager.commit();
0242 
0243     // Remove the first identity, which we couldn't remove above
0244     QVERIFY(manager.removeIdentity(manager.identities().at(0)));
0245     manager.commit();
0246 
0247     QCOMPARE(manager.allEmails().size(), 4);
0248     QCOMPARE(manager.identityForAddress(QStringLiteral("firstname@example.com")).identityName().toLatin1().data(), "Test1");
0249 }
0250 
0251 void IdentityTester::test_toMimeData()
0252 {
0253     Identity identity(QStringLiteral("Test1"));
0254     identity.setFullName(QStringLiteral("name"));
0255     QMimeData mimeData;
0256     identity.populateMimeData(&mimeData);
0257 
0258     Identity identity2 = Identity::fromMimeData(&mimeData);
0259 
0260     QVERIFY(compareIdentities(identity, identity2));
0261     QCOMPARE(identity, identity2);
0262 
0263     QCOMPARE(identity.fullName(), identity2.fullName());
0264 }
0265 
0266 void IdentityTester::test_migration()
0267 {
0268     Identity identity(QStringLiteral("Test1"));
0269     identity.setFullName(QStringLiteral("name"));
0270     QVERIFY(!identity.encryptionOverride());
0271     {
0272         KConfig config(QStringLiteral("test"));
0273         QVERIFY(config.isConfigWritable(true));
0274         KConfigGroup cg(&config, QStringLiteral("test"));
0275         identity.writeConfig(cg);
0276         config.sync();
0277     }
0278     { // Generate a config that triggers the migration code
0279         KConfig config(QStringLiteral("test_old"));
0280         QVERIFY(config.isConfigWritable(true));
0281         KConfigGroup cg(&config, QStringLiteral("test"));
0282         identity.writeConfig(cg);
0283         cg.deleteEntry(s_encryptionOverride);
0284         cg.deleteEntry(s_warnnotencrypt);
0285         cg.deleteEntry(s_warnnotsign);
0286         config.sync();
0287     }
0288     { // The migration is not triggerd
0289         KConfig config(QStringLiteral("test"));
0290         KConfigGroup cg(&config, QStringLiteral("test"));
0291         Identity i2;
0292         i2.readConfig(cg);
0293         QVERIFY(compareIdentities(i2, identity));
0294     }
0295     { // The migration is triggered
0296         // for old config files (< v5.21.41)
0297         KConfig config(QStringLiteral("test_old"));
0298         KConfigGroup cg(&config, QStringLiteral("test"));
0299         Identity i2;
0300         i2.readConfig(cg);
0301         QVERIFY(i2.encryptionOverride());
0302         i2.setEncryptionOverride(false);
0303         QVERIFY(compareIdentities(i2, identity));
0304     }
0305 }
0306 
0307 #include "moc_identitytest.cpp"