annotate 3rdparty/vmime/tests/parser/htmlTextPartTest.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
rev   line source
ferencd@0 1 //
ferencd@0 2 // VMime library (http://www.vmime.org)
ferencd@0 3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
ferencd@0 4 //
ferencd@0 5 // This program is free software; you can redistribute it and/or
ferencd@0 6 // modify it under the terms of the GNU General Public License as
ferencd@0 7 // published by the Free Software Foundation; either version 3 of
ferencd@0 8 // the License, or (at your option) any later version.
ferencd@0 9 //
ferencd@0 10 // This program is distributed in the hope that it will be useful,
ferencd@0 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
ferencd@0 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ferencd@0 13 // General Public License for more details.
ferencd@0 14 //
ferencd@0 15 // You should have received a copy of the GNU General Public License along
ferencd@0 16 // with this program; if not, write to the Free Software Foundation, Inc.,
ferencd@0 17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ferencd@0 18 //
ferencd@0 19 // Linking this library statically or dynamically with other modules is making
ferencd@0 20 // a combined work based on this library. Thus, the terms and conditions of
ferencd@0 21 // the GNU General Public License cover the whole combination.
ferencd@0 22 //
ferencd@0 23
ferencd@0 24 #include "tests/testUtils.hpp"
ferencd@0 25 #include "vmime/htmlTextPart.hpp"
ferencd@0 26
ferencd@0 27
ferencd@0 28 VMIME_TEST_SUITE_BEGIN(htmlTextPartTest)
ferencd@0 29
ferencd@0 30 VMIME_TEST_LIST_BEGIN
ferencd@0 31 VMIME_TEST(testParseText)
ferencd@0 32 VMIME_TEST(testParseEmbeddedObjectsCID)
ferencd@0 33 VMIME_TEST(testParseEmbeddedObjectsLocation)
ferencd@0 34 VMIME_TEST_LIST_END
ferencd@0 35
ferencd@0 36
ferencd@0 37 static const vmime::string extractContent
ferencd@0 38 (vmime::shared_ptr <const vmime::contentHandler> cth)
ferencd@0 39 {
ferencd@0 40 std::ostringstream oss;
ferencd@0 41 vmime::utility::outputStreamAdapter osa(oss);
ferencd@0 42
ferencd@0 43 cth->extract(osa);
ferencd@0 44
ferencd@0 45 return oss.str();
ferencd@0 46 }
ferencd@0 47
ferencd@0 48
ferencd@0 49 void testParseText()
ferencd@0 50 {
ferencd@0 51 const vmime::string msgString = ""
ferencd@0 52 "MIME-Version: 1.0\r\n"
ferencd@0 53 "Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n"
ferencd@0 54 "\r\n"
ferencd@0 55 "--LEVEL1\r\n"
ferencd@0 56 "Content-Type: text/plain; charset=\"x-ch1\"\r\n"
ferencd@0 57 "\r\n"
ferencd@0 58 "Plain text part\r\n"
ferencd@0 59 "--LEVEL1\r\n"
ferencd@0 60 "Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n"
ferencd@0 61 "\r\n"
ferencd@0 62 "--LEVEL2\r\n"
ferencd@0 63 "Content-Type: text/html; charset=\"x-ch2\"\r\n"
ferencd@0 64 "\r\n"
ferencd@0 65 "<img src=\"cid:image@test\"/>\r\n"
ferencd@0 66 "--LEVEL2\r\n"
ferencd@0 67 "Content-Type: image/png; name=\"image.png\"\r\n"
ferencd@0 68 "Content-Disposition: inline; filename=\"image.png\"\r\n"
ferencd@0 69 "Content-ID: <image@test>\r\n"
ferencd@0 70 "\r\n"
ferencd@0 71 "Image\r\n"
ferencd@0 72 "--LEVEL2--\r\n"
ferencd@0 73 "\r\n"
ferencd@0 74 "--LEVEL1--\r\n"
ferencd@0 75 "";
ferencd@0 76
ferencd@0 77 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>();
ferencd@0 78 msg->parse(msgString);
ferencd@0 79
ferencd@0 80 // Sanity checks
ferencd@0 81 VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount());
ferencd@0 82 VASSERT_EQ("part-count2", 2, msg->getBody()->getPartAt(1)->getBody()->getPartCount());
ferencd@0 83
ferencd@0 84 vmime::htmlTextPart htmlPart;
ferencd@0 85 htmlPart.parse(msg, msg->getBody()->getPartAt(1),
ferencd@0 86 msg->getBody()->getPartAt(1)->getBody()->getPartAt(0));
ferencd@0 87
ferencd@0 88 VASSERT_EQ("plain", "Plain text part", extractContent(htmlPart.getPlainText()));
ferencd@0 89 VASSERT_EQ("html", "<img src=\"cid:image@test\"/>", extractContent(htmlPart.getText()));
ferencd@0 90
ferencd@0 91 // Should return the charset of the HTML part
ferencd@0 92 VASSERT_EQ("charset", "x-ch2", htmlPart.getCharset().generate());
ferencd@0 93 }
ferencd@0 94
ferencd@0 95 /** Test parsing of embedded objects by CID (Content-Id).
ferencd@0 96 */
ferencd@0 97 void testParseEmbeddedObjectsCID()
ferencd@0 98 {
ferencd@0 99 const vmime::string msgString = ""
ferencd@0 100 "MIME-Version: 1.0\r\n"
ferencd@0 101 "Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n"
ferencd@0 102 "\r\n"
ferencd@0 103 "--LEVEL1\r\n"
ferencd@0 104 "Content-Type: text/plain; charset=\"x-ch1\"\r\n"
ferencd@0 105 "\r\n"
ferencd@0 106 "Plain text part\r\n"
ferencd@0 107 "--LEVEL1\r\n"
ferencd@0 108 "Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n"
ferencd@0 109 "\r\n"
ferencd@0 110 "--LEVEL2\r\n" // one embedded object before...
ferencd@0 111 "Content-Type: image/png; name=\"image1.png\"\r\n"
ferencd@0 112 "Content-Disposition: inline; filename=\"image1.png\"\r\n"
ferencd@0 113 "Content-ID: <image1@test>\r\n"
ferencd@0 114 "\r\n"
ferencd@0 115 "Image1\r\n"
ferencd@0 116 "--LEVEL2\r\n" // ...the actual text part...
ferencd@0 117 "Content-Type: text/html; charset=\"x-ch2\"\r\n"
ferencd@0 118 "\r\n"
ferencd@0 119 "<img src=\"cid:image1@test\"/>\r\n"
ferencd@0 120 "<img src=\"CID:image2@test\"/>\r\n"
ferencd@0 121 "--LEVEL2\r\n" // ...and one after
ferencd@0 122 "Content-Type: image/jpeg; name=\"image2.jpg\"\r\n"
ferencd@0 123 "Content-Disposition: inline; filename=\"image2.jpg\"\r\n"
ferencd@0 124 "Content-ID: <image2@test>\r\n"
ferencd@0 125 "\r\n"
ferencd@0 126 "Image2\r\n"
ferencd@0 127 "--LEVEL2--\r\n"
ferencd@0 128 "\r\n"
ferencd@0 129 "--LEVEL1--\r\n"
ferencd@0 130 "";
ferencd@0 131
ferencd@0 132 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>();
ferencd@0 133 msg->parse(msgString);
ferencd@0 134
ferencd@0 135 // Sanity checks
ferencd@0 136 VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount());
ferencd@0 137 VASSERT_EQ("part-count2", 3, msg->getBody()->getPartAt(1)->getBody()->getPartCount());
ferencd@0 138
ferencd@0 139 vmime::htmlTextPart htmlPart;
ferencd@0 140 htmlPart.parse(msg, msg->getBody()->getPartAt(1),
ferencd@0 141 msg->getBody()->getPartAt(1)->getBody()->getPartAt(1));
ferencd@0 142
ferencd@0 143 // Two embedded objects should be found.
ferencd@0 144 // BUGFIX: "CID:" prefix is not case-sensitive.
ferencd@0 145 VASSERT_EQ("count", 2, htmlPart.getObjectCount());
ferencd@0 146
ferencd@0 147 // Ensure the right objects have been found.
ferencd@0 148 VASSERT_EQ("has-obj1", true, htmlPart.hasObject("image1@test"));
ferencd@0 149 VASSERT_EQ("has-obj2", true, htmlPart.hasObject("image2@test"));
ferencd@0 150
ferencd@0 151 // hasObject() should also work with prefixes
ferencd@0 152 VASSERT_EQ("has-obj1-pre", true, htmlPart.hasObject("CID:image1@test"));
ferencd@0 153 VASSERT_EQ("has-obj2-pre", true, htmlPart.hasObject("cid:image2@test"));
ferencd@0 154
ferencd@0 155 // Check data in objects
ferencd@0 156 vmime::shared_ptr <const vmime::htmlTextPart::embeddedObject> obj;
ferencd@0 157
ferencd@0 158 obj = htmlPart.findObject("image1@test");
ferencd@0 159
ferencd@0 160 VASSERT_EQ("ref-type1", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_ID, obj->getReferenceType());
ferencd@0 161 VASSERT_EQ("id-obj1", "image1@test", obj->getId());
ferencd@0 162 VASSERT_EQ("data-obj1", "Image1", extractContent(obj->getData()));
ferencd@0 163 VASSERT_EQ("type-obj1", "image/png", obj->getType().generate());
ferencd@0 164
ferencd@0 165 obj = htmlPart.findObject("image2@test");
ferencd@0 166
ferencd@0 167 VASSERT_EQ("ref-type2", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_ID, obj->getReferenceType());
ferencd@0 168 VASSERT_EQ("id-obj2", "image2@test", obj->getId());
ferencd@0 169 VASSERT_EQ("data-obj2", "Image2", extractContent(obj->getData()));
ferencd@0 170 VASSERT_EQ("type-obj2", "image/jpeg", obj->getType().generate());
ferencd@0 171 }
ferencd@0 172
ferencd@0 173 /** Test parsing of embedded objects by location.
ferencd@0 174 */
ferencd@0 175 void testParseEmbeddedObjectsLocation()
ferencd@0 176 {
ferencd@0 177 const vmime::string msgString = ""
ferencd@0 178 "MIME-Version: 1.0\r\n"
ferencd@0 179 "Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n"
ferencd@0 180 "\r\n"
ferencd@0 181 "--LEVEL1\r\n"
ferencd@0 182 "Content-Type: text/plain; charset=\"x-ch1\"\r\n"
ferencd@0 183 "\r\n"
ferencd@0 184 "Plain text part\r\n"
ferencd@0 185 "--LEVEL1\r\n"
ferencd@0 186 "Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n"
ferencd@0 187 "\r\n"
ferencd@0 188 "--LEVEL2\r\n"
ferencd@0 189 "Content-Type: image/png; name=\"image1.png\"\r\n"
ferencd@0 190 "Content-Location: http://www.vmime.org/test/image1.png\r\n"
ferencd@0 191 "Content-Disposition: inline; filename=\"image1.png\"\r\n"
ferencd@0 192 "Content-Id: <image1@test>\r\n"
ferencd@0 193 "\r\n"
ferencd@0 194 "Image1\r\n"
ferencd@0 195 "--LEVEL2\r\n"
ferencd@0 196 "Content-Type: text/html; charset=\"x-ch2\"\r\n"
ferencd@0 197 "\r\n"
ferencd@0 198 "<img src=\"http://www.vmime.org/test/image1.png\"/>\r\n"
ferencd@0 199 "--LEVEL2--\r\n"
ferencd@0 200 "\r\n"
ferencd@0 201 "--LEVEL1--\r\n"
ferencd@0 202 "";
ferencd@0 203
ferencd@0 204 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>();
ferencd@0 205 msg->parse(msgString);
ferencd@0 206
ferencd@0 207 // Sanity checks
ferencd@0 208 VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount());
ferencd@0 209 VASSERT_EQ("part-count2", 2, msg->getBody()->getPartAt(1)->getBody()->getPartCount());
ferencd@0 210
ferencd@0 211 vmime::htmlTextPart htmlPart;
ferencd@0 212 htmlPart.parse(msg, msg->getBody()->getPartAt(1),
ferencd@0 213 msg->getBody()->getPartAt(1)->getBody()->getPartAt(1));
ferencd@0 214
ferencd@0 215 // Only one embedded object
ferencd@0 216 VASSERT_EQ("count", 1, htmlPart.getObjectCount());
ferencd@0 217
ferencd@0 218 // Should work only with Content-Location as the Content-Id is
ferencd@0 219 // not referenced in the HTML contents
ferencd@0 220 VASSERT_EQ("has-obj-loc", true, htmlPart.hasObject("http://www.vmime.org/test/image1.png"));
ferencd@0 221 VASSERT_EQ("has-obj-cid", false, htmlPart.hasObject("image1@test"));
ferencd@0 222
ferencd@0 223 // Check data
ferencd@0 224 vmime::shared_ptr <const vmime::htmlTextPart::embeddedObject> obj;
ferencd@0 225
ferencd@0 226 obj = htmlPart.findObject("http://www.vmime.org/test/image1.png");
ferencd@0 227
ferencd@0 228 VASSERT_EQ("ref-type", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_LOCATION, obj->getReferenceType());
ferencd@0 229 VASSERT_EQ("id-obj", "http://www.vmime.org/test/image1.png", obj->getId());
ferencd@0 230 VASSERT_EQ("data-obj", "Image1", extractContent(obj->getData()));
ferencd@0 231 VASSERT_EQ("type-obj", "image/png", obj->getType().generate());
ferencd@0 232 }
ferencd@0 233
ferencd@0 234 // TODO: test generation of text parts
ferencd@0 235
ferencd@0 236 VMIME_TEST_SUITE_END
ferencd@0 237