blob: a7f0fb0ba8cfff18ab3cd1c853d08190a4211309 [file] [log] [blame]
Benny Prijono0ca04b62005-12-30 23:50:15 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
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.
9 *
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.
14 *
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
18 */
19
20#include "test.h"
Benny Prijono40f2f642006-01-30 18:40:05 +000021#include <pjsip.h>
Benny Prijono0ca04b62005-12-30 23:50:15 +000022#include <pjlib.h>
23
Benny Prijono0ca04b62005-12-30 23:50:15 +000024
Benny Prijono85598d92006-01-07 18:44:25 +000025#define THIS_FILE "txdata_test.c"
26
27
Benny Prijonoe93e2872006-06-28 16:46:49 +000028#define HFIND(msg,h,H) ((pjsip_##h##_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_##H, NULL))
29
30#if defined(PJ_DEBUG) && PJ_DEBUG!=0
31# define LOOP 10000
32#else
33# define LOOP 100000
34#endif
35
36
Benny Prijono0ca04b62005-12-30 23:50:15 +000037/*
38 * This tests various core message creation functions.
39 */
Benny Prijono0c2bc612006-01-10 13:31:40 +000040static int core_txdata_test(void)
Benny Prijono0ca04b62005-12-30 23:50:15 +000041{
42 pj_status_t status;
43 pj_str_t target, from, to, contact, body;
44 pjsip_rx_data dummy_rdata;
45 pjsip_tx_data *invite, *invite2, *cancel, *response, *ack;
46
Benny Prijono0c2bc612006-01-10 13:31:40 +000047 PJ_LOG(3,(THIS_FILE, " core transmit data test"));
48
Benny Prijono0ca04b62005-12-30 23:50:15 +000049 /* Create INVITE request. */
50 target = pj_str("tel:+1");
51 from = pj_str("tel:+0");
52 to = pj_str("tel:+1");
53 contact = pj_str("Bob <sip:+0@example.com;user=phone>");
54 body = pj_str("Hello world!");
55
56 status = pjsip_endpt_create_request( endpt, &pjsip_invite_method, &target,
57 &from, &to, &contact, NULL, 10, &body,
58 &invite);
59 if (status != PJ_SUCCESS) {
60 app_perror(" error: unable to create request", status);
61 return -10;
62 }
63
64 /* Buffer must be invalid. */
65 if (pjsip_tx_data_is_valid(invite) != 0) {
Benny Prijono85598d92006-01-07 18:44:25 +000066 PJ_LOG(3,(THIS_FILE, " error: buffer must be invalid"));
Benny Prijono0ca04b62005-12-30 23:50:15 +000067 return -14;
68 }
69 /* Reference counter must be set to 1. */
70 if (pj_atomic_get(invite->ref_cnt) != 1) {
Benny Prijono85598d92006-01-07 18:44:25 +000071 PJ_LOG(3,(THIS_FILE, " error: invalid reference counter"));
Benny Prijono0ca04b62005-12-30 23:50:15 +000072 return -15;
73 }
74 /* Check message type. */
75 if (invite->msg->type != PJSIP_REQUEST_MSG)
76 return -16;
77 /* Check method. */
78 if (invite->msg->line.req.method.id != PJSIP_INVITE_METHOD)
79 return -17;
80
81 /* Check that mandatory headers are present. */
82 if (HFIND(invite->msg, from, FROM) == 0)
83 return -20;
84 if (HFIND(invite->msg, to, TO) == 0)
85 return -21;
86 if (HFIND(invite->msg, contact, CONTACT) == 0)
87 return -22;
88 if (HFIND(invite->msg, cid, CALL_ID) == 0)
89 return -23;
90 if (HFIND(invite->msg, cseq, CSEQ) == 0)
91 return -24;
92 do {
93 pjsip_via_hdr *via = HFIND(invite->msg, via, VIA);
94 if (via == NULL)
95 return -25;
96 /* Branch param must be empty. */
97 if (via->branch_param.slen != 0)
98 return -26;
99 } while (0);
100 if (invite->msg->body == NULL)
101 return -28;
102
103 /* Create another INVITE request from first request. */
104 status = pjsip_endpt_create_request_from_hdr( endpt, &pjsip_invite_method,
105 invite->msg->line.req.uri,
106 HFIND(invite->msg,from,FROM),
107 HFIND(invite->msg,to,TO),
108 HFIND(invite->msg,contact,CONTACT),
109 HFIND(invite->msg,cid,CALL_ID),
110 10, &body, &invite2);
111 if (status != PJ_SUCCESS) {
112 app_perror(" error: create second request failed", status);
113 return -30;
114 }
115
116 /* Buffer must be invalid. */
117 if (pjsip_tx_data_is_valid(invite2) != 0) {
Benny Prijono85598d92006-01-07 18:44:25 +0000118 PJ_LOG(3,(THIS_FILE, " error: buffer must be invalid"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000119 return -34;
120 }
121 /* Reference counter must be set to 1. */
122 if (pj_atomic_get(invite2->ref_cnt) != 1) {
Benny Prijono85598d92006-01-07 18:44:25 +0000123 PJ_LOG(3,(THIS_FILE, " error: invalid reference counter"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000124 return -35;
125 }
126 /* Check message type. */
127 if (invite2->msg->type != PJSIP_REQUEST_MSG)
128 return -36;
129 /* Check method. */
130 if (invite2->msg->line.req.method.id != PJSIP_INVITE_METHOD)
131 return -37;
132
133 /* Check that mandatory headers are again present. */
134 if (HFIND(invite2->msg, from, FROM) == 0)
135 return -40;
136 if (HFIND(invite2->msg, to, TO) == 0)
137 return -41;
138 if (HFIND(invite2->msg, contact, CONTACT) == 0)
139 return -42;
140 if (HFIND(invite2->msg, cid, CALL_ID) == 0)
141 return -43;
142 if (HFIND(invite2->msg, cseq, CSEQ) == 0)
143 return -44;
144 if (HFIND(invite2->msg, via, VIA) == 0)
145 return -45;
146 /*
147 if (HFIND(invite2->msg, ctype, CONTENT_TYPE) == 0)
148 return -46;
149 if (HFIND(invite2->msg, clen, CONTENT_LENGTH) == 0)
150 return -47;
151 */
152 if (invite2->msg->body == NULL)
153 return -48;
154
155 /* Done checking invite2. We can delete this. */
156 if (pjsip_tx_data_dec_ref(invite2) != PJSIP_EBUFDESTROYED) {
Benny Prijono85598d92006-01-07 18:44:25 +0000157 PJ_LOG(3,(THIS_FILE, " error: request buffer not destroyed!"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000158 return -49;
159 }
160
161 /* Initialize dummy rdata (to simulate receiving a request)
162 * We should never do this in real application, as there are many
163 * many more fields need to be initialized!!
164 */
Benny Prijono728a9052006-01-18 23:34:15 +0000165 dummy_rdata.msg_info.cid = HFIND(invite->msg, cid, CALL_ID);
Benny Prijono0ca04b62005-12-30 23:50:15 +0000166 dummy_rdata.msg_info.clen = NULL;
167 dummy_rdata.msg_info.cseq = HFIND(invite->msg, cseq, CSEQ);
168 dummy_rdata.msg_info.ctype = NULL;
169 dummy_rdata.msg_info.from = HFIND(invite->msg, from, FROM);
170 dummy_rdata.msg_info.max_fwd = NULL;
171 dummy_rdata.msg_info.msg = invite->msg;
172 dummy_rdata.msg_info.record_route = NULL;
173 dummy_rdata.msg_info.require = NULL;
174 dummy_rdata.msg_info.route = NULL;
175 dummy_rdata.msg_info.to = HFIND(invite->msg, to, TO);
176 dummy_rdata.msg_info.via = HFIND(invite->msg, via, VIA);
177
178 /* Create a response message for the request. */
179 status = pjsip_endpt_create_response( endpt, &dummy_rdata, 301, NULL,
180 &response);
181 if (status != PJ_SUCCESS) {
182 app_perror(" error: unable to create response", status);
183 return -50;
184 }
185
186 /* Buffer must be invalid. */
187 if (pjsip_tx_data_is_valid(response) != 0) {
Benny Prijono85598d92006-01-07 18:44:25 +0000188 PJ_LOG(3,(THIS_FILE, " error: buffer must be invalid"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000189 return -54;
190 }
191 /* Check reference counter. */
192 if (pj_atomic_get(response->ref_cnt) != 1) {
Benny Prijono85598d92006-01-07 18:44:25 +0000193 PJ_LOG(3,(THIS_FILE, " error: invalid ref count in response"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000194 return -55;
195 }
196 /* Check message type. */
197 if (response->msg->type != PJSIP_RESPONSE_MSG)
198 return -56;
199 /* Check correct status is set. */
200 if (response->msg->line.status.code != 301)
201 return -57;
202
203 /* Check that mandatory headers are again present. */
204 if (HFIND(response->msg, from, FROM) == 0)
205 return -60;
206 if (HFIND(response->msg, to, TO) == 0)
207 return -61;
208 /*
209 if (HFIND(response->msg, contact, CONTACT) == 0)
210 return -62;
211 */
212 if (HFIND(response->msg, cid, CALL_ID) == 0)
213 return -63;
214 if (HFIND(response->msg, cseq, CSEQ) == 0)
215 return -64;
216 if (HFIND(response->msg, via, VIA) == 0)
217 return -65;
218
219 /* This response message will be used later when creating ACK */
220
221 /* Create CANCEL request for the original request. */
222 status = pjsip_endpt_create_cancel( endpt, invite, &cancel);
223 if (status != PJ_SUCCESS) {
224 app_perror(" error: unable to create CANCEL request", status);
225 return -80;
226 }
227
228 /* Buffer must be invalid. */
229 if (pjsip_tx_data_is_valid(cancel) != 0) {
Benny Prijono85598d92006-01-07 18:44:25 +0000230 PJ_LOG(3,(THIS_FILE, " error: buffer must be invalid"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000231 return -84;
232 }
233 /* Check reference counter. */
234 if (pj_atomic_get(cancel->ref_cnt) != 1) {
Benny Prijono85598d92006-01-07 18:44:25 +0000235 PJ_LOG(3,(THIS_FILE, " error: invalid ref count in CANCEL request"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000236 return -85;
237 }
238 /* Check message type. */
239 if (cancel->msg->type != PJSIP_REQUEST_MSG)
240 return -86;
241 /* Check method. */
242 if (cancel->msg->line.req.method.id != PJSIP_CANCEL_METHOD)
243 return -87;
244
245 /* Check that mandatory headers are again present. */
246 if (HFIND(cancel->msg, from, FROM) == 0)
247 return -90;
248 if (HFIND(cancel->msg, to, TO) == 0)
249 return -91;
250 /*
251 if (HFIND(cancel->msg, contact, CONTACT) == 0)
252 return -92;
253 */
254 if (HFIND(cancel->msg, cid, CALL_ID) == 0)
255 return -93;
256 if (HFIND(cancel->msg, cseq, CSEQ) == 0)
257 return -94;
258 if (HFIND(cancel->msg, via, VIA) == 0)
259 return -95;
260
261 /* Done checking CANCEL request. */
262 if (pjsip_tx_data_dec_ref(cancel) != PJSIP_EBUFDESTROYED) {
Benny Prijono85598d92006-01-07 18:44:25 +0000263 PJ_LOG(3,(THIS_FILE, " error: response buffer not destroyed!"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000264 return -99;
265 }
266
267 /* Modify dummy_rdata to simulate receiving response. */
268 pj_memset(&dummy_rdata, 0, sizeof(dummy_rdata));
269 dummy_rdata.msg_info.msg = response->msg;
270 dummy_rdata.msg_info.to = HFIND(response->msg, to, TO);
271
272 /* Create ACK request */
273 status = pjsip_endpt_create_ack( endpt, invite, &dummy_rdata, &ack );
274 if (status != PJ_SUCCESS) {
Benny Prijono85598d92006-01-07 18:44:25 +0000275 PJ_LOG(3,(THIS_FILE, " error: unable to create ACK"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000276 return -100;
277 }
278 /* Buffer must be invalid. */
279 if (pjsip_tx_data_is_valid(ack) != 0) {
Benny Prijono85598d92006-01-07 18:44:25 +0000280 PJ_LOG(3,(THIS_FILE, " error: buffer must be invalid"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000281 return -104;
282 }
283 /* Check reference counter. */
284 if (pj_atomic_get(ack->ref_cnt) != 1) {
Benny Prijono85598d92006-01-07 18:44:25 +0000285 PJ_LOG(3,(THIS_FILE, " error: invalid ref count in ACK request"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000286 return -105;
287 }
288 /* Check message type. */
289 if (ack->msg->type != PJSIP_REQUEST_MSG)
290 return -106;
291 /* Check method. */
292 if (ack->msg->line.req.method.id != PJSIP_ACK_METHOD)
293 return -107;
294 /* Check Request-URI is present. */
295 if (ack->msg->line.req.uri == NULL)
296 return -108;
297
298 /* Check that mandatory headers are again present. */
299 if (HFIND(ack->msg, from, FROM) == 0)
300 return -110;
301 if (HFIND(ack->msg, to, TO) == 0)
302 return -111;
303 if (HFIND(ack->msg, cid, CALL_ID) == 0)
304 return -112;
305 if (HFIND(ack->msg, cseq, CSEQ) == 0)
306 return -113;
307 if (HFIND(ack->msg, via, VIA) == 0)
308 return -114;
309 if (ack->msg->body != NULL)
310 return -115;
311
312 /* Done checking invite message. */
313 if (pjsip_tx_data_dec_ref(invite) != PJSIP_EBUFDESTROYED) {
Benny Prijono85598d92006-01-07 18:44:25 +0000314 PJ_LOG(3,(THIS_FILE, " error: response buffer not destroyed!"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000315 return -120;
316 }
317
318 /* Done checking response message. */
319 if (pjsip_tx_data_dec_ref(response) != PJSIP_EBUFDESTROYED) {
Benny Prijono85598d92006-01-07 18:44:25 +0000320 PJ_LOG(3,(THIS_FILE, " error: response buffer not destroyed!"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000321 return -130;
322 }
323
324 /* Done checking ack message. */
325 if (pjsip_tx_data_dec_ref(ack) != PJSIP_EBUFDESTROYED) {
Benny Prijono85598d92006-01-07 18:44:25 +0000326 PJ_LOG(3,(THIS_FILE, " error: response buffer not destroyed!"));
Benny Prijono0ca04b62005-12-30 23:50:15 +0000327 return -140;
328 }
329
330 /* Done. */
331 return 0;
332}
333
Benny Prijonoe93e2872006-06-28 16:46:49 +0000334
335
336/*
337 * This test demonstrate the bug as reported in:
338 * http://bugzilla.pjproject.net/show_bug.cgi?id=49
Benny Prijono0c2bc612006-01-10 13:31:40 +0000339 */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000340static int gcc_test()
Benny Prijono0c2bc612006-01-10 13:31:40 +0000341{
342 char msgbuf[512];
343 pj_str_t target = pj_str("sip:alice@wonderland:5061;x-param=param%201"
344 "?X-Hdr-1=Header%201"
345 "&X-Empty-Hdr=");
Benny Prijono0c2bc612006-01-10 13:31:40 +0000346 pjsip_tx_data *tdata;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000347 pjsip_parser_err_report err_list;
Benny Prijono0c2bc612006-01-10 13:31:40 +0000348 pjsip_msg *msg;
349 int len;
350 pj_status_t status;
351
352 PJ_LOG(3,(THIS_FILE, " header param in URI to create request"));
353
354 /* Create request with header param in target URI. */
355 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method, &target,
356 &target, &target, &target, NULL, -1,
357 NULL, &tdata);
358 if (status != 0) {
359 app_perror(" error: Unable to create request", status);
360 return -200;
361 }
362
363 /* Print and parse the request.
364 * We'll check that header params are not present in
365 */
366 len = pjsip_msg_print(tdata->msg, msgbuf, sizeof(msgbuf));
367 if (len < 1) {
368 PJ_LOG(3,(THIS_FILE, " error: printing message"));
369 pjsip_tx_data_dec_ref(tdata);
370 return -250;
371 }
372 msgbuf[len] = '\0';
373
374 PJ_LOG(5,(THIS_FILE, "%d bytes request created:--begin-msg--\n"
375 "%s\n"
376 "--end-msg--", len, msgbuf));
377
378 /* Now parse the message. */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000379 pj_list_init(&err_list);
380 msg = pjsip_parse_msg( tdata->pool, msgbuf, len, &err_list);
Benny Prijono0c2bc612006-01-10 13:31:40 +0000381 if (msg == NULL) {
Benny Prijonoe93e2872006-06-28 16:46:49 +0000382 pjsip_parser_err_report *e;
383
384 PJ_LOG(3,(THIS_FILE, " error: parsing message message"));
385
386 e = err_list.next;
387 while (e != &err_list) {
388 PJ_LOG(3,(THIS_FILE, " %s in line %d col %d hname=%.*s",
389 pj_exception_id_name(e->except_code),
390 e->line, e->col+1,
391 (int)e->hname.slen,
392 e->hname.ptr));
393 e = e->next;
394 }
395
396 pjsip_tx_data_dec_ref(tdata);
397 return -255;
398 }
399
400 pjsip_tx_data_dec_ref(tdata);
401 return 0;
402}
403
404
405/* This tests the request creating functions against the following
406 * requirements:
407 * - header params in URI creates header in the request.
408 * - method and headers params are correctly shown or hidden in
409 * request URI, From, To, and Contact header.
410 */
411static int txdata_test_uri_params(void)
412{
413 char msgbuf[512];
414 pj_str_t target = pj_str("sip:alice@wonderland:5061;x-param=param%201"
415 "?X-Hdr-1=Header%201"
416 "&X-Empty-Hdr=");
417 pj_str_t pname = pj_str("x-param");
418 pj_str_t hname = pj_str("X-Hdr-1");
419 pj_str_t hemptyname = pj_str("X-Empty-Hdr");
420 pjsip_from_hdr *from_hdr;
421 pjsip_to_hdr *to_hdr;
422 pjsip_contact_hdr *contact_hdr;
423 pjsip_generic_string_hdr *hdr;
424 pjsip_tx_data *tdata;
425 pjsip_sip_uri *uri;
426 pjsip_param *param;
427 pjsip_via_hdr *via;
428 pjsip_parser_err_report err_list;
429 pjsip_msg *msg;
430 int len;
431 pj_status_t status;
432
433 PJ_LOG(3,(THIS_FILE, " header param in URI to create request"));
434
435 /* Create request with header param in target URI. */
436 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method, &target,
437 &target, &target, &target, NULL, -1,
438 NULL, &tdata);
439 if (status != 0) {
440 app_perror(" error: Unable to create request", status);
441 return -200;
442 }
443
444 /* Fill up the Via header to prevent syntax error on parsing */
445 via = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
446 via->transport = pj_str("TCP");
447 via->sent_by.host = pj_str("127.0.0.1");
448
449 /* Print and parse the request.
450 * We'll check that header params are not present in
451 */
452 len = pjsip_msg_print(tdata->msg, msgbuf, sizeof(msgbuf));
453 if (len < 1) {
454 PJ_LOG(3,(THIS_FILE, " error: printing message"));
Benny Prijono0c2bc612006-01-10 13:31:40 +0000455 pjsip_tx_data_dec_ref(tdata);
456 return -250;
457 }
Benny Prijonoe93e2872006-06-28 16:46:49 +0000458 msgbuf[len] = '\0';
459
460 PJ_LOG(5,(THIS_FILE, "%d bytes request created:--begin-msg--\n"
461 "%s\n"
462 "--end-msg--", len, msgbuf));
463
464 /* Now parse the message. */
465 pj_list_init(&err_list);
466 msg = pjsip_parse_msg( tdata->pool, msgbuf, len, &err_list);
467 if (msg == NULL) {
468 pjsip_parser_err_report *e;
469
470 PJ_LOG(3,(THIS_FILE, " error: parsing message message"));
471
472 e = err_list.next;
473 while (e != &err_list) {
474 PJ_LOG(3,(THIS_FILE, " %s in line %d col %d hname=%.*s",
475 pj_exception_id_name(e->except_code),
476 e->line, e->col+1,
477 (int)e->hname.slen,
478 e->hname.ptr));
479 e = e->next;
480 }
481
482 pjsip_tx_data_dec_ref(tdata);
483 return -256;
484 }
Benny Prijono0c2bc612006-01-10 13:31:40 +0000485
486 /* Check the existence of port, other_param, and header param.
487 * Port is now allowed in To and From header.
488 */
489 /* Port in request URI. */
490 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(msg->line.req.uri);
491 if (uri->port != 5061) {
492 PJ_LOG(3,(THIS_FILE, " error: port not present in request URI"));
493 pjsip_tx_data_dec_ref(tdata);
494 return -260;
495 }
496 /* other_param in request_uri */
497 param = pjsip_param_find(&uri->other_param, &pname);
498 if (param == NULL || pj_strcmp2(&param->value, "param 1") != 0) {
499 PJ_LOG(3,(THIS_FILE, " error: x-param not present in request URI"));
500 pjsip_tx_data_dec_ref(tdata);
501 return -261;
502 }
503 /* header param in request uri. */
504 if (!pj_list_empty(&uri->header_param)) {
505 PJ_LOG(3,(THIS_FILE, " error: hparam in request URI"));
506 pjsip_tx_data_dec_ref(tdata);
507 return -262;
508 }
509
510 /* Port in From header. */
511 from_hdr = (pjsip_from_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_FROM, NULL);
512 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(from_hdr->uri);
513 if (uri->port != 0) {
514 PJ_LOG(3,(THIS_FILE, " error: port most not exist in From header"));
515 pjsip_tx_data_dec_ref(tdata);
516 return -270;
517 }
518 /* other_param in From header */
519 param = pjsip_param_find(&uri->other_param, &pname);
520 if (param == NULL || pj_strcmp2(&param->value, "param 1") != 0) {
521 PJ_LOG(3,(THIS_FILE, " error: x-param not present in From header"));
522 pjsip_tx_data_dec_ref(tdata);
523 return -271;
524 }
525 /* header param in From header. */
526 if (!pj_list_empty(&uri->header_param)) {
527 PJ_LOG(3,(THIS_FILE, " error: hparam in From header"));
528 pjsip_tx_data_dec_ref(tdata);
529 return -272;
530 }
531
532
533 /* Port in To header. */
534 to_hdr = (pjsip_to_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_TO, NULL);
535 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(to_hdr->uri);
536 if (uri->port != 0) {
537 PJ_LOG(3,(THIS_FILE, " error: port most not exist in To header"));
538 pjsip_tx_data_dec_ref(tdata);
539 return -280;
540 }
541 /* other_param in To header */
542 param = pjsip_param_find(&uri->other_param, &pname);
543 if (param == NULL || pj_strcmp2(&param->value, "param 1") != 0) {
544 PJ_LOG(3,(THIS_FILE, " error: x-param not present in To header"));
545 pjsip_tx_data_dec_ref(tdata);
546 return -281;
547 }
548 /* header param in From header. */
549 if (!pj_list_empty(&uri->header_param)) {
550 PJ_LOG(3,(THIS_FILE, " error: hparam in To header"));
551 pjsip_tx_data_dec_ref(tdata);
552 return -282;
553 }
554
555
556
557 /* Port in Contact header. */
558 contact_hdr = (pjsip_contact_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, NULL);
559 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(contact_hdr->uri);
560 if (uri->port != 5061) {
561 PJ_LOG(3,(THIS_FILE, " error: port not present in Contact header"));
562 pjsip_tx_data_dec_ref(tdata);
563 return -290;
564 }
565 /* other_param in Contact header */
566 param = pjsip_param_find(&uri->other_param, &pname);
567 if (param == NULL || pj_strcmp2(&param->value, "param 1") != 0) {
568 PJ_LOG(3,(THIS_FILE, " error: x-param not present in Contact header"));
569 pjsip_tx_data_dec_ref(tdata);
570 return -291;
571 }
572 /* header param in Contact header. */
573 if (pj_list_empty(&uri->header_param)) {
574 PJ_LOG(3,(THIS_FILE, " error: hparam is missing in Contact header"));
575 pjsip_tx_data_dec_ref(tdata);
576 return -292;
577 }
578 /* Check for X-Hdr-1 */
579 param = pjsip_param_find(&uri->header_param, &hname);
580 if (param == NULL || pj_strcmp2(&param->value, "Header 1")!=0) {
581 PJ_LOG(3,(THIS_FILE, " error: hparam is missing in Contact header"));
582 pjsip_tx_data_dec_ref(tdata);
583 return -293;
584 }
585 /* Check for X-Empty-Hdr */
586 param = pjsip_param_find(&uri->header_param, &hemptyname);
587 if (param == NULL || pj_strcmp2(&param->value, "")!=0) {
588 PJ_LOG(3,(THIS_FILE, " error: hparam is missing in Contact header"));
589 pjsip_tx_data_dec_ref(tdata);
590 return -294;
591 }
592
593
594 /* Check that headers are present in the request. */
595 hdr = (pjsip_generic_string_hdr*)
596 pjsip_msg_find_hdr_by_name(msg, &hname, NULL);
597 if (hdr == NULL || pj_strcmp2(&hdr->hvalue, "Header 1")!=0) {
598 PJ_LOG(3,(THIS_FILE, " error: header X-Hdr-1 not created"));
599 pjsip_tx_data_dec_ref(tdata);
600 return -300;
601 }
602
603 hdr = (pjsip_generic_string_hdr*)
604 pjsip_msg_find_hdr_by_name(msg, &hemptyname, NULL);
605 if (hdr == NULL || pj_strcmp2(&param->value, "")!=0) {
606 PJ_LOG(3,(THIS_FILE, " error: header X-Empty-Hdr not created"));
607 pjsip_tx_data_dec_ref(tdata);
608 return -330;
609 }
610
611 pjsip_tx_data_dec_ref(tdata);
612 return 0;
613}
614
Benny Prijonoe93e2872006-06-28 16:46:49 +0000615
616/*
617 * create request benchmark
618 */
619static int create_request_bench(pj_timestamp *p_elapsed)
620{
621 enum { COUNT = 100 };
622 unsigned i, j;
623 pjsip_tx_data *tdata[COUNT];
624 pj_timestamp t1, t2, elapsed;
625 pj_status_t status;
626
627 pj_str_t str_target = pj_str("sip:someuser@someprovider.com");
628 pj_str_t str_from = pj_str("\"Local User\" <sip:localuser@serviceprovider.com>");
629 pj_str_t str_to = pj_str("\"Remote User\" <sip:remoteuser@serviceprovider.com>");
630 pj_str_t str_contact = str_from;
631
632 elapsed.u64 = 0;
633
634 for (i=0; i<LOOP; i+=COUNT) {
635 pj_memset(tdata, 0, sizeof(tdata));
636
637 pj_get_timestamp(&t1);
638
639 for (j=0; j<COUNT; ++j) {
640 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method,
641 &str_target, &str_from, &str_to,
642 &str_contact, NULL, -1, NULL,
643 &tdata[j]);
644 if (status != PJ_SUCCESS) {
645 app_perror(" error: unable to create request", status);
646 goto on_error;
647 }
648 }
649
650 pj_get_timestamp(&t2);
651 pj_sub_timestamp(&t2, &t1);
652 pj_add_timestamp(&elapsed, &t2);
653
654 for (j=0; j<COUNT; ++j)
655 pjsip_tx_data_dec_ref(tdata[j]);
656 }
657
658 p_elapsed->u64 = elapsed.u64;
659 return PJ_SUCCESS;
660
661on_error:
662 for (i=0; i<COUNT; ++i) {
663 if (tdata[i])
664 pjsip_tx_data_dec_ref(tdata[i]);
665 }
666 return -400;
667}
668
669
670
671/*
672 * create response benchmark
673 */
674static int create_response_bench(pj_timestamp *p_elapsed)
675{
676 enum { COUNT = 100 };
677 unsigned i, j;
678 pjsip_via_hdr *via;
679 pjsip_rx_data rdata;
680 pjsip_tx_data *request;
681 pjsip_tx_data *tdata[COUNT];
682 pj_timestamp t1, t2, elapsed;
683 pj_status_t status;
684
685 /* Create the request first. */
686 pj_str_t str_target = pj_str("sip:someuser@someprovider.com");
687 pj_str_t str_from = pj_str("\"Local User\" <sip:localuser@serviceprovider.com>");
688 pj_str_t str_to = pj_str("\"Remote User\" <sip:remoteuser@serviceprovider.com>");
689 pj_str_t str_contact = str_from;
690
691 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method,
692 &str_target, &str_from, &str_to,
693 &str_contact, NULL, -1, NULL,
694 &request);
695 if (status != PJ_SUCCESS) {
696 app_perror(" error: unable to create request", status);
697 return status;
698 }
699
700 /* Create several Via headers */
701 via = pjsip_via_hdr_create(request->pool);
702 via->sent_by.host = pj_str("192.168.0.7");
703 via->sent_by.port = 5061;
704 via->transport = pj_str("udp");
705 via->rport_param = 0;
706 via->branch_param = pj_str("012345678901234567890123456789");
707 via->recvd_param = pj_str("192.168.0.7");
708 pjsip_msg_insert_first_hdr(request->msg, pjsip_hdr_clone(request->pool, via));
709 pjsip_msg_insert_first_hdr(request->msg, pjsip_hdr_clone(request->pool, via));
710 pjsip_msg_insert_first_hdr(request->msg, (pjsip_hdr*)via);
711
712
713 /* Create "dummy" rdata from the tdata */
714 pj_memset(&rdata, 0, sizeof(pjsip_rx_data));
715 rdata.tp_info.pool = request->pool;
716 rdata.msg_info.msg = request->msg;
717 rdata.msg_info.from = pjsip_msg_find_hdr(request->msg, PJSIP_H_FROM, NULL);
718 rdata.msg_info.to = pjsip_msg_find_hdr(request->msg, PJSIP_H_TO, NULL);
719 rdata.msg_info.cseq = pjsip_msg_find_hdr(request->msg, PJSIP_H_CSEQ, NULL);
720 rdata.msg_info.cid = pjsip_msg_find_hdr(request->msg, PJSIP_H_FROM, NULL);
721 rdata.msg_info.via = via;
722
723 /*
724 * Now benchmark create_response
725 */
726 elapsed.u64 = 0;
727
728 for (i=0; i<LOOP; i+=COUNT) {
729 pj_memset(tdata, 0, sizeof(tdata));
730
731 pj_get_timestamp(&t1);
732
733 for (j=0; j<COUNT; ++j) {
734 status = pjsip_endpt_create_response(endpt, &rdata, 200, NULL, &tdata[j]);
735 if (status != PJ_SUCCESS) {
736 app_perror(" error: unable to create request", status);
737 goto on_error;
738 }
739 }
740
741 pj_get_timestamp(&t2);
742 pj_sub_timestamp(&t2, &t1);
743 pj_add_timestamp(&elapsed, &t2);
744
745 for (j=0; j<COUNT; ++j)
746 pjsip_tx_data_dec_ref(tdata[j]);
747 }
748
749 p_elapsed->u64 = elapsed.u64;
750 pjsip_tx_data_dec_ref(request);
751 return PJ_SUCCESS;
752
753on_error:
754 for (i=0; i<COUNT; ++i) {
755 if (tdata[i])
756 pjsip_tx_data_dec_ref(tdata[i]);
757 }
758 return -400;
759}
760
761
Benny Prijono0c2bc612006-01-10 13:31:40 +0000762int txdata_test(void)
763{
Benny Prijonoe93e2872006-06-28 16:46:49 +0000764 enum { REPEAT = 4 };
765 unsigned i, msgs;
766 pj_timestamp usec[REPEAT], min, freq;
Benny Prijono0c2bc612006-01-10 13:31:40 +0000767 int status;
768
Benny Prijonoe93e2872006-06-28 16:46:49 +0000769 status = pj_get_timestamp_freq(&freq);
770 if (status != PJ_SUCCESS)
771 return status;
772
Benny Prijono0c2bc612006-01-10 13:31:40 +0000773 status = core_txdata_test();
774 if (status != 0)
775 return status;
776
Benny Prijonoe93e2872006-06-28 16:46:49 +0000777#if INCLUDE_GCC_TEST
778 status = gcc_test();
779 if (status != 0)
780 return status;
781#endif
Benny Prijono0c2bc612006-01-10 13:31:40 +0000782
783 status = txdata_test_uri_params();
784 if (status != 0)
785 return status;
786
Benny Prijonoe93e2872006-06-28 16:46:49 +0000787
788 /*
789 * Benchmark create_request()
790 */
791 PJ_LOG(3,(THIS_FILE, " benchmarking request creation:"));
792 for (i=0; i<REPEAT; ++i) {
793 PJ_LOG(3,(THIS_FILE, " test %d of %d..",
794 i+1, REPEAT));
795 status = create_request_bench(&usec[i]);
796 if (status != PJ_SUCCESS)
797 return status;
798 }
799
800 min.u64 = PJ_UINT64(0xFFFFFFFFFFFFFFF);
801 for (i=0; i<REPEAT; ++i) {
802 if (usec[i].u64 < min.u64) min.u64 = usec[i].u64;
803 }
804
805 msgs = (unsigned)(freq.u64 * LOOP / min.u64);
806
807 PJ_LOG(3,(THIS_FILE, " Requests created at %d requests/sec", msgs));
808
809 report_ival("create-request-per-sec",
810 msgs, "msg/sec",
811 "Number of typical request messages that can be created "
812 "per second with <tt>pjsip_endpt_create_request()</tt>");
813
814
815 /*
816 * Benchmark create_response()
817 */
818 PJ_LOG(3,(THIS_FILE, " benchmarking response creation:"));
819 for (i=0; i<REPEAT; ++i) {
820 PJ_LOG(3,(THIS_FILE, " test %d of %d..",
821 i+1, REPEAT));
822 status = create_response_bench(&usec[i]);
823 if (status != PJ_SUCCESS)
824 return status;
825 }
826
827 min.u64 = PJ_UINT64(0xFFFFFFFFFFFFFFF);
828 for (i=0; i<REPEAT; ++i) {
829 if (usec[i].u64 < min.u64) min.u64 = usec[i].u64;
830 }
831
832 msgs = (unsigned)(freq.u64 * LOOP / min.u64);
833
834 PJ_LOG(3,(THIS_FILE, " Responses created at %d responses/sec", msgs));
835
836 report_ival("create-response-per-sec",
837 msgs, "msg/sec",
838 "Number of typical response messages that can be created "
839 "per second with <tt>pjsip_endpt_create_response()</tt>");
840
841
Benny Prijono0c2bc612006-01-10 13:31:40 +0000842 return 0;
843}
Benny Prijonoe93e2872006-06-28 16:46:49 +0000844