Blame lang/qt/tests/t-keylist.cpp

Packit Service 672cf4
/* t-keylist.cpp
Packit Service 672cf4
Packit Service 672cf4
    This file is part of qgpgme, the Qt API binding for gpgme
Packit Service 672cf4
    Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
Packit Service 672cf4
    Software engineering by Intevation GmbH
Packit Service 672cf4
Packit Service 672cf4
    QGpgME is free software; you can redistribute it and/or
Packit Service 672cf4
    modify it under the terms of the GNU General Public License as
Packit Service 672cf4
    published by the Free Software Foundation; either version 2 of the
Packit Service 672cf4
    License, or (at your option) any later version.
Packit Service 672cf4
Packit Service 672cf4
    QGpgME is distributed in the hope that it will be useful,
Packit Service 672cf4
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 672cf4
    General Public License for more details.
Packit Service 672cf4
Packit Service 672cf4
    You should have received a copy of the GNU General Public License
Packit Service 672cf4
    along with this program; if not, write to the Free Software
Packit Service 672cf4
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit Service 672cf4
Packit Service 672cf4
    In addition, as a special exception, the copyright holders give
Packit Service 672cf4
    permission to link the code of this program with any edition of
Packit Service 672cf4
    the Qt library by Trolltech AS, Norway (or with modified versions
Packit Service 672cf4
    of Qt that use the same license as Qt), and distribute linked
Packit Service 672cf4
    combinations including the two.  You must obey the GNU General
Packit Service 672cf4
    Public License in all respects for all of the code used other than
Packit Service 672cf4
    Qt.  If you modify this file, you may extend this exception to
Packit Service 672cf4
    your version of the file, but you are not obligated to do so.  If
Packit Service 672cf4
    you do not wish to do so, delete this exception statement from
Packit Service 672cf4
    your version.
Packit Service 672cf4
*/
Packit Service 672cf4
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
 #include "config.h"
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include <QDebug>
Packit Service 672cf4
#include <QTest>
Packit Service 672cf4
#include <QSignalSpy>
Packit Service 672cf4
#include <QMap>
Packit Service 672cf4
#include "keylistjob.h"
Packit Service 672cf4
#include "qgpgmebackend.h"
Packit Service 672cf4
#include "keylistresult.h"
Packit Service 672cf4
Packit Service 672cf4
#include "t-support.h"
Packit Service 672cf4
Packit Service 672cf4
using namespace QGpgME;
Packit Service 672cf4
using namespace GpgME;
Packit Service 672cf4
Packit Service 672cf4
class KeyListTest : public QGpgMETest
Packit Service 672cf4
{
Packit Service 672cf4
    Q_OBJECT
Packit Service 672cf4
Packit Service 672cf4
Q_SIGNALS:
Packit Service 672cf4
    void asyncDone();
Packit Service 672cf4
Packit Service 672cf4
private Q_SLOTS:
Packit Service 672cf4
    void testSingleKeyListSync()
Packit Service 672cf4
    {
Packit Service 672cf4
        KeyListJob *job = openpgp()->keyListJob(false, false, false);
Packit Service 672cf4
        std::vector<GpgME::Key> keys;
Packit Service 672cf4
        GpgME::KeyListResult result = job->exec(QStringList() << QStringLiteral("alfa@example.net"),
Packit Service 672cf4
                                                false, keys);
Packit Service 672cf4
        delete job;
Packit Service 672cf4
        QVERIFY (!result.error());
Packit Service 672cf4
        QVERIFY (keys.size() == 1);
Packit Service 672cf4
        const QString kId = QLatin1String(keys.front().keyID());
Packit Service 672cf4
        QVERIFY (kId == QStringLiteral("2D727CC768697734"));
Packit Service 672cf4
Packit Service 672cf4
        QVERIFY (keys[0].subkeys().size() == 2);
Packit Service 672cf4
        QVERIFY (keys[0].subkeys()[0].publicKeyAlgorithm() == Subkey::AlgoDSA);
Packit Service 672cf4
        QVERIFY (keys[0].subkeys()[1].publicKeyAlgorithm() == Subkey::AlgoELG_E);
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    void testPubkeyAlgoAsString()
Packit Service 672cf4
    {
Packit Service 672cf4
        static const QMap<Subkey::PubkeyAlgo, QString> expected {
Packit Service 672cf4
            { Subkey::AlgoRSA,    QStringLiteral("RSA") },
Packit Service 672cf4
            { Subkey::AlgoRSA_E,  QStringLiteral("RSA-E") },
Packit Service 672cf4
            { Subkey::AlgoRSA_S,  QStringLiteral("RSA-S") },
Packit Service 672cf4
            { Subkey::AlgoELG_E,  QStringLiteral("ELG-E") },
Packit Service 672cf4
            { Subkey::AlgoDSA,    QStringLiteral("DSA") },
Packit Service 672cf4
            { Subkey::AlgoECC,    QStringLiteral("ECC") },
Packit Service 672cf4
            { Subkey::AlgoELG,    QStringLiteral("ELG") },
Packit Service 672cf4
            { Subkey::AlgoECDSA,  QStringLiteral("ECDSA") },
Packit Service 672cf4
            { Subkey::AlgoECDH,   QStringLiteral("ECDH") },
Packit Service 672cf4
            { Subkey::AlgoEDDSA,  QStringLiteral("EdDSA") },
Packit Service 672cf4
            { Subkey::AlgoUnknown, QString() }
Packit Service 672cf4
        };
Packit Service 672cf4
        Q_FOREACH (Subkey::PubkeyAlgo algo, expected.keys()) {
Packit Service 672cf4
            QVERIFY(QString::fromUtf8(Subkey::publicKeyAlgorithmAsString(algo)) ==
Packit Service 672cf4
                     expected.value(algo));
Packit Service 672cf4
        }
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    void testKeyListAsync()
Packit Service 672cf4
    {
Packit Service 672cf4
        KeyListJob *job = openpgp()->keyListJob();
Packit Service 672cf4
        connect(job, &KeyListJob::result, job, [this, job](KeyListResult, std::vector<Key> keys, QString, Error)
Packit Service 672cf4
        {
Packit Service 672cf4
            QVERIFY(keys.size() == 1);
Packit Service 672cf4
            Q_EMIT asyncDone();
Packit Service 672cf4
        });
Packit Service 672cf4
        job->start(QStringList() << "alfa@example.net");
Packit Service 672cf4
        QSignalSpy spy (this, SIGNAL(asyncDone()));
Packit Service 672cf4
        QVERIFY(spy.wait(QSIGNALSPY_TIMEOUT));
Packit Service 672cf4
    }
Packit Service 672cf4
};
Packit Service 672cf4
Packit Service 672cf4
QTEST_MAIN(KeyListTest)
Packit Service 672cf4
Packit Service 672cf4
#include "t-keylist.moc"