File indexing completed on 2024-11-10 04:50:00
0001 /* 0002 * SPDX-FileCopyrightText: 2015 Sandro Knauß <bugs@sandroknauss.de> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "filteractionpipethroughtest.h" 0008 #include "../filteractions/filteractionpipethrough.h" 0009 #include <QTest> 0010 0011 using namespace MailCommon; 0012 0013 FilterActionPipeThroughTest::FilterActionPipeThroughTest() = default; 0014 0015 void FilterActionPipeThroughTest::setOutput(FilterAction *filter, const QByteArray &output) 0016 { 0017 QByteArray sendData = output; 0018 filter->argsFromString(QLatin1StringView("echo \"") + QString::fromUtf8(sendData.replace('"', "\\\"")) + QStringLiteral("\"")); 0019 } 0020 0021 void FilterActionPipeThroughTest::testWithNoCommand() 0022 { 0023 /* No command to execute -> no output -> error 0024 */ 0025 0026 FilterActionPipeThrough filter(this); 0027 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0028 Akonadi::Item item; 0029 item.setPayload<KMime::Message::Ptr>(msgPtr); 0030 ItemContext context(item, true); 0031 0032 filter.argsFromString(QLatin1StringView("")); 0033 QCOMPARE(filter.process(context, false), FilterAction::ErrorButGoOn); 0034 QCOMPARE(context.needsPayloadStore(), false); 0035 } 0036 0037 void FilterActionPipeThroughTest::testWithInvalidCommandPath() 0038 { 0039 /* put a mail in the pipe and make sure we get the same output 0040 */ 0041 QByteArray data = 0042 "From: Konqui <konqui@kde.org>\n" 0043 "To: Friends <friends@kde.org>\n" 0044 "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n" 0045 "Subject: Sample message\n" 0046 "MIME-Version: 1.0\n" 0047 "Content-type: multipart/mixed; boundary=\"simple boundary\"\n" 0048 "\n" 0049 "\n" 0050 "--simple boundary\n" 0051 "Content-type: text/plain; charset=us-ascii\n" 0052 "\n" 0053 "This is explicitly typed plain US-ASCII text.\n" 0054 "It DOES end with a linebreak.\n" 0055 "\n" 0056 "--simple boundary--\n"; 0057 0058 FilterActionPipeThrough filter(this); 0059 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0060 Akonadi::Item item; 0061 msgPtr->setContent(data); 0062 item.setPayload<KMime::Message::Ptr>(msgPtr); 0063 ItemContext context(item, true); 0064 0065 filter.argsFromString(QStringLiteral("/home/cat ")); 0066 QCOMPARE(filter.process(context, false), FilterAction::ErrorButGoOn); 0067 QCOMPARE(context.needsPayloadStore(), false); 0068 QCOMPARE(msgPtr->encodedContent(), data); 0069 } 0070 0071 void FilterActionPipeThroughTest::testCommandWithoutOutput() 0072 { 0073 /* Valid command but no output -> error 0074 */ 0075 0076 FilterActionPipeThrough filter(this); 0077 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0078 Akonadi::Item item; 0079 item.setPayload<KMime::Message::Ptr>(msgPtr); 0080 ItemContext context(item, true); 0081 0082 filter.argsFromString(QStringLiteral("echo ''")); 0083 QCOMPARE(filter.process(context, false), FilterAction::ErrorButGoOn); 0084 QCOMPARE(context.needsPayloadStore(), false); 0085 } 0086 0087 void FilterActionPipeThroughTest::testWithMailOutput() 0088 { 0089 /* Make sure that mail is not changed from output to KMIME::Message 0090 * and also no assemble changes the mail 0091 * 0092 * Very important for not break signatures from mails. 0093 */ 0094 QByteArray data = 0095 "From: Konqui <konqui@kde.org>\n" 0096 "To: Friends <friends@kde.org>\n" 0097 "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n" 0098 "Subject: Sample message\n" 0099 "MIME-Version: 1.0\n" 0100 "Content-type: multipart/mixed;\n" 0101 " boundary=\"simple boundary\"\n" 0102 "\n" 0103 "\n" 0104 "--simple boundary\n" 0105 "Content-type: text/plain;\n" 0106 " charset=us-ascii\n" 0107 "\n" 0108 "This is explicitly typed plain US-ASCII text.\n" 0109 "It DOES end with a linebreak.\n" 0110 "\n" 0111 "--simple boundary--\n"; 0112 0113 FilterActionPipeThrough filter(this); 0114 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0115 Akonadi::Item item; 0116 item.setPayload<KMime::Message::Ptr>(msgPtr); 0117 ItemContext context(item, true); 0118 0119 setOutput(&filter, data); 0120 QCOMPARE(filter.process(context, false), FilterAction::GoOn); 0121 QCOMPARE(context.needsPayloadStore(), true); 0122 QByteArray expected = data + '\n'; 0123 QCOMPARE(msgPtr->encodedContent(), expected); 0124 msgPtr->assemble(); // Make sure that the message isFrozen so no submimes do not change 0125 QCOMPARE(msgPtr->encodedContent(), expected); 0126 } 0127 0128 void FilterActionPipeThroughTest::testCopyMail() 0129 { 0130 /* put a mail in the pipe and make sure we get the same output 0131 */ 0132 QByteArray data = 0133 "From: Konqui <konqui@kde.org>\n" 0134 "To: Friends <friends@kde.org>\n" 0135 "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n" 0136 "Subject: Sample message\n" 0137 "MIME-Version: 1.0\n" 0138 "Content-type: multipart/mixed; boundary=\"simple boundary\"\n" 0139 "\n" 0140 "\n" 0141 "--simple boundary\n" 0142 "Content-type: text/plain; charset=us-ascii\n" 0143 "\n" 0144 "This is explicitly typed plain US-ASCII text.\n" 0145 "It DOES end with a linebreak.\n" 0146 "\n" 0147 "--simple boundary--\n"; 0148 0149 FilterActionPipeThrough filter(this); 0150 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0151 Akonadi::Item item; 0152 msgPtr->setContent(data); 0153 item.setPayload<KMime::Message::Ptr>(msgPtr); 0154 ItemContext context(item, true); 0155 0156 filter.argsFromString(QStringLiteral("cat ")); 0157 QCOMPARE(filter.process(context, false), FilterAction::GoOn); 0158 QCOMPARE(context.needsPayloadStore(), true); 0159 QCOMPARE(msgPtr->encodedContent(), data); 0160 } 0161 0162 void FilterActionPipeThroughTest::testXUidUnchange() 0163 { 0164 // the X-UID header isn't changed -> mail isn't changed anyhow 0165 QByteArray data = 0166 "From: Konqui <konqui@kde.org>\n" 0167 "To: Friends <friends@kde.org>\n" 0168 "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n" 0169 "Subject: Sample message\n" 0170 "MIME-Version: 1.0\n" 0171 "X-UID: XXXX1\n" 0172 "Content-type: multipart/mixed;\n" 0173 " boundary=\"simple boundary\"\n" 0174 "\n" 0175 "\n" 0176 "--simple boundary\n" 0177 "Content-type: text/plain;\n" 0178 " charset=us-ascii\n" 0179 "\n" 0180 "This is explicitly typed plain US-ASCII text.\n" 0181 "It DOES end with a linebreak.\n" 0182 "\n" 0183 "--simple boundary--\n"; 0184 0185 FilterActionPipeThrough filter(this); 0186 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0187 Akonadi::Item item; 0188 msgPtr->setContent(data); 0189 msgPtr->parse(); 0190 item.setPayload<KMime::Message::Ptr>(msgPtr); 0191 ItemContext context(item, true); 0192 0193 filter.argsFromString(QStringLiteral("cat ")); 0194 QCOMPARE(filter.process(context, false), FilterAction::GoOn); 0195 QCOMPARE(context.needsPayloadStore(), true); 0196 QCOMPARE(QString::fromLatin1(msgPtr->encodedContent()), QString::fromLatin1(data)); 0197 } 0198 0199 void FilterActionPipeThroughTest::testXUidRemoved() 0200 { 0201 /* Make sure if the X-Uid is removed from pipe through, that we add it again 0202 * but we have to assemble the mail, so we create some changes in the header. 0203 * More important is, that the body isn't changed. 0204 */ 0205 0206 QByteArray data = 0207 "From: Konqui <konqui@kde.org>\n" 0208 "To: Friends <friends@kde.org>\n" 0209 "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n" 0210 "Subject: Sample message\n" 0211 "MIME-Version: 1.0\n" 0212 "X-UID: XXXX1\n" 0213 "Content-type: multipart/mixed;\n" 0214 " boundary=\"simple boundary\"\n" 0215 "\n" 0216 "\n" 0217 "--simple boundary\n" 0218 "Content-type: text/plain;\n" 0219 " charset=us-ascii\n" 0220 "\n" 0221 "This is explicitly typed plain US-ASCII text.\n" 0222 "It DOES end with a linebreak.\n" 0223 "\n" 0224 "--simple boundary--\n"; 0225 0226 QByteArray send = 0227 "From: Konqui <konqui@kde.org>\n" 0228 "To: Friends <friends@kde.org>\n" 0229 "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n" 0230 "Subject: Sample message\n" 0231 "MIME-Version: 1.0\n" 0232 "Content-type: multipart/mixed;\n" 0233 " boundary=\"simple boundary\"\n" 0234 "\n" 0235 "\n" 0236 "--simple boundary\n" 0237 "Content-type: text/plain;\n" 0238 " charset=us-ascii\n" 0239 "\n" 0240 "This is explicitly typed plain US-ASCII text.\n" 0241 "It DOES end with a linebreak.\n" 0242 "\n" 0243 "--simple boundary--\n"; 0244 0245 QByteArray output = 0246 "From: Konqui <konqui@kde.org>\n" 0247 "To: Friends <friends@kde.org>\n" 0248 "Date: Sun, 21 Mar 1993 23:56:48 -0800\n" // <- this is removed, because we assemble 0249 "Subject: Sample message\n" 0250 "MIME-Version: 1.0\n" 0251 "Content-Type: multipart/mixed; boundary=\"simple boundary\"\n" // <- this nweline is removed, because we assemble 0252 "X-UID: XXXX1\n" 0253 "\n" 0254 "\n" 0255 "--simple boundary\n" // <- body isn't changed 0256 "Content-type: text/plain;\n" 0257 " charset=us-ascii\n" 0258 "\n" 0259 "This is explicitly typed plain US-ASCII text.\n" 0260 "It DOES end with a linebreak.\n" 0261 "\n" 0262 "--simple boundary--\n" 0263 "\n"; 0264 0265 FilterActionPipeThrough filter(this); 0266 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0267 Akonadi::Item item; 0268 msgPtr->setContent(data); 0269 msgPtr->parse(); 0270 item.setPayload<KMime::Message::Ptr>(msgPtr); 0271 ItemContext context(item, true); 0272 setOutput(&filter, send); 0273 QCOMPARE(filter.process(context, false), FilterAction::GoOn); 0274 QCOMPARE(context.needsPayloadStore(), true); 0275 QCOMPARE(msgPtr->encodedContent(), output); 0276 } 0277 0278 void FilterActionPipeThroughTest::shouldRequiresPart() 0279 { 0280 FilterActionPipeThrough filter(this); 0281 QCOMPARE(filter.requiredPart(), SearchRule::CompleteMessage); 0282 } 0283 0284 void FilterActionPipeThroughTest::testXUidChange() 0285 { 0286 /* Make sure if the X-Uid is changed from pipe through, that we put is to the original value again. 0287 * The mail is assembled again, so we create some changes in the header. 0288 * More important is, that the body isn't changed. 0289 */ 0290 0291 QByteArray data = 0292 "From: Konqui <konqui@kde.org>\n" 0293 "To: Friends <friends@kde.org>\n" 0294 "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n" 0295 "Subject: Sample message\n" 0296 "MIME-Version: 1.0\n" 0297 "X-UID: XXXX1\n" 0298 "Content-type: multipart/mixed;\n" 0299 " boundary=\"simple boundary\"\n" 0300 "\n" 0301 "\n" 0302 "--simple boundary\n" 0303 "Content-type: text/plain;\n" 0304 " charset=us-ascii\n" 0305 "\n" 0306 "This is explicitly typed plain US-ASCII text.\n" 0307 "It DOES end with a linebreak.\n" 0308 "\n" 0309 "--simple boundary--\n"; 0310 0311 QByteArray send = 0312 "From: Konqui <konqui@kde.org>\n" 0313 "To: Friends <friends@kde.org>\n" 0314 "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n" 0315 "Subject: Sample message\n" 0316 "MIME-Version: 1.0\n" 0317 "X-UID: XXXX2\n" 0318 "Content-type: multipart/mixed;\n" 0319 " boundary=\"simple boundary\"\n" 0320 "\n" 0321 "\n" 0322 "--simple boundary\n" 0323 "Content-type: text/plain;\n" 0324 " charset=us-ascii\n" 0325 "\n" 0326 "This is explicitly typed plain US-ASCII text.\n" 0327 "It DOES end with a linebreak.\n" 0328 "\n" 0329 "--simple boundary--\n"; 0330 0331 QByteArray output = 0332 "From: Konqui <konqui@kde.org>\n" 0333 "To: Friends <friends@kde.org>\n" 0334 "Date: Sun, 21 Mar 1993 23:56:48 -0800\n" // <- this is removed, because we assemble 0335 "Subject: Sample message\n" 0336 "MIME-Version: 1.0\n" 0337 "Content-Type: multipart/mixed; boundary=\"simple boundary\"\n" // <- this nweline is removed, because we assemble 0338 "X-UID: XXXX1\n" 0339 "\n" 0340 "\n" 0341 "--simple boundary\n" // <- body isn't changed 0342 "Content-type: text/plain;\n" 0343 " charset=us-ascii\n" 0344 "\n" 0345 "This is explicitly typed plain US-ASCII text.\n" 0346 "It DOES end with a linebreak.\n" 0347 "\n" 0348 "--simple boundary--\n" 0349 "\n"; 0350 0351 FilterActionPipeThrough filter(this); 0352 KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); 0353 Akonadi::Item item; 0354 msgPtr->setContent(data); 0355 msgPtr->parse(); 0356 item.setPayload<KMime::Message::Ptr>(msgPtr); 0357 ItemContext context(item, true); 0358 setOutput(&filter, send); 0359 QCOMPARE(filter.process(context, false), FilterAction::GoOn); 0360 QCOMPARE(context.needsPayloadStore(), true); 0361 QCOMPARE(msgPtr->encodedContent(), output); 0362 } 0363 0364 QTEST_MAIN(FilterActionPipeThroughTest) 0365 0366 #include "moc_filteractionpipethroughtest.cpp"