blob: cc603103aa5424a928fa37c54c7844c2ba589ecc [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
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#include "test.h"
Benny Prijono40f2f642006-01-30 18:40:05 +000020#include <pjsip.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000021#include <pjlib.h>
22
Benny Prijono3818c932005-11-22 01:06:33 +000023#define POOL_SIZE 8000
Benny Prijonoe93e2872006-06-28 16:46:49 +000024#if defined(PJ_DEBUG) && PJ_DEBUG!=0
25# define LOOP 10000
26#else
27# define LOOP 100000
28#endif
Benny Prijono3818c932005-11-22 01:06:33 +000029#define AVERAGE_MSG_LEN 800
Benny Prijono85598d92006-01-07 18:44:25 +000030#define THIS_FILE "msg_test.c"
Benny Prijono3818c932005-11-22 01:06:33 +000031
Benny Prijono5dcb38d2005-11-21 01:55:47 +000032static pjsip_msg *create_msg0(pj_pool_t *pool);
Benny Prijono3818c932005-11-22 01:06:33 +000033static pjsip_msg *create_msg1(pj_pool_t *pool);
34
35#define STATUS_PARTIAL 1
36#define STATUS_SYNTAX_ERROR 2
37
38#define FLAG_DETECT_ONLY 1
39#define FLAG_PARSE_ONLY 4
40#define FLAG_PRINT_ONLY 8
41
Benny Prijono5dcb38d2005-11-21 01:55:47 +000042struct test_msg
43{
44 char msg[1024];
45 pjsip_msg *(*creator)(pj_pool_t *pool);
46 pj_size_t len;
Benny Prijono3818c932005-11-22 01:06:33 +000047 int expected_status;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000048} test_array[] =
49{
Benny Prijono3818c932005-11-22 01:06:33 +000050{
51 /* 'Normal' message with all headers. */
52 "INVITE sip:user@foo SIP/2.0\n"
Benny Prijonob6eab2c2006-07-03 22:08:47 +000053 "from: Hi I'm Joe <sip:joe.user@bar.otherdomain.com>;tag=123457890123456\r"
Benny Prijono3818c932005-11-22 01:06:33 +000054 "To: Fellow User <sip:user@foo.bar.domain.com>\r\n"
55 "Call-ID: 12345678901234567890@bar\r\n"
56 "Content-Length: 0\r\n"
57 "CSeq: 123456 INVITE\n"
58 "Contact: <sip:joe@bar> ; q=0.5;expires=3600,sip:user@host;q=0.500\r"
59 " ,sip:user2@host2\n"
60 "Content-Type: text/html ; charset=ISO-8859-4\r"
61 "Route: <sip:bigbox3.site3.atlanta.com;lr>,\r\n"
62 " <sip:server10.biloxi.com;lr>\r"
Benny Prijonofa73e3e2006-01-05 23:35:46 +000063 "Record-Route: <sip:server10.biloxi.com>,\r\n" /* multiple routes+folding*/
Benny Prijono3818c932005-11-22 01:06:33 +000064 " <sip:bigbox3.site3.atlanta.com;lr>\n"
Benny Prijono69f73fe2007-03-19 10:27:13 +000065 "v: SIP/2.0/SCTP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c230\n"
Benny Prijonofa73e3e2006-01-05 23:35:46 +000066 "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8\n" /* folding. */
Benny Prijono3818c932005-11-22 01:06:33 +000067 " ;received=192.0.2.1\r\n"
68 "Via: SIP/2.0/UDP 10.2.1.1, SIP/2.0/TCP 192.168.1.1\n"
69 "Organization: \r"
70 "Max-Forwards: 70\n"
Benny Prijonofa73e3e2006-01-05 23:35:46 +000071 "X-Header: \r\n" /* empty header */
72 "P-Associated-URI:\r\n" /* empty header without space */
Benny Prijono3818c932005-11-22 01:06:33 +000073 "\r\n",
74 &create_msg0,
75 PJ_SUCCESS
76},
77{
78 /* Typical response message. */
79 "SIP/2.0 200 OK\r\n"
Benny Prijono4cf8d1c2007-04-28 15:37:49 +000080 "Via: SIP/2.0/SCTP server10.biloxi.com;branch=z9hG4bKnashds8;rport;received=192.0.2.1\r\n"
Benny Prijono3818c932005-11-22 01:06:33 +000081 "Via: SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2\r\n"
82 "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds ;received=192.0.2.3\r\n"
83 "Route: <sip:proxy.sipprovider.com>\r\n"
84 "Route: <sip:proxy.supersip.com:5060>\r\n"
85 "Max-Forwards: 70\r\n"
86 "To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n"
87 "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n"
88 "Call-ID: a84b4c76e66710@pc33.atlanta.com\r\n"
89 "CSeq: 314159 INVITE\r\n"
90 "Contact: <sips:bob@192.0.2.4>\r\n"
91 "Content-Type: application/sdp\r\n"
92 "Content-Length: 150\r\n"
93 "\r\n"
94 "v=0\r\n"
95 "o=alice 53655765 2353687637 IN IP4 pc33.atlanta.com\r\n"
96 "s=-\r\n"
97 "t=0 0\r\n"
98 "c=IN IP4 pc33.atlanta.com\r\n"
99 "m=audio 3456 RTP/AVP 0 1 3 99\r\n"
100 "a=rtpmap:0 PCMU/8000\r\n",
101 &create_msg1,
102 PJ_SUCCESS
103}
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000104};
105
Benny Prijonoe93e2872006-06-28 16:46:49 +0000106static struct
107{
108 int flag;
109 pj_highprec_t detect_len, parse_len, print_len;
110 pj_timestamp detect_time, parse_time, print_time;
111} var;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000112
113static pj_status_t test_entry( pj_pool_t *pool, struct test_msg *entry )
114{
Benny Prijonoe960bb52007-01-21 17:53:39 +0000115 pjsip_msg *parsed_msg, *ref_msg = NULL;
Benny Prijonocec5f4a2005-11-22 23:51:50 +0000116 static pjsip_msg *print_msg;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000117 pj_status_t status = PJ_SUCCESS;
118 int len;
119 pj_str_t str1, str2;
120 pjsip_hdr *hdr1, *hdr2;
121 pj_timestamp t1, t2;
Benny Prijono3818c932005-11-22 01:06:33 +0000122 pjsip_parser_err_report err_list;
123 pj_size_t msg_size;
124 char msgbuf1[PJSIP_MAX_PKT_LEN];
125 char msgbuf2[PJSIP_MAX_PKT_LEN];
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000126 enum { BUFLEN = 512 };
Benny Prijono3818c932005-11-22 01:06:33 +0000127
128 entry->len = pj_native_strlen(entry->msg);
129
Benny Prijonoe93e2872006-06-28 16:46:49 +0000130 if (var.flag & FLAG_PARSE_ONLY)
Benny Prijono3818c932005-11-22 01:06:33 +0000131 goto parse_msg;
Benny Prijonocec5f4a2005-11-22 23:51:50 +0000132
Benny Prijonoe93e2872006-06-28 16:46:49 +0000133 if (var.flag & FLAG_PRINT_ONLY) {
Benny Prijonocec5f4a2005-11-22 23:51:50 +0000134 if (print_msg == NULL)
135 print_msg = entry->creator(pool);
Benny Prijono3818c932005-11-22 01:06:33 +0000136 goto print_msg;
Benny Prijonocec5f4a2005-11-22 23:51:50 +0000137 }
Benny Prijono3818c932005-11-22 01:06:33 +0000138
139 /* Detect message. */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000140 var.detect_len = var.detect_len + entry->len;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000141 pj_get_timestamp(&t1);
Benny Prijono3818c932005-11-22 01:06:33 +0000142 status = pjsip_find_msg(entry->msg, entry->len, PJ_FALSE, &msg_size);
143 if (status != PJ_SUCCESS) {
144 if (status!=PJSIP_EPARTIALMSG ||
145 entry->expected_status!=STATUS_PARTIAL)
146 {
147 app_perror(" error: unable to detect message", status);
148 return -5;
149 }
150 }
151 if (msg_size != entry->len) {
Benny Prijono85598d92006-01-07 18:44:25 +0000152 PJ_LOG(3,(THIS_FILE, " error: size mismatch"));
Benny Prijono3818c932005-11-22 01:06:33 +0000153 return -6;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000154 }
155 pj_get_timestamp(&t2);
Benny Prijono3818c932005-11-22 01:06:33 +0000156 pj_sub_timestamp(&t2, &t1);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000157 pj_add_timestamp(&var.detect_time, &t2);
Benny Prijono3818c932005-11-22 01:06:33 +0000158
Benny Prijonoe93e2872006-06-28 16:46:49 +0000159 if (var.flag & FLAG_DETECT_ONLY)
Benny Prijono3818c932005-11-22 01:06:33 +0000160 return PJ_SUCCESS;
161
162 /* Parse message. */
163parse_msg:
Benny Prijonoe93e2872006-06-28 16:46:49 +0000164 var.parse_len = var.parse_len + entry->len;
Benny Prijono3818c932005-11-22 01:06:33 +0000165 pj_get_timestamp(&t1);
166 pj_list_init(&err_list);
167 parsed_msg = pjsip_parse_msg(pool, entry->msg, entry->len, &err_list);
168 if (parsed_msg == NULL) {
169 if (entry->expected_status != STATUS_SYNTAX_ERROR) {
170 status = -10;
171 if (err_list.next != &err_list) {
Benny Prijono85598d92006-01-07 18:44:25 +0000172 PJ_LOG(3,(THIS_FILE, " Syntax error in line %d col %d",
Benny Prijono3818c932005-11-22 01:06:33 +0000173 err_list.next->line, err_list.next->col));
174 }
175 goto on_return;
176 }
177 }
178 pj_get_timestamp(&t2);
179 pj_sub_timestamp(&t2, &t1);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000180 pj_add_timestamp(&var.parse_time, &t2);
Benny Prijono3818c932005-11-22 01:06:33 +0000181
Benny Prijonoe93e2872006-06-28 16:46:49 +0000182 if (var.flag & FLAG_PARSE_ONLY)
Benny Prijono3818c932005-11-22 01:06:33 +0000183 return PJ_SUCCESS;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000184
185 /* Create reference message. */
186 ref_msg = entry->creator(pool);
187
188 /* Create buffer for comparison. */
Benny Prijonoa1e69682007-05-11 15:14:34 +0000189 str1.ptr = (char*)pj_pool_alloc(pool, BUFLEN);
190 str2.ptr = (char*)pj_pool_alloc(pool, BUFLEN);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000191
192 /* Compare message type. */
193 if (parsed_msg->type != ref_msg->type) {
194 status = -20;
195 goto on_return;
196 }
197
198 /* Compare request or status line. */
199 if (parsed_msg->type == PJSIP_REQUEST_MSG) {
200 pjsip_method *m1 = &parsed_msg->line.req.method;
201 pjsip_method *m2 = &ref_msg->line.req.method;
202
Benny Prijono3818c932005-11-22 01:06:33 +0000203 if (pjsip_method_cmp(m1, m2) != 0) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000204 status = -30;
205 goto on_return;
206 }
Benny Prijono3818c932005-11-22 01:06:33 +0000207 status = pjsip_uri_cmp(PJSIP_URI_IN_REQ_URI,
208 parsed_msg->line.req.uri,
209 ref_msg->line.req.uri);
210 if (status != PJ_SUCCESS) {
211 app_perror(" error: request URI mismatch", status);
212 status = -31;
213 goto on_return;
214 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000215 } else {
Benny Prijono3818c932005-11-22 01:06:33 +0000216 if (parsed_msg->line.status.code != ref_msg->line.status.code) {
Benny Prijono85598d92006-01-07 18:44:25 +0000217 PJ_LOG(3,(THIS_FILE, " error: status code mismatch"));
Benny Prijono3818c932005-11-22 01:06:33 +0000218 status = -32;
219 goto on_return;
220 }
221 if (pj_strcmp(&parsed_msg->line.status.reason,
222 &ref_msg->line.status.reason) != 0)
223 {
Benny Prijono85598d92006-01-07 18:44:25 +0000224 PJ_LOG(3,(THIS_FILE, " error: status text mismatch"));
Benny Prijono3818c932005-11-22 01:06:33 +0000225 status = -33;
226 goto on_return;
227 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000228 }
229
230 /* Compare headers. */
231 hdr1 = parsed_msg->hdr.next;
232 hdr2 = ref_msg->hdr.next;
233
234 while (hdr1 != &parsed_msg->hdr && hdr2 != &ref_msg->hdr) {
235 len = hdr1->vptr->print_on(hdr1, str1.ptr, BUFLEN);
236 if (len < 1) {
237 status = -40;
238 goto on_return;
239 }
Benny Prijono3818c932005-11-22 01:06:33 +0000240 str1.ptr[len] = '\0';
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000241 str1.slen = len;
242
243 len = hdr2->vptr->print_on(hdr2, str2.ptr, BUFLEN);
244 if (len < 1) {
245 status = -50;
246 goto on_return;
247 }
Benny Prijono3818c932005-11-22 01:06:33 +0000248 str2.ptr[len] = '\0';
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000249 str2.slen = len;
250
251 if (pj_strcmp(&str1, &str2) != 0) {
252 status = -60;
Benny Prijono85598d92006-01-07 18:44:25 +0000253 PJ_LOG(3,(THIS_FILE, " error: header string mismatch:\n"
Benny Prijono3818c932005-11-22 01:06:33 +0000254 " h1='%s'\n"
255 " h2='%s'\n",
256 str1.ptr, str2.ptr));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000257 goto on_return;
258 }
259
260 hdr1 = hdr1->next;
261 hdr2 = hdr2->next;
262 }
263
264 if (hdr1 != &parsed_msg->hdr || hdr2 != &ref_msg->hdr) {
265 status = -70;
266 goto on_return;
267 }
268
Benny Prijono3818c932005-11-22 01:06:33 +0000269 /* Compare body? */
270 if (parsed_msg->body==NULL && ref_msg->body==NULL)
271 goto print_msg;
272
273 /* Compare msg body length. */
274 if (parsed_msg->body->len != ref_msg->body->len) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000275 status = -80;
276 goto on_return;
277 }
Benny Prijono3818c932005-11-22 01:06:33 +0000278
279 /* Compare msg body content type. */
280 if (pj_strcmp(&parsed_msg->body->content_type.type,
281 &ref_msg->body->content_type.type) != 0) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000282 status = -90;
283 goto on_return;
284 }
Benny Prijono3818c932005-11-22 01:06:33 +0000285 if (pj_strcmp(&parsed_msg->body->content_type.subtype,
286 &ref_msg->body->content_type.subtype) != 0) {
287 status = -100;
288 goto on_return;
289 }
290
291 /* Compare body content. */
292 str1.slen = parsed_msg->body->print_body(parsed_msg->body,
293 msgbuf1, sizeof(msgbuf1));
294 if (str1.slen < 1) {
295 status = -110;
296 goto on_return;
297 }
298 str1.ptr = msgbuf1;
299
300 str2.slen = ref_msg->body->print_body(ref_msg->body,
301 msgbuf2, sizeof(msgbuf2));
302 if (str2.slen < 1) {
303 status = -120;
304 goto on_return;
305 }
306 str2.ptr = msgbuf2;
307
308 if (pj_strcmp(&str1, &str2) != 0) {
309 status = -140;
310 goto on_return;
311 }
312
313 /* Print message. */
314print_msg:
Benny Prijonoe93e2872006-06-28 16:46:49 +0000315 var.print_len = var.print_len + entry->len;
Benny Prijono3818c932005-11-22 01:06:33 +0000316 pj_get_timestamp(&t1);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000317 if (var.flag && FLAG_PRINT_ONLY)
Benny Prijonocec5f4a2005-11-22 23:51:50 +0000318 ref_msg = print_msg;
319 len = pjsip_msg_print(ref_msg, msgbuf1, PJSIP_MAX_PKT_LEN);
Benny Prijono3818c932005-11-22 01:06:33 +0000320 if (len < 1) {
321 status = -150;
322 goto on_return;
323 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000324 pj_get_timestamp(&t2);
Benny Prijono3818c932005-11-22 01:06:33 +0000325 pj_sub_timestamp(&t2, &t1);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000326 pj_add_timestamp(&var.print_time, &t2);
Benny Prijono3818c932005-11-22 01:06:33 +0000327
328
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000329 status = PJ_SUCCESS;
330
331on_return:
332 return status;
333}
334
335
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000336static pjsip_msg *create_msg0(pj_pool_t *pool)
337{
338
339 pjsip_msg *msg;
340 pjsip_name_addr *name_addr;
Benny Prijono0c2bc612006-01-10 13:31:40 +0000341 pjsip_sip_uri *url;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000342 pjsip_fromto_hdr *fromto;
343 pjsip_cid_hdr *cid;
344 pjsip_clen_hdr *clen;
345 pjsip_cseq_hdr *cseq;
346 pjsip_contact_hdr *contact;
347 pjsip_ctype_hdr *ctype;
348 pjsip_routing_hdr *routing;
349 pjsip_via_hdr *via;
350 pjsip_generic_string_hdr *generic;
351 pj_str_t str;
352
353 msg = pjsip_msg_create(pool, PJSIP_REQUEST_MSG);
354
355 /* "INVITE sip:user@foo SIP/2.0\n" */
356 pjsip_method_set(&msg->line.req.method, PJSIP_INVITE_METHOD);
Benny Prijono40f2f642006-01-30 18:40:05 +0000357 url = pjsip_sip_uri_create(pool, 0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000358 msg->line.req.uri = (pjsip_uri*)url;
359 pj_strdup2(pool, &url->user, "user");
360 pj_strdup2(pool, &url->host, "foo");
361
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000362 /* "From: Hi I'm Joe <sip:joe.user@bar.otherdomain.com>;tag=123457890123456\r" */
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000363 fromto = pjsip_from_hdr_create(pool);
364 pjsip_msg_add_hdr(msg, (pjsip_hdr*)fromto);
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000365 pj_strdup2(pool, &fromto->tag, "123457890123456");
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000366 name_addr = pjsip_name_addr_create(pool);
367 fromto->uri = (pjsip_uri*)name_addr;
368 pj_strdup2(pool, &name_addr->display, "Hi I'm Joe");
Benny Prijono40f2f642006-01-30 18:40:05 +0000369 url = pjsip_sip_uri_create(pool, 0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000370 name_addr->uri = (pjsip_uri*)url;
371 pj_strdup2(pool, &url->user, "joe.user");
372 pj_strdup2(pool, &url->host, "bar.otherdomain.com");
373
374 /* "To: Fellow User <sip:user@foo.bar.domain.com>\r\n" */
375 fromto = pjsip_to_hdr_create(pool);
376 pjsip_msg_add_hdr(msg, (pjsip_hdr*)fromto);
377 name_addr = pjsip_name_addr_create(pool);
378 fromto->uri = (pjsip_uri*)name_addr;
379 pj_strdup2(pool, &name_addr->display, "Fellow User");
Benny Prijono40f2f642006-01-30 18:40:05 +0000380 url = pjsip_sip_uri_create(pool, 0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000381 name_addr->uri = (pjsip_uri*)url;
382 pj_strdup2(pool, &url->user, "user");
383 pj_strdup2(pool, &url->host, "foo.bar.domain.com");
384
385 /* "Call-ID: 12345678901234567890@bar\r\n" */
386 cid = pjsip_cid_hdr_create(pool);
387 pjsip_msg_add_hdr(msg, (pjsip_hdr*)cid);
388 pj_strdup2(pool, &cid->id, "12345678901234567890@bar");
389
390 /* "Content-Length: 0\r\n" */
391 clen = pjsip_clen_hdr_create(pool);
392 pjsip_msg_add_hdr(msg, (pjsip_hdr*)clen);
393 clen->len = 0;
394
395 /* "CSeq: 123456 INVITE\n" */
396 cseq = pjsip_cseq_hdr_create(pool);
397 pjsip_msg_add_hdr(msg, (pjsip_hdr*)cseq);
398 cseq->cseq = 123456;
399 pjsip_method_set(&cseq->method, PJSIP_INVITE_METHOD);
400
401 /* "Contact: <sip:joe@bar>;q=0.5;expires=3600*/
402 contact = pjsip_contact_hdr_create(pool);
403 pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
404 contact->q1000 = 500;
405 contact->expires = 3600;
406 name_addr = pjsip_name_addr_create(pool);
407 contact->uri = (pjsip_uri*)name_addr;
Benny Prijono40f2f642006-01-30 18:40:05 +0000408 url = pjsip_sip_uri_create(pool, 0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000409 name_addr->uri = (pjsip_uri*)url;
410 pj_strdup2(pool, &url->user, "joe");
411 pj_strdup2(pool, &url->host, "bar");
412
413 /*, sip:user@host;q=0.500\r" */
414 contact = pjsip_contact_hdr_create(pool);
415 pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
416 contact->q1000 = 500;
Benny Prijono3818c932005-11-22 01:06:33 +0000417 name_addr = pjsip_name_addr_create(pool);
418 contact->uri = (pjsip_uri*)name_addr;
Benny Prijono40f2f642006-01-30 18:40:05 +0000419 url = pjsip_sip_uri_create(pool, 0);
Benny Prijono3818c932005-11-22 01:06:33 +0000420 name_addr->uri = (pjsip_uri*)url;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000421 pj_strdup2(pool, &url->user, "user");
422 pj_strdup2(pool, &url->host, "host");
423
424 /* " ,sip:user2@host2\n" */
425 contact = pjsip_contact_hdr_create(pool);
426 pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
Benny Prijono3818c932005-11-22 01:06:33 +0000427 name_addr = pjsip_name_addr_create(pool);
428 contact->uri = (pjsip_uri*)name_addr;
Benny Prijono40f2f642006-01-30 18:40:05 +0000429 url = pjsip_sip_uri_create(pool, 0);
Benny Prijono3818c932005-11-22 01:06:33 +0000430 name_addr->uri = (pjsip_uri*)url;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000431 pj_strdup2(pool, &url->user, "user2");
432 pj_strdup2(pool, &url->host, "host2");
433
434 /* "Content-Type: text/html; charset=ISO-8859-4\r" */
435 ctype = pjsip_ctype_hdr_create(pool);
436 pjsip_msg_add_hdr(msg, (pjsip_hdr*)ctype);
437 pj_strdup2(pool, &ctype->media.type, "text");
438 pj_strdup2(pool, &ctype->media.subtype, "html");
439 pj_strdup2(pool, &ctype->media.param, ";charset=ISO-8859-4");
440
441 /* "Route: <sip:bigbox3.site3.atlanta.com;lr>,\r\n" */
442 routing = pjsip_route_hdr_create(pool);
443 pjsip_msg_add_hdr(msg, (pjsip_hdr*)routing);
Benny Prijono40f2f642006-01-30 18:40:05 +0000444 url = pjsip_sip_uri_create(pool, 0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000445 routing->name_addr.uri = (pjsip_uri*)url;
446 pj_strdup2(pool, &url->host, "bigbox3.site3.atlanta.com");
447 url->lr_param = 1;
448
449 /* " <sip:server10.biloxi.com;lr>\r" */
450 routing = pjsip_route_hdr_create(pool);
451 pjsip_msg_add_hdr(msg, (pjsip_hdr*)routing);
Benny Prijono40f2f642006-01-30 18:40:05 +0000452 url = pjsip_sip_uri_create(pool, 0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000453 routing->name_addr.uri = (pjsip_uri*)url;
454 pj_strdup2(pool, &url->host, "server10.biloxi.com");
455 url->lr_param = 1;
456
457 /* "Record-Route: <sip:server10.biloxi.com>,\r\n" */
458 routing = pjsip_rr_hdr_create(pool);
459 pjsip_msg_add_hdr(msg, (pjsip_hdr*)routing);
Benny Prijono40f2f642006-01-30 18:40:05 +0000460 url = pjsip_sip_uri_create(pool, 0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000461 routing->name_addr.uri = (pjsip_uri*)url;
462 pj_strdup2(pool, &url->host, "server10.biloxi.com");
463 url->lr_param = 0;
464
465 /* " <sip:bigbox3.site3.atlanta.com;lr>\n" */
466 routing = pjsip_rr_hdr_create(pool);
467 pjsip_msg_add_hdr(msg, (pjsip_hdr*)routing);
Benny Prijono40f2f642006-01-30 18:40:05 +0000468 url = pjsip_sip_uri_create(pool, 0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000469 routing->name_addr.uri = (pjsip_uri*)url;
470 pj_strdup2(pool, &url->host, "bigbox3.site3.atlanta.com");
471 url->lr_param = 1;
472
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000473 /* "Via: SIP/2.0/SCTP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c230\n" */
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000474 via = pjsip_via_hdr_create(pool);
475 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
476 pj_strdup2(pool, &via->transport, "SCTP");
477 pj_strdup2(pool, &via->sent_by.host, "bigbox3.site3.atlanta.com");
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000478 pj_strdup2(pool, &via->branch_param, "z9hG4bK77ef4c230");
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000479
480 /* "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8\n"
481 " ;received=192.0.2.1\r\n" */
482 via = pjsip_via_hdr_create(pool);
483 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
484 pj_strdup2(pool, &via->transport, "UDP");
485 pj_strdup2(pool, &via->sent_by.host, "pc33.atlanta.com");
486 pj_strdup2(pool, &via->branch_param, "z9hG4bKnashds8");
487 pj_strdup2(pool, &via->recvd_param, "192.0.2.1");
488
489
490 /* "Via: SIP/2.0/UDP 10.2.1.1, */
491 via = pjsip_via_hdr_create(pool);
492 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
493 pj_strdup2(pool, &via->transport, "UDP");
494 pj_strdup2(pool, &via->sent_by.host, "10.2.1.1");
495
496
497 /*SIP/2.0/TCP 192.168.1.1\n" */
498 via = pjsip_via_hdr_create(pool);
499 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
500 pj_strdup2(pool, &via->transport, "TCP");
501 pj_strdup2(pool, &via->sent_by.host, "192.168.1.1");
502
503 /* "Organization: \r" */
504 str.ptr = "Organization";
505 str.slen = 12;
Benny Prijono40f2f642006-01-30 18:40:05 +0000506 generic = pjsip_generic_string_hdr_create(pool, &str, NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000507 pjsip_msg_add_hdr(msg, (pjsip_hdr*)generic);
508 generic->hvalue.ptr = NULL;
509 generic->hvalue.slen = 0;
510
511 /* "Max-Forwards: 70\n" */
512 str.ptr = "Max-Forwards";
513 str.slen = 12;
Benny Prijono40f2f642006-01-30 18:40:05 +0000514 generic = pjsip_generic_string_hdr_create(pool, &str, NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000515 pjsip_msg_add_hdr(msg, (pjsip_hdr*)generic);
516 str.ptr = "70";
517 str.slen = 2;
518 generic->hvalue = str;
519
520 /* "X-Header: \r\n" */
521 str.ptr = "X-Header";
522 str.slen = 8;
Benny Prijono40f2f642006-01-30 18:40:05 +0000523 generic = pjsip_generic_string_hdr_create(pool, &str, NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000524 pjsip_msg_add_hdr(msg, (pjsip_hdr*)generic);
525 str.ptr = NULL;
526 str.slen = 0;
527 generic->hvalue = str;
528
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000529 /* P-Associated-URI:\r\n */
530 str.ptr = "P-Associated-URI";
531 str.slen = 16;
Benny Prijono40f2f642006-01-30 18:40:05 +0000532 generic = pjsip_generic_string_hdr_create(pool, &str, NULL);
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000533 pjsip_msg_add_hdr(msg, (pjsip_hdr*)generic);
534 str.ptr = NULL;
535 str.slen = 0;
536 generic->hvalue = str;
537
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000538 return msg;
539}
Benny Prijono3818c932005-11-22 01:06:33 +0000540
541static pjsip_msg *create_msg1(pj_pool_t *pool)
542{
543 pjsip_via_hdr *via;
544 pjsip_route_hdr *route;
545 pjsip_name_addr *name_addr;
Benny Prijono0c2bc612006-01-10 13:31:40 +0000546 pjsip_sip_uri *url;
Benny Prijono40f2f642006-01-30 18:40:05 +0000547 pjsip_max_fwd_hdr *max_fwd;
Benny Prijono3818c932005-11-22 01:06:33 +0000548 pjsip_to_hdr *to;
549 pjsip_from_hdr *from;
550 pjsip_contact_hdr *contact;
551 pjsip_ctype_hdr *ctype;
552 pjsip_cid_hdr *cid;
553 pjsip_clen_hdr *clen;
554 pjsip_cseq_hdr *cseq;
555 pjsip_msg *msg = pjsip_msg_create(pool, PJSIP_RESPONSE_MSG);
556 pjsip_msg_body *body;
557
558 //"SIP/2.0 200 OK\r\n"
559 msg->line.status.code = 200;
560 msg->line.status.reason = pj_str("OK");
561
Benny Prijono4cf8d1c2007-04-28 15:37:49 +0000562 //"Via: SIP/2.0/SCTP server10.biloxi.com;branch=z9hG4bKnashds8;rport;received=192.0.2.1\r\n"
Benny Prijono3818c932005-11-22 01:06:33 +0000563 via = pjsip_via_hdr_create(pool);
564 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
565 via->transport = pj_str("SCTP");
566 via->sent_by.host = pj_str("server10.biloxi.com");
567 via->branch_param = pj_str("z9hG4bKnashds8");
568 via->rport_param = 0;
569 via->recvd_param = pj_str("192.0.2.1");
570
571 //"Via: SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2\r\n"
572 via = pjsip_via_hdr_create(pool);
573 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
574 via->transport = pj_str("UDP");
575 via->sent_by.host = pj_str("bigbox3.site3.atlanta.com");
576 via->branch_param = pj_str("z9hG4bK77ef4c2312983.1");
577 via->recvd_param = pj_str("192.0.2.2");
578
579 //"Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds ;received=192.0.2.3\r\n"
580 via = pjsip_via_hdr_create(pool);
581 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
582 via->transport = pj_str("UDP");
583 via->sent_by.host = pj_str("pc33.atlanta.com");
584 via->branch_param = pj_str("z9hG4bK776asdhds");
585 via->recvd_param = pj_str("192.0.2.3");
586
587 //"Route: <sip:proxy.sipprovider.com>\r\n"
588 route = pjsip_route_hdr_create(pool);
589 pjsip_msg_add_hdr(msg, (pjsip_hdr*)route);
Benny Prijono40f2f642006-01-30 18:40:05 +0000590 url = pjsip_sip_uri_create(pool, PJ_FALSE);
Benny Prijono3818c932005-11-22 01:06:33 +0000591 route->name_addr.uri = (pjsip_uri*)url;
592 url->host = pj_str("proxy.sipprovider.com");
593
594 //"Route: <sip:proxy.supersip.com:5060>\r\n"
595 route = pjsip_route_hdr_create(pool);
596 pjsip_msg_add_hdr(msg, (pjsip_hdr*)route);
Benny Prijono40f2f642006-01-30 18:40:05 +0000597 url = pjsip_sip_uri_create(pool, PJ_FALSE);
Benny Prijono3818c932005-11-22 01:06:33 +0000598 route->name_addr.uri = (pjsip_uri*)url;
599 url->host = pj_str("proxy.supersip.com");
600 url->port = 5060;
601
602 //"Max-Forwards: 70\r\n"
Benny Prijono40f2f642006-01-30 18:40:05 +0000603 max_fwd = pjsip_max_fwd_hdr_create(pool, 70);
Benny Prijono3818c932005-11-22 01:06:33 +0000604 pjsip_msg_add_hdr(msg, (pjsip_hdr*)max_fwd);
Benny Prijono3818c932005-11-22 01:06:33 +0000605
606 //"To: Bob <sip:bob@biloxi.com>;tag=a6c85cf\r\n"
607 to = pjsip_to_hdr_create(pool);
608 pjsip_msg_add_hdr(msg, (pjsip_hdr*)to);
609 name_addr = pjsip_name_addr_create(pool);
610 name_addr->display = pj_str("Bob");
611 to->uri = (pjsip_uri*)name_addr;
Benny Prijono40f2f642006-01-30 18:40:05 +0000612 url = pjsip_sip_uri_create(pool, PJ_FALSE);
Benny Prijono3818c932005-11-22 01:06:33 +0000613 name_addr->uri = (pjsip_uri*)url;
614 url->user = pj_str("bob");
615 url->host = pj_str("biloxi.com");
616 to->tag = pj_str("a6c85cf");
617
618 //"From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n"
619 from = pjsip_from_hdr_create(pool);
620 pjsip_msg_add_hdr(msg, (pjsip_hdr*)from);
621 name_addr = pjsip_name_addr_create(pool);
622 name_addr->display = pj_str("Alice");
623 from->uri = (pjsip_uri*)name_addr;
Benny Prijono40f2f642006-01-30 18:40:05 +0000624 url = pjsip_sip_uri_create(pool, PJ_FALSE);
Benny Prijono3818c932005-11-22 01:06:33 +0000625 name_addr->uri = (pjsip_uri*)url;
626 url->user = pj_str("alice");
627 url->host = pj_str("atlanta.com");
628 from->tag = pj_str("1928301774");
629
630 //"Call-ID: a84b4c76e66710@pc33.atlanta.com\r\n"
631 cid = pjsip_cid_hdr_create(pool);
632 pjsip_msg_add_hdr(msg, (pjsip_hdr*)cid);
633 cid->id = pj_str("a84b4c76e66710@pc33.atlanta.com");
634
635 //"CSeq: 314159 INVITE\r\n"
636 cseq = pjsip_cseq_hdr_create(pool);
637 pjsip_msg_add_hdr(msg, (pjsip_hdr*)cseq);
638 cseq->cseq = 314159;
639 pjsip_method_set(&cseq->method, PJSIP_INVITE_METHOD);
640
641 //"Contact: <sips:bob@192.0.2.4>\r\n"
642 contact = pjsip_contact_hdr_create(pool);
643 pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
644 name_addr = pjsip_name_addr_create(pool);
645 contact->uri = (pjsip_uri*)name_addr;
Benny Prijono40f2f642006-01-30 18:40:05 +0000646 url = pjsip_sip_uri_create(pool, PJ_TRUE);
Benny Prijono3818c932005-11-22 01:06:33 +0000647 name_addr->uri = (pjsip_uri*)url;
648 url->user = pj_str("bob");
649 url->host = pj_str("192.0.2.4");
650
651 //"Content-Type: application/sdp\r\n"
652 ctype = pjsip_ctype_hdr_create(pool);
653 pjsip_msg_add_hdr(msg, (pjsip_hdr*)ctype);
654 ctype->media.type = pj_str("application");
655 ctype->media.subtype = pj_str("sdp");
656
657 //"Content-Length: 150\r\n"
658 clen = pjsip_clen_hdr_create(pool);
659 pjsip_msg_add_hdr(msg, (pjsip_hdr*)clen);
660 clen->len = 150;
661
662 // Body
Benny Prijonoa1e69682007-05-11 15:14:34 +0000663 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
Benny Prijono3818c932005-11-22 01:06:33 +0000664 msg->body = body;
665 body->content_type.type = pj_str("application");
666 body->content_type.subtype = pj_str("sdp");
Benny Prijonoa1e69682007-05-11 15:14:34 +0000667 body->data = (void*)
Benny Prijono3818c932005-11-22 01:06:33 +0000668 "v=0\r\n"
669 "o=alice 53655765 2353687637 IN IP4 pc33.atlanta.com\r\n"
670 "s=-\r\n"
671 "t=0 0\r\n"
672 "c=IN IP4 pc33.atlanta.com\r\n"
673 "m=audio 3456 RTP/AVP 0 1 3 99\r\n"
674 "a=rtpmap:0 PCMU/8000\r\n";
Benny Prijonoa1e69682007-05-11 15:14:34 +0000675 body->len = pj_native_strlen((const char*) body->data);
Benny Prijono3818c932005-11-22 01:06:33 +0000676 body->print_body = &pjsip_print_text_body;
677
678 return msg;
679}
680
681/*****************************************************************************/
682
Benny Prijonoe93e2872006-06-28 16:46:49 +0000683static pj_status_t simple_test(void)
Benny Prijono3818c932005-11-22 01:06:33 +0000684{
Benny Prijonoe93e2872006-06-28 16:46:49 +0000685 unsigned i;
Benny Prijono3818c932005-11-22 01:06:33 +0000686 pj_status_t status;
Benny Prijono3818c932005-11-22 01:06:33 +0000687
Benny Prijono85598d92006-01-07 18:44:25 +0000688 PJ_LOG(3,(THIS_FILE, " simple test.."));
Benny Prijono3818c932005-11-22 01:06:33 +0000689 for (i=0; i<PJ_ARRAY_SIZE(test_array); ++i) {
Benny Prijonoe93e2872006-06-28 16:46:49 +0000690 pj_pool_t *pool;
Benny Prijono3818c932005-11-22 01:06:33 +0000691 pool = pjsip_endpt_create_pool(endpt, NULL, POOL_SIZE, POOL_SIZE);
692 status = test_entry( pool, &test_array[i] );
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000693 pjsip_endpt_release_pool(endpt, pool);
Benny Prijono3818c932005-11-22 01:06:33 +0000694
695 if (status != PJ_SUCCESS)
696 return status;
697 }
698
Benny Prijonoe93e2872006-06-28 16:46:49 +0000699 return PJ_SUCCESS;
700}
701
702
Benny Prijonod0bd4982007-12-02 15:40:52 +0000703#if INCLUDE_BENCHMARKS
Benny Prijonoe93e2872006-06-28 16:46:49 +0000704static int msg_benchmark(unsigned *p_detect, unsigned *p_parse,
705 unsigned *p_print)
706{
707 pj_status_t status;
708 pj_pool_t *pool;
709 int i, loop;
710 pj_timestamp zero;
711 pj_time_val elapsed;
712 pj_highprec_t avg_detect, avg_parse, avg_print, kbytes;
713
714
Benny Prijonoac623b32006-07-03 15:19:31 +0000715 pj_bzero(&var, sizeof(var));
Benny Prijonoe93e2872006-06-28 16:46:49 +0000716 zero.u64 = 0;
717
Benny Prijono3818c932005-11-22 01:06:33 +0000718 for (loop=0; loop<LOOP; ++loop) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000719 for (i=0; i<(int)PJ_ARRAY_SIZE(test_array); ++i) {
Benny Prijono3818c932005-11-22 01:06:33 +0000720 pool = pjsip_endpt_create_pool(endpt, NULL, POOL_SIZE, POOL_SIZE);
721 status = test_entry( pool, &test_array[i] );
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000722 pjsip_endpt_release_pool(endpt, pool);
Benny Prijono3818c932005-11-22 01:06:33 +0000723
724 if (status != PJ_SUCCESS)
725 return status;
726 }
727 }
728
Benny Prijonoe93e2872006-06-28 16:46:49 +0000729 kbytes = var.detect_len;
Benny Prijono3818c932005-11-22 01:06:33 +0000730 pj_highprec_mod(kbytes, 1000000);
731 pj_highprec_div(kbytes, 100000);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000732 elapsed = pj_elapsed_time(&zero, &var.detect_time);
733 avg_detect = pj_elapsed_usec(&zero, &var.detect_time);
Benny Prijono3818c932005-11-22 01:06:33 +0000734 pj_highprec_mul(avg_detect, AVERAGE_MSG_LEN);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000735 pj_highprec_div(avg_detect, var.detect_len);
Benny Prijono3818c932005-11-22 01:06:33 +0000736 avg_detect = 1000000 / avg_detect;
737
Benny Prijono85598d92006-01-07 18:44:25 +0000738 PJ_LOG(3,(THIS_FILE,
739 " %u.%u MB detected in %d.%03ds (avg=%d msg detection/sec)",
Benny Prijonoe93e2872006-06-28 16:46:49 +0000740 (unsigned)(var.detect_len/1000000), (unsigned)kbytes,
Benny Prijono85598d92006-01-07 18:44:25 +0000741 elapsed.sec, elapsed.msec,
742 (unsigned)avg_detect));
Benny Prijonoe93e2872006-06-28 16:46:49 +0000743 *p_detect = (unsigned)avg_detect;
Benny Prijono3818c932005-11-22 01:06:33 +0000744
Benny Prijonoe93e2872006-06-28 16:46:49 +0000745 kbytes = var.parse_len;
Benny Prijono3818c932005-11-22 01:06:33 +0000746 pj_highprec_mod(kbytes, 1000000);
747 pj_highprec_div(kbytes, 100000);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000748 elapsed = pj_elapsed_time(&zero, &var.parse_time);
749 avg_parse = pj_elapsed_usec(&zero, &var.parse_time);
Benny Prijono3818c932005-11-22 01:06:33 +0000750 pj_highprec_mul(avg_parse, AVERAGE_MSG_LEN);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000751 pj_highprec_div(avg_parse, var.parse_len);
Benny Prijono3818c932005-11-22 01:06:33 +0000752 avg_parse = 1000000 / avg_parse;
753
Benny Prijono85598d92006-01-07 18:44:25 +0000754 PJ_LOG(3,(THIS_FILE,
755 " %u.%u MB parsed in %d.%03ds (avg=%d msg parsing/sec)",
Benny Prijonoe93e2872006-06-28 16:46:49 +0000756 (unsigned)(var.parse_len/1000000), (unsigned)kbytes,
Benny Prijono85598d92006-01-07 18:44:25 +0000757 elapsed.sec, elapsed.msec,
758 (unsigned)avg_parse));
Benny Prijonoe93e2872006-06-28 16:46:49 +0000759 *p_parse = (unsigned)avg_parse;
Benny Prijono3818c932005-11-22 01:06:33 +0000760
Benny Prijonoe93e2872006-06-28 16:46:49 +0000761 kbytes = var.print_len;
Benny Prijono3818c932005-11-22 01:06:33 +0000762 pj_highprec_mod(kbytes, 1000000);
763 pj_highprec_div(kbytes, 100000);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000764 elapsed = pj_elapsed_time(&zero, &var.print_time);
765 avg_print = pj_elapsed_usec(&zero, &var.print_time);
Benny Prijono3818c932005-11-22 01:06:33 +0000766 pj_highprec_mul(avg_print, AVERAGE_MSG_LEN);
Benny Prijonoe93e2872006-06-28 16:46:49 +0000767 pj_highprec_div(avg_print, var.print_len);
Benny Prijono3818c932005-11-22 01:06:33 +0000768 avg_print = 1000000 / avg_print;
769
Benny Prijono85598d92006-01-07 18:44:25 +0000770 PJ_LOG(3,(THIS_FILE,
771 " %u.%u MB printed in %d.%03ds (avg=%d msg print/sec)",
Benny Prijonoe93e2872006-06-28 16:46:49 +0000772 (unsigned)(var.print_len/1000000), (unsigned)kbytes,
Benny Prijono85598d92006-01-07 18:44:25 +0000773 elapsed.sec, elapsed.msec,
774 (unsigned)avg_print));
Benny Prijono3818c932005-11-22 01:06:33 +0000775
Benny Prijonoe93e2872006-06-28 16:46:49 +0000776 *p_print = (unsigned)avg_print;
Benny Prijono3818c932005-11-22 01:06:33 +0000777 return status;
778}
Benny Prijonod0bd4982007-12-02 15:40:52 +0000779#endif /* INCLUDE_BENCHMARKS */
Benny Prijono3818c932005-11-22 01:06:33 +0000780
781/*****************************************************************************/
Benny Prijono8aea0d92007-04-29 06:23:11 +0000782/* Test various header parsing and production */
783static int hdr_test_success(pjsip_hdr *h);
784static int hdr_test_accept0(pjsip_hdr *h);
785static int hdr_test_accept1(pjsip_hdr *h);
786static int hdr_test_accept2(pjsip_hdr *h);
787static int hdr_test_allow0(pjsip_hdr *h);
788static int hdr_test_authorization(pjsip_hdr *h);
789static int hdr_test_cid(pjsip_hdr *h);
790static int hdr_test_contact0(pjsip_hdr *h);
791static int hdr_test_contact1(pjsip_hdr *h);
Benny Prijono123fad72008-06-11 11:18:04 +0000792static int hdr_test_contact_q0(pjsip_hdr *h);
793static int hdr_test_contact_q1(pjsip_hdr *h);
794static int hdr_test_contact_q2(pjsip_hdr *h);
795static int hdr_test_contact_q3(pjsip_hdr *h);
796static int hdr_test_contact_q4(pjsip_hdr *h);
Benny Prijono8aea0d92007-04-29 06:23:11 +0000797static int hdr_test_content_length(pjsip_hdr *h);
798static int hdr_test_content_type(pjsip_hdr *h);
799static int hdr_test_from(pjsip_hdr *h);
800static int hdr_test_proxy_authenticate(pjsip_hdr *h);
801static int hdr_test_record_route(pjsip_hdr *h);
802static int hdr_test_supported(pjsip_hdr *h);
803static int hdr_test_to(pjsip_hdr *h);
804static int hdr_test_via(pjsip_hdr *h);
Benny Prijonod0bd4982007-12-02 15:40:52 +0000805static int hdr_test_via_ipv6_1(pjsip_hdr *h);
806static int hdr_test_via_ipv6_2(pjsip_hdr *h);
807static int hdr_test_via_ipv6_3(pjsip_hdr *h);
Benny Prijono054c5df2008-05-15 10:07:36 +0000808static int hdr_test_retry_after1(pjsip_hdr *h);
Benny Prijono8aea0d92007-04-29 06:23:11 +0000809
810
811#define GENERIC_PARAM "p0=a;p1=\"ab:;cd\";p2=ab%3acd;p3"
812#define GENERIC_PARAM_PARSED "p0=a;p1=\"ab:;cd\";p2=ab:cd;p3"
Benny Prijonod0bd4982007-12-02 15:40:52 +0000813#define PARAM_CHAR "][/:&+$"
Benny Prijono8aea0d92007-04-29 06:23:11 +0000814#define SIMPLE_ADDR_SPEC "sip:host"
Benny Prijono8fcbe2d2007-04-29 18:07:53 +0000815#define ADDR_SPEC SIMPLE_ADDR_SPEC ";"PARAM_CHAR"="PARAM_CHAR ";p1=\";\""
Benny Prijono8aea0d92007-04-29 06:23:11 +0000816#define NAME_ADDR "<" ADDR_SPEC ">"
817
818#define HDR_FLAG_PARSE_FAIL 1
819#define HDR_FLAG_DONT_PRINT 2
820
821struct hdr_test_t
822{
823 char *hname;
824 char *hshort_name;
825 char *hcontent;
826 int (*test)(pjsip_hdr*);
827 unsigned flags;
828} hdr_test_data[] =
829{
830 {
831 /* Empty Accept */
832 "Accept", NULL,
833 "",
834 &hdr_test_accept0
835 },
836
837 {
838 /* Overflowing generic string header */
839 "Accept", NULL,
840 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
841 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
842 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
843 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
844 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
845 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
846 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
847 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a",
848 &hdr_test_success,
849 HDR_FLAG_PARSE_FAIL
850 },
851
852 {
853 /* Normal Accept */
854 "Accept", NULL,
855 "application/*, text/plain",
856 &hdr_test_accept1
857 },
858
859 {
860 /* Accept with params */
861 "Accept", NULL,
862 "application/*;p1=v1, text/plain",
863 &hdr_test_accept2
864 },
865
866 {
867 /* Empty Allow */
868 "Allow", NULL,
869 "",
870 &hdr_test_allow0,
871 },
872
873 {
874 /* Authorization, testing which params should be quoted */
875 "Authorization", NULL,
876 "Digest username=\"username\", realm=\"realm\", nonce=\"nonce\", " \
877 "uri=\"sip:domain\", response=\"RESPONSE\", algorithm=MD5, " \
878 "cnonce=\"CNONCE\", opaque=\"OPAQUE\", qop=auth, nc=00000001",
879 &hdr_test_authorization
880 },
881
882 {
883 /* Call ID */
884 "Call-ID", "i",
885 "-.!%*_+`'~()<>:\\\"/[]?{}",
886 &hdr_test_cid,
887 },
888
889 {
890 /* Parameter belong to hparam */
891 "Contact", "m",
892 SIMPLE_ADDR_SPEC ";p1=v1",
893 &hdr_test_contact0,
894 HDR_FLAG_DONT_PRINT
895 },
896
897 {
898 /* generic-param in Contact header */
899 "Contact", "m",
900 NAME_ADDR ";" GENERIC_PARAM,
901 &hdr_test_contact1
902 },
903
904 {
Benny Prijono123fad72008-06-11 11:18:04 +0000905 /* q=0 parameter in Contact header */
906 "Contact", "m",
907 NAME_ADDR ";q=0",
908 &hdr_test_contact_q0,
909 HDR_FLAG_DONT_PRINT
910 },
911
912 {
913 /* q=0.5 parameter in Contact header */
914 "Contact", "m",
915 NAME_ADDR ";q=0.5",
916 &hdr_test_contact_q1
917 },
918
919 {
920 /* q=1 parameter in Contact header */
921 "Contact", "m",
922 NAME_ADDR ";q=1",
923 &hdr_test_contact_q2
924 },
925
926 {
927 /* q=1.0 parameter in Contact header */
928 "Contact", "m",
929 NAME_ADDR ";q=1.0",
930 &hdr_test_contact_q3,
931 HDR_FLAG_DONT_PRINT
932 },
933
934 {
935 /* q=1.1 parameter in Contact header */
936 "Contact", "m",
937 NAME_ADDR ";q=1.15",
938 &hdr_test_contact_q4
939 },
940
941 {
Benny Prijono8aea0d92007-04-29 06:23:11 +0000942 /* Content-Length */
943 "Content-Length", "l",
944 "10",
945 &hdr_test_content_length
946 },
947
948 {
949 /* Content-Type, with generic-param */
950 "Content-Type", "c",
951 "application/sdp" ";" GENERIC_PARAM,
952 &hdr_test_content_type,
953 HDR_FLAG_DONT_PRINT
954 },
955
956 {
957 /* From, testing parameters and generic-param */
958 "From", "f",
959 NAME_ADDR ";" GENERIC_PARAM,
960 &hdr_test_from
961 },
962
963 {
964 /* Proxy-Authenticate, testing which params should be quoted */
965 "Proxy-Authenticate", NULL,
966 "Digest realm=\"realm\",domain=\"sip:domain\",nonce=\"nonce\"," \
967 "opaque=\"opaque\",stale=true,algorithm=MD5,qop=\"auth\"",
968 &hdr_test_proxy_authenticate
969 },
970
971 {
972 /* Record-Route, param belong to header */
973 "Record-Route", NULL,
974 NAME_ADDR ";" GENERIC_PARAM,
975 &hdr_test_record_route
976 },
977
978 {
979 /* Empty Supported */
980 "Supported", "k",
981 "",
982 &hdr_test_supported,
983 },
984
985 {
986 /* To */
987 "To", "t",
988 NAME_ADDR ";" GENERIC_PARAM,
989 &hdr_test_to
990 },
991
992 {
993 /* Via */
994 "Via", "v",
995 "SIP/2.0/XYZ host" ";" GENERIC_PARAM,
996 &hdr_test_via
Benny Prijonod0bd4982007-12-02 15:40:52 +0000997 },
998
999 {
1000 /* Via with IPv6 */
1001 "Via", "v",
1002 "SIP/2.0/UDP [::1]",
1003 &hdr_test_via_ipv6_1
1004 },
1005
1006 {
1007 /* Via with IPv6 */
1008 "Via", "v",
1009 "SIP/2.0/UDP [::1]:5061",
1010 &hdr_test_via_ipv6_2
1011 },
1012
1013 {
1014 /* Via with IPv6 */
1015 "Via", "v",
1016 "SIP/2.0/UDP [::1];rport=5061;received=::2",
1017 &hdr_test_via_ipv6_3
Benny Prijono054c5df2008-05-15 10:07:36 +00001018 },
1019
1020 {
1021 /* Retry-After header with comment */
1022 "Retry-After", NULL,
1023 "10(Already Pending Register)",
1024 &hdr_test_retry_after1
Benny Prijono8aea0d92007-04-29 06:23:11 +00001025 }
1026};
1027
1028static int hdr_test_success(pjsip_hdr *h)
1029{
1030 PJ_UNUSED_ARG(h);
1031 return 0;
1032}
1033
1034/* "" */
1035static int hdr_test_accept0(pjsip_hdr *h)
1036{
1037 pjsip_accept_hdr *hdr = (pjsip_accept_hdr*)h;
1038
1039 if (h->type != PJSIP_H_ACCEPT)
1040 return -1010;
1041
1042 if (hdr->count != 0)
1043 return -1020;
1044
1045 return 0;
1046}
1047
Benny Prijono64898b52007-05-01 06:36:15 +00001048/* "application/ *, text/plain\r\n" */
Benny Prijono8aea0d92007-04-29 06:23:11 +00001049static int hdr_test_accept1(pjsip_hdr *h)
1050{
1051 pjsip_accept_hdr *hdr = (pjsip_accept_hdr*)h;
1052
1053 if (h->type != PJSIP_H_ACCEPT)
1054 return -1110;
1055
1056 if (hdr->count != 2)
1057 return -1120;
1058
1059 if (pj_strcmp2(&hdr->values[0], "application/*"))
1060 return -1130;
1061
1062 if (pj_strcmp2(&hdr->values[1], "text/plain"))
1063 return -1140;
1064
1065 return 0;
1066}
1067
Benny Prijono64898b52007-05-01 06:36:15 +00001068/* "application/ *;p1=v1, text/plain\r\n" */
Benny Prijono8aea0d92007-04-29 06:23:11 +00001069static int hdr_test_accept2(pjsip_hdr *h)
1070{
1071 pjsip_accept_hdr *hdr = (pjsip_accept_hdr*)h;
1072
1073 if (h->type != PJSIP_H_ACCEPT)
1074 return -1210;
1075
1076 if (hdr->count != 2)
1077 return -1220;
1078
1079 if (pj_strcmp2(&hdr->values[0], "application/*;p1=v1"))
1080 return -1230;
1081
1082 if (pj_strcmp2(&hdr->values[1], "text/plain"))
1083 return -1240;
1084
1085 return 0;
1086}
1087
1088/* "" */
1089static int hdr_test_allow0(pjsip_hdr *h)
1090{
1091 pjsip_allow_hdr *hdr = (pjsip_allow_hdr*)h;
1092
1093 if (h->type != PJSIP_H_ALLOW)
1094 return -1310;
1095
1096 if (hdr->count != 0)
1097 return -1320;
1098
1099 return 0;
1100
1101}
1102
1103
1104/*
1105 "Digest username=\"username\", realm=\"realm\", nonce=\"nonce\", " \
1106 "uri=\"sip:domain\", response=\"RESPONSE\", algorithm=MD5, " \
1107 "cnonce=\"CNONCE\", opaque=\"OPAQUE\", qop=auth, nc=00000001",
1108 */
1109static int hdr_test_authorization(pjsip_hdr *h)
1110{
1111 pjsip_authorization_hdr *hdr = (pjsip_authorization_hdr*)h;
1112
1113 if (h->type != PJSIP_H_AUTHORIZATION)
1114 return -1410;
1115
1116 if (pj_strcmp2(&hdr->scheme, "Digest"))
1117 return -1420;
1118
1119 if (pj_strcmp2(&hdr->credential.digest.username, "username"))
1120 return -1421;
1121
1122 if (pj_strcmp2(&hdr->credential.digest.realm, "realm"))
1123 return -1422;
1124
1125 if (pj_strcmp2(&hdr->credential.digest.nonce, "nonce"))
1126 return -1423;
1127
1128 if (pj_strcmp2(&hdr->credential.digest.uri, "sip:domain"))
1129 return -1424;
1130
1131 if (pj_strcmp2(&hdr->credential.digest.response, "RESPONSE"))
1132 return -1425;
1133
1134 if (pj_strcmp2(&hdr->credential.digest.algorithm, "MD5"))
1135 return -1426;
1136
1137 if (pj_strcmp2(&hdr->credential.digest.cnonce, "CNONCE"))
1138 return -1427;
1139
1140 if (pj_strcmp2(&hdr->credential.digest.opaque, "OPAQUE"))
1141 return -1428;
1142
1143 if (pj_strcmp2(&hdr->credential.digest.qop, "auth"))
1144 return -1429;
1145
1146 if (pj_strcmp2(&hdr->credential.digest.nc, "00000001"))
1147 return -1430;
1148
1149 return 0;
1150}
1151
1152
1153/*
1154 "-.!%*_+`'~()<>:\\\"/[]?{}\r\n"
1155 */
1156static int hdr_test_cid(pjsip_hdr *h)
1157{
1158 pjsip_cid_hdr *hdr = (pjsip_cid_hdr*)h;
1159
1160 if (h->type != PJSIP_H_CALL_ID)
1161 return -1510;
1162
Benny Prijono91a5a3a2007-09-24 21:16:48 +00001163 if (pj_strcmp2(&hdr->id, "-.!%*_+`'~()<>:\\\"/[]?{}"))
Benny Prijono8aea0d92007-04-29 06:23:11 +00001164 return -1520;
1165
1166 return 0;
1167}
1168
1169/*
1170 #define SIMPLE_ADDR_SPEC "sip:host"
1171 */
1172static int test_simple_addr_spec(pjsip_uri *uri)
1173{
1174 pjsip_sip_uri *sip_uri = (pjsip_sip_uri *)pjsip_uri_get_uri(uri);
1175
1176 if (!PJSIP_URI_SCHEME_IS_SIP(uri))
1177 return -900;
1178
1179 if (pj_strcmp2(&sip_uri->host, "host"))
1180 return -910;
1181
1182 if (sip_uri->port != 0)
1183 return -920;
1184
1185 return 0;
1186}
1187
1188/*
Benny Prijonod0bd4982007-12-02 15:40:52 +00001189#define PARAM_CHAR "][/:&+$"
Benny Prijono8aea0d92007-04-29 06:23:11 +00001190#define SIMPLE_ADDR_SPEC "sip:host"
Benny Prijono8fcbe2d2007-04-29 18:07:53 +00001191#define ADDR_SPEC SIMPLE_ADDR_SPEC ";"PARAM_CHAR"="PARAM_CHAR ";p1=\";\""
Benny Prijono8aea0d92007-04-29 06:23:11 +00001192#define NAME_ADDR "<" ADDR_SPEC ">"
1193 */
Benny Prijono64898b52007-05-01 06:36:15 +00001194static int nameaddr_test(void *uri)
Benny Prijono8aea0d92007-04-29 06:23:11 +00001195{
Benny Prijono64898b52007-05-01 06:36:15 +00001196 pjsip_sip_uri *sip_uri=(pjsip_sip_uri *)pjsip_uri_get_uri((pjsip_uri*)uri);
Benny Prijono8aea0d92007-04-29 06:23:11 +00001197 pjsip_param *param;
1198 int rc;
1199
1200 if (!PJSIP_URI_SCHEME_IS_SIP(uri))
1201 return -930;
1202
1203 rc = test_simple_addr_spec((pjsip_uri*)sip_uri);
1204 if (rc != 0)
1205 return rc;
1206
Benny Prijono8fcbe2d2007-04-29 18:07:53 +00001207 if (pj_list_size(&sip_uri->other_param) != 2)
Benny Prijono8aea0d92007-04-29 06:23:11 +00001208 return -940;
1209
1210 param = sip_uri->other_param.next;
1211
1212 if (pj_strcmp2(&param->name, PARAM_CHAR))
1213 return -942;
1214
1215 if (pj_strcmp2(&param->value, PARAM_CHAR))
1216 return -943;
1217
Benny Prijono8fcbe2d2007-04-29 18:07:53 +00001218 param = param->next;
1219 if (pj_strcmp2(&param->name, "p1"))
1220 return -942;
1221 if (pj_strcmp2(&param->value, "\";\""))
1222 return -943;
1223
Benny Prijono8aea0d92007-04-29 06:23:11 +00001224 return 0;
1225}
1226
1227/*
1228#define GENERIC_PARAM "p0=a;p1=\"ab:;cd\";p2=ab%3acd;p3"
1229 */
1230static int generic_param_test(pjsip_param *param_head)
1231{
1232 pjsip_param *param;
1233
1234 if (pj_list_size(param_head) != 4)
1235 return -950;
1236
1237 param = param_head->next;
1238
1239 if (pj_strcmp2(&param->name, "p0"))
1240 return -952;
1241 if (pj_strcmp2(&param->value, "a"))
1242 return -953;
1243
1244 param = param->next;
1245 if (pj_strcmp2(&param->name, "p1"))
1246 return -954;
1247 if (pj_strcmp2(&param->value, "\"ab:;cd\""))
1248 return -955;
1249
1250 param = param->next;
1251 if (pj_strcmp2(&param->name, "p2"))
1252 return -956;
1253 if (pj_strcmp2(&param->value, "ab:cd"))
1254 return -957;
1255
1256 param = param->next;
1257 if (pj_strcmp2(&param->name, "p3"))
1258 return -958;
1259 if (pj_strcmp2(&param->value, ""))
1260 return -959;
1261
1262 return 0;
1263}
1264
1265
1266
1267/*
1268 SIMPLE_ADDR_SPEC ";p1=v1\r\n"
1269 */
1270static int hdr_test_contact0(pjsip_hdr *h)
1271{
1272 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1273 pjsip_param *param;
1274 int rc;
1275
1276 if (h->type != PJSIP_H_CONTACT)
1277 return -1610;
1278
1279 rc = test_simple_addr_spec(hdr->uri);
1280 if (rc != 0)
1281 return rc;
1282
1283 if (pj_list_size(&hdr->other_param) != 1)
1284 return -1620;
1285
1286 param = hdr->other_param.next;
1287
1288 if (pj_strcmp2(&param->name, "p1"))
1289 return -1630;
1290
1291 if (pj_strcmp2(&param->value, "v1"))
1292 return -1640;
1293
1294 return 0;
1295}
1296
1297/*
1298 NAME_ADDR GENERIC_PARAM "\r\n",
1299 */
1300static int hdr_test_contact1(pjsip_hdr *h)
1301{
1302 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1303 int rc;
1304
1305 if (h->type != PJSIP_H_CONTACT)
1306 return -1710;
1307
1308 rc = nameaddr_test(hdr->uri);
1309 if (rc != 0)
1310 return rc;
1311
1312 rc = generic_param_test(&hdr->other_param);
1313 if (rc != 0)
1314 return rc;
1315
1316 return 0;
1317}
1318
1319/*
Benny Prijono123fad72008-06-11 11:18:04 +00001320 NAME_ADDR ";q=0"
1321 */
1322static int hdr_test_contact_q0(pjsip_hdr *h)
1323{
1324 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1325 int rc;
1326
1327 if (h->type != PJSIP_H_CONTACT)
1328 return -1710;
1329
1330 rc = nameaddr_test(hdr->uri);
1331 if (rc != 0)
1332 return rc;
1333
1334 if (hdr->q1000 != 0)
1335 return -1711;
1336
1337 return 0;
1338}
1339
1340/*
1341 NAME_ADDR ";q=0.5"
1342 */
1343static int hdr_test_contact_q1(pjsip_hdr *h)
1344{
1345 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1346 int rc;
1347
1348 if (h->type != PJSIP_H_CONTACT)
1349 return -1710;
1350
1351 rc = nameaddr_test(hdr->uri);
1352 if (rc != 0)
1353 return rc;
1354
1355 if (hdr->q1000 != 500)
1356 return -1712;
1357
1358 return 0;
1359}
1360
1361/*
1362 NAME_ADDR ";q=1"
1363 */
1364static int hdr_test_contact_q2(pjsip_hdr *h)
1365{
1366 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1367 int rc;
1368
1369 if (h->type != PJSIP_H_CONTACT)
1370 return -1710;
1371
1372 rc = nameaddr_test(hdr->uri);
1373 if (rc != 0)
1374 return rc;
1375
1376 if (hdr->q1000 != 1000)
1377 return -1713;
1378
1379 return 0;
1380}
1381
1382/*
1383 NAME_ADDR ";q=1.0"
1384 */
1385static int hdr_test_contact_q3(pjsip_hdr *h)
1386{
1387 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1388 int rc;
1389
1390 if (h->type != PJSIP_H_CONTACT)
1391 return -1710;
1392
1393 rc = nameaddr_test(hdr->uri);
1394 if (rc != 0)
1395 return rc;
1396
1397 if (hdr->q1000 != 1000)
1398 return -1714;
1399
1400 return 0;
1401}
1402
1403/*
1404 NAME_ADDR ";q=1.15"
1405 */
1406static int hdr_test_contact_q4(pjsip_hdr *h)
1407{
1408 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1409 int rc;
1410
1411 if (h->type != PJSIP_H_CONTACT)
1412 return -1710;
1413
1414 rc = nameaddr_test(hdr->uri);
1415 if (rc != 0)
1416 return rc;
1417
1418 if (hdr->q1000 != 1150)
1419 return -1715;
1420
1421 return 0;
1422}
1423
1424/*
Benny Prijono8aea0d92007-04-29 06:23:11 +00001425 "10"
1426 */
1427static int hdr_test_content_length(pjsip_hdr *h)
1428{
1429 pjsip_clen_hdr *hdr = (pjsip_clen_hdr*)h;
1430
1431 if (h->type != PJSIP_H_CONTENT_LENGTH)
1432 return -1810;
1433
1434 if (hdr->len != 10)
1435 return -1820;
1436
1437 return 0;
1438}
1439
1440/*
1441 "application/sdp" GENERIC_PARAM,
1442 */
1443static int hdr_test_content_type(pjsip_hdr *h)
1444{
1445 pjsip_ctype_hdr *hdr = (pjsip_ctype_hdr*)h;
1446
1447 if (h->type != PJSIP_H_CONTENT_TYPE)
1448 return -1910;
1449
1450 if (pj_strcmp2(&hdr->media.type, "application"))
1451 return -1920;
1452
1453 if (pj_strcmp2(&hdr->media.subtype, "sdp"))
1454 return -1930;
1455
1456 /* Currently, if the media parameter contains escaped characters,
1457 * pjsip will print the parameter unescaped.
1458 */
1459 PJ_TODO(FIX_PARAMETER_IN_MEDIA_TYPE);
1460
1461 if (pj_strcmp2(&hdr->media.param, ";" GENERIC_PARAM_PARSED))
1462 return -1940;
1463
1464 return 0;
1465}
1466
1467/*
1468 NAME_ADDR GENERIC_PARAM,
1469 */
1470static int hdr_test_from(pjsip_hdr *h)
1471{
1472 pjsip_from_hdr *hdr = (pjsip_from_hdr*)h;
1473 int rc;
1474
1475 if (h->type != PJSIP_H_FROM)
1476 return -2010;
1477
1478 rc = nameaddr_test(hdr->uri);
1479 if (rc != 0)
1480 return rc;
1481
1482 rc = generic_param_test(&hdr->other_param);
1483 if (rc != 0)
1484 return rc;
1485
1486 return 0;
1487}
1488
1489/*
1490 "Digest realm=\"realm\", domain=\"sip:domain\", nonce=\"nonce\", " \
1491 "opaque=\"opaque\", stale=true, algorithm=MD5, qop=\"auth\"",
1492 */
1493static int hdr_test_proxy_authenticate(pjsip_hdr *h)
1494{
1495 pjsip_proxy_authenticate_hdr *hdr = (pjsip_proxy_authenticate_hdr*)h;
1496
1497 if (h->type != PJSIP_H_PROXY_AUTHENTICATE)
1498 return -2110;
1499
1500 if (pj_strcmp2(&hdr->scheme, "Digest"))
1501 return -2120;
1502
1503 if (pj_strcmp2(&hdr->challenge.digest.realm, "realm"))
1504 return -2130;
1505
1506 if (pj_strcmp2(&hdr->challenge.digest.domain, "sip:domain"))
1507 return -2140;
1508
1509 if (pj_strcmp2(&hdr->challenge.digest.nonce, "nonce"))
1510 return -2150;
1511
1512 if (pj_strcmp2(&hdr->challenge.digest.opaque, "opaque"))
1513 return -2160;
1514
1515 if (hdr->challenge.digest.stale != 1)
1516 return -2170;
1517
1518 if (pj_strcmp2(&hdr->challenge.digest.algorithm, "MD5"))
1519 return -2180;
1520
1521 if (pj_strcmp2(&hdr->challenge.digest.qop, "auth"))
1522 return -2190;
1523
1524 return 0;
1525}
1526
1527/*
1528 NAME_ADDR GENERIC_PARAM,
1529 */
1530static int hdr_test_record_route(pjsip_hdr *h)
1531{
1532 pjsip_rr_hdr *hdr = (pjsip_rr_hdr*)h;
1533 int rc;
1534
1535 if (h->type != PJSIP_H_RECORD_ROUTE)
1536 return -2210;
1537
Benny Prijono64898b52007-05-01 06:36:15 +00001538 rc = nameaddr_test(&hdr->name_addr);
Benny Prijono8aea0d92007-04-29 06:23:11 +00001539 if (rc != 0)
1540 return rc;
1541
1542 rc = generic_param_test(&hdr->other_param);
1543 if (rc != 0)
1544 return rc;
1545
1546 return 0;
1547
1548}
1549
1550/*
1551 " \r\n"
1552 */
1553static int hdr_test_supported(pjsip_hdr *h)
1554{
1555 pjsip_supported_hdr *hdr = (pjsip_supported_hdr*)h;
1556
1557 if (h->type != PJSIP_H_SUPPORTED)
1558 return -2310;
1559
1560 if (hdr->count != 0)
1561 return -2320;
1562
1563 return 0;
1564}
1565
1566/*
1567 NAME_ADDR GENERIC_PARAM,
1568 */
1569static int hdr_test_to(pjsip_hdr *h)
1570{
1571 pjsip_to_hdr *hdr = (pjsip_to_hdr*)h;
1572 int rc;
1573
1574 if (h->type != PJSIP_H_TO)
1575 return -2410;
1576
1577 rc = nameaddr_test(hdr->uri);
1578 if (rc != 0)
1579 return rc;
1580
1581 rc = generic_param_test(&hdr->other_param);
1582 if (rc != 0)
1583 return rc;
1584
1585 return 0;
1586}
1587
1588/*
1589 "SIP/2.0 host" GENERIC_PARAM
1590 */
1591static int hdr_test_via(pjsip_hdr *h)
1592{
1593 pjsip_via_hdr *hdr = (pjsip_via_hdr*)h;
1594 int rc;
1595
1596 if (h->type != PJSIP_H_VIA)
1597 return -2510;
1598
1599 if (pj_strcmp2(&hdr->transport, "XYZ"))
1600 return -2515;
1601
1602 if (pj_strcmp2(&hdr->sent_by.host, "host"))
1603 return -2520;
1604
1605 if (hdr->sent_by.port != 0)
1606 return -2530;
1607
1608 rc = generic_param_test(&hdr->other_param);
1609 if (rc != 0)
1610 return rc;
1611
1612 return 0;
1613}
1614
1615
Benny Prijonod0bd4982007-12-02 15:40:52 +00001616/*
1617 "SIP/2.0/UDP [::1]"
1618 */
1619static int hdr_test_via_ipv6_1(pjsip_hdr *h)
1620{
1621 pjsip_via_hdr *hdr = (pjsip_via_hdr*)h;
1622
1623 if (h->type != PJSIP_H_VIA)
1624 return -2610;
1625
1626 if (pj_strcmp2(&hdr->transport, "UDP"))
1627 return -2615;
1628
1629 if (pj_strcmp2(&hdr->sent_by.host, "::1"))
1630 return -2620;
1631
1632 if (hdr->sent_by.port != 0)
1633 return -2630;
1634
1635 return 0;
1636}
1637
1638/* "SIP/2.0/UDP [::1]:5061" */
1639static int hdr_test_via_ipv6_2(pjsip_hdr *h)
1640{
1641 pjsip_via_hdr *hdr = (pjsip_via_hdr*)h;
1642
1643 if (h->type != PJSIP_H_VIA)
1644 return -2710;
1645
1646 if (pj_strcmp2(&hdr->transport, "UDP"))
1647 return -2715;
1648
1649 if (pj_strcmp2(&hdr->sent_by.host, "::1"))
1650 return -2720;
1651
1652 if (hdr->sent_by.port != 5061)
1653 return -2730;
1654
1655 return 0;
1656}
1657
1658/* "SIP/2.0/UDP [::1];rport=5061;received=::2" */
1659static int hdr_test_via_ipv6_3(pjsip_hdr *h)
1660{
1661 pjsip_via_hdr *hdr = (pjsip_via_hdr*)h;
1662
1663 if (h->type != PJSIP_H_VIA)
1664 return -2810;
1665
1666 if (pj_strcmp2(&hdr->transport, "UDP"))
1667 return -2815;
1668
1669 if (pj_strcmp2(&hdr->sent_by.host, "::1"))
1670 return -2820;
1671
1672 if (hdr->sent_by.port != 0)
1673 return -2830;
1674
1675 if (pj_strcmp2(&hdr->recvd_param, "::2"))
1676 return -2840;
1677
1678 if (hdr->rport_param != 5061)
1679 return -2850;
1680
1681 return 0;
1682}
1683
Benny Prijono054c5df2008-05-15 10:07:36 +00001684/* "10(Already Pending Register)" */
1685static int hdr_test_retry_after1(pjsip_hdr *h)
1686{
1687 pjsip_retry_after_hdr *hdr = (pjsip_retry_after_hdr*)h;
1688
1689 if (h->type != PJSIP_H_RETRY_AFTER)
1690 return -2910;
1691
1692 if (hdr->ivalue != 10)
1693 return -2920;
1694
1695 if (pj_strcmp2(&hdr->comment, "Already Pending Register"))
1696 return -2930;
1697
1698 return 0;
1699}
1700
Benny Prijono8aea0d92007-04-29 06:23:11 +00001701static int hdr_test(void)
1702{
1703 unsigned i;
1704
1705 PJ_LOG(3,(THIS_FILE, " testing header parsing.."));
1706
1707 for (i=0; i<PJ_ARRAY_SIZE(hdr_test_data); ++i) {
1708 struct hdr_test_t *test = &hdr_test_data[i];
1709 pj_str_t hname;
1710 int len, parsed_len;
1711 pj_pool_t *pool;
1712 pjsip_hdr *parsed_hdr1=NULL, *parsed_hdr2=NULL;
1713 char *input, *output;
1714 int rc;
1715
1716 pool = pjsip_endpt_create_pool(endpt, NULL, POOL_SIZE, POOL_SIZE);
1717
1718 /* Parse the header */
1719 hname = pj_str(test->hname);
1720 len = strlen(test->hcontent);
Benny Prijonoa1e69682007-05-11 15:14:34 +00001721 parsed_hdr1 = (pjsip_hdr*) pjsip_parse_hdr(pool, &hname,
1722 test->hcontent, len,
1723 &parsed_len);
Benny Prijono8aea0d92007-04-29 06:23:11 +00001724 if (parsed_hdr1 == NULL) {
1725 if (test->flags & HDR_FLAG_PARSE_FAIL) {
1726 pj_pool_release(pool);
1727 continue;
1728 }
1729 PJ_LOG(3,(THIS_FILE, " error parsing header %s: %s", test->hname, test->hcontent));
1730 return -500;
1731 }
1732
1733 /* Test the parsing result */
1734 if (test->test && (rc=test->test(parsed_hdr1)) != 0) {
1735 PJ_LOG(3,(THIS_FILE, " validation failed for header %s: %s", test->hname, test->hcontent));
1736 PJ_LOG(3,(THIS_FILE, " error code is %d", rc));
1737 return -502;
1738 }
1739
Benny Prijono64898b52007-05-01 06:36:15 +00001740#if 1
Benny Prijono8aea0d92007-04-29 06:23:11 +00001741 /* Parse with hshortname, if present */
1742 if (test->hshort_name) {
1743 hname = pj_str(test->hshort_name);
1744 len = strlen(test->hcontent);
Benny Prijonoa1e69682007-05-11 15:14:34 +00001745 parsed_hdr2 = (pjsip_hdr*) pjsip_parse_hdr(pool, &hname, test->hcontent, len, &parsed_len);
Benny Prijono8aea0d92007-04-29 06:23:11 +00001746 if (parsed_hdr2 == NULL) {
1747 PJ_LOG(3,(THIS_FILE, " error parsing header %s: %s", test->hshort_name, test->hcontent));
1748 return -510;
1749 }
1750 }
1751#endif
1752
1753 if (test->flags & HDR_FLAG_DONT_PRINT) {
1754 pj_pool_release(pool);
1755 continue;
1756 }
1757
1758 /* Print the original header */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001759 input = (char*) pj_pool_alloc(pool, 1024);
Benny Prijono8aea0d92007-04-29 06:23:11 +00001760 len = pj_ansi_snprintf(input, 1024, "%s: %s", test->hname, test->hcontent);
1761 if (len < 1 || len >= 1024)
1762 return -520;
1763
1764 /* Print the parsed header*/
Benny Prijonoa1e69682007-05-11 15:14:34 +00001765 output = (char*) pj_pool_alloc(pool, 1024);
Benny Prijono8aea0d92007-04-29 06:23:11 +00001766 len = pjsip_hdr_print_on(parsed_hdr1, output, 1024);
1767 if (len < 1 || len >= 1024) {
1768 PJ_LOG(3,(THIS_FILE, " header too long: %s: %s", test->hname, test->hcontent));
1769 return -530;
1770 }
1771 output[len] = 0;
1772
1773 if (strcmp(input, output) != 0) {
1774 PJ_LOG(3,(THIS_FILE, " header character by character comparison failed."));
1775 PJ_LOG(3,(THIS_FILE, " original header=|%s|", input));
1776 PJ_LOG(3,(THIS_FILE, " parsed header =|%s|", output));
1777 return -540;
1778 }
1779
1780 pj_pool_release(pool);
1781 }
1782
1783 return 0;
1784}
1785
1786
1787/*****************************************************************************/
Benny Prijonoe93e2872006-06-28 16:46:49 +00001788
1789int msg_test(void)
1790{
Benny Prijonod4791af2007-06-19 08:46:02 +00001791 enum { COUNT = 1, DETECT=0, PARSE=1, PRINT=2 };
Benny Prijonoe93e2872006-06-28 16:46:49 +00001792 struct {
1793 unsigned detect;
1794 unsigned parse;
1795 unsigned print;
1796 } run[COUNT];
1797 unsigned i, max, avg_len;
1798 char desc[250];
1799 pj_status_t status;
1800
Benny Prijono8aea0d92007-04-29 06:23:11 +00001801 status = hdr_test();
1802 if (status != 0)
1803 return status;
1804
Benny Prijonod0bd4982007-12-02 15:40:52 +00001805 status = simple_test();
1806 if (status != PJ_SUCCESS)
1807 return status;
1808
1809#if INCLUDE_BENCHMARKS
Benny Prijonoe93e2872006-06-28 16:46:49 +00001810 for (i=0; i<COUNT; ++i) {
1811 PJ_LOG(3,(THIS_FILE, " benchmarking (%d of %d)..", i+1, COUNT));
1812 status = msg_benchmark(&run[i].detect, &run[i].parse, &run[i].print);
1813 if (status != PJ_SUCCESS)
1814 return status;
1815 }
1816
1817 /* Calculate average message length */
1818 for (i=0, avg_len=0; i<PJ_ARRAY_SIZE(test_array); ++i) {
1819 avg_len += test_array[i].len;
1820 }
1821 avg_len /= PJ_ARRAY_SIZE(test_array);
1822
1823
1824 /* Print maximum detect/sec */
1825 for (i=0, max=0; i<COUNT; ++i)
1826 if (run[i].detect > max) max = run[i].detect;
1827
1828 PJ_LOG(3,("", " Maximum message detection/sec=%u", max));
1829
1830 pj_ansi_sprintf(desc, "Number of SIP messages "
1831 "can be pre-parse by <tt>pjsip_find_msg()</tt> "
1832 "per second (tested with %d message sets with "
1833 "average message length of "
Benny Prijono7db431e2006-07-23 14:38:49 +00001834 "%d bytes)", (int)PJ_ARRAY_SIZE(test_array), avg_len);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001835 report_ival("msg-detect-per-sec", max, "msg/sec", desc);
1836
1837 /* Print maximum parse/sec */
1838 for (i=0, max=0; i<COUNT; ++i)
1839 if (run[i].parse > max) max = run[i].parse;
1840
1841 PJ_LOG(3,("", " Maximum message parsing/sec=%u", max));
1842
1843 pj_ansi_sprintf(desc, "Number of SIP messages "
1844 "can be <b>parsed</b> by <tt>pjsip_parse_msg()</tt> "
1845 "per second (tested with %d message sets with "
1846 "average message length of "
Benny Prijono7db431e2006-07-23 14:38:49 +00001847 "%d bytes)", (int)PJ_ARRAY_SIZE(test_array), avg_len);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001848 report_ival("msg-parse-per-sec", max, "msg/sec", desc);
1849
1850 /* Msg parsing bandwidth */
1851 report_ival("msg-parse-bandwidth-mb", avg_len*max/1000000, "MB/sec",
1852 "Message parsing bandwidth in megabytes (number of megabytes"
1853 " worth of SIP messages that can be parsed per second). "
1854 "The value is derived from msg-parse-per-sec above.");
1855
1856
1857 /* Print maximum print/sec */
1858 for (i=0, max=0; i<COUNT; ++i)
1859 if (run[i].print > max) max = run[i].print;
1860
1861 PJ_LOG(3,("", " Maximum message print/sec=%u", max));
1862
1863 pj_ansi_sprintf(desc, "Number of SIP messages "
1864 "can be <b>printed</b> by <tt>pjsip_msg_print()</tt>"
1865 " per second (tested with %d message sets with "
1866 "average message length of "
Benny Prijono7db431e2006-07-23 14:38:49 +00001867 "%d bytes)", (int)PJ_ARRAY_SIZE(test_array), avg_len);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001868
1869 report_ival("msg-print-per-sec", max, "msg/sec", desc);
1870
1871 /* Msg print bandwidth */
1872 report_ival("msg-printed-bandwidth-mb", avg_len*max/1000000, "MB/sec",
1873 "Message print bandwidth in megabytes (total size of "
1874 "SIP messages printed per second). "
1875 "The value is derived from msg-print-per-sec above.");
1876
Benny Prijonod0bd4982007-12-02 15:40:52 +00001877#endif /* INCLUDE_BENCHMARKS */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001878
1879 return PJ_SUCCESS;
1880}
1881
Benny Prijono8aea0d92007-04-29 06:23:11 +00001882
1883
1884