File indexing completed on 2025-03-09 04:53:56

0001 /*
0002   SPDX-FileCopyrightText: 2010 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
0003   SPDX-FileCopyrightText: 2010 Leo Franchi <lfranchi@kde.org>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "setupenv.h"
0009 
0010 #include <QGpgME/ExportJob>
0011 #include <QGpgME/ImportJob>
0012 #include <QGpgME/KeyListJob>
0013 #include <QGpgME/Protocol>
0014 #include <gpgme++/context.h>
0015 #include <gpgme++/importresult.h>
0016 #include <gpgme++/keylistresult.h>
0017 
0018 #include <QFile>
0019 #include <QProcess>
0020 #include <QStandardPaths>
0021 #include <QTest>
0022 
0023 using namespace MessageComposer;
0024 
0025 void Test::setupEnv()
0026 {
0027     QStandardPaths::setTestModeEnabled(true);
0028 }
0029 
0030 void Test::setupFullEnv()
0031 {
0032     setupEnv();
0033     qputenv("LC_ALL", "en_US.UTF-8");
0034     qputenv("TZ", "UTC");
0035 }
0036 
0037 std::vector<GpgME::Key, std::allocator<GpgME::Key>> Test::getKeys(bool smime)
0038 {
0039     QGpgME::KeyListJob *job = nullptr;
0040 
0041     if (smime) {
0042         const QGpgME::Protocol *const backend = QGpgME::smime();
0043         Q_ASSERT(backend);
0044         job = backend->keyListJob(false);
0045     } else {
0046         const QGpgME::Protocol *const backend = QGpgME::openpgp();
0047         Q_ASSERT(backend);
0048         job = backend->keyListJob(false);
0049     }
0050     Q_ASSERT(job);
0051 
0052     std::vector<GpgME::Key> keys;
0053     GpgME::KeyListResult res = job->exec(QStringList(), true, keys);
0054 
0055     if (!smime) {
0056         Q_ASSERT(keys.size() == 3);
0057     }
0058 
0059     Q_ASSERT(!res.error());
0060 
0061     /*
0062     qDebug() << "got private keys:" << keys.size();
0063 
0064     for (std::vector< GpgME::Key >::iterator i = keys.begin(); i != keys.end(); ++i) {
0065         qDebug() << "key isnull:" << i->isNull() << "isexpired:" << i->isExpired();
0066         qDebug() << "key numuserIds:" << i->numUserIDs();
0067         for (uint k = 0; k < i->numUserIDs(); ++k) {
0068             qDebug() << "userIDs:" << i->userID(k).email();
0069         }
0070     }
0071     */
0072 
0073     return keys;
0074 }
0075 
0076 KMime::Message::Ptr Test::loadMessage(const QString &filename)
0077 {
0078     QFile mailFile(filename);
0079     Q_ASSERT(mailFile.open(QIODevice::ReadOnly));
0080     const QByteArray mailData = KMime::CRLFtoLF(mailFile.readAll());
0081     Q_ASSERT(!mailData.isEmpty());
0082 
0083     KMime::Message::Ptr origMsg(new KMime::Message);
0084     origMsg->setContent(mailData);
0085     origMsg->parse();
0086     return origMsg;
0087 }
0088 
0089 KMime::Message::Ptr Test::loadMessageFromDataDir(const QString &filename)
0090 {
0091     return loadMessage(QLatin1StringView(QByteArray(MAIL_DATA_DIR "/" + filename.toLatin1())));
0092 }
0093 
0094 void Test::compareFile(KMime::Content *content, const QString &referenceFile)
0095 {
0096     QFileInfo fi(referenceFile);
0097     const QString actualFile = fi.fileName();
0098     QFile f(actualFile);
0099     QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
0100     const QByteArray encodedContent(content->encodedContent());
0101     f.write(encodedContent);
0102     if (!encodedContent.endsWith('\n')) {
0103         f.write("\n");
0104     }
0105     f.close();
0106     compareFile(actualFile, referenceFile);
0107 }
0108 
0109 void Test::compareFile(const QString &outFile, const QString &referenceFile)
0110 {
0111     QVERIFY(QFile::exists(outFile));
0112 
0113     // compare to reference file
0114     const auto args = QStringList() << QStringLiteral("-u") << referenceFile << outFile;
0115     QProcess proc;
0116     proc.setProcessChannelMode(QProcess::ForwardedChannels);
0117     proc.start(QStringLiteral("diff"), args);
0118     QVERIFY(proc.waitForFinished());
0119 
0120     QCOMPARE(proc.exitCode(), 0);
0121 }
0122 
0123 void Test::populateKeyring(const QString &gnupgHome, const GpgME::Key &key)
0124 {
0125     QEventLoop loop;
0126     const QGpgME::Protocol *proto(QGpgME::openpgp());
0127     auto exportJob = proto->publicKeyExportJob(false);
0128     QObject::connect(
0129         exportJob,
0130         &QGpgME::ExportJob::result,
0131         [&gnupgHome, &loop](const GpgME::Error &result, const QByteArray &keyData, const QString &auditLogAsHtml, const GpgME::Error &auditLogError) {
0132             Q_UNUSED(auditLogAsHtml);
0133             Q_UNUSED(auditLogError);
0134             loop.quit();
0135             QVERIFY(!result);
0136             populateKeyring(gnupgHome, keyData);
0137         });
0138     QStringList patterns = {QString::fromUtf8(key.primaryFingerprint())};
0139     exportJob->start(patterns);
0140     loop.exec();
0141 }
0142 
0143 void Test::populateKeyring(const QString &gnupgHome, const QByteArray &keydata)
0144 {
0145     const QGpgME::Protocol *proto(QGpgME::openpgp());
0146     auto importJob = proto->importJob();
0147     QGpgME::Job::context(importJob)->setEngineHomeDirectory(gnupgHome.toUtf8().constData());
0148     const auto result = importJob->exec(keydata);
0149     importJob->deleteLater();
0150 }