comparison 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
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 #include "vmime/htmlTextPart.hpp"
26
27
28 VMIME_TEST_SUITE_BEGIN(htmlTextPartTest)
29
30 VMIME_TEST_LIST_BEGIN
31 VMIME_TEST(testParseText)
32 VMIME_TEST(testParseEmbeddedObjectsCID)
33 VMIME_TEST(testParseEmbeddedObjectsLocation)
34 VMIME_TEST_LIST_END
35
36
37 static const vmime::string extractContent
38 (vmime::shared_ptr <const vmime::contentHandler> cth)
39 {
40 std::ostringstream oss;
41 vmime::utility::outputStreamAdapter osa(oss);
42
43 cth->extract(osa);
44
45 return oss.str();
46 }
47
48
49 void testParseText()
50 {
51 const vmime::string msgString = ""
52 "MIME-Version: 1.0\r\n"
53 "Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n"
54 "\r\n"
55 "--LEVEL1\r\n"
56 "Content-Type: text/plain; charset=\"x-ch1\"\r\n"
57 "\r\n"
58 "Plain text part\r\n"
59 "--LEVEL1\r\n"
60 "Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n"
61 "\r\n"
62 "--LEVEL2\r\n"
63 "Content-Type: text/html; charset=\"x-ch2\"\r\n"
64 "\r\n"
65 "<img src=\"cid:image@test\"/>\r\n"
66 "--LEVEL2\r\n"
67 "Content-Type: image/png; name=\"image.png\"\r\n"
68 "Content-Disposition: inline; filename=\"image.png\"\r\n"
69 "Content-ID: <image@test>\r\n"
70 "\r\n"
71 "Image\r\n"
72 "--LEVEL2--\r\n"
73 "\r\n"
74 "--LEVEL1--\r\n"
75 "";
76
77 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>();
78 msg->parse(msgString);
79
80 // Sanity checks
81 VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount());
82 VASSERT_EQ("part-count2", 2, msg->getBody()->getPartAt(1)->getBody()->getPartCount());
83
84 vmime::htmlTextPart htmlPart;
85 htmlPart.parse(msg, msg->getBody()->getPartAt(1),
86 msg->getBody()->getPartAt(1)->getBody()->getPartAt(0));
87
88 VASSERT_EQ("plain", "Plain text part", extractContent(htmlPart.getPlainText()));
89 VASSERT_EQ("html", "<img src=\"cid:image@test\"/>", extractContent(htmlPart.getText()));
90
91 // Should return the charset of the HTML part
92 VASSERT_EQ("charset", "x-ch2", htmlPart.getCharset().generate());
93 }
94
95 /** Test parsing of embedded objects by CID (Content-Id).
96 */
97 void testParseEmbeddedObjectsCID()
98 {
99 const vmime::string msgString = ""
100 "MIME-Version: 1.0\r\n"
101 "Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n"
102 "\r\n"
103 "--LEVEL1\r\n"
104 "Content-Type: text/plain; charset=\"x-ch1\"\r\n"
105 "\r\n"
106 "Plain text part\r\n"
107 "--LEVEL1\r\n"
108 "Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n"
109 "\r\n"
110 "--LEVEL2\r\n" // one embedded object before...
111 "Content-Type: image/png; name=\"image1.png\"\r\n"
112 "Content-Disposition: inline; filename=\"image1.png\"\r\n"
113 "Content-ID: <image1@test>\r\n"
114 "\r\n"
115 "Image1\r\n"
116 "--LEVEL2\r\n" // ...the actual text part...
117 "Content-Type: text/html; charset=\"x-ch2\"\r\n"
118 "\r\n"
119 "<img src=\"cid:image1@test\"/>\r\n"
120 "<img src=\"CID:image2@test\"/>\r\n"
121 "--LEVEL2\r\n" // ...and one after
122 "Content-Type: image/jpeg; name=\"image2.jpg\"\r\n"
123 "Content-Disposition: inline; filename=\"image2.jpg\"\r\n"
124 "Content-ID: <image2@test>\r\n"
125 "\r\n"
126 "Image2\r\n"
127 "--LEVEL2--\r\n"
128 "\r\n"
129 "--LEVEL1--\r\n"
130 "";
131
132 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>();
133 msg->parse(msgString);
134
135 // Sanity checks
136 VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount());
137 VASSERT_EQ("part-count2", 3, msg->getBody()->getPartAt(1)->getBody()->getPartCount());
138
139 vmime::htmlTextPart htmlPart;
140 htmlPart.parse(msg, msg->getBody()->getPartAt(1),
141 msg->getBody()->getPartAt(1)->getBody()->getPartAt(1));
142
143 // Two embedded objects should be found.
144 // BUGFIX: "CID:" prefix is not case-sensitive.
145 VASSERT_EQ("count", 2, htmlPart.getObjectCount());
146
147 // Ensure the right objects have been found.
148 VASSERT_EQ("has-obj1", true, htmlPart.hasObject("image1@test"));
149 VASSERT_EQ("has-obj2", true, htmlPart.hasObject("image2@test"));
150
151 // hasObject() should also work with prefixes
152 VASSERT_EQ("has-obj1-pre", true, htmlPart.hasObject("CID:image1@test"));
153 VASSERT_EQ("has-obj2-pre", true, htmlPart.hasObject("cid:image2@test"));
154
155 // Check data in objects
156 vmime::shared_ptr <const vmime::htmlTextPart::embeddedObject> obj;
157
158 obj = htmlPart.findObject("image1@test");
159
160 VASSERT_EQ("ref-type1", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_ID, obj->getReferenceType());
161 VASSERT_EQ("id-obj1", "image1@test", obj->getId());
162 VASSERT_EQ("data-obj1", "Image1", extractContent(obj->getData()));
163 VASSERT_EQ("type-obj1", "image/png", obj->getType().generate());
164
165 obj = htmlPart.findObject("image2@test");
166
167 VASSERT_EQ("ref-type2", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_ID, obj->getReferenceType());
168 VASSERT_EQ("id-obj2", "image2@test", obj->getId());
169 VASSERT_EQ("data-obj2", "Image2", extractContent(obj->getData()));
170 VASSERT_EQ("type-obj2", "image/jpeg", obj->getType().generate());
171 }
172
173 /** Test parsing of embedded objects by location.
174 */
175 void testParseEmbeddedObjectsLocation()
176 {
177 const vmime::string msgString = ""
178 "MIME-Version: 1.0\r\n"
179 "Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n"
180 "\r\n"
181 "--LEVEL1\r\n"
182 "Content-Type: text/plain; charset=\"x-ch1\"\r\n"
183 "\r\n"
184 "Plain text part\r\n"
185 "--LEVEL1\r\n"
186 "Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n"
187 "\r\n"
188 "--LEVEL2\r\n"
189 "Content-Type: image/png; name=\"image1.png\"\r\n"
190 "Content-Location: http://www.vmime.org/test/image1.png\r\n"
191 "Content-Disposition: inline; filename=\"image1.png\"\r\n"
192 "Content-Id: <image1@test>\r\n"
193 "\r\n"
194 "Image1\r\n"
195 "--LEVEL2\r\n"
196 "Content-Type: text/html; charset=\"x-ch2\"\r\n"
197 "\r\n"
198 "<img src=\"http://www.vmime.org/test/image1.png\"/>\r\n"
199 "--LEVEL2--\r\n"
200 "\r\n"
201 "--LEVEL1--\r\n"
202 "";
203
204 vmime::shared_ptr <vmime::message> msg = vmime::make_shared <vmime::message>();
205 msg->parse(msgString);
206
207 // Sanity checks
208 VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount());
209 VASSERT_EQ("part-count2", 2, msg->getBody()->getPartAt(1)->getBody()->getPartCount());
210
211 vmime::htmlTextPart htmlPart;
212 htmlPart.parse(msg, msg->getBody()->getPartAt(1),
213 msg->getBody()->getPartAt(1)->getBody()->getPartAt(1));
214
215 // Only one embedded object
216 VASSERT_EQ("count", 1, htmlPart.getObjectCount());
217
218 // Should work only with Content-Location as the Content-Id is
219 // not referenced in the HTML contents
220 VASSERT_EQ("has-obj-loc", true, htmlPart.hasObject("http://www.vmime.org/test/image1.png"));
221 VASSERT_EQ("has-obj-cid", false, htmlPart.hasObject("image1@test"));
222
223 // Check data
224 vmime::shared_ptr <const vmime::htmlTextPart::embeddedObject> obj;
225
226 obj = htmlPart.findObject("http://www.vmime.org/test/image1.png");
227
228 VASSERT_EQ("ref-type", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_LOCATION, obj->getReferenceType());
229 VASSERT_EQ("id-obj", "http://www.vmime.org/test/image1.png", obj->getId());
230 VASSERT_EQ("data-obj", "Image1", extractContent(obj->getData()));
231 VASSERT_EQ("type-obj", "image/png", obj->getType().generate());
232 }
233
234 // TODO: test generation of text parts
235
236 VMIME_TEST_SUITE_END
237