ferencd@0: // ferencd@0: // VMime library (http://www.vmime.org) ferencd@0: // Copyright (C) 2002-2013 Vincent Richard ferencd@0: // ferencd@0: // This program is free software; you can redistribute it and/or ferencd@0: // modify it under the terms of the GNU General Public License as ferencd@0: // published by the Free Software Foundation; either version 3 of ferencd@0: // the License, or (at your option) any later version. ferencd@0: // ferencd@0: // This program is distributed in the hope that it will be useful, ferencd@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of ferencd@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ferencd@0: // General Public License for more details. ferencd@0: // ferencd@0: // You should have received a copy of the GNU General Public License along ferencd@0: // with this program; if not, write to the Free Software Foundation, Inc., ferencd@0: // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ferencd@0: // ferencd@0: // Linking this library statically or dynamically with other modules is making ferencd@0: // a combined work based on this library. Thus, the terms and conditions of ferencd@0: // the GNU General Public License cover the whole combination. ferencd@0: // ferencd@0: ferencd@0: #include "tests/testUtils.hpp" ferencd@0: #include "vmime/htmlTextPart.hpp" ferencd@0: ferencd@0: ferencd@0: VMIME_TEST_SUITE_BEGIN(htmlTextPartTest) ferencd@0: ferencd@0: VMIME_TEST_LIST_BEGIN ferencd@0: VMIME_TEST(testParseText) ferencd@0: VMIME_TEST(testParseEmbeddedObjectsCID) ferencd@0: VMIME_TEST(testParseEmbeddedObjectsLocation) ferencd@0: VMIME_TEST_LIST_END ferencd@0: ferencd@0: ferencd@0: static const vmime::string extractContent ferencd@0: (vmime::shared_ptr cth) ferencd@0: { ferencd@0: std::ostringstream oss; ferencd@0: vmime::utility::outputStreamAdapter osa(oss); ferencd@0: ferencd@0: cth->extract(osa); ferencd@0: ferencd@0: return oss.str(); ferencd@0: } ferencd@0: ferencd@0: ferencd@0: void testParseText() ferencd@0: { ferencd@0: const vmime::string msgString = "" ferencd@0: "MIME-Version: 1.0\r\n" ferencd@0: "Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL1\r\n" ferencd@0: "Content-Type: text/plain; charset=\"x-ch1\"\r\n" ferencd@0: "\r\n" ferencd@0: "Plain text part\r\n" ferencd@0: "--LEVEL1\r\n" ferencd@0: "Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL2\r\n" ferencd@0: "Content-Type: text/html; charset=\"x-ch2\"\r\n" ferencd@0: "\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL2\r\n" ferencd@0: "Content-Type: image/png; name=\"image.png\"\r\n" ferencd@0: "Content-Disposition: inline; filename=\"image.png\"\r\n" ferencd@0: "Content-ID: \r\n" ferencd@0: "\r\n" ferencd@0: "Image\r\n" ferencd@0: "--LEVEL2--\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL1--\r\n" ferencd@0: ""; ferencd@0: ferencd@0: vmime::shared_ptr msg = vmime::make_shared (); ferencd@0: msg->parse(msgString); ferencd@0: ferencd@0: // Sanity checks ferencd@0: VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount()); ferencd@0: VASSERT_EQ("part-count2", 2, msg->getBody()->getPartAt(1)->getBody()->getPartCount()); ferencd@0: ferencd@0: vmime::htmlTextPart htmlPart; ferencd@0: htmlPart.parse(msg, msg->getBody()->getPartAt(1), ferencd@0: msg->getBody()->getPartAt(1)->getBody()->getPartAt(0)); ferencd@0: ferencd@0: VASSERT_EQ("plain", "Plain text part", extractContent(htmlPart.getPlainText())); ferencd@0: VASSERT_EQ("html", "", extractContent(htmlPart.getText())); ferencd@0: ferencd@0: // Should return the charset of the HTML part ferencd@0: VASSERT_EQ("charset", "x-ch2", htmlPart.getCharset().generate()); ferencd@0: } ferencd@0: ferencd@0: /** Test parsing of embedded objects by CID (Content-Id). ferencd@0: */ ferencd@0: void testParseEmbeddedObjectsCID() ferencd@0: { ferencd@0: const vmime::string msgString = "" ferencd@0: "MIME-Version: 1.0\r\n" ferencd@0: "Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL1\r\n" ferencd@0: "Content-Type: text/plain; charset=\"x-ch1\"\r\n" ferencd@0: "\r\n" ferencd@0: "Plain text part\r\n" ferencd@0: "--LEVEL1\r\n" ferencd@0: "Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL2\r\n" // one embedded object before... ferencd@0: "Content-Type: image/png; name=\"image1.png\"\r\n" ferencd@0: "Content-Disposition: inline; filename=\"image1.png\"\r\n" ferencd@0: "Content-ID: \r\n" ferencd@0: "\r\n" ferencd@0: "Image1\r\n" ferencd@0: "--LEVEL2\r\n" // ...the actual text part... ferencd@0: "Content-Type: text/html; charset=\"x-ch2\"\r\n" ferencd@0: "\r\n" ferencd@0: "\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL2\r\n" // ...and one after ferencd@0: "Content-Type: image/jpeg; name=\"image2.jpg\"\r\n" ferencd@0: "Content-Disposition: inline; filename=\"image2.jpg\"\r\n" ferencd@0: "Content-ID: \r\n" ferencd@0: "\r\n" ferencd@0: "Image2\r\n" ferencd@0: "--LEVEL2--\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL1--\r\n" ferencd@0: ""; ferencd@0: ferencd@0: vmime::shared_ptr msg = vmime::make_shared (); ferencd@0: msg->parse(msgString); ferencd@0: ferencd@0: // Sanity checks ferencd@0: VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount()); ferencd@0: VASSERT_EQ("part-count2", 3, msg->getBody()->getPartAt(1)->getBody()->getPartCount()); ferencd@0: ferencd@0: vmime::htmlTextPart htmlPart; ferencd@0: htmlPart.parse(msg, msg->getBody()->getPartAt(1), ferencd@0: msg->getBody()->getPartAt(1)->getBody()->getPartAt(1)); ferencd@0: ferencd@0: // Two embedded objects should be found. ferencd@0: // BUGFIX: "CID:" prefix is not case-sensitive. ferencd@0: VASSERT_EQ("count", 2, htmlPart.getObjectCount()); ferencd@0: ferencd@0: // Ensure the right objects have been found. ferencd@0: VASSERT_EQ("has-obj1", true, htmlPart.hasObject("image1@test")); ferencd@0: VASSERT_EQ("has-obj2", true, htmlPart.hasObject("image2@test")); ferencd@0: ferencd@0: // hasObject() should also work with prefixes ferencd@0: VASSERT_EQ("has-obj1-pre", true, htmlPart.hasObject("CID:image1@test")); ferencd@0: VASSERT_EQ("has-obj2-pre", true, htmlPart.hasObject("cid:image2@test")); ferencd@0: ferencd@0: // Check data in objects ferencd@0: vmime::shared_ptr obj; ferencd@0: ferencd@0: obj = htmlPart.findObject("image1@test"); ferencd@0: ferencd@0: VASSERT_EQ("ref-type1", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_ID, obj->getReferenceType()); ferencd@0: VASSERT_EQ("id-obj1", "image1@test", obj->getId()); ferencd@0: VASSERT_EQ("data-obj1", "Image1", extractContent(obj->getData())); ferencd@0: VASSERT_EQ("type-obj1", "image/png", obj->getType().generate()); ferencd@0: ferencd@0: obj = htmlPart.findObject("image2@test"); ferencd@0: ferencd@0: VASSERT_EQ("ref-type2", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_ID, obj->getReferenceType()); ferencd@0: VASSERT_EQ("id-obj2", "image2@test", obj->getId()); ferencd@0: VASSERT_EQ("data-obj2", "Image2", extractContent(obj->getData())); ferencd@0: VASSERT_EQ("type-obj2", "image/jpeg", obj->getType().generate()); ferencd@0: } ferencd@0: ferencd@0: /** Test parsing of embedded objects by location. ferencd@0: */ ferencd@0: void testParseEmbeddedObjectsLocation() ferencd@0: { ferencd@0: const vmime::string msgString = "" ferencd@0: "MIME-Version: 1.0\r\n" ferencd@0: "Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL1\r\n" ferencd@0: "Content-Type: text/plain; charset=\"x-ch1\"\r\n" ferencd@0: "\r\n" ferencd@0: "Plain text part\r\n" ferencd@0: "--LEVEL1\r\n" ferencd@0: "Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL2\r\n" ferencd@0: "Content-Type: image/png; name=\"image1.png\"\r\n" ferencd@0: "Content-Location: http://www.vmime.org/test/image1.png\r\n" ferencd@0: "Content-Disposition: inline; filename=\"image1.png\"\r\n" ferencd@0: "Content-Id: \r\n" ferencd@0: "\r\n" ferencd@0: "Image1\r\n" ferencd@0: "--LEVEL2\r\n" ferencd@0: "Content-Type: text/html; charset=\"x-ch2\"\r\n" ferencd@0: "\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL2--\r\n" ferencd@0: "\r\n" ferencd@0: "--LEVEL1--\r\n" ferencd@0: ""; ferencd@0: ferencd@0: vmime::shared_ptr msg = vmime::make_shared (); ferencd@0: msg->parse(msgString); ferencd@0: ferencd@0: // Sanity checks ferencd@0: VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount()); ferencd@0: VASSERT_EQ("part-count2", 2, msg->getBody()->getPartAt(1)->getBody()->getPartCount()); ferencd@0: ferencd@0: vmime::htmlTextPart htmlPart; ferencd@0: htmlPart.parse(msg, msg->getBody()->getPartAt(1), ferencd@0: msg->getBody()->getPartAt(1)->getBody()->getPartAt(1)); ferencd@0: ferencd@0: // Only one embedded object ferencd@0: VASSERT_EQ("count", 1, htmlPart.getObjectCount()); ferencd@0: ferencd@0: // Should work only with Content-Location as the Content-Id is ferencd@0: // not referenced in the HTML contents ferencd@0: VASSERT_EQ("has-obj-loc", true, htmlPart.hasObject("http://www.vmime.org/test/image1.png")); ferencd@0: VASSERT_EQ("has-obj-cid", false, htmlPart.hasObject("image1@test")); ferencd@0: ferencd@0: // Check data ferencd@0: vmime::shared_ptr obj; ferencd@0: ferencd@0: obj = htmlPart.findObject("http://www.vmime.org/test/image1.png"); ferencd@0: ferencd@0: VASSERT_EQ("ref-type", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_LOCATION, obj->getReferenceType()); ferencd@0: VASSERT_EQ("id-obj", "http://www.vmime.org/test/image1.png", obj->getId()); ferencd@0: VASSERT_EQ("data-obj", "Image1", extractContent(obj->getData())); ferencd@0: VASSERT_EQ("type-obj", "image/png", obj->getType().generate()); ferencd@0: } ferencd@0: ferencd@0: // TODO: test generation of text parts ferencd@0: ferencd@0: VMIME_TEST_SUITE_END ferencd@0: