summaryrefslogtreecommitdiff
path: root/pjsip/src/test/multipart_test.c
blob: 4f16e68bfda3d7694508052ed0cf4403e2b02d38 (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
/* $Id$ */
/* 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#include "test.h"
#include <pjsip.h>
#include <pjlib.h>

#define THIS_FILE	""

/*
 * multipart tests
 */
typedef pj_status_t (*verify_ptr)(pj_pool_t*,pjsip_msg_body*);

static pj_status_t verify1(pj_pool_t *pool, pjsip_msg_body *body);

static struct test_t
{
    char *ctype;
    char *csubtype;
    char *boundary;
    const char *msg;
    verify_ptr  verify;
} p_tests[] =
{
	{
		/* Content-type */
		"multipart", "mixed", "12345",

		/* Body: */
		"This is the prolog, which should be ignored.\r\n"
		"--12345\r\n"
		"Content-Type: my/text\r\n"
		"\r\n"
		"Header and body\r\n"
		"--12345 \t\r\n"
		"Content-Type: hello/world\r\n"
		"Content-Length: 0\r\n"
		"\r\n"
		"--12345\r\n"
		"\r\n"
		"Body only\r\n"
		"--12345\r\n"
		"Content-Type: multipart/mixed;boundary=6789\r\n"
		"\r\n"
		"Prolog of the subbody, should be ignored\r\n"
		"--6789\r\n"
		"\r\n"
		"Subbody\r\n"
		"--6789--\r\n"
		"Epilogue of the subbody, should be ignored\r\n"
		"--12345--\r\n"
		"This is epilogue, which should be ignored too",

		&verify1
	}
};

static void init_media_type(pjsip_media_type *mt,
			    char *type, char *subtype, char *boundary)
{
    static pjsip_param prm;

    pjsip_media_type_init(mt, NULL, NULL);
    if (type) mt->type = pj_str(type);
    if (subtype) mt->subtype = pj_str(subtype);
    if (boundary) {
	pj_list_init(&prm);
	prm.name = pj_str("boundary");
	prm.value = pj_str(boundary);
	pj_list_push_back(&mt->param, &prm);
    }
}

static int verify_part(pjsip_multipart_part *part,
		       char *h_content_type,
		       char *h_content_subtype,
		       char *boundary,
		       int h_content_length,
		       const char *body)
{
    pjsip_ctype_hdr *ctype_hdr = NULL;
    pjsip_clen_hdr *clen_hdr = NULL;
    pjsip_hdr *hdr;
    pj_str_t the_body;

    hdr = part->hdr.next;
    while (hdr != &part->hdr) {
	if (hdr->type == PJSIP_H_CONTENT_TYPE)
	    ctype_hdr = (pjsip_ctype_hdr*)hdr;
	else if (hdr->type == PJSIP_H_CONTENT_LENGTH)
	    clen_hdr = (pjsip_clen_hdr*)hdr;
	hdr = hdr->next;
    }

    if (h_content_type) {
	pjsip_media_type mt;

	if (ctype_hdr == NULL)
	    return -10;

	init_media_type(&mt, h_content_type, h_content_subtype, boundary);

	if (pjsip_media_type_cmp(&ctype_hdr->media, &mt, 2) != 0)
	    return -20;

    } else {
	if (ctype_hdr)
	    return -30;
    }

    if (h_content_length >= 0) {
	if (clen_hdr == NULL)
	    return -50;
	if (clen_hdr->len != h_content_length)
	    return -60;
    } else {
	if (clen_hdr)
	    return -70;
    }

    the_body.ptr = (char*)part->body->data;
    the_body.slen = part->body->len;

    if (pj_strcmp2(&the_body, body) != 0)
	return -90;

    return 0;
}

static pj_status_t verify1(pj_pool_t *pool, pjsip_msg_body *body)
{
    pjsip_media_type mt;
    pjsip_multipart_part *part;
    int rc;

    PJ_UNUSED_ARG(pool);

    /* Check content-type: "multipart/mixed;boundary=12345" */
    init_media_type(&mt, "multipart", "mixed", "12345");
    if (pjsip_media_type_cmp(&body->content_type, &mt, 2) != 0)
	return -200;

    /* First part:
		"Content-Type: my/text\r\n"
		"\r\n"
		"Header and body\r\n"
     */
    part = pjsip_multipart_get_first_part(body);
    if (!part)
	return -210;
    if (verify_part(part, "my", "text", NULL, -1, "Header and body"))
	return -220;

    /* Next part:
		"Content-Type: hello/world\r\n"
		"Content-Length: 0\r\n"
		"\r\n"
     */
    part = pjsip_multipart_get_next_part(body, part);
    if (!part)
	return -230;
    if ((rc=verify_part(part, "hello", "world", NULL, 0, ""))!=0) {
	PJ_LOG(3,(THIS_FILE, "   err: verify_part rc=%d", rc));
	return -240;
    }

    /* Next part:
		"\r\n"
		"Body only\r\n"
     */
    part = pjsip_multipart_get_next_part(body, part);
    if (!part)
	return -260;
    if (verify_part(part, NULL, NULL, NULL, -1, "Body only"))
	return -270;

    /* Next part:
		"Content-Type: multipart/mixed;boundary=6789\r\n"
		"\r\n"
		"Prolog of the subbody, should be ignored\r\n"
		"--6789\r\n"
		"\r\n"
		"Subbody\r\n"
		"--6789--\r\n"
		"Epilogue of the subbody, should be ignored\r\n"

     */
    part = pjsip_multipart_get_next_part(body, part);
    if (!part)
	return -280;
    if ((rc=verify_part(part, "multipart", "mixed", "6789", -1,
	        "Prolog of the subbody, should be ignored\r\n"
		"--6789\r\n"
		"\r\n"
		"Subbody\r\n"
		"--6789--\r\n"
		"Epilogue of the subbody, should be ignored"))!=0) {
	PJ_LOG(3,(THIS_FILE, "   err: verify_part rc=%d", rc));
	return -290;
    }

    return 0;
}

static int parse_test(void)
{
    unsigned i;

    for (i=0; i<PJ_ARRAY_SIZE(p_tests); ++i) {
	pj_pool_t *pool;
	pjsip_media_type ctype;
	pjsip_msg_body *body;
	pj_str_t str;
	int rc;

	pool = pjsip_endpt_create_pool(endpt, NULL, 512, 512);

	init_media_type(&ctype, p_tests[i].ctype, p_tests[i].csubtype,
			p_tests[i].boundary);

	pj_strdup2_with_null(pool, &str, p_tests[i].msg);
	body = pjsip_multipart_parse(pool, str.ptr, str.slen, &ctype, 0);
	if (!body)
	    return -100;

	if (p_tests[i].verify) {
	    rc = p_tests[i].verify(pool, body);
	} else {
	    rc = 0;
	}

	pj_pool_release(pool);
	if (rc)
	    return rc;
    }

    return 0;
}

int multipart_test(void)
{
    int rc;

    rc = parse_test();
    if (rc)
	return rc;

    return rc;
}