| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include "test.h" |
| 19 |
#include <pjsip.h> |
| 20 |
#include <pjlib.h> |
| 21 |
|
| 22 |
#define THIS_FILE "" |
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 43 |
"multipart", "mixed", "12345", |
| 44 |
|
| 45 |
|
| 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 |
|
| 74 |
"multipart", "mixed", "12345", |
| 75 |
|
| 76 |
|
| 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 |
|
| 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 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 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 |
|
| 299 |
|
| 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 |
|
| 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 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 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 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 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 |
|
| 404 |
|
| 405 |
|
| 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 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 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 |
|