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

0001 // SPDX-FileCopyrightText: 2015 Sandro Knauß <knauss@kolabsys.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "cryptohelpertest.h"
0005 
0006 #include "cryptohelper.h"
0007 
0008 #include <QTest>
0009 
0010 using namespace MimeTreeParser;
0011 
0012 QByteArray readMailFromFile(const QString &mailFile)
0013 {
0014     QFile file(QLatin1StringView(MAIL_DATA_DIR) + QLatin1Char('/') + mailFile);
0015     file.open(QIODevice::ReadOnly);
0016     Q_ASSERT(file.isOpen());
0017     return file.readAll();
0018 }
0019 
0020 void CryptoHelperTest::testPMFDEmpty()
0021 {
0022     QCOMPARE(prepareMessageForDecryption("").count(), 0);
0023 }
0024 
0025 void CryptoHelperTest::testPMFDWithNoPGPBlock()
0026 {
0027     const QByteArray text = "testblabla";
0028     const QList<Block> blocks = prepareMessageForDecryption(text);
0029     QCOMPARE(blocks.count(), 1);
0030     QCOMPARE(blocks[0].text(), text);
0031     QCOMPARE(blocks[0].type(), NoPgpBlock);
0032 }
0033 
0034 void CryptoHelperTest::testPGPBlockType()
0035 {
0036     const QString blockText = QStringLiteral("text");
0037     const QString preString = QStringLiteral("before\n");
0038     for (int i = 1; i <= PrivateKeyBlock; ++i) {
0039         QString name;
0040         switch (i) {
0041         case PgpMessageBlock:
0042             name = QStringLiteral("MESSAGE");
0043             break;
0044         case MultiPgpMessageBlock:
0045             name = QStringLiteral("MESSAGE PART");
0046             break;
0047         case SignatureBlock:
0048             name = QStringLiteral("SIGNATURE");
0049             break;
0050         case ClearsignedBlock:
0051             name = QStringLiteral("SIGNED MESSAGE");
0052             break;
0053         case PublicKeyBlock:
0054             name = QStringLiteral("PUBLIC KEY BLOCK");
0055             break;
0056         case PrivateKeyBlock:
0057             name = QStringLiteral("PRIVATE KEY BLOCK");
0058             break;
0059         }
0060         QString text = QLatin1StringView("-----BEGIN PGP ") + name + QLatin1Char('\n') + blockText;
0061         QList<Block> blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1());
0062         QCOMPARE(blocks.count(), 1);
0063         QCOMPARE(blocks[0].type(), UnknownBlock);
0064 
0065         text += QLatin1StringView("\n-----END PGP ") + name + QLatin1Char('\n');
0066         blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1());
0067         QCOMPARE(blocks.count(), 2);
0068         QCOMPARE(blocks[1].text(), text.toLatin1());
0069         QCOMPARE(blocks[1].type(), static_cast<PGPBlockType>(i));
0070     }
0071 }
0072 
0073 void CryptoHelperTest::testDeterminePGPBlockType()
0074 {
0075     const QString blockText = QStringLiteral("text");
0076     for (int i = 1; i <= PrivateKeyBlock; ++i) {
0077         QString name;
0078         switch (i) {
0079         case PgpMessageBlock:
0080             name = QStringLiteral("MESSAGE");
0081             break;
0082         case MultiPgpMessageBlock:
0083             name = QStringLiteral("MESSAGE PART");
0084             break;
0085         case SignatureBlock:
0086             name = QStringLiteral("SIGNATURE");
0087             break;
0088         case ClearsignedBlock:
0089             name = QStringLiteral("SIGNED MESSAGE");
0090             break;
0091         case PublicKeyBlock:
0092             name = QStringLiteral("PUBLIC KEY BLOCK");
0093             break;
0094         case PrivateKeyBlock:
0095             name = QStringLiteral("PRIVATE KEY BLOCK");
0096             break;
0097         }
0098         const QString text = QLatin1StringView("-----BEGIN PGP ") + name + QLatin1Char('\n') + blockText + QLatin1Char('\n');
0099         const Block block = Block(text.toLatin1());
0100         QCOMPARE(block.text(), text.toLatin1());
0101         QCOMPARE(block.type(), static_cast<PGPBlockType>(i));
0102     }
0103 }
0104 
0105 void CryptoHelperTest::testEmbededPGPBlock()
0106 {
0107     const QByteArray text = QByteArray("before\n-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\nafter");
0108     const QList<Block> blocks = prepareMessageForDecryption(text);
0109     QCOMPARE(blocks.count(), 3);
0110     QCOMPARE(blocks[0].text(), QByteArray("before\n"));
0111     QCOMPARE(blocks[1].text(), QByteArray("-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n"));
0112     QCOMPARE(blocks[2].text(), QByteArray("after"));
0113 }
0114 
0115 void CryptoHelperTest::testClearSignedMessage()
0116 {
0117     const QByteArray text = QByteArray(
0118         "before\n-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\nafter");
0119     const QList<Block> blocks = prepareMessageForDecryption(text);
0120     QCOMPARE(blocks.count(), 3);
0121     QCOMPARE(blocks[0].text(), QByteArray("before\n"));
0122     QCOMPARE(blocks[1].text(),
0123              QByteArray("-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\n"));
0124     QCOMPARE(blocks[2].text(), QByteArray("after"));
0125 }
0126 
0127 void CryptoHelperTest::testMultipleBlockMessage()
0128 {
0129     const QByteArray text = QByteArray(
0130         "before\n-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP "
0131         "SIGNATURE-----\nafter\n-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n");
0132     const QList<Block> blocks = prepareMessageForDecryption(text);
0133     QCOMPARE(blocks.count(), 4);
0134     QCOMPARE(blocks[0].text(), QByteArray("before\n"));
0135     QCOMPARE(blocks[1].text(),
0136              QByteArray("-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\n"));
0137     QCOMPARE(blocks[2].text(), QByteArray("after\n"));
0138     QCOMPARE(blocks[3].text(), QByteArray("-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n"));
0139 }
0140 
0141 void CryptoHelperTest::testDecryptMessage()
0142 {
0143     auto message = KMime::Message::Ptr(new KMime::Message);
0144     message->setContent(readMailFromFile(QLatin1StringView("openpgp-encrypted+signed.mbox")));
0145     message->parse();
0146 
0147     bool wasEncrypted = false;
0148     auto decryptedMessage = CryptoUtils::decryptMessage(message, wasEncrypted);
0149     QVERIFY(wasEncrypted);
0150     QVERIFY(decryptedMessage);
0151     QCOMPARE(decryptedMessage->decodedContent(), QByteArray("encrypted message text"));
0152     QCOMPARE(decryptedMessage->encodedContent(),
0153              QByteArray("From test@kolab.org Wed, 08 Sep 2010 17: 02:52 +0200\nFrom: OpenPGP Test <test@kolab.org>\nTo: test@kolab.org\nSubject: OpenPGP "
0154                         "encrypted\nDate: Wed, 08 Sep 2010 17:02:52 +0200\nUser-Agent: KMail/4.6 pre (Linux/2.6.34-rc2-2-default; KDE/4.5.60; x86_64; ; "
0155                         ")\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7Bit\nContent-Type: text/plain; charset=\"us-ascii\"\n\nencrypted message text"));
0156 }
0157 
0158 void CryptoHelperTest::testDecryptInlineMessage()
0159 {
0160     auto message = KMime::Message::Ptr(new KMime::Message);
0161     message->setContent(readMailFromFile(QLatin1StringView("openpgp-inline-encrypted+nonenc.mbox")));
0162     message->parse();
0163 
0164     qWarning() << message->decodedContent();
0165 
0166     bool wasEncrypted = false;
0167     auto decryptedMessage = CryptoUtils::decryptMessage(message, wasEncrypted);
0168     QVERIFY(wasEncrypted);
0169     QVERIFY(decryptedMessage);
0170     QCOMPARE(decryptedMessage->decodedContent(), QByteArray("Not encrypted not signed :(\n\nsome random text\n"));
0171     qWarning() << decryptedMessage->encodedContent();
0172     QCOMPARE(decryptedMessage->encodedContent(),
0173              QByteArray("From test@kolab.org Wed, 25 May 2011 23: 49:40 +0100\nFrom: OpenPGP Test <test@kolab.org>\nTo: test@kolab.org\nSubject: "
0174                         "inlinepgpencrypted + non enc text\nDate: Wed, 25 May 2011 23:49:40 +0100\nMessage-ID: "
0175                         "<1786696.yKXrOjjflF@herrwackelpudding.localhost>\nX-KMail-Transport: GMX\nX-KMail-Fcc: 28\nX-KMail-Drafts: 7\nX-KMail-Templates: "
0176                         "9\nUser-Agent: KMail/4.6 beta5 (Linux/2.6.34.7-0.7-desktop; KDE/4.6.41; x86_64;\n git-0269848; 2011-04-19)\nMIME-Version: "
0177                         "1.0\nContent-Type: text/plain; charset=\"us-ascii\"\n\nNot encrypted not signed :(\n\nsome random text\n"));
0178 }
0179 
0180 QTEST_APPLESS_MAIN(CryptoHelperTest)
0181 
0182 #include "moc_cryptohelpertest.cpp"