File indexing completed on 2025-01-26 04:58:13
0001 /* Copyright 2015 Sandro Knauß <knauss@kolabsys.com> 0002 0003 This program is free software; you can redistribute it and/or 0004 modify it under the terms of the GNU General Public License as 0005 published by the Free Software Foundation; either version 2 of 0006 the License or (at your option) version 3 or any later version 0007 accepted by the membership of KDE e.V. (or its successor approved 0008 by the membership of KDE e.V.), which shall act as a proxy 0009 defined in Section 14 of version 3 of the license. 0010 0011 This program is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 #include "cryptohelpertest.h" 0020 0021 #include "cryptohelper.h" 0022 0023 #include <QTest> 0024 0025 using namespace MimeTreeParser; 0026 0027 void CryptoHelperTest::testPMFDEmpty() 0028 { 0029 QCOMPARE(prepareMessageForDecryption("").count(), 0); 0030 } 0031 0032 void CryptoHelperTest::testPMFDWithNoPGPBlock() 0033 { 0034 const QByteArray text = "testblabla"; 0035 const QList<Block> blocks = prepareMessageForDecryption(text); 0036 QCOMPARE(blocks.count(), 1); 0037 QCOMPARE(blocks[0].text(), text); 0038 QCOMPARE(blocks[0].type(), NoPgpBlock); 0039 } 0040 0041 void CryptoHelperTest::testPGPBlockType() 0042 { 0043 const QString blockText = QStringLiteral("text"); 0044 const QString preString = QStringLiteral("before\n"); 0045 for (int i = 1; i <= PrivateKeyBlock; ++i) { 0046 QString name; 0047 switch (i) { 0048 case PgpMessageBlock: 0049 name = QStringLiteral("MESSAGE"); 0050 break; 0051 case MultiPgpMessageBlock: 0052 name = QStringLiteral("MESSAGE PART"); 0053 break; 0054 case SignatureBlock: 0055 name = QStringLiteral("SIGNATURE"); 0056 break; 0057 case ClearsignedBlock: 0058 name = QStringLiteral("SIGNED MESSAGE"); 0059 break; 0060 case PublicKeyBlock: 0061 name = QStringLiteral("PUBLIC KEY BLOCK"); 0062 break; 0063 case PrivateKeyBlock: 0064 name = QStringLiteral("PRIVATE KEY BLOCK"); 0065 break; 0066 } 0067 QString text = QLatin1String("-----BEGIN PGP ") + name + QLatin1String("\n") + blockText; 0068 QList<Block> blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1()); 0069 QCOMPARE(blocks.count(), 1); 0070 QCOMPARE(blocks[0].type(), UnknownBlock); 0071 0072 text += QLatin1String("\n-----END PGP ") + name + QLatin1String("\n"); 0073 blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1()); 0074 QCOMPARE(blocks.count(), 2); 0075 QCOMPARE(blocks[1].text(), text.toLatin1()); 0076 QCOMPARE(blocks[1].type(), static_cast<PGPBlockType>(i)); 0077 } 0078 } 0079 0080 void CryptoHelperTest::testDeterminePGPBlockType() 0081 { 0082 const QString blockText = QStringLiteral("text"); 0083 for (int i = 1; i <= PrivateKeyBlock; ++i) { 0084 QString name; 0085 switch (i) { 0086 0087 case PgpMessageBlock: 0088 name = QStringLiteral("MESSAGE"); 0089 break; 0090 case MultiPgpMessageBlock: 0091 name = QStringLiteral("MESSAGE PART"); 0092 break; 0093 case SignatureBlock: 0094 name = QStringLiteral("SIGNATURE"); 0095 break; 0096 case ClearsignedBlock: 0097 name = QStringLiteral("SIGNED MESSAGE"); 0098 break; 0099 case PublicKeyBlock: 0100 name = QStringLiteral("PUBLIC KEY BLOCK"); 0101 break; 0102 case PrivateKeyBlock: 0103 name = QStringLiteral("PRIVATE KEY BLOCK"); 0104 break; 0105 } 0106 const QString text = QLatin1String("-----BEGIN PGP ") + name + QLatin1String("\n") + blockText + QLatin1String("\n"); 0107 const Block block = Block(text.toLatin1()); 0108 QCOMPARE(block.text(), text.toLatin1()); 0109 QCOMPARE(block.type(), static_cast<PGPBlockType>(i)); 0110 } 0111 } 0112 0113 void CryptoHelperTest::testEmbededPGPBlock() 0114 { 0115 const QByteArray text = QByteArray("before\n-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\nafter"); 0116 const QList<Block> blocks = prepareMessageForDecryption(text); 0117 QCOMPARE(blocks.count(), 3); 0118 QCOMPARE(blocks[0].text(), QByteArray("before\n")); 0119 QCOMPARE(blocks[1].text(), QByteArray("-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n")); 0120 QCOMPARE(blocks[2].text(), QByteArray("after")); 0121 } 0122 0123 void CryptoHelperTest::testClearSignedMessage() 0124 { 0125 const QByteArray text = QByteArray("before\n-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\nafter"); 0126 const QList<Block> blocks = prepareMessageForDecryption(text); 0127 QCOMPARE(blocks.count(), 3); 0128 QCOMPARE(blocks[0].text(), QByteArray("before\n")); 0129 QCOMPARE(blocks[1].text(), QByteArray("-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\n")); 0130 QCOMPARE(blocks[2].text(), QByteArray("after")); 0131 } 0132 0133 void CryptoHelperTest::testMultipleBlockMessage() 0134 { 0135 const QByteArray text = QByteArray("before\n-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\nafter\n-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n"); 0136 const QList<Block> blocks = prepareMessageForDecryption(text); 0137 QCOMPARE(blocks.count(), 4); 0138 QCOMPARE(blocks[0].text(), QByteArray("before\n")); 0139 QCOMPARE(blocks[1].text(), QByteArray("-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\n")); 0140 QCOMPARE(blocks[2].text(), QByteArray("after\n")); 0141 QCOMPARE(blocks[3].text(), QByteArray("-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n")); 0142 } 0143 0144 QTEST_APPLESS_MAIN(CryptoHelperTest)