1//
2// SMTPClientSessionTest.cpp
3//
4// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "SMTPClientSessionTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "DialogServer.h"
15#include "Poco/Net/SMTPClientSession.h"
16#include "Poco/Net/MailMessage.h"
17#include "Poco/Net/MailRecipient.h"
18#include "Poco/Net/NetException.h"
19
20
21using Poco::Net::SMTPClientSession;
22using Poco::Net::MailMessage;
23using Poco::Net::MailRecipient;
24using Poco::Net::SMTPException;
25
26
27SMTPClientSessionTest::SMTPClientSessionTest(const std::string& name): CppUnit::TestCase(name)
28{
29}
30
31
32SMTPClientSessionTest::~SMTPClientSessionTest()
33{
34}
35
36
37void SMTPClientSessionTest::testLoginEHLO()
38{
39 DialogServer server;
40 server.addResponse("220 localhost SMTP ready");
41 server.addResponse("250 Hello localhost");
42 server.addResponse("221 Bye");
43 SMTPClientSession session("127.0.0.1", server.port());
44 session.login("localhost");
45 std::string cmd = server.popCommand();
46 assertTrue (cmd == "EHLO localhost");
47 session.close();
48 cmd = server.popCommand();
49 assertTrue (cmd == "QUIT");
50}
51
52
53void SMTPClientSessionTest::testLoginHELO()
54{
55 DialogServer server;
56 server.addResponse("220 localhost SMTP ready");
57 server.addResponse("500 EHLO not understood");
58 server.addResponse("250 Hello localhost");
59 server.addResponse("221 Bye");
60 SMTPClientSession session("127.0.0.1", server.port());
61 session.login("localhost");
62 std::string cmd = server.popCommand();
63 assertTrue (cmd == "EHLO localhost");
64 cmd = server.popCommand();
65 assertTrue (cmd == "HELO localhost");
66 session.close();
67 cmd = server.popCommand();
68 assertTrue (cmd == "QUIT");
69}
70
71
72void SMTPClientSessionTest::testLoginFailed()
73{
74 DialogServer server;
75 server.addResponse("500 No SMTP service here");
76 server.addResponse("221 Bye");
77 SMTPClientSession session("127.0.0.1", server.port());
78 try
79 {
80 session.login("localhost");
81 fail("cannot login - must throw");
82 }
83 catch (SMTPException&)
84 {
85 }
86 session.close();
87}
88
89
90void SMTPClientSessionTest::testSend()
91{
92 DialogServer server;
93 server.addResponse("220 localhost SMTP ready");
94 server.addResponse("250 Hello localhost");
95 server.addResponse("250 OK");
96 server.addResponse("250 OK");
97 server.addResponse("354 Send data");
98 server.addResponse("250 OK");
99 server.addResponse("221 Bye");
100 SMTPClientSession session("127.0.0.1", server.port());
101 session.login("localhost");
102
103 MailMessage message;
104 message.setSender("john.doe@no.where");
105 message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, "jane.doe@no.where", "Jane Doe"));
106 message.setSubject("Test Message");
107 message.setContent("Hello\r\nblah blah\r\n\r\nJohn\r\n");
108 server.clearCommands();
109 session.sendMessage(message);
110 std::string cmd = server.popCommandWait();
111 assertTrue (cmd == "MAIL FROM: <john.doe@no.where>");
112 cmd = server.popCommandWait();
113 assertTrue (cmd == "RCPT TO: <jane.doe@no.where>");
114 cmd = server.popCommandWait();
115 assertTrue (cmd == "DATA");
116 cmd = server.popCommandWait();
117 assertTrue (cmd.substr(0, 4) == "Date");
118 cmd = server.popCommandWait();
119 assertTrue (cmd == "Content-Type: text/plain");
120 cmd = server.popCommandWait();
121 assertTrue (cmd == "From: john.doe@no.where");
122 cmd = server.popCommandWait();
123 assertTrue (cmd == "Subject: Test Message");
124 cmd = server.popCommandWait();
125 assertTrue (cmd == "Content-Transfer-Encoding: quoted-printable");
126 cmd = server.popCommandWait();
127 assertTrue (cmd == "To: Jane Doe <jane.doe@no.where>");
128 cmd = server.popCommandWait();
129 assertTrue (cmd == "Hello");
130 cmd = server.popCommandWait();
131 assertTrue (cmd == "blah blah");
132 cmd = server.popCommandWait();
133 assertTrue (cmd == "John");
134 cmd = server.popCommandWait();
135 assertTrue (cmd == ".");
136
137 session.close();
138}
139
140
141void SMTPClientSessionTest::testSendMultiRecipient()
142{
143 DialogServer server;
144 server.addResponse("220 localhost SMTP ready");
145 server.addResponse("250 Hello localhost");
146 server.addResponse("250 OK");
147 server.addResponse("250 OK");
148 server.addResponse("250 OK");
149 server.addResponse("250 OK");
150 server.addResponse("354 Send data");
151 server.addResponse("250 OK");
152 server.addResponse("250 OK");
153 server.addResponse("221 Bye");
154 SMTPClientSession session("127.0.0.1", server.port());
155 session.login("localhost");
156
157 MailMessage message;
158 message.setSender("john.doe@no.where");
159 MailMessage::Recipients msgRecipients;
160 msgRecipients.push_back(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, "jane.doe@no.where", "Jane Doe"));
161 msgRecipients.push_back(MailRecipient(MailRecipient::CC_RECIPIENT, "jack.doe@no.where", "Jack Doe"));
162 msgRecipients.push_back(MailRecipient(MailRecipient::BCC_RECIPIENT, "joe.doe@no.where", "Joe Doe"));
163 message.setRecipients(msgRecipients);
164 message.setSubject("Test Message");
165 message.setContent("Hello\r\nblah blah\r\n\r\nJohn\r\n");
166
167 server.clearCommands();
168 session.sendMessage(message);
169 std::string cmd = server.popCommandWait();
170 assertTrue (cmd == "MAIL FROM: <john.doe@no.where>");
171 cmd = server.popCommandWait();
172 assertTrue (cmd == "RCPT TO: <jane.doe@no.where>");
173 cmd = server.popCommandWait();
174 assertTrue (cmd == "RCPT TO: <jack.doe@no.where>");
175 cmd = server.popCommandWait();
176 assertTrue (cmd == "RCPT TO: <joe.doe@no.where>");
177 cmd = server.popCommandWait();
178 assertTrue (cmd == "DATA");
179 cmd = server.popCommandWait();
180 assertTrue (cmd.substr(0, 4) == "Date");
181 cmd = server.popCommandWait();
182 assertTrue (cmd == "Content-Type: text/plain");
183 cmd = server.popCommandWait();
184 assertTrue (cmd == "From: john.doe@no.where");
185 cmd = server.popCommandWait();
186 assertTrue (cmd == "Subject: Test Message");
187 cmd = server.popCommandWait();
188 assertTrue (cmd == "Content-Transfer-Encoding: quoted-printable");
189 cmd = server.popCommandWait();
190 assertTrue (cmd == "To: Jane Doe <jane.doe@no.where>");
191 cmd = server.popCommandWait();
192 assertTrue (cmd == "CC: Jack Doe <jack.doe@no.where>");
193 cmd = server.popCommandWait();
194 assertTrue (cmd == "Hello");
195 cmd = server.popCommandWait();
196 assertTrue (cmd == "blah blah");
197 cmd = server.popCommandWait();
198 assertTrue (cmd == "John");
199 cmd = server.popCommandWait();
200 assertTrue (cmd == ".");
201
202 session.close();
203}
204
205
206void SMTPClientSessionTest::testMultiSeparateRecipient()
207{
208 DialogServer server;
209 server.addResponse("220 localhost SMTP ready");
210 server.addResponse("250 Hello localhost");
211 server.addResponse("250 OK");
212 server.addResponse("250 OK");
213 server.addResponse("250 OK");
214 server.addResponse("250 OK");
215 server.addResponse("354 Send data");
216 server.addResponse("250 OK");
217 server.addResponse("250 OK");
218 server.addResponse("221 Bye");
219 SMTPClientSession session("127.0.0.1", server.port());
220 session.login("localhost");
221
222 MailMessage message;
223 message.setSender("john.doe@no.where");
224 MailMessage::Recipients msgRecipients;
225 msgRecipients.push_back(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, "jane.doe@no.where", "Jane Doe"));
226 msgRecipients.push_back(MailRecipient(MailRecipient::CC_RECIPIENT, "jack.doe@no.where", "Jack Doe"));
227 msgRecipients.push_back(MailRecipient(MailRecipient::CC_RECIPIENT, "joe.doe@no.where", "Joe Doe"));
228 message.setRecipients(msgRecipients);
229 message.setSubject("Test Message");
230 message.setContent("Hello\r\nblah blah\r\n\r\nJohn\r\n");
231
232 SMTPClientSession::Recipients recipients;
233 recipients.push_back("jill.doe@no.where");
234 recipients.push_back("josh.doe@no.where");
235 recipients.push_back("jake.doe@no.where");
236
237 server.clearCommands();
238 session.sendMessage(message, recipients);
239 std::string cmd = server.popCommandWait();
240 assertTrue (cmd == "MAIL FROM: <john.doe@no.where>");
241 cmd = server.popCommandWait();
242 assertTrue (cmd == "RCPT TO: <jill.doe@no.where>");
243 cmd = server.popCommandWait();
244 assertTrue (cmd == "RCPT TO: <josh.doe@no.where>");
245 cmd = server.popCommandWait();
246 assertTrue (cmd == "RCPT TO: <jake.doe@no.where>");
247 cmd = server.popCommandWait();
248 assertTrue (cmd == "DATA");
249 cmd = server.popCommandWait();
250 assertTrue (cmd.substr(0, 4) == "Date");
251 cmd = server.popCommandWait();
252 assertTrue (cmd == "Content-Type: text/plain");
253 cmd = server.popCommandWait();
254 assertTrue (cmd == "From: john.doe@no.where");
255 cmd = server.popCommandWait();
256 assertTrue (cmd == "Subject: Test Message");
257 cmd = server.popCommandWait();
258 assertTrue (cmd == "Content-Transfer-Encoding: quoted-printable");
259 cmd = server.popCommandWait();
260 assertTrue (cmd == "To: Jane Doe <jane.doe@no.where>");
261 cmd = server.popCommandWait();
262 assertTrue (cmd == "CC: Jack Doe <jack.doe@no.where>, Joe Doe <joe.doe@no.where>");
263 cmd = server.popCommandWait();
264 assertTrue (cmd == "Hello");
265 cmd = server.popCommandWait();
266 assertTrue (cmd == "blah blah");
267 cmd = server.popCommandWait();
268 assertTrue (cmd == "John");
269 cmd = server.popCommandWait();
270 assertTrue (cmd == ".");
271
272 session.close();
273}
274
275
276void SMTPClientSessionTest::testSendFailed()
277{
278 DialogServer server;
279 server.addResponse("220 localhost SMTP ready");
280 server.addResponse("250 Hello localhost");
281 server.addResponse("250 OK");
282 server.addResponse("250 OK");
283 server.addResponse("354 Send data");
284 server.addResponse("500 Error");
285 server.addResponse("221 Bye");
286 SMTPClientSession session("127.0.0.1", server.port());
287 session.login("localhost");
288
289 MailMessage message;
290 message.setSender("john.doe@no.where");
291 message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, "jane.doe@no.where", "Jane Doe"));
292 message.setSubject("Test Message");
293 message.setContent("Hello\r\nblah blah\r\n\r\nJohn\r\n");
294 server.clearCommands();
295 try
296 {
297 session.sendMessage(message);
298 fail("internal error - must throw");
299 }
300 catch (SMTPException&)
301 {
302 }
303
304 session.close();
305}
306
307
308void SMTPClientSessionTest::setUp()
309{
310}
311
312
313void SMTPClientSessionTest::tearDown()
314{
315}
316
317
318CppUnit::Test* SMTPClientSessionTest::suite()
319{
320 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SMTPClientSessionTest");
321
322 CppUnit_addTest(pSuite, SMTPClientSessionTest, testLoginEHLO);
323 CppUnit_addTest(pSuite, SMTPClientSessionTest, testLoginHELO);
324 CppUnit_addTest(pSuite, SMTPClientSessionTest, testLoginFailed);
325 CppUnit_addTest(pSuite, SMTPClientSessionTest, testSend);
326 CppUnit_addTest(pSuite, SMTPClientSessionTest, testSendMultiRecipient);
327 CppUnit_addTest(pSuite, SMTPClientSessionTest, testMultiSeparateRecipient);
328 CppUnit_addTest(pSuite, SMTPClientSessionTest, testSendFailed);
329
330 return pSuite;
331}
332