aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test/llmime_tut.cpp
blob: f5cf38f4bb04208a052b85960e96e969c535feb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/** 
 * @file llmime_tut.cpp
 * @author Phoenix
 * @date 2006-12-24
 * @brief BRIEF_DESC of llmime_tut.cpp
 *
 * Copyright (c) 2006-2007, Linden Research, Inc.
 * 
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 */

#include <tut/tut.h>
#include "linden_common.h"
#include "llmime.h"
#include "llsdserialize.h"

namespace tut
{
	struct mime_index
	{
	};
	typedef test_group<mime_index> mime_index_t;
	typedef mime_index_t::object mime_index_object_t;
	tut::mime_index_t tut_mime_index("mime_index");

	template<> template<>
	void mime_index_object_t::test<1>()
	{
		LLMimeIndex mime;
		ensure("no headers", mime.headers().isUndefined());
		ensure_equals("invalid offset", mime.offset(), -1);
		ensure_equals("invalid content length", mime.contentLength(), -1);
		ensure("no content type", mime.contentType().empty());
		ensure("not multipart", !mime.isMultipart());
		ensure_equals("no attachments", mime.subPartCount(), 0);
	}

	template<> template<>
	void mime_index_object_t::test<2>()
	{
		const S32 CONTENT_LENGTH = 6000;
		const S32 CONTENT_OFFSET = 100;
		const std::string CONTENT_TYPE = std::string("image/j2c");
		LLSD headers;
		headers["Content-Length"] = CONTENT_LENGTH;
		headers["Content-Type"] = CONTENT_TYPE;
		LLMimeIndex mime(headers, CONTENT_OFFSET);
		ensure("headers are map", mime.headers().isMap());
		ensure_equals("offset", mime.offset(), CONTENT_OFFSET);
		ensure_equals("content length", mime.contentLength(), CONTENT_LENGTH);
		ensure_equals("type is image/j2c", mime.contentType(), CONTENT_TYPE);
		ensure("not multipart", !mime.isMultipart());
		ensure_equals("no attachments", mime.subPartCount(), 0);
	}

	template<> template<>
	void mime_index_object_t::test<3>()
	{
		const S32 MULTI_CONTENT_LENGTH = 8000;
		const S32 MULTI_CONTENT_OFFSET = 100;
		const std::string MULTI_CONTENT_TYPE = std::string("multipart/mixed");
		LLSD headers;
		headers["Content-Length"] = MULTI_CONTENT_LENGTH;
		headers["Content-Type"] = MULTI_CONTENT_TYPE;
		LLMimeIndex mime(headers, MULTI_CONTENT_OFFSET);
		llinfos << "headers: " << LLSDOStreamer<LLSDNotationFormatter>(headers)
			<< llendl;


		const S32 META_CONTENT_LENGTH = 700;
		const S32 META_CONTENT_OFFSET = 69;
		const std::string META_CONTENT_TYPE = std::string(
			"text/llsd+xml");
		headers = LLSD::emptyMap();
		headers["Content-Length"] = META_CONTENT_LENGTH;
		headers["Content-Type"] = META_CONTENT_TYPE;
		LLMimeIndex meta(headers, META_CONTENT_OFFSET);
		mime.attachSubPart(meta);

		const S32 IMAGE_CONTENT_LENGTH = 6000;
		const S32 IMAGE_CONTENT_OFFSET = 200;
		const std::string IMAGE_CONTENT_TYPE = std::string("image/j2c");
		headers = LLSD::emptyMap();
		headers["Content-Length"] = IMAGE_CONTENT_LENGTH;
		headers["Content-Type"] = IMAGE_CONTENT_TYPE;
		LLMimeIndex image(headers, IMAGE_CONTENT_OFFSET);
		mime.attachSubPart(image);

		// make sure we have a valid multi-part
		ensure("is multipart", mime.isMultipart());
		ensure_equals("multi offset", mime.offset(), MULTI_CONTENT_OFFSET);
		ensure_equals(
			"multi content length",
			mime.contentLength(),
			MULTI_CONTENT_LENGTH);
		ensure_equals("two attachments", mime.subPartCount(), 2);

		// make sure ranged gets do the right thing with out of bounds
		// sub-parts.
		LLMimeIndex invalid_child(mime.subPart(-1));
		ensure("no headers", invalid_child.headers().isUndefined());
		ensure_equals("invalid offset", invalid_child.offset(), -1);
		ensure_equals(
			"invalid content length", invalid_child.contentLength(), -1);
		ensure("no content type", invalid_child.contentType().empty());
		ensure("not multipart", !invalid_child.isMultipart());
		ensure_equals("no attachments", invalid_child.subPartCount(), 0);

		invalid_child = mime.subPart(2);
		ensure("no headers", invalid_child.headers().isUndefined());
		ensure_equals("invalid offset", invalid_child.offset(), -1);
		ensure_equals(
			"invalid content length", invalid_child.contentLength(), -1);
		ensure("no content type", invalid_child.contentType().empty());
		ensure("not multipart", !invalid_child.isMultipart());
		ensure_equals("no attachments", invalid_child.subPartCount(), 0);
	}

	template<> template<>
	void mime_index_object_t::test<4>()
	{
		const S32 MULTI_CONTENT_LENGTH = 8000;
		const S32 MULTI_CONTENT_OFFSET = 100;
		const std::string MULTI_CONTENT_TYPE = std::string("multipart/mixed");
		LLSD headers;
		headers["Content-Length"] = MULTI_CONTENT_LENGTH;
		headers["Content-Type"] = MULTI_CONTENT_TYPE;
		LLMimeIndex mime(headers, MULTI_CONTENT_OFFSET);

		const S32 META_CONTENT_LENGTH = 700;
		const S32 META_CONTENT_OFFSET = 69;
		const std::string META_CONTENT_TYPE = std::string(
			"application/llsd+xml");
		headers = LLSD::emptyMap();
		headers["Content-Length"] = META_CONTENT_LENGTH;
		headers["Content-Type"] = META_CONTENT_TYPE;
		LLMimeIndex meta(headers, META_CONTENT_OFFSET);
		mime.attachSubPart(meta);

		const S32 IMAGE_CONTENT_LENGTH = 6000;
		const S32 IMAGE_CONTENT_OFFSET = 200;
		const std::string IMAGE_CONTENT_TYPE = std::string("image/j2c");
		headers = LLSD::emptyMap();
		headers["Content-Length"] = IMAGE_CONTENT_LENGTH;
		headers["Content-Type"] = IMAGE_CONTENT_TYPE;
		LLMimeIndex image(headers, IMAGE_CONTENT_OFFSET);
		mime.attachSubPart(image);

		// check what we have
		ensure("is multipart", mime.isMultipart());
		ensure_equals("multi offset", mime.offset(), MULTI_CONTENT_OFFSET);
		ensure_equals(
			"multi content length",
			mime.contentLength(),
			MULTI_CONTENT_LENGTH);
		ensure_equals("two attachments", mime.subPartCount(), 2);

		LLMimeIndex actual_meta = mime.subPart(0);
		ensure_equals(
			"meta type", actual_meta.contentType(), META_CONTENT_TYPE);
		ensure_equals(
			"meta offset", actual_meta.offset(), META_CONTENT_OFFSET);
		ensure_equals(
			"meta content length",
			actual_meta.contentLength(),
			META_CONTENT_LENGTH);

		LLMimeIndex actual_image = mime.subPart(1);
		ensure_equals(
			"image type", actual_image.contentType(), IMAGE_CONTENT_TYPE);
		ensure_equals(
			"image offset", actual_image.offset(), IMAGE_CONTENT_OFFSET);
		ensure_equals(
			"image content length",
			actual_image.contentLength(),
			IMAGE_CONTENT_LENGTH);
	}

/*
	template<> template<>
	void mime_index_object_t::test<5>()
	{
	}
	template<> template<>
	void mime_index_object_t::test<6>()
	{
	}
	template<> template<>
	void mime_index_object_t::test<7>()
	{
	}
	template<> template<>
	void mime_index_object_t::test<8>()
	{
	}
	template<> template<>
	void mime_index_object_t::test<>()
	{
	}
*/
}


namespace tut
{
	struct mime_parse
	{
	};
	typedef test_group<mime_parse> mime_parse_t;
	typedef mime_parse_t::object mime_parse_object_t;
	tut::mime_parse_t tut_mime_parse("mime_parse");

	template<> template<>
	void mime_parse_object_t::test<1>()
	{
		// parse one mime object
		const std::string SERIALIZED_MIME("Content-Length: 200\r\nContent-Type: text/plain\r\n\r\naaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccc\r\n");
		std::stringstream istr;
		istr.str(SERIALIZED_MIME);
		LLMimeIndex mime;
		LLMimeParser parser;
		bool ok = parser.parseIndex(istr, mime);
		ensure("Parse successful.", ok);
		ensure_equals("content type", mime.contentType(), "text/plain");
		ensure_equals("content length", mime.contentLength(), 200);
		ensure_equals("offset", mime.offset(), 49);
 	}

	template<> template<>
	void mime_parse_object_t::test<2>()
	{
		// make sure we only parse one.
		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");
		std::stringstream istr;
		istr.str(SERIALIZED_MIME);
		LLMimeIndex mime;
		LLMimeParser parser;
		bool ok = parser.parseIndex(istr, mime);
		ensure("Parse successful.", ok);
		ensure("not multipart.", !mime.isMultipart());
		ensure_equals("content type", mime.contentType(), "text/plain");
		ensure_equals("content length", mime.contentLength(), 200);
		ensure_equals("offset", mime.offset(), 49);
	}

	template<> template<>
	void mime_parse_object_t::test<3>()
	{
		// test multi-part and lack of content length for some of it.
		/*
Content-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
		 */
		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");
		std::stringstream istr;
		istr.str(SERIALIZED_MIME);
		LLMimeIndex mime;
		LLMimeParser parser;
		bool ok = parser.parseIndex(istr, mime);
		ensure("Parse successful.", ok);
		ensure("is multipart.", mime.isMultipart());
		ensure_equals("sub-part count", mime.subPartCount(), 2);
		ensure_equals("content length", mime.contentLength(), 150);
		ensure_equals("data offset for multipart", mime.offset(), 74);

		LLMimeIndex mime_plain(mime.subPart(0));
		ensure_equals(
			"first part type",
			mime_plain.contentType(),
			"text/plain");
		ensure_equals(
			"first part content length not known.",
			mime_plain.contentLength(),
			-1);
		ensure_equals("first part offset", mime_plain.offset(), 113);

		LLMimeIndex mime_xml(mime.subPart(1));
		ensure_equals(
			"second part type",
			mime_xml.contentType(),
			"text/xml; charset=UTF-8");
		ensure_equals(
			"second part content length",
			mime_xml.contentLength(),
			22);
		ensure_equals("second part offset", mime_xml.offset(), 198);
	}

	template<> template<>
	void mime_parse_object_t::test<4>()
	{
		// test multi-part, unquoted separator, and premature eof conditions
		/*
Content-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		 */
		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");
		std::stringstream istr;
		istr.str(SERIALIZED_MIME);
		LLMimeIndex mime;
		LLMimeParser parser;
		bool ok = parser.parseIndex(istr, mime);
		ensure("Parse successful.", ok);
		ensure("is multipart.", mime.isMultipart());
		ensure_equals("sub-part count", mime.subPartCount(), 2);
		ensure_equals("content length", mime.contentLength(), 220);
		ensure_equals("data offset for multipart", mime.offset(), 72);

		LLMimeIndex mime_plain(mime.subPart(0));
		ensure_equals(
			"first part type",
			mime_plain.contentType(),
			"text/plain");
		ensure_equals(
			"first part content length",
			mime_plain.contentLength(),
			55);
		ensure_equals("first part offset", mime_plain.offset(), 131);

		LLMimeIndex mime_xml(mime.subPart(1));
		ensure_equals(
			"second part type",
			mime_xml.contentType(),
			"text/xml; charset=UTF-8");
		ensure_equals(
			"second part content length",
			mime_xml.contentLength(),
			22);
		ensure_equals("second part offset", mime_xml.offset(), 262);
	}

	template<> template<>
	void mime_parse_object_t::test<5>()
	{
		// test multi-part with multiple params
		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");
		std::stringstream istr;
		istr.str(SERIALIZED_MIME);
		LLMimeIndex mime;
		LLMimeParser parser;
		bool ok = parser.parseIndex(istr, mime);
		ensure("Parse successful.", ok);
		ensure("is multipart.", mime.isMultipart());
		ensure_equals("sub-part count", mime.subPartCount(), 2);
		ensure_equals("content length", mime.contentLength(), 220);

		LLMimeIndex mime_plain(mime.subPart(0));
		ensure_equals(
			"first part type",
			mime_plain.contentType(),
			"text/plain");
		ensure_equals(
			"first part content length",
			mime_plain.contentLength(),
			55);

		LLMimeIndex mime_xml(mime.subPart(1));
		ensure_equals(
			"second part type",
			mime_xml.contentType(),
			"text/xml; charset=UTF-8");
		ensure_equals(
			"second part content length",
			mime_xml.contentLength(),
			22);
	}

	template<> template<>
	void mime_parse_object_t::test<6>()
	{
		// test multi-part with no specified boundary and eof
/*
Content-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
*/
		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");
		std::stringstream istr;
		istr.str(SERIALIZED_MIME);
		LLMimeIndex mime;
		LLMimeParser parser;
		bool ok = parser.parseIndex(istr, mime);
		ensure("Parse successful.", ok);
		ensure("is multipart.", mime.isMultipart());
		ensure_equals("sub-part count", mime.subPartCount(), 2);
		ensure_equals("content length", mime.contentLength(), 500);
		ensure_equals("data offset for multipart", mime.offset(), 56);

		LLMimeIndex mime_plain(mime.subPart(0));
		ensure_equals(
			"first part type",
			mime_plain.contentType(),
			"text/plain");
		ensure_equals(
			"first part content length",
			mime_plain.contentLength(),
			55);
		ensure_equals("first part offset", mime_plain.offset(), 108);

		LLMimeIndex mime_xml(mime.subPart(1));
		ensure_equals(
			"second part type",
			mime_xml.contentType(),
			"text/xml; charset=UTF-8");
		ensure_equals(
			"second part content length",
			mime_xml.contentLength(),
			22);
		ensure_equals("second part offset", mime_xml.offset(), 232);
	}

/*
	template<> template<>
	void mime_parse_object_t::test<>()
	{
	}
	template<> template<>
	void mime_parse_object_t::test<>()
	{
	}
	template<> template<>
	void mime_parse_object_t::test<>()
	{
	}
	template<> template<>
	void mime_parse_object_t::test<>()
	{
	}
*/
}