Line data Source code
1 : /* t-various.cpp
2 :
3 : This file is part of qgpgme, the Qt API binding for gpgme
4 : Copyright (c) 2017 by Bundesamt für Sicherheit in der Informationstechnik
5 : Software engineering by Intevation GmbH
6 :
7 : QGpgME is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU General Public License as
9 : published by the Free Software Foundation; either version 2 of the
10 : License, or (at your option) any later version.
11 :
12 : QGpgME is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program; if not, write to the Free Software
19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 :
21 : In addition, as a special exception, the copyright holders give
22 : permission to link the code of this program with any edition of
23 : the Qt library by Trolltech AS, Norway (or with modified versions
24 : of Qt that use the same license as Qt), and distribute linked
25 : combinations including the two. You must obey the GNU General
26 : Public License in all respects for all of the code used other than
27 : Qt. If you modify this file, you may extend this exception to
28 : your version of the file, but you are not obligated to do so. If
29 : you do not wish to do so, delete this exception statement from
30 : your version.
31 : */
32 :
33 : #ifdef HAVE_CONFIG_H
34 : #include "config.h"
35 : #endif
36 :
37 : #include <QDebug>
38 : #include <QTest>
39 : #include <QSignalSpy>
40 : #include <QTemporaryDir>
41 : #include "keylistjob.h"
42 : #include "protocol.h"
43 : #include "keylistresult.h"
44 : #include "context.h"
45 : #include "engineinfo.h"
46 : #include "dn.h"
47 : #include "data.h"
48 : #include "dataprovider.h"
49 :
50 : #include "t-support.h"
51 :
52 : using namespace QGpgME;
53 : using namespace GpgME;
54 :
55 : static const char aKey[] = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n"
56 : "\n"
57 : "mDMEWG+w/hYJKwYBBAHaRw8BAQdAiq1oStvDYg8ZfFs5DgisYJo8dJxD+C/AA21O\n"
58 : "K/aif0O0GXRvZnVfY29uZmxpY3RAZXhhbXBsZS5jb22IlgQTFggAPhYhBHoJBLaV\n"
59 : "DamYAgoa1L5BwMOl/x88BQJYb7D+AhsDBQkDwmcABQsJCAcCBhUICQoLAgQWAgMB\n"
60 : "Ah4BAheAAAoJEL5BwMOl/x88GvwA/0SxkbLyAcshGm2PRrPsFQsSVAfwaSYFVmS2\n"
61 : "cMVIw1PfAQDclRH1Z4MpufK07ju4qI33o4s0UFpVRBuSxt7A4P2ZD7g4BFhvsP4S\n"
62 : "CisGAQQBl1UBBQEBB0AmVrgaDNJ7K2BSalsRo2EkRJjHGqnp5bBB0tapnF81CQMB\n"
63 : "CAeIeAQYFggAIBYhBHoJBLaVDamYAgoa1L5BwMOl/x88BQJYb7D+AhsMAAoJEL5B\n"
64 : "wMOl/x88OR0BAMq4/vmJUORRTmzjHcv/DDrQB030DSq666rlckGIKTShAPoDXM9N\n"
65 : "0gZK+YzvrinSKZXHmn0aSwmC1/hyPybJPEljBw==\n"
66 : "=p2Oj\n"
67 : "-----END PGP PUBLIC KEY BLOCK-----\n";
68 :
69 2 : class TestVarious: public QGpgMETest
70 : {
71 : Q_OBJECT
72 :
73 : Q_SIGNALS:
74 : void asyncDone();
75 :
76 : private Q_SLOTS:
77 1 : void testDN()
78 : {
79 3 : DN dn(QStringLiteral("CN=Before\\0DAfter,OU=Test,DC=North America,DC=Fabrikam,DC=COM"));
80 2 : QVERIFY(dn.dn() == QStringLiteral("CN=Before\rAfter,OU=Test,DC=North America,DC=Fabrikam,DC=COM"));
81 2 : QStringList attrOrder;
82 4 : attrOrder << QStringLiteral("DC") << QStringLiteral("OU") << QStringLiteral("CN");
83 1 : dn.setAttributeOrder(attrOrder);
84 2 : QVERIFY(dn.prettyDN() == QStringLiteral("DC=North America,DC=Fabrikam,DC=COM,OU=Test,CN=Before\rAfter"));
85 : }
86 :
87 1 : void testKeyFromFile()
88 : {
89 1 : if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.14") {
90 0 : return;
91 : }
92 2 : QGpgME::QByteArrayDataProvider dp(aKey);
93 2 : Data data(&dp);
94 2 : const auto keys = data.toKeys();
95 1 : QVERIFY(keys.size() == 1);
96 2 : const auto key = keys[0];
97 1 : QVERIFY(!key.isNull());
98 2 : QVERIFY(key.primaryFingerprint() == QStringLiteral("7A0904B6950DA998020A1AD4BE41C0C3A5FF1F3C"));
99 : }
100 :
101 1 : void testDataRewind()
102 : {
103 1 : if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.14") {
104 0 : return;
105 : }
106 2 : QGpgME::QByteArrayDataProvider dp(aKey);
107 2 : Data data(&dp);
108 : char buf[20];
109 1 : data.read(buf, 20);
110 :
111 2 : auto keys = data.toKeys();
112 1 : QVERIFY(keys.size() == 0);
113 :
114 1 : data.rewind();
115 :
116 1 : keys = data.toKeys();
117 1 : QVERIFY(keys.size() == 1);
118 : }
119 :
120 1 : void testQuickUid()
121 : {
122 1 : if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.13") {
123 0 : return;
124 : }
125 1 : KeyListJob *job = openpgp()->keyListJob(false, true, true);
126 2 : std::vector<GpgME::Key> keys;
127 3 : GpgME::KeyListResult result = job->exec(QStringList() << QStringLiteral("alfa@example.net"),
128 3 : false, keys);
129 1 : delete job;
130 1 : QVERIFY (!result.error());
131 1 : QVERIFY (keys.size() == 1);
132 2 : Key key = keys.front();
133 :
134 1 : QVERIFY (key.numUserIDs() == 3);
135 1 : const char uid[] = "Foo Bar (with comment) <foo@bar.baz>";
136 :
137 1 : auto ctx = Context::createForProtocol(key.protocol());
138 1 : QVERIFY (ctx);
139 2 : TestPassphraseProvider provider;
140 1 : ctx->setPassphraseProvider(&provider);
141 1 : ctx->setPinentryMode(Context::PinentryLoopback);
142 :
143 1 : QVERIFY(!ctx->addUid(key, uid));
144 1 : delete ctx;
145 1 : key.update();
146 :
147 1 : QVERIFY (key.numUserIDs() == 4);
148 1 : bool id_found = false;;
149 1 : for (const auto &u: key.userIDs()) {
150 1 : if (!strcmp (u.id(), uid)) {
151 1 : QVERIFY (!u.isRevoked());
152 1 : id_found = true;
153 1 : break;
154 : }
155 : }
156 1 : QVERIFY (id_found);
157 :
158 1 : ctx = Context::createForProtocol(key.protocol());
159 1 : QVERIFY (!ctx->revUid(key, uid));
160 1 : delete ctx;
161 1 : key.update();
162 :
163 1 : bool id_revoked = false;;
164 4 : for (const auto &u: key.userIDs()) {
165 4 : if (!strcmp (u.id(), uid)) {
166 1 : id_revoked = true;
167 1 : break;
168 : }
169 : }
170 1 : QVERIFY(id_revoked);
171 : }
172 :
173 1 : void testVersion()
174 : {
175 1 : QVERIFY(EngineInfo::Version("2.1.0") < EngineInfo::Version("2.1.1"));
176 1 : QVERIFY(EngineInfo::Version("2.1.10") < EngineInfo::Version("2.1.11"));
177 1 : QVERIFY(EngineInfo::Version("2.2.0") > EngineInfo::Version("2.1.19"));
178 1 : QVERIFY(EngineInfo::Version("1.0.0") < EngineInfo::Version("2.0.0"));
179 1 : QVERIFY(EngineInfo::Version("0.1.0") < EngineInfo::Version("1.0.0"));
180 1 : QVERIFY(!(EngineInfo::Version("2.0.0") < EngineInfo::Version("2.0.0")));
181 1 : QVERIFY(EngineInfo::Version("3.0.0") > EngineInfo::Version("2.3.20"));
182 1 : QVERIFY(EngineInfo::Version("3.0.1") > EngineInfo::Version("3.0.0"));
183 1 : QVERIFY(EngineInfo::Version("3.1.0") > EngineInfo::Version("3.0.20"));
184 : }
185 :
186 1 : void initTestCase()
187 : {
188 1 : QGpgMETest::initTestCase();
189 2 : const QString gpgHome = qgetenv("GNUPGHOME");
190 1 : QVERIFY(copyKeyrings(gpgHome, mDir.path()));
191 1 : qputenv("GNUPGHOME", mDir.path().toUtf8());
192 : }
193 :
194 : private:
195 : QTemporaryDir mDir;
196 : };
197 :
198 1 : QTEST_MAIN(TestVarious)
199 :
200 : #include "t-various.moc"
|