File indexing completed on 2024-10-27 04:58:05

0001 // Copyright 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 void CryptoHelperTest::testPMFDEmpty()
0013 {
0014     QCOMPARE(prepareMessageForDecryption("").count(), 0);
0015 }
0016 
0017 void CryptoHelperTest::testPMFDWithNoPGPBlock()
0018 {
0019     const QByteArray text = "testblabla";
0020     const QList<Block> blocks = prepareMessageForDecryption(text);
0021     QCOMPARE(blocks.count(), 1);
0022     QCOMPARE(blocks[0].text(), text);
0023     QCOMPARE(blocks[0].type(), NoPgpBlock);
0024 }
0025 
0026 void CryptoHelperTest::testPGPBlockType()
0027 {
0028     const QString blockText = QStringLiteral("text");
0029     const QString preString = QStringLiteral("before\n");
0030     for (int i = 1; i <= PrivateKeyBlock; ++i) {
0031         QString name;
0032         switch (i) {
0033         case PgpMessageBlock:
0034             name = QStringLiteral("MESSAGE");
0035             break;
0036         case MultiPgpMessageBlock:
0037             name = QStringLiteral("MESSAGE PART");
0038             break;
0039         case SignatureBlock:
0040             name = QStringLiteral("SIGNATURE");
0041             break;
0042         case ClearsignedBlock:
0043             name = QStringLiteral("SIGNED MESSAGE");
0044             break;
0045         case PublicKeyBlock:
0046             name = QStringLiteral("PUBLIC KEY BLOCK");
0047             break;
0048         case PrivateKeyBlock:
0049             name = QStringLiteral("PRIVATE KEY BLOCK");
0050             break;
0051         }
0052         QString text = QLatin1String("-----BEGIN PGP ") + name + QLatin1String("\n") + blockText;
0053         QList<Block> blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1());
0054         QCOMPARE(blocks.count(), 1);
0055         QCOMPARE(blocks[0].type(), UnknownBlock);
0056 
0057         text += QLatin1String("\n-----END PGP ") + name + QLatin1String("\n");
0058         blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1());
0059         QCOMPARE(blocks.count(), 2);
0060         QCOMPARE(blocks[1].text(), text.toLatin1());
0061         QCOMPARE(blocks[1].type(), static_cast<PGPBlockType>(i));
0062     }
0063 }
0064 
0065 void CryptoHelperTest::testDeterminePGPBlockType()
0066 {
0067     const QString blockText = QStringLiteral("text");
0068     for (int i = 1; i <= PrivateKeyBlock; ++i) {
0069         QString name;
0070         switch (i) {
0071         case PgpMessageBlock:
0072             name = QStringLiteral("MESSAGE");
0073             break;
0074         case MultiPgpMessageBlock:
0075             name = QStringLiteral("MESSAGE PART");
0076             break;
0077         case SignatureBlock:
0078             name = QStringLiteral("SIGNATURE");
0079             break;
0080         case ClearsignedBlock:
0081             name = QStringLiteral("SIGNED MESSAGE");
0082             break;
0083         case PublicKeyBlock:
0084             name = QStringLiteral("PUBLIC KEY BLOCK");
0085             break;
0086         case PrivateKeyBlock:
0087             name = QStringLiteral("PRIVATE KEY BLOCK");
0088             break;
0089         }
0090         const QString text = QLatin1String("-----BEGIN PGP ") + name + QLatin1String("\n") + blockText + QLatin1String("\n");
0091         const Block block = Block(text.toLatin1());
0092         QCOMPARE(block.text(), text.toLatin1());
0093         QCOMPARE(block.type(), static_cast<PGPBlockType>(i));
0094     }
0095 }
0096 
0097 void CryptoHelperTest::testEmbededPGPBlock()
0098 {
0099     const QByteArray text = QByteArray("before\n-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\nafter");
0100     const QList<Block> blocks = prepareMessageForDecryption(text);
0101     QCOMPARE(blocks.count(), 3);
0102     QCOMPARE(blocks[0].text(), QByteArray("before\n"));
0103     QCOMPARE(blocks[1].text(), QByteArray("-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n"));
0104     QCOMPARE(blocks[2].text(), QByteArray("after"));
0105 }
0106 
0107 void CryptoHelperTest::testClearSignedMessage()
0108 {
0109     const QByteArray text = QByteArray(
0110         "before\n-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\nafter");
0111     const QList<Block> blocks = prepareMessageForDecryption(text);
0112     QCOMPARE(blocks.count(), 3);
0113     QCOMPARE(blocks[0].text(), QByteArray("before\n"));
0114     QCOMPARE(blocks[1].text(),
0115              QByteArray("-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\n"));
0116     QCOMPARE(blocks[2].text(), QByteArray("after"));
0117 }
0118 
0119 void CryptoHelperTest::testMultipleBlockMessage()
0120 {
0121     const QByteArray text = QByteArray(
0122         "before\n-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP "
0123         "SIGNATURE-----\nafter\n-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n");
0124     const QList<Block> blocks = prepareMessageForDecryption(text);
0125     QCOMPARE(blocks.count(), 4);
0126     QCOMPARE(blocks[0].text(), QByteArray("before\n"));
0127     QCOMPARE(blocks[1].text(),
0128              QByteArray("-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\n"));
0129     QCOMPARE(blocks[2].text(), QByteArray("after\n"));
0130     QCOMPARE(blocks[3].text(), QByteArray("-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n"));
0131 }
0132 
0133 QTEST_APPLESS_MAIN(CryptoHelperTest)