qt: Add pattern used for WKD lookup to result

* lang/qt/src/qgpgmewkdlookupjob.cpp (run_wkd_get): Change type of
arg email.
(lookup_keys): Add pattern to result.
* lang/qt/src/wkdlookupresult.h (class WKDLookupResult): Add arg
pattern to c'tors. Add member function pattern.
* lang/qt/src/wkdlookupresult.cpp (class WKDLookupResult::Private):
Add field pattern.
(WKDLookupResult::WKDLookupResult): Pass pattern to pimpl.
(WKDLookupResult::pattern): New.

* lang/qt/tests/t-wkdlookup.cpp (testWKDLookupAsync, testWKDLookupSync,
testLookupWithNoResultAsync): Check pattern.
--

GnuPG-bug-id: 5728
This commit is contained in:
Ingo Klöcker 2021-12-14 14:49:39 +01:00
parent fd680254da
commit af820811ec
4 changed files with 23 additions and 12 deletions

View File

@ -118,11 +118,11 @@ static GpgME::Error setUpDirmngrAssuanConnection(Context *ctx)
return err; return err;
} }
static GpgME::Error run_wkd_get(Context *ctx, const QString &email) static GpgME::Error run_wkd_get(Context *ctx, const std::string &email)
{ {
Error err; Error err;
const auto cmd = std::string{"WKD_GET "} + email.toUtf8().toStdString(); const auto cmd = std::string{"WKD_GET "} + email;
err = ctx->assuanTransact(cmd.c_str()); err = ctx->assuanTransact(cmd.c_str());
if (err.code() == GPG_ERR_NO_NAME || err.code() == GPG_ERR_NO_DATA) { if (err.code() == GPG_ERR_NO_NAME || err.code() == GPG_ERR_NO_DATA) {
// ignore those benign errors; GPG_ERR_NO_NAME indicates that the domain // ignore those benign errors; GPG_ERR_NO_NAME indicates that the domain
@ -146,8 +146,9 @@ static QGpgMEWKDLookupJob::result_type lookup_keys(Context *ctx, const QString &
Error err = setUpDirmngrAssuanConnection(ctx); Error err = setUpDirmngrAssuanConnection(ctx);
const auto pattern = email.toUtf8().toStdString();
if (!err) { if (!err) {
err = run_wkd_get(ctx, email); err = run_wkd_get(ctx, pattern);
} }
if (!err) { if (!err) {
@ -156,14 +157,14 @@ static QGpgMEWKDLookupJob::result_type lookup_keys(Context *ctx, const QString &
const auto rawData = transaction->data(); const auto rawData = transaction->data();
if (rawData.size() == 0) { if (rawData.size() == 0) {
qCDebug(QGPGME_LOG) << "No key found for" << email; qCDebug(QGPGME_LOG) << "No key found for" << email;
result = WKDLookupResult{GpgME::Data::null, {}, {}}; result = WKDLookupResult{pattern, GpgME::Data::null, {}, {}};
} else { } else {
qCDebug(QGPGME_LOG) << "Found key for" << email << "at" << source.c_str(); qCDebug(QGPGME_LOG) << "Found key for" << email << "at" << source.c_str();
result = WKDLookupResult{GpgME::Data{rawData.c_str(), rawData.size()}, source, {}}; result = WKDLookupResult{pattern, GpgME::Data{rawData.c_str(), rawData.size()}, source, {}};
} }
} }
return std::make_tuple(err ? WKDLookupResult{err} : result, QString{}, Error{}); return std::make_tuple(err ? WKDLookupResult{pattern, err} : result, QString{}, Error{});
} }
Error QGpgMEWKDLookupJob::start(const QString &email) Error QGpgMEWKDLookupJob::start(const QString &email)

View File

@ -45,6 +45,7 @@ using namespace GpgME;
class WKDLookupResult::Private class WKDLookupResult::Private
{ {
public: public:
std::string pattern;
GpgME::Data keyData; GpgME::Data keyData;
std::string source; std::string source;
}; };
@ -53,15 +54,15 @@ WKDLookupResult::WKDLookupResult() = default;
WKDLookupResult::~WKDLookupResult() = default; WKDLookupResult::~WKDLookupResult() = default;
WKDLookupResult::WKDLookupResult(const Error &error) WKDLookupResult::WKDLookupResult(const std::string &pattern, const Error &error)
: Result{error} : Result{error}
, d{} , d{new Private{pattern, {}, {}}}
{ {
} }
WKDLookupResult::WKDLookupResult(const Data &keyData, const std::string &source, const Error &error) WKDLookupResult::WKDLookupResult(const std::string &pattern, const Data &keyData, const std::string &source, const Error &error)
: Result{error} : Result{error}
, d{new Private{keyData, source}} , d{new Private{pattern, keyData, source}}
{ {
} }
@ -95,6 +96,11 @@ bool WKDLookupResult::isNull() const
return !d && !bool(error()); return !d && !bool(error());
} }
std::string WKDLookupResult::pattern() const
{
return d ? d->pattern : std::string{};
}
Data WKDLookupResult::keyData() const Data WKDLookupResult::keyData() const
{ {
return d ? d->keyData : Data{}; return d ? d->keyData : Data{};

View File

@ -55,8 +55,8 @@ public:
WKDLookupResult(); WKDLookupResult();
~WKDLookupResult(); ~WKDLookupResult();
explicit WKDLookupResult(const GpgME::Error &err); explicit WKDLookupResult(const std::string &pattern, const GpgME::Error &err);
explicit WKDLookupResult(const GpgME::Data &keyData, const std::string &source, const GpgME::Error &err); explicit WKDLookupResult(const std::string &pattern, const GpgME::Data &keyData, const std::string &source, const GpgME::Error &err);
WKDLookupResult(const WKDLookupResult &other); WKDLookupResult(const WKDLookupResult &other);
WKDLookupResult &operator=(const WKDLookupResult &other); WKDLookupResult &operator=(const WKDLookupResult &other);
@ -68,6 +68,7 @@ public:
bool isNull() const; bool isNull() const;
std::string pattern() const;
GpgME::Data keyData() const; GpgME::Data keyData() const;
std::string source() const; std::string source() const;

View File

@ -97,6 +97,7 @@ private Q_SLOTS:
QVERIFY(spy.wait(QSIGNALSPY_TIMEOUT)); QVERIFY(spy.wait(QSIGNALSPY_TIMEOUT));
QVERIFY(result.error().code() == GPG_ERR_NO_ERROR); QVERIFY(result.error().code() == GPG_ERR_NO_ERROR);
QCOMPARE(result.pattern(), "wk@gnupg.org");
QCOMPARE(result.source(), "https://openpgpkey.gnupg.org"); QCOMPARE(result.source(), "https://openpgpkey.gnupg.org");
const auto keys = result.keyData().toKeys(GpgME::OpenPGP); const auto keys = result.keyData().toKeys(GpgME::OpenPGP);
QVERIFY(keys.size() == 1); QVERIFY(keys.size() == 1);
@ -117,6 +118,7 @@ private Q_SLOTS:
const auto result = job->exec(email); const auto result = job->exec(email);
QVERIFY(result.error().code() == GPG_ERR_NO_ERROR); QVERIFY(result.error().code() == GPG_ERR_NO_ERROR);
QCOMPARE(result.pattern(), "wk@gnupg.org");
QCOMPARE(result.source(), "https://openpgpkey.gnupg.org"); QCOMPARE(result.source(), "https://openpgpkey.gnupg.org");
const auto keys = result.keyData().toKeys(GpgME::OpenPGP); const auto keys = result.keyData().toKeys(GpgME::OpenPGP);
QVERIFY(keys.size() == 1); QVERIFY(keys.size() == 1);
@ -145,6 +147,7 @@ private Q_SLOTS:
QVERIFY(spy.wait(QSIGNALSPY_TIMEOUT)); QVERIFY(spy.wait(QSIGNALSPY_TIMEOUT));
QVERIFY(result.error().code() == GPG_ERR_NO_ERROR); QVERIFY(result.error().code() == GPG_ERR_NO_ERROR);
QCOMPARE(result.pattern(), "alfa@example.net");
QCOMPARE(result.source(), ""); QCOMPARE(result.source(), "");
QVERIFY(result.keyData().isNull()); QVERIFY(result.keyData().isNull());
} }