File indexing completed on 2024-05-12 04:33:30

0001 /*
0002     SPDX-FileCopyrightText: 2022 Albert Astals Cid <aacid@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QDialogButtonBox>
0008 #include <QLineEdit>
0009 #include <QMimeDatabase>
0010 #include <QPushButton>
0011 #include <QTemporaryFile>
0012 #include <QTest>
0013 #include <QTimer>
0014 
0015 #include "../core/document.h"
0016 #include "../core/form.h"
0017 #include "../core/page.h"
0018 #include "../settings_core.h"
0019 
0020 class EnterPasswordDialogHelper : public QObject
0021 {
0022     Q_OBJECT
0023 
0024 public:
0025     EnterPasswordDialogHelper()
0026     {
0027         QTimer::singleShot(0, this, &EnterPasswordDialogHelper::enterPassword);
0028     }
0029 
0030     void enterPassword()
0031     {
0032         QWidget *dialog = qApp->activeModalWidget();
0033         if (!dialog) {
0034             QTimer::singleShot(0, this, &EnterPasswordDialogHelper::enterPassword);
0035             return;
0036         }
0037         QLineEdit *lineEdit = dialog->findChild<QLineEdit *>();
0038         lineEdit->setText(QStringLiteral("fakeokular"));
0039 
0040         QDialogButtonBox *buttonBox = dialog->findChild<QDialogButtonBox *>();
0041         buttonBox->button(QDialogButtonBox::Ok)->click();
0042     }
0043 };
0044 
0045 class SignUnsignedFieldTest : public QObject
0046 {
0047     Q_OBJECT
0048 
0049 private Q_SLOTS:
0050     void initTestCase();
0051     void init();
0052     void cleanup();
0053     void testSignUnsignedField();
0054 
0055 private:
0056     Okular::Document *m_document;
0057 };
0058 
0059 void SignUnsignedFieldTest::initTestCase()
0060 {
0061     QStandardPaths::setTestModeEnabled(true);
0062     Okular::SettingsCore::instance(QStringLiteral("signunsignedfieldtest"));
0063 
0064     KConfig cfg(QStringLiteral("okular-generator-popplerrc"));
0065     KConfigGroup g = cfg.group(QStringLiteral("Signatures"));
0066     g.writeEntry(QStringLiteral("UseDefaultCertDB"), false);
0067     g.writeEntry(QStringLiteral("DBCertificatePath"), "file://" KDESRCDIR "data/fake_okular_certstore");
0068 
0069     m_document = new Okular::Document(nullptr);
0070 }
0071 
0072 void SignUnsignedFieldTest::init()
0073 {
0074     const QString testFile = QStringLiteral(KDESRCDIR "data/hello_with_dummy_signature.pdf");
0075     QMimeDatabase db;
0076     const QMimeType mime = db.mimeTypeForFile(testFile);
0077     QCOMPARE(m_document->openDocument(testFile, QUrl(), mime), Okular::Document::OpenSuccess);
0078 }
0079 
0080 void SignUnsignedFieldTest::cleanup()
0081 {
0082     m_document->closeDocument();
0083 }
0084 
0085 void SignUnsignedFieldTest::testSignUnsignedField()
0086 {
0087     const QList<Okular::FormField *> forms = m_document->page(0)->formFields();
0088     QCOMPARE(forms.count(), 1);
0089     Okular::FormFieldSignature *ffs = dynamic_cast<Okular::FormFieldSignature *>(forms.first());
0090 
0091     QCOMPARE(ffs->signatureType(), Okular::FormFieldSignature::UnsignedSignature);
0092 
0093     const Okular::CertificateStore *certStore = m_document->certificateStore();
0094     bool userCancelled, nonDateValidCerts;
0095     {
0096         EnterPasswordDialogHelper helper;
0097         const QList<Okular::CertificateInfo> &certs = certStore->signingCertificatesForNow(&userCancelled, &nonDateValidCerts);
0098         QCOMPARE(certs.count(), 1);
0099     }
0100 
0101     Okular::NewSignatureData data;
0102     data.setCertNickname(QStringLiteral("fake-okular"));
0103     QTemporaryFile f;
0104     f.open();
0105     QVERIFY(ffs->sign(data, f.fileName()));
0106 
0107     m_document->closeDocument();
0108     QMimeDatabase db;
0109     const QMimeType mime = db.mimeTypeForFile(f.fileName());
0110     QCOMPARE(m_document->openDocument(f.fileName(), QUrl(), mime), Okular::Document::OpenSuccess);
0111 
0112     const QList<Okular::FormField *> newForms = m_document->page(0)->formFields();
0113     QCOMPARE(newForms.count(), 1);
0114     ffs = dynamic_cast<Okular::FormFieldSignature *>(newForms.first());
0115     QCOMPARE(ffs->signatureType(), Okular::FormFieldSignature::AdbePkcs7detached);
0116     QCOMPARE(ffs->signatureInfo().signerName(), QStringLiteral("FakeOkular"));
0117 }
0118 
0119 QTEST_MAIN(SignUnsignedFieldTest)
0120 #include "signunsignedfieldtest.moc"