summaryrefslogtreecommitdiffstats
path: root/framework/domain/mimetreeparser/tests/gpgerrortest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/domain/mimetreeparser/tests/gpgerrortest.cpp')
-rw-r--r--framework/domain/mimetreeparser/tests/gpgerrortest.cpp214
1 files changed, 0 insertions, 214 deletions
diff --git a/framework/domain/mimetreeparser/tests/gpgerrortest.cpp b/framework/domain/mimetreeparser/tests/gpgerrortest.cpp
deleted file mode 100644
index 4254d972..00000000
--- a/framework/domain/mimetreeparser/tests/gpgerrortest.cpp
+++ /dev/null
@@ -1,214 +0,0 @@
1/*
2 Copyright (c) 2016 Sandro Knauß <knauss@kolabsystems.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "interface.h"
21#include "interface_p.h"
22
23#include <QGpgME/Protocol>
24#include <gpgme++/context.h>
25#include <gpgme++/engineinfo.h>
26#include <gpgme.h>
27
28#include <QDebug>
29#include <QDir>
30#include <QProcess>
31#include <QTest>
32
33QByteArray readMailFromFile(const QString &mailFile)
34{
35 QFile file(QLatin1String(MAIL_DATA_DIR) + QLatin1Char('/') + mailFile);
36 file.open(QIODevice::ReadOnly);
37 Q_ASSERT(file.isOpen());
38 return file.readAll();
39}
40
41void killAgent(const QString& dir)
42{
43 QProcess proc;
44 proc.setProgram(QStringLiteral("gpg-connect-agent"));
45 QStringList arguments;
46 arguments << "-S " << dir + "/S.gpg-agent";
47 proc.start();
48 proc.waitForStarted();
49 proc.write("KILLAGENT\n");
50 proc.write("BYE\n");
51 proc.closeWriteChannel();
52 proc.waitForFinished();
53}
54
55class GpgErrorTest : public QObject
56{
57 Q_OBJECT
58
59private slots:
60
61 void testGpgConfiguredCorrectly()
62 {
63 setEnv("GNUPGHOME", GNUPGHOME);
64
65 Parser parser(readMailFromFile("openpgp-inline-charset-encrypted.mbox"));
66
67 auto contentPartList = parser.collectContentParts();
68 QCOMPARE(contentPartList.size(), 1);
69 auto contentPart = contentPartList[0];
70 QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "plaintext");
71 auto contentList = contentPart->content("plaintext");
72 QVERIFY(contentList[0]->content().startsWith("asdasd"));
73 QCOMPARE(contentList[0]->encryptions().size(), 1);
74 auto enc = contentList[0]->encryptions()[0];
75 QCOMPARE(enc->errorType(), Encryption::NoError);
76 QCOMPARE(enc->errorString(), QString());
77 QCOMPARE((int) enc->recipients().size(), 2);
78 }
79
80 void testNoGPGInstalled_data()
81 {
82 QTest::addColumn<QString>("mailFileName");
83
84 QTest::newRow("openpgp-inline-charset-encrypted") << "openpgp-inline-charset-encrypted.mbox";
85 QTest::newRow("openpgp-encrypted-attachment-and-non-encrypted-attachment") << "openpgp-encrypted-attachment-and-non-encrypted-attachment.mbox";
86 QTest::newRow("smime-encrypted") << "smime-encrypted.mbox";
87 }
88
89 void testNoGPGInstalled()
90 {
91 QFETCH(QString, mailFileName);
92
93 setEnv("PATH", "/nonexististing");
94 setGpgMEfname("/nonexisting/gpg", "");
95
96 Parser parser(readMailFromFile(mailFileName));
97 auto contentPartList = parser.collectContentParts();
98
99 QCOMPARE(contentPartList.size(), 1);
100 auto contentPart = contentPartList[0];
101 QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "plaintext");
102 auto contentList = contentPart->content("plaintext");
103 QCOMPARE(contentList[0]->encryptions().size(), 1);
104 QVERIFY(contentList[0]->content().isEmpty());
105 auto enc = contentList[0]->encryptions()[0];
106 qDebug() << "HUHU"<< enc->errorType();
107 QCOMPARE(enc->errorType(), Encryption::UnknownError);
108 QCOMPARE(enc->errorString(), QString("Crypto plug-in \"OpenPGP\" could not decrypt the data.<br />Error: No data"));
109 QCOMPARE((int) enc->recipients().size(), 0);
110 }
111
112 void testGpgIncorrectGPGHOME_data()
113 {
114 QTest::addColumn<QString>("mailFileName");
115
116 QTest::newRow("openpgp-inline-charset-encrypted") << "openpgp-inline-charset-encrypted.mbox";
117 QTest::newRow("openpgp-encrypted-attachment-and-non-encrypted-attachment") << "openpgp-encrypted-attachment-and-non-encrypted-attachment.mbox";
118 QTest::newRow("smime-encrypted") << "smime-encrypted.mbox";
119 }
120
121 void testGpgIncorrectGPGHOME()
122 {
123 QFETCH(QString, mailFileName);
124 setEnv("GNUPGHOME", QByteArray(GNUPGHOME) + QByteArray("noexist"));
125
126 Parser parser(readMailFromFile(mailFileName));
127
128 auto contentPartList = parser.collectContentParts();
129 QCOMPARE(contentPartList.size(), 1);
130 auto contentPart = contentPartList[0];
131 QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "plaintext");
132 auto contentList = contentPart->content("plaintext");
133 QCOMPARE(contentList[0]->encryptions().size(), 1);
134 QCOMPARE(contentList[0]->signatures().size(), 0);
135 QVERIFY(contentList[0]->content().isEmpty());
136 auto enc = contentList[0]->encryptions()[0];
137 qDebug() << enc->errorType();
138 QCOMPARE(enc->errorType(), Encryption::KeyMissing);
139 QCOMPARE(enc->errorString(), QString("Crypto plug-in \"OpenPGP\" could not decrypt the data.<br />Error: Decryption failed"));
140 QCOMPARE((int) enc->recipients().size(), 2);
141 }
142
143public Q_SLOTS:
144 void init()
145 {
146 mResetGpgmeEngine = false;
147 mModifiedEnv.clear();
148 {
149 QGpgME::openpgp(); // We need to intialize it, otherwise ctx will be a nullpointer
150 const GpgME::Context *ctx = GpgME::Context::createForProtocol(GpgME::Protocol::OpenPGP);
151 const auto engineinfo = ctx->engineInfo();
152 mGpgmeEngine_fname = engineinfo.fileName();
153 }
154 mEnv = QProcessEnvironment::systemEnvironment();
155 unsetEnv("GNUPGHOME");
156 }
157
158 void cleanup()
159 {
160 QCoreApplication::sendPostedEvents();
161
162 const QString &gnupghome = qgetenv("GNUPGHOME");
163 if (!gnupghome.isEmpty()) {
164 killAgent(gnupghome);
165 }
166
167 resetGpgMfname();
168 resetEnv();
169 }
170private:
171 void unsetEnv(const QByteArray &name)
172 {
173 mModifiedEnv << name;
174 unsetenv(name);
175 }
176
177 void setEnv(const QByteArray &name, const QByteArray &value)
178 {
179 mModifiedEnv << name;
180 setenv(name, value , 1);
181 }
182
183 void resetEnv()
184 {
185 foreach(const auto &i, mModifiedEnv) {
186 if (mEnv.contains(i)) {
187 setenv(i, mEnv.value(i).toUtf8(), 1);
188 } else {
189 unsetenv(i);
190 }
191 }
192 }
193
194 void resetGpgMfname()
195 {
196 if (mResetGpgmeEngine) {
197 gpgme_set_engine_info (GPGME_PROTOCOL_OpenPGP, mGpgmeEngine_fname, NULL);
198 }
199 }
200
201 void setGpgMEfname(const QByteArray &fname, const QByteArray &homedir)
202 {
203 mResetGpgmeEngine = true;
204 gpgme_set_engine_info (GPGME_PROTOCOL_OpenPGP, fname, homedir);
205 }
206
207 QSet<QByteArray> mModifiedEnv;
208 QProcessEnvironment mEnv;
209 bool mResetGpgmeEngine;
210 QByteArray mGpgmeEngine_fname;
211};
212
213QTEST_GUILESS_MAIN(GpgErrorTest)
214#include "gpgerrortest.moc"