1
/* $Id: multipart_test.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
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
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
typedef pj_status_t (*verify_ptr)(pj_pool_t*,pjsip_msg_body*);
30
static pj_status_t verify1(pj_pool_t *pool, pjsip_msg_body *body);
43
"multipart", "mixed", "12345",
46
"This is the prolog, which should be ignored.\r\n"
48
"Content-Type: my/text\r\n"
52
"Content-Type: hello/world\r\n"
53
"Content-Length: 0\r\n"
59
"Content-Type: multipart/mixed;boundary=6789\r\n"
61
"Prolog of the subbody, should be ignored\r\n"
66
"Epilogue of the subbody, should be ignored\r\n"
68
"This is epilogue, which should be ignored too",
74
static void init_media_type(pjsip_media_type *mt,
75
char *type, char *subtype, char *boundary)
77
static pjsip_param prm;
79
pjsip_media_type_init(mt, NULL, NULL);
80
if (type) mt->type = pj_str(type);
81
if (subtype) mt->subtype = pj_str(subtype);
84
prm.name = pj_str("boundary");
85
prm.value = pj_str(boundary);
86
pj_list_push_back(&mt->param, &prm);
90
static int verify_part(pjsip_multipart_part *part,
92
char *h_content_subtype,
97
pjsip_ctype_hdr *ctype_hdr = NULL;
98
pjsip_clen_hdr *clen_hdr = NULL;
102
hdr = part->hdr.next;
103
while (hdr != &part->hdr) {
104
if (hdr->type == PJSIP_H_CONTENT_TYPE)
105
ctype_hdr = (pjsip_ctype_hdr*)hdr;
106
else if (hdr->type == PJSIP_H_CONTENT_LENGTH)
107
clen_hdr = (pjsip_clen_hdr*)hdr;
111
if (h_content_type) {
114
if (ctype_hdr == NULL)
117
init_media_type(&mt, h_content_type, h_content_subtype, boundary);
119
if (pjsip_media_type_cmp(&ctype_hdr->media, &mt, 2) != 0)
127
if (h_content_length >= 0) {
128
if (clen_hdr == NULL)
130
if (clen_hdr->len != h_content_length)
137
the_body.ptr = (char*)part->body->data;
138
the_body.slen = part->body->len;
140
if (pj_strcmp2(&the_body, body) != 0)
146
static pj_status_t verify1(pj_pool_t *pool, pjsip_msg_body *body)
149
pjsip_multipart_part *part;
154
/* Check content-type: "multipart/mixed;boundary=12345" */
155
init_media_type(&mt, "multipart", "mixed", "12345");
156
if (pjsip_media_type_cmp(&body->content_type, &mt, 2) != 0)
160
"Content-Type: my/text\r\n"
162
"Header and body\r\n"
164
part = pjsip_multipart_get_first_part(body);
167
if (verify_part(part, "my", "text", NULL, -1, "Header and body"))
171
"Content-Type: hello/world\r\n"
172
"Content-Length: 0\r\n"
175
part = pjsip_multipart_get_next_part(body, part);
178
if ((rc=verify_part(part, "hello", "world", NULL, 0, ""))!=0) {
179
PJ_LOG(3,(THIS_FILE, " err: verify_part rc=%d", rc));
187
part = pjsip_multipart_get_next_part(body, part);
190
if (verify_part(part, NULL, NULL, NULL, -1, "Body only"))
194
"Content-Type: multipart/mixed;boundary=6789\r\n"
196
"Prolog of the subbody, should be ignored\r\n"
201
"Epilogue of the subbody, should be ignored\r\n"
204
part = pjsip_multipart_get_next_part(body, part);
207
if ((rc=verify_part(part, "multipart", "mixed", "6789", -1,
208
"Prolog of the subbody, should be ignored\r\n"
213
"Epilogue of the subbody, should be ignored"))!=0) {
214
PJ_LOG(3,(THIS_FILE, " err: verify_part rc=%d", rc));
221
static int parse_test(void)
225
for (i=0; i<PJ_ARRAY_SIZE(p_tests); ++i) {
227
pjsip_media_type ctype;
228
pjsip_msg_body *body;
232
pool = pjsip_endpt_create_pool(endpt, NULL, 512, 512);
234
init_media_type(&ctype, p_tests[i].ctype, p_tests[i].csubtype,
235
p_tests[i].boundary);
237
pj_strdup2_with_null(pool, &str, p_tests[i].msg);
238
body = pjsip_multipart_parse(pool, str.ptr, str.slen, &ctype, 0);
242
if (p_tests[i].verify) {
243
rc = p_tests[i].verify(pool, body);
248
pj_pool_release(pool);
256
int multipart_test(void)