Debian Codegraph @

Xpjproject/pjsip/src/test/msg_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 #include "test.h"
20 #include <pjsip.h>
21 #include <pjlib.h>
22
23 #define POOL_SIZE 8000
24 #if defined(PJ_DEBUG) && PJ_DEBUG!=0
25 # define LOOP 10000
26 #else
27 # define LOOP 100000
28 #endif
29 #define AVERAGE_MSG_LEN 800
30 #define THIS_FILE "msg_test.c"
31
32 static pjsip_msg *create_msg0(pj_pool_t *pool);
33 static 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
42 struct test_msg
43 {
44 char msg[1024];
45 pjsip_msg *(*creator)(pj_pool_t *pool);
46 pj_size_t len;
47 int expected_status;
48 } test_array[] =
49 {
50 {
51 /* 'Normal' message with all headers. */
52 "INVITE sip:user@foo SIP/2.0\n"
53 "from: Hi I'm Joe <sip:[email protected]>;tag=123457890123456\r"
54 "To: Fellow User <sip:[email protected]>\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"
63 "Record-Route: <sip:server10.biloxi.com>,\r\n" /* multiple routes+folding*/
64 " <sip:bigbox3.site3.atlanta.com;lr>\n"
65 "v: SIP/2.0/SCTP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c230\n"
66 "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8\n" /* folding. */
67 " ;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"
71 "X-Header: \r\n" /* empty header */
72 "P-Associated-URI:\r\n" /* empty header without space */
73 "\r\n",
74 &create_msg0,
75 0,
76 PJ_SUCCESS
77 },
78 {
79 /* Typical response message. */
80 "SIP/2.0 200 OK\r\n"
81 "Via: SIP/2.0/SCTP server10.biloxi.com;branch=z9hG4bKnashds8;rport;received=192.0.2.1\r\n"
82 "Via: SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2\r\n"
83 "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds ;received=192.0.2.3\r\n"
84 "Route: <sip:proxy.sipprovider.com>\r\n"
85 "Route: <sip:proxy.supersip.com:5060>\r\n"
86 "Max-Forwards: 70\r\n"
87 "To: Bob <sip:[email protected]>;tag=a6c85cf\r\n"
88 "From: Alice <sip:[email protected]>;tag=1928301774\r\n"
89 "Call-ID: [email protected]\r\n"
90 "CSeq: 314159 INVITE\r\n"
91 "Contact: <sips:[email protected]>\r\n"
92 "Content-Type: application/sdp\r\n"
93 "Content-Length: 150\r\n"
94 "\r\n"
95 "v=0\r\n"
96 "o=alice 53655765 2353687637 IN IP4 pc33.atlanta.com\r\n"
97 "s=-\r\n"
98 "t=0 0\r\n"
99 "c=IN IP4 pc33.atlanta.com\r\n"
100 "m=audio 3456 RTP/AVP 0 1 3 99\r\n"
101 "a=rtpmap:0 PCMU/8000\r\n",
102 &create_msg1,
103 0,
104 PJ_SUCCESS
105 },
106 {
107 /* Torture message from RFC 4475
108 * 3.1.1.1 A short tortuous INVITE
109 */
110 "INVITE sip:[email protected];unknownparam SIP/2.0\n"
111 "TO :\n"
112 " sip:[email protected] ; tag = 1918181833n\n"
113 "from : \"J Rosenberg \\\\\\\"\" <sip:[email protected]>\n"
114 " ;\n"
115 " tag = 98asjd8\n"
116 "MaX-fOrWaRdS: 0068\n"
117 "Call-ID: [email protected]\n"
118 "Content-Length : 150\n"
119 "cseq: 0009\n"
120 " INVITE\n"
121 "Via : SIP / 2.0\n"
122 " /UDP\n"
123 " 192.0.2.2;rport;branch=390skdjuw\n"
124 "s :\n"
125 "NewFangledHeader: newfangled value\n"
126 " continued newfangled value\n"
127 "UnknownHeaderWithUnusualValue: ;;,,;;,;\n"
128 "Content-Type: application/sdp\n"
129 "Route:\n"
130 " <sip:services.example.com;lr;unknownwith=value;unknown-no-value>\n"
131 "v: SIP / 2.0 / TCP spindle.example.com ;\n"
132 " branch = z9hG4bK9ikj8 ,\n"
133 " SIP / 2.0 / UDP 192.168.255.111 ; branch=\n"
134 " z9hG4bK30239\n"
135 "m:\"Quoted string \\\"\\\"\" <sip:[email protected]> ; newparam =\n"
136 " newvalue ;\n"
137 " secondparam ; q = 0.33\r\n"
138 "\r\n"
139 "v=0\r\n"
140 "o=mhandley 29739 7272939 IN IP4 192.0.2.3\r\n"
141 "s=-\r\n"
142 "c=IN IP4 192.0.2.4\r\n"
143 "t=0 0\r\n"
144 "m=audio 49217 RTP/AVP 0 12\r\n"
145 "m=video 3227 RTP/AVP 31\r\n"
146 "a=rtpmap:31 LPC\r\n",
147 NULL,
148 0,
149 PJ_SUCCESS
150 },
151 {
152 /* Torture message from RFC 4475
153 * 3.1.1.2 Wide Range of Valid Characters
154 */
155 "!interesting-Method0123456789_*+`.%indeed'~ sip:1_unusual.URI~(to-be!sure)&isn't+it$/crazy?,/;;*:&it+has=1,weird!*pas$wo~d_too.(doesn't-it)@example.com SIP/2.0\n"
156 "Via: SIP/2.0/UDP host1.example.com;rport;branch=z9hG4bK-.!%66*_+`'~\n"
157 "To: \"BEL:\\\x07 NUL:\\\x00 DEL:\\\x7F\" <sip:1_unusual.URI~(to-be!sure)&isn't+it$/crazy?,/;;*@example.com>\n"
158 "From: token1~` token2'+_ token3*%!.- <sip:[email protected]> ;fromParam''~+*_!.-%=\"\xD1\x80\xD0\xB0\xD0\xB1\xD0\xBE\xD1\x82\xD0\xB0\xD1\x8E\xD1\x89\xD0\xB8\xD0\xB9\";tag=_token~1'+`*%!-.\n"
159 "Call-ID: intmeth.word%ZK-!.*_+'@word`~)(><:\\/\"][?}{\n"
160 "CSeq: 139122385 !interesting-Method0123456789_*+`.%indeed'~\n"
161 "Max-Forwards: 255\n"
162 "extensionHeader-!.%*+_`'~: \xEF\xBB\xBF\xE5\xA4\xA7\xE5\x81\x9C\xE9\x9B\xBB\n"
163 "Content-Length: 0\r\n\r\n",
164 NULL,
165 641,
166 PJ_SUCCESS
167 },
168 {
169 /* Torture message from RFC 4475
170 * 3.1.1.3 Valid Use of the % Escaping Mechanism
171 */
172 "INVITE sip:sips%3Auser%[email protected] SIP/2.0\n"
173 "To: sip:%75se%[email protected]\n"
174 "From: <sip:I%20have%[email protected]>;tag=1234\n"
175 "Max-Forwards: 87\n"
176 "i: esc01.239409asdfakjkn23onasd0-3234\n"
177 "CSeq: 234234 INVITE\n"
178 "Via: SIP/2.0/UDP host5.example.net;rport;branch=z9hG4bKkdjuw\n"
179 "C: application/sdp\n"
180 "Contact:\n"
181 " <sip:cal%[email protected]:5060;%6C%72;n%61me=v%61lue%25%34%31>\n"
182 "Content-Length: 150\r\n"
183 "\r\n"
184 "v=0\r\n"
185 "o=mhandley 29739 7272939 IN IP4 192.0.2.1\r\n"
186 "s=-\r\n"
187 "c=IN IP4 192.0.2.1\r\n"
188 "t=0 0\r\n"
189 "m=audio 49217 RTP/AVP 0 12\r\n"
190 "m=video 3227 RTP/AVP 31\r\n"
191 "a=rtpmap:31 LPC\r\n",
192 NULL,
193 0,
194 PJ_SUCCESS
195 },
196 {
197 /* Torture message from RFC 4475
198 * 3.1.1.4 Escaped Nulls in URIs
199 */
200 "REGISTER sip:example.com SIP/2.0\r\n"
201 "To: sip:null-%[email protected]\r\n"
202 "From: sip:null-%[email protected];tag=839923423\r\n"
203 "Max-Forwards: 70\r\n"
204 "Call-ID: escnull.39203ndfvkjdasfkq3w4otrq0adsfdfnavd\r\n"
205 "CSeq: 14398234 REGISTER\r\n"
206 "Via: SIP/2.0/UDP host5.example.com;rport;branch=z9hG4bKkdjuw\r\n"
207 "Contact: <sip:%[email protected]>\r\n"
208 "Contact: <sip:%00%[email protected]>\r\n"
209 "L:0\r\n"
210 "\r\n",
211 NULL,
212 0,
213 PJ_SUCCESS
214 },
215 {
216 /* Torture message from RFC 4475
217 * 3.1.1.5 Use of % When It Is Not an Escape
218 */
219 "RE%47IST%45R sip:registrar.example.com SIP/2.0\r\n"
220 "To: \"%Z%45\" <sip:[email protected]>\r\n"
221 "From: \"%Z%45\" <sip:[email protected]>;tag=f232jadfj23\r\n"
222 "Call-ID: esc02.asdfnqwo34rq23i34jrjasdcnl23nrlknsdf\r\n"
223 "Via: SIP/2.0/TCP host.example.com;rport;branch=z9hG4bK209%fzsnel234\r\n"
224 "CSeq: 29344 RE%47IST%45R\r\n"
225 "Max-Forwards: 70\r\n"
226 "Contact: <sip:[email protected]>\r\n"
227 "C%6Fntact: <sip:[email protected]>\r\n"
228 "Contact: <sip:[email protected]>\r\n"
229 "l: 0\r\n"
230 "\r\n",
231 NULL,
232 0,
233 PJ_SUCCESS
234 }
235 };
236
237 static struct
238 {
239 int flag;
240 pj_highprec_t detect_len, parse_len, print_len;
241 pj_timestamp detect_time, parse_time, print_time;
242 } var;
243
244 static pj_status_t test_entry( pj_pool_t *pool, struct test_msg *entry )
245 {
246 pjsip_msg *parsed_msg, *ref_msg = NULL;
247 static pjsip_msg *print_msg;
248 pj_status_t status = PJ_SUCCESS;
249 pj_ssize_t len;
250 pj_str_t str1, str2;
251 pjsip_hdr *hdr1, *hdr2;
252 pj_timestamp t1, t2;
253 pjsip_parser_err_report err_list;
254 pj_size_t msg_size;
255 char msgbuf1[PJSIP_MAX_PKT_LEN];
256 char msgbuf2[PJSIP_MAX_PKT_LEN];
257 enum { BUFLEN = 512 };
258
259 if (entry->len==0)
260 entry->len = pj_ansi_strlen(entry->msg);
261
262 if (var.flag & FLAG_PARSE_ONLY)
263 goto parse_msg;
264
265 if (var.flag & FLAG_PRINT_ONLY) {
266 if (print_msg == NULL)
267 print_msg = entry->creator(pool);
268 goto print_msg;
269 }
270
271 /* Detect message. */
272 var.detect_len = var.detect_len + entry->len;
273 pj_get_timestamp(&t1);
274 status = pjsip_find_msg(entry->msg, entry->len, PJ_FALSE, &msg_size);
275 if (status != PJ_SUCCESS) {
276 if (status!=PJSIP_EPARTIALMSG ||
277 entry->expected_status!=STATUS_PARTIAL)
278 {
279 app_perror(" error: unable to detect message", status);
280 return -5;
281 }
282 }
283 if (msg_size != entry->len) {
284 PJ_LOG(3,(THIS_FILE, " error: size mismatch"));
285 return -6;
286 }
287 pj_get_timestamp(&t2);
288 pj_sub_timestamp(&t2, &t1);
289 pj_add_timestamp(&var.detect_time, &t2);
290
291 if (var.flag & FLAG_DETECT_ONLY)
292 return PJ_SUCCESS;
293
294 /* Parse message. */
295 parse_msg:
296 var.parse_len = var.parse_len + entry->len;
297 pj_get_timestamp(&t1);
298 pj_list_init(&err_list);
299 parsed_msg = pjsip_parse_msg(pool, entry->msg, entry->len, &err_list);
300 if (parsed_msg == NULL) {
301 if (entry->expected_status != STATUS_SYNTAX_ERROR) {
302 status = -10;
303 if (err_list.next != &err_list) {
304 PJ_LOG(3,(THIS_FILE, " Syntax error in line %d col %d",
305 err_list.next->line, err_list.next->col));
306 }
307 goto on_return;
308 }
309 status = -11;
310 goto on_return;
311 }
312 pj_get_timestamp(&t2);
313 pj_sub_timestamp(&t2, &t1);
314 pj_add_timestamp(&var.parse_time, &t2);
315
316 if ((var.flag & FLAG_PARSE_ONLY) || entry->creator==NULL)
317 return PJ_SUCCESS;
318
319 /* Create reference message. */
320 ref_msg = entry->creator(pool);
321 if (ref_msg == NULL) {
322 status = -12;
323 goto on_return;
324 }
325
326 /* Create buffer for comparison. */
327 str1.ptr = (char*)pj_pool_alloc(pool, BUFLEN);
328 str2.ptr = (char*)pj_pool_alloc(pool, BUFLEN);
329
330 /* Compare message type. */
331 if (parsed_msg->type != ref_msg->type) {
332 status = -20;
333 goto on_return;
334 }
335
336 /* Compare request or status line. */
337 if (parsed_msg->type == PJSIP_REQUEST_MSG) {
338 pjsip_method *m1 = &parsed_msg->line.req.method;
339 pjsip_method *m2 = &ref_msg->line.req.method;
340
341 if (pjsip_method_cmp(m1, m2) != 0) {
342 status = -30;
343 goto on_return;
344 }
345 status = pjsip_uri_cmp(PJSIP_URI_IN_REQ_URI,
346 parsed_msg->line.req.uri,
347 ref_msg->line.req.uri);
348 if (status != PJ_SUCCESS) {
349 app_perror(" error: request URI mismatch", status);
350 status = -31;
351 goto on_return;
352 }
353 } else {
354 if (parsed_msg->line.status.code != ref_msg->line.status.code) {
355 PJ_LOG(3,(THIS_FILE, " error: status code mismatch"));
356 status = -32;
357 goto on_return;
358 }
359 if (pj_strcmp(&parsed_msg->line.status.reason,
360 &ref_msg->line.status.reason) != 0)
361 {
362 PJ_LOG(3,(THIS_FILE, " error: status text mismatch"));
363 status = -33;
364 goto on_return;
365 }
366 }
367
368 /* Compare headers. */
369 hdr1 = parsed_msg->hdr.next;
370 hdr2 = ref_msg->hdr.next;
371
372 while (hdr1 != &parsed_msg->hdr && hdr2 != &ref_msg->hdr) {
373 len = pjsip_hdr_print_on(hdr1, str1.ptr, BUFLEN);
374 if (len < 0) {
375 status = -40;
376 goto on_return;
377 }
378 str1.ptr[len] = '\0';
379 str1.slen = len;
380
381 len = pjsip_hdr_print_on(hdr2, str2.ptr, BUFLEN);
382 if (len < 0) {
383 status = -50;
384 goto on_return;
385 }
386 str2.ptr[len] = '\0';
387 str2.slen = len;
388
389 if (pj_strcmp(&str1, &str2) != 0) {
390 status = -60;
391 PJ_LOG(3,(THIS_FILE, " error: header string mismatch:\n"
392 " h1='%s'\n"
393 " h2='%s'\n",
394 str1.ptr, str2.ptr));
395 goto on_return;
396 }
397
398 hdr1 = hdr1->next;
399 hdr2 = hdr2->next;
400 }
401
402 if (hdr1 != &parsed_msg->hdr || hdr2 != &ref_msg->hdr) {
403 status = -70;
404 goto on_return;
405 }
406
407 /* Compare body? */
408 if (parsed_msg->body==NULL || ref_msg->body==NULL)
409 goto print_msg;
410
411 /* Compare msg body length. */
412 if (parsed_msg->body->len != ref_msg->body->len) {
413 status = -80;
414 goto on_return;
415 }
416
417 /* Compare msg body content type. */
418 if (pj_strcmp(&parsed_msg->body->content_type.type,
419 &ref_msg->body->content_type.type) != 0) {
420 status = -90;
421 goto on_return;
422 }
423 if (pj_strcmp(&parsed_msg->body->content_type.subtype,
424 &ref_msg->body->content_type.subtype) != 0) {
425 status = -100;
426 goto on_return;
427 }
428
429 /* Compare body content. */
430 str1.slen = parsed_msg->body->print_body(parsed_msg->body,
431 msgbuf1, sizeof(msgbuf1));
432 if (str1.slen < 1) {
433 status = -110;
434 goto on_return;
435 }
436 str1.ptr = msgbuf1;
437
438 str2.slen = ref_msg->body->print_body(ref_msg->body,
439 msgbuf2, sizeof(msgbuf2));
440 if (str2.slen < 1) {
441 status = -120;
442 goto on_return;
443 }
444 str2.ptr = msgbuf2;
445
446 if (pj_strcmp(&str1, &str2) != 0) {
447 status = -140;
448 goto on_return;
449 }
450
451 /* Print message. */
452 print_msg:
453 var.print_len = var.print_len + entry->len;
454 pj_get_timestamp(&t1);
455 if (var.flag & FLAG_PRINT_ONLY)
456 ref_msg = print_msg;
457 len = pjsip_msg_print(ref_msg, msgbuf1, PJSIP_MAX_PKT_LEN);
458 if (len < 1) {
459 status = -150;
460 goto on_return;
461 }
462 pj_get_timestamp(&t2);
463 pj_sub_timestamp(&t2, &t1);
464 pj_add_timestamp(&var.print_time, &t2);
465
466
467 status = PJ_SUCCESS;
468
469 on_return:
470 return status;
471 }
472
473
474 static pjsip_msg *create_msg0(pj_pool_t *pool)
475 {
476
477 pjsip_msg *msg;
478 pjsip_name_addr *name_addr;
479 pjsip_sip_uri *url;
480 pjsip_fromto_hdr *fromto;
481 pjsip_cid_hdr *cid;
482 pjsip_clen_hdr *clen;
483 pjsip_cseq_hdr *cseq;
484 pjsip_contact_hdr *contact;
485 pjsip_ctype_hdr *ctype;
486 pjsip_routing_hdr *routing;
487 pjsip_via_hdr *via;
488 pjsip_generic_string_hdr *generic;
489 pjsip_param *prm;
490 pj_str_t str;
491
492 msg = pjsip_msg_create(pool, PJSIP_REQUEST_MSG);
493
494 /* "INVITE sip:user@foo SIP/2.0\n" */
495 pjsip_method_set(&msg->line.req.method, PJSIP_INVITE_METHOD);
496 url = pjsip_sip_uri_create(pool, 0);
497 msg->line.req.uri = (pjsip_uri*)url;
498 pj_strdup2(pool, &url->user, "user");
499 pj_strdup2(pool, &url->host, "foo");
500
501 /* "From: Hi I'm Joe <sip:[email protected]>;tag=123457890123456\r" */
502 fromto = pjsip_from_hdr_create(pool);
503 pjsip_msg_add_hdr(msg, (pjsip_hdr*)fromto);
504 pj_strdup2(pool, &fromto->tag, "123457890123456");
505 name_addr = pjsip_name_addr_create(pool);
506 fromto->uri = (pjsip_uri*)name_addr;
507 pj_strdup2(pool, &name_addr->display, "Hi I'm Joe");
508 url = pjsip_sip_uri_create(pool, 0);
509 name_addr->uri = (pjsip_uri*)url;
510 pj_strdup2(pool, &url->user, "joe.user");
511 pj_strdup2(pool, &url->host, "bar.otherdomain.com");
512
513 /* "To: Fellow User <sip:[email protected]>\r\n" */
514 fromto = pjsip_to_hdr_create(pool);
515 pjsip_msg_add_hdr(msg, (pjsip_hdr*)fromto);
516 name_addr = pjsip_name_addr_create(pool);
517 fromto->uri = (pjsip_uri*)name_addr;
518 pj_strdup2(pool, &name_addr->display, "Fellow User");
519 url = pjsip_sip_uri_create(pool, 0);
520 name_addr->uri = (pjsip_uri*)url;
521 pj_strdup2(pool, &url->user, "user");
522 pj_strdup2(pool, &url->host, "foo.bar.domain.com");
523
524 /* "Call-ID: 12345678901234567890@bar\r\n" */
525 cid = pjsip_cid_hdr_create(pool);
526 pjsip_msg_add_hdr(msg, (pjsip_hdr*)cid);
527 pj_strdup2(pool, &cid->id, "12345678901234567890@bar");
528
529 /* "Content-Length: 0\r\n" */
530 clen = pjsip_clen_hdr_create(pool);
531 pjsip_msg_add_hdr(msg, (pjsip_hdr*)clen);
532 clen->len = 0;
533
534 /* "CSeq: 123456 INVITE\n" */
535 cseq = pjsip_cseq_hdr_create(pool);
536 pjsip_msg_add_hdr(msg, (pjsip_hdr*)cseq);
537 cseq->cseq = 123456;
538 pjsip_method_set(&cseq->method, PJSIP_INVITE_METHOD);
539
540 /* "Contact: <sip:joe@bar>;q=0.5;expires=3600*/
541 contact = pjsip_contact_hdr_create(pool);
542 pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
543 contact->q1000 = 500;
544 contact->expires = 3600;
545 name_addr = pjsip_name_addr_create(pool);
546 contact->uri = (pjsip_uri*)name_addr;
547 url = pjsip_sip_uri_create(pool, 0);
548 name_addr->uri = (pjsip_uri*)url;
549 pj_strdup2(pool, &url->user, "joe");
550 pj_strdup2(pool, &url->host, "bar");
551
552 /*, sip:user@host;q=0.500\r" */
553 contact = pjsip_contact_hdr_create(pool);
554 pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
555 contact->q1000 = 500;
556 name_addr = pjsip_name_addr_create(pool);
557 contact->uri = (pjsip_uri*)name_addr;
558 url = pjsip_sip_uri_create(pool, 0);
559 name_addr->uri = (pjsip_uri*)url;
560 pj_strdup2(pool, &url->user, "user");
561 pj_strdup2(pool, &url->host, "host");
562
563 /* " ,sip:user2@host2\n" */
564 contact = pjsip_contact_hdr_create(pool);
565 pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
566 name_addr = pjsip_name_addr_create(pool);
567 contact->uri = (pjsip_uri*)name_addr;
568 url = pjsip_sip_uri_create(pool, 0);
569 name_addr->uri = (pjsip_uri*)url;
570 pj_strdup2(pool, &url->user, "user2");
571 pj_strdup2(pool, &url->host, "host2");
572
573 /* "Content-Type: text/html; charset=ISO-8859-4\r" */
574 ctype = pjsip_ctype_hdr_create(pool);
575 pjsip_msg_add_hdr(msg, (pjsip_hdr*)ctype);
576 pj_strdup2(pool, &ctype->media.type, "text");
577 pj_strdup2(pool, &ctype->media.subtype, "html");
578 prm = PJ_POOL_ALLOC_T(pool, pjsip_param);
579 prm->name = pj_str("charset");
580 prm->value = pj_str("ISO-8859-4");
581 pj_list_push_back(&ctype->media.param, prm);
582
583 /* "Route: <sip:bigbox3.site3.atlanta.com;lr>,\r\n" */
584 routing = pjsip_route_hdr_create(pool);
585 pjsip_msg_add_hdr(msg, (pjsip_hdr*)routing);
586 url = pjsip_sip_uri_create(pool, 0);
587 routing->name_addr.uri = (pjsip_uri*)url;
588 pj_strdup2(pool, &url->host, "bigbox3.site3.atlanta.com");
589 url->lr_param = 1;
590
591 /* " <sip:server10.biloxi.com;lr>\r" */
592 routing = pjsip_route_hdr_create(pool);
593 pjsip_msg_add_hdr(msg, (pjsip_hdr*)routing);
594 url = pjsip_sip_uri_create(pool, 0);
595 routing->name_addr.uri = (pjsip_uri*)url;
596 pj_strdup2(pool, &url->host, "server10.biloxi.com");
597 url->lr_param = 1;
598
599 /* "Record-Route: <sip:server10.biloxi.com>,\r\n" */
600 routing = pjsip_rr_hdr_create(pool);
601 pjsip_msg_add_hdr(msg, (pjsip_hdr*)routing);
602 url = pjsip_sip_uri_create(pool, 0);
603 routing->name_addr.uri = (pjsip_uri*)url;
604 pj_strdup2(pool, &url->host, "server10.biloxi.com");
605 url->lr_param = 0;
606
607 /* " <sip:bigbox3.site3.atlanta.com;lr>\n" */
608 routing = pjsip_rr_hdr_create(pool);
609 pjsip_msg_add_hdr(msg, (pjsip_hdr*)routing);
610 url = pjsip_sip_uri_create(pool, 0);
611 routing->name_addr.uri = (pjsip_uri*)url;
612 pj_strdup2(pool, &url->host, "bigbox3.site3.atlanta.com");
613 url->lr_param = 1;
614
615 /* "Via: SIP/2.0/SCTP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c230\n" */
616 via = pjsip_via_hdr_create(pool);
617 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
618 pj_strdup2(pool, &via->transport, "SCTP");
619 pj_strdup2(pool, &via->sent_by.host, "bigbox3.site3.atlanta.com");
620 pj_strdup2(pool, &via->branch_param, "z9hG4bK77ef4c230");
621
622 /* "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8\n"
623 " ;received=192.0.2.1\r\n" */
624 via = pjsip_via_hdr_create(pool);
625 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
626 pj_strdup2(pool, &via->transport, "UDP");
627 pj_strdup2(pool, &via->sent_by.host, "pc33.atlanta.com");
628 pj_strdup2(pool, &via->branch_param, "z9hG4bKnashds8");
629 pj_strdup2(pool, &via->recvd_param, "192.0.2.1");
630
631
632 /* "Via: SIP/2.0/UDP 10.2.1.1, */
633 via = pjsip_via_hdr_create(pool);
634 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
635 pj_strdup2(pool, &via->transport, "UDP");
636 pj_strdup2(pool, &via->sent_by.host, "10.2.1.1");
637
638
639 /*SIP/2.0/TCP 192.168.1.1\n" */
640 via = pjsip_via_hdr_create(pool);
641 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
642 pj_strdup2(pool, &via->transport, "TCP");
643 pj_strdup2(pool, &via->sent_by.host, "192.168.1.1");
644
645 /* "Organization: \r" */
646 str.ptr = "Organization";
647 str.slen = 12;
648 generic = pjsip_generic_string_hdr_create(pool, &str, NULL);
649 pjsip_msg_add_hdr(msg, (pjsip_hdr*)generic);
650 generic->hvalue.ptr = NULL;
651 generic->hvalue.slen = 0;
652
653 /* "Max-Forwards: 70\n" */
654 str.ptr = "Max-Forwards";
655 str.slen = 12;
656 generic = pjsip_generic_string_hdr_create(pool, &str, NULL);
657 pjsip_msg_add_hdr(msg, (pjsip_hdr*)generic);
658 str.ptr = "70";
659 str.slen = 2;
660 generic->hvalue = str;
661
662 /* "X-Header: \r\n" */
663 str.ptr = "X-Header";
664 str.slen = 8;
665 generic = pjsip_generic_string_hdr_create(pool, &str, NULL);
666 pjsip_msg_add_hdr(msg, (pjsip_hdr*)generic);
667 str.ptr = NULL;
668 str.slen = 0;
669 generic->hvalue = str;
670
671 /* P-Associated-URI:\r\n */
672 str.ptr = "P-Associated-URI";
673 str.slen = 16;
674 generic = pjsip_generic_string_hdr_create(pool, &str, NULL);
675 pjsip_msg_add_hdr(msg, (pjsip_hdr*)generic);
676 str.ptr = NULL;
677 str.slen = 0;
678 generic->hvalue = str;
679
680 return msg;
681 }
682
683 static pjsip_msg *create_msg1(pj_pool_t *pool)
684 {
685 pjsip_via_hdr *via;
686 pjsip_route_hdr *route;
687 pjsip_name_addr *name_addr;
688 pjsip_sip_uri *url;
689 pjsip_max_fwd_hdr *max_fwd;
690 pjsip_to_hdr *to;
691 pjsip_from_hdr *from;
692 pjsip_contact_hdr *contact;
693 pjsip_ctype_hdr *ctype;
694 pjsip_cid_hdr *cid;
695 pjsip_clen_hdr *clen;
696 pjsip_cseq_hdr *cseq;
697 pjsip_msg *msg = pjsip_msg_create(pool, PJSIP_RESPONSE_MSG);
698 pjsip_msg_body *body;
699
700 //"SIP/2.0 200 OK\r\n"
701 msg->line.status.code = 200;
702 msg->line.status.reason = pj_str("OK");
703
704 //"Via: SIP/2.0/SCTP server10.biloxi.com;branch=z9hG4bKnashds8;rport;received=192.0.2.1\r\n"
705 via = pjsip_via_hdr_create(pool);
706 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
707 via->transport = pj_str("SCTP");
708 via->sent_by.host = pj_str("server10.biloxi.com");
709 via->branch_param = pj_str("z9hG4bKnashds8");
710 via->rport_param = 0;
711 via->recvd_param = pj_str("192.0.2.1");
712
713 //"Via: SIP/2.0/UDP bigbox3.site3.atlanta.com;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2\r\n"
714 via = pjsip_via_hdr_create(pool);
715 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
716 via->transport = pj_str("UDP");
717 via->sent_by.host = pj_str("bigbox3.site3.atlanta.com");
718 via->branch_param = pj_str("z9hG4bK77ef4c2312983.1");
719 via->recvd_param = pj_str("192.0.2.2");
720
721 //"Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds ;received=192.0.2.3\r\n"
722 via = pjsip_via_hdr_create(pool);
723 pjsip_msg_add_hdr(msg, (pjsip_hdr*)via);
724 via->transport = pj_str("UDP");
725 via->sent_by.host = pj_str("pc33.atlanta.com");
726 via->branch_param = pj_str("z9hG4bK776asdhds");
727 via->recvd_param = pj_str("192.0.2.3");
728
729 //"Route: <sip:proxy.sipprovider.com>\r\n"
730 route = pjsip_route_hdr_create(pool);
731 pjsip_msg_add_hdr(msg, (pjsip_hdr*)route);
732 url = pjsip_sip_uri_create(pool, PJ_FALSE);
733 route->name_addr.uri = (pjsip_uri*)url;
734 url->host = pj_str("proxy.sipprovider.com");
735
736 //"Route: <sip:proxy.supersip.com:5060>\r\n"
737 route = pjsip_route_hdr_create(pool);
738 pjsip_msg_add_hdr(msg, (pjsip_hdr*)route);
739 url = pjsip_sip_uri_create(pool, PJ_FALSE);
740 route->name_addr.uri = (pjsip_uri*)url;
741 url->host = pj_str("proxy.supersip.com");
742 url->port = 5060;
743
744 //"Max-Forwards: 70\r\n"
745 max_fwd = pjsip_max_fwd_hdr_create(pool, 70);
746 pjsip_msg_add_hdr(msg, (pjsip_hdr*)max_fwd);
747
748 //"To: Bob <sip:[email protected]>;tag=a6c85cf\r\n"
749 to = pjsip_to_hdr_create(pool);
750 pjsip_msg_add_hdr(msg, (pjsip_hdr*)to);
751 name_addr = pjsip_name_addr_create(pool);
752 name_addr->display = pj_str("Bob");
753 to->uri = (pjsip_uri*)name_addr;
754 url = pjsip_sip_uri_create(pool, PJ_FALSE);
755 name_addr->uri = (pjsip_uri*)url;
756 url->user = pj_str("bob");
757 url->host = pj_str("biloxi.com");
758 to->tag = pj_str("a6c85cf");
759
760 //"From: Alice <sip:[email protected]>;tag=1928301774\r\n"
761 from = pjsip_from_hdr_create(pool);
762 pjsip_msg_add_hdr(msg, (pjsip_hdr*)from);
763 name_addr = pjsip_name_addr_create(pool);
764 name_addr->display = pj_str("Alice");
765 from->uri = (pjsip_uri*)name_addr;
766 url = pjsip_sip_uri_create(pool, PJ_FALSE);
767 name_addr->uri = (pjsip_uri*)url;
768 url->user = pj_str("alice");
769 url->host = pj_str("atlanta.com");
770 from->tag = pj_str("1928301774");
771
772 //"Call-ID: [email protected]\r\n"
773 cid = pjsip_cid_hdr_create(pool);
774 pjsip_msg_add_hdr(msg, (pjsip_hdr*)cid);
775 cid->id = pj_str("[email protected]");
776
777 //"CSeq: 314159 INVITE\r\n"
778 cseq = pjsip_cseq_hdr_create(pool);
779 pjsip_msg_add_hdr(msg, (pjsip_hdr*)cseq);
780 cseq->cseq = 314159;
781 pjsip_method_set(&cseq->method, PJSIP_INVITE_METHOD);
782
783 //"Contact: <sips:[email protected]>\r\n"
784 contact = pjsip_contact_hdr_create(pool);
785 pjsip_msg_add_hdr(msg, (pjsip_hdr*)contact);
786 name_addr = pjsip_name_addr_create(pool);
787 contact->uri = (pjsip_uri*)name_addr;
788 url = pjsip_sip_uri_create(pool, PJ_TRUE);
789 name_addr->uri = (pjsip_uri*)url;
790 url->user = pj_str("bob");
791 url->host = pj_str("192.0.2.4");
792
793 //"Content-Type: application/sdp\r\n"
794 ctype = pjsip_ctype_hdr_create(pool);
795 pjsip_msg_add_hdr(msg, (pjsip_hdr*)ctype);
796 ctype->media.type = pj_str("application");
797 ctype->media.subtype = pj_str("sdp");
798
799 //"Content-Length: 150\r\n"
800 clen = pjsip_clen_hdr_create(pool);
801 pjsip_msg_add_hdr(msg, (pjsip_hdr*)clen);
802 clen->len = 150;
803
804 // Body
805 body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body);
806 msg->body = body;
807 body->content_type.type = pj_str("application");
808 body->content_type.subtype = pj_str("sdp");
809 body->data = (void*)
810 "v=0\r\n"
811 "o=alice 53655765 2353687637 IN IP4 pc33.atlanta.com\r\n"
812 "s=-\r\n"
813 "t=0 0\r\n"
814 "c=IN IP4 pc33.atlanta.com\r\n"
815 "m=audio 3456 RTP/AVP 0 1 3 99\r\n"
816 "a=rtpmap:0 PCMU/8000\r\n";
817 body->len = (unsigned)pj_ansi_strlen((const char*) body->data);
818 body->print_body = &pjsip_print_text_body;
819
820 return msg;
821 }
822
823 /*****************************************************************************/
824
825 static pj_status_t simple_test(void)
826 {
827 char stbuf[] = "SIP/2.0 180 Ringing like it never rings before";
828 unsigned i;
829 pjsip_status_line st_line;
830 pj_status_t status;
831
832 PJ_LOG(3,(THIS_FILE, " simple test.."));
833
834 status = pjsip_parse_status_line(stbuf, pj_ansi_strlen(stbuf), &st_line);
835 if (status != PJ_SUCCESS)
836 return status;
837
838 for (i=0; i<PJ_ARRAY_SIZE(test_array); ++i) {
839 pj_pool_t *pool;
840 pool = pjsip_endpt_create_pool(endpt, NULL, POOL_SIZE, POOL_SIZE);
841 status = test_entry( pool, &test_array[i] );
842 pjsip_endpt_release_pool(endpt, pool);
843
844 if (status != PJ_SUCCESS)
845 return status;
846 }
847
848 return PJ_SUCCESS;
849 }
850
851
852 #if INCLUDE_BENCHMARKS
853 static int msg_benchmark(unsigned *p_detect, unsigned *p_parse,
854 unsigned *p_print)
855 {
856 pj_pool_t *pool;
857 int i, loop;
858 pj_timestamp zero;
859 pj_time_val elapsed;
860 pj_highprec_t avg_detect, avg_parse, avg_print, kbytes;
861 pj_status_t status = PJ_SUCCESS;
862
863 pj_bzero(&var, sizeof(var));
864 zero.u64 = 0;
865
866 for (loop=0; loop<LOOP; ++loop) {
867 for (i=0; i<(int)PJ_ARRAY_SIZE(test_array); ++i) {
868 pool = pjsip_endpt_create_pool(endpt, NULL, POOL_SIZE, POOL_SIZE);
869 status = test_entry( pool, &test_array[i] );
870 pjsip_endpt_release_pool(endpt, pool);
871
872 if (status != PJ_SUCCESS)
873 return status;
874 }
875 }
876
877 kbytes = var.detect_len;
878 pj_highprec_mod(kbytes, 1000000);
879 pj_highprec_div(kbytes, 100000);
880 elapsed = pj_elapsed_time(&zero, &var.detect_time);
881 avg_detect = pj_elapsed_usec(&zero, &var.detect_time);
882 pj_highprec_mul(avg_detect, AVERAGE_MSG_LEN);
883 pj_highprec_div(avg_detect, var.detect_len);
884 avg_detect = 1000000 / avg_detect;
885
886 PJ_LOG(3,(THIS_FILE,
887 " %u.%u MB detected in %ld.%03lds (avg=%d msg detection/sec)",
888 (unsigned)(var.detect_len/1000000), (unsigned)kbytes,
889 elapsed.sec, elapsed.msec,
890 (unsigned)avg_detect));
891 *p_detect = (unsigned)avg_detect;
892
893 kbytes = var.parse_len;
894 pj_highprec_mod(kbytes, 1000000);
895 pj_highprec_div(kbytes, 100000);
896 elapsed = pj_elapsed_time(&zero, &var.parse_time);
897 avg_parse = pj_elapsed_usec(&zero, &var.parse_time);
898 pj_highprec_mul(avg_parse, AVERAGE_MSG_LEN);
899 pj_highprec_div(avg_parse, var.parse_len);
900 avg_parse = 1000000 / avg_parse;
901
902 PJ_LOG(3,(THIS_FILE,
903 " %u.%u MB parsed in %ld.%03lds (avg=%d msg parsing/sec)",
904 (unsigned)(var.parse_len/1000000), (unsigned)kbytes,
905 elapsed.sec, elapsed.msec,
906 (unsigned)avg_parse));
907 *p_parse = (unsigned)avg_parse;
908
909 kbytes = var.print_len;
910 pj_highprec_mod(kbytes, 1000000);
911 pj_highprec_div(kbytes, 100000);
912 elapsed = pj_elapsed_time(&zero, &var.print_time);
913 avg_print = pj_elapsed_usec(&zero, &var.print_time);
914 pj_highprec_mul(avg_print, AVERAGE_MSG_LEN);
915 pj_highprec_div(avg_print, var.print_len);
916 avg_print = 1000000 / avg_print;
917
918 PJ_LOG(3,(THIS_FILE,
919 " %u.%u MB printed in %ld.%03lds (avg=%d msg print/sec)",
920 (unsigned)(var.print_len/1000000), (unsigned)kbytes,
921 elapsed.sec, elapsed.msec,
922 (unsigned)avg_print));
923
924 *p_print = (unsigned)avg_print;
925 return status;
926 }
927 #endif /* INCLUDE_BENCHMARKS */
928
929 /*****************************************************************************/
930 /* Test various header parsing and production */
931 static int hdr_test_success(pjsip_hdr *h);
932 static int hdr_test_accept0(pjsip_hdr *h);
933 static int hdr_test_accept1(pjsip_hdr *h);
934 static int hdr_test_accept2(pjsip_hdr *h);
935 static int hdr_test_allow0(pjsip_hdr *h);
936 static int hdr_test_authorization(pjsip_hdr *h);
937 static int hdr_test_cid(pjsip_hdr *h);
938 static int hdr_test_contact0(pjsip_hdr *h);
939 static int hdr_test_contact1(pjsip_hdr *h);
940 static int hdr_test_contact_q0(pjsip_hdr *h);
941 static int hdr_test_contact_q1(pjsip_hdr *h);
942 static int hdr_test_contact_q2(pjsip_hdr *h);
943 static int hdr_test_contact_q3(pjsip_hdr *h);
944 static int hdr_test_contact_q4(pjsip_hdr *h);
945 static int hdr_test_content_length(pjsip_hdr *h);
946 static int hdr_test_content_type(pjsip_hdr *h);
947 static int hdr_test_from(pjsip_hdr *h);
948 static int hdr_test_proxy_authenticate(pjsip_hdr *h);
949 static int hdr_test_record_route(pjsip_hdr *h);
950 static int hdr_test_supported(pjsip_hdr *h);
951 static int hdr_test_supported_with_leading_comma(pjsip_hdr *h);
952 static int hdr_test_supported_with_trailing_comma(pjsip_hdr *h);
953 static int hdr_test_supported_with_multiple_commas(pjsip_hdr *h);
954 static int hdr_test_supported_with_middle_commas(pjsip_hdr *h);
955 static int hdr_test_to(pjsip_hdr *h);
956 static int hdr_test_via(pjsip_hdr *h);
957 static int hdr_test_via_ipv6_1(pjsip_hdr *h);
958 static int hdr_test_via_ipv6_2(pjsip_hdr *h);
959 static int hdr_test_via_ipv6_3(pjsip_hdr *h);
960 static int hdr_test_retry_after1(pjsip_hdr *h);
961 static int hdr_test_subject_utf(pjsip_hdr *h);
962
963
964 #define GENERIC_PARAM "p0=a;p1=\"ab:;cd\";p2=ab%3acd;p3"
965 #define GENERIC_PARAM_PARSED "p0=a;p1=\"ab:;cd\";p2=ab%3acd;p3"
966 #define PARAM_CHAR "][/:&+$"
967 #define SIMPLE_ADDR_SPEC "sip:host"
968 #define ADDR_SPEC SIMPLE_ADDR_SPEC ";"PARAM_CHAR"="PARAM_CHAR ";p1=\";\""
969 #define NAME_ADDR "<" ADDR_SPEC ">"
970
971 #define HDR_FLAG_PARSE_FAIL 1
972 #define HDR_FLAG_DONT_PRINT 2
973
974 struct hdr_test_t
975 {
976 char *hname;
977 char *hshort_name;
978 char *hcontent;
979 int (*test)(pjsip_hdr*);
980 unsigned flags;
981 } hdr_test_data[] =
982 {
983 {
984 /* Empty Accept */
985 "Accept", NULL,
986 "",
987 &hdr_test_accept0
988 },
989
990 {
991 /* Overflowing generic string header */
992 "Accept", NULL,
993 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
994 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
995 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
996 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
997 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
998 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
999 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, " \
1000 "a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a",
1001 &hdr_test_success,
1002 HDR_FLAG_PARSE_FAIL
1003 },
1004
1005 {
1006 /* Normal Accept */
1007 "Accept", NULL,
1008 "application/*, text/plain",
1009 &hdr_test_accept1
1010 },
1011
1012 {
1013 /* Accept with params */
1014 "Accept", NULL,
1015 "application/*;p1=v1, text/plain",
1016 &hdr_test_accept2
1017 },
1018
1019 {
1020 /* Empty Allow */
1021 "Allow", NULL,
1022 "",
1023 &hdr_test_allow0,
1024 },
1025
1026 {
1027 /* Authorization, testing which params should be quoted */
1028 "Authorization", NULL,
1029 "Digest username=\"username\", realm=\"realm\", nonce=\"nonce\", " \
1030 "uri=\"sip:domain\", response=\"RESPONSE\", algorithm=MD5, " \
1031 "cnonce=\"CNONCE\", opaque=\"OPAQUE\", qop=auth, nc=00000001",
1032 &hdr_test_authorization
1033 },
1034
1035 {
1036 /* Call ID */
1037 "Call-ID", "i",
1038 "-.!%*_+`'~()<>:\\\"/[]?{}",
1039 &hdr_test_cid,
1040 },
1041
1042 {
1043 /* Parameter belong to hparam */
1044 "Contact", "m",
1045 SIMPLE_ADDR_SPEC ";p1=v1",
1046 &hdr_test_contact0,
1047 HDR_FLAG_DONT_PRINT
1048 },
1049
1050 {
1051 /* generic-param in Contact header */
1052 "Contact", "m",
1053 NAME_ADDR ";" GENERIC_PARAM,
1054 &hdr_test_contact1
1055 },
1056
1057 {
1058 /* q=0 parameter in Contact header */
1059 "Contact", "m",
1060 NAME_ADDR ";q=0",
1061 &hdr_test_contact_q0,
1062 HDR_FLAG_DONT_PRINT
1063 },
1064
1065 {
1066 /* q=0.5 parameter in Contact header */
1067 "Contact", "m",
1068 NAME_ADDR ";q=0.5",
1069 &hdr_test_contact_q1
1070 },
1071
1072 {
1073 /* q=1 parameter in Contact header */
1074 "Contact", "m",
1075 NAME_ADDR ";q=1",
1076 &hdr_test_contact_q2
1077 },
1078
1079 {
1080 /* q=1.0 parameter in Contact header */
1081 "Contact", "m",
1082 NAME_ADDR ";q=1.0",
1083 &hdr_test_contact_q3,
1084 HDR_FLAG_DONT_PRINT
1085 },
1086
1087 {
1088 /* q=1.1 parameter in Contact header */
1089 "Contact", "m",
1090 NAME_ADDR ";q=1.15",
1091 &hdr_test_contact_q4
1092 },
1093
1094 {
1095 /* Content-Length */
1096 "Content-Length", "l",
1097 "10",
1098 &hdr_test_content_length
1099 },
1100
1101 {
1102 /* Content-Type, with generic-param */
1103 "Content-Type", "c",
1104 "application/sdp" ";" GENERIC_PARAM,
1105 &hdr_test_content_type,
1106 HDR_FLAG_DONT_PRINT
1107 },
1108
1109 {
1110 /* From, testing parameters and generic-param */
1111 "From", "f",
1112 NAME_ADDR ";" GENERIC_PARAM,
1113 &hdr_test_from
1114 },
1115
1116 {
1117 /* Proxy-Authenticate, testing which params should be quoted */
1118 "Proxy-Authenticate", NULL,
1119 "Digest realm=\"realm\",domain=\"sip:domain\",nonce=\"nonce\"," \
1120 "opaque=\"opaque\",stale=true,algorithm=MD5,qop=\"auth\"",
1121 &hdr_test_proxy_authenticate
1122 },
1123
1124 {
1125 /* Record-Route, param belong to header */
1126 "Record-Route", NULL,
1127 NAME_ADDR ";" GENERIC_PARAM,
1128 &hdr_test_record_route
1129 },
1130
1131 {
1132 /* Empty Supported */
1133 "Supported", "k",
1134 "",
1135 &hdr_test_supported
1136 },
1137
1138 {
1139 /* Supported with leading comma (buggy MizuDroid format) */
1140 "Supported", "k",
1141 ",outbound",
1142 &hdr_test_supported_with_leading_comma,
1143 HDR_FLAG_DONT_PRINT
1144 },
1145
1146 {
1147 /* Supported with trailing comma */
1148 "Supported", "k",
1149 "outbound,",
1150 &hdr_test_supported_with_trailing_comma,
1151 HDR_FLAG_DONT_PRINT
1152 },
1153
1154 {
1155 /* Supported with multiple consecutive commas at beginning */
1156 "Supported", "k",
1157 ",,,outbound,path",
1158 &hdr_test_supported_with_multiple_commas,
1159 HDR_FLAG_DONT_PRINT
1160 },
1161
1162 {
1163 /* Supported with multiple consecutive commas in middle */
1164 "Supported", "k",
1165 "outbound,,,path",
1166 &hdr_test_supported_with_middle_commas,
1167 HDR_FLAG_DONT_PRINT
1168 },
1169
1170 {
1171 /* To */
1172 "To", "t",
1173 NAME_ADDR ";" GENERIC_PARAM,
1174 &hdr_test_to
1175 },
1176
1177 {
1178 /* Via */
1179 "Via", "v",
1180 "SIP/2.0/XYZ host" ";" GENERIC_PARAM,
1181 &hdr_test_via
1182 },
1183
1184 {
1185 /* Via with IPv6 */
1186 "Via", "v",
1187 "SIP/2.0/UDP [::1]",
1188 &hdr_test_via_ipv6_1
1189 },
1190
1191 {
1192 /* Via with IPv6 */
1193 "Via", "v",
1194 "SIP/2.0/UDP [::1]:5061",
1195 &hdr_test_via_ipv6_2
1196 },
1197
1198 {
1199 /* Via with IPv6 */
1200 "Via", "v",
1201 "SIP/2.0/UDP [::1];rport=5061;received=::2",
1202 &hdr_test_via_ipv6_3
1203 },
1204
1205 {
1206 /* Retry-After header with comment */
1207 "Retry-After", NULL,
1208 "10(Already Pending Register)",
1209 &hdr_test_retry_after1
1210 },
1211
1212 {
1213 /* Non-ASCII UTF-8 characters in Subject */
1214 "Subject", NULL,
1215 "\xC0\x81",
1216 &hdr_test_subject_utf
1217 }
1218 };
1219
1220 static int hdr_test_success(pjsip_hdr *h)
1221 {
1222 PJ_UNUSED_ARG(h);
1223 return 0;
1224 }
1225
1226 /* "" */
1227 static int hdr_test_accept0(pjsip_hdr *h)
1228 {
1229 pjsip_accept_hdr *hdr = (pjsip_accept_hdr*)h;
1230
1231 if (h->type != PJSIP_H_ACCEPT)
1232 return -1010;
1233
1234 if (hdr->count != 0)
1235 return -1020;
1236
1237 return 0;
1238 }
1239
1240 /* "application/ *, text/plain\r\n" */
1241 static int hdr_test_accept1(pjsip_hdr *h)
1242 {
1243 pjsip_accept_hdr *hdr = (pjsip_accept_hdr*)h;
1244
1245 if (h->type != PJSIP_H_ACCEPT)
1246 return -1110;
1247
1248 if (hdr->count != 2)
1249 return -1120;
1250
1251 if (pj_strcmp2(&hdr->values[0], "application/*"))
1252 return -1130;
1253
1254 if (pj_strcmp2(&hdr->values[1], "text/plain"))
1255 return -1140;
1256
1257 return 0;
1258 }
1259
1260 /* "application/ *;p1=v1, text/plain\r\n" */
1261 static int hdr_test_accept2(pjsip_hdr *h)
1262 {
1263 pjsip_accept_hdr *hdr = (pjsip_accept_hdr*)h;
1264
1265 if (h->type != PJSIP_H_ACCEPT)
1266 return -1210;
1267
1268 if (hdr->count != 2)
1269 return -1220;
1270
1271 if (pj_strcmp2(&hdr->values[0], "application/*;p1=v1"))
1272 return -1230;
1273
1274 if (pj_strcmp2(&hdr->values[1], "text/plain"))
1275 return -1240;
1276
1277 return 0;
1278 }
1279
1280 /* "" */
1281 static int hdr_test_allow0(pjsip_hdr *h)
1282 {
1283 pjsip_allow_hdr *hdr = (pjsip_allow_hdr*)h;
1284
1285 if (h->type != PJSIP_H_ALLOW)
1286 return -1310;
1287
1288 if (hdr->count != 0)
1289 return -1320;
1290
1291 return 0;
1292
1293 }
1294
1295
1296 /*
1297 "Digest username=\"username\", realm=\"realm\", nonce=\"nonce\", " \
1298 "uri=\"sip:domain\", response=\"RESPONSE\", algorithm=MD5, " \
1299 "cnonce=\"CNONCE\", opaque=\"OPAQUE\", qop=auth, nc=00000001",
1300 */
1301 static int hdr_test_authorization(pjsip_hdr *h)
1302 {
1303 pjsip_authorization_hdr *hdr = (pjsip_authorization_hdr*)h;
1304
1305 if (h->type != PJSIP_H_AUTHORIZATION)
1306 return -1410;
1307
1308 if (pj_strcmp2(&hdr->scheme, "Digest"))
1309 return -1420;
1310
1311 if (pj_strcmp2(&hdr->credential.digest.username, "username"))
1312 return -1421;
1313
1314 if (pj_strcmp2(&hdr->credential.digest.realm, "realm"))
1315 return -1422;
1316
1317 if (pj_strcmp2(&hdr->credential.digest.nonce, "nonce"))
1318 return -1423;
1319
1320 if (pj_strcmp2(&hdr->credential.digest.uri, "sip:domain"))
1321 return -1424;
1322
1323 if (pj_strcmp2(&hdr->credential.digest.response, "RESPONSE"))
1324 return -1425;
1325
1326 if (pj_strcmp2(&hdr->credential.digest.algorithm, "MD5"))
1327 return -1426;
1328
1329 if (pj_strcmp2(&hdr->credential.digest.cnonce, "CNONCE"))
1330 return -1427;
1331
1332 if (pj_strcmp2(&hdr->credential.digest.opaque, "OPAQUE"))
1333 return -1428;
1334
1335 if (pj_strcmp2(&hdr->credential.digest.qop, "auth"))
1336 return -1429;
1337
1338 if (pj_strcmp2(&hdr->credential.digest.nc, "00000001"))
1339 return -1430;
1340
1341 return 0;
1342 }
1343
1344
1345 /*
1346 "-.!%*_+`'~()<>:\\\"/[]?{}\r\n"
1347 */
1348 static int hdr_test_cid(pjsip_hdr *h)
1349 {
1350 pjsip_cid_hdr *hdr = (pjsip_cid_hdr*)h;
1351
1352 if (h->type != PJSIP_H_CALL_ID)
1353 return -1510;
1354
1355 if (pj_strcmp2(&hdr->id, "-.!%*_+`'~()<>:\\\"/[]?{}"))
1356 return -1520;
1357
1358 return 0;
1359 }
1360
1361 /*
1362 #define SIMPLE_ADDR_SPEC "sip:host"
1363 */
1364 static int test_simple_addr_spec(pjsip_uri *uri)
1365 {
1366 pjsip_sip_uri *sip_uri = (pjsip_sip_uri *)pjsip_uri_get_uri(uri);
1367
1368 if (!PJSIP_URI_SCHEME_IS_SIP(uri))
1369 return -900;
1370
1371 if (pj_strcmp2(&sip_uri->host, "host"))
1372 return -910;
1373
1374 if (sip_uri->port != 0)
1375 return -920;
1376
1377 return 0;
1378 }
1379
1380 /*
1381 #define PARAM_CHAR "][/:&+$"
1382 #define SIMPLE_ADDR_SPEC "sip:host"
1383 #define ADDR_SPEC SIMPLE_ADDR_SPEC ";"PARAM_CHAR"="PARAM_CHAR ";p1=\";\""
1384 #define NAME_ADDR "<" ADDR_SPEC ">"
1385 */
1386 static int nameaddr_test(void *uri)
1387 {
1388 pjsip_sip_uri *sip_uri=(pjsip_sip_uri *)pjsip_uri_get_uri((pjsip_uri*)uri);
1389 pjsip_param *param;
1390 int rc;
1391
1392 if (!PJSIP_URI_SCHEME_IS_SIP(uri))
1393 return -930;
1394
1395 rc = test_simple_addr_spec((pjsip_uri*)sip_uri);
1396 if (rc != 0)
1397 return rc;
1398
1399 if (pj_list_size(&sip_uri->other_param) != 2)
1400 return -940;
1401
1402 param = sip_uri->other_param.next;
1403
1404 if (pj_strcmp2(&param->name, PARAM_CHAR))
1405 return -942;
1406
1407 if (pj_strcmp2(&param->value, PARAM_CHAR))
1408 return -943;
1409
1410 param = param->next;
1411 if (pj_strcmp2(&param->name, "p1"))
1412 return -942;
1413 if (pj_strcmp2(&param->value, "\";\""))
1414 return -943;
1415
1416 return 0;
1417 }
1418
1419 /*
1420 #define GENERIC_PARAM "p0=a;p1=\"ab:;cd\";p2=ab%3acd;p3"
1421 */
1422 static int generic_param_test(pjsip_param *param_head)
1423 {
1424 pjsip_param *param;
1425
1426 if (pj_list_size(param_head) != 4)
1427 return -950;
1428
1429 param = param_head->next;
1430
1431 if (pj_strcmp2(&param->name, "p0"))
1432 return -952;
1433 if (pj_strcmp2(&param->value, "a"))
1434 return -953;
1435
1436 param = param->next;
1437 if (pj_strcmp2(&param->name, "p1"))
1438 return -954;
1439 if (pj_strcmp2(&param->value, "\"ab:;cd\""))
1440 return -955;
1441
1442 param = param->next;
1443 if (pj_strcmp2(&param->name, "p2"))
1444 return -956;
1445 if (pj_strcmp2(&param->value, "ab%3acd"))
1446 return -957;
1447
1448 param = param->next;
1449 if (pj_strcmp2(&param->name, "p3"))
1450 return -958;
1451 if (pj_strcmp2(&param->value, ""))
1452 return -959;
1453
1454 return 0;
1455 }
1456
1457
1458
1459 /*
1460 SIMPLE_ADDR_SPEC ";p1=v1\r\n"
1461 */
1462 static int hdr_test_contact0(pjsip_hdr *h)
1463 {
1464 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1465 pjsip_param *param;
1466 int rc;
1467
1468 if (h->type != PJSIP_H_CONTACT)
1469 return -1610;
1470
1471 rc = test_simple_addr_spec(hdr->uri);
1472 if (rc != 0)
1473 return rc;
1474
1475 if (pj_list_size(&hdr->other_param) != 1)
1476 return -1620;
1477
1478 param = hdr->other_param.next;
1479
1480 if (pj_strcmp2(&param->name, "p1"))
1481 return -1630;
1482
1483 if (pj_strcmp2(&param->value, "v1"))
1484 return -1640;
1485
1486 return 0;
1487 }
1488
1489 /*
1490 NAME_ADDR GENERIC_PARAM "\r\n",
1491 */
1492 static int hdr_test_contact1(pjsip_hdr *h)
1493 {
1494 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1495 int rc;
1496
1497 if (h->type != PJSIP_H_CONTACT)
1498 return -1710;
1499
1500 rc = nameaddr_test(hdr->uri);
1501 if (rc != 0)
1502 return rc;
1503
1504 rc = generic_param_test(&hdr->other_param);
1505 if (rc != 0)
1506 return rc;
1507
1508 return 0;
1509 }
1510
1511 /*
1512 NAME_ADDR ";q=0"
1513 */
1514 static int hdr_test_contact_q0(pjsip_hdr *h)
1515 {
1516 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1517 int rc;
1518
1519 if (h->type != PJSIP_H_CONTACT)
1520 return -1710;
1521
1522 rc = nameaddr_test(hdr->uri);
1523 if (rc != 0)
1524 return rc;
1525
1526 if (hdr->q1000 != 0)
1527 return -1711;
1528
1529 return 0;
1530 }
1531
1532 /*
1533 NAME_ADDR ";q=0.5"
1534 */
1535 static int hdr_test_contact_q1(pjsip_hdr *h)
1536 {
1537 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1538 int rc;
1539
1540 if (h->type != PJSIP_H_CONTACT)
1541 return -1710;
1542
1543 rc = nameaddr_test(hdr->uri);
1544 if (rc != 0)
1545 return rc;
1546
1547 if (hdr->q1000 != 500)
1548 return -1712;
1549
1550 return 0;
1551 }
1552
1553 /*
1554 NAME_ADDR ";q=1"
1555 */
1556 static int hdr_test_contact_q2(pjsip_hdr *h)
1557 {
1558 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1559 int rc;
1560
1561 if (h->type != PJSIP_H_CONTACT)
1562 return -1710;
1563
1564 rc = nameaddr_test(hdr->uri);
1565 if (rc != 0)
1566 return rc;
1567
1568 if (hdr->q1000 != 1000)
1569 return -1713;
1570
1571 return 0;
1572 }
1573
1574 /*
1575 NAME_ADDR ";q=1.0"
1576 */
1577 static int hdr_test_contact_q3(pjsip_hdr *h)
1578 {
1579 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1580 int rc;
1581
1582 if (h->type != PJSIP_H_CONTACT)
1583 return -1710;
1584
1585 rc = nameaddr_test(hdr->uri);
1586 if (rc != 0)
1587 return rc;
1588
1589 if (hdr->q1000 != 1000)
1590 return -1714;
1591
1592 return 0;
1593 }
1594
1595 /*
1596 NAME_ADDR ";q=1.15"
1597 */
1598 static int hdr_test_contact_q4(pjsip_hdr *h)
1599 {
1600 pjsip_contact_hdr *hdr = (pjsip_contact_hdr*)h;
1601 int rc;
1602
1603 if (h->type != PJSIP_H_CONTACT)
1604 return -1710;
1605
1606 rc = nameaddr_test(hdr->uri);
1607 if (rc != 0)
1608 return rc;
1609
1610 if (hdr->q1000 != 1150)
1611 return -1715;
1612
1613 return 0;
1614 }
1615
1616 /*
1617 "10"
1618 */
1619 static int hdr_test_content_length(pjsip_hdr *h)
1620 {
1621 pjsip_clen_hdr *hdr = (pjsip_clen_hdr*)h;
1622
1623 if (h->type != PJSIP_H_CONTENT_LENGTH)
1624 return -1810;
1625
1626 if (hdr->len != 10)
1627 return -1820;
1628
1629 return 0;
1630 }
1631
1632 /*
1633 "application/sdp" GENERIC_PARAM,
1634 */
1635 static int hdr_test_content_type(pjsip_hdr *h)
1636 {
1637 pjsip_ctype_hdr *hdr = (pjsip_ctype_hdr*)h;
1638 const pjsip_param *prm;
1639
1640 if (h->type != PJSIP_H_CONTENT_TYPE)
1641 return -1910;
1642
1643 if (pj_strcmp2(&hdr->media.type, "application"))
1644 return -1920;
1645
1646 if (pj_strcmp2(&hdr->media.subtype, "sdp"))
1647 return -1930;
1648
1649 /* Currently, if the media parameter contains escaped characters,
1650 * pjsip will print the parameter unescaped.
1651 */
1652 prm = hdr->media.param.next;
1653 if (prm == &hdr->media.param) return -1940;
1654 if (pj_strcmp2(&prm->name, "p0")) return -1941;
1655 if (pj_strcmp2(&prm->value, "a")) return -1942;
1656
1657 prm = prm->next;
1658 if (prm == &hdr->media.param) return -1950;
1659 if (pj_strcmp2(&prm->name, "p1")) { PJ_LOG(3,("", "%.*s", (int)prm->name.slen, prm->name.ptr)); return -1951; }
1660 if (pj_strcmp2(&prm->value, "\"ab:;cd\"")) { PJ_LOG(3,("", "%.*s", (int)prm->value.slen, prm->value.ptr)); return -1952; }
1661
1662 prm = prm->next;
1663 if (prm == &hdr->media.param) return -1960;
1664 if (pj_strcmp2(&prm->name, "p2")) return -1961;
1665 if (pj_strcmp2(&prm->value, "ab%3acd")) return -1962;
1666
1667 prm = prm->next;
1668 if (prm == &hdr->media.param) return -1970;
1669 if (pj_strcmp2(&prm->name, "p3")) return -1971;
1670 if (pj_strcmp2(&prm->value, "")) return -1972;
1671
1672 return 0;
1673 }
1674
1675 /*
1676 NAME_ADDR GENERIC_PARAM,
1677 */
1678 static int hdr_test_from(pjsip_hdr *h)
1679 {
1680 pjsip_from_hdr *hdr = (pjsip_from_hdr*)h;
1681 int rc;
1682
1683 if (h->type != PJSIP_H_FROM)
1684 return -2010;
1685
1686 rc = nameaddr_test(hdr->uri);
1687 if (rc != 0)
1688 return rc;
1689
1690 rc = generic_param_test(&hdr->other_param);
1691 if (rc != 0)
1692 return rc;
1693
1694 return 0;
1695 }
1696
1697 /*
1698 "Digest realm=\"realm\", domain=\"sip:domain\", nonce=\"nonce\", " \
1699 "opaque=\"opaque\", stale=true, algorithm=MD5, qop=\"auth\"",
1700 */
1701 static int hdr_test_proxy_authenticate(pjsip_hdr *h)
1702 {
1703 pjsip_proxy_authenticate_hdr *hdr = (pjsip_proxy_authenticate_hdr*)h;
1704
1705 if (h->type != PJSIP_H_PROXY_AUTHENTICATE)
1706 return -2110;
1707
1708 if (pj_strcmp2(&hdr->scheme, "Digest"))
1709 return -2120;
1710
1711 if (pj_strcmp2(&hdr->challenge.digest.realm, "realm"))
1712 return -2130;
1713
1714 if (pj_strcmp2(&hdr->challenge.digest.domain, "sip:domain"))
1715 return -2140;
1716
1717 if (pj_strcmp2(&hdr->challenge.digest.nonce, "nonce"))
1718 return -2150;
1719
1720 if (pj_strcmp2(&hdr->challenge.digest.opaque, "opaque"))
1721 return -2160;
1722
1723 if (hdr->challenge.digest.stale != 1)
1724 return -2170;
1725
1726 if (pj_strcmp2(&hdr->challenge.digest.algorithm, "MD5"))
1727 return -2180;
1728
1729 if (pj_strcmp2(&hdr->challenge.digest.qop, "auth"))
1730 return -2190;
1731
1732 return 0;
1733 }
1734
1735 /*
1736 NAME_ADDR GENERIC_PARAM,
1737 */
1738 static int hdr_test_record_route(pjsip_hdr *h)
1739 {
1740 pjsip_rr_hdr *hdr = (pjsip_rr_hdr*)h;
1741 int rc;
1742
1743 if (h->type != PJSIP_H_RECORD_ROUTE)
1744 return -2210;
1745
1746 rc = nameaddr_test(&hdr->name_addr);
1747 if (rc != 0)
1748 return rc;
1749
1750 rc = generic_param_test(&hdr->other_param);
1751 if (rc != 0)
1752 return rc;
1753
1754 return 0;
1755
1756 }
1757
1758 /*
1759 " \r\n"
1760 */
1761 static int hdr_test_supported(pjsip_hdr *h)
1762 {
1763 pjsip_supported_hdr *hdr = (pjsip_supported_hdr*)h;
1764
1765 if (h->type != PJSIP_H_SUPPORTED)
1766 return -2310;
1767
1768 if (hdr->count != 0)
1769 return -2320;
1770
1771 return 0;
1772 }
1773
1774 /*
1775 ",outbound"
1776 Test for handling leading comma (buggy MizuDroid format)
1777 */
1778 static int hdr_test_supported_with_leading_comma(pjsip_hdr *h)
1779 {
1780 pjsip_supported_hdr *hdr = (pjsip_supported_hdr*)h;
1781
1782 if (h->type != PJSIP_H_SUPPORTED)
1783 return -2330;
1784
1785 if (hdr->count != 1)
1786 return -2331;
1787
1788 if (pj_strcmp2(&hdr->values[0], "outbound") != 0)
1789 return -2332;
1790
1791 return 0;
1792 }
1793
1794 /*
1795 "outbound,"
1796 Test for handling trailing comma
1797 */
1798 static int hdr_test_supported_with_trailing_comma(pjsip_hdr *h)
1799 {
1800 pjsip_supported_hdr *hdr = (pjsip_supported_hdr*)h;
1801
1802 if (h->type != PJSIP_H_SUPPORTED)
1803 return -2340;
1804
1805 if (hdr->count != 1)
1806 return -2341;
1807
1808 if (pj_strcmp2(&hdr->values[0], "outbound") != 0)
1809 return -2342;
1810
1811 return 0;
1812 }
1813
1814 /*
1815 ",,,outbound,path"
1816 Test for handling multiple consecutive commas at beginning
1817 */
1818 static int hdr_test_supported_with_multiple_commas(pjsip_hdr *h)
1819 {
1820 pjsip_supported_hdr *hdr = (pjsip_supported_hdr*)h;
1821
1822 if (h->type != PJSIP_H_SUPPORTED)
1823 return -2350;
1824
1825 if (hdr->count != 2)
1826 return -2351;
1827
1828 if (pj_strcmp2(&hdr->values[0], "outbound") != 0)
1829 return -2352;
1830
1831 if (pj_strcmp2(&hdr->values[1], "path") != 0)
1832 return -2353;
1833
1834 return 0;
1835 }
1836
1837 /*
1838 "outbound,,,path"
1839 Test for handling multiple consecutive commas in middle
1840 */
1841 static int hdr_test_supported_with_middle_commas(pjsip_hdr *h)
1842 {
1843 pjsip_supported_hdr *hdr = (pjsip_supported_hdr*)h;
1844
1845 if (h->type != PJSIP_H_SUPPORTED)
1846 return -2360;
1847
1848 if (hdr->count != 2)
1849 return -2361;
1850
1851 if (pj_strcmp2(&hdr->values[0], "outbound") != 0)
1852 return -2362;
1853
1854 if (pj_strcmp2(&hdr->values[1], "path") != 0)
1855 return -2363;
1856
1857 return 0;
1858 }
1859
1860 /*
1861 NAME_ADDR GENERIC_PARAM,
1862 */
1863 static int hdr_test_to(pjsip_hdr *h)
1864 {
1865 pjsip_to_hdr *hdr = (pjsip_to_hdr*)h;
1866 int rc;
1867
1868 if (h->type != PJSIP_H_TO)
1869 return -2410;
1870
1871 rc = nameaddr_test(hdr->uri);
1872 if (rc != 0)
1873 return rc;
1874
1875 rc = generic_param_test(&hdr->other_param);
1876 if (rc != 0)
1877 return rc;
1878
1879 return 0;
1880 }
1881
1882 /*
1883 "SIP/2.0 host" GENERIC_PARAM
1884 */
1885 static int hdr_test_via(pjsip_hdr *h)
1886 {
1887 pjsip_via_hdr *hdr = (pjsip_via_hdr*)h;
1888 int rc;
1889
1890 if (h->type != PJSIP_H_VIA)
1891 return -2510;
1892
1893 if (pj_strcmp2(&hdr->transport, "XYZ"))
1894 return -2515;
1895
1896 if (pj_strcmp2(&hdr->sent_by.host, "host"))
1897 return -2520;
1898
1899 if (hdr->sent_by.port != 0)
1900 return -2530;
1901
1902 rc = generic_param_test(&hdr->other_param);
1903 if (rc != 0)
1904 return rc;
1905
1906 return 0;
1907 }
1908
1909
1910 /*
1911 "SIP/2.0/UDP [::1]"
1912 */
1913 static int hdr_test_via_ipv6_1(pjsip_hdr *h)
1914 {
1915 pjsip_via_hdr *hdr = (pjsip_via_hdr*)h;
1916
1917 if (h->type != PJSIP_H_VIA)
1918 return -2610;
1919
1920 if (pj_strcmp2(&hdr->transport, "UDP"))
1921 return -2615;
1922
1923 if (pj_strcmp2(&hdr->sent_by.host, "::1"))
1924 return -2620;
1925
1926 if (hdr->sent_by.port != 0)
1927 return -2630;
1928
1929 return 0;
1930 }
1931
1932 /* "SIP/2.0/UDP [::1]:5061" */
1933 static int hdr_test_via_ipv6_2(pjsip_hdr *h)
1934 {
1935 pjsip_via_hdr *hdr = (pjsip_via_hdr*)h;
1936
1937 if (h->type != PJSIP_H_VIA)
1938 return -2710;
1939
1940 if (pj_strcmp2(&hdr->transport, "UDP"))
1941 return -2715;
1942
1943 if (pj_strcmp2(&hdr->sent_by.host, "::1"))
1944 return -2720;
1945
1946 if (hdr->sent_by.port != 5061)
1947 return -2730;
1948
1949 return 0;
1950 }
1951
1952 /* "SIP/2.0/UDP [::1];rport=5061;received=::2" */
1953 static int hdr_test_via_ipv6_3(pjsip_hdr *h)
1954 {
1955 pjsip_via_hdr *hdr = (pjsip_via_hdr*)h;
1956
1957 if (h->type != PJSIP_H_VIA)
1958 return -2810;
1959
1960 if (pj_strcmp2(&hdr->transport, "UDP"))
1961 return -2815;
1962
1963 if (pj_strcmp2(&hdr->sent_by.host, "::1"))
1964 return -2820;
1965
1966 if (hdr->sent_by.port != 0)
1967 return -2830;
1968
1969 if (pj_strcmp2(&hdr->recvd_param, "::2"))
1970 return -2840;
1971
1972 if (hdr->rport_param != 5061)
1973 return -2850;
1974
1975 return 0;
1976 }
1977
1978 /* "10(Already Pending Register)" */
1979 static int hdr_test_retry_after1(pjsip_hdr *h)
1980 {
1981 pjsip_retry_after_hdr *hdr = (pjsip_retry_after_hdr*)h;
1982
1983 if (h->type != PJSIP_H_RETRY_AFTER)
1984 return -2910;
1985
1986 if (hdr->ivalue != 10)
1987 return -2920;
1988
1989 if (pj_strcmp2(&hdr->comment, "Already Pending Register"))
1990 return -2930;
1991
1992 return 0;
1993 }
1994
1995 /* Subject: \xC0\x81 */
1996 static int hdr_test_subject_utf(pjsip_hdr *h)
1997 {
1998 pjsip_subject_hdr *hdr = (pjsip_subject_hdr*)h;
1999
2000 if (pj_strcmp2(&h->name, "Subject"))
2001 return -2950;
2002
2003 if (pj_strcmp2(&hdr->hvalue, "\xC0\x81"))
2004 return -2960;
2005
2006 return 0;
2007 }
2008
2009 static int hdr_test(void)
2010 {
2011 unsigned i;
2012
2013 PJ_LOG(3,(THIS_FILE, " testing header parsing.."));
2014
2015 for (i=0; i<PJ_ARRAY_SIZE(hdr_test_data); ++i) {
2016 struct hdr_test_t *test = &hdr_test_data[i];
2017 pj_str_t hname;
2018 pj_ssize_t len;
2019 int parsed_len;
2020 pj_pool_t *pool;
2021 pjsip_hdr *parsed_hdr1=NULL, *parsed_hdr2=NULL;
2022 char *input, *output;
2023 #if defined(PJSIP_UNESCAPE_IN_PLACE) && PJSIP_UNESCAPE_IN_PLACE!=0
2024 static char hcontent[1024];
2025 #else
2026 char *hcontent;
2027 #endif
2028 int rc;
2029
2030 pool = pjsip_endpt_create_pool(endpt, NULL, POOL_SIZE, POOL_SIZE);
2031
2032 /* Parse the header */
2033 hname = pj_str(test->hname);
2034 len = strlen(test->hcontent);
2035 #if defined(PJSIP_UNESCAPE_IN_PLACE) && PJSIP_UNESCAPE_IN_PLACE!=0
2036 PJ_ASSERT_RETURN(len < sizeof(hcontent), PJSIP_EMSGTOOLONG);
2037 pj_ansi_strxcpy(hcontent, test->hcontent, sizeof(hcontent));
2038 #else
2039 hcontent = test->hcontent;
2040 #endif
2041
2042 parsed_hdr1 = (pjsip_hdr*) pjsip_parse_hdr(pool, &hname,
2043 hcontent, len,
2044 &parsed_len);
2045 if (parsed_hdr1 == NULL) {
2046 if (test->flags & HDR_FLAG_PARSE_FAIL) {
2047 pj_pool_release(pool);
2048 continue;
2049 }
2050 PJ_LOG(3,(THIS_FILE, " error parsing header %s: %s", test->hname, test->hcontent));
2051 return -500;
2052 }
2053
2054 /* Test the parsing result */
2055 if (test->test && (rc=test->test(parsed_hdr1)) != 0) {
2056 PJ_LOG(3,(THIS_FILE, " validation failed for header %s: %s", test->hname, test->hcontent));
2057 PJ_LOG(3,(THIS_FILE, " error code is %d", rc));
2058 return -502;
2059 }
2060
2061 #if 1
2062 /* Parse with hshortname, if present */
2063 if (test->hshort_name) {
2064 hname = pj_str(test->hshort_name);
2065 len = strlen(test->hcontent);
2066 #if defined(PJSIP_UNESCAPE_IN_PLACE) && PJSIP_UNESCAPE_IN_PLACE!=0
2067 PJ_ASSERT_RETURN(len < sizeof(hcontent), PJSIP_EMSGTOOLONG);
2068 pj_ansi_strxcpy(hcontent, test->hcontent, sizeof(hcontent));
2069 #else
2070 hcontent = test->hcontent;
2071 #endif
2072
2073 parsed_hdr2 = (pjsip_hdr*) pjsip_parse_hdr(pool, &hname, hcontent, len, &parsed_len);
2074 if (parsed_hdr2 == NULL) {
2075 PJ_LOG(3,(THIS_FILE, " error parsing header %s: %s", test->hshort_name, test->hcontent));
2076 return -510;
2077 }
2078 }
2079 #endif
2080
2081 if (test->flags & HDR_FLAG_DONT_PRINT) {
2082 pj_pool_release(pool);
2083 continue;
2084 }
2085
2086 /* Print the original header */
2087 input = (char*) pj_pool_alloc(pool, 1024);
2088 len = pj_ansi_snprintf(input, 1024, "%s: %s", test->hname, test->hcontent);
2089 if (len < 1 || len >= 1024)
2090 return -520;
2091
2092 /* Print the parsed header*/
2093 output = (char*) pj_pool_alloc(pool, 1024);
2094 len = pjsip_hdr_print_on(parsed_hdr1, output, 1024);
2095 if (len < 0 || len >= 1024) {
2096 PJ_LOG(3,(THIS_FILE, " header too long: %s: %s", test->hname, test->hcontent));
2097 return -530;
2098 }
2099 output[len] = 0;
2100
2101 if (strcmp(input, output) != 0) {
2102 PJ_LOG(3,(THIS_FILE, " header character by character comparison failed."));
2103 PJ_LOG(3,(THIS_FILE, " original header=|%s|", input));
2104 PJ_LOG(3,(THIS_FILE, " parsed header =|%s|", output));
2105 return -540;
2106 }
2107
2108 pj_pool_release(pool);
2109 }
2110
2111 return 0;
2112 }
2113
2114
2115 /*****************************************************************************/
2116 /* Test that parsing a complete SIP message with a malformed registered header
2117 * value does not crash and always produces a valid (non-NULL) hname pointer
2118 * in every error-report entry.
2119 *
2120 * This directly exercises the longjmp/volatile clobbering fix in
2121 * int_parse_msg() (sip_parser.c): before that fix, longjmp() could restore
2122 * the CPU registers holding hname.ptr/hname.slen to their setjmp-time
2123 * values, yielding a garbage pointer that later caused SIGSEGV when the
2124 * error was logged via pj_ansi_snprintf(..., "%.*s", ..., err->hname.ptr).
2125 */
2126 static pj_status_t malformed_hdr_test(void)
2127 {
2128 /* Each test case is a complete SIP message that contains at least one
2129 * header whose value is malformed enough to cause the registered handler
2130 * to throw a PJ_THROW exception.
2131 */
2132 static const struct {
2133 const char *desc;
2134 const char *msg;
2135 const char *expected_hname; /* expected hname in first error entry */
2136 } cases[] = {
2137 {
2138 /* Max-Forwards handler calls parse_generic_int_hdr() which calls
2139 * pj_scan_get(DIGIT_SPEC,...). "notanumber" has no digits, so
2140 * pj_scan_get() throws immediately.
2141 */
2142 "Max-Forwards: notanumber",
2143 "INVITE sip:[email protected] SIP/2.0\r\n"
2144 "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8\r\n"
2145 "Max-Forwards: notanumber\r\n"
2146 "To: Bob <sip:[email protected]>\r\n"
2147 "From: Alice <sip:[email protected]>;tag=1928301774\r\n"
2148 "Call-ID: [email protected]\r\n"
2149 "CSeq: 1 INVITE\r\n"
2150 "Content-Length: 0\r\n"
2151 "\r\n",
2152 "Max-Forwards"
2153 },
2154 {
2155 /* Content-Length handler also calls pj_scan_get(DIGIT_SPEC,...).
2156 * Same throw path as above but with a different header.
2157 */
2158 "Content-Length: notanumber",
2159 "SIP/2.0 200 OK\r\n"
2160 "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bKnashds8\r\n"
2161 "To: Bob <sip:[email protected]>;tag=abc\r\n"
2162 "From: Alice <sip:[email protected]>;tag=1928301774\r\n"
2163 "Call-ID: [email protected]\r\n"
2164 "CSeq: 1 INVITE\r\n"
2165 "Content-Length: notanumber\r\n"
2166 "\r\n",
2167 "Content-Length"
2168 },
2169 };
2170
2171 unsigned i;
2172
2173 PJ_LOG(3,(THIS_FILE, " testing malformed header error reporting.."));
2174
2175 for (i = 0; i < PJ_ARRAY_SIZE(cases); ++i) {
2176 pj_pool_t *pool;
2177 pjsip_parser_err_report err_list;
2178 pjsip_parser_err_report *err;
2179 pj_size_t len;
2180
2181 pool = pjsip_endpt_create_pool(endpt, NULL, POOL_SIZE, POOL_SIZE);
2182 pj_list_init(&err_list);
2183 len = pj_ansi_strlen(cases[i].msg);
2184
2185 /* Must not crash (SIGSEGV was the pre-fix symptom). */
2186 /* Cast away const: pjsip_parse_msg() requires a mutable buffer
2187 * (it modifies it during scanning), and the test data is read-only.
2188 */
2189 pjsip_parse_msg(pool, (char *)cases[i].msg, len, &err_list);
2190
2191 /* The malformed header value must have produced at least one
2192 * error report entry.
2193 */
2194 if (pj_list_empty(&err_list)) {
2195 PJ_LOG(3,(THIS_FILE,
2196 " error: no parse error reported for: %s",
2197 cases[i].desc));
2198 pjsip_endpt_release_pool(endpt, pool);
2199 return -600;
2200 }
2201
2202 /* Every error entry must carry a non-NULL hname.ptr.
2203 * Passing NULL to %.*s (as done in sip_transport.c) is
2204 * undefined behavior even when the length field is 0.
2205 */
2206 for (err = err_list.next; err != &err_list; err = err->next) {
2207 if (err->hname.ptr == NULL) {
2208 PJ_LOG(3,(THIS_FILE,
2209 " error: err_info->hname.ptr is NULL for: %s",
2210 cases[i].desc));
2211 pjsip_endpt_release_pool(endpt, pool);
2212 return -610;
2213 }
2214 }
2215
2216 /* The first error entry must identify the offending header. */
2217 err = err_list.next;
2218 if (pj_strcmp2(&err->hname, cases[i].expected_hname) != 0) {
2219 PJ_LOG(3,(THIS_FILE,
2220 " error: wrong hname '%.*s', expected '%s' for: %s",
2221 (int)err->hname.slen, err->hname.ptr,
2222 cases[i].expected_hname, cases[i].desc));
2223 pjsip_endpt_release_pool(endpt, pool);
2224 return -620;
2225 }
2226
2227 pjsip_endpt_release_pool(endpt, pool);
2228 }
2229
2230 return PJ_SUCCESS;
2231 }
2232
2233
2234 /*****************************************************************************/
2235
2236 int msg_test(void)
2237 {
2238 enum { COUNT = 1, DETECT=0, PARSE=1, PRINT=2 };
2239 struct {
2240 unsigned detect;
2241 unsigned parse;
2242 unsigned print;
2243 } run[COUNT];
2244 unsigned i, max, avg_len;
2245 char desc[250];
2246 pj_status_t status;
2247
2248 status = hdr_test();
2249 if (status != 0)
2250 return status;
2251
2252 status = simple_test();
2253 if (status != PJ_SUCCESS)
2254 return status;
2255
2256 status = malformed_hdr_test();
2257 if (status != PJ_SUCCESS)
2258 return status;
2259
2260 #if INCLUDE_BENCHMARKS
2261 for (i=0; i<COUNT; ++i) {
2262 PJ_LOG(3,(THIS_FILE, " benchmarking (%d of %d)..", i+1, COUNT));
2263 status = msg_benchmark(&run[i].detect, &run[i].parse, &run[i].print);
2264 if (status != PJ_SUCCESS)
2265 return status;
2266 }
2267
2268 /* Calculate average message length */
2269 for (i=0, avg_len=0; i<PJ_ARRAY_SIZE(test_array); ++i) {
2270 avg_len += (unsigned)test_array[i].len;
2271 }
2272 avg_len /= PJ_ARRAY_SIZE(test_array);
2273
2274
2275 /* Print maximum detect/sec */
2276 for (i=0, max=0; i<COUNT; ++i)
2277 if (run[i].detect > max) max = run[i].detect;
2278
2279 PJ_LOG(3,("", " Maximum message detection/sec=%u", max));
2280
2281 pj_ansi_snprintf(desc, sizeof(desc),
2282 "Number of SIP messages "
2283 "can be pre-parse by <tt>pjsip_find_msg()</tt> "
2284 "per second (tested with %d message sets with "
2285 "average message length of "
2286 "%d bytes)", (int)PJ_ARRAY_SIZE(test_array), avg_len);
2287 report_ival("msg-detect-per-sec", max, "msg/sec", desc);
2288
2289 /* Print maximum parse/sec */
2290 for (i=0, max=0; i<COUNT; ++i)
2291 if (run[i].parse > max) max = run[i].parse;
2292
2293 PJ_LOG(3,("", " Maximum message parsing/sec=%u", max));
2294
2295 pj_ansi_snprintf(desc, sizeof(desc),
2296 "Number of SIP messages "
2297 "can be <b>parsed</b> by <tt>pjsip_parse_msg()</tt> "
2298 "per second (tested with %d message sets with "
2299 "average message length of "
2300 "%d bytes)", (int)PJ_ARRAY_SIZE(test_array), avg_len);
2301 report_ival("msg-parse-per-sec", max, "msg/sec", desc);
2302
2303 /* Msg parsing bandwidth */
2304 report_ival("msg-parse-bandwidth-mb", avg_len*max/1000000, "MB/sec",
2305 "Message parsing bandwidth in megabytes (number of megabytes"
2306 " worth of SIP messages that can be parsed per second). "
2307 "The value is derived from msg-parse-per-sec above.");
2308
2309
2310 /* Print maximum print/sec */
2311 for (i=0, max=0; i<COUNT; ++i)
2312 if (run[i].print > max) max = run[i].print;
2313
2314 PJ_LOG(3,("", " Maximum message print/sec=%u", max));
2315
2316 pj_ansi_snprintf(desc, sizeof(desc),
2317 "Number of SIP messages "
2318 "can be <b>printed</b> by <tt>pjsip_msg_print()</tt>"
2319 " per second (tested with %d message sets with "
2320 "average message length of "
2321 "%d bytes)", (int)PJ_ARRAY_SIZE(test_array), avg_len);
2322
2323 report_ival("msg-print-per-sec", max, "msg/sec", desc);
2324
2325 /* Msg print bandwidth */
2326 report_ival("msg-printed-bandwidth-mb", avg_len*max/1000000, "MB/sec",
2327 "Message print bandwidth in megabytes (total size of "
2328 "SIP messages printed per second). "
2329 "The value is derived from msg-print-per-sec above.");
2330
2331 #endif /* INCLUDE_BENCHMARKS */
2332
2333 return PJ_SUCCESS;
2334 }
2335
2336
2337
2338