From f990d02235173f2c8caa96e5f6007dc1562e10f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Knau=C3=9F?= Date: Thu, 17 Nov 2016 14:19:16 +0100 Subject: check errorType for errors from gpgme --- framework/domain/mimetreeparser/interface.cpp | 53 ++++++++++++++++++++-- framework/domain/mimetreeparser/interface.h | 20 ++++---- .../domain/mimetreeparser/tests/gpgerrortest.cpp | 16 ++++++- 3 files changed, 71 insertions(+), 18 deletions(-) (limited to 'framework/domain/mimetreeparser') diff --git a/framework/domain/mimetreeparser/interface.cpp b/framework/domain/mimetreeparser/interface.cpp index 77ffa4fd..2380e624 100644 --- a/framework/domain/mimetreeparser/interface.cpp +++ b/framework/domain/mimetreeparser/interface.cpp @@ -172,6 +172,7 @@ class KeyPrivate public: Key *q; GpgME::Key mKey; + QByteArray mKeyID; }; Key::Key() @@ -194,23 +195,37 @@ Key::~Key() QString Key::keyid() const { - return d->mKey.keyID(); + if (!d->mKey.isNull()) { + return d->mKey.keyID(); + } + + return d->mKeyID; } QString Key::name() const { //FIXME: is this the correct way to get the primary UID? - return d->mKey.userID(0).name(); + if (!d->mKey.isNull()) { + return d->mKey.userID(0).name(); + } + + return QString(); } QString Key::email() const { - return d->mKey.userID(0).email(); + if (!d->mKey.isNull()) { + return d->mKey.userID(0).email(); + } + return QString(); } QString Key::comment() const { - return d->mKey.userID(0).comment(); + if (!d->mKey.isNull()) { + return d->mKey.userID(0).comment(); + } + return QString(); } class SignaturePrivate @@ -269,6 +284,8 @@ class EncryptionPrivate public: Encryption *q; std::vector mRecipients; + Encryption::ErrorType mErrorType; + QString mErrorString; }; Encryption::Encryption(EncryptionPrivate *d_ptr) @@ -281,6 +298,7 @@ Encryption::Encryption() :d(std::unique_ptr(new EncryptionPrivate)) { d->q = this; + d->mErrorType = Encryption::NoError; } Encryption::~Encryption() @@ -293,6 +311,17 @@ std::vector Encryption::recipients() const return d->mRecipients; } +QString Encryption::errorString() +{ + return d->mErrorString; +} + +Encryption::ErrorType Encryption::errorType() +{ + return d->mErrorType; +} + + class PartPrivate { public: @@ -381,6 +410,19 @@ Encryption::Ptr PartPrivate::createEncryption(const MimeTreeParser::EncryptedMes } auto encpriv = new EncryptionPrivate(); + if (part->passphraseError()) { + encpriv->mErrorType = Encryption::PassphraseError; + encpriv->mErrorString = part->mMetaData.errorText; + } else if (part->isEncrypted() && !part->isDecryptable()) { + encpriv->mErrorType = Encryption::KeyMissing; + encpriv->mErrorString = part->mMetaData.errorText; + } else if (!part->isEncrypted() && !part->isDecryptable()) { + encpriv->mErrorType = Encryption::UnknownError; + encpriv->mErrorString = part->mMetaData.errorText; + } else { + encpriv->mErrorType = Encryption::NoError; + } + foreach(const auto &recipient, part->mDecryptRecipients) { std::vector found_keys; const auto &keyid = recipient.keyID(); @@ -396,6 +438,9 @@ Encryption::Ptr PartPrivate::createEncryption(const MimeTreeParser::EncryptedMes if (found_keys.size() != 1) { // Should not Happen at this point qWarning() << "Oops: Found no Key for Fingerprint: " << keyid; + auto keypriv = new KeyPrivate; + keypriv->mKeyID = keyid; + encpriv->mRecipients.push_back(Key::Ptr(new Key(keypriv))); } else { auto key = found_keys[0]; auto keypriv = new KeyPrivate; diff --git a/framework/domain/mimetreeparser/interface.h b/framework/domain/mimetreeparser/interface.h index 1c6fd31d..7c3ea28b 100644 --- a/framework/domain/mimetreeparser/interface.h +++ b/framework/domain/mimetreeparser/interface.h @@ -255,18 +255,6 @@ private: friend class SinglePartPrivate; }; - -class EncryptionPart : public Part -{ -public: - typedef std::shared_ptr Ptr; - QByteArray type() const Q_DECL_OVERRIDE; - - EncryptionError error() const; -private: - std::unique_ptr d; -}; - /* * we want to request complete headers like: * from/to... @@ -344,11 +332,19 @@ public: class Encryption { public: + enum ErrorType { + NoError, + PassphraseError, + KeyMissing, + UnknownError + }; typedef std::shared_ptr Ptr; Encryption(); Encryption(EncryptionPrivate *); ~Encryption(); std::vector recipients() const; + QString errorString(); + ErrorType errorType(); private: std::unique_ptr d; }; diff --git a/framework/domain/mimetreeparser/tests/gpgerrortest.cpp b/framework/domain/mimetreeparser/tests/gpgerrortest.cpp index aca12280..4254d972 100644 --- a/framework/domain/mimetreeparser/tests/gpgerrortest.cpp +++ b/framework/domain/mimetreeparser/tests/gpgerrortest.cpp @@ -71,7 +71,10 @@ private slots: auto contentList = contentPart->content("plaintext"); QVERIFY(contentList[0]->content().startsWith("asdasd")); QCOMPARE(contentList[0]->encryptions().size(), 1); - QCOMPARE(contentList[0]->signatures().size(), 1); + auto enc = contentList[0]->encryptions()[0]; + QCOMPARE(enc->errorType(), Encryption::NoError); + QCOMPARE(enc->errorString(), QString()); + QCOMPARE((int) enc->recipients().size(), 2); } void testNoGPGInstalled_data() @@ -98,8 +101,12 @@ private slots: QCOMPARE(contentPart->availableContents(), QVector() << "plaintext"); auto contentList = contentPart->content("plaintext"); QCOMPARE(contentList[0]->encryptions().size(), 1); - QCOMPARE(contentList[0]->signatures().size(), 0); QVERIFY(contentList[0]->content().isEmpty()); + auto enc = contentList[0]->encryptions()[0]; + qDebug() << "HUHU"<< enc->errorType(); + QCOMPARE(enc->errorType(), Encryption::UnknownError); + QCOMPARE(enc->errorString(), QString("Crypto plug-in \"OpenPGP\" could not decrypt the data.
Error: No data")); + QCOMPARE((int) enc->recipients().size(), 0); } void testGpgIncorrectGPGHOME_data() @@ -126,6 +133,11 @@ private slots: QCOMPARE(contentList[0]->encryptions().size(), 1); QCOMPARE(contentList[0]->signatures().size(), 0); QVERIFY(contentList[0]->content().isEmpty()); + auto enc = contentList[0]->encryptions()[0]; + qDebug() << enc->errorType(); + QCOMPARE(enc->errorType(), Encryption::KeyMissing); + QCOMPARE(enc->errorString(), QString("Crypto plug-in \"OpenPGP\" could not decrypt the data.
Error: Decryption failed")); + QCOMPARE((int) enc->recipients().size(), 2); } public Q_SLOTS: -- cgit v1.2.3