2 This file is part of Kleopatra's test suite.
3 Copyright (c) 2007 Klarälvdalens Datakonsult AB
5 Kleopatra is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 Kleopatra is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of this program with any edition of
21 the Qt library by Trolltech AS, Norway (or with modified versions
22 of Qt that use the same license as Qt), and distribute linked
23 combinations including the two. You must obey the GNU General
24 Public License in all respects for all of the code used other than
25 Qt. If you modify this file, you may extend this exception to
26 your version of the file, but you are not obligated to do so. If
27 you do not wish to do so, delete this exception statement from
31 #include <config-kleopatra.h>
33 #include "kleo_test.h"
35 #include <Libkleo/CryptoBackendFactory>
36 #include <Libkleo/VerifyDetachedJob>
37 #include <Libkleo/KeyListJob>
40 #include <gpgme++/error.h>
41 #include <gpgme++/verificationresult.h>
42 #include <gpgme++/key.h>
47 // Replace this with a gpgme version check once GnuPG Bug #2092
48 // ( https://bugs.gnupg.org/gnupg/issue2092 ) is fixed.
49 #define GPGME_MULTITHREADED_KEYLIST_BROKEN
51 Q_DECLARE_METATYPE(GpgME::VerificationResult
)
53 class VerifyTest
: public QObject
58 // Data shared with all tests
59 QByteArray mSignature
;
60 QByteArray mSignedData
;
61 const Kleo::CryptoBackend::Protocol
*mBackend
;
62 QEventLoop mEventLoop
;
64 // Data for testParallelVerifyAndKeyListJobs()
65 QList
<Kleo::VerifyDetachedJob
*> mParallelVerifyJobs
;
66 QList
<Kleo::KeyListJob
*> mParallelKeyListJobs
;
68 // Data for testMixedParallelJobs()
69 QList
<Kleo::Job
*> mRunningJobs
;
73 void slotParallelKeyListJobFinished()
75 mParallelKeyListJobs
.removeAll(static_cast<Kleo::KeyListJob
*>(sender()));
77 // When all jobs are done, quit the event loop
78 if (mParallelVerifyJobs
.isEmpty() && mParallelKeyListJobs
.isEmpty()) {
83 void slotParallelVerifyJobFinished(GpgME::VerificationResult result
)
85 // Verify the result of the job is correct
86 QVERIFY(mParallelVerifyJobs
.contains(static_cast<Kleo::VerifyDetachedJob
*>(sender())));
87 QCOMPARE(result
.signature(0).validity(), GpgME::Signature::Full
);
88 mParallelVerifyJobs
.removeAll(static_cast<Kleo::VerifyDetachedJob
*>(sender()));
90 // Start a key list job
91 Kleo::KeyListJob
*job
= mBackend
->keyListJob();
92 mParallelKeyListJobs
.append(job
);
93 connect(job
, &Kleo::Job::done
,
94 this, &VerifyTest::slotParallelKeyListJobFinished
);
95 QVERIFY(!job
->start(QStringList()));
100 // Don't bother checking any results here
101 mRunningJobs
.removeAll(static_cast<Kleo::Job
*>(sender()));
104 void startAnotherJob()
106 static int counter
= 0;
109 // Randomly kill a running job
110 if (counter
% 10 == 0 && !mRunningJobs
.isEmpty()) {
111 mRunningJobs
.at(counter
% mRunningJobs
.size())->slotCancel();
114 // Randomly either start a keylist or a verify job
116 if (counter
% 2 == 0) {
117 Kleo::VerifyDetachedJob
*vdj
= mBackend
->verifyDetachedJob();
118 QVERIFY(!vdj
->start(mSignature
, mSignedData
));
121 Kleo::KeyListJob
*klj
= mBackend
->keyListJob();
122 QVERIFY(!klj
->start(QStringList()));
125 mRunningJobs
.append(job
);
126 connect(job
, &Kleo::Job::done
, this, &VerifyTest::someJobDone
);
128 // Quit after 2500 jobs, that should be enough
130 if (mJobsStarted
>= 2500) {
131 QTimer::singleShot(1000, &mEventLoop
, &QEventLoop::quit
);
133 QTimer::singleShot(0, this, &VerifyTest::startAnotherJob
);
140 qRegisterMetaType
<GpgME::VerificationResult
>();
142 const QString sigFileName
= QLatin1String(KLEO_TEST_DATADIR
) + QLatin1String("/test.data.sig");
143 const QString dataFileName
= QLatin1String(KLEO_TEST_DATADIR
) + QLatin1String("/test.data");
145 QFile
sigFile(sigFileName
);
146 QVERIFY(sigFile
.open(QFile::ReadOnly
));
147 QFile
dataFile(dataFileName
);
148 QVERIFY(dataFile
.open(QFile::ReadOnly
));
150 mSignature
= sigFile
.readAll();
151 mSignedData
= dataFile
.readAll();
153 mBackend
= Kleo::CryptoBackendFactory::instance()->protocol("openpgp");
158 Kleo::VerifyDetachedJob
*job
= mBackend
->verifyDetachedJob();
159 QSignalSpy
spy(job
, SIGNAL(result(GpgME::VerificationResult
)));
160 QVERIFY(spy
.isValid());
161 GpgME::Error err
= job
->start(mSignature
, mSignedData
);
163 QTest::qWait(1000); // ### we need to enter the event loop, can be done nicer though
165 QCOMPARE(spy
.count(), 1);
166 GpgME::VerificationResult result
= spy
.takeFirst().at(0).value
<GpgME::VerificationResult
>();
167 QCOMPARE(result
.numSignatures(), 1U);
169 GpgME::Signature sig
= result
.signature(0);
170 QCOMPARE(sig
.summary() & GpgME::Signature::KeyMissing
, 0);
171 QCOMPARE((quint64
) sig
.creationTime(), Q_UINT64_C(1189650248));
172 QCOMPARE(sig
.validity(), GpgME::Signature::Full
);
175 #ifndef GPGME_MULTITHREADED_KEYLIST_BROKEN
176 // The following two tests are disabled because they trigger an
177 // upstream bug in gpgme. See: https://bugs.gnupg.org/gnupg/issue2092
178 // Which has a testcase attached that does similar things using gpgme
179 // directly and triggers various problems.
180 void testParallelVerifyAndKeyListJobs()
182 // ### Increasing 10 to 500 makes the verify jobs fail!
183 // ^ This should also be reevaluated if the underlying bug in gpgme
185 for (int i
= 0; i
< 10; ++i
) {
186 Kleo::VerifyDetachedJob
*job
= mBackend
->verifyDetachedJob();
187 mParallelVerifyJobs
.append(job
);
188 QVERIFY(!job
->start(mSignature
, mSignedData
));
189 connect(job
, SIGNAL(result(GpgME::VerificationResult
)),
190 this, SLOT(slotParallelVerifyJobFinished(GpgME::VerificationResult
)));
196 void testMixedParallelJobs()
199 QTimer::singleShot(0, this, SLOT(startAnotherJob()));
205 QTEST_KLEOMAIN(VerifyTest
)
207 #include "test_verify.moc"