Debian Codegraph @

Xpjproject/pjsip/src/test/txdata_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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"
21 #include <pjsip.h>
22 #include <pjlib.h>
23
24
25 #define THIS_FILE "txdata_test.c"
26
27
28 #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
37 /*
38 * This tests various core message creation functions.
39 */
40 static int core_txdata_test(void)
41 {
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
47 PJ_LOG(3,(THIS_FILE, " core transmit data test"));
48
49 /* 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:[email protected];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) {
66 PJ_LOG(3,(THIS_FILE, " error: buffer must be invalid"));
67 return -14;
68 }
69 /* Reference counter must be set to 1. */
70 if (pj_atomic_get(invite->ref_cnt) != 1) {
71 PJ_LOG(3,(THIS_FILE, " error: invalid reference counter"));
72 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) {
118 PJ_LOG(3,(THIS_FILE, " error: buffer must be invalid"));
119 return -34;
120 }
121 /* Reference counter must be set to 1. */
122 if (pj_atomic_get(invite2->ref_cnt) != 1) {
123 PJ_LOG(3,(THIS_FILE, " error: invalid reference counter"));
124 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) {
157 PJ_LOG(3,(THIS_FILE, " error: request buffer not destroyed!"));
158 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 */
165 dummy_rdata.msg_info.cid = HFIND(invite->msg, cid, CALL_ID);
166 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) {
188 PJ_LOG(3,(THIS_FILE, " error: buffer must be invalid"));
189 return -54;
190 }
191 /* Check reference counter. */
192 if (pj_atomic_get(response->ref_cnt) != 1) {
193 PJ_LOG(3,(THIS_FILE, " error: invalid ref count in response"));
194 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) {
230 PJ_LOG(3,(THIS_FILE, " error: buffer must be invalid"));
231 return -84;
232 }
233 /* Check reference counter. */
234 if (pj_atomic_get(cancel->ref_cnt) != 1) {
235 PJ_LOG(3,(THIS_FILE, " error: invalid ref count in CANCEL request"));
236 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) {
263 PJ_LOG(3,(THIS_FILE, " error: response buffer not destroyed!"));
264 return -99;
265 }
266
267 /* Modify dummy_rdata to simulate receiving response. */
268 pj_bzero(&dummy_rdata, 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) {
275 PJ_LOG(3,(THIS_FILE, " error: unable to create ACK"));
276 return -100;
277 }
278 /* Buffer must be invalid. */
279 if (pjsip_tx_data_is_valid(ack) != 0) {
280 PJ_LOG(3,(THIS_FILE, " error: buffer must be invalid"));
281 return -104;
282 }
283 /* Check reference counter. */
284 if (pj_atomic_get(ack->ref_cnt) != 1) {
285 PJ_LOG(3,(THIS_FILE, " error: invalid ref count in ACK request"));
286 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) {
314 PJ_LOG(3,(THIS_FILE, " error: response buffer not destroyed!"));
315 return -120;
316 }
317
318 /* Done checking response message. */
319 if (pjsip_tx_data_dec_ref(response) != PJSIP_EBUFDESTROYED) {
320 PJ_LOG(3,(THIS_FILE, " error: response buffer not destroyed!"));
321 return -130;
322 }
323
324 /* Done checking ack message. */
325 if (pjsip_tx_data_dec_ref(ack) != PJSIP_EBUFDESTROYED) {
326 PJ_LOG(3,(THIS_FILE, " error: response buffer not destroyed!"));
327 return -140;
328 }
329
330 /* Done. */
331 return 0;
332 }
333
334
335
336 /*
337 * This test demonstrate the bug as reported in:
338 * http://bugzilla.pjproject.net/show_bug.cgi?id=49
339 */
340 #if INCLUDE_GCC_TEST
341 static int gcc_test()
342 {
343 char msgbuf[512];
344 pj_str_t target = pj_str("sip:alice@wonderland:5061;x-param=param%201"
345 "?X-Hdr-1=Header%201"
346 "&X-Empty-Hdr=");
347 pjsip_tx_data *tdata;
348 pjsip_parser_err_report err_list;
349 pjsip_msg *msg;
350 int len;
351 pj_status_t status;
352
353 PJ_LOG(3,(THIS_FILE, " header param in URI to create request"));
354
355 /* Create request with header param in target URI. */
356 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method, &target,
357 &target, &target, &target, NULL, -1,
358 NULL, &tdata);
359 if (status != 0) {
360 app_perror(" error: Unable to create request", status);
361 return -200;
362 }
363
364 /* Print and parse the request.
365 * We'll check that header params are not present in
366 */
367 len = pjsip_msg_print(tdata->msg, msgbuf, sizeof(msgbuf));
368 if (len < 1) {
369 PJ_LOG(3,(THIS_FILE, " error: printing message"));
370 pjsip_tx_data_dec_ref(tdata);
371 return -250;
372 }
373 msgbuf[len] = '\0';
374
375 PJ_LOG(5,(THIS_FILE, "%d bytes request created:--begin-msg--\n"
376 "%s\n"
377 "--end-msg--", len, msgbuf));
378
379 /* Now parse the message. */
380 pj_list_init(&err_list);
381 msg = pjsip_parse_msg( tdata->pool, msgbuf, len, &err_list);
382 if (msg == NULL) {
383 pjsip_parser_err_report *e;
384
385 PJ_LOG(3,(THIS_FILE, " error: parsing message message"));
386
387 e = err_list.next;
388 while (e != &err_list) {
389 PJ_LOG(3,(THIS_FILE, " %s in line %d col %d hname=%.*s",
390 pj_exception_id_name(e->except_code),
391 e->line, e->col+1,
392 (int)e->hname.slen,
393 e->hname.ptr));
394 e = e->next;
395 }
396
397 pjsip_tx_data_dec_ref(tdata);
398 return -255;
399 }
400
401 pjsip_tx_data_dec_ref(tdata);
402 return 0;
403 }
404 #endif
405
406
407 /* This tests the request creating functions against the following
408 * requirements:
409 * - header params in URI creates header in the request.
410 * - method and headers params are correctly shown or hidden in
411 * request URI, From, To, and Contact header.
412 */
413 static int txdata_test_uri_params(void)
414 {
415 char msgbuf[512];
416 pj_str_t target = pj_str("sip:alice@wonderland:5061;x-param=param%201"
417 "?X-Hdr-1=Header%201"
418 "&X-Empty-Hdr=");
419 pj_str_t contact;
420 pj_str_t pname = pj_str("x-param");
421 pj_str_t hname = pj_str("X-Hdr-1");
422 pj_str_t hemptyname = pj_str("X-Empty-Hdr");
423 pjsip_from_hdr *from_hdr;
424 pjsip_to_hdr *to_hdr;
425 pjsip_contact_hdr *contact_hdr;
426 pjsip_generic_string_hdr *hdr;
427 pjsip_tx_data *tdata;
428 pjsip_sip_uri *uri;
429 pjsip_param *param;
430 pjsip_via_hdr *via;
431 pjsip_parser_err_report err_list;
432 pjsip_msg *msg;
433 pj_ssize_t len;
434 pj_status_t status;
435
436 PJ_LOG(3,(THIS_FILE, " header param in URI to create request"));
437
438 /* Due to #930, contact argument is now parsed as Contact header, so
439 * must enclose it with <> to make it be parsed as URI.
440 */
441 pj_ansi_snprintf(msgbuf, sizeof(msgbuf), "<%.*s>",
442 (int)target.slen, target.ptr);
443 contact.ptr = msgbuf;
444 contact.slen = strlen(msgbuf);
445
446 /* Create request with header param in target URI. */
447 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method, &target,
448 &target, &target, &contact, NULL, -1,
449 NULL, &tdata);
450 if (status != 0) {
451 app_perror(" error: Unable to create request", status);
452 return -200;
453 }
454
455 /* Fill up the Via header to prevent syntax error on parsing */
456 via = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
457 via->transport = pj_str("TCP");
458 via->sent_by.host = pj_str("127.0.0.1");
459
460 /* Print and parse the request.
461 * We'll check that header params are not present in
462 */
463 len = pjsip_msg_print(tdata->msg, msgbuf, sizeof(msgbuf));
464 if (len < 1) {
465 PJ_LOG(3,(THIS_FILE, " error: printing message"));
466 pjsip_tx_data_dec_ref(tdata);
467 return -250;
468 }
469 msgbuf[len] = '\0';
470
471 PJ_LOG(5,(THIS_FILE, "%ld bytes request created:--begin-msg--\n"
472 "%s\n"
473 "--end-msg--", len, msgbuf));
474
475 /* Now parse the message. */
476 pj_list_init(&err_list);
477 msg = pjsip_parse_msg( tdata->pool, msgbuf, len, &err_list);
478 if (msg == NULL) {
479 pjsip_parser_err_report *e;
480
481 PJ_LOG(3,(THIS_FILE, " error: parsing message message"));
482
483 e = err_list.next;
484 while (e != &err_list) {
485 PJ_LOG(3,(THIS_FILE, " %s in line %d col %d hname=%.*s",
486 pj_exception_id_name(e->except_code),
487 e->line, e->col+1,
488 (int)e->hname.slen,
489 e->hname.ptr));
490 e = e->next;
491 }
492
493 pjsip_tx_data_dec_ref(tdata);
494 return -256;
495 }
496
497 /* Check the existence of port, other_param, and header param.
498 * Port is now allowed in To and From header.
499 */
500 /* Port in request URI. */
501 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(msg->line.req.uri);
502 if (uri->port != 5061) {
503 PJ_LOG(3,(THIS_FILE, " error: port not present in request URI"));
504 pjsip_tx_data_dec_ref(tdata);
505 return -260;
506 }
507 /* other_param in request_uri */
508 param = pjsip_param_find(&uri->other_param, &pname);
509 if (param == NULL || pj_strcmp2(&param->value, "param 1") != 0) {
510 PJ_LOG(3,(THIS_FILE, " error: x-param not present in request URI"));
511 pjsip_tx_data_dec_ref(tdata);
512 return -261;
513 }
514 /* header param in request uri. */
515 if (!pj_list_empty(&uri->header_param)) {
516 PJ_LOG(3,(THIS_FILE, " error: hparam in request URI"));
517 pjsip_tx_data_dec_ref(tdata);
518 return -262;
519 }
520
521 /* Port in From header. */
522 from_hdr = (pjsip_from_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_FROM, NULL);
523 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(from_hdr->uri);
524 if (uri->port != 0) {
525 PJ_LOG(3,(THIS_FILE, " error: port most not exist in From header"));
526 pjsip_tx_data_dec_ref(tdata);
527 return -270;
528 }
529 /* other_param in From header */
530 param = pjsip_param_find(&uri->other_param, &pname);
531 if (param == NULL || pj_strcmp2(&param->value, "param 1") != 0) {
532 PJ_LOG(3,(THIS_FILE, " error: x-param not present in From header"));
533 pjsip_tx_data_dec_ref(tdata);
534 return -271;
535 }
536 /* header param in From header. */
537 if (!pj_list_empty(&uri->header_param)) {
538 PJ_LOG(3,(THIS_FILE, " error: hparam in From header"));
539 pjsip_tx_data_dec_ref(tdata);
540 return -272;
541 }
542
543
544 /* Port in To header. */
545 to_hdr = (pjsip_to_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_TO, NULL);
546 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(to_hdr->uri);
547 if (uri->port != 0) {
548 PJ_LOG(3,(THIS_FILE, " error: port most not exist in To header"));
549 pjsip_tx_data_dec_ref(tdata);
550 return -280;
551 }
552 /* other_param in To header */
553 param = pjsip_param_find(&uri->other_param, &pname);
554 if (param == NULL || pj_strcmp2(&param->value, "param 1") != 0) {
555 PJ_LOG(3,(THIS_FILE, " error: x-param not present in To header"));
556 pjsip_tx_data_dec_ref(tdata);
557 return -281;
558 }
559 /* header param in From header. */
560 if (!pj_list_empty(&uri->header_param)) {
561 PJ_LOG(3,(THIS_FILE, " error: hparam in To header"));
562 pjsip_tx_data_dec_ref(tdata);
563 return -282;
564 }
565
566
567
568 /* Port in Contact header. */
569 contact_hdr = (pjsip_contact_hdr*) pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, NULL);
570 uri = (pjsip_sip_uri*) pjsip_uri_get_uri(contact_hdr->uri);
571 if (uri->port != 5061) {
572 PJ_LOG(3,(THIS_FILE, " error: port not present in Contact header"));
573 pjsip_tx_data_dec_ref(tdata);
574 return -290;
575 }
576 /* other_param in Contact header */
577 param = pjsip_param_find(&uri->other_param, &pname);
578 if (param == NULL || pj_strcmp2(&param->value, "param 1") != 0) {
579 PJ_LOG(3,(THIS_FILE, " error: x-param not present in Contact header"));
580 pjsip_tx_data_dec_ref(tdata);
581 return -291;
582 }
583 /* header param in Contact header. */
584 if (pj_list_empty(&uri->header_param)) {
585 PJ_LOG(3,(THIS_FILE, " error: hparam is missing in Contact header"));
586 pjsip_tx_data_dec_ref(tdata);
587 return -292;
588 }
589 /* Check for X-Hdr-1 */
590 param = pjsip_param_find(&uri->header_param, &hname);
591 if (param == NULL || pj_strcmp2(&param->value, "Header 1")!=0) {
592 PJ_LOG(3,(THIS_FILE, " error: hparam is missing in Contact header"));
593 pjsip_tx_data_dec_ref(tdata);
594 return -293;
595 }
596 /* Check for X-Empty-Hdr */
597 param = pjsip_param_find(&uri->header_param, &hemptyname);
598 if (param == NULL || pj_strcmp2(&param->value, "")!=0) {
599 PJ_LOG(3,(THIS_FILE, " error: hparam is missing in Contact header"));
600 pjsip_tx_data_dec_ref(tdata);
601 return -294;
602 }
603
604
605 /* Check that headers are present in the request. */
606 hdr = (pjsip_generic_string_hdr*)
607 pjsip_msg_find_hdr_by_name(msg, &hname, NULL);
608 if (hdr == NULL || pj_strcmp2(&hdr->hvalue, "Header 1")!=0) {
609 PJ_LOG(3,(THIS_FILE, " error: header X-Hdr-1 not created"));
610 pjsip_tx_data_dec_ref(tdata);
611 return -300;
612 }
613
614 hdr = (pjsip_generic_string_hdr*)
615 pjsip_msg_find_hdr_by_name(msg, &hemptyname, NULL);
616 if (hdr == NULL || pj_strcmp2(&param->value, "")!=0) {
617 PJ_LOG(3,(THIS_FILE, " error: header X-Empty-Hdr not created"));
618 pjsip_tx_data_dec_ref(tdata);
619 return -330;
620 }
621
622 pjsip_tx_data_dec_ref(tdata);
623 return 0;
624 }
625
626
627 /*
628 * create request benchmark
629 */
630 static int create_request_bench(pj_timestamp *p_elapsed)
631 {
632 enum { COUNT = 100 };
633 unsigned i, j;
634 pjsip_tx_data *tdata[COUNT];
635 pj_timestamp t1, t2, elapsed;
636 pj_status_t status;
637
638 pj_str_t str_target = pj_str("sip:[email protected]");
639 pj_str_t str_from = pj_str("\"Local User\" <sip:[email protected]>");
640 pj_str_t str_to = pj_str("\"Remote User\" <sip:[email protected]>");
641 pj_str_t str_contact = str_from;
642
643 elapsed.u64 = 0;
644
645 for (i=0; i<LOOP; i+=COUNT) {
646 pj_bzero(tdata, sizeof(tdata));
647
648 pj_get_timestamp(&t1);
649
650 for (j=0; j<COUNT; ++j) {
651 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method,
652 &str_target, &str_from, &str_to,
653 &str_contact, NULL, -1, NULL,
654 &tdata[j]);
655 if (status != PJ_SUCCESS) {
656 app_perror(" error: unable to create request", status);
657 goto on_error;
658 }
659 }
660
661 pj_get_timestamp(&t2);
662 pj_sub_timestamp(&t2, &t1);
663 pj_add_timestamp(&elapsed, &t2);
664
665 for (j=0; j<COUNT; ++j)
666 pjsip_tx_data_dec_ref(tdata[j]);
667 }
668
669 p_elapsed->u64 = elapsed.u64;
670 return PJ_SUCCESS;
671
672 on_error:
673 for (i=0; i<COUNT; ++i) {
674 if (tdata[i])
675 pjsip_tx_data_dec_ref(tdata[i]);
676 }
677 return -400;
678 }
679
680
681
682 /*
683 * create response benchmark
684 */
685 static int create_response_bench(pj_timestamp *p_elapsed)
686 {
687 enum { COUNT = 100 };
688 unsigned i, j;
689 pjsip_via_hdr *via;
690 pjsip_rx_data rdata;
691 pjsip_tx_data *request;
692 pjsip_tx_data *tdata[COUNT];
693 pj_timestamp t1, t2, elapsed;
694 pj_status_t status;
695
696 /* Create the request first. */
697 pj_str_t str_target = pj_str("sip:[email protected]");
698 pj_str_t str_from = pj_str("\"Local User\" <sip:[email protected]>");
699 pj_str_t str_to = pj_str("\"Remote User\" <sip:[email protected]>");
700 pj_str_t str_contact = str_from;
701
702 status = pjsip_endpt_create_request(endpt, &pjsip_invite_method,
703 &str_target, &str_from, &str_to,
704 &str_contact, NULL, -1, NULL,
705 &request);
706 if (status != PJ_SUCCESS) {
707 app_perror(" error: unable to create request", status);
708 return status;
709 }
710
711 /* Create several Via headers */
712 via = pjsip_via_hdr_create(request->pool);
713 via->sent_by.host = pj_str("192.168.0.7");
714 via->sent_by.port = 5061;
715 via->transport = pj_str("udp");
716 via->rport_param = 0;
717 via->branch_param = pj_str("012345678901234567890123456789");
718 via->recvd_param = pj_str("192.168.0.7");
719 pjsip_msg_insert_first_hdr(request->msg, (pjsip_hdr*) pjsip_hdr_clone(request->pool, via));
720 pjsip_msg_insert_first_hdr(request->msg, (pjsip_hdr*) pjsip_hdr_clone(request->pool, via));
721 pjsip_msg_insert_first_hdr(request->msg, (pjsip_hdr*)via);
722
723
724 /* Create "dummy" rdata from the tdata */
725 pj_bzero(&rdata, sizeof(pjsip_rx_data));
726 rdata.tp_info.pool = request->pool;
727 rdata.msg_info.msg = request->msg;
728 rdata.msg_info.from = (pjsip_from_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_FROM, NULL);
729 rdata.msg_info.to = (pjsip_to_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_TO, NULL);
730 rdata.msg_info.cseq = (pjsip_cseq_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_CSEQ, NULL);
731 rdata.msg_info.cid = (pjsip_cid_hdr*) pjsip_msg_find_hdr(request->msg, PJSIP_H_FROM, NULL);
732 rdata.msg_info.via = via;
733
734 /*
735 * Now benchmark create_response
736 */
737 elapsed.u64 = 0;
738
739 for (i=0; i<LOOP; i+=COUNT) {
740 pj_bzero(tdata, sizeof(tdata));
741
742 pj_get_timestamp(&t1);
743
744 for (j=0; j<COUNT; ++j) {
745 status = pjsip_endpt_create_response(endpt, &rdata, 200, NULL, &tdata[j]);
746 if (status != PJ_SUCCESS) {
747 app_perror(" error: unable to create request", status);
748 goto on_error;
749 }
750 }
751
752 pj_get_timestamp(&t2);
753 pj_sub_timestamp(&t2, &t1);
754 pj_add_timestamp(&elapsed, &t2);
755
756 for (j=0; j<COUNT; ++j)
757 pjsip_tx_data_dec_ref(tdata[j]);
758 }
759
760 p_elapsed->u64 = elapsed.u64;
761 pjsip_tx_data_dec_ref(request);
762 return PJ_SUCCESS;
763
764 on_error:
765 for (i=0; i<COUNT; ++i) {
766 if (tdata[i])
767 pjsip_tx_data_dec_ref(tdata[i]);
768 }
769 return -400;
770 }
771
772
773 int txdata_test(void)
774 {
775 enum { REPEAT = 4 };
776 unsigned i, msgs;
777 pj_timestamp usec[REPEAT], min, freq;
778 int status;
779
780 status = pj_get_timestamp_freq(&freq);
781 if (status != PJ_SUCCESS)
782 return status;
783
784 status = core_txdata_test();
785 if (status != 0)
786 return status;
787
788 #if INCLUDE_GCC_TEST
789 status = gcc_test();
790 if (status != 0)
791 return status;
792 #endif
793
794 status = txdata_test_uri_params();
795 if (status != 0)
796 return status;
797
798
799 /*
800 * Benchmark create_request()
801 */
802 PJ_LOG(3,(THIS_FILE, " benchmarking request creation:"));
803 for (i=0; i<REPEAT; ++i) {
804 PJ_LOG(3,(THIS_FILE, " test %d of %d..",
805 i+1, REPEAT));
806 status = create_request_bench(&usec[i]);
807 if (status != PJ_SUCCESS)
808 return status;
809 }
810
811 min.u64 = PJ_UINT64(0xFFFFFFFFFFFFFFF);
812 for (i=0; i<REPEAT; ++i) {
813 if (usec[i].u64 < min.u64) min.u64 = usec[i].u64;
814 }
815
816 msgs = (unsigned)(freq.u64 * LOOP / min.u64);
817
818 PJ_LOG(3,(THIS_FILE, " Requests created at %d requests/sec", msgs));
819
820 report_ival("create-request-per-sec",
821 msgs, "msg/sec",
822 "Number of typical request messages that can be created "
823 "per second with <tt>pjsip_endpt_create_request()</tt>");
824
825
826 /*
827 * Benchmark create_response()
828 */
829 PJ_LOG(3,(THIS_FILE, " benchmarking response creation:"));
830 for (i=0; i<REPEAT; ++i) {
831 PJ_LOG(3,(THIS_FILE, " test %d of %d..",
832 i+1, REPEAT));
833 status = create_response_bench(&usec[i]);
834 if (status != PJ_SUCCESS)
835 return status;
836 }
837
838 min.u64 = PJ_UINT64(0xFFFFFFFFFFFFFFF);
839 for (i=0; i<REPEAT; ++i) {
840 if (usec[i].u64 < min.u64) min.u64 = usec[i].u64;
841 }
842
843 msgs = (unsigned)(freq.u64 * LOOP / min.u64);
844
845 PJ_LOG(3,(THIS_FILE, " Responses created at %d responses/sec", msgs));
846
847 report_ival("create-response-per-sec",
848 msgs, "msg/sec",
849 "Number of typical response messages that can be created "
850 "per second with <tt>pjsip_endpt_create_response()</tt>");
851
852
853 return 0;
854 }
855