Line data Source code
1 : /* t-keylist.cpp
2 :
3 : This file is part of qgpgme, the Qt API binding for gpgme
4 : Copyright (c) 2016 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 <QMap>
41 : #include "keylistjob.h"
42 : #include "qgpgmebackend.h"
43 : #include "keylistresult.h"
44 :
45 : #include "context.h"
46 :
47 : #include <memory>
48 :
49 : #include "t-support.h"
50 :
51 : using namespace QGpgME;
52 : using namespace GpgME;
53 :
54 2 : class KeyListTest : public QGpgMETest
55 : {
56 : Q_OBJECT
57 :
58 : Q_SIGNALS:
59 : void asyncDone();
60 :
61 : private Q_SLOTS:
62 1 : void testSingleKeyListSync()
63 : {
64 1 : KeyListJob *job = openpgp()->keyListJob(false, false, false);
65 2 : std::vector<GpgME::Key> keys;
66 3 : GpgME::KeyListResult result = job->exec(QStringList() << QStringLiteral("alfa@example.net"),
67 3 : false, keys);
68 1 : delete job;
69 1 : QVERIFY (!result.error());
70 1 : QVERIFY (keys.size() == 1);
71 2 : const QString kId = QLatin1String(keys.front().keyID());
72 2 : QVERIFY (kId == QStringLiteral("2D727CC768697734"));
73 :
74 1 : QVERIFY (keys[0].subkeys().size() == 2);
75 1 : QVERIFY (keys[0].subkeys()[0].publicKeyAlgorithm() == Subkey::AlgoDSA);
76 1 : QVERIFY (keys[0].subkeys()[1].publicKeyAlgorithm() == Subkey::AlgoELG_E);
77 : }
78 :
79 : // This test can help with valgrind to check for memleaks when handling
80 : // keys
81 1 : void testGetKey()
82 : {
83 2 : GpgME::Key key;
84 : {
85 2 : auto ctx = std::unique_ptr<GpgME::Context> (GpgME::Context::createForProtocol(GpgME::OpenPGP));
86 1 : ctx->setKeyListMode (GpgME::KeyListMode::Local |
87 : GpgME::KeyListMode::Signatures |
88 : GpgME::KeyListMode::Validate |
89 1 : GpgME::KeyListMode::WithTofu);
90 2 : GpgME::Error err;
91 1 : key = ctx->key ("A0FF4590BB6122EDEF6E3C542D727CC768697734", err, false);
92 : }
93 1 : QVERIFY(key.primaryFingerprint());
94 1 : QVERIFY(!strcmp(key.primaryFingerprint(), "A0FF4590BB6122EDEF6E3C542D727CC768697734"));
95 : {
96 2 : auto ctx = std::unique_ptr<GpgME::Context> (GpgME::Context::createForProtocol(GpgME::OpenPGP));
97 1 : ctx->setKeyListMode (GpgME::KeyListMode::Local |
98 : GpgME::KeyListMode::Signatures |
99 : GpgME::KeyListMode::Validate |
100 1 : GpgME::KeyListMode::WithTofu);
101 2 : GpgME::Error err;
102 1 : key = ctx->key ("A0FF4590BB6122EDEF6E3C542D727CC768697734", err, false);
103 : }
104 1 : QVERIFY(key.primaryFingerprint());
105 1 : QVERIFY(!strcmp(key.primaryFingerprint(), "A0FF4590BB6122EDEF6E3C542D727CC768697734"));
106 : }
107 :
108 1 : void testPubkeyAlgoAsString()
109 : {
110 : static const QMap<Subkey::PubkeyAlgo, QString> expected {
111 3 : { Subkey::AlgoRSA, QStringLiteral("RSA") },
112 3 : { Subkey::AlgoRSA_E, QStringLiteral("RSA-E") },
113 3 : { Subkey::AlgoRSA_S, QStringLiteral("RSA-S") },
114 3 : { Subkey::AlgoELG_E, QStringLiteral("ELG-E") },
115 3 : { Subkey::AlgoDSA, QStringLiteral("DSA") },
116 3 : { Subkey::AlgoECC, QStringLiteral("ECC") },
117 3 : { Subkey::AlgoELG, QStringLiteral("ELG") },
118 3 : { Subkey::AlgoECDSA, QStringLiteral("ECDSA") },
119 3 : { Subkey::AlgoECDH, QStringLiteral("ECDH") },
120 3 : { Subkey::AlgoEDDSA, QStringLiteral("EdDSA") },
121 2 : { Subkey::AlgoUnknown, QString() }
122 23 : };
123 12 : Q_FOREACH (Subkey::PubkeyAlgo algo, expected.keys()) {
124 11 : QVERIFY(QString::fromUtf8(Subkey::publicKeyAlgorithmAsString(algo)) ==
125 : expected.value(algo));
126 : }
127 : }
128 :
129 1 : void testKeyListAsync()
130 : {
131 1 : KeyListJob *job = openpgp()->keyListJob();
132 1 : connect(job, &KeyListJob::result, job, [this, job](KeyListResult, std::vector<Key> keys, QString, Error)
133 1 : {
134 1 : QVERIFY(keys.size() == 1);
135 1 : Q_EMIT asyncDone();
136 1 : });
137 1 : job->start(QStringList() << "alfa@example.net");
138 2 : QSignalSpy spy (this, SIGNAL(asyncDone()));
139 1 : QVERIFY(spy.wait(QSIGNALSPY_TIMEOUT));
140 : }
141 : };
142 :
143 1 : QTEST_MAIN(KeyListTest)
144 :
145 : #include "t-keylist.moc"
|