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
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.
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.
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
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
40 #include <QTemporaryDir>
41 #include "keylistjob.h"
43 #include "keylistresult.h"
45 #include "engineinfo.h"
48 #include "dataprovider.h"
50 #include "t-support.h"
52 using namespace QGpgME;
53 using namespace GpgME;
55 static const char aKey[] = "-----BEGIN PGP PUBLIC KEY BLOCK-----\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"
67 "-----END PGP PUBLIC KEY BLOCK-----\n";
69 class TestVarious: public QGpgMETest
79 DN dn(QStringLiteral("CN=Before\\0DAfter,OU=Test,DC=North America,DC=Fabrikam,DC=COM"));
80 QVERIFY(dn.dn() == QStringLiteral("CN=Before\rAfter,OU=Test,DC=North America,DC=Fabrikam,DC=COM"));
81 QStringList attrOrder;
82 attrOrder << QStringLiteral("DC") << QStringLiteral("OU") << QStringLiteral("CN");
83 dn.setAttributeOrder(attrOrder);
84 QVERIFY(dn.prettyDN() == QStringLiteral("DC=North America,DC=Fabrikam,DC=COM,OU=Test,CN=Before\rAfter"));
87 void testKeyFromFile()
89 if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.14") {
92 QGpgME::QByteArrayDataProvider dp(aKey);
94 const auto keys = data.toKeys();
95 QVERIFY(keys.size() == 1);
96 const auto key = keys[0];
97 QVERIFY(!key.isNull());
98 QVERIFY(key.primaryFingerprint() == QStringLiteral("7A0904B6950DA998020A1AD4BE41C0C3A5FF1F3C"));
101 void testDataRewind()
103 if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.14") {
106 QGpgME::QByteArrayDataProvider dp(aKey);
111 auto keys = data.toKeys();
112 QVERIFY(keys.size() == 0);
116 keys = data.toKeys();
117 QVERIFY(keys.size() == 1);
122 if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.13") {
125 KeyListJob *job = openpgp()->keyListJob(false, true, true);
126 std::vector<GpgME::Key> keys;
127 GpgME::KeyListResult result = job->exec(QStringList() << QStringLiteral("alfa@example.net"),
130 QVERIFY (!result.error());
131 QVERIFY (keys.size() == 1);
132 Key key = keys.front();
134 QVERIFY (key.numUserIDs() == 3);
135 const char uid[] = "Foo Bar (with comment) <foo@bar.baz>";
137 auto ctx = Context::createForProtocol(key.protocol());
139 TestPassphraseProvider provider;
140 ctx->setPassphraseProvider(&provider);
141 ctx->setPinentryMode(Context::PinentryLoopback);
143 QVERIFY(!ctx->addUid(key, uid));
147 QVERIFY (key.numUserIDs() == 4);
148 bool id_found = false;;
149 for (const auto &u: key.userIDs()) {
150 if (!strcmp (u.id(), uid)) {
151 QVERIFY (!u.isRevoked());
158 ctx = Context::createForProtocol(key.protocol());
159 QVERIFY (!ctx->revUid(key, uid));
163 bool id_revoked = false;;
164 for (const auto &u: key.userIDs()) {
165 if (!strcmp (u.id(), uid)) {
175 QVERIFY(EngineInfo::Version("2.1.0") < EngineInfo::Version("2.1.1"));
176 QVERIFY(EngineInfo::Version("2.1.10") < EngineInfo::Version("2.1.11"));
177 QVERIFY(EngineInfo::Version("2.2.0") > EngineInfo::Version("2.1.19"));
178 QVERIFY(EngineInfo::Version("1.0.0") < EngineInfo::Version("2.0.0"));
179 QVERIFY(EngineInfo::Version("0.1.0") < EngineInfo::Version("1.0.0"));
180 QVERIFY(!(EngineInfo::Version("2.0.0") < EngineInfo::Version("2.0.0")));
181 QVERIFY(EngineInfo::Version("3.0.0") > EngineInfo::Version("2.3.20"));
182 QVERIFY(EngineInfo::Version("3.0.1") > EngineInfo::Version("3.0.0"));
183 QVERIFY(EngineInfo::Version("3.1.0") > EngineInfo::Version("3.0.20"));
188 QGpgMETest::initTestCase();
189 const QString gpgHome = qgetenv("GNUPGHOME");
190 QVERIFY(copyKeyrings(gpgHome, mDir.path()));
191 qputenv("GNUPGHOME", mDir.path().toUtf8());
198 QTEST_MAIN(TestVarious)
200 #include "t-various.moc"