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
This commit is contained in:
Ingo Klöcker 2022-01-13 12:40:35 +01:00
parent a54402fc2d
commit d308910cdf
8 changed files with 69 additions and 179 deletions

View File

@ -76,12 +76,7 @@ private Q_SLOTS:
// Create the job
auto job = std::unique_ptr<ChangeExpiryJob>{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<ChangeExpiryJob>{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<ChangeExpiryJob>{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<ChangeExpiryJob>{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<ChangeExpiryJob>{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);

View File

@ -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);

View File

@ -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);

View File

@ -35,7 +35,8 @@
#endif
#include "t-support.h"
#include "context.h"
#include "job.h"
#include <QTest>
@ -44,8 +45,12 @@
#include <QObject>
#include <QDir>
#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;

View File

@ -38,6 +38,16 @@
#include <gpg-error.h>
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

View File

@ -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<Key> keys;
keys.push_back(key);

View File

@ -80,12 +80,7 @@ private Q_SLOTS:
// Create the job
auto job = std::unique_ptr<SignKeyJob>{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<SignKeyJob>{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<SignKeyJob>{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<SignKeyJob>{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<SignKeyJob>{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<SignKeyJob>{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<SignKeyJob>{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<SignKeyJob>{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);

View File

@ -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<SignKeyJob>{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<SignKeyJob>{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);