Mercurial > thymian
comparison 3rdparty/vmime/tests/parser/attachmentHelperTest.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-2013 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 | |
| 27 VMIME_TEST_SUITE_BEGIN(attachmentHelperTest) | |
| 28 | |
| 29 VMIME_TEST_LIST_BEGIN | |
| 30 VMIME_TEST(testAddAttachment1) | |
| 31 VMIME_TEST(testAddAttachment2) | |
| 32 VMIME_TEST(testAddAttachment3) | |
| 33 VMIME_TEST(testIsBodyPartAnAttachment1) | |
| 34 VMIME_TEST(testIsBodyPartAnAttachment2) | |
| 35 VMIME_TEST(testIsBodyPartAnAttachment3) | |
| 36 VMIME_TEST(testGetBodyPartAttachment) | |
| 37 VMIME_TEST(testAddAttachmentMessage1) | |
| 38 VMIME_TEST(testGetBodyPartAttachmentMessage) | |
| 39 VMIME_TEST_LIST_END | |
| 40 | |
| 41 | |
| 42 static const vmime::string getStructure(vmime::shared_ptr <vmime::bodyPart> part) | |
| 43 { | |
| 44 vmime::shared_ptr <vmime::body> bdy = part->getBody(); | |
| 45 | |
| 46 vmime::string res = part->getBody()->getContentType().generate(); | |
| 47 | |
| 48 if (bdy->getPartCount() == 0) | |
| 49 return res; | |
| 50 | |
| 51 res += "["; | |
| 52 | |
| 53 for (size_t i = 0 ; i < bdy->getPartCount() ; ++i) | |
| 54 { | |
| 55 vmime::shared_ptr <vmime::bodyPart> subPart = bdy->getPartAt(i); | |
| 56 | |
| 57 if (i != 0) | |
| 58 res += ","; | |
| 59 | |
| 60 res += getStructure(subPart); | |
| 61 } | |
| 62 | |
| 63 return res + "]"; | |
| 64 } | |
| 65 | |
| 66 static const vmime::string extractBodyContents(vmime::shared_ptr <const vmime::bodyPart> part) | |
| 67 { | |
| 68 vmime::shared_ptr <const vmime::contentHandler> cth = part->getBody()->getContents(); | |
| 69 | |
| 70 vmime::string data; | |
| 71 vmime::utility::outputStreamStringAdapter os(data); | |
| 72 | |
| 73 cth->extract(os); | |
| 74 | |
| 75 return data; | |
| 76 } | |
| 77 | |
| 78 void testAddAttachment1() | |
| 79 { | |
| 80 vmime::string data = | |
| 81 "Content-Type: text/plain\r\n" | |
| 82 "\r\n" | |
| 83 "The text\r\n" | |
| 84 ""; | |
| 85 | |
| 86 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>(); | |
| 87 msg->parse(data); | |
| 88 | |
| 89 vmime::shared_ptr <vmime::attachment> att = vmime::make_shared <vmime::defaultAttachment> | |
| 90 (vmime::make_shared <vmime::stringContentHandler>("test"), | |
| 91 vmime::mediaType("image/jpeg")); | |
| 92 | |
| 93 vmime::attachmentHelper::addAttachment(msg, att); | |
| 94 | |
| 95 VASSERT_EQ("1", "multipart/mixed[text/plain,image/jpeg]", getStructure(msg)); | |
| 96 VASSERT_EQ("2", "The text\r\n", extractBodyContents(msg->getBody()->getPartAt(0))); | |
| 97 } | |
| 98 | |
| 99 void testAddAttachment2() | |
| 100 { | |
| 101 vmime::string data = | |
| 102 "Content-Type: multipart/mixed; boundary=\"foo\"\r\n" | |
| 103 "\r\n" | |
| 104 "--foo\r\n" | |
| 105 "Content-Type: text/plain\r\n" | |
| 106 "\r\n" | |
| 107 "The text\r\n" | |
| 108 "--foo\r\n" | |
| 109 "Content-Type: application/octet-stream\r\n" | |
| 110 "\r\n" | |
| 111 "Blah\r\n" | |
| 112 "--foo--\r\n" | |
| 113 ""; | |
| 114 | |
| 115 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>(); | |
| 116 msg->parse(data); | |
| 117 | |
| 118 vmime::shared_ptr <vmime::attachment> att = vmime::make_shared <vmime::defaultAttachment> | |
| 119 (vmime::make_shared <vmime::stringContentHandler>("test"), | |
| 120 vmime::mediaType("image/jpeg")); | |
| 121 | |
| 122 vmime::attachmentHelper::addAttachment(msg, att); | |
| 123 | |
| 124 VASSERT_EQ("1", "multipart/mixed[text/plain,application/octet-stream,image/jpeg]", getStructure(msg)); | |
| 125 VASSERT_EQ("2", "The text", extractBodyContents(msg->getBody()->getPartAt(0))); | |
| 126 VASSERT_EQ("3", "Blah", extractBodyContents(msg->getBody()->getPartAt(1))); | |
| 127 VASSERT_EQ("4", "test", extractBodyContents(msg->getBody()->getPartAt(2))); | |
| 128 } | |
| 129 | |
| 130 // Initial part is encoded | |
| 131 void testAddAttachment3() | |
| 132 { | |
| 133 vmime::string data = | |
| 134 "Content-Type: text/plain\r\n" | |
| 135 "Content-Transfer-Encoding: base64\r\n" | |
| 136 "\r\n" | |
| 137 "TWVzc2FnZSBib2R5"; | |
| 138 | |
| 139 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>(); | |
| 140 msg->parse(data); | |
| 141 | |
| 142 vmime::shared_ptr <vmime::attachment> att = vmime::make_shared <vmime::defaultAttachment> | |
| 143 (vmime::make_shared <vmime::stringContentHandler>("test"), | |
| 144 vmime::mediaType("image/jpeg")); | |
| 145 | |
| 146 vmime::attachmentHelper::addAttachment(msg, att); | |
| 147 | |
| 148 VASSERT_EQ("1", "multipart/mixed[text/plain,image/jpeg]", getStructure(msg)); | |
| 149 VASSERT_EQ("2", "Message body", extractBodyContents(msg->getBody()->getPartAt(0))); | |
| 150 } | |
| 151 | |
| 152 // Content-Disposition: attachment | |
| 153 // No other field | |
| 154 void testIsBodyPartAnAttachment1() | |
| 155 { | |
| 156 vmime::string data = "Content-Disposition: attachment\r\n\r\nFoo\r\n"; | |
| 157 | |
| 158 vmime::shared_ptr <vmime::bodyPart> p = vmime::make_shared <vmime::bodyPart>(); | |
| 159 p->parse(data); | |
| 160 | |
| 161 VASSERT_EQ("1", true, vmime::attachmentHelper::isBodyPartAnAttachment(p)); | |
| 162 } | |
| 163 | |
| 164 // No Content-Disposition field | |
| 165 // Content-Type: multipart/* or text/* | |
| 166 void testIsBodyPartAnAttachment2() | |
| 167 { | |
| 168 vmime::string data = "Content-Type: multipart/*\r\n\r\nFoo\r\n"; | |
| 169 | |
| 170 vmime::shared_ptr <vmime::bodyPart> p = vmime::make_shared <vmime::bodyPart>(); | |
| 171 p->parse(data); | |
| 172 | |
| 173 VASSERT_EQ("1", false, vmime::attachmentHelper::isBodyPartAnAttachment(p)); | |
| 174 | |
| 175 data = "Content-Type: text/*\r\n\r\nFoo\r\n"; | |
| 176 | |
| 177 p->parse(data); | |
| 178 | |
| 179 VASSERT_EQ("2", false, vmime::attachmentHelper::isBodyPartAnAttachment(p)); | |
| 180 } | |
| 181 | |
| 182 // No Content-Disposition field | |
| 183 void testIsBodyPartAnAttachment3() | |
| 184 { | |
| 185 vmime::string data = "Content-Type: application/octet-stream\r\n\r\nFoo\r\n"; | |
| 186 | |
| 187 vmime::shared_ptr <vmime::bodyPart> p = vmime::make_shared <vmime::bodyPart>(); | |
| 188 p->parse(data); | |
| 189 | |
| 190 VASSERT_EQ("1", true, vmime::attachmentHelper::isBodyPartAnAttachment(p)); | |
| 191 } | |
| 192 | |
| 193 // Content-Disposition: attachment | |
| 194 // Content-Id field present | |
| 195 void testIsBodyPartAnAttachment4() | |
| 196 { | |
| 197 vmime::string data = "Content-Disposition: attachment\r\n" | |
| 198 "Content-Type: application/octet-stream\r\n" | |
| 199 "Content-Id: bar\r\n" | |
| 200 "\r\nFoo\r\n"; | |
| 201 | |
| 202 vmime::shared_ptr <vmime::bodyPart> p = vmime::make_shared <vmime::bodyPart>(); | |
| 203 p->parse(data); | |
| 204 | |
| 205 VASSERT_EQ("1", false, vmime::attachmentHelper::isBodyPartAnAttachment(p)); | |
| 206 } | |
| 207 | |
| 208 void testGetBodyPartAttachment() | |
| 209 { | |
| 210 vmime::string data = | |
| 211 "Content-Type: image/jpeg\r\n" | |
| 212 "Content-Description: foobar\r\n" | |
| 213 "Content-Transfer-Encoding: x-baz\r\n" | |
| 214 "Content-Disposition: attachment; filename=\"foobar.baz\"\r\n" | |
| 215 "\r\n" | |
| 216 "Foo bar baz"; | |
| 217 | |
| 218 vmime::shared_ptr <vmime::bodyPart> part = vmime::make_shared <vmime::bodyPart>(); | |
| 219 part->parse(data); | |
| 220 | |
| 221 vmime::shared_ptr <const vmime::attachment> att = | |
| 222 vmime::attachmentHelper::getBodyPartAttachment(part); | |
| 223 | |
| 224 VASSERT_EQ("1", "image/jpeg", att->getType().generate()); | |
| 225 VASSERT_EQ("2", "foobar", att->getDescription().generate()); | |
| 226 VASSERT_EQ("3", "x-baz", att->getEncoding().generate()); | |
| 227 VASSERT_EQ("4", "foobar.baz", att->getName().generate()); | |
| 228 | |
| 229 vmime::string attData; | |
| 230 vmime::utility::outputStreamStringAdapter out(attData); | |
| 231 att->getData()->extractRaw(out); // 'x-baz' encoding not supported | |
| 232 | |
| 233 VASSERT_EQ("5", "Foo bar baz", attData); | |
| 234 | |
| 235 //VASSERT_EQ("6", part, att->getPart()); | |
| 236 VASSERT_EQ("6", part->generate(), vmime::dynamicCast <const vmime::component>(att->getPart())->generate()); | |
| 237 //VASSERT_EQ("7", part->getHeader(), att->getHeader()); | |
| 238 VASSERT_EQ("7", part->getHeader()->generate(), att->getHeader()->generate()); | |
| 239 } | |
| 240 | |
| 241 void testAddAttachmentMessage1() | |
| 242 { | |
| 243 const vmime::string data = | |
| 244 "Subject: Test message\r\n" | |
| 245 "Content-Type: text/plain\r\n" | |
| 246 "\r\n" | |
| 247 "Message body"; | |
| 248 | |
| 249 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>(); | |
| 250 msg->parse(data); | |
| 251 | |
| 252 const vmime::string attData = | |
| 253 "Subject: Attached message\r\n" | |
| 254 "Content-Type: text/plain\r\n" | |
| 255 "Content-Transfer-Encoding: base64\r\n" | |
| 256 "\r\n" | |
| 257 "QXR0YWNoZWQgbWVzc2FnZSBib2R5"; | |
| 258 | |
| 259 vmime::shared_ptr <vmime::message> amsg = vmime::make_shared <vmime::message>(); | |
| 260 amsg->parse(attData); | |
| 261 | |
| 262 vmime::attachmentHelper::addAttachment(msg, amsg); | |
| 263 | |
| 264 VASSERT_EQ("1", "multipart/mixed[text/plain,message/rfc822]", getStructure(msg)); | |
| 265 VASSERT_EQ("2", "Message body", extractBodyContents(msg->getBody()->getPartAt(0))); | |
| 266 | |
| 267 // Ensure message has been encoded properly | |
| 268 vmime::shared_ptr <const vmime::bodyPart> attPart = msg->getBody()->getPartAt(1); | |
| 269 vmime::shared_ptr <const vmime::contentHandler> attCth = attPart->getBody()->getContents(); | |
| 270 | |
| 271 vmime::string attDataOut; | |
| 272 vmime::utility::outputStreamStringAdapter attDataOutOs(attDataOut); | |
| 273 | |
| 274 attCth->extract(attDataOutOs); | |
| 275 | |
| 276 vmime::shared_ptr <vmime::message> amsgOut = vmime::make_shared <vmime::message>(); | |
| 277 amsgOut->parse(attDataOut); | |
| 278 | |
| 279 vmime::shared_ptr <vmime::header> hdr = amsgOut->getHeader(); | |
| 280 | |
| 281 VASSERT_EQ("3", "Attached message", hdr->Subject()->getValue <vmime::text>()->generate()); | |
| 282 VASSERT_EQ("4", "Attached message body", extractBodyContents(amsgOut)); | |
| 283 } | |
| 284 | |
| 285 void testGetBodyPartAttachmentMessage() | |
| 286 { | |
| 287 const vmime::string data = | |
| 288 "Subject: Test message\r\n" | |
| 289 "Content-Type: multipart/mixed; boundary=\"foo\"\r\n" | |
| 290 "\r\n" | |
| 291 "--foo\r\n" | |
| 292 "Content-Type: message/rfc822\r\n" | |
| 293 "\r\n" | |
| 294 "Subject: Attached message\r\n" | |
| 295 "\r\n" | |
| 296 "Attached message body\r\n" | |
| 297 "--foo\r\n" | |
| 298 "Content-Type: text/plain\r\n" | |
| 299 "\r\n" | |
| 300 "FooBar\r\n" | |
| 301 "--foo--\r\n"; | |
| 302 | |
| 303 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>(); | |
| 304 msg->parse(data); | |
| 305 | |
| 306 VASSERT_EQ("0", 2, msg->getBody()->getPartCount()); | |
| 307 | |
| 308 vmime::shared_ptr <const vmime::attachment> att = vmime::attachmentHelper:: | |
| 309 getBodyPartAttachment(msg->getBody()->getPartAt(0)); | |
| 310 | |
| 311 VASSERT("1", att != NULL); | |
| 312 | |
| 313 vmime::shared_ptr <const vmime::messageAttachment> msgAtt = | |
| 314 vmime::dynamicCast <const vmime::messageAttachment>(att); | |
| 315 | |
| 316 VASSERT("2", msgAtt != NULL); | |
| 317 | |
| 318 vmime::shared_ptr <vmime::message> amsg = msgAtt->getMessage(); | |
| 319 vmime::shared_ptr <vmime::header> hdr = amsg->getHeader(); | |
| 320 | |
| 321 VASSERT_EQ("3", "Attached message", hdr->Subject()->getValue <vmime::text>()->generate()); | |
| 322 VASSERT_EQ("4", "Attached message body", extractBodyContents(amsg)); | |
| 323 } | |
| 324 | |
| 325 VMIME_TEST_SUITE_END | |
| 326 |
