Debian Codegraph @

qt6/tests/check_create_pgp_signature1.cpp

raw
1 //========================================================================
2 //
3 // check_signature_basics.cpp
4 //
5 // This file is licensed under the GPLv2 or later
6 //
7 // Copyright 2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]>
8 // Copyright 2025 Albert Astals Cid <[email protected]>
9 //========================================================================
10
11 // Simple tests of reading signatures
12 //
13
14 #include "config.h"
15 #include <filesystem>
16 #if ENABLE_NSS3
17 # include "NSSCryptoSignBackend.h"
18 #endif
19 #include <QtTest/QTest>
20 #include <QTemporaryDir>
21 #include <QTemporaryFile>
22 #include "PDFDoc.h"
23 #include "GlobalParams.h"
24 #include "SignatureInfo.h"
25 #include "CryptoSignBackend.h"
26 #include "GPGMECryptoSignBackendConfiguration.h"
27 #include <gpgme++/importresult.h>
28 #include <gpgme++/context.h>
29 #include <gpgme++/data.h>
30
31 class TestSignWithGnupgPgp : public QObject
32 {
33 Q_OBJECT
34 public:
35 explicit TestSignWithGnupgPgp(QObject *parent = nullptr) : QObject(parent) { }
36
37 static std::unique_ptr<QTemporaryDir> nssdir;
38 static std::unique_ptr<QTemporaryDir> gpgdir;
39 static void initMain()
40 {
41 nssdir = std::make_unique<QTemporaryDir>();
42 #if ENABLE_NSS3
43 NSSSignatureConfiguration::setNSSDir(GooString(nssdir->path().toStdString()));
44 #endif
45 gpgdir = std::make_unique<QTemporaryDir>();
46 // copy out the data for two reasons:
47 // 1) gpg-agent might get angry if the path is too long
48 // 2) Ensure that accidental writes from the test (and e
49 // specially other tests getting inspired by this)
50 // does not carry over to the next tests
51 std::filesystem::copy(TESTDATADIR "/unittestcases/check_create_pgp_signature1_keyring/", gpgdir->path().toStdString(), std::filesystem::copy_options::recursive);
52 qputenv("GNUPGHOME", gpgdir->path().toLocal8Bit());
53 }
54
55 private Q_SLOTS:
56 void init();
57 void initTestCase_data();
58 void initTestCase() { }
59 void cleanupTestCase();
60 void testPgpSignVerify();
61 void testKeyList();
62 };
63
64 std::unique_ptr<QTemporaryDir> TestSignWithGnupgPgp::nssdir;
65 std::unique_ptr<QTemporaryDir> TestSignWithGnupgPgp::gpgdir;
66
67 void TestSignWithGnupgPgp::init()
68 {
69 QFETCH_GLOBAL(CryptoSign::Backend::Type, backend);
70 CryptoSign::Factory::setPreferredBackend(backend);
71 QCOMPARE(CryptoSign::Factory::getActive(), backend);
72 GpgSignatureConfiguration::setPgpSignaturesAllowed(true);
73
74 globalParams = std::make_unique<GlobalParams>();
75 }
76
77 void TestSignWithGnupgPgp::initTestCase_data()
78 {
79 QTest::addColumn<CryptoSign::Backend::Type>("backend");
80
81 const auto availableBackends = CryptoSign::Factory::getAvailable();
82
83 #if ENABLE_NSS3
84 if (std::ranges::find(availableBackends, CryptoSign::Backend::Type::NSS3) != availableBackends.end()) {
85 QTest::newRow("nss") << CryptoSign::Backend::Type::NSS3;
86 } else {
87 QWARN("Compiled with NSS3, but NSS not functional");
88 }
89 #endif
90 if (std::ranges::find(availableBackends, CryptoSign::Backend::Type::GPGME) != availableBackends.end()) {
91 QTest::newRow("gpg") << CryptoSign::Backend::Type::GPGME;
92 } else {
93 QWARN("Compiled with GPGME, but GPGME not functional");
94 }
95 }
96
97 void TestSignWithGnupgPgp::cleanupTestCase()
98 {
99 globalParams.reset();
100 }
101
102 void TestSignWithGnupgPgp::testKeyList()
103 {
104 auto backend = CryptoSign::Factory::createActive();
105 QVERIFY(backend);
106
107 auto certificateList = backend->getAvailableSigningCertificates();
108
109 auto activeBackendType = CryptoSign::Factory::getActive();
110 if (activeBackendType == CryptoSign::Backend::Type::NSS3) {
111 QVERIFY(certificateList.empty());
112 } else if (activeBackendType == CryptoSign::Backend::Type::GPGME) {
113 QCOMPARE(certificateList.size(), 1);
114 QCOMPARE(certificateList[0]->getNickName().c_str(), "36E39802E4F49A259091DA69381B80FEF3535BC1");
115 }
116 }
117
118 void TestSignWithGnupgPgp::testPgpSignVerify()
119 {
120 auto doc = std::make_unique<PDFDoc>(std::make_unique<GooString>(TESTDATADIR "/unittestcases/WithActualText.pdf"));
121 QVERIFY(doc->isOk());
122 {
123 auto signatureFields = doc->getSignatureFields();
124 QCOMPARE(signatureFields.size(), 0);
125 }
126
127 QTemporaryDir d;
128
129 auto signingResult =
130 doc->sign(std::string { d.filePath(QStringLiteral("signedFile.pdf")).toStdString() }, std::string { "36E39802E4F49A259091DA69381B80FEF3535BC1" }, std::string {}, std::make_unique<GooString>("newSignatureFieldName"), /*page*/ 1,
131 /*rect */ { 0, 0, 0, 0 }, /*signatureText*/ {}, /*signatureTextLeft*/ {}, /*fontSize */ 0, /*leftFontSize*/ 0,
132 /*fontColor*/ {}, /*borderWidth*/ 0, /*borderColor*/ {}, /*backgroundColor*/ {}, /*reason*/ {}, /* location */ nullptr, /* image path */ "", {}, {});
133
134 auto activeBackendType = CryptoSign::Factory::getActive();
135 if (activeBackendType == CryptoSign::Backend::Type::NSS3) {
136 QVERIFY(signingResult.has_value());
137 QCOMPARE(signingResult.value().type, CryptoSign::SigningError::KeyMissing);
138 } else if (activeBackendType == CryptoSign::Backend::Type::GPGME) {
139 QVERIFY(!signingResult.has_value());
140 auto signedDoc = std::make_unique<PDFDoc>(std::make_unique<GooString>(d.filePath(QStringLiteral("signedFile.pdf")).toStdString()));
141 QVERIFY(signedDoc->isOk());
142 auto signatureFields = signedDoc->getSignatureFields();
143 QCOMPARE(signatureFields.size(), 1);
144 QCOMPARE(signatureFields[0]->getSignatureType(), CryptoSign::SignatureType::g10c_pgp_signature_detached);
145 auto siginfo0 = signatureFields[0]->validateSignatureAsync(false, false, -1 /* now */, false, false, {});
146 signatureFields[0]->validateSignatureResult();
147 QCOMPARE(siginfo0->getSignatureValStatus(), SignatureValidationStatus::SIGNATURE_VALID);
148 QCOMPARE(siginfo0->getSignerName(), std::string { "testuser" });
149 QCOMPARE(siginfo0->getCertificateInfo()->getNickName().toStr(), std::string { "36E39802E4F49A259091DA69381B80FEF3535BC1" });
150 }
151 }
152
153 QTEST_GUILESS_MAIN(TestSignWithGnupgPgp)
154 #include "check_create_pgp_signature1.moc"