From 391e55411cda11446ca9de4dd0dc2b54d3e6fff5 Mon Sep 17 00:00:00 2001 From: Andre Heinecke Date: Fri, 12 Aug 2016 16:55:51 +0200 Subject: [PATCH] Qt: Add test for progress signal of encryptjob * lang/qt/tests/t-encrypt.cpp (testProgress): New. -- This tests that a ByteArray IODevice now gives proper progress signals. --- lang/qt/tests/t-encrypt.cpp | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/lang/qt/tests/t-encrypt.cpp b/lang/qt/tests/t-encrypt.cpp index c6fcaa21..708c2050 100644 --- a/lang/qt/tests/t-encrypt.cpp +++ b/lang/qt/tests/t-encrypt.cpp @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include "keylistjob.h" #include "encryptjob.h" #include "qgpgmeencryptjob.h" @@ -39,8 +41,11 @@ #include "qgpgmedecryptjob.h" #include "qgpgmebackend.h" #include "keylistresult.h" +#include "engineinfo.h" #include "t-support.h" +#define PROGRESS_TEST_SIZE 1 * 1024 * 1024 + using namespace QGpgME; using namespace GpgME; @@ -48,6 +53,9 @@ class EncryptionTest : public QGpgMETest { Q_OBJECT +Q_SIGNALS: + void asyncDone(); + private Q_SLOTS: void testSimpleEncryptDecrypt() @@ -82,6 +90,60 @@ private Q_SLOTS: delete decJob; } + void testProgress() + { + if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.15") { + // We can only test the progress with 2.1.15 as this started to + // have total progress for memory callbacks + return; + } + auto listjob = openpgp()->keyListJob(false, false, false); + std::vector keys; + auto keylistresult = listjob->exec(QStringList() << QStringLiteral("alfa@example.net"), + false, keys); + Q_ASSERT(!keylistresult.error()); + Q_ASSERT(keys.size() == 1); + delete listjob; + + auto job = openpgp()->encryptJob(/*ASCII Armor */false, /* Textmode */ false); + Q_ASSERT(job); + QByteArray plainBa; + plainBa.fill('X', PROGRESS_TEST_SIZE); + QByteArray cipherText; + + bool initSeen = false; + bool finishSeen = false; + connect(job, &Job::progress, this, [this, &initSeen, &finishSeen] (const QString& what, int current, int total) { + // We only check for progress 0 and max progress as the other progress + // lines depend on the system speed and are as such unreliable to test. + Q_ASSERT(total == PROGRESS_TEST_SIZE); + if (current == 0) { + initSeen = true; + } + if (current == total) { + finishSeen = true; + } + Q_ASSERT(current >= 0 && current <= total); + }); + connect(job, &EncryptJob::result, this, [this, &initSeen, &finishSeen] (const GpgME::EncryptionResult &result, + const QByteArray &cipherText, + const QString, + const GpgME::Error) { + Q_ASSERT(initSeen); + Q_ASSERT(finishSeen); + Q_EMIT asyncDone(); + }); + + auto inptr = std::shared_ptr(new QBuffer(&plainBa)); + inptr->open(QIODevice::ReadOnly); + auto outptr = std::shared_ptr(new QBuffer(&cipherText)); + outptr->open(QIODevice::WriteOnly); + + job->start(keys, inptr, outptr, Context::AlwaysTrust); + QSignalSpy spy (this, SIGNAL(asyncDone())); + Q_ASSERT(spy.wait()); + } + void testSymmetricEncryptDecrypt() { auto ctx = Context::createForProtocol(OpenPGP);