Better wording
[kdepim.git] / kleopatra / tests / test_verify.cpp
blob8c2217ecf64d2e5ccabdc651bb3b7c6f54c85936
1 /*
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
28 your version.
31 #include <config-kleopatra.h>
33 #include "kleo_test.h"
35 #include <kleo/cryptobackendfactory.h>
36 #include <kleo/verifydetachedjob.h>
37 #include <kleo/keylistjob.h>
39 #include <gpgme++/error.h>
40 #include <gpgme++/verificationresult.h>
41 #include <gpgme++/key.h>
43 #include <QtCore/QObject>
45 Q_DECLARE_METATYPE( GpgME::VerificationResult )
47 class VerifyTest : public QObject
49 Q_OBJECT
50 private:
52 // Data shared with all tests
53 QByteArray mSignature;
54 QByteArray mSignedData;
55 const Kleo::CryptoBackend::Protocol * mBackend;
56 QEventLoop mEventLoop;
58 // Data for testParallelVerifyAndKeyListJobs()
59 QList<Kleo::VerifyDetachedJob*> mParallelVerifyJobs;
60 QList<Kleo::KeyListJob*> mParallelKeyListJobs;
62 // Data for testMixedParallelJobs()
63 QList<Kleo::Job*> mRunningJobs;
64 int mJobsStarted;
66 public slots:
67 void slotParallelKeyListJobFinished()
69 mParallelKeyListJobs.removeAll( static_cast<Kleo::KeyListJob*>( sender() ) );
71 // When all jobs are done, quit the event loop
72 if ( mParallelVerifyJobs.isEmpty() && mParallelKeyListJobs.isEmpty() )
73 mEventLoop.quit();
76 void slotParallelVerifyJobFinished( GpgME::VerificationResult result )
78 // Verify the result of the job is correct
79 QVERIFY( mParallelVerifyJobs.contains( static_cast<Kleo::VerifyDetachedJob*>( sender() ) ) );
80 QCOMPARE( result.signature( 0 ).validity(), GpgME::Signature::Full );
81 mParallelVerifyJobs.removeAll( static_cast<Kleo::VerifyDetachedJob*>( sender() ) );
83 // Start a key list job
84 Kleo::KeyListJob *job = mBackend->keyListJob();
85 mParallelKeyListJobs.append( job );
86 connect( job, SIGNAL(done()),
87 this, SLOT(slotParallelKeyListJobFinished()) );
88 QVERIFY( !job->start( QStringList() ) );
91 void someJobDone()
93 // Don't bother checking any results here
94 mRunningJobs.removeAll( static_cast<Kleo::Job*>( sender() ) );
97 void startAnotherJob()
99 static int counter = 0;
100 counter++;
102 // Randomly kill a running job
103 if ( counter % 10 == 0 && !mRunningJobs.isEmpty() ) {
104 mRunningJobs.at( counter % mRunningJobs.size() )->slotCancel();
107 // Randomly either start a keylist or a verify job
108 Kleo::Job *job;
109 if ( counter % 2 == 0 ) {
110 Kleo::VerifyDetachedJob *vdj = mBackend->verifyDetachedJob();
111 QVERIFY( !vdj->start( mSignature, mSignedData ) );
112 job = vdj;
114 else {
115 Kleo::KeyListJob *klj = mBackend->keyListJob();
116 QVERIFY( !klj->start( QStringList() ) );
117 job = klj;
119 mRunningJobs.append( job );
120 connect( job, SIGNAL(done()), this, SLOT(someJobDone()) );
122 // Quit after 2500 jobs, that should be enough
123 mJobsStarted++;
124 if ( mJobsStarted >= 2500 ) {
125 QTimer::singleShot( 1000, &mEventLoop, SLOT(quit()) );
127 else {
128 QTimer::singleShot( 0, this, SLOT(startAnotherJob()) );
132 private slots:
133 void initTestCase()
135 qRegisterMetaType<GpgME::VerificationResult>();
137 const QString sigFileName = KLEO_TEST_DATADIR "/test.data.sig";
138 const QString dataFileName = KLEO_TEST_DATADIR "/test.data";
140 QFile sigFile( sigFileName );
141 QVERIFY( sigFile.open(QFile::ReadOnly ) );
142 QFile dataFile( dataFileName );
143 QVERIFY( dataFile.open(QFile::ReadOnly ) );
145 mSignature = sigFile.readAll();
146 mSignedData = dataFile.readAll();
148 mBackend = Kleo::CryptoBackendFactory::instance()->protocol( "openpgp" );
151 void testVerify()
153 Kleo::VerifyDetachedJob *job = mBackend->verifyDetachedJob();
154 QSignalSpy spy( job, SIGNAL(result(GpgME::VerificationResult)) );
155 QVERIFY( spy.isValid() );
156 GpgME::Error err = job->start( mSignature, mSignedData );
157 QVERIFY( !err );
158 QTest::qWait( 1000 ); // ### we need to enter the event loop, can be done nicer though
160 QCOMPARE( spy.count(), 1 );
161 GpgME::VerificationResult result = spy.takeFirst().at(0).value<GpgME::VerificationResult>();
162 QCOMPARE( result.numSignatures(), 1U );
164 GpgME::Signature sig = result.signature( 0 );
165 QCOMPARE( sig.summary() & GpgME::Signature::KeyMissing, 0 );
166 QCOMPARE( (quint64) sig.creationTime(), Q_UINT64_C( 1189650248 ) );
167 QCOMPARE( sig.validity(), GpgME::Signature::Full );
170 void testParallelVerifyAndKeyListJobs()
172 // ### Increasing 10 to 500 makes the verify jobs fail!
173 for ( int i = 0; i < 10; i++ ) {
174 Kleo::VerifyDetachedJob *job = mBackend->verifyDetachedJob();
175 mParallelVerifyJobs.append( job );
176 QVERIFY( !job->start( mSignature, mSignedData ) );
177 connect( job, SIGNAL(result(GpgME::VerificationResult)),
178 this, SLOT(slotParallelVerifyJobFinished(GpgME::VerificationResult)) );
181 mEventLoop.exec();
184 void testMixedParallelJobs()
186 mJobsStarted = 0;
187 QTimer::singleShot( 0, this, SLOT(startAnotherJob()) );
188 mEventLoop.exec();
192 QTEST_KLEOMAIN( VerifyTest, NoGUI )
194 #include "test_verify.moc"