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 : #ifdef HAVE_CONFIG_H
32 : #include "config.h"
33 : #endif
34 :
35 : #include <QDebug>
36 : #include <QTest>
37 : #include <QSignalSpy>
38 : #include <QTemporaryDir>
39 : #include "keylistjob.h"
40 : #include "protocol.h"
41 : #include "keylistresult.h"
42 : #include "engineinfo.h"
43 :
44 : #include "t-support.h"
45 :
46 : using namespace QGpgME;
47 : using namespace GpgME;
48 :
49 2 : class KeyLocateTest : public QGpgMETest
50 : {
51 : Q_OBJECT
52 :
53 : Q_SIGNALS:
54 : void asyncDone();
55 :
56 : private Q_SLOTS:
57 :
58 : #ifdef DO_ONLINE_TESTS
59 : void testDaneKeyLocate()
60 : {
61 : QTemporaryDir dir;
62 : const QString oldHome = qgetenv("GNUPGHOME");
63 : qputenv("GNUPGHOME", dir.path().toUtf8());
64 : /* Could do this with gpgconf but this is not a gpgconf test ;-) */
65 : QFile conf(dir.path() + QStringLiteral("/gpg.conf"));
66 : Q_ASSERT(conf.open(QIODevice::WriteOnly));
67 : conf.write("auto-key-locate dane");
68 : conf.close();
69 :
70 : auto *job = openpgp()->locateKeysJob();
71 : mTestpattern = QStringLiteral("wk@gnupg.org");
72 : connect(job, &KeyListJob::result, job, [this, job](KeyListResult result, std::vector<Key> keys, QString, Error)
73 : {
74 : Q_ASSERT(!result.error());
75 : Q_ASSERT(keys.size() == 1);
76 :
77 : Key k = keys.front();
78 : Q_ASSERT(k.numUserIDs());
79 : bool found = false;
80 : Q_FOREACH (const UserID uid, k.userIDs()) {
81 : const QString mailBox = QString::fromUtf8(uid.email());
82 : if (mTestpattern.toLower() == mailBox.toLower()) {
83 : found = true;
84 : }
85 : }
86 : Q_ASSERT(found);
87 : Q_EMIT asyncDone();
88 : });
89 : job->start(QStringList() << mTestpattern);
90 : QSignalSpy spy (this, SIGNAL(asyncDone()));
91 : Q_ASSERT(spy.wait());
92 : qputenv("GNUPGHOME", oldHome.toUtf8());
93 : }
94 : #endif
95 :
96 1 : void testKeyLocateSingle()
97 : {
98 1 : if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.0.10") {
99 1 : return;
100 : }
101 1 : auto *job = openpgp()->locateKeysJob();
102 2 : mTestpattern = QStringLiteral("alfa@example.net");
103 :
104 1 : connect(job, &KeyListJob::result, job, [this, job](KeyListResult result, std::vector<Key> keys, QString, Error)
105 : {
106 1 : Q_ASSERT(!result.isNull());
107 1 : Q_ASSERT(!result.isTruncated());
108 1 : Q_ASSERT(!result.error());
109 1 : Q_ASSERT(keys.size() == 1);
110 :
111 1 : Key k = keys.front();
112 1 : Q_ASSERT(k.numUserIDs());
113 1 : bool found = false;
114 7 : Q_FOREACH (const UserID uid, k.userIDs()) {
115 3 : const QString mailBox = QString::fromUtf8(uid.email());
116 3 : if (mTestpattern.toLower() == mailBox.toLower()) {
117 1 : found = true;
118 : }
119 7 : }
120 1 : Q_ASSERT(found);
121 1 : Q_EMIT asyncDone();
122 2 : });
123 1 : job->start(QStringList() << mTestpattern);
124 1 : QSignalSpy spy (this, SIGNAL(asyncDone()));
125 1 : Q_ASSERT(spy.wait());
126 : }
127 :
128 : private:
129 : QString mTestpattern;
130 : };
131 :
132 1 : QTEST_MAIN(KeyLocateTest)
133 :
134 : #include "t-keylocate.moc"
|