1 | // |
2 | // POP3ClientSessionTest.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 "POP3ClientSessionTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "DialogServer.h" |
15 | #include "Poco/Net/POP3ClientSession.h" |
16 | #include "Poco/Net/MailMessage.h" |
17 | #include "Poco/Net/MailRecipient.h" |
18 | #include "Poco/Net/NetException.h" |
19 | |
20 | |
21 | using Poco::Net::POP3ClientSession; |
22 | using Poco::Net::MessageHeader; |
23 | using Poco::Net::MailMessage; |
24 | using Poco::Net::MailRecipient; |
25 | using Poco::Net::POP3Exception; |
26 | |
27 | |
28 | POP3ClientSessionTest::POP3ClientSessionTest(const std::string& name): CppUnit::TestCase(name) |
29 | { |
30 | } |
31 | |
32 | |
33 | POP3ClientSessionTest::~POP3ClientSessionTest() |
34 | { |
35 | } |
36 | |
37 | |
38 | void POP3ClientSessionTest::testLogin() |
39 | { |
40 | DialogServer server; |
41 | server.addResponse("+OK POP3 Ready..." ); |
42 | server.addResponse("+OK USER" ); |
43 | server.addResponse("+OK PASS" ); |
44 | server.addResponse("+OK QUIT" ); |
45 | POP3ClientSession session("127.0.0.1" , server.port()); |
46 | session.login("user" , "secret" ); |
47 | std::string cmd = server.popCommand(); |
48 | assertTrue (cmd == "USER user" ); |
49 | cmd = server.popCommand(); |
50 | assertTrue (cmd == "PASS secret" ); |
51 | session.close(); |
52 | cmd = server.popCommand(); |
53 | assertTrue (cmd == "QUIT" ); |
54 | } |
55 | |
56 | |
57 | void POP3ClientSessionTest::testLoginFail() |
58 | { |
59 | DialogServer server; |
60 | server.addResponse("+OK POP3 Ready..." ); |
61 | server.addResponse("+OK USER" ); |
62 | server.addResponse("-ERR PASS" ); |
63 | server.addResponse("+OK QUIT" ); |
64 | POP3ClientSession session("127.0.0.1" , server.port()); |
65 | try |
66 | { |
67 | session.login("user" , "secret" ); |
68 | fail("login failed - must throw" ); |
69 | } |
70 | catch (POP3Exception&) |
71 | { |
72 | } |
73 | session.close(); |
74 | } |
75 | |
76 | |
77 | void POP3ClientSessionTest::testMessageCount() |
78 | { |
79 | DialogServer server; |
80 | server.addResponse("+OK POP3 Ready..." ); |
81 | server.addResponse("+OK USER" ); |
82 | server.addResponse("+OK PASS" ); |
83 | server.addResponse("+OK 42 12345" ); |
84 | server.addResponse("+OK QUIT" ); |
85 | POP3ClientSession session("127.0.0.1" , server.port()); |
86 | session.login("user" , "secret" ); |
87 | server.clearCommands(); |
88 | int n = session.messageCount(); |
89 | std::string cmd = server.popCommand(); |
90 | assertTrue (cmd == "STAT" ); |
91 | assertTrue (n == 42); |
92 | session.close(); |
93 | } |
94 | |
95 | |
96 | void POP3ClientSessionTest::testList() |
97 | { |
98 | DialogServer server; |
99 | server.addResponse("+OK POP3 Ready..." ); |
100 | server.addResponse("+OK USER" ); |
101 | server.addResponse("+OK PASS" ); |
102 | server.addResponse( |
103 | "+OK Here comes da list\r\n" |
104 | "1 1234\r\n" |
105 | "2 5678\r\n" |
106 | "3 987\r\n" |
107 | ".\r\n" |
108 | ); |
109 | server.addResponse("+OK QUIT" ); |
110 | POP3ClientSession session("127.0.0.1" , server.port()); |
111 | session.login("user" , "secret" ); |
112 | server.clearCommands(); |
113 | std::vector<POP3ClientSession::MessageInfo> infos; |
114 | session.listMessages(infos); |
115 | std::string cmd = server.popCommand(); |
116 | assertTrue (cmd == "LIST" ); |
117 | assertTrue (infos.size() == 3); |
118 | assertTrue (infos[0].id == 1); |
119 | assertTrue (infos[0].size == 1234); |
120 | assertTrue (infos[1].id == 2); |
121 | assertTrue (infos[1].size == 5678); |
122 | assertTrue (infos[2].id == 3); |
123 | assertTrue (infos[2].size == 987); |
124 | session.close(); |
125 | } |
126 | |
127 | |
128 | void POP3ClientSessionTest::testRetrieveMessage() |
129 | { |
130 | DialogServer server; |
131 | server.addResponse("+OK POP3 Ready..." ); |
132 | server.addResponse("+OK USER" ); |
133 | server.addResponse("+OK PASS" ); |
134 | server.addResponse( |
135 | "+OK Here comes the message\r\n" |
136 | "From: john.doe@no.where\r\n" |
137 | "To: jane.doe@no.where\r\n" |
138 | "Subject: test\r\n" |
139 | "\r\n" |
140 | "Hello Jane,\r\n" |
141 | "\r\n" |
142 | "blah blah blah...\r\n" |
143 | "....\r\n" |
144 | "\r\n" |
145 | "Yours, John\r\n" |
146 | ".\r\n" |
147 | ); |
148 | server.addResponse("+OK QUIT" ); |
149 | POP3ClientSession session("127.0.0.1" , server.port()); |
150 | session.login("user" , "secret" ); |
151 | server.clearCommands(); |
152 | MailMessage message; |
153 | session.retrieveMessage(1, message); |
154 | std::string cmd = server.popCommand(); |
155 | assertTrue (cmd == "RETR 1" ); |
156 | |
157 | assertTrue (message.getContent() == |
158 | "Hello Jane,\r\n" |
159 | "\r\n" |
160 | "blah blah blah...\r\n" |
161 | "...\r\n" |
162 | "\r\n" |
163 | "Yours, John\r\n" |
164 | ); |
165 | |
166 | session.close(); |
167 | } |
168 | |
169 | |
170 | void POP3ClientSessionTest::() |
171 | { |
172 | DialogServer server; |
173 | server.addResponse("+OK POP3 Ready..." ); |
174 | server.addResponse("+OK USER" ); |
175 | server.addResponse("+OK PASS" ); |
176 | server.addResponse( |
177 | "+OK Here comes the message\r\n" |
178 | "From: john.doe@no.where\r\n" |
179 | "To: jane.doe@no.where\r\n" |
180 | "Subject: test\r\n" |
181 | "\r\n" |
182 | "." |
183 | ); |
184 | server.addResponse("+OK QUIT" ); |
185 | POP3ClientSession session("127.0.0.1" , server.port()); |
186 | session.login("user" , "secret" ); |
187 | server.clearCommands(); |
188 | MessageHeader ; |
189 | session.retrieveHeader(1, header); |
190 | std::string cmd = server.popCommand(); |
191 | assertTrue (cmd == "TOP 1 0" ); |
192 | assertTrue (header.get("From" ) == "john.doe@no.where" ); |
193 | assertTrue (header.get("To" ) == "jane.doe@no.where" ); |
194 | assertTrue (header.get("Subject" ) == "test" ); |
195 | session.close(); |
196 | } |
197 | |
198 | |
199 | void POP3ClientSessionTest::testRetrieveMessages() |
200 | { |
201 | DialogServer server; |
202 | server.addResponse("+OK POP3 Ready..." ); |
203 | server.addResponse("+OK USER" ); |
204 | server.addResponse("+OK PASS" ); |
205 | server.addResponse( |
206 | "+OK Here comes the message\r\n" |
207 | "From: john.doe@no.where\r\n" |
208 | "To: \"Jane Doe\" <jane.doe@no.where>, walter.foo@no.where\r\n" |
209 | "Subject: test\r\n" |
210 | "\r\n" |
211 | "." |
212 | ); |
213 | server.addResponse( |
214 | "+OK Here comes the message\r\n" |
215 | "From: john.doe@no.where\r\n" |
216 | "To: \"Jane Doe\" <jane.doe@no.where>, walter.foo@no.where\r\n" |
217 | "CC: \"Homer Simpson\" <homer.simpson@no.where>, bart.simpson@no.where\r\n" |
218 | "BCC: lisa.simpson@no.where, Maggie Simpson <maggie.simpson@no.where>\r\n" // tolerate non-quoted real name |
219 | "Subject: test\r\n" |
220 | "\r\n" |
221 | "Hello Jane,\r\n" |
222 | "\r\n" |
223 | "blah blah blah...\r\n" |
224 | "....\r\n" |
225 | "\r\n" |
226 | "Yours, John\r\n" |
227 | "." |
228 | ); |
229 | server.addResponse("+OK QUIT" ); |
230 | POP3ClientSession session("127.0.0.1" , server.port()); |
231 | session.login("user" , "secret" ); |
232 | server.clearCommands(); |
233 | MessageHeader ; |
234 | session.retrieveHeader(1, header); |
235 | std::string cmd = server.popCommand(); |
236 | assertTrue (cmd == "TOP 1 0" ); |
237 | assertTrue (header.get("From" ) == "john.doe@no.where" ); |
238 | assertTrue (header.get("To" ) == "\"Jane Doe\" <jane.doe@no.where>, walter.foo@no.where" ); |
239 | assertTrue (header.get("Subject" ) == "test" ); |
240 | |
241 | MailMessage message; |
242 | session.retrieveMessage(2, message); |
243 | cmd = server.popCommand(); |
244 | assertTrue (cmd == "RETR 2" ); |
245 | MailMessage::Recipients recipients = message.recipients(); |
246 | assertTrue (recipients.size() == 6); |
247 | assertTrue (recipients[0].getAddress() == "jane.doe@no.where" ); |
248 | assertTrue (recipients[0].getRealName() == "Jane Doe" ); |
249 | assertTrue (recipients[0].getType() == MailRecipient::PRIMARY_RECIPIENT); |
250 | assertTrue (recipients[1].getAddress() == "walter.foo@no.where" ); |
251 | assertTrue (recipients[1].getRealName().empty()); |
252 | assertTrue (recipients[1].getType() == MailRecipient::PRIMARY_RECIPIENT); |
253 | assertTrue (recipients[2].getAddress() == "homer.simpson@no.where" ); |
254 | assertTrue (recipients[2].getRealName() == "Homer Simpson" ); |
255 | assertTrue (recipients[2].getType() == MailRecipient::CC_RECIPIENT); |
256 | assertTrue (recipients[3].getAddress() == "bart.simpson@no.where" ); |
257 | assertTrue (recipients[3].getRealName().empty()); |
258 | assertTrue (recipients[3].getType() == MailRecipient::CC_RECIPIENT); |
259 | assertTrue (recipients[4].getAddress() == "lisa.simpson@no.where" ); |
260 | assertTrue (recipients[4].getRealName().empty()); |
261 | assertTrue (recipients[4].getType() == MailRecipient::BCC_RECIPIENT); |
262 | assertTrue (recipients[5].getAddress() == "maggie.simpson@no.where" ); |
263 | assertTrue (recipients[5].getRealName() == "Maggie Simpson" ); |
264 | assertTrue (recipients[5].getType() == MailRecipient::BCC_RECIPIENT); |
265 | |
266 | assertTrue (message.getContent() == |
267 | "Hello Jane,\r\n" |
268 | "\r\n" |
269 | "blah blah blah...\r\n" |
270 | "...\r\n" |
271 | "\r\n" |
272 | "Yours, John\r\n" |
273 | ); |
274 | session.close(); |
275 | } |
276 | |
277 | |
278 | void POP3ClientSessionTest::testDeleteMessage() |
279 | { |
280 | DialogServer server; |
281 | server.addResponse("+OK POP3 Ready..." ); |
282 | server.addResponse("+OK USER" ); |
283 | server.addResponse("+OK PASS" ); |
284 | server.addResponse("+OK DELETED" ); |
285 | server.addResponse("+OK QUIT" ); |
286 | POP3ClientSession session("127.0.0.1" , server.port()); |
287 | session.login("user" , "secret" ); |
288 | server.clearCommands(); |
289 | session.deleteMessage(42); |
290 | std::string cmd = server.popCommand(); |
291 | assertTrue (cmd == "DELE 42" ); |
292 | session.close(); |
293 | } |
294 | |
295 | |
296 | void POP3ClientSessionTest::setUp() |
297 | { |
298 | } |
299 | |
300 | |
301 | void POP3ClientSessionTest::tearDown() |
302 | { |
303 | } |
304 | |
305 | |
306 | CppUnit::Test* POP3ClientSessionTest::suite() |
307 | { |
308 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("POP3ClientSessionTest" ); |
309 | |
310 | CppUnit_addTest(pSuite, POP3ClientSessionTest, testLogin); |
311 | CppUnit_addTest(pSuite, POP3ClientSessionTest, testLoginFail); |
312 | CppUnit_addTest(pSuite, POP3ClientSessionTest, testMessageCount); |
313 | CppUnit_addTest(pSuite, POP3ClientSessionTest, testList); |
314 | CppUnit_addTest(pSuite, POP3ClientSessionTest, testRetrieveMessage); |
315 | CppUnit_addTest(pSuite, POP3ClientSessionTest, testRetrieveHeader); |
316 | CppUnit_addTest(pSuite, POP3ClientSessionTest, testRetrieveMessages); |
317 | CppUnit_addTest(pSuite, POP3ClientSessionTest, testDeleteMessage); |
318 | |
319 | return pSuite; |
320 | } |
321 | |