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