comparison 3rdparty/vmime/tests/net/imap/IMAPUtilsTest.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a4671277546c
1 //
2 // VMime library (http://www.vmime.org)
3 // Copyright (C) 2002-2014 Vincent Richard <vincent@vmime.org>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3 of
8 // the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // Linking this library statically or dynamically with other modules is making
20 // a combined work based on this library. Thus, the terms and conditions of
21 // the GNU General Public License cover the whole combination.
22 //
23
24 #include "tests/testUtils.hpp"
25
26 #include "vmime/net/imap/IMAPUtils.hpp"
27 #include "vmime/net/imap/IMAPParser.hpp"
28
29
30 using namespace vmime::net::imap;
31
32
33
34 VMIME_TEST_SUITE_BEGIN(IMAPUtilsTest)
35
36 VMIME_TEST_LIST_BEGIN
37 VMIME_TEST(testQuoteString)
38 VMIME_TEST(testToModifiedUTF7)
39 VMIME_TEST(testFromModifiedUTF7)
40 VMIME_TEST(testConvertAddressList)
41 VMIME_TEST(testMessageFlagList)
42 VMIME_TEST(testDateTime)
43 VMIME_TEST(testPathToString)
44 VMIME_TEST(testStringToPath)
45 VMIME_TEST(testBuildFetchCommand)
46 VMIME_TEST_LIST_END
47
48
49 void testQuoteString()
50 {
51 VASSERT_EQ("unquoted", "ascii", IMAPUtils::quoteString("ascii"));
52 VASSERT_EQ("space", "\"ascii with space\"", IMAPUtils::quoteString("ascii with space"));
53
54 VASSERT_EQ("special1", "\"(\"", IMAPUtils::quoteString("("));
55 VASSERT_EQ("special2", "\")\"", IMAPUtils::quoteString(")"));
56 VASSERT_EQ("special3", "\"{\"", IMAPUtils::quoteString("{"));
57 VASSERT_EQ("special4", "\" \"", IMAPUtils::quoteString(" "));
58 VASSERT_EQ("special5", "\"%\"", IMAPUtils::quoteString("%"));
59 VASSERT_EQ("special6", "\"*\"", IMAPUtils::quoteString("*"));
60 VASSERT_EQ("special7", "\"\\\"\"", IMAPUtils::quoteString("\""));
61 VASSERT_EQ("special8", "\"\\\\\"", IMAPUtils::quoteString("\\"));
62 VASSERT_EQ("special9", "\"\x7f\"", IMAPUtils::quoteString("\x7f"));
63
64 }
65
66 void testToModifiedUTF7()
67 {
68 #define FC(x) vmime::net::folder::path::component(x, vmime::charsets::UTF_8)
69
70 // Example strings from RFC-1642 (modified for IMAP UTF-7)
71 VASSERT_EQ("1", "A&ImIDkQ-.", IMAPUtils::toModifiedUTF7('/', FC("A\xe2\x89\xa2\xce\x91.")));
72 VASSERT_EQ("2", "Hi Mum &Jjo-!", IMAPUtils::toModifiedUTF7('/', FC("Hi Mum \xe2\x98\xba!")));
73 VASSERT_EQ("3", "&ZeVnLIqe-", IMAPUtils::toModifiedUTF7('/', FC("\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e")));
74 VASSERT_EQ("4", "Item 3 is &AKM- 1.", IMAPUtils::toModifiedUTF7('/', FC("Item 3 is \xc2\xa3 1.")));
75
76 VASSERT_EQ("escape char", "&-", IMAPUtils::toModifiedUTF7('/', FC("&")));
77 VASSERT_EQ("ascii", "plain ascii text", IMAPUtils::toModifiedUTF7('/', FC("plain ascii text")));
78 VASSERT_EQ("special", "!\"#$%*+-;<=>@[]^_`{|}", IMAPUtils::toModifiedUTF7('/', FC("!\"#$%*+-;<=>@[]^_`{|}")));
79
80 #undef FC
81 }
82
83 void testFromModifiedUTF7()
84 {
85 #define FC(x) vmime::net::folder::path::component(x, vmime::charsets::UTF_8)
86
87 // Example strings from RFC-1642 (modified for IMAP UTF-7)
88 VASSERT_EQ("1", FC("A\xe2\x89\xa2\xce\x91."), IMAPUtils::fromModifiedUTF7("A&ImIDkQ-."));
89 VASSERT_EQ("2", FC("Hi Mum \xe2\x98\xba!"), IMAPUtils::fromModifiedUTF7("Hi Mum &Jjo-!"));
90 VASSERT_EQ("3", FC("\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"), IMAPUtils::fromModifiedUTF7("&ZeVnLIqe-"));
91 VASSERT_EQ("4", FC("Item 3 is \xc2\xa3 1."), IMAPUtils::fromModifiedUTF7("Item 3 is &AKM- 1."));
92
93 VASSERT_EQ("escape char", FC("&"), IMAPUtils::fromModifiedUTF7("&-"));
94 VASSERT_EQ("ascii", FC("plain ascii text"), IMAPUtils::fromModifiedUTF7("plain ascii text"));
95 VASSERT_EQ("special", FC("!\"#$%*+;<=>@[]^_`{|}"), IMAPUtils::fromModifiedUTF7("!\"#$%*+-;<=>@[]^_`{|}"));
96
97 #undef FC
98 }
99
100 void testConvertAddressList()
101 {
102 IMAPParser parser;
103 IMAPParser::address_list addrList;
104
105 vmime::string line("((\"name\" NIL \"mailbox\" \"host\")(\"name2\" NIL \"mailbox2\" \"host2\"))");
106 vmime::size_t pos = 0;
107
108 VASSERT("parse", addrList.parseImpl(parser, line, &pos));
109
110 vmime::mailboxList mboxList;
111 IMAPUtils::convertAddressList(addrList, mboxList);
112
113 VASSERT_EQ("mbox-count", 2, mboxList.getMailboxCount());
114 VASSERT_EQ("mbox-1", "mailbox@host", mboxList.getMailboxAt(0)->getEmail().toString());
115 VASSERT_EQ("mbox-1", "name", mboxList.getMailboxAt(0)->getName().getWholeBuffer());
116 VASSERT_EQ("mbox-2", "mailbox2@host2", mboxList.getMailboxAt(1)->getEmail().toString());
117 VASSERT_EQ("mbox-2", "name2", mboxList.getMailboxAt(1)->getName().getWholeBuffer());
118 }
119
120 void testMessageFlagList()
121 {
122 int flags = 0;
123 std::vector <vmime::string> flagList;
124
125 // Test each flag
126 flags = vmime::net::message::FLAG_REPLIED;
127 flagList = IMAPUtils::messageFlagList(flags);
128 VASSERT("replied", std::find(flagList.begin(), flagList.end(), "\\Answered") != flagList.end());
129
130 flags = vmime::net::message::FLAG_MARKED;
131 flagList = IMAPUtils::messageFlagList(flags);
132 VASSERT("marked", std::find(flagList.begin(), flagList.end(), "\\Flagged") != flagList.end());
133
134 flags = vmime::net::message::FLAG_DELETED;
135 flagList = IMAPUtils::messageFlagList(flags);
136 VASSERT("deleted", std::find(flagList.begin(), flagList.end(), "\\Deleted") != flagList.end());
137
138 flags = vmime::net::message::FLAG_SEEN;
139 flagList = IMAPUtils::messageFlagList(flags);
140 VASSERT("seen", std::find(flagList.begin(), flagList.end(), "\\Seen") != flagList.end());
141
142 flags = vmime::net::message::FLAG_DRAFT;
143 flagList = IMAPUtils::messageFlagList(flags);
144 VASSERT("draft", std::find(flagList.begin(), flagList.end(), "\\Draft") != flagList.end());
145
146 // Multiple flags
147 flags = vmime::net::message::FLAG_REPLIED;
148 flagList = IMAPUtils::messageFlagList(flags);
149
150 VASSERT_EQ("1.size", 1, flagList.size());
151 VASSERT("1.found", std::find(flagList.begin(), flagList.end(), "\\Answered") != flagList.end());
152
153 flags |= vmime::net::message::FLAG_SEEN;
154 flagList = IMAPUtils::messageFlagList(flags);
155
156 VASSERT_EQ("2.size", 2, flagList.size());
157 VASSERT("2.found1", std::find(flagList.begin(), flagList.end(), "\\Answered") != flagList.end());
158 VASSERT("2.found2", std::find(flagList.begin(), flagList.end(), "\\Seen") != flagList.end());
159 }
160
161 void testDateTime()
162 {
163 vmime::datetime dt(2014, 3, 17, 23, 26, 22, vmime::datetime::GMT2);
164 VASSERT_EQ("datetime", "\"17-Mar-2014 23:26:22 +0200\"", IMAPUtils::dateTime(dt));
165 }
166
167 void testPathToString()
168 {
169 #define FC(x) vmime::net::folder::path::component(x, vmime::charsets::UTF_8)
170
171 vmime::net::folder::path path;
172 path /= FC("Hi Mum \xe2\x98\xba!");
173 path /= FC("\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e");
174
175 VASSERT_EQ("string", "Hi Mum &Jjo-!/&ZeVnLIqe-", IMAPUtils::pathToString('/', path));
176
177 #undef FC
178 }
179
180 void testStringToPath()
181 {
182 #define FC(x) vmime::net::folder::path::component(x, vmime::charsets::UTF_8)
183
184 vmime::net::folder::path path = IMAPUtils::stringToPath('/', "Hi Mum &Jjo-!/&ZeVnLIqe-");
185
186 VASSERT_EQ("count", 2, path.getSize());
187 VASSERT_EQ("component1", FC("Hi Mum \xe2\x98\xba!"), path[0]);
188 VASSERT_EQ("component2", FC("\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"), path[1]);
189
190 #undef FC
191 }
192
193 void testBuildFetchCommand()
194 {
195 vmime::shared_ptr <IMAPConnection> cnt;
196 vmime::net::messageSet msgs = vmime::net::messageSet::byNumber(42);
197
198 // SIZE
199 {
200 vmime::net::fetchAttributes attribs = vmime::net::fetchAttributes::SIZE;
201
202 vmime::shared_ptr <IMAPCommand> cmd = IMAPUtils::buildFetchCommand(cnt, msgs, attribs);
203 VASSERT_EQ("size", "FETCH 42 RFC822.SIZE", cmd->getText());
204 }
205
206 // FLAGS
207 {
208 vmime::net::fetchAttributes attribs = vmime::net::fetchAttributes::FLAGS;
209
210 vmime::shared_ptr <IMAPCommand> cmd = IMAPUtils::buildFetchCommand(cnt, msgs, attribs);
211 VASSERT_EQ("flags", "FETCH 42 FLAGS", cmd->getText());
212 }
213
214 // STRUCTURE
215 {
216 vmime::net::fetchAttributes attribs = vmime::net::fetchAttributes::STRUCTURE;
217
218 vmime::shared_ptr <IMAPCommand> cmd = IMAPUtils::buildFetchCommand(cnt, msgs, attribs);
219 VASSERT_EQ("structure", "FETCH 42 BODYSTRUCTURE", cmd->getText());
220 }
221
222 // UID
223 {
224 vmime::net::fetchAttributes attribs = vmime::net::fetchAttributes::UID;
225
226 vmime::shared_ptr <IMAPCommand> cmd = IMAPUtils::buildFetchCommand(cnt, msgs, attribs);
227 VASSERT_EQ("uid", "FETCH 42 UID", cmd->getText());
228 }
229
230 // ENVELOPE
231 {
232 vmime::net::fetchAttributes attribs = vmime::net::fetchAttributes::ENVELOPE;
233
234 vmime::shared_ptr <IMAPCommand> cmd = IMAPUtils::buildFetchCommand(cnt, msgs, attribs);
235 VASSERT_EQ("envelope", "FETCH 42 ENVELOPE", cmd->getText());
236 }
237
238 // CONTENT_INFO
239 {
240 vmime::net::fetchAttributes attribs = vmime::net::fetchAttributes::CONTENT_INFO;
241
242 vmime::shared_ptr <IMAPCommand> cmd = IMAPUtils::buildFetchCommand(cnt, msgs, attribs);
243 VASSERT_EQ("content", "FETCH 42 BODY[HEADER.FIELDS (CONTENT_TYPE)]", cmd->getText());
244 }
245
246 // IMPORTANCE
247 {
248 vmime::net::fetchAttributes attribs = vmime::net::fetchAttributes::IMPORTANCE;
249
250 vmime::shared_ptr <IMAPCommand> cmd = IMAPUtils::buildFetchCommand(cnt, msgs, attribs);
251 VASSERT_EQ("importance", "FETCH 42 BODY[HEADER.FIELDS (IMPORTANCE X-PRIORITY)]", cmd->getText());
252 }
253
254 // Any header attribute + full header should give RFC822.HEADER
255 {
256 vmime::net::fetchAttributes attribs;
257 attribs.add(vmime::net::fetchAttributes::ENVELOPE);
258 attribs.add(vmime::net::fetchAttributes::FULL_HEADER);
259
260 vmime::shared_ptr <IMAPCommand> cmd = IMAPUtils::buildFetchCommand(cnt, msgs, attribs);
261 VASSERT_EQ("full-header", "FETCH 42 RFC822.HEADER", cmd->getText());
262 }
263
264 // Test custom header
265 {
266 vmime::net::fetchAttributes attribs;
267 attribs.add("X-MyHeader");
268
269 vmime::shared_ptr <IMAPCommand> cmd = IMAPUtils::buildFetchCommand(cnt, msgs, attribs);
270 VASSERT_EQ("custom-header", "FETCH 42 BODY[HEADER.FIELDS (x-myheader)]", cmd->getText());
271 }
272
273 // Test multiple flags
274 {
275 vmime::net::fetchAttributes attribs =
276 vmime::net::fetchAttributes::UID | vmime::net::fetchAttributes::FLAGS;
277
278 vmime::shared_ptr <IMAPCommand> cmd = IMAPUtils::buildFetchCommand(cnt, msgs, attribs);
279 VASSERT_EQ("multiple", "FETCH 42 (FLAGS UID)", cmd->getText());
280 }
281 }
282
283 VMIME_TEST_SUITE_END