File indexing completed on 2024-05-12 09:56:31

0001 /*
0002     SPDX-FileCopyrightText: 2022 Rolf Eike Beer <kde@opensource.sf-tec.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "kgpggeneratekey.h"
0007 #include "kgpggeneratekeytesttransaction.h"
0008 
0009 #include "../core/kgpgkey.h"
0010 #include "../core/KGpgRootNode.h"
0011 #include "../kgpginterface.h"
0012 #include "../model/kgpgitemmodel.h"
0013 #include "../transactions/kgpggeneratekey.h"
0014 #include "common.h"
0015 
0016 #include <QSignalSpy>
0017 #include <QTest>
0018 
0019 void KGpgGenerateKeyTest::init()
0020 {
0021     resetGpgConf(m_tempdir);
0022 }
0023 
0024 void KGpgGenerateKeyTest::testDeleteKey()
0025 {
0026     QFETCH(QString, name);
0027     QFETCH(QString, email);
0028     QFETCH(QByteArray, passphrase);
0029 
0030     KGpgItemModel *model = new KGpgItemModel(this);
0031 
0032     KGpgGenerateKey *transaction = new KGpgGenerateKeyTestTransaction(this, name, email, QString(),
0033                                     passphrase, KgpgCore::ALGO_RSA_RSA, 2048);
0034 
0035     QObject::connect(transaction, &KGpgGenerateKey::done,
0036              [](int result) { QCOMPARE(result, static_cast<int>(KGpgTransaction::TS_OK)); });
0037     QSignalSpy spy(transaction, &KGpgGenerateKey::done);
0038 
0039     transaction->start();
0040     QVERIFY(spy.wait(10000));
0041     QCOMPARE(spy.count(), 1);
0042     QCOMPARE(transaction->getName(), name);
0043     QCOMPARE(transaction->getEmail(), email);
0044 
0045     const QString fingerprint = transaction->getFingerprint();
0046 
0047     QCOMPARE(KgpgInterface::readSecretKeys().size(), 1);
0048     QCOMPARE(KgpgInterface::readPublicKeys().size(), 1);
0049     QCOMPARE(KgpgInterface::readSecretKeys( { fingerprint } ).size(), 1);
0050     QCOMPARE(KgpgInterface::readPublicKeys( { fingerprint } ).size(), 1);
0051 
0052     model->refreshKey(fingerprint);
0053 
0054     QCOMPARE(model->rowCount(), 1);
0055 }
0056 
0057 void KGpgGenerateKeyTest::testDeleteKey_data()
0058 {
0059     QTest::addColumn<QString>("name");
0060     QTest::addColumn<QString>("email");
0061     QTest::addColumn<QByteArray>("passphrase");
0062 
0063     QTest::newRow("with email")
0064         << QStringLiteral("test name with email")
0065         << QStringLiteral("foobar@example.org")
0066         << QByteArray("secret");
0067 
0068     QTest::newRow("without email")
0069         << QStringLiteral("test name without email")
0070         << QString()
0071         << QByteArray("secret");
0072 }
0073 
0074 QTEST_MAIN(KGpgGenerateKeyTest)