Debian Codegraph @

Xpjproject/pjsip/src/test/multipart_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include "test.h"
19 #include <pjsip.h>
20 #include <pjlib.h>
21
22 #define THIS_FILE ""
23
24 /*
25 * multipart tests
26 */
27 typedef pj_status_t (*verify_ptr)(pj_pool_t*,pjsip_msg_body*);
28
29 static pj_status_t verify1(pj_pool_t *pool, pjsip_msg_body *body);
30 static pj_status_t verify2(pj_pool_t *pool, pjsip_msg_body *body);
31
32 static struct test_t
33 {
34 char *ctype;
35 char *csubtype;
36 char *boundary;
37 const char *msg;
38 verify_ptr verify;
39 } p_tests[] =
40 {
41 {
42 /* Content-type */
43 "multipart", "mixed", "12345",
44
45 /* Body: */
46 "This is the prolog, which should be ignored.\r\n"
47 "--12345\r\n"
48 "Content-Type: my/text\r\n"
49 "\r\n"
50 "Header and body\r\n"
51 "--12345 \t\r\n"
52 "Content-Type: hello/world\r\n"
53 "Content-Length: 0\r\n"
54 "\r\n"
55 "--12345\r\n"
56 "\r\n"
57 "Body only\r\n"
58 "--12345\r\n"
59 "Content-Type: multipart/mixed;boundary=6789\r\n"
60 "\r\n"
61 "Prolog of the subbody, should be ignored\r\n"
62 "--6789\r\n"
63 "\r\n"
64 "Subbody\r\n"
65 "--6789--\r\n"
66 "Epilogue of the subbody, should be ignored\r\n"
67 "--12345--\r\n"
68 "This is epilogue, which should be ignored too",
69
70 &verify1
71 },
72 {
73 /* Content-type */
74 "multipart", "mixed", "12345",
75
76 /* Body: */
77 "This is the prolog, which should be ignored.\r\n"
78 "--12345\r\n"
79 "Content-Type: text/plain\r\n"
80 "Content-ID: <[email protected]>\r\n"
81 "Content-ID: <\"header1\"@example.org>\r\n"
82 "Content-Length: 13\r\n"
83 "\r\n"
84 "has header1\r\n"
85 "--12345 \t\r\n"
86 "Content-Type: application/pidf+xml\r\n"
87 "Content-ID: <my [email protected]>\r\n"
88 "Content-ID: <my\[email protected]>\r\n"
89 "Content-Length: 13\r\n"
90 "\r\n"
91 "has header2\r\n"
92 "--12345\r\n"
93 "Content-Type: text/plain\r\n"
94 "Content-ID: <my [email protected]>\r\n"
95 "Content-ID: <[email protected]>\r\n"
96 "Content-ID: <my [email protected]>\r\n"
97 "Content-Length: 13\r\n"
98 "\r\n"
99 "has header4\r\n"
100 "--12345--\r\n"
101 "This is epilogue, which should be ignored too",
102
103 &verify2
104 }
105
106 };
107
108 static void init_media_type(pjsip_media_type *mt,
109 char *type, char *subtype, char *boundary)
110 {
111 static pjsip_param prm;
112
113 pjsip_media_type_init(mt, NULL, NULL);
114 if (type) mt->type = pj_str(type);
115 if (subtype) mt->subtype = pj_str(subtype);
116 if (boundary) {
117 pj_list_init(&prm);
118 prm.name = pj_str("boundary");
119 prm.value = pj_str(boundary);
120 pj_list_push_back(&mt->param, &prm);
121 }
122 }
123
124 static int verify_hdr(pj_pool_t *pool, pjsip_msg_body *multipart_body,
125 void *hdr, char *part_body)
126 {
127 pjsip_multipart_part *part;
128 pj_str_t the_body;
129
130
131 part = pjsip_multipart_find_part_by_header(pool, multipart_body, hdr, NULL);
132 if (!part) {
133 return -1;
134 }
135
136 the_body.ptr = (char*)part->body->data;
137 the_body.slen = part->body->len;
138
139 if (pj_strcmp2(&the_body, part_body) != 0) {
140 return -2;
141 }
142
143 return 0;
144 }
145
146 static int verify_cid_str(pj_pool_t *pool, pjsip_msg_body *multipart_body,
147 pj_str_t cid_url, char *part_body)
148 {
149 pjsip_multipart_part *part;
150 pj_str_t the_body;
151
152 part = pjsip_multipart_find_part_by_cid_str(pool, multipart_body, &cid_url);
153 if (!part) {
154 return -3;
155 }
156
157 the_body.ptr = (char*)part->body->data;
158 the_body.slen = part->body->len;
159
160 if (pj_strcmp2(&the_body, part_body) != 0) {
161 return -4;
162 }
163
164 return 0;
165 }
166
167 static int verify_cid_uri(pj_pool_t *pool, pjsip_msg_body *multipart_body,
168 pjsip_other_uri *cid_uri, char *part_body)
169 {
170 pjsip_multipart_part *part;
171 pj_str_t the_body;
172
173 part = pjsip_multipart_find_part_by_cid_uri(pool, multipart_body, cid_uri);
174 if (!part) {
175 return -5;
176 }
177
178 the_body.ptr = (char*)part->body->data;
179 the_body.slen = part->body->len;
180
181 if (pj_strcmp2(&the_body, part_body) != 0) {
182 return -6;
183 }
184
185 return 0;
186 }
187
188 static pj_status_t verify2(pj_pool_t *pool, pjsip_msg_body *body)
189 {
190 int rc = 0;
191 int rcbase = 300;
192 pjsip_other_uri *cid_uri;
193 pjsip_ctype_hdr *ctype_hdr = pjsip_ctype_hdr_create(pool);
194
195 ctype_hdr->media.type = pj_str("application");
196 ctype_hdr->media.subtype = pj_str("pidf+xml");
197
198 rc = verify_hdr(pool, body, ctype_hdr, "has header2");
199 if (rc) {
200 return (rc - rcbase);
201 }
202
203 rcbase += 10;
204 rc = verify_cid_str(pool, body, pj_str("cid:[email protected]"), "has header1");
205 if (rc) {
206 return (rc - rcbase);
207 }
208
209 rcbase += 10;
210 rc = verify_cid_str(pool, body, pj_str("%22header1%[email protected]"), "has header1");
211 if (rc) {
212 return (rc - rcbase);
213 }
214
215 cid_uri = pjsip_uri_get_uri(pjsip_parse_uri(pool, "<cid:%22header1%[email protected]>",
216 strlen("<cid:%22header1%[email protected]>"), 0));
217 rcbase += 10;
218 rc = verify_cid_uri(pool, body, cid_uri, "has header1");
219 if (rc) {
220 return (rc - rcbase);
221 }
222
223 rcbase += 10;
224 rc = verify_cid_str(pool, body, pj_str("<cid:my%[email protected]>"), "has header2");
225 if (rc) {
226 return (rc - rcbase);
227 }
228
229 rcbase += 10;
230 rc = verify_cid_str(pool, body, pj_str("cid:my%[email protected]"), "has header2");
231 if (rc) {
232 return (rc - rcbase);
233 }
234
235 cid_uri = pjsip_uri_get_uri(pjsip_parse_uri(pool, "<cid:my%[email protected]>",
236 strlen("<cid:my%[email protected]>"), 0));
237 rcbase += 10;
238 rc = verify_cid_uri(pool, body, cid_uri, "has header2");
239 if (rc) {
240 return (rc - rcbase);
241 }
242
243 rcbase += 10;
244 rc = verify_cid_str(pool, body, pj_str("cid:my%[email protected]"), "has header4");
245 if (rc) {
246 return (rc - rcbase);
247 }
248
249 rcbase += 10;
250 rc = verify_cid_str(pool, body, pj_str("<cid:my%[email protected]>"), "has header4");
251 if (rc) {
252 return (rc - rcbase);
253 }
254
255 cid_uri = pjsip_uri_get_uri(pjsip_parse_uri(pool, "<cid:my%[email protected]>",
256 strlen("<cid:my%[email protected]>"), 0));
257 rcbase += 10;
258 rc = verify_cid_uri(pool, body, cid_uri, "has header4");
259 if (rc) {
260 return (rc - rcbase);
261 }
262
263 rcbase += 10;
264 rc = verify_cid_str(pool, body, pj_str("<my%[email protected]>"), "has header4");
265 if (rc) {
266 return (rc - rcbase);
267 }
268
269 /* These should all fail for malformed or missing URI */
270 rcbase += 10;
271 rc = verify_cid_str(pool, body, pj_str("cid:"), "has header4");
272 if (!rc) {
273 return (rc - rcbase);
274 }
275
276 /* This will trigger assertion. */
277 /*
278 rcbase += 10;
279 rc = verify_cid_str(pool, body, pj_str(""), "has header4");
280 if (!rc) {
281 return (rc - rcbase);
282 }
283 */
284
285 rcbase += 10;
286 rc = verify_cid_str(pool, body, pj_str("<>"), "has header4");
287 if (!rc) {
288 return (rc - rcbase);
289 }
290
291 rcbase += 10;
292 rc = verify_cid_str(pool, body, pj_str("<cid>"), "has header4");
293 if (!rc) {
294 return (rc - rcbase);
295 }
296
297 /*
298 * This is going to pass but the ' ' in the uri is un-encoded which is invalid
299 * so we should never see it.
300 */
301 rcbase += 10;
302 rc = verify_cid_str(pool, body, pj_str("cid:my [email protected]"), "has header4");
303 if (rc) {
304 return (rc - rcbase);
305 }
306
307 return 0;
308 }
309
310 static int verify_part(pjsip_multipart_part *part,
311 char *h_content_type,
312 char *h_content_subtype,
313 char *boundary,
314 int h_content_length,
315 const char *body)
316 {
317 pjsip_ctype_hdr *ctype_hdr = NULL;
318 pjsip_clen_hdr *clen_hdr = NULL;
319 pjsip_hdr *hdr;
320 pj_str_t the_body;
321
322 hdr = part->hdr.next;
323 while (hdr != &part->hdr) {
324 if (hdr->type == PJSIP_H_CONTENT_TYPE)
325 ctype_hdr = (pjsip_ctype_hdr*)hdr;
326 else if (hdr->type == PJSIP_H_CONTENT_LENGTH)
327 clen_hdr = (pjsip_clen_hdr*)hdr;
328 hdr = hdr->next;
329 }
330
331 if (h_content_type) {
332 pjsip_media_type mt;
333
334 if (ctype_hdr == NULL)
335 return -10;
336
337 init_media_type(&mt, h_content_type, h_content_subtype, boundary);
338
339 if (pjsip_media_type_cmp(&ctype_hdr->media, &mt, 2) != 0)
340 return -20;
341
342 } else {
343 if (ctype_hdr)
344 return -30;
345 }
346
347 if (h_content_length >= 0) {
348 if (clen_hdr == NULL)
349 return -50;
350 if (clen_hdr->len != h_content_length)
351 return -60;
352 } else {
353 if (clen_hdr)
354 return -70;
355 }
356
357 the_body.ptr = (char*)part->body->data;
358 the_body.slen = part->body->len;
359
360 if (pj_strcmp2(&the_body, body) != 0)
361 return -90;
362
363 return 0;
364 }
365
366 static pj_status_t verify1(pj_pool_t *pool, pjsip_msg_body *body)
367 {
368 pjsip_media_type mt;
369 pjsip_multipart_part *part;
370 int rc;
371
372 PJ_UNUSED_ARG(pool);
373
374 /* Check content-type: "multipart/mixed;boundary=12345" */
375 init_media_type(&mt, "multipart", "mixed", "12345");
376 if (pjsip_media_type_cmp(&body->content_type, &mt, 2) != 0)
377 return -200;
378
379 /* First part:
380 "Content-Type: my/text\r\n"
381 "\r\n"
382 "Header and body\r\n"
383 */
384 part = pjsip_multipart_get_first_part(body);
385 if (!part)
386 return -210;
387 if (verify_part(part, "my", "text", NULL, -1, "Header and body"))
388 return -220;
389
390 /* Next part:
391 "Content-Type: hello/world\r\n"
392 "Content-Length: 0\r\n"
393 "\r\n"
394 */
395 part = pjsip_multipart_get_next_part(body, part);
396 if (!part)
397 return -230;
398 if ((rc=verify_part(part, "hello", "world", NULL, 0, ""))!=0) {
399 PJ_LOG(3,(THIS_FILE, " err: verify_part rc=%d", rc));
400 return -240;
401 }
402
403 /* Next part:
404 "\r\n"
405 "Body only\r\n"
406 */
407 part = pjsip_multipart_get_next_part(body, part);
408 if (!part)
409 return -260;
410 if (verify_part(part, NULL, NULL, NULL, -1, "Body only"))
411 return -270;
412
413 /* Next part:
414 "Content-Type: multipart/mixed;boundary=6789\r\n"
415 "\r\n"
416 "Prolog of the subbody, should be ignored\r\n"
417 "--6789\r\n"
418 "\r\n"
419 "Subbody\r\n"
420 "--6789--\r\n"
421 "Epilogue of the subbody, should be ignored\r\n"
422
423 */
424 part = pjsip_multipart_get_next_part(body, part);
425 if (!part)
426 return -280;
427 if ((rc=verify_part(part, "multipart", "mixed", "6789", -1,
428 "Prolog of the subbody, should be ignored\r\n"
429 "--6789\r\n"
430 "\r\n"
431 "Subbody\r\n"
432 "--6789--\r\n"
433 "Epilogue of the subbody, should be ignored"))!=0) {
434 PJ_LOG(3,(THIS_FILE, " err: verify_part rc=%d", rc));
435 return -290;
436 }
437
438 return 0;
439 }
440
441 static int parse_test(void)
442 {
443 unsigned i;
444
445 for (i=0; i<PJ_ARRAY_SIZE(p_tests); ++i) {
446 pj_pool_t *pool;
447 pjsip_media_type ctype;
448 pjsip_msg_body *body;
449 pj_str_t str;
450 int rc;
451
452 pool = pjsip_endpt_create_pool(endpt, NULL, 512, 512);
453
454 init_media_type(&ctype, p_tests[i].ctype, p_tests[i].csubtype,
455 p_tests[i].boundary);
456
457 pj_strdup2_with_null(pool, &str, p_tests[i].msg);
458 body = pjsip_multipart_parse(pool, str.ptr, str.slen, &ctype, 0);
459 if (!body) {
460 pj_pool_release(pool);
461 return -100;
462 }
463
464 if (p_tests[i].verify) {
465 rc = p_tests[i].verify(pool, body);
466 } else {
467 rc = 0;
468 }
469
470 pj_pool_release(pool);
471 if (rc)
472 return rc;
473 }
474
475 return 0;
476 }
477
478 int multipart_test(void)
479 {
480 int rc;
481
482 rc = parse_test();
483 if (rc)
484 return rc;
485
486 return rc;
487 }
488