• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

mailcommon

  • sources
  • kde-4.14
  • kdepim
  • mailcommon
  • filter
  • autotests
filteractionpipethroughtest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Sandro Knauß <bugs@sandroknauss.de>
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 "filteractionpipethroughtest.h"
21 #include "filteractions/filteractionpipethrough.h"
22 #include <qtest_kde.h>
23 #include <kdebug.h>
24 
25 using namespace MailCommon;
26 
27 FilterActionPipeThroughTest::FilterActionPipeThroughTest()
28 {
29 
30 }
31 
32 void FilterActionPipeThroughTest::setOutput(FilterAction *filter, const QByteArray &output)
33 {
34  QByteArray sendData = output;
35  filter->argsFromString(QLatin1String("echo \"")+sendData.replace('"',"\\\"")+"\"");
36 }
37 
38 
39 void FilterActionPipeThroughTest::testWithNoCommand()
40 {
41  /* No command to exceute -> no output -> error
42  */
43 
44  FilterActionPipeThrough filter(this);
45  KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
46  Akonadi::Item item;
47  item.setPayload<KMime::Message::Ptr>(msgPtr);
48  ItemContext context(item, true);
49 
50  filter.argsFromString("");
51  QCOMPARE(filter.process(context, false), FilterAction::ErrorButGoOn);
52  QCOMPARE(context.needsPayloadStore(), false);
53 }
54 
55 void FilterActionPipeThroughTest::testWithInvalidCommandPath()
56 {
57  /* put a mail in the pipe and make sure we get the same output
58  */
59  QByteArray data =
60  "From: Konqui <konqui@kde.org>\n"
61  "To: Friends <friends@kde.org>\n"
62  "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
63  "Subject: Sample message\n"
64  "MIME-Version: 1.0\n"
65  "Content-type: multipart/mixed; boundary=\"simple boundary\"\n"
66  "\n"
67  "\n"
68  "--simple boundary\n"
69  "Content-type: text/plain; charset=us-ascii\n"
70  "\n"
71  "This is explicitly typed plain US-ASCII text.\n"
72  "It DOES end with a linebreak.\n"
73  "\n"
74  "--simple boundary--\n";
75 
76  FilterActionPipeThrough filter(this);
77  KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
78  Akonadi::Item item;
79  msgPtr->setContent(data);
80  item.setPayload<KMime::Message::Ptr>(msgPtr);
81  ItemContext context(item, true);
82 
83  filter.argsFromString(QLatin1String("/home/cat "));
84  QCOMPARE(filter.process(context, false), FilterAction::ErrorButGoOn);
85  QCOMPARE(context.needsPayloadStore(), false);
86  QCOMPARE(msgPtr->encodedContent(), data);
87 }
88 
89 void FilterActionPipeThroughTest::testCommandWithoutOutput()
90 {
91  /* Valid command but no output -> error
92  */
93 
94  FilterActionPipeThrough filter(this);
95  KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
96  Akonadi::Item item;
97  item.setPayload<KMime::Message::Ptr>(msgPtr);
98  ItemContext context(item, true);
99 
100  filter.argsFromString("echo ''");
101  QCOMPARE(filter.process(context, false), FilterAction::ErrorButGoOn);
102  QCOMPARE(context.needsPayloadStore(), false);
103 }
104 
105 void FilterActionPipeThroughTest::testWithMailOutput()
106 {
107  /* Make sure that mail is not changed from output to KMIME::Message
108  * and also no assemble changes the mail
109  *
110  * Very important for not breake signatures from mails.
111  */
112  QByteArray data =
113  "From: Konqui <konqui@kde.org>\n"
114  "To: Friends <friends@kde.org>\n"
115  "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
116  "Subject: Sample message\n"
117  "MIME-Version: 1.0\n"
118  "Content-type: multipart/mixed;\n"
119  " boundary=\"simple boundary\"\n"
120  "\n"
121  "\n"
122  "--simple boundary\n"
123  "Content-type: text/plain;\n"
124  " charset=us-ascii\n"
125  "\n"
126  "This is explicitly typed plain US-ASCII text.\n"
127  "It DOES end with a linebreak.\n"
128  "\n"
129  "--simple boundary--\n";
130 
131  FilterActionPipeThrough filter(this);
132  KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
133  Akonadi::Item item;
134  item.setPayload<KMime::Message::Ptr>(msgPtr);
135  ItemContext context(item, true);
136 
137  setOutput(&filter, data);
138  QCOMPARE(filter.process(context, false), FilterAction::GoOn);
139  QCOMPARE(context.needsPayloadStore(), true);
140  QByteArray expected = data + '\n';
141  QCOMPARE( msgPtr->encodedContent(), expected);
142  msgPtr->assemble(); //Make sure that the message isFrozen so no submimes do not change
143  QCOMPARE( msgPtr->encodedContent(), expected);
144 }
145 
146 
147 void FilterActionPipeThroughTest::testCopyMail()
148 {
149  /* put a mail in the pipe and make sure we get the same output
150  */
151  QByteArray data =
152  "From: Konqui <konqui@kde.org>\n"
153  "To: Friends <friends@kde.org>\n"
154  "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
155  "Subject: Sample message\n"
156  "MIME-Version: 1.0\n"
157  "Content-type: multipart/mixed; boundary=\"simple boundary\"\n"
158  "\n"
159  "\n"
160  "--simple boundary\n"
161  "Content-type: text/plain; charset=us-ascii\n"
162  "\n"
163  "This is explicitly typed plain US-ASCII text.\n"
164  "It DOES end with a linebreak.\n"
165  "\n"
166  "--simple boundary--\n";
167 
168  FilterActionPipeThrough filter(this);
169  KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
170  Akonadi::Item item;
171  msgPtr->setContent(data);
172  item.setPayload<KMime::Message::Ptr>(msgPtr);
173  ItemContext context(item, true);
174 
175  filter.argsFromString(QLatin1String("cat "));
176  QCOMPARE(filter.process(context, false), FilterAction::GoOn);
177  QCOMPARE(context.needsPayloadStore(), true);
178  QCOMPARE(msgPtr->encodedContent(), data);
179 }
180 
181 void FilterActionPipeThroughTest::testXUidUnchange()
182 {
183  // the X-UID header isn't changed -> mail isn't changed anyhow
184  QByteArray data =
185  "From: Konqui <konqui@kde.org>\n"
186  "To: Friends <friends@kde.org>\n"
187  "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
188  "Subject: Sample message\n"
189  "MIME-Version: 1.0\n"
190  "X-UID: XXXX1\n"
191  "Content-type: multipart/mixed;\n"
192  " boundary=\"simple boundary\"\n"
193  "\n"
194  "\n"
195  "--simple boundary\n"
196  "Content-type: text/plain;\n"
197  " charset=us-ascii\n"
198  "\n"
199  "This is explicitly typed plain US-ASCII text.\n"
200  "It DOES end with a linebreak.\n"
201  "\n"
202  "--simple boundary--\n";
203 
204  FilterActionPipeThrough filter(this);
205  KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
206  Akonadi::Item item;
207  msgPtr->setContent(data);
208  msgPtr->parse();
209  item.setPayload<KMime::Message::Ptr>(msgPtr);
210  ItemContext context(item, true);
211 
212  filter.argsFromString(QLatin1String("cat "));
213  QCOMPARE(filter.process(context, false), FilterAction::GoOn);
214  QCOMPARE(context.needsPayloadStore(), true);
215  QCOMPARE(QString::fromAscii(msgPtr->encodedContent()), QString::fromAscii(data));
216 }
217 
218 void FilterActionPipeThroughTest::testXUidRemoved()
219 {
220  /* Make sure if the X-Uid is removed from pipe through, that we add it again
221  * but we have to assemble the mail, so we create some changes in the header.
222  * More important is, that the body isn't changed.
223  */
224 
225  QByteArray data =
226  "From: Konqui <konqui@kde.org>\n"
227  "To: Friends <friends@kde.org>\n"
228  "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
229  "Subject: Sample message\n"
230  "MIME-Version: 1.0\n"
231  "X-UID: XXXX1\n"
232  "Content-type: multipart/mixed;\n"
233  " boundary=\"simple boundary\"\n"
234  "\n"
235  "\n"
236  "--simple boundary\n"
237  "Content-type: text/plain;\n"
238  " charset=us-ascii\n"
239  "\n"
240  "This is explicitly typed plain US-ASCII text.\n"
241  "It DOES end with a linebreak.\n"
242  "\n"
243  "--simple boundary--\n";
244 
245  QByteArray send =
246  "From: Konqui <konqui@kde.org>\n"
247  "To: Friends <friends@kde.org>\n"
248  "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
249  "Subject: Sample message\n"
250  "MIME-Version: 1.0\n"
251  "Content-type: multipart/mixed;\n"
252  " boundary=\"simple boundary\"\n"
253  "\n"
254  "\n"
255  "--simple boundary\n"
256  "Content-type: text/plain;\n"
257  " charset=us-ascii\n"
258  "\n"
259  "This is explicitly typed plain US-ASCII text.\n"
260  "It DOES end with a linebreak.\n"
261  "\n"
262  "--simple boundary--\n";
263 
264  QByteArray output =
265  "From: Konqui <konqui@kde.org>\n"
266  "To: Friends <friends@kde.org>\n"
267  "Date: Sun, 21 Mar 1993 23:56:48 -0800\n" // <- this is removed, because we assemble
268  "Subject: Sample message\n"
269  "MIME-Version: 1.0\n"
270  "Content-Type: multipart/mixed; boundary=\"simple boundary\"\n" // <- this nweline is removed, because we assemble
271  "X-UID: XXXX1\n"
272  "\n"
273  "\n"
274  "--simple boundary\n" // <- body isn't changed
275  "Content-type: text/plain;\n"
276  " charset=us-ascii\n"
277  "\n"
278  "This is explicitly typed plain US-ASCII text.\n"
279  "It DOES end with a linebreak.\n"
280  "\n"
281  "--simple boundary--\n"
282  "\n";
283 
284  FilterActionPipeThrough filter(this);
285  KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
286  Akonadi::Item item;
287  msgPtr->setContent(data);
288  msgPtr->parse();
289  item.setPayload<KMime::Message::Ptr>(msgPtr);
290  ItemContext context(item, true);
291  setOutput(&filter, send);
292  QCOMPARE(filter.process(context, false), FilterAction::GoOn);
293  QCOMPARE(context.needsPayloadStore(), true);
294  QCOMPARE(msgPtr->encodedContent(), output);
295 }
296 
297 void FilterActionPipeThroughTest::shouldRequiresPart()
298 {
299  FilterActionPipeThrough filter(this);
300  QCOMPARE(filter.requiredPart(), SearchRule::CompleteMessage);
301 }
302 
303 void FilterActionPipeThroughTest::testXUidChange()
304 {
305  /* Make sure if the X-Uid is changed from pipe through, that we put is to the original value again.
306  * The mail is assembled again, so we create some changes in the header.
307  * More important is, that the body isn't changed.
308  */
309 
310  QByteArray data =
311  "From: Konqui <konqui@kde.org>\n"
312  "To: Friends <friends@kde.org>\n"
313  "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
314  "Subject: Sample message\n"
315  "MIME-Version: 1.0\n"
316  "X-UID: XXXX1\n"
317  "Content-type: multipart/mixed;\n"
318  " boundary=\"simple boundary\"\n"
319  "\n"
320  "\n"
321  "--simple boundary\n"
322  "Content-type: text/plain;\n"
323  " charset=us-ascii\n"
324  "\n"
325  "This is explicitly typed plain US-ASCII text.\n"
326  "It DOES end with a linebreak.\n"
327  "\n"
328  "--simple boundary--\n";
329 
330  QByteArray send =
331  "From: Konqui <konqui@kde.org>\n"
332  "To: Friends <friends@kde.org>\n"
333  "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n"
334  "Subject: Sample message\n"
335  "MIME-Version: 1.0\n"
336  "X-UID: XXXX2\n"
337  "Content-type: multipart/mixed;\n"
338  " boundary=\"simple boundary\"\n"
339  "\n"
340  "\n"
341  "--simple boundary\n"
342  "Content-type: text/plain;\n"
343  " charset=us-ascii\n"
344  "\n"
345  "This is explicitly typed plain US-ASCII text.\n"
346  "It DOES end with a linebreak.\n"
347  "\n"
348  "--simple boundary--\n";
349 
350  QByteArray output =
351  "From: Konqui <konqui@kde.org>\n"
352  "To: Friends <friends@kde.org>\n"
353  "Date: Sun, 21 Mar 1993 23:56:48 -0800\n" // <- this is removed, because we assemble
354  "Subject: Sample message\n"
355  "MIME-Version: 1.0\n"
356  "Content-Type: multipart/mixed; boundary=\"simple boundary\"\n" // <- this nweline is removed, because we assemble
357  "X-UID: XXXX1\n"
358  "\n"
359  "\n"
360  "--simple boundary\n" // <- body isn't changed
361  "Content-type: text/plain;\n"
362  " charset=us-ascii\n"
363  "\n"
364  "This is explicitly typed plain US-ASCII text.\n"
365  "It DOES end with a linebreak.\n"
366  "\n"
367  "--simple boundary--\n"
368  "\n";
369 
370  FilterActionPipeThrough filter(this);
371  KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message());
372  Akonadi::Item item;
373  msgPtr->setContent(data);
374  msgPtr->parse();
375  item.setPayload<KMime::Message::Ptr>(msgPtr);
376  ItemContext context(item, true);
377  setOutput(&filter, send);
378  QCOMPARE(filter.process(context, false), FilterAction::GoOn);
379  QCOMPARE(context.needsPayloadStore(), true);
380  QCOMPARE(msgPtr->encodedContent(), output);
381 }
382 
383 QTEST_KDEMAIN( FilterActionPipeThroughTest, NoGUI )
QString::fromAscii
QString fromAscii(const char *str, int size)
MailCommon::FilterAction::GoOn
Go on with applying filter actions.
Definition: filteraction.h:62
FilterActionPipeThroughTest::FilterActionPipeThroughTest
FilterActionPipeThroughTest()
Definition: filteractionpipethroughtest.cpp:27
QByteArray
MailCommon::FilterAction::requiredPart
virtual SearchRule::RequiredPart requiredPart() const =0
Returns the required part from the item that is needed for the action to operate. ...
MailCommon::SearchRule::CompleteMessage
Definition: searchrule.h:82
MailCommon::FilterAction
Abstract base class for mail filter actions.
Definition: filteraction.h:52
FilterActionPipeThroughTest
Definition: filteractionpipethroughtest.h:29
MailCommon::FilterAction::ErrorButGoOn
A non-critical error occurred.
Definition: filteraction.h:63
QByteArray::replace
QByteArray & replace(int pos, int len, const char *after)
MailCommon::FilterActionPipeThrough
Definition: filteractionpipethrough.h:32
QLatin1String
filteractionpipethroughtest.h
MailCommon::FilterAction::process
virtual ReturnCode process(ItemContext &context, bool applyOnOutbound) const =0
Execute action on given message (inside the item context).
MailCommon::FilterAction::argsFromString
virtual void argsFromString(const QString &argsStr)=0
Read extra arguments from given string.
context
const char * context
Definition: searchpatternedit.cpp:54
MailCommon::ItemContext
A helper class for the filtering process.
Definition: itemcontext.h:39
filteractionpipethrough.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailcommon

Skip menu "mailcommon"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal