Line data Source code
1 : /* t-keylocate.cpp
2 :
3 : This file is part of qgpgme, the Qt API binding for gpgme
4 : Copyright (c) 2016 Intevation GmbH
5 :
6 : QGpgME is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU General Public License as
8 : published by the Free Software Foundation; either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : QGpgME is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with this program; if not, write to the Free Software
18 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : In addition, as a special exception, the copyright holders give
21 : permission to link the code of this program with any edition of
22 : the Qt library by Trolltech AS, Norway (or with modified versions
23 : of Qt that use the same license as Qt), and distribute linked
24 : combinations including the two. You must obey the GNU General
25 : Public License in all respects for all of the code used other than
26 : Qt. If you modify this file, you may extend this exception to
27 : your version of the file, but you are not obligated to do so. If
28 : you do not wish to do so, delete this exception statement from
29 : your version.
30 : */
31 : #include <QDebug>
32 : #include <QTest>
33 : #include <QSignalSpy>
34 : #include <QTemporaryDir>
35 : #include "keylistjob.h"
36 : #include "protocol.h"
37 : #include "keylistresult.h"
38 : #include "engineinfo.h"
39 :
40 : #include "t-support.h"
41 :
42 : using namespace QGpgME;
43 : using namespace GpgME;
44 :
45 2 : class KeyLocateTest : public QGpgMETest
46 : {
47 : Q_OBJECT
48 :
49 : Q_SIGNALS:
50 : void asyncDone();
51 :
52 : private Q_SLOTS:
53 :
54 : #ifdef DO_ONLINE_TESTS
55 : void testDaneKeyLocate()
56 : {
57 : QTemporaryDir dir;
58 : const QString oldHome = qgetenv("GNUPGHOME");
59 : qputenv("GNUPGHOME", dir.path().toUtf8());
60 : /* Could do this with gpgconf but this is not a gpgconf test ;-) */
61 : QFile conf(dir.path() + QStringLiteral("/gpg.conf"));
62 : Q_ASSERT(conf.open(QIODevice::WriteOnly));
63 : conf.write("auto-key-locate dane");
64 : conf.close();
65 :
66 : auto *job = openpgp()->locateKeysJob();
67 : mTestpattern = QStringLiteral("wk@gnupg.org");
68 : connect(job, &KeyListJob::result, job, [this, job](KeyListResult result, std::vector<Key> keys, QString, Error)
69 : {
70 : Q_ASSERT(!result.error());
71 : Q_ASSERT(keys.size() == 1);
72 :
73 : Key k = keys.front();
74 : Q_ASSERT(k.numUserIDs());
75 : bool found = false;
76 : Q_FOREACH (const UserID uid, k.userIDs()) {
77 : const QString mailBox = QString::fromUtf8(uid.email());
78 : if (mTestpattern.toLower() == mailBox.toLower()) {
79 : found = true;
80 : }
81 : }
82 : Q_ASSERT(found);
83 : Q_EMIT asyncDone();
84 : });
85 : job->start(QStringList() << mTestpattern);
86 : QSignalSpy spy (this, SIGNAL(asyncDone()));
87 : Q_ASSERT(spy.wait());
88 : qputenv("GNUPGHOME", oldHome.toUtf8());
89 : }
90 : #endif
91 :
92 1 : void testKeyLocateSingle()
93 : {
94 1 : if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.0.10") {
95 1 : return;
96 : }
97 1 : auto *job = openpgp()->locateKeysJob();
98 2 : mTestpattern = QStringLiteral("alfa@example.net");
99 :
100 1 : connect(job, &KeyListJob::result, job, [this, job](KeyListResult result, std::vector<Key> keys, QString, Error)
101 : {
102 1 : Q_ASSERT(!result.isNull());
103 1 : Q_ASSERT(!result.isTruncated());
104 1 : Q_ASSERT(!result.error());
105 1 : Q_ASSERT(keys.size() == 1);
106 :
107 1 : Key k = keys.front();
108 1 : Q_ASSERT(k.numUserIDs());
109 1 : bool found = false;
110 7 : Q_FOREACH (const UserID uid, k.userIDs()) {
111 3 : const QString mailBox = QString::fromUtf8(uid.email());
112 3 : if (mTestpattern.toLower() == mailBox.toLower()) {
113 1 : found = true;
114 : }
115 7 : }
116 1 : Q_ASSERT(found);
117 1 : Q_EMIT asyncDone();
118 2 : });
119 1 : job->start(QStringList() << mTestpattern);
120 1 : QSignalSpy spy (this, SIGNAL(asyncDone()));
121 1 : Q_ASSERT(spy.wait());
122 : }
123 :
124 : private:
125 : QString mTestpattern;
126 : };
127 :
128 1 : QTEST_MAIN(KeyLocateTest)
129 :
130 : #include "t-keylocate.moc"
|