File indexing completed on 2024-05-12 05:22:53

0001 /*
0002     test_jobs.cpp
0003 
0004     This file is part of libkleopatra's test suite.
0005     SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-only
0008 */
0009 
0010 #include <libkleo/formatting.h>
0011 
0012 #include <qgpgme/keylistjob.h>
0013 #include <qgpgme/protocol.h>
0014 #include <qgpgme/signjob.h>
0015 
0016 #include <gpgme++/key.h>
0017 #include <gpgme++/keylistresult.h>
0018 #include <gpgme++/signingresult.h>
0019 
0020 #include <KAboutData>
0021 #include <QDebug>
0022 
0023 #include <KLocalizedString>
0024 #include <QApplication>
0025 #include <QCommandLineParser>
0026 #include <memory>
0027 
0028 static const char *protocol = nullptr;
0029 
0030 static void testSign()
0031 {
0032     const QGpgME::Protocol *proto = !strcmp(protocol, "openpgp") ? QGpgME::openpgp() : QGpgME::smime();
0033     Q_ASSERT(proto);
0034 
0035     qDebug() << "Using protocol" << proto->name();
0036 
0037     std::vector<GpgME::Key> signingKeys;
0038 
0039     std::unique_ptr<QGpgME::KeyListJob> listJob(proto->keyListJob(false, false, true)); // use validating keylisting
0040     if (listJob.get()) {
0041         // ##### Adjust this to your own identity
0042         listJob->exec(QStringList(QStringLiteral("kloecker@kde.org")), true /*secret*/, signingKeys);
0043         Q_ASSERT(!signingKeys.empty());
0044     } else {
0045         Q_ASSERT(0); // job failed
0046     }
0047 
0048     QGpgME::SignJob *job = proto->signJob(true, true);
0049 
0050     QByteArray plainText = "Hallo Leute\n"; // like gpgme's t-sign.c
0051     qDebug() << "plainText=" << plainText;
0052 
0053     qDebug() << " signing with" << signingKeys[0].primaryFingerprint();
0054 
0055     QByteArray signature;
0056     const GpgME::SigningResult res = job->exec(signingKeys, plainText, GpgME::Clearsigned, signature);
0057     if (res.error().isCanceled()) {
0058         qDebug() << "signing was canceled by user";
0059         return;
0060     }
0061     if (res.error()) {
0062         qDebug() << "signing failed:" << Kleo::Formatting::errorAsString(res.error());
0063         return;
0064     }
0065     qDebug() << "signing resulted in signature=" << signature;
0066 }
0067 
0068 int main(int argc, char **argv)
0069 {
0070     protocol = "openpgp";
0071     if (argc == 2) {
0072         protocol = argv[1];
0073         argc = 1; // hide from KDE
0074     }
0075     QApplication app(argc, argv);
0076     KAboutData aboutData(QStringLiteral("test_jobs"), i18n("Signing Job Test"), QStringLiteral("0.1"));
0077     QCommandLineParser parser;
0078     KAboutData::setApplicationData(aboutData);
0079     aboutData.setupCommandLine(&parser);
0080     parser.process(app);
0081     aboutData.processCommandLine(&parser);
0082 
0083     testSign();
0084 }