~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Net/testsuite/src/MailMessageTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// MailMessageTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Net/testsuite/src/MailMessageTest.cpp#2 $
 
5
//
 
6
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person or organization
 
10
// obtaining a copy of the software and accompanying documentation covered by
 
11
// this license (the "Software") to use, reproduce, display, distribute,
 
12
// execute, and transmit the Software, and to prepare derivative works of the
 
13
// Software, and to permit third-parties to whom the Software is furnished to
 
14
// do so, all subject to the following:
 
15
// 
 
16
// The copyright notices in the Software and this entire statement, including
 
17
// the above license grant, this restriction and the following disclaimer,
 
18
// must be included in all copies of the Software, in whole or in part, and
 
19
// all derivative works of the Software, unless such copies or derivative
 
20
// works are solely in the form of machine-executable object code generated by
 
21
// a source language processor.
 
22
// 
 
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
29
// DEALINGS IN THE SOFTWARE.
 
30
//
 
31
 
 
32
 
 
33
#include "MailMessageTest.h"
 
34
#include "CppUnit/TestCaller.h"
 
35
#include "CppUnit/TestSuite.h"
 
36
#include "Poco/Net/MailMessage.h"
 
37
#include "Poco/Net/MailRecipient.h"
 
38
#include "Poco/Net/PartHandler.h"
 
39
#include "Poco/Net/StringPartSource.h"
 
40
#include "Poco/Net/MediaType.h"
 
41
#include "Poco/Timestamp.h"
 
42
#include <sstream>
 
43
#include <vector>
 
44
 
 
45
 
 
46
using Poco::Net::MailMessage;
 
47
using Poco::Net::MailRecipient;
 
48
using Poco::Net::MessageHeader;
 
49
using Poco::Net::PartHandler;
 
50
using Poco::Net::MediaType;
 
51
using Poco::Net::StringPartSource;
 
52
using Poco::Timestamp;
 
53
 
 
54
 
 
55
namespace
 
56
{
 
57
        class StringPartHandler: public PartHandler
 
58
        {
 
59
        public:
 
60
                StringPartHandler()
 
61
                {
 
62
                }
 
63
                
 
64
                void handlePart(const MessageHeader& header, std::istream& stream)
 
65
                {
 
66
                        _disp.push_back(header["Content-Disposition"]);
 
67
                        _type.push_back(header["Content-Type"]);
 
68
                        std::string data;
 
69
                        int ch = stream.get();
 
70
                        while (ch > 0)
 
71
                        {
 
72
                                data += (char) ch;
 
73
                                ch = stream.get();
 
74
                        }
 
75
                        _data.push_back(data);
 
76
                }
 
77
                
 
78
                const std::vector<std::string>& data() const
 
79
                {
 
80
                        return _data;
 
81
                }
 
82
 
 
83
                const std::vector<std::string>& disp() const
 
84
                {
 
85
                        return _disp;
 
86
                }
 
87
 
 
88
                const std::vector<std::string>& type() const
 
89
                {
 
90
                        return _type;
 
91
                }
 
92
                
 
93
        private:
 
94
                std::vector<std::string> _data;
 
95
                std::vector<std::string> _disp;
 
96
                std::vector<std::string> _type;
 
97
        };
 
98
}
 
99
 
 
100
 
 
101
MailMessageTest::MailMessageTest(const std::string& name): CppUnit::TestCase(name)
 
102
{
 
103
}
 
104
 
 
105
 
 
106
MailMessageTest::~MailMessageTest()
 
107
{
 
108
}
 
109
 
 
110
 
 
111
void MailMessageTest::testWriteQP()
 
112
{
 
113
        MailMessage message;
 
114
        MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "john.doe@no.where", "John Doe");
 
115
        MailRecipient r2(MailRecipient::CC_RECIPIENT, "jane.doe@no.where", "Jane Doe");
 
116
        MailRecipient r3(MailRecipient::BCC_RECIPIENT, "walter.foo@no.where", "Frank Foo");
 
117
        MailRecipient r4(MailRecipient::BCC_RECIPIENT, "bernie.bar@no.where", "Bernie Bar");
 
118
        message.addRecipient(r1);
 
119
        message.addRecipient(r2);
 
120
        message.addRecipient(r3);
 
121
        message.addRecipient(r4);
 
122
        message.setSubject("Test Message");
 
123
        message.setSender("poco@appinf.com");
 
124
        message.setContent(
 
125
                "Hello, world!\r\n"
 
126
                "This is a test for the MailMessage class.\r\n"
 
127
                "To test the quoted-printable encoding, we'll put an extra long line here. This should be enough.\r\n"
 
128
                "And here is some more =fe.\r\n"
 
129
        );
 
130
        Timestamp ts(0);
 
131
        message.setDate(ts);
 
132
        
 
133
        assert (!message.isMultipart());
 
134
        
 
135
        std::ostringstream str;
 
136
        message.write(str);
 
137
        std::string s = str.str();
 
138
        assert (s == 
 
139
                "CC: Jane Doe <jane.doe@no.where>\r\n"
 
140
                "Content-Transfer-Encoding: quoted-printable\r\n"
 
141
                "Content-Type: text/plain\r\n"
 
142
                "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
 
143
                "From: poco@appinf.com\r\n"
 
144
                "Subject: Test Message\r\n"
 
145
                "To: John Doe <john.doe@no.where>\r\n"
 
146
                "\r\n"
 
147
                "Hello, world!\r\n"
 
148
                "This is a test for the MailMessage class.\r\n"
 
149
                "To test the quoted-printable encoding, we'll put an extra long line here. T=\r\n"
 
150
                "his should be enough.\r\n"
 
151
                "And here is some more =3Dfe.\r\n"
 
152
        );
 
153
}
 
154
 
 
155
 
 
156
void MailMessageTest::testWrite8Bit()
 
157
{
 
158
        MailMessage message;
 
159
        MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "john.doe@no.where", "John Doe");
 
160
        message.addRecipient(r1);
 
161
        message.setSubject("Test Message");
 
162
        message.setSender("poco@appinf.com");
 
163
        message.setContent(
 
164
                "Hello, world!\r\n"
 
165
                "This is a test for the MailMessage class.\r\n",
 
166
                MailMessage::ENCODING_8BIT
 
167
        );
 
168
        Timestamp ts(0);
 
169
        message.setDate(ts);
 
170
        
 
171
        std::ostringstream str;
 
172
        message.write(str);
 
173
        std::string s = str.str();
 
174
        assert (s == 
 
175
                "Content-Transfer-Encoding: 8bit\r\n"
 
176
                "Content-Type: text/plain\r\n"
 
177
                "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
 
178
                "From: poco@appinf.com\r\n"
 
179
                "Subject: Test Message\r\n"
 
180
                "To: John Doe <john.doe@no.where>\r\n"
 
181
                "\r\n"
 
182
                "Hello, world!\r\n"
 
183
                "This is a test for the MailMessage class.\r\n"
 
184
        );
 
185
}
 
186
 
 
187
 
 
188
void MailMessageTest::testWriteBase64()
 
189
{
 
190
        MailMessage message;
 
191
        MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "john.doe@no.where", "John Doe");
 
192
        message.addRecipient(r1);
 
193
        message.setSubject("Test Message");
 
194
        message.setSender("poco@appinf.com");
 
195
        message.setContent(
 
196
                "Hello, world!\r\n"
 
197
                "This is a test for the MailMessage class.\r\n",
 
198
                MailMessage::ENCODING_BASE64
 
199
        );
 
200
        Timestamp ts(0);
 
201
        message.setDate(ts);
 
202
 
 
203
        std::ostringstream str;
 
204
        message.write(str);
 
205
        std::string s = str.str();
 
206
        assert (s == 
 
207
                "Content-Transfer-Encoding: base64\r\n"
 
208
                "Content-Type: text/plain\r\n"
 
209
                "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
 
210
                "From: poco@appinf.com\r\n"
 
211
                "Subject: Test Message\r\n"
 
212
                "To: John Doe <john.doe@no.where>\r\n"
 
213
                "\r\n"
 
214
                "SGVsbG8sIHdvcmxkIQ0KVGhpcyBpcyBhIHRlc3QgZm9yIHRoZSBNYWlsTWVzc2FnZSBjbGFz\r\n"
 
215
                "cy4NCg=="
 
216
        );
 
217
}
 
218
 
 
219
 
 
220
void MailMessageTest::testWriteManyRecipients()
 
221
{
 
222
        MailMessage message;
 
223
        MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "john.doe@no.where", "John Doe");
 
224
        MailRecipient r2(MailRecipient::PRIMARY_RECIPIENT, "jane.doe@no.where", "Jane Doe");
 
225
        MailRecipient r3(MailRecipient::PRIMARY_RECIPIENT, "walter.foo@no.where", "Frank Foo");
 
226
        MailRecipient r4(MailRecipient::PRIMARY_RECIPIENT, "bernie.bar@no.where", "Bernie Bar");
 
227
        MailRecipient r5(MailRecipient::PRIMARY_RECIPIENT, "joe.spammer@no.where", "Joe Spammer");
 
228
        message.addRecipient(r1);
 
229
        message.addRecipient(r2);
 
230
        message.addRecipient(r3);
 
231
        message.addRecipient(r4);
 
232
        message.addRecipient(r5);
 
233
        message.setSubject("Test Message");
 
234
        message.setSender("poco@appinf.com");
 
235
        message.setContent(
 
236
                "Hello, world!\r\n"
 
237
                "This is a test for the MailMessage class.\r\n",
 
238
                MailMessage::ENCODING_8BIT
 
239
        );
 
240
        Timestamp ts(0);
 
241
        message.setDate(ts);
 
242
        
 
243
        std::ostringstream str;
 
244
        message.write(str);
 
245
        std::string s = str.str();
 
246
        assert (s == 
 
247
                "Content-Transfer-Encoding: 8bit\r\n"
 
248
                "Content-Type: text/plain\r\n"
 
249
                "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
 
250
                "From: poco@appinf.com\r\n"
 
251
                "Subject: Test Message\r\n"
 
252
                "To: John Doe <john.doe@no.where>, Jane Doe <jane.doe@no.where>, \r\n"
 
253
        "\tFrank Foo <walter.foo@no.where>, Bernie Bar <bernie.bar@no.where>, \r\n"
 
254
        "\tJoe Spammer <joe.spammer@no.where>\r\n"
 
255
                "\r\n"
 
256
                "Hello, world!\r\n"
 
257
                "This is a test for the MailMessage class.\r\n"
 
258
        );
 
259
}
 
260
 
 
261
 
 
262
void MailMessageTest::testWriteMultiPart()
 
263
{
 
264
        MailMessage message;
 
265
        MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "john.doe@no.where", "John Doe");
 
266
        message.addRecipient(r1);
 
267
        message.setSubject("Test Message");
 
268
        message.setSender("poco@appinf.com");
 
269
        Timestamp ts(0);
 
270
        message.setDate(ts);
 
271
        message.addContent(new StringPartSource("Hello World!\r\n", "text/plain"), MailMessage::ENCODING_8BIT);
 
272
        message.addAttachment("sample", new StringPartSource("This is some binary data. Really.", "application/octet-stream", "sample.dat"));
 
273
 
 
274
        assert (message.isMultipart());
 
275
 
 
276
        std::ostringstream str;
 
277
        message.write(str);
 
278
        std::string s = str.str();
 
279
        std::string rawMsg(
 
280
                "Content-Type: multipart/mixed; boundary=$\r\n"
 
281
                "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
 
282
                "From: poco@appinf.com\r\n"
 
283
                "Mime-Version: 1.0\r\n"
 
284
                "Subject: Test Message\r\n"
 
285
                "To: John Doe <john.doe@no.where>\r\n"
 
286
                "\r\n"
 
287
                "--$\r\n"
 
288
                "Content-Disposition: inline\r\n"
 
289
                "Content-Transfer-Encoding: 8bit\r\n"
 
290
                "Content-Type: text/plain\r\n"
 
291
                "\r\n"
 
292
                "Hello World!\r\n"
 
293
                "\r\n"
 
294
                "--$\r\n"
 
295
                "Content-Disposition: attachment; filename=sample.dat\r\n"
 
296
                "Content-Transfer-Encoding: base64\r\n"
 
297
                "Content-Type: application/octet-stream; name=sample\r\n"
 
298
                "\r\n"
 
299
                "VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhLiBSZWFsbHku\r\n"
 
300
                "--$--\r\n"
 
301
        );
 
302
        std::string::size_type p1 = s.find('=') + 1;
 
303
        std::string::size_type p2 = s.find('\r');
 
304
        std::string boundary(s, p1, p2 - p1);
 
305
        std::string msg;
 
306
        for (std::string::const_iterator it = rawMsg.begin(); it != rawMsg.end(); ++it)
 
307
        {
 
308
                if (*it == '$')
 
309
                        msg += boundary;
 
310
                else
 
311
                        msg += *it;
 
312
        }
 
313
        assert (s == msg);
 
314
}
 
315
 
 
316
 
 
317
void MailMessageTest::testReadQP()
 
318
{
 
319
        std::istringstream istr(
 
320
                "Content-Transfer-Encoding: quoted-printable\r\n"
 
321
                "Content-Type: text/plain\r\n"
 
322
                "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
 
323
                "From: poco@appinf.com\r\n"
 
324
                "Subject: Test Message\r\n"
 
325
                "To: John Doe <john.doe@no.where>\r\n"
 
326
                "\r\n"
 
327
                "Hello, world!\r\n"
 
328
                "This is a test for the MailMessage class.\r\n"
 
329
                "To test the quoted-printable encoding, we'll put an extra long line here. T=\r\n"
 
330
                "his should be enough.\r\n"
 
331
                "And here is some more =3Dfe.\r\n"
 
332
        );
 
333
        
 
334
        MailMessage message;
 
335
        message.read(istr);
 
336
        
 
337
        assert (message.getSender() == "poco@appinf.com");
 
338
        assert (message.getContentType() == "text/plain");
 
339
        assert (message.getContent() == 
 
340
                "Hello, world!\r\n"
 
341
                "This is a test for the MailMessage class.\r\n"
 
342
                "To test the quoted-printable encoding, we'll put an extra long line here. This should be enough.\r\n"
 
343
                "And here is some more =fe.\r\n"
 
344
        );
 
345
}
 
346
 
 
347
 
 
348
void MailMessageTest::testRead8Bit()
 
349
{
 
350
        std::istringstream istr(
 
351
                "Content-Transfer-Encoding: 8bit\r\n"
 
352
                "Content-Type: text/plain\r\n"
 
353
                "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
 
354
                "From: poco@appinf.com\r\n"
 
355
                "Subject: Test Message\r\n"
 
356
                "To: John Doe <john.doe@no.where>\r\n"
 
357
                "\r\n"
 
358
                "Hello, world!\r\n"
 
359
                "This is a test for the MailMessage class.\r\n"
 
360
        );
 
361
        
 
362
        MailMessage message;
 
363
        message.read(istr);
 
364
        
 
365
        assert (message.getSender() == "poco@appinf.com");
 
366
        assert (message.getContentType() == "text/plain");
 
367
        assert (message.getContent() == 
 
368
                "Hello, world!\r\n"
 
369
                "This is a test for the MailMessage class.\r\n"
 
370
        );
 
371
}
 
372
 
 
373
 
 
374
void MailMessageTest::testReadMultiPart()
 
375
{
 
376
        std::istringstream istr(
 
377
                "Content-Type: multipart/mixed; boundary=MIME_boundary_01234567\r\n"
 
378
                "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
 
379
                "From: poco@appinf.com\r\n"
 
380
                "Mime-Version: 1.0\r\n"
 
381
                "Subject: Test Message\r\n"
 
382
                "To: John Doe <john.doe@no.where>\r\n"
 
383
                "\r\n"
 
384
                "\r\n"
 
385
                "--MIME_boundary_01234567\r\n"
 
386
                "Content-Disposition: inline\r\n"
 
387
                "Content-Transfer-Encoding: 8bit\r\n"
 
388
                "Content-Type: text/plain\r\n"
 
389
                "\r\n"
 
390
                "Hello World!\r\n"
 
391
                "\r\n"
 
392
                "--MIME_boundary_01234567\r\n"
 
393
                "Content-Disposition: attachment; filename=sample.dat\r\n"
 
394
                "Content-Transfer-Encoding: base64\r\n"
 
395
                "Content-Type: application/octet-stream; name=sample\r\n"
 
396
                "\r\n"
 
397
                "VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhLiBSZWFsbHku\r\n"
 
398
                "--MIME_boundary_01234567--\r\n"
 
399
        );
 
400
        
 
401
        StringPartHandler handler;
 
402
        MailMessage message;
 
403
        message.read(istr, handler);
 
404
        
 
405
        assert (handler.data().size() == 2);
 
406
        assert (handler.data()[0] == "Hello World!\r\n");
 
407
        assert (handler.type()[0] == "text/plain");
 
408
        assert (handler.disp()[0] == "inline");
 
409
 
 
410
        assert (handler.data()[1] == "This is some binary data. Really.");
 
411
        assert (handler.type()[1] == "application/octet-stream; name=sample");
 
412
        assert (handler.disp()[1] == "attachment; filename=sample.dat");
 
413
}
 
414
 
 
415
 
 
416
void MailMessageTest::setUp()
 
417
{
 
418
}
 
419
 
 
420
 
 
421
void MailMessageTest::tearDown()
 
422
{
 
423
}
 
424
 
 
425
 
 
426
CppUnit::Test* MailMessageTest::suite()
 
427
{
 
428
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MailMessageTest");
 
429
 
 
430
        CppUnit_addTest(pSuite, MailMessageTest, testWriteQP);
 
431
        CppUnit_addTest(pSuite, MailMessageTest, testWrite8Bit);
 
432
        CppUnit_addTest(pSuite, MailMessageTest, testWriteBase64);
 
433
        CppUnit_addTest(pSuite, MailMessageTest, testWriteManyRecipients);
 
434
        CppUnit_addTest(pSuite, MailMessageTest, testWriteMultiPart);
 
435
        CppUnit_addTest(pSuite, MailMessageTest, testReadQP);
 
436
        CppUnit_addTest(pSuite, MailMessageTest, testRead8Bit);
 
437
        CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPart);
 
438
 
 
439
        return pSuite;
 
440
}