File indexing completed on 2024-06-02 05:29:12

0001 /*
0002   SPDX-FileCopyrightText: 2022 Sandro Knauß <knauss@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "autocryptresolvercoretest.h"
0008 
0009 #include <MessageComposer/AutocryptKeyResolverCore>
0010 
0011 #include "qtest_messagecomposer.h"
0012 #include "setupenv.h"
0013 
0014 #include <Libkleo/KeyCache>
0015 
0016 #include <QStandardPaths>
0017 #include <QTest>
0018 
0019 QTEST_MAIN(AutocryptKeyResolverCoreTest)
0020 
0021 using namespace MessageComposer;
0022 
0023 void AutocryptKeyResolverCoreTest::initTestCase()
0024 {
0025     Test::setupFullEnv();
0026 
0027     const QDir genericDataLocation(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
0028     autocryptDir = QDir(genericDataLocation.filePath(QStringLiteral("autocrypt")));
0029 }
0030 
0031 void AutocryptKeyResolverCoreTest::init()
0032 {
0033     autocryptDir.removeRecursively();
0034     autocryptDir.mkpath(QStringLiteral("."));
0035 
0036     // hold a reference to the key cache to avoid rebuilding while the test is running
0037     mKeyCache = Kleo::KeyCache::instance();
0038     // make sure that the key cache has been populated
0039     (void)mKeyCache->keys();
0040 }
0041 
0042 void AutocryptKeyResolverCoreTest::cleanup()
0043 {
0044     autocryptDir.removeRecursively();
0045     QVERIFY(mKeyCache.use_count() == 1);
0046     mKeyCache.reset();
0047 }
0048 
0049 void AutocryptKeyResolverCoreTest::testAutocryptKeyResolver()
0050 {
0051     QStringList recipients = {QStringLiteral("recipient@autocrypt.example"), QStringLiteral("recipient2@autocrypt.example")};
0052 
0053     QFile file1(QLatin1StringView(MAIL_DATA_DIR) + QStringLiteral("/autocrypt/recipient%40autocrypt.example.json"));
0054     QVERIFY(file1.copy(autocryptDir.filePath(QStringLiteral("recipient%40autocrypt.example.json"))));
0055     QFile file2(QLatin1StringView(MAIL_DATA_DIR) + QStringLiteral("/autocrypt/recipient2%40autocrypt.example.json"));
0056     QVERIFY(file2.copy(autocryptDir.filePath(QStringLiteral("recipient2%40autocrypt.example.json"))));
0057 
0058     AutocryptKeyResolverCore resolver(/*encrypt=*/true, /*sign=*/false);
0059     resolver.setRecipients(recipients);
0060 
0061     const auto result = resolver.resolve();
0062 
0063     QCOMPARE(result.flags & Kleo::KeyResolverCore::ResolvedMask, Kleo::KeyResolverCore::AllResolved);
0064     QCOMPARE(result.flags & Kleo::KeyResolverCore::ProtocolsMask, Kleo::KeyResolverCore::OpenPGPOnly);
0065     QCOMPARE(result.solution.protocol, GpgME::OpenPGP);
0066     QCOMPARE(result.solution.encryptionKeys.value(recipients[0]).size(), 1);
0067     QCOMPARE(resolver.isAutocryptKey(recipients[0]), true);
0068     QCOMPARE(resolver.isGossipKey(recipients[0]), true);
0069     QCOMPARE(result.solution.encryptionKeys.value(recipients[1]).size(), 1);
0070     QCOMPARE(resolver.isAutocryptKey(recipients[1]), true);
0071     QCOMPARE(resolver.isGossipKey(recipients[1]), false);
0072 }
0073 
0074 void AutocryptKeyResolverCoreTest::testAutocryptKeyResolverSkipSender()
0075 {
0076     QStringList recipients = {QStringLiteral("recipient@autocrypt.example"), QStringLiteral("recipient2@autocrypt.example")};
0077 
0078     QFile file1(QLatin1StringView(MAIL_DATA_DIR) + QStringLiteral("/autocrypt/recipient%40autocrypt.example.json"));
0079     QVERIFY(file1.copy(autocryptDir.filePath(QStringLiteral("recipient%40autocrypt.example.json"))));
0080     QFile file2(QLatin1StringView(MAIL_DATA_DIR) + QStringLiteral("/autocrypt/recipient2%40autocrypt.example.json"));
0081     QVERIFY(file2.copy(autocryptDir.filePath(QStringLiteral("recipient2%40autocrypt.example.json"))));
0082 
0083     AutocryptKeyResolverCore resolver(/*encrypt=*/true, /*sign=*/false);
0084     resolver.setSender(QStringLiteral("recipient@autocrypt.example"));
0085     resolver.setRecipients(recipients);
0086 
0087     const auto result = resolver.resolve();
0088 
0089     QCOMPARE(result.flags & Kleo::KeyResolverCore::ResolvedMask, Kleo::KeyResolverCore::SomeUnresolved);
0090     QCOMPARE(result.flags & Kleo::KeyResolverCore::ProtocolsMask, Kleo::KeyResolverCore::OpenPGPOnly);
0091     QCOMPARE(result.solution.protocol, GpgME::OpenPGP);
0092     QCOMPARE(result.solution.encryptionKeys.value(recipients[0]).size(), 0);
0093     QCOMPARE(resolver.isAutocryptKey(recipients[0]), false);
0094     QCOMPARE(resolver.isGossipKey(recipients[0]), false);
0095     QCOMPARE(result.solution.encryptionKeys.value(recipients[1]).size(), 1);
0096     QCOMPARE(resolver.isAutocryptKey(recipients[1]), true);
0097     QCOMPARE(resolver.isGossipKey(recipients[1]), false);
0098 }
0099 
0100 void AutocryptKeyResolverCoreTest::testAutocryptKeyResolverUnresolved()
0101 {
0102     QStringList recipients = {QStringLiteral("recipient@autocrypt.example"), QStringLiteral("recipient2@autocrypt.example"), QStringLiteral("unresolved@test.example")};
0103 
0104     QFile file1(QLatin1StringView(MAIL_DATA_DIR) + QStringLiteral("/autocrypt/recipient%40autocrypt.example.json"));
0105     QVERIFY(file1.copy(autocryptDir.filePath(QStringLiteral("recipient%40autocrypt.example.json"))));
0106     QFile file2(QLatin1StringView(MAIL_DATA_DIR) + QStringLiteral("/autocrypt/recipient2%40autocrypt.example.json"));
0107     QVERIFY(file2.copy(autocryptDir.filePath(QStringLiteral("recipient2%40autocrypt.example.json"))));
0108 
0109     AutocryptKeyResolverCore resolver(/*encrypt=*/true, /*sign=*/false);
0110     resolver.setRecipients(recipients);
0111 
0112     const auto result = resolver.resolve();
0113 
0114     QCOMPARE(result.flags & Kleo::KeyResolverCore::ResolvedMask, Kleo::KeyResolverCore::SomeUnresolved);
0115     QCOMPARE(result.flags & Kleo::KeyResolverCore::ProtocolsMask, Kleo::KeyResolverCore::OpenPGPOnly);
0116     QCOMPARE(result.solution.protocol, GpgME::OpenPGP);
0117     QCOMPARE(result.solution.encryptionKeys.value(recipients[0]).size(), 1);
0118     QCOMPARE(resolver.isAutocryptKey(recipients[0]), true);
0119     QCOMPARE(resolver.isGossipKey(recipients[0]), true);
0120     QCOMPARE(result.solution.encryptionKeys.value(recipients[1]).size(), 1);
0121     QCOMPARE(resolver.isAutocryptKey(recipients[1]), true);
0122     QCOMPARE(resolver.isGossipKey(recipients[1]), false);
0123     QCOMPARE(result.solution.encryptionKeys.value(recipients[2]).size(), 0);
0124     QCOMPARE(resolver.isAutocryptKey(recipients[2]), false);
0125     QCOMPARE(resolver.isGossipKey(recipients[2]), false);
0126 }
0127 
0128 void AutocryptKeyResolverCoreTest::testAutocryptKeyResolverPreferNormal()
0129 {
0130     QStringList recipients = {QStringLiteral("recipient@autocrypt.example"), QStringLiteral("test@kolab.org")};
0131     auto key = mKeyCache->findBestByMailBox("test@kolab.org", GpgME::OpenPGP, Kleo::KeyCache::KeyUsage::Encrypt);
0132 
0133     QFile file1(QLatin1StringView(MAIL_DATA_DIR) + QStringLiteral("/autocrypt/recipient%40autocrypt.example.json"));
0134     QVERIFY(file1.copy(autocryptDir.filePath(QStringLiteral("recipient%40autocrypt.example.json"))));
0135     QFile file2(QLatin1StringView(MAIL_DATA_DIR) + QStringLiteral("/autocrypt/recipient2%40autocrypt.example.json"));
0136     QVERIFY(file2.copy(autocryptDir.filePath(QStringLiteral("test%40kolab.org.json"))));
0137 
0138     AutocryptKeyResolverCore resolver(/*encrypt=*/true, /*sign=*/false);
0139     resolver.setRecipients(recipients);
0140 
0141     const auto result = resolver.resolve();
0142 
0143     QCOMPARE(result.flags & Kleo::KeyResolverCore::ResolvedMask, Kleo::KeyResolverCore::AllResolved);
0144     QCOMPARE(result.flags & Kleo::KeyResolverCore::ProtocolsMask, Kleo::KeyResolverCore::OpenPGPOnly);
0145     QCOMPARE(result.solution.protocol, GpgME::OpenPGP);
0146     QCOMPARE(result.solution.encryptionKeys.value(recipients[0]).size(), 1);
0147     QCOMPARE(resolver.isAutocryptKey(recipients[0]), true);
0148     QCOMPARE(result.solution.encryptionKeys.value(recipients[1]).size(), 1);
0149     QCOMPARE(result.solution.encryptionKeys.value(recipients[1]).at(0).primaryFingerprint(), key.primaryFingerprint());
0150     QCOMPARE(resolver.isAutocryptKey(recipients[1]), false);
0151 }
0152 
0153 void AutocryptKeyResolverCoreTest::testNormalKeyResolver()
0154 {
0155     AutocryptKeyResolverCore resolver(/*encrypt=*/true, /*sign=*/false);
0156     QString recipient(QStringLiteral("test@kolab.org"));
0157     resolver.setRecipients({recipient});
0158 
0159     const auto result = resolver.resolve();
0160 
0161     QCOMPARE(result.flags & Kleo::KeyResolverCore::ResolvedMask, Kleo::KeyResolverCore::AllResolved);
0162     QCOMPARE(result.flags & Kleo::KeyResolverCore::ProtocolsMask, Kleo::KeyResolverCore::OpenPGPOnly);
0163     QCOMPARE(result.solution.protocol, GpgME::OpenPGP);
0164     QCOMPARE(result.solution.encryptionKeys.value(recipient).size(), 1);
0165     QCOMPARE(resolver.isAutocryptKey(recipient), false);
0166     QCOMPARE(resolver.isGossipKey(recipient), false);
0167 }
0168 
0169 #include "moc_autocryptresolvercoretest.cpp"