aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test/llmime_tut.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--linden/indra/test/llmime_tut.cpp443
1 files changed, 443 insertions, 0 deletions
diff --git a/linden/indra/test/llmime_tut.cpp b/linden/indra/test/llmime_tut.cpp
new file mode 100644
index 0000000..f5cf38f
--- /dev/null
+++ b/linden/indra/test/llmime_tut.cpp
@@ -0,0 +1,443 @@
1/**
2 * @file llmime_tut.cpp
3 * @author Phoenix
4 * @date 2006-12-24
5 * @brief BRIEF_DESC of llmime_tut.cpp
6 *
7 * Copyright (c) 2006-2007, Linden Research, Inc.
8 *
9 * The source code in this file ("Source Code") is provided by Linden Lab
10 * to you under the terms of the GNU General Public License, version 2.0
11 * ("GPL"), unless you have obtained a separate licensing agreement
12 * ("Other License"), formally executed by you and Linden Lab. Terms of
13 * the GPL can be found in doc/GPL-license.txt in this distribution, or
14 * online at http://secondlife.com/developers/opensource/gplv2
15 *
16 * There are special exceptions to the terms and conditions of the GPL as
17 * it is applied to this Source Code. View the full text of the exception
18 * in the file doc/FLOSS-exception.txt in this software distribution, or
19 * online at http://secondlife.com/developers/opensource/flossexception
20 *
21 * By copying, modifying or distributing this software, you acknowledge
22 * that you have read and understood your obligations described above,
23 * and agree to abide by those obligations.
24 *
25 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
26 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
27 * COMPLETENESS OR PERFORMANCE.
28 */
29
30#include <tut/tut.h>
31#include "linden_common.h"
32#include "llmime.h"
33#include "llsdserialize.h"
34
35namespace tut
36{
37 struct mime_index
38 {
39 };
40 typedef test_group<mime_index> mime_index_t;
41 typedef mime_index_t::object mime_index_object_t;
42 tut::mime_index_t tut_mime_index("mime_index");
43
44 template<> template<>
45 void mime_index_object_t::test<1>()
46 {
47 LLMimeIndex mime;
48 ensure("no headers", mime.headers().isUndefined());
49 ensure_equals("invalid offset", mime.offset(), -1);
50 ensure_equals("invalid content length", mime.contentLength(), -1);
51 ensure("no content type", mime.contentType().empty());
52 ensure("not multipart", !mime.isMultipart());
53 ensure_equals("no attachments", mime.subPartCount(), 0);
54 }
55
56 template<> template<>
57 void mime_index_object_t::test<2>()
58 {
59 const S32 CONTENT_LENGTH = 6000;
60 const S32 CONTENT_OFFSET = 100;
61 const std::string CONTENT_TYPE = std::string("image/j2c");
62 LLSD headers;
63 headers["Content-Length"] = CONTENT_LENGTH;
64 headers["Content-Type"] = CONTENT_TYPE;
65 LLMimeIndex mime(headers, CONTENT_OFFSET);
66 ensure("headers are map", mime.headers().isMap());
67 ensure_equals("offset", mime.offset(), CONTENT_OFFSET);
68 ensure_equals("content length", mime.contentLength(), CONTENT_LENGTH);
69 ensure_equals("type is image/j2c", mime.contentType(), CONTENT_TYPE);
70 ensure("not multipart", !mime.isMultipart());
71 ensure_equals("no attachments", mime.subPartCount(), 0);
72 }
73
74 template<> template<>
75 void mime_index_object_t::test<3>()
76 {
77 const S32 MULTI_CONTENT_LENGTH = 8000;
78 const S32 MULTI_CONTENT_OFFSET = 100;
79 const std::string MULTI_CONTENT_TYPE = std::string("multipart/mixed");
80 LLSD headers;
81 headers["Content-Length"] = MULTI_CONTENT_LENGTH;
82 headers["Content-Type"] = MULTI_CONTENT_TYPE;
83 LLMimeIndex mime(headers, MULTI_CONTENT_OFFSET);
84 llinfos << "headers: " << LLSDOStreamer<LLSDNotationFormatter>(headers)
85 << llendl;
86
87
88 const S32 META_CONTENT_LENGTH = 700;
89 const S32 META_CONTENT_OFFSET = 69;
90 const std::string META_CONTENT_TYPE = std::string(
91 "text/llsd+xml");
92 headers = LLSD::emptyMap();
93 headers["Content-Length"] = META_CONTENT_LENGTH;
94 headers["Content-Type"] = META_CONTENT_TYPE;
95 LLMimeIndex meta(headers, META_CONTENT_OFFSET);
96 mime.attachSubPart(meta);
97
98 const S32 IMAGE_CONTENT_LENGTH = 6000;
99 const S32 IMAGE_CONTENT_OFFSET = 200;
100 const std::string IMAGE_CONTENT_TYPE = std::string("image/j2c");
101 headers = LLSD::emptyMap();
102 headers["Content-Length"] = IMAGE_CONTENT_LENGTH;
103 headers["Content-Type"] = IMAGE_CONTENT_TYPE;
104 LLMimeIndex image(headers, IMAGE_CONTENT_OFFSET);
105 mime.attachSubPart(image);
106
107 // make sure we have a valid multi-part
108 ensure("is multipart", mime.isMultipart());
109 ensure_equals("multi offset", mime.offset(), MULTI_CONTENT_OFFSET);
110 ensure_equals(
111 "multi content length",
112 mime.contentLength(),
113 MULTI_CONTENT_LENGTH);
114 ensure_equals("two attachments", mime.subPartCount(), 2);
115
116 // make sure ranged gets do the right thing with out of bounds
117 // sub-parts.
118 LLMimeIndex invalid_child(mime.subPart(-1));
119 ensure("no headers", invalid_child.headers().isUndefined());
120 ensure_equals("invalid offset", invalid_child.offset(), -1);
121 ensure_equals(
122 "invalid content length", invalid_child.contentLength(), -1);
123 ensure("no content type", invalid_child.contentType().empty());
124 ensure("not multipart", !invalid_child.isMultipart());
125 ensure_equals("no attachments", invalid_child.subPartCount(), 0);
126
127 invalid_child = mime.subPart(2);
128 ensure("no headers", invalid_child.headers().isUndefined());
129 ensure_equals("invalid offset", invalid_child.offset(), -1);
130 ensure_equals(
131 "invalid content length", invalid_child.contentLength(), -1);
132 ensure("no content type", invalid_child.contentType().empty());
133 ensure("not multipart", !invalid_child.isMultipart());
134 ensure_equals("no attachments", invalid_child.subPartCount(), 0);
135 }
136
137 template<> template<>
138 void mime_index_object_t::test<4>()
139 {
140 const S32 MULTI_CONTENT_LENGTH = 8000;
141 const S32 MULTI_CONTENT_OFFSET = 100;
142 const std::string MULTI_CONTENT_TYPE = std::string("multipart/mixed");
143 LLSD headers;
144 headers["Content-Length"] = MULTI_CONTENT_LENGTH;
145 headers["Content-Type"] = MULTI_CONTENT_TYPE;
146 LLMimeIndex mime(headers, MULTI_CONTENT_OFFSET);
147
148 const S32 META_CONTENT_LENGTH = 700;
149 const S32 META_CONTENT_OFFSET = 69;
150 const std::string META_CONTENT_TYPE = std::string(
151 "application/llsd+xml");
152 headers = LLSD::emptyMap();
153 headers["Content-Length"] = META_CONTENT_LENGTH;
154 headers["Content-Type"] = META_CONTENT_TYPE;
155 LLMimeIndex meta(headers, META_CONTENT_OFFSET);
156 mime.attachSubPart(meta);
157
158 const S32 IMAGE_CONTENT_LENGTH = 6000;
159 const S32 IMAGE_CONTENT_OFFSET = 200;
160 const std::string IMAGE_CONTENT_TYPE = std::string("image/j2c");
161 headers = LLSD::emptyMap();
162 headers["Content-Length"] = IMAGE_CONTENT_LENGTH;
163 headers["Content-Type"] = IMAGE_CONTENT_TYPE;
164 LLMimeIndex image(headers, IMAGE_CONTENT_OFFSET);
165 mime.attachSubPart(image);
166
167 // check what we have
168 ensure("is multipart", mime.isMultipart());
169 ensure_equals("multi offset", mime.offset(), MULTI_CONTENT_OFFSET);
170 ensure_equals(
171 "multi content length",
172 mime.contentLength(),
173 MULTI_CONTENT_LENGTH);
174 ensure_equals("two attachments", mime.subPartCount(), 2);
175
176 LLMimeIndex actual_meta = mime.subPart(0);
177 ensure_equals(
178 "meta type", actual_meta.contentType(), META_CONTENT_TYPE);
179 ensure_equals(
180 "meta offset", actual_meta.offset(), META_CONTENT_OFFSET);
181 ensure_equals(
182 "meta content length",
183 actual_meta.contentLength(),
184 META_CONTENT_LENGTH);
185
186 LLMimeIndex actual_image = mime.subPart(1);
187 ensure_equals(
188 "image type", actual_image.contentType(), IMAGE_CONTENT_TYPE);
189 ensure_equals(
190 "image offset", actual_image.offset(), IMAGE_CONTENT_OFFSET);
191 ensure_equals(
192 "image content length",
193 actual_image.contentLength(),
194 IMAGE_CONTENT_LENGTH);
195 }
196
197/*
198 template<> template<>
199 void mime_index_object_t::test<5>()
200 {
201 }
202 template<> template<>
203 void mime_index_object_t::test<6>()
204 {
205 }
206 template<> template<>
207 void mime_index_object_t::test<7>()
208 {
209 }
210 template<> template<>
211 void mime_index_object_t::test<8>()
212 {
213 }
214 template<> template<>
215 void mime_index_object_t::test<>()
216 {
217 }
218*/
219}
220
221
222namespace tut
223{
224 struct mime_parse
225 {
226 };
227 typedef test_group<mime_parse> mime_parse_t;
228 typedef mime_parse_t::object mime_parse_object_t;
229 tut::mime_parse_t tut_mime_parse("mime_parse");
230
231 template<> template<>
232 void mime_parse_object_t::test<1>()
233 {
234 // parse one mime object
235 const std::string SERIALIZED_MIME("Content-Length: 200\r\nContent-Type: text/plain\r\n\r\naaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccc\r\n");
236 std::stringstream istr;
237 istr.str(SERIALIZED_MIME);
238 LLMimeIndex mime;
239 LLMimeParser parser;
240 bool ok = parser.parseIndex(istr, mime);
241 ensure("Parse successful.", ok);
242 ensure_equals("content type", mime.contentType(), "text/plain");
243 ensure_equals("content length", mime.contentLength(), 200);
244 ensure_equals("offset", mime.offset(), 49);
245 }
246
247 template<> template<>
248 void mime_parse_object_t::test<2>()
249 {
250 // make sure we only parse one.
251 const std::string SERIALIZED_MIME("Content-Length: 200\r\nContent-Type: text/plain\r\n\r\naaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccc\r\n\r\nContent-Length: 200\r\nContent-Type: text/plain\r\n\r\naaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccc\r\n\r\n");
252 std::stringstream istr;
253 istr.str(SERIALIZED_MIME);
254 LLMimeIndex mime;
255 LLMimeParser parser;
256 bool ok = parser.parseIndex(istr, mime);
257 ensure("Parse successful.", ok);
258 ensure("not multipart.", !mime.isMultipart());
259 ensure_equals("content type", mime.contentType(), "text/plain");
260 ensure_equals("content length", mime.contentLength(), 200);
261 ensure_equals("offset", mime.offset(), 49);
262 }
263
264 template<> template<>
265 void mime_parse_object_t::test<3>()
266 {
267 // test multi-part and lack of content length for some of it.
268 /*
269Content-Type: multipart/mixed; boundary="segment"rnContent-Length: 148rnrn--segmentrnContent-Type: text/plainrnrnsome datarnrn--segmentrnContent-Type: text/xml; charset=UTF-8rnContent-Length: 22rnrn<llsd><undef /></llsd>rnrn
270 */
271 const std::string SERIALIZED_MIME("Content-Type: multipart/mixed; boundary=\"segment\"\r\nContent-Length: 150\r\n\r\n--segment\r\nContent-Type: text/plain\r\n\r\nsome data\r\n\r\n--segment\r\nContent-Type: text/xml; charset=UTF-8\r\nContent-Length: 22\r\n\r\n<llsd><undef /></llsd>\r\n\r\n");
272 std::stringstream istr;
273 istr.str(SERIALIZED_MIME);
274 LLMimeIndex mime;
275 LLMimeParser parser;
276 bool ok = parser.parseIndex(istr, mime);
277 ensure("Parse successful.", ok);
278 ensure("is multipart.", mime.isMultipart());
279 ensure_equals("sub-part count", mime.subPartCount(), 2);
280 ensure_equals("content length", mime.contentLength(), 150);
281 ensure_equals("data offset for multipart", mime.offset(), 74);
282
283 LLMimeIndex mime_plain(mime.subPart(0));
284 ensure_equals(
285 "first part type",
286 mime_plain.contentType(),
287 "text/plain");
288 ensure_equals(
289 "first part content length not known.",
290 mime_plain.contentLength(),
291 -1);
292 ensure_equals("first part offset", mime_plain.offset(), 113);
293
294 LLMimeIndex mime_xml(mime.subPart(1));
295 ensure_equals(
296 "second part type",
297 mime_xml.contentType(),
298 "text/xml; charset=UTF-8");
299 ensure_equals(
300 "second part content length",
301 mime_xml.contentLength(),
302 22);
303 ensure_equals("second part offset", mime_xml.offset(), 198);
304 }
305
306 template<> template<>
307 void mime_parse_object_t::test<4>()
308 {
309 // test multi-part, unquoted separator, and premature eof conditions
310 /*
311Content-Type: multipart/mixed; boundary=segmentrnContent-Length: 220rnrn--segmentrnContent-Type: text/plainrnContent-Length: 55rnrnhow are you today?rnI do not know. I guess I am:n'fine'rnrn--segmentrnContent-Type: text/xml; charset=UTF-8rnContent-Length: 22rnrn<llsd><undef /></llsd>rnrn */
312 const std::string SERIALIZED_MIME("Content-Type: multipart/mixed; boundary=segment\r\nContent-Length: 220\r\n\r\n--segment\r\nContent-Type: text/plain\r\nContent-Length: 55\r\n\r\nhow are you today?\r\nI do not know. I guess I am:\n'fine'\r\n\r\n--segment\r\nContent-Type: text/xml; charset=UTF-8\r\nContent-Length: 22\r\n\r\n<llsd><undef /></llsd>\r\n\r\n");
313 std::stringstream istr;
314 istr.str(SERIALIZED_MIME);
315 LLMimeIndex mime;
316 LLMimeParser parser;
317 bool ok = parser.parseIndex(istr, mime);
318 ensure("Parse successful.", ok);
319 ensure("is multipart.", mime.isMultipart());
320 ensure_equals("sub-part count", mime.subPartCount(), 2);
321 ensure_equals("content length", mime.contentLength(), 220);
322 ensure_equals("data offset for multipart", mime.offset(), 72);
323
324 LLMimeIndex mime_plain(mime.subPart(0));
325 ensure_equals(
326 "first part type",
327 mime_plain.contentType(),
328 "text/plain");
329 ensure_equals(
330 "first part content length",
331 mime_plain.contentLength(),
332 55);
333 ensure_equals("first part offset", mime_plain.offset(), 131);
334
335 LLMimeIndex mime_xml(mime.subPart(1));
336 ensure_equals(
337 "second part type",
338 mime_xml.contentType(),
339 "text/xml; charset=UTF-8");
340 ensure_equals(
341 "second part content length",
342 mime_xml.contentLength(),
343 22);
344 ensure_equals("second part offset", mime_xml.offset(), 262);
345 }
346
347 template<> template<>
348 void mime_parse_object_t::test<5>()
349 {
350 // test multi-part with multiple params
351 const std::string SERIALIZED_MIME("Content-Type: multipart/mixed; boundary=segment; comment=\"testing multiple params.\"\r\nContent-Length: 220\r\n\r\n--segment\r\nContent-Type: text/plain\r\nContent-Length: 55\r\n\r\nhow are you today?\r\nI do not know. I guess I am:\n'fine'\r\n\r\n--segment\r\nContent-Type: text/xml; charset=UTF-8\r\nContent-Length: 22\r\n\r\n<llsd><undef /></llsd>\r\n\r\n");
352 std::stringstream istr;
353 istr.str(SERIALIZED_MIME);
354 LLMimeIndex mime;
355 LLMimeParser parser;
356 bool ok = parser.parseIndex(istr, mime);
357 ensure("Parse successful.", ok);
358 ensure("is multipart.", mime.isMultipart());
359 ensure_equals("sub-part count", mime.subPartCount(), 2);
360 ensure_equals("content length", mime.contentLength(), 220);
361
362 LLMimeIndex mime_plain(mime.subPart(0));
363 ensure_equals(
364 "first part type",
365 mime_plain.contentType(),
366 "text/plain");
367 ensure_equals(
368 "first part content length",
369 mime_plain.contentLength(),
370 55);
371
372 LLMimeIndex mime_xml(mime.subPart(1));
373 ensure_equals(
374 "second part type",
375 mime_xml.contentType(),
376 "text/xml; charset=UTF-8");
377 ensure_equals(
378 "second part content length",
379 mime_xml.contentLength(),
380 22);
381 }
382
383 template<> template<>
384 void mime_parse_object_t::test<6>()
385 {
386 // test multi-part with no specified boundary and eof
387/*
388Content-Type: multipart/relatedrnContent-Length: 220rnrn--rnContent-Type: text/plainrnContent-Length: 55rnrnhow are you today?rnI do not know. I guess I am:n'fine'rnrn--rnContent-Type: text/xml; charset=UTF-8rnContent-Length: 22rnrn<llsd><undef /></llsd>rnrn
389*/
390 const std::string SERIALIZED_MIME("Content-Type: multipart/related\r\nContent-Length: 500\r\n\r\n--\r\nContent-Type: text/plain\r\nContent-Length: 55\r\n\r\nhow are you today?\r\nI do not know. I guess I am:\n'fine'\r\n\r\n--\r\nContent-Type: text/xml; charset=UTF-8\r\nContent-Length: 22\r\n\r\n<llsd><undef /></llsd>\r\n\r\n");
391 std::stringstream istr;
392 istr.str(SERIALIZED_MIME);
393 LLMimeIndex mime;
394 LLMimeParser parser;
395 bool ok = parser.parseIndex(istr, mime);
396 ensure("Parse successful.", ok);
397 ensure("is multipart.", mime.isMultipart());
398 ensure_equals("sub-part count", mime.subPartCount(), 2);
399 ensure_equals("content length", mime.contentLength(), 500);
400 ensure_equals("data offset for multipart", mime.offset(), 56);
401
402 LLMimeIndex mime_plain(mime.subPart(0));
403 ensure_equals(
404 "first part type",
405 mime_plain.contentType(),
406 "text/plain");
407 ensure_equals(
408 "first part content length",
409 mime_plain.contentLength(),
410 55);
411 ensure_equals("first part offset", mime_plain.offset(), 108);
412
413 LLMimeIndex mime_xml(mime.subPart(1));
414 ensure_equals(
415 "second part type",
416 mime_xml.contentType(),
417 "text/xml; charset=UTF-8");
418 ensure_equals(
419 "second part content length",
420 mime_xml.contentLength(),
421 22);
422 ensure_equals("second part offset", mime_xml.offset(), 232);
423 }
424
425/*
426 template<> template<>
427 void mime_parse_object_t::test<>()
428 {
429 }
430 template<> template<>
431 void mime_parse_object_t::test<>()
432 {
433 }
434 template<> template<>
435 void mime_parse_object_t::test<>()
436 {
437 }
438 template<> template<>
439 void mime_parse_object_t::test<>()
440 {
441 }
442*/
443}