summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/mime/mimetreeparser/autotests/cryptohelpertest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/domain/mime/mimetreeparser/autotests/cryptohelpertest.cpp')
-rw-r--r--framework/src/domain/mime/mimetreeparser/autotests/cryptohelpertest.cpp144
1 files changed, 144 insertions, 0 deletions
diff --git a/framework/src/domain/mime/mimetreeparser/autotests/cryptohelpertest.cpp b/framework/src/domain/mime/mimetreeparser/autotests/cryptohelpertest.cpp
new file mode 100644
index 00000000..251e657d
--- /dev/null
+++ b/framework/src/domain/mime/mimetreeparser/autotests/cryptohelpertest.cpp
@@ -0,0 +1,144 @@
1/* Copyright 2015 Sandro Knauß <knauss@kolabsys.com>
2
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License as
5 published by the Free Software Foundation; either version 2 of
6 the License or (at your option) version 3 or any later version
7 accepted by the membership of KDE e.V. (or its successor approved
8 by the membership of KDE e.V.), which shall act as a proxy
9 defined in Section 14 of version 3 of the license.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "cryptohelpertest.h"
20
21#include "cryptohelper.h"
22
23#include <QTest>
24
25using namespace MimeTreeParser;
26
27void CryptoHelperTest::testPMFDEmpty()
28{
29 QCOMPARE(prepareMessageForDecryption("").count(), 0);
30}
31
32void CryptoHelperTest::testPMFDWithNoPGPBlock()
33{
34 const QByteArray text = "testblabla";
35 const QList<Block> blocks = prepareMessageForDecryption(text);
36 QCOMPARE(blocks.count(), 1);
37 QCOMPARE(blocks[0].text(), text);
38 QCOMPARE(blocks[0].type(), NoPgpBlock);
39}
40
41void CryptoHelperTest::testPGPBlockType()
42{
43 const QString blockText = QStringLiteral("text");
44 const QString preString = QStringLiteral("before\n");
45 for (int i = 1; i <= PrivateKeyBlock; ++i) {
46 QString name;
47 switch (i) {
48 case PgpMessageBlock:
49 name = QStringLiteral("MESSAGE");
50 break;
51 case MultiPgpMessageBlock:
52 name = QStringLiteral("MESSAGE PART");
53 break;
54 case SignatureBlock:
55 name = QStringLiteral("SIGNATURE");
56 break;
57 case ClearsignedBlock:
58 name = QStringLiteral("SIGNED MESSAGE");
59 break;
60 case PublicKeyBlock:
61 name = QStringLiteral("PUBLIC KEY BLOCK");
62 break;
63 case PrivateKeyBlock:
64 name = QStringLiteral("PRIVATE KEY BLOCK");
65 break;
66 }
67 QString text = QLatin1String("-----BEGIN PGP ") + name + QLatin1String("\n") + blockText;
68 QList<Block> blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1());
69 QCOMPARE(blocks.count(), 1);
70 QCOMPARE(blocks[0].type(), UnknownBlock);
71
72 text += QLatin1String("\n-----END PGP ") + name + QLatin1String("\n");
73 blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1());
74 QCOMPARE(blocks.count(), 2);
75 QCOMPARE(blocks[1].text(), text.toLatin1());
76 QCOMPARE(blocks[1].type(), static_cast<PGPBlockType>(i));
77 }
78}
79
80void CryptoHelperTest::testDeterminePGPBlockType()
81{
82 const QString blockText = QStringLiteral("text");
83 for (int i = 1; i <= PrivateKeyBlock; ++i) {
84 QString name;
85 switch (i) {
86
87 case PgpMessageBlock:
88 name = QStringLiteral("MESSAGE");
89 break;
90 case MultiPgpMessageBlock:
91 name = QStringLiteral("MESSAGE PART");
92 break;
93 case SignatureBlock:
94 name = QStringLiteral("SIGNATURE");
95 break;
96 case ClearsignedBlock:
97 name = QStringLiteral("SIGNED MESSAGE");
98 break;
99 case PublicKeyBlock:
100 name = QStringLiteral("PUBLIC KEY BLOCK");
101 break;
102 case PrivateKeyBlock:
103 name = QStringLiteral("PRIVATE KEY BLOCK");
104 break;
105 }
106 const QString text = QLatin1String("-----BEGIN PGP ") + name + QLatin1String("\n") + blockText + QLatin1String("\n");
107 const Block block = Block(text.toLatin1());
108 QCOMPARE(block.text(), text.toLatin1());
109 QCOMPARE(block.type(), static_cast<PGPBlockType>(i));
110 }
111}
112
113void CryptoHelperTest::testEmbededPGPBlock()
114{
115 const QByteArray text = QByteArray("before\n-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\nafter");
116 const QList<Block> blocks = prepareMessageForDecryption(text);
117 QCOMPARE(blocks.count(), 3);
118 QCOMPARE(blocks[0].text(), QByteArray("before\n"));
119 QCOMPARE(blocks[1].text(), QByteArray("-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n"));
120 QCOMPARE(blocks[2].text(), QByteArray("after"));
121}
122
123void CryptoHelperTest::testClearSignedMessage()
124{
125 const QByteArray text = QByteArray("before\n-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\nafter");
126 const QList<Block> blocks = prepareMessageForDecryption(text);
127 QCOMPARE(blocks.count(), 3);
128 QCOMPARE(blocks[0].text(), QByteArray("before\n"));
129 QCOMPARE(blocks[1].text(), QByteArray("-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\n"));
130 QCOMPARE(blocks[2].text(), QByteArray("after"));
131}
132
133void CryptoHelperTest::testMultipleBlockMessage()
134{
135 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");
136 const QList<Block> blocks = prepareMessageForDecryption(text);
137 QCOMPARE(blocks.count(), 4);
138 QCOMPARE(blocks[0].text(), QByteArray("before\n"));
139 QCOMPARE(blocks[1].text(), QByteArray("-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\n"));
140 QCOMPARE(blocks[2].text(), QByteArray("after\n"));
141 QCOMPARE(blocks[3].text(), QByteArray("-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n"));
142}
143
144QTEST_APPLESS_MAIN(CryptoHelperTest)