From d308910cdfb31accae7cf790a956eb2f3cc6ae71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Thu, 13 Jan 2022 12:40:35 +0100 Subject: [PATCH] qt,tests: Add helper to hook up the test passphrase provider * lang/qt/tests/t-support.h, lang/qt/tests/t-support.cpp (class QGpgMETest): Add member function hookUpPassphraseProvider. Add member mPassphraseProvider. * lang/qt/tests/t-changeexpiryjob.cpp, lang/qt/tests/t-encrypt.cpp, lang/qt/tests/t-remarks.cpp, lang/qt/tests/t-tofuinfo.cpp, lang/qt/tests/t-trustsignatures.cpp, lang/qt/tests/t-various.cpp: Use new helper. -- GnuPG-bug-id: 5770 --- lang/qt/tests/t-changeexpiryjob.cpp | 35 +++------------- lang/qt/tests/t-encrypt.cpp | 31 ++++---------- lang/qt/tests/t-remarks.cpp | 65 +++++------------------------ lang/qt/tests/t-support.cpp | 18 +++++++- lang/qt/tests/t-support.h | 16 +++++++ lang/qt/tests/t-tofuinfo.cpp | 5 +-- lang/qt/tests/t-trustsignatures.cpp | 56 ++++--------------------- lang/qt/tests/t-various.cpp | 22 ++-------- 8 files changed, 69 insertions(+), 179 deletions(-) diff --git a/lang/qt/tests/t-changeexpiryjob.cpp b/lang/qt/tests/t-changeexpiryjob.cpp index e20d1be2..090002f3 100644 --- a/lang/qt/tests/t-changeexpiryjob.cpp +++ b/lang/qt/tests/t-changeexpiryjob.cpp @@ -76,12 +76,7 @@ private Q_SLOTS: // Create the job auto job = std::unique_ptr{openpgp()->changeExpiryJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job.get()); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job.get()); // Use defaults of job @@ -144,12 +139,7 @@ private Q_SLOTS: // Create the job auto job = std::unique_ptr{openpgp()->changeExpiryJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job.get()); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job.get()); // Use defaults of job @@ -212,12 +202,7 @@ private Q_SLOTS: // Create the job auto job = std::unique_ptr{openpgp()->changeExpiryJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job.get()); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job.get()); // Set up the job job->setOptions(ChangeExpiryJob::UpdatePrimaryKey); @@ -280,12 +265,7 @@ private Q_SLOTS: // Create the job auto job = std::unique_ptr{openpgp()->changeExpiryJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job.get()); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job.get()); // Set up the job job->setOptions(ChangeExpiryJob::UpdatePrimaryKey); @@ -353,12 +333,7 @@ private Q_SLOTS: // Create the job auto job = std::unique_ptr{openpgp()->changeExpiryJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job.get()); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job.get()); // Set up the job job->setOptions(ChangeExpiryJob::UpdatePrimaryKey | ChangeExpiryJob::UpdateAllSubkeys); diff --git a/lang/qt/tests/t-encrypt.cpp b/lang/qt/tests/t-encrypt.cpp index 2249de3b..6a4c68e9 100644 --- a/lang/qt/tests/t-encrypt.cpp +++ b/lang/qt/tests/t-encrypt.cpp @@ -89,10 +89,7 @@ private Q_SLOTS: return; } auto decJob = openpgp()->decryptJob(); - auto ctx = Job::context(decJob); - TestPassphraseProvider provider; - ctx->setPassphraseProvider(&provider); - ctx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(decJob); QByteArray plainText; auto decResult = decJob->exec(cipherText, plainText); QVERIFY(!decResult.error()); @@ -160,10 +157,8 @@ private Q_SLOTS: return; } auto job = openpgp()->encryptJob(); + hookUpPassphraseProvider(job); auto ctx = Job::context(job); - TestPassphraseProvider provider; - ctx->setPassphraseProvider(&provider); - ctx->setPinentryMode(Context::PinentryLoopback); ctx->setArmor(true); ctx->setTextMode(true); QByteArray cipherText; @@ -176,9 +171,7 @@ private Q_SLOTS: killAgent(mDir.path()); auto decJob = openpgp()->decryptJob(); - auto ctx2 = Job::context(decJob); - ctx2->setPassphraseProvider(&provider); - ctx2->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(decJob); QByteArray plainText; auto decResult = decJob->exec(cipherText, plainText); QVERIFY(!result.error()); @@ -201,13 +194,9 @@ private Q_SLOTS: delete listjob; auto job = openpgp()->signEncryptJob(/*ASCII Armor */true, /* Textmode */ true); - - auto encSignCtx = Job::context(job); - TestPassphraseProvider provider1; - encSignCtx->setPassphraseProvider(&provider1); - encSignCtx->setPinentryMode(Context::PinentryLoopback); - QVERIFY(job); + hookUpPassphraseProvider(job); + QByteArray cipherText; auto result = job->exec(keys, keys, QStringLiteral("Hello World").toUtf8(), Context::AlwaysTrust, cipherText); delete job; @@ -222,10 +211,8 @@ private Q_SLOTS: } auto decJob = openpgp()->decryptJob(); + hookUpPassphraseProvider(decJob); auto ctx = Job::context(decJob); - TestPassphraseProvider provider; - ctx->setPassphraseProvider(&provider); - ctx->setPinentryMode(Context::PinentryLoopback); ctx->setDecryptionFlags(Context::DecryptUnwrap); QByteArray plainText; @@ -266,9 +253,8 @@ private: delete listjob; auto job = openpgp()->encryptJob(); + hookUpPassphraseProvider(job); auto ctx = Job::context(job); - ctx->setPassphraseProvider(new TestPassphraseProvider); - ctx->setPinentryMode(Context::PinentryLoopback); ctx->setArmor(true); ctx->setTextMode(true); QByteArray cipherText; @@ -294,9 +280,8 @@ private: agentConf.close(); auto decJob = openpgp()->decryptJob(); + hookUpPassphraseProvider(decJob); auto ctx2 = Job::context(decJob); - ctx2->setPassphraseProvider(new TestPassphraseProvider); - ctx2->setPinentryMode(Context::PinentryLoopback); ctx2->setTextMode(true); QByteArray plainText; auto decResult = decJob->exec(cipherText, plainText); diff --git a/lang/qt/tests/t-remarks.cpp b/lang/qt/tests/t-remarks.cpp index bba14ce9..f9a901ab 100644 --- a/lang/qt/tests/t-remarks.cpp +++ b/lang/qt/tests/t-remarks.cpp @@ -74,12 +74,7 @@ public: // Create the job auto job = openpgp()->signKeyJob(); QVERIFY (job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the job job->setExportable(false); @@ -130,12 +125,7 @@ private Q_SLOTS: // Create the job auto job = openpgp()->signKeyJob(); QVERIFY (job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the job job->setExportable(true); @@ -167,11 +157,7 @@ private Q_SLOTS: // Now replace the remark auto job3 = openpgp()->signKeyJob(); QVERIFY (job3); - - // Hack in the passphrase provider - auto jobCtx3 = Job::context(job3); - jobCtx3->setPassphraseProvider(&provider); - jobCtx3->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job3); // Setup the job job3->setExportable(false); @@ -226,12 +212,7 @@ private Q_SLOTS: // Create the job auto job = openpgp()->signKeyJob(); QVERIFY (job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the first job job->setExportable(false); @@ -254,11 +235,7 @@ private Q_SLOTS: // Now another remark from zulu auto job3 = openpgp()->signKeyJob(); QVERIFY (job3); - - // Hack in the passphrase provider - auto jobCtx3 = Job::context(job3); - jobCtx3->setPassphraseProvider(&provider); - jobCtx3->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job3); // Setup the job job3->setExportable(false); @@ -312,12 +289,7 @@ private Q_SLOTS: // Create the job auto job = openpgp()->signKeyJob(); QVERIFY (job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the job job->setExportable(false); @@ -349,11 +321,7 @@ private Q_SLOTS: // Now replace the remark auto job3 = openpgp()->signKeyJob(); QVERIFY (job3); - - // Hack in the passphrase provider - auto jobCtx3 = Job::context(job3); - jobCtx3->setPassphraseProvider(&provider); - jobCtx3->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job3); // Setup the job job3->setExportable(false); @@ -402,12 +370,7 @@ private Q_SLOTS: // Create the job auto job = openpgp()->signKeyJob(); QVERIFY (job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the job job->setExportable(false); @@ -438,11 +401,7 @@ private Q_SLOTS: // Try to replace it without dupeOK auto job2 = openpgp()->signKeyJob(); QVERIFY (job2); - - // Hack in the passphrase provider - auto jobCtx2 = Job::context(job2); - jobCtx2->setPassphraseProvider(&provider); - jobCtx2->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job2); // Setup the job job2->setExportable(false); @@ -463,11 +422,7 @@ private Q_SLOTS: // Now replace the remark auto job3 = openpgp()->signKeyJob(); QVERIFY (job3); - - // Hack in the passphrase provider - auto jobCtx3 = Job::context(job3); - jobCtx3->setPassphraseProvider(&provider); - jobCtx3->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job3); // Setup the job job3->setExportable(false); diff --git a/lang/qt/tests/t-support.cpp b/lang/qt/tests/t-support.cpp index 854dd7bd..e827b517 100644 --- a/lang/qt/tests/t-support.cpp +++ b/lang/qt/tests/t-support.cpp @@ -35,7 +35,8 @@ #endif #include "t-support.h" -#include "context.h" + +#include "job.h" #include @@ -44,8 +45,12 @@ #include #include +#include "context.h" #include "engineinfo.h" +using namespace GpgME; +using namespace QGpgME; + void QGpgMETest::initTestCase() { GpgME::initializeLibrary(); @@ -92,6 +97,17 @@ bool QGpgMETest::copyKeyrings(const QString &src, const QString &dest) return true; } +void QGpgMETest::hookUpPassphraseProvider(GpgME::Context *context) +{ + context->setPassphraseProvider(&mPassphraseProvider); + context->setPinentryMode(Context::PinentryLoopback); +} + +void QGpgMETest::hookUpPassphraseProvider(QGpgME::Job *job) +{ + hookUpPassphraseProvider(Job::context(job)); +} + void killAgent(const QString& dir) { QProcess proc; diff --git a/lang/qt/tests/t-support.h b/lang/qt/tests/t-support.h index 0cff0c17..0d5757c8 100644 --- a/lang/qt/tests/t-support.h +++ b/lang/qt/tests/t-support.h @@ -38,6 +38,16 @@ #include +namespace GpgME +{ +class Context; +} + +namespace QGpgME +{ +class Job; +} + namespace QTest { template <> @@ -78,9 +88,15 @@ protected: bool copyKeyrings(const QString &from, const QString& to); + void hookUpPassphraseProvider(GpgME::Context *context); + void hookUpPassphraseProvider(QGpgME::Job *job); + public Q_SLOTS: void initTestCase(); void cleanupTestCase(); + +private: + GpgME::TestPassphraseProvider mPassphraseProvider; }; /* Timeout, in milliseconds, for use with QSignalSpy to wait on diff --git a/lang/qt/tests/t-tofuinfo.cpp b/lang/qt/tests/t-tofuinfo.cpp index 61543056..a1871c4f 100644 --- a/lang/qt/tests/t-tofuinfo.cpp +++ b/lang/qt/tests/t-tofuinfo.cpp @@ -153,10 +153,7 @@ private: void signAndVerify(const QString &what, const GpgME::Key &key, int expected) { auto job = openpgp()->signJob(); - auto ctx = Job::context(job); - TestPassphraseProvider provider; - ctx->setPassphraseProvider(&provider); - ctx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); std::vector keys; keys.push_back(key); diff --git a/lang/qt/tests/t-trustsignatures.cpp b/lang/qt/tests/t-trustsignatures.cpp index f908c674..6f7ad827 100644 --- a/lang/qt/tests/t-trustsignatures.cpp +++ b/lang/qt/tests/t-trustsignatures.cpp @@ -80,12 +80,7 @@ private Q_SLOTS: // Create the job auto job = std::unique_ptr{openpgp()->signKeyJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job.get()); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job.get()); // Setup the job job->setExportable(true); @@ -128,12 +123,7 @@ private Q_SLOTS: // Create the job auto job = std::unique_ptr{openpgp()->signKeyJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job.get()); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job.get()); // Setup the job job->setExportable(true); @@ -201,12 +191,7 @@ private Q_SLOTS: // Create the job auto job = openpgp()->signKeyJob();//std::unique_ptr{openpgp()->signKeyJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the job job->setExportable(true); @@ -249,12 +234,7 @@ private Q_SLOTS: // Create the job auto job = openpgp()->signKeyJob();//std::unique_ptr{openpgp()->signKeyJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the job job->setExportable(true); @@ -322,12 +302,7 @@ private Q_SLOTS: // Create the job auto job = openpgp()->signKeyJob();//std::unique_ptr{openpgp()->signKeyJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the job job->setExportable(true); @@ -371,12 +346,7 @@ private Q_SLOTS: // Create the job auto job = openpgp()->signKeyJob();//std::unique_ptr{openpgp()->signKeyJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the job job->setExportable(true); @@ -444,12 +414,7 @@ private Q_SLOTS: // Create the job auto job = openpgp()->signKeyJob();//std::unique_ptr{openpgp()->signKeyJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the job job->setExportable(true); @@ -492,12 +457,7 @@ private Q_SLOTS: // Create the job auto job = openpgp()->signKeyJob();//std::unique_ptr{openpgp()->signKeyJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job); // Setup the job job->setExportable(true); diff --git a/lang/qt/tests/t-various.cpp b/lang/qt/tests/t-various.cpp index 1bb490f5..b630350c 100644 --- a/lang/qt/tests/t-various.cpp +++ b/lang/qt/tests/t-various.cpp @@ -134,9 +134,7 @@ private Q_SLOTS: auto ctx = Context::createForProtocol(key.protocol()); QVERIFY (ctx); - TestPassphraseProvider provider; - ctx->setPassphraseProvider(&provider); - ctx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(ctx); QVERIFY(!ctx->addUid(key, uid)); delete ctx; @@ -187,9 +185,7 @@ private Q_SLOTS: auto ctx = Context::createForProtocol(key.protocol()); QVERIFY (ctx); - TestPassphraseProvider provider; - ctx->setPassphraseProvider(&provider); - ctx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(ctx); // change expiration of the main key QVERIFY(!ctx->setExpire(key, 1000)); @@ -256,12 +252,7 @@ private Q_SLOTS: // Create the job auto job = std::unique_ptr{openpgp()->signKeyJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job.get()); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job.get()); // Setup the job job->setExportable(true); @@ -316,12 +307,7 @@ private Q_SLOTS: // Create the job auto job = std::unique_ptr{openpgp()->signKeyJob()}; QVERIFY(job); - - // Hack in the passphrase provider - auto jobCtx = Job::context(job.get()); - TestPassphraseProvider provider; - jobCtx->setPassphraseProvider(&provider); - jobCtx->setPinentryMode(Context::PinentryLoopback); + hookUpPassphraseProvider(job.get()); // Setup the job job->setExportable(true);