From 2187a4de8c1731d8621a4b056baf84ee29e0ff00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20Kl=C3=B6cker?= Date: Wed, 12 Jun 2024 11:34:41 +0200 Subject: [PATCH] qt: Allow specifying import options when importing keys * lang/qt/src/importjob.cpp, lang/qt/src/importjob.h (class ImportJob): Add member functions setImportOptions and importOptions. * lang/qt/src/importjob_p.h (struct ImportJobPrivate): Add member m_importOptions. * lang/qt/src/qgpgmeimportjob.cpp (import_qba): Add arg importOptions and adjust the callers. Set import options context flag. * lang/qt/tests/t-import.cpp (ImportTest): Add member function testImportWithImportOptions. -- GnuPG-bug-id: 7152 --- lang/qt/src/importjob.cpp | 12 ++++++++++ lang/qt/src/importjob.h | 3 +++ lang/qt/src/importjob_p.h | 1 + lang/qt/src/qgpgmeimportjob.cpp | 12 ++++++---- lang/qt/tests/t-import.cpp | 42 +++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 4 deletions(-) diff --git a/lang/qt/src/importjob.cpp b/lang/qt/src/importjob.cpp index ee5318a4..8d70367f 100644 --- a/lang/qt/src/importjob.cpp +++ b/lang/qt/src/importjob.cpp @@ -55,6 +55,18 @@ QString QGpgME::ImportJob::importFilter() const return d->m_importFilter; } +void QGpgME::ImportJob::setImportOptions(const QString &options) +{ + const auto d = jobPrivate(this); + d->m_importOptions = options; +} + +QString QGpgME::ImportJob::importOptions() const +{ + const auto d = jobPrivate(this); + return d->m_importOptions; +} + void ImportJob::setKeyOrigin(GpgME::Key::Origin origin, const QString &url) { const auto d = jobPrivate(this); diff --git a/lang/qt/src/importjob.h b/lang/qt/src/importjob.h index 0d677bed..a24de0fa 100644 --- a/lang/qt/src/importjob.h +++ b/lang/qt/src/importjob.h @@ -79,6 +79,9 @@ public: void setImportFilter(const QString &filter); QString importFilter() const; + void setImportOptions(const QString &options); + QString importOptions() const; + void setKeyOrigin(GpgME::Key::Origin origin, const QString &url = {}); GpgME::Key::Origin keyOrigin() const; QString keyOriginUrl() const; diff --git a/lang/qt/src/importjob_p.h b/lang/qt/src/importjob_p.h index 9f0a35ae..037e0a63 100644 --- a/lang/qt/src/importjob_p.h +++ b/lang/qt/src/importjob_p.h @@ -44,6 +44,7 @@ namespace QGpgME struct ImportJobPrivate : public JobPrivate { QString m_importFilter; + QString m_importOptions; GpgME::Key::Origin m_keyOrigin = GpgME::Key::OriginUnknown; QString m_keyOriginUrl; }; diff --git a/lang/qt/src/qgpgmeimportjob.cpp b/lang/qt/src/qgpgmeimportjob.cpp index 848bcacf..0f90df42 100644 --- a/lang/qt/src/qgpgmeimportjob.cpp +++ b/lang/qt/src/qgpgmeimportjob.cpp @@ -106,11 +106,15 @@ static const char *originToString(Key::Origin origin) } static QGpgMEImportJob::result_type import_qba(Context *ctx, const QByteArray &certData, const QString &importFilter, - Key::Origin keyOrigin, const QString &keyOriginUrl) + const QString &importOptions, Key::Origin keyOrigin, + const QString &keyOriginUrl) { if (!importFilter.isEmpty()) { ctx->setFlag("import-filter", importFilter.toStdString().c_str()); } + if (!importOptions.isEmpty()) { + ctx->setFlag("import-options", importOptions.toStdString().c_str()); + } if (keyOrigin != Key::OriginUnknown) { if (const auto origin = originToString(keyOrigin)) { std::string value{origin}; @@ -149,19 +153,19 @@ static QGpgMEImportJob::result_type import_qba(Context *ctx, const QByteArray &c Error QGpgMEImportJob::start(const QByteArray &certData) { - run(std::bind(&import_qba, std::placeholders::_1, certData, importFilter(), keyOrigin(), keyOriginUrl())); + run(std::bind(&import_qba, std::placeholders::_1, certData, importFilter(), importOptions(), keyOrigin(), keyOriginUrl())); return Error(); } GpgME::ImportResult QGpgME::QGpgMEImportJob::exec(const QByteArray &keyData) { - const result_type r = import_qba(context(), keyData, importFilter(), keyOrigin(), keyOriginUrl()); + const result_type r = import_qba(context(), keyData, importFilter(), importOptions(), keyOrigin(), keyOriginUrl()); return std::get<0>(r); } Error QGpgMEImportJob::startLater(const QByteArray &certData) { - setWorkerFunction(std::bind(&import_qba, std::placeholders::_1, certData, importFilter(), keyOrigin(), keyOriginUrl())); + setWorkerFunction(std::bind(&import_qba, std::placeholders::_1, certData, importFilter(), importOptions(), keyOrigin(), keyOriginUrl())); return {}; } diff --git a/lang/qt/tests/t-import.cpp b/lang/qt/tests/t-import.cpp index 91d5bcbd..79edf4d1 100644 --- a/lang/qt/tests/t-import.cpp +++ b/lang/qt/tests/t-import.cpp @@ -121,6 +121,48 @@ private Q_SLOTS: QCOMPARE(key.userID(0).id(), "importWithImportFilter@example.net"); } + void testImportWithImportOptions() + { + if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.23") { + QSKIP("gpg does not yet support --import-options show-only"); + } + + // pub ed25519 2024-06-12 [SC] + // A52F4947AF1506F3A7572EFC140278B773CA7C16 + // uid importOptions@example.net + static const char keyFpr[] = "A52F4947AF1506F3A7572EFC140278B773CA7C16"; + static const char keyData[] = + "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + "\n" + "mDMEZmlpmBYJKwYBBAHaRw8BAQdAZaSopKwccTwnMlJBVCWMT6et1T1WF9EkXdJi\n" + "gzI74xW0GWltcG9ydE9wdGlvbnNAZXhhbXBsZS5uZXSIkwQTFgoAOxYhBKUvSUev\n" + "FQbzp1cu/BQCeLdzynwWBQJmaWmYAhsDBQsJCAcCAiICBhUKCQgLAgQWAgMBAh4H\n" + "AheAAAoJEBQCeLdzynwWjmQBAP4dQEN/M4/dKIAlxNAbWzIkV+eSoUFLJszOJ/xx\n" + "FwJzAP43gkdXkUsHZt/U3mLZqtiHJFd7JxVm7hKRoAVBhZZYDw==\n" + "=7Z1j\n" + "-----END PGP PUBLIC KEY BLOCK-----\n"; + + auto *job = openpgp()->importJob(); + job->setImportOptions(QLatin1String{"show-only"}); + connect(job, &ImportJob::result, this, + [this](ImportResult result, QString, Error) + { + QVERIFY(!result.error()); + QCOMPARE(result.numConsidered(), 0); + QCOMPARE(result.numImported(), 0); + QVERIFY(result.imports().empty()); + Q_EMIT asyncDone(); + }); + job->start(QByteArray{keyData}); + QSignalSpy spy (this, SIGNAL(asyncDone())); + QVERIFY(spy.wait()); + + auto ctx = std::unique_ptr(Context::createForProtocol(GpgME::OpenPGP)); + GpgME::Error err; + const auto key = ctx->key(keyFpr, err, false); + QVERIFY(key.isNull()); + } + void testImportWithKeyOrigin() { if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.22") {