diff options
Diffstat (limited to 'framework/domain/mimetreeparser/tests/gpgerrortest.cpp')
-rw-r--r-- | framework/domain/mimetreeparser/tests/gpgerrortest.cpp | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/framework/domain/mimetreeparser/tests/gpgerrortest.cpp b/framework/domain/mimetreeparser/tests/gpgerrortest.cpp new file mode 100644 index 00000000..aca12280 --- /dev/null +++ b/framework/domain/mimetreeparser/tests/gpgerrortest.cpp | |||
@@ -0,0 +1,202 @@ | |||
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 | |||
33 | QByteArray 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 | |||
41 | void 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 | |||
55 | class GpgErrorTest : public QObject | ||
56 | { | ||
57 | Q_OBJECT | ||
58 | |||
59 | private 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 | QCOMPARE(contentList[0]->signatures().size(), 1); | ||
75 | } | ||
76 | |||
77 | void testNoGPGInstalled_data() | ||
78 | { | ||
79 | QTest::addColumn<QString>("mailFileName"); | ||
80 | |||
81 | QTest::newRow("openpgp-inline-charset-encrypted") << "openpgp-inline-charset-encrypted.mbox"; | ||
82 | QTest::newRow("openpgp-encrypted-attachment-and-non-encrypted-attachment") << "openpgp-encrypted-attachment-and-non-encrypted-attachment.mbox"; | ||
83 | QTest::newRow("smime-encrypted") << "smime-encrypted.mbox"; | ||
84 | } | ||
85 | |||
86 | void testNoGPGInstalled() | ||
87 | { | ||
88 | QFETCH(QString, mailFileName); | ||
89 | |||
90 | setEnv("PATH", "/nonexististing"); | ||
91 | setGpgMEfname("/nonexisting/gpg", ""); | ||
92 | |||
93 | Parser parser(readMailFromFile(mailFileName)); | ||
94 | auto contentPartList = parser.collectContentParts(); | ||
95 | |||
96 | QCOMPARE(contentPartList.size(), 1); | ||
97 | auto contentPart = contentPartList[0]; | ||
98 | QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "plaintext"); | ||
99 | auto contentList = contentPart->content("plaintext"); | ||
100 | QCOMPARE(contentList[0]->encryptions().size(), 1); | ||
101 | QCOMPARE(contentList[0]->signatures().size(), 0); | ||
102 | QVERIFY(contentList[0]->content().isEmpty()); | ||
103 | } | ||
104 | |||
105 | void testGpgIncorrectGPGHOME_data() | ||
106 | { | ||
107 | QTest::addColumn<QString>("mailFileName"); | ||
108 | |||
109 | QTest::newRow("openpgp-inline-charset-encrypted") << "openpgp-inline-charset-encrypted.mbox"; | ||
110 | QTest::newRow("openpgp-encrypted-attachment-and-non-encrypted-attachment") << "openpgp-encrypted-attachment-and-non-encrypted-attachment.mbox"; | ||
111 | QTest::newRow("smime-encrypted") << "smime-encrypted.mbox"; | ||
112 | } | ||
113 | |||
114 | void testGpgIncorrectGPGHOME() | ||
115 | { | ||
116 | QFETCH(QString, mailFileName); | ||
117 | setEnv("GNUPGHOME", QByteArray(GNUPGHOME) + QByteArray("noexist")); | ||
118 | |||
119 | Parser parser(readMailFromFile(mailFileName)); | ||
120 | |||
121 | auto contentPartList = parser.collectContentParts(); | ||
122 | QCOMPARE(contentPartList.size(), 1); | ||
123 | auto contentPart = contentPartList[0]; | ||
124 | QCOMPARE(contentPart->availableContents(), QVector<QByteArray>() << "plaintext"); | ||
125 | auto contentList = contentPart->content("plaintext"); | ||
126 | QCOMPARE(contentList[0]->encryptions().size(), 1); | ||
127 | QCOMPARE(contentList[0]->signatures().size(), 0); | ||
128 | QVERIFY(contentList[0]->content().isEmpty()); | ||
129 | } | ||
130 | |||
131 | public Q_SLOTS: | ||
132 | void init() | ||
133 | { | ||
134 | mResetGpgmeEngine = false; | ||
135 | mModifiedEnv.clear(); | ||
136 | { | ||
137 | QGpgME::openpgp(); // We need to intialize it, otherwise ctx will be a nullpointer | ||
138 | const GpgME::Context *ctx = GpgME::Context::createForProtocol(GpgME::Protocol::OpenPGP); | ||
139 | const auto engineinfo = ctx->engineInfo(); | ||
140 | mGpgmeEngine_fname = engineinfo.fileName(); | ||
141 | } | ||
142 | mEnv = QProcessEnvironment::systemEnvironment(); | ||
143 | unsetEnv("GNUPGHOME"); | ||
144 | } | ||
145 | |||
146 | void cleanup() | ||
147 | { | ||
148 | QCoreApplication::sendPostedEvents(); | ||
149 | |||
150 | const QString &gnupghome = qgetenv("GNUPGHOME"); | ||
151 | if (!gnupghome.isEmpty()) { | ||
152 | killAgent(gnupghome); | ||
153 | } | ||
154 | |||
155 | resetGpgMfname(); | ||
156 | resetEnv(); | ||
157 | } | ||
158 | private: | ||
159 | void unsetEnv(const QByteArray &name) | ||
160 | { | ||
161 | mModifiedEnv << name; | ||
162 | unsetenv(name); | ||
163 | } | ||
164 | |||
165 | void setEnv(const QByteArray &name, const QByteArray &value) | ||
166 | { | ||
167 | mModifiedEnv << name; | ||
168 | setenv(name, value , 1); | ||
169 | } | ||
170 | |||
171 | void resetEnv() | ||
172 | { | ||
173 | foreach(const auto &i, mModifiedEnv) { | ||
174 | if (mEnv.contains(i)) { | ||
175 | setenv(i, mEnv.value(i).toUtf8(), 1); | ||
176 | } else { | ||
177 | unsetenv(i); | ||
178 | } | ||
179 | } | ||
180 | } | ||
181 | |||
182 | void resetGpgMfname() | ||
183 | { | ||
184 | if (mResetGpgmeEngine) { | ||
185 | gpgme_set_engine_info (GPGME_PROTOCOL_OpenPGP, mGpgmeEngine_fname, NULL); | ||
186 | } | ||
187 | } | ||
188 | |||
189 | void setGpgMEfname(const QByteArray &fname, const QByteArray &homedir) | ||
190 | { | ||
191 | mResetGpgmeEngine = true; | ||
192 | gpgme_set_engine_info (GPGME_PROTOCOL_OpenPGP, fname, homedir); | ||
193 | } | ||
194 | |||
195 | QSet<QByteArray> mModifiedEnv; | ||
196 | QProcessEnvironment mEnv; | ||
197 | bool mResetGpgmeEngine; | ||
198 | QByteArray mGpgmeEngine_fname; | ||
199 | }; | ||
200 | |||
201 | QTEST_GUILESS_MAIN(GpgErrorTest) | ||
202 | #include "gpgerrortest.moc" | ||