Debian Codegraph @

Xpjproject/pjsip/src/test/uri_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 THIS_FILE "uri_test.c"
24
25
26 #define ALPHANUM "abcdefghijklmnopqrstuvwxyz" \
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
28 "0123456789"
29 #define MARK "-_.!~*'()"
30 #define USER_CHAR ALPHANUM MARK "&=+$,;?/"
31 #define PASS_CHAR ALPHANUM MARK "&=+$,"
32 #define PARAM_CHAR ALPHANUM MARK "[]/:&+$"
33
34 #define POOL_SIZE 8000
35 #if defined(PJ_DEBUG) && PJ_DEBUG!=0
36 # define LOOP_COUNT 10000
37 #else
38 # define LOOP_COUNT 40000
39 #endif
40 #define AVERAGE_URL_LEN 80
41 #define THREAD_COUNT 4
42
43 static struct
44 {
45 pj_highprec_t parse_len, print_len, cmp_len;
46 pj_timestamp parse_time, print_time, cmp_time;
47 } var;
48
49
50 /* URI creator functions. */
51 static pjsip_uri *create_uri0( pj_pool_t *pool );
52 static pjsip_uri *create_uri1( pj_pool_t *pool );
53 static pjsip_uri *create_uri2( pj_pool_t *pool );
54 static pjsip_uri *create_uri3( pj_pool_t *pool );
55 static pjsip_uri *create_uri4( pj_pool_t *pool );
56 static pjsip_uri *create_uri5( pj_pool_t *pool );
57 static pjsip_uri *create_uri6( pj_pool_t *pool );
58 static pjsip_uri *create_uri7( pj_pool_t *pool );
59 static pjsip_uri *create_uri8( pj_pool_t *pool );
60 static pjsip_uri *create_uri9( pj_pool_t *pool );
61 static pjsip_uri *create_uri10( pj_pool_t *pool );
62 static pjsip_uri *create_uri11( pj_pool_t *pool );
63 static pjsip_uri *create_uri12( pj_pool_t *pool );
64 static pjsip_uri *create_uri13( pj_pool_t *pool );
65 static pjsip_uri *create_uri14( pj_pool_t *pool );
66 static pjsip_uri *create_uri15( pj_pool_t *pool );
67 static pjsip_uri *create_uri16( pj_pool_t *pool );
68 static pjsip_uri *create_uri17( pj_pool_t *pool );
69 static pjsip_uri *create_uri25( pj_pool_t *pool );
70 static pjsip_uri *create_uri26( pj_pool_t *pool );
71 static pjsip_uri *create_uri27( pj_pool_t *pool );
72 static pjsip_uri *create_uri28( pj_pool_t *pool );
73 static pjsip_uri *create_uri29( pj_pool_t *pool );
74 static pjsip_uri *create_uri30( pj_pool_t *pool );
75 static pjsip_uri *create_uri31( pj_pool_t *pool );
76 static pjsip_uri *create_uri32( pj_pool_t *pool );
77 static pjsip_uri *create_uri33( pj_pool_t *pool );
78 static pjsip_uri *create_uri34( pj_pool_t *pool );
79 static pjsip_uri *create_uri35( pj_pool_t *pool );
80 static pjsip_uri *create_uri36( pj_pool_t *pool );
81 static pjsip_uri *create_uri37( pj_pool_t *pool );
82 static pjsip_uri *create_uri38( pj_pool_t *pool );
83 static pjsip_uri *create_uri39( pj_pool_t *pool );
84 static pjsip_uri *create_uri40( pj_pool_t *pool );
85 static pjsip_uri *create_dummy( pj_pool_t *pool );
86
87 #define ERR_NOT_EQUAL -1001
88 #define ERR_SYNTAX_ERR -1002
89
90 struct uri_test
91 {
92 pj_status_t status;
93 char str[PJSIP_MAX_URL_SIZE];
94 pjsip_uri *(*creator)(pj_pool_t *pool);
95 const char *printed;
96 pj_size_t len;
97 } uri_test_array[] =
98 {
99 {
100 PJ_SUCCESS,
101 "sip:localhost",
102 &create_uri0
103 },
104 {
105 PJ_SUCCESS,
106 "sip:user@localhost",
107 &create_uri1
108 },
109 {
110 PJ_SUCCESS,
111 "sip:user:password@localhost:5060",
112 &create_uri2, },
113 {
114 /* Port is specified should not match unspecified port. */
115 ERR_NOT_EQUAL,
116 "sip:localhost:5060",
117 &create_uri3
118 },
119 {
120 /* All recognized parameters. */
121 PJ_SUCCESS,
122 "sip:localhost;transport=tcp;user=ip;ttl=255;lr;maddr=127.0.0.1;method=ACK",
123 &create_uri4
124 },
125 {
126 /* Params mixed with other params and header params. */
127 PJ_SUCCESS,
128 "sip:localhost;pickup=hurry;user=phone;message=I%20am%20sorry"
129 "?Subject=Hello%20There&Server=SIP%20Server",
130 &create_uri5
131 },
132 {
133 /* SIPS. */
134 PJ_SUCCESS,
135 "sips:localhost",
136 &create_uri6,
137 },
138 {
139 /* Name address */
140 PJ_SUCCESS,
141 "<sip:localhost>",
142 &create_uri7
143 },
144 {
145 /* Name address with display name and SIPS scheme with some redundant
146 * whitespaced.
147 */
148 PJ_SUCCESS,
149 " Power Administrator <sips:localhost>",
150 &create_uri8
151 },
152 {
153 /* Name address. */
154 PJ_SUCCESS,
155 " \"User\" <sip:user@localhost:5071>",
156 &create_uri9
157 },
158 {
159 /* Escaped sequence in display name (display=Strange User\"\\\"). */
160 PJ_SUCCESS,
161 " \"Strange User\\\"\\\\\\\"\" <sip:localhost>",
162 &create_uri10,
163 },
164 {
165 /* Errorneous escaping in display name. */
166 ERR_SYNTAX_ERR,
167 " \"Rogue User\\\" <sip:localhost>",
168 &create_uri11,
169 },
170 {
171 /* Dangling quote in display name, but that should be OK. */
172 PJ_SUCCESS,
173 "Strange User\" <sip:localhost>",
174 &create_uri12,
175 },
176 {
177 /* Special characters in parameter value must be quoted. */
178 PJ_SUCCESS,
179 "sip:localhost;pvalue=\"hello world\"",
180 &create_uri13,
181 },
182 {
183 /* Excercise strange character sets allowed in display, user, password,
184 * host, and port.
185 */
186 PJ_SUCCESS,
187 "This is -. !% *_+`'~ me <sip:a19A&=+$,;?/%2c:%40a&Zz=+$,@"
188 "my_proxy09.MY-domain.com:9801>",
189 &create_uri14,
190 },
191 {
192 /* Another excercise to the allowed character sets to the hostname. */
193 PJ_SUCCESS,
194 "sip:" ALPHANUM "-_.com",
195 &create_uri15,
196 },
197 {
198 /* Another excercise to the allowed character sets to the username
199 * and password.
200 */
201 PJ_SUCCESS,
202 "sip:" USER_CHAR ":" PASS_CHAR "@host",
203 &create_uri16,
204 },
205 {
206 /* Excercise to the pname and pvalue, and mixup of other-param
207 * between 'recognized' params.
208 */
209 PJ_SUCCESS,
210 "sip:host;user=ip;" PARAM_CHAR "%21=" PARAM_CHAR "%21"
211 ";lr;other=1;transport=sctp;other2",
212 &create_uri17,
213 },
214 {
215 /* 18: This should trigger syntax error. */
216 ERR_SYNTAX_ERR,
217 "sip:",
218 &create_dummy,
219 },
220 {
221 /* 19: Syntax error: whitespace after scheme. */
222 ERR_SYNTAX_ERR,
223 "sip :host",
224 &create_dummy,
225 },
226 {
227 /* 20: Syntax error: whitespace before hostname. */
228 ERR_SYNTAX_ERR,
229 "sip: host",
230 &create_dummy,
231 },
232 {
233 /* 21: Syntax error: invalid port. */
234 ERR_SYNTAX_ERR,
235 "sip:user:password",
236 &create_dummy,
237 },
238 {
239 /* 22: Syntax error: no host. */
240 ERR_SYNTAX_ERR,
241 "sip:user@",
242 &create_dummy,
243 },
244 {
245 /* 23: Syntax error: no user/host. */
246 ERR_SYNTAX_ERR,
247 "sip:@",
248 &create_dummy,
249 },
250 {
251 /* 24: Syntax error: empty string. */
252 ERR_SYNTAX_ERR,
253 "",
254 &create_dummy,
255 },
256 {
257 /* 25: Simple tel: URI with global context */
258 PJ_SUCCESS,
259 "tel:+1-201-555-0123",
260 &create_uri25,
261 "tel:+1-201-555-0123"
262 },
263 {
264 /* 26: Simple tel: URI with local context */
265 PJ_SUCCESS,
266 "tel:7042;phone-context=example.com",
267 &create_uri26,
268 "tel:7042;phone-context=example.com"
269 },
270 {
271 /* 27: Simple tel: URI with local context */
272 PJ_SUCCESS,
273 "tel:863-1234;phone-context=+1-914-555",
274 &create_uri27,
275 "tel:863-1234;phone-context=+1-914-555"
276 },
277 {
278 /* 28: Comparison between local and global number */
279 ERR_NOT_EQUAL,
280 "tel:+1",
281 &create_uri28,
282 "tel:+1"
283 },
284 {
285 /* 29: tel: with some visual chars and spaces */
286 PJ_SUCCESS,
287 "tel:(44).1234-*#+Deaf",
288 &create_uri29,
289 "tel:(44).1234-*#+Deaf"
290 },
291 {
292 /* 30: isub parameters */
293 PJ_SUCCESS,
294 "tel:+1;isub=/:@&$,-_.!~*'()[]/:&$aA1%21+=",
295 &create_uri30,
296 "tel:+1;isub=/:@&$,-_.!~*'()[]/:&$aA1!+%3d"
297 },
298 {
299 /* 31: extension number parsing and encoding */
300 PJ_SUCCESS,
301 "tel:+1;ext=+123",
302 &create_uri31,
303 "tel:+1;ext=%2b123"
304 },
305 {
306 /* 32: context parameter parsing and encoding */
307 PJ_SUCCESS,
308 "tel:911;phone-context=+1-911",
309 &create_uri32,
310 "tel:911;phone-context=+1-911"
311 },
312 {
313 /* 33: case-insensitive comparison */
314 PJ_SUCCESS,
315 "tel:911;phone-context=emergency.example.com",
316 &create_uri33,
317 "tel:911;phone-context=emergency.example.com"
318 },
319 {
320 /* 34: parameter only appears in one URL */
321 ERR_NOT_EQUAL,
322 "tel:911;p1=p1;p2=p2",
323 &create_uri34,
324 "tel:911;p1=p1;p2=p2"
325 },
326 {
327 /* 35: IPv6 in host and maddr parameter */
328 PJ_SUCCESS,
329 "sip:user@[::1];maddr=[::01]",
330 &create_uri35,
331 "sip:user@[::1];maddr=[::01]"
332 },
333 {
334 /* 36: IPv6 in host and maddr, without username */
335 PJ_SUCCESS,
336 "sip:[::1];maddr=[::01]",
337 &create_uri36,
338 "sip:[::1];maddr=[::01]"
339 },
340 {
341 /* 37: Non-ASCII UTF-8 in display name, with quote */
342 PJ_SUCCESS,
343 "\"\xC0\x81\" <sip:localhost>",
344 &create_uri37,
345 "\"\xC0\x81\" <sip:localhost>"
346 },
347 {
348 /* 38: Non-ASCII UTF-8 in display name, without quote */
349 PJ_SUCCESS,
350 "\xC0\x81 <sip:localhost>",
351 &create_uri38,
352 "\"\xC0\x81\" <sip:localhost>"
353 },
354 {
355 /* Even number of backslash before end quote in display name. */
356 PJ_SUCCESS,
357 "\"User\\\\\" <sip:localhost>",
358 &create_uri39,
359 },
360 {
361 /* Quoted display name. */
362 PJ_SUCCESS,
363 "\"\\\"User\\\"\" <sip:localhost>",
364 &create_uri40,
365 }
366
367 };
368
369 static pjsip_uri *create_uri0(pj_pool_t *pool)
370 {
371 /* "sip:localhost" */
372 pjsip_sip_uri *url = pjsip_sip_uri_create(pool, 0);
373
374 pj_strdup2(pool, &url->host, "localhost");
375 return (pjsip_uri*)url;
376 }
377
378 static pjsip_uri *create_uri1(pj_pool_t *pool)
379 {
380 /* "sip:user@localhost" */
381 pjsip_sip_uri *url = pjsip_sip_uri_create(pool, 0);
382
383 pj_strdup2( pool, &url->user, "user");
384 pj_strdup2( pool, &url->host, "localhost");
385
386 return (pjsip_uri*) url;
387 }
388
389 static pjsip_uri *create_uri2(pj_pool_t *pool)
390 {
391 /* "sip:user:password@localhost:5060" */
392 pjsip_sip_uri *url = pjsip_sip_uri_create(pool, 0);
393
394 pj_strdup2( pool, &url->user, "user");
395 pj_strdup2( pool, &url->passwd, "password");
396 pj_strdup2( pool, &url->host, "localhost");
397 url->port = 5060;
398
399 return (pjsip_uri*) url;
400 }
401
402 static pjsip_uri *create_uri3(pj_pool_t *pool)
403 {
404 /* Like: "sip:localhost:5060", but without the port. */
405 pjsip_sip_uri *url = pjsip_sip_uri_create(pool, 0);
406
407 pj_strdup2(pool, &url->host, "localhost");
408 return (pjsip_uri*)url;
409 }
410
411 static pjsip_uri *create_uri4(pj_pool_t *pool)
412 {
413 /* "sip:localhost;transport=tcp;user=ip;ttl=255;lr;maddr=127.0.0.1;method=ACK" */
414 pjsip_sip_uri *url = pjsip_sip_uri_create(pool, 0);
415
416 pj_strdup2(pool, &url->host, "localhost");
417 pj_strdup2(pool, &url->transport_param, "tcp");
418 pj_strdup2(pool, &url->user_param, "ip");
419 url->ttl_param = 255;
420 url->lr_param = 1;
421 pj_strdup2(pool, &url->maddr_param, "127.0.0.1");
422 pj_strdup2(pool, &url->method_param, "ACK");
423
424 return (pjsip_uri*)url;
425 }
426
427 #define param_add(list,pname,pvalue) \
428 do { \
429 pjsip_param *param; \
430 param=PJ_POOL_ALLOC_T(pool, pjsip_param); \
431 param->name = pj_str(pname); \
432 param->value = pj_str(pvalue); \
433 pj_list_insert_before(&list, param); \
434 } while (0)
435
436 static pjsip_uri *create_uri5(pj_pool_t *pool)
437 {
438 /* "sip:localhost;pickup=hurry;user=phone;message=I%20am%20sorry"
439 "?Subject=Hello%20There&Server=SIP%20Server"
440 */
441 pjsip_sip_uri *url = pjsip_sip_uri_create(pool, 0);
442
443 pj_strdup2(pool, &url->host, "localhost");
444 pj_strdup2(pool, &url->user_param, "phone");
445
446 //pj_strdup2(pool, &url->other_param, ";pickup=hurry;message=I%20am%20sorry");
447 param_add(url->other_param, "pickup", "hurry");
448 param_add(url->other_param, "message", "I am sorry");
449
450 //pj_strdup2(pool, &url->header_param, "?Subject=Hello%20There&Server=SIP%20Server");
451 param_add(url->header_param, "Subject", "Hello There");
452 param_add(url->header_param, "Server", "SIP Server");
453 return (pjsip_uri*)url;
454
455 }
456
457 static pjsip_uri *create_uri6(pj_pool_t *pool)
458 {
459 /* "sips:localhost" */
460 pjsip_sip_uri *url = pjsip_sip_uri_create(pool, 1);
461
462 pj_strdup2(pool, &url->host, "localhost");
463 return (pjsip_uri*)url;
464 }
465
466 static pjsip_uri *create_uri7(pj_pool_t *pool)
467 {
468 /* "<sip:localhost>" */
469 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
470 pjsip_sip_uri *url;
471
472 url = pjsip_sip_uri_create(pool, 0);
473 name_addr->uri = (pjsip_uri*) url;
474
475 pj_strdup2(pool, &url->host, "localhost");
476 return (pjsip_uri*)name_addr;
477 }
478
479 static pjsip_uri *create_uri8(pj_pool_t *pool)
480 {
481 /* " Power Administrator <sips:localhost>" */
482 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
483 pjsip_sip_uri *url;
484
485 url = pjsip_sip_uri_create(pool, 1);
486 name_addr->uri = (pjsip_uri*) url;
487
488 pj_strdup2(pool, &name_addr->display, "Power Administrator");
489 pj_strdup2(pool, &url->host, "localhost");
490 return (pjsip_uri*)name_addr;
491 }
492
493 static pjsip_uri *create_uri9(pj_pool_t *pool)
494 {
495 /* " \"User\" <sip:user@localhost:5071>" */
496 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
497 pjsip_sip_uri *url;
498
499 url = pjsip_sip_uri_create(pool, 0);
500 name_addr->uri = (pjsip_uri*) url;
501
502 pj_strdup2(pool, &name_addr->display, "User");
503 pj_strdup2(pool, &url->user, "user");
504 pj_strdup2(pool, &url->host, "localhost");
505 url->port = 5071;
506 return (pjsip_uri*)name_addr;
507 }
508
509 static pjsip_uri *create_uri10(pj_pool_t *pool)
510 {
511 /* " \"Strange User\\\"\\\\\\\"\" <sip:localhost>" */
512 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
513 pjsip_sip_uri *url;
514
515 url = pjsip_sip_uri_create(pool, 0);
516 name_addr->uri = (pjsip_uri*) url;
517
518 pj_strdup2(pool, &name_addr->display, "Strange User\\\"\\\\\\\"");
519 pj_strdup2(pool, &url->host, "localhost");
520 return (pjsip_uri*)name_addr;
521 }
522
523 static pjsip_uri *create_uri11(pj_pool_t *pool)
524 {
525 /* " \"Rogue User\\\" <sip:localhost>" */
526 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
527 pjsip_sip_uri *url;
528
529 url = pjsip_sip_uri_create(pool, 0);
530 name_addr->uri = (pjsip_uri*) url;
531
532 pj_strdup2(pool, &name_addr->display, "Rogue User\\");
533 pj_strdup2(pool, &url->host, "localhost");
534 return (pjsip_uri*)name_addr;
535 }
536
537 static pjsip_uri *create_uri12(pj_pool_t *pool)
538 {
539 /* "Strange User\" <sip:localhost>" */
540 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
541 pjsip_sip_uri *url;
542
543 url = pjsip_sip_uri_create(pool, 0);
544 name_addr->uri = (pjsip_uri*) url;
545
546 pj_strdup2(pool, &name_addr->display, "Strange User\"");
547 pj_strdup2(pool, &url->host, "localhost");
548 return (pjsip_uri*)name_addr;
549 }
550
551 static pjsip_uri *create_uri13(pj_pool_t *pool)
552 {
553 /* "sip:localhost;pvalue=\"hello world\"" */
554 pjsip_sip_uri *url;
555 url = pjsip_sip_uri_create(pool, 0);
556 pj_strdup2(pool, &url->host, "localhost");
557 //pj_strdup2(pool, &url->other_param, ";pvalue=\"hello world\"");
558 param_add(url->other_param, "pvalue", "\"hello world\"");
559 return (pjsip_uri*)url;
560 }
561
562 static pjsip_uri *create_uri14(pj_pool_t *pool)
563 {
564 /* "This is -. !% *_+`'~ me <sip:a19A&=+$,;?/%2c:%40a&Zz=+$,@my_proxy09.my-domain.com:9801>" */
565 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
566 pjsip_sip_uri *url;
567
568 url = pjsip_sip_uri_create(pool, 0);
569 name_addr->uri = (pjsip_uri*) url;
570
571 pj_strdup2(pool, &name_addr->display, "This is -. !% *_+`'~ me");
572 pj_strdup2(pool, &url->user, "a19A&=+$,;?/,");
573 pj_strdup2(pool, &url->passwd, "@a&Zz=+$,");
574 #if defined (PJSIP_URI_USE_ORIG_USERPASS) && (PJSIP_URI_USE_ORIG_USERPASS)
575 pj_strdup2(pool, &url->orig_userpass, "a19A&=+$,;?/%2c:%40a&Zz=+$,");
576 #endif
577 pj_strdup2(pool, &url->host, "my_proxy09.MY-domain.com");
578 url->port = 9801;
579 return (pjsip_uri*)name_addr;
580 }
581
582 static pjsip_uri *create_uri15(pj_pool_t *pool)
583 {
584 /* "sip:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.com" */
585 pjsip_sip_uri *url;
586 url = pjsip_sip_uri_create(pool, 0);
587 pj_strdup2(pool, &url->host, ALPHANUM "-_.com");
588 return (pjsip_uri*)url;
589 }
590
591 static pjsip_uri *create_uri16(pj_pool_t *pool)
592 {
593 /* "sip:" USER_CHAR ":" PASS_CHAR "@host" */
594 pjsip_sip_uri *url;
595 url = pjsip_sip_uri_create(pool, 0);
596 pj_strdup2(pool, &url->user, USER_CHAR);
597 pj_strdup2(pool, &url->passwd, PASS_CHAR);
598 pj_strdup2(pool, &url->host, "host");
599 return (pjsip_uri*)url;
600 }
601
602 static pjsip_uri *create_uri17(pj_pool_t *pool)
603 {
604 /* "sip:host;user=ip;" PARAM_CHAR "%21=" PARAM_CHAR "%21;lr;other=1;transport=sctp;other2" */
605 pjsip_sip_uri *url;
606 url = pjsip_sip_uri_create(pool, 0);
607 pj_strdup2(pool, &url->host, "host");
608 pj_strdup2(pool, &url->user_param, "ip");
609 pj_strdup2(pool, &url->transport_param, "sctp");
610 param_add(url->other_param, PARAM_CHAR "!", PARAM_CHAR "!");
611 param_add(url->other_param, "other", "1");
612 param_add(url->other_param, "other2", "");
613 url->lr_param = 1;
614 return (pjsip_uri*)url;
615 }
616
617
618 static pjsip_uri *create_uri25(pj_pool_t *pool)
619 {
620 /* "tel:+1-201-555-0123" */
621 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
622
623 uri->number = pj_str("+1-201-555-0123");
624 return (pjsip_uri*)uri;
625 }
626
627 static pjsip_uri *create_uri26(pj_pool_t *pool)
628 {
629 /* tel:7042;phone-context=example.com */
630 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
631
632 uri->number = pj_str("7042");
633 uri->context = pj_str("example.com");
634 return (pjsip_uri*)uri;
635 }
636
637 static pjsip_uri *create_uri27(pj_pool_t *pool)
638 {
639 /* "tel:863-1234;phone-context=+1-914-555" */
640 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
641
642 uri->number = pj_str("863-1234");
643 uri->context = pj_str("+1-914-555");
644 return (pjsip_uri*)uri;
645 }
646
647 /* "tel:1" */
648 static pjsip_uri *create_uri28(pj_pool_t *pool)
649 {
650 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
651
652 uri->number = pj_str("1");
653 return (pjsip_uri*)uri;
654 }
655
656 /* "tel:(44).1234-*#+Deaf" */
657 static pjsip_uri *create_uri29(pj_pool_t *pool)
658 {
659 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
660
661 uri->number = pj_str("(44).1234-*#+Deaf");
662 return (pjsip_uri*)uri;
663 }
664
665 /* "tel:+1;isub=/:@&$,-_.!~*'()[]/:&$aA1%21+=" */
666 static pjsip_uri *create_uri30(pj_pool_t *pool)
667 {
668 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
669
670 uri->number = pj_str("+1");
671 uri->isub_param = pj_str("/:@&$,-_.!~*'()[]/:&$aA1!+=");
672 return (pjsip_uri*)uri;
673 }
674
675 /* "tel:+1;ext=+123" */
676 static pjsip_uri *create_uri31(pj_pool_t *pool)
677 {
678 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
679
680 uri->number = pj_str("+1");
681 uri->ext_param = pj_str("+123");
682 return (pjsip_uri*)uri;
683 }
684
685 /* "tel:911;phone-context=+1-911" */
686 static pjsip_uri *create_uri32(pj_pool_t *pool)
687 {
688 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
689
690 uri->number = pj_str("911");
691 uri->context = pj_str("+1-911");
692 return (pjsip_uri*)uri;
693 }
694
695 /* "tel:911;phone-context=emergency.example.com" */
696 static pjsip_uri *create_uri33(pj_pool_t *pool)
697 {
698 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
699
700 uri->number = pj_str("911");
701 uri->context = pj_str("EMERGENCY.EXAMPLE.COM");
702 return (pjsip_uri*)uri;
703 }
704
705 /* "tel:911;p1=p1;p2=p2" */
706 static pjsip_uri *create_uri34(pj_pool_t *pool)
707 {
708 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
709 pjsip_param *p;
710
711 uri->number = pj_str("911");
712
713 p = PJ_POOL_ALLOC_T(pool, pjsip_param);
714 p->name = p->value = pj_str("p1");
715 pj_list_insert_before(&uri->other_param, p);
716
717 return (pjsip_uri*)uri;
718 }
719
720 /* "sip:user@[::1];maddr=[::01]" */
721 static pjsip_uri *create_uri35( pj_pool_t *pool )
722 {
723 pjsip_sip_uri *url;
724 url = pjsip_sip_uri_create(pool, 0);
725 url->user = pj_str("user");
726 url->host = pj_str("::1");
727 url->maddr_param = pj_str("[::01]");
728 return (pjsip_uri*)url;
729 }
730
731 /* "sip:[::1];maddr=[::01]" */
732 static pjsip_uri *create_uri36( pj_pool_t *pool )
733 {
734 pjsip_sip_uri *url;
735 url = pjsip_sip_uri_create(pool, 0);
736 url->host = pj_str("::1");
737 url->maddr_param = pj_str("[::01]");
738 return (pjsip_uri*)url;
739
740 }
741
742 /* "\"\xC0\x81\" <sip:localhost>" */
743 static pjsip_uri *create_uri37( pj_pool_t *pool )
744 {
745 pjsip_name_addr *name;
746 pjsip_sip_uri *url;
747
748 name = pjsip_name_addr_create(pool);
749 name->display = pj_str("\xC0\x81");
750
751 url = pjsip_sip_uri_create(pool, 0);
752 url->host = pj_str("localhost");
753
754 name->uri = (pjsip_uri*)url;
755
756 return (pjsip_uri*)name;
757
758 }
759
760 /* "\xC0\x81 <sip:localhost>" */
761 static pjsip_uri *create_uri38( pj_pool_t *pool )
762 {
763 pjsip_name_addr *name;
764 pjsip_sip_uri *url;
765
766 name = pjsip_name_addr_create(pool);
767 name->display = pj_str("\xC0\x81");
768
769 url = pjsip_sip_uri_create(pool, 0);
770 url->host = pj_str("localhost");
771
772 name->uri = (pjsip_uri*)url;
773
774 return (pjsip_uri*)name;
775
776 }
777
778 /* "\"User\\\\\" <sip:localhost>" */
779 static pjsip_uri *create_uri39(pj_pool_t *pool)
780 {
781 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
782 pjsip_sip_uri *url;
783
784 url = pjsip_sip_uri_create(pool, 0);
785 name_addr->uri = (pjsip_uri*) url;
786
787 pj_strdup2(pool, &name_addr->display, "User\\\\");
788 pj_strdup2(pool, &url->host, "localhost");
789 return (pjsip_uri*)name_addr;
790 }
791
792 /* "\"\\\"User\\\"\" <sip:localhost>" */
793 static pjsip_uri *create_uri40(pj_pool_t *pool)
794 {
795 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
796 pjsip_sip_uri *url;
797
798 url = pjsip_sip_uri_create(pool, 0);
799 name_addr->uri = (pjsip_uri*) url;
800
801 pj_strdup2(pool, &name_addr->display, "\\\"User\\\"");
802 pj_strdup2(pool, &url->host, "localhost");
803 return (pjsip_uri*)name_addr;
804 }
805
806 static pjsip_uri *create_dummy(pj_pool_t *pool)
807 {
808 PJ_UNUSED_ARG(pool);
809 return NULL;
810 }
811
812 /*****************************************************************************/
813
814 /*
815 * Test one test entry.
816 */
817 static pj_status_t do_uri_test(pj_pool_t *pool, struct uri_test *entry)
818 {
819 pj_status_t status;
820 int len;
821 char *input;
822 pjsip_uri *parsed_uri, *ref_uri;
823 pj_str_t s1 = {NULL, 0}, s2 = {NULL, 0};
824 pj_timestamp t1, t2;
825
826 if (entry->len == 0)
827 entry->len = pj_ansi_strlen(entry->str);
828
829 #if defined(PJSIP_UNESCAPE_IN_PLACE) && PJSIP_UNESCAPE_IN_PLACE!=0
830 input = pj_pool_alloc(pool, entry->len + 1);
831 pj_memcpy(input, entry->str, entry->len);
832 input[entry->len] = '\0';
833 #else
834 input = entry->str;
835 #endif
836
837 /* Parse URI text. */
838 pj_get_timestamp(&t1);
839 var.parse_len = var.parse_len + entry->len;
840 parsed_uri = pjsip_parse_uri(pool, input, entry->len, 0);
841 if (!parsed_uri) {
842 /* Parsing failed. If the entry says that this is expected, then
843 * return OK.
844 */
845 status = entry->status==ERR_SYNTAX_ERR ? PJ_SUCCESS : -10;
846 if (status != 0) {
847 PJ_LOG(3,(THIS_FILE, " uri parse error!\n"
848 " uri='%s'\n",
849 input));
850 }
851 goto on_return;
852 }
853 pj_get_timestamp(&t2);
854 pj_sub_timestamp(&t2, &t1);
855 pj_add_timestamp(&var.parse_time, &t2);
856
857 /* Create the reference URI. */
858 ref_uri = entry->creator(pool);
859
860 /* Print both URI. */
861 s1.ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
862 s2.ptr = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
863
864 pj_get_timestamp(&t1);
865 len = pjsip_uri_print( PJSIP_URI_IN_OTHER, parsed_uri, s1.ptr, PJSIP_MAX_URL_SIZE);
866 if (len < 1) {
867 status = -20;
868 goto on_return;
869 }
870 s1.ptr[len] = '\0';
871 s1.slen = len;
872
873 var.print_len = var.print_len + len;
874 pj_get_timestamp(&t2);
875 pj_sub_timestamp(&t2, &t1);
876 pj_add_timestamp(&var.print_time, &t2);
877
878 len = pjsip_uri_print( PJSIP_URI_IN_OTHER, ref_uri, s2.ptr, PJSIP_MAX_URL_SIZE);
879 if (len < 1) {
880 status = -30;
881 goto on_return;
882 }
883 s2.ptr[len] = '\0';
884 s2.slen = len;
885
886 /* Full comparison of parsed URI with reference URI. */
887 pj_get_timestamp(&t1);
888 status = pjsip_uri_cmp(PJSIP_URI_IN_OTHER, parsed_uri, ref_uri);
889 if (status != 0) {
890 /* Not equal. See if this is the expected status. */
891 status = entry->status==ERR_NOT_EQUAL ? PJ_SUCCESS : -40;
892 if (status != 0) {
893 PJ_LOG(3,(THIS_FILE, " uri comparison mismatch, status=%d:\n"
894 " uri1='%s'\n"
895 " uri2='%s'",
896 status, s1.ptr, s2.ptr));
897 }
898 goto on_return;
899
900 } else {
901 /* Equal. See if this is the expected status. */
902 status = entry->status==PJ_SUCCESS ? PJ_SUCCESS : -50;
903 if (status != PJ_SUCCESS) {
904 goto on_return;
905 }
906 }
907
908 var.cmp_len = var.cmp_len + len;
909 pj_get_timestamp(&t2);
910 pj_sub_timestamp(&t2, &t1);
911 pj_add_timestamp(&var.cmp_time, &t2);
912
913 /* Compare text. */
914 if (entry->printed) {
915 if (pj_strcmp2(&s1, entry->printed) != 0) {
916 /* Not equal. */
917 PJ_LOG(3,(THIS_FILE, " uri print mismatch:\n"
918 " printed='%s'\n"
919 " expectd='%s'",
920 s1.ptr, entry->printed));
921 status = -60;
922 }
923 } else {
924 if (pj_strcmp(&s1, &s2) != 0) {
925 /* Not equal. */
926 PJ_LOG(3,(THIS_FILE, " uri print mismatch:\n"
927 " uri1='%s'\n"
928 " uri2='%s'",
929 s1.ptr, s2.ptr));
930 status = -70;
931 }
932 }
933
934 on_return:
935 return status;
936 }
937
938
939 static int simple_uri_test(void)
940 {
941 unsigned i;
942 pj_pool_t *pool;
943 pj_status_t status;
944
945 PJ_LOG(3,(THIS_FILE, " simple test"));
946 for (i=0; i<PJ_ARRAY_SIZE(uri_test_array); ++i) {
947 pool = pjsip_endpt_create_pool(endpt, "", POOL_SIZE, POOL_SIZE);
948 status = do_uri_test(pool, &uri_test_array[i]);
949 pjsip_endpt_release_pool(endpt, pool);
950 if (status != PJ_SUCCESS) {
951 PJ_LOG(3,(THIS_FILE, " error %d when testing entry %d",
952 status, i));
953 return status;
954 }
955 }
956
957 return 0;
958 }
959
960 #if INCLUDE_BENCHMARKS
961 static int uri_benchmark(unsigned *p_parse, unsigned *p_print, unsigned *p_cmp)
962 {
963 unsigned i, loop;
964 pj_status_t status = PJ_SUCCESS;
965 pj_timestamp zero;
966 pj_time_val elapsed;
967 pj_highprec_t avg_parse, avg_print, avg_cmp, kbytes;
968
969 pj_bzero(&var, sizeof(var));
970
971 zero.u32.hi = zero.u32.lo = 0;
972
973 var.parse_len = var.print_len = var.cmp_len = 0;
974 var.parse_time.u32.hi = var.parse_time.u32.lo = 0;
975 var.print_time.u32.hi = var.print_time.u32.lo = 0;
976 var.cmp_time.u32.hi = var.cmp_time.u32.lo = 0;
977 for (loop=0; loop<LOOP_COUNT; ++loop) {
978 for (i=0; i<PJ_ARRAY_SIZE(uri_test_array); ++i) {
979 pj_pool_t *pool;
980 pool = pjsip_endpt_create_pool(endpt, "", POOL_SIZE, POOL_SIZE);
981 status = do_uri_test(pool, &uri_test_array[i]);
982 pjsip_endpt_release_pool(endpt, pool);
983 if (status != PJ_SUCCESS) {
984 PJ_LOG(3,(THIS_FILE, " error %d when testing entry %d",
985 status, i));
986 pjsip_endpt_release_pool(endpt, pool);
987 goto on_return;
988 }
989 }
990 }
991
992 kbytes = var.parse_len;
993 pj_highprec_mod(kbytes, 1000000);
994 pj_highprec_div(kbytes, 100000);
995 elapsed = pj_elapsed_time(&zero, &var.parse_time);
996 avg_parse = pj_elapsed_usec(&zero, &var.parse_time);
997 pj_highprec_mul(avg_parse, AVERAGE_URL_LEN);
998 pj_highprec_div(avg_parse, var.parse_len);
999 if (avg_parse == 0)
1000 avg_parse = 1;
1001 avg_parse = 1000000 / avg_parse;
1002
1003 PJ_LOG(3,(THIS_FILE,
1004 " %u.%u MB of urls parsed in %ld.%03lds (avg=%d urls/sec)",
1005 (unsigned)(var.parse_len/1000000), (unsigned)kbytes,
1006 elapsed.sec, elapsed.msec,
1007 (unsigned)avg_parse));
1008
1009 *p_parse = (unsigned)avg_parse;
1010
1011 kbytes = var.print_len;
1012 pj_highprec_mod(kbytes, 1000000);
1013 pj_highprec_div(kbytes, 100000);
1014 elapsed = pj_elapsed_time(&zero, &var.print_time);
1015 avg_print = pj_elapsed_usec(&zero, &var.print_time);
1016 pj_highprec_mul(avg_print, AVERAGE_URL_LEN);
1017 pj_highprec_div(avg_print, var.parse_len);
1018 if (avg_print == 0)
1019 avg_print = 1;
1020 avg_print = 1000000 / avg_print;
1021
1022 PJ_LOG(3,(THIS_FILE,
1023 " %u.%u MB of urls printed in %ld.%03lds (avg=%d urls/sec)",
1024 (unsigned)(var.print_len/1000000), (unsigned)kbytes,
1025 elapsed.sec, elapsed.msec,
1026 (unsigned)avg_print));
1027
1028 *p_print = (unsigned)avg_print;
1029
1030 kbytes = var.cmp_len;
1031 pj_highprec_mod(kbytes, 1000000);
1032 pj_highprec_div(kbytes, 100000);
1033 elapsed = pj_elapsed_time(&zero, &var.cmp_time);
1034 avg_cmp = pj_elapsed_usec(&zero, &var.cmp_time);
1035 if (PJ_HIGHPREC_VALUE_IS_ZERO(avg_cmp) || var.cmp_len == 0) {
1036 avg_cmp = 0;
1037 } else {
1038 pj_highprec_mul(avg_cmp, AVERAGE_URL_LEN);
1039 pj_highprec_div(avg_cmp, var.cmp_len);
1040 avg_cmp = 1000000 / avg_cmp;
1041 }
1042
1043 PJ_LOG(3,(THIS_FILE,
1044 " %u.%u MB of urls compared in %ld.%03lds (avg=%d urls/sec)",
1045 (unsigned)(var.cmp_len/1000000), (unsigned)kbytes,
1046 elapsed.sec, elapsed.msec,
1047 (unsigned)avg_cmp));
1048
1049 *p_cmp = (unsigned)avg_cmp;
1050
1051 on_return:
1052 return status;
1053 }
1054 #endif /* INCLUDE_BENCHMARKS */
1055
1056 /*****************************************************************************/
1057
1058 int uri_test(void)
1059 {
1060 enum { COUNT = 1, DETECT=0, PARSE=1, PRINT=2 };
1061 struct {
1062 unsigned parse;
1063 unsigned print;
1064 unsigned cmp;
1065 } run[COUNT];
1066 unsigned i, max;
1067 pj_ssize_t avg_len;
1068 char desc[200];
1069 pj_status_t status;
1070
1071 status = simple_uri_test();
1072 if (status != PJ_SUCCESS)
1073 return status;
1074
1075 #if INCLUDE_BENCHMARKS
1076 for (i=0; i<COUNT; ++i) {
1077 PJ_LOG(3,(THIS_FILE, " benchmarking (%d of %d)...", i+1, COUNT));
1078 status = uri_benchmark(&run[i].parse, &run[i].print, &run[i].cmp);
1079 if (status != PJ_SUCCESS)
1080 return status;
1081 }
1082
1083 /* Calculate average URI length */
1084 for (i=0, avg_len=0; i<PJ_ARRAY_SIZE(uri_test_array); ++i) {
1085 avg_len += uri_test_array[i].len;
1086 }
1087 avg_len /= PJ_ARRAY_SIZE(uri_test_array);
1088
1089
1090 /*
1091 * Print maximum parse/sec
1092 */
1093 for (i=0, max=0; i<COUNT; ++i)
1094 if (run[i].parse > max) max = run[i].parse;
1095
1096 PJ_LOG(3,("", " Maximum URI parse/sec=%u", max));
1097
1098 pj_ansi_snprintf(desc, sizeof(desc),
1099 "Number of SIP/TEL URIs that can be <B>parsed</B> with "
1100 "<tt>pjsip_parse_uri()</tt> per second "
1101 "(tested with %d URI set, with average length of "
1102 "%d chars)",
1103 (int)PJ_ARRAY_SIZE(uri_test_array), (int)avg_len);
1104
1105 report_ival("uri-parse-per-sec", max, "URI/sec", desc);
1106
1107 /* URI parsing bandwidth */
1108 report_ival("uri-parse-bandwidth-mb", (int)avg_len*max/1000000, "MB/sec",
1109 "URI parsing bandwidth in megabytes (number of megabytes "
1110 "worth of URI that can be parsed per second)");
1111
1112
1113 /* Print maximum print/sec */
1114 for (i=0, max=0; i<COUNT; ++i)
1115 if (run[i].print > max) max = run[i].print;
1116
1117 PJ_LOG(3,("", " Maximum URI print/sec=%u", max));
1118
1119 pj_ansi_snprintf(desc, sizeof(desc),
1120 "Number of SIP/TEL URIs that can be <B>printed</B> with "
1121 "<tt>pjsip_uri_print()</tt> per second "
1122 "(tested with %d URI set, with average length of "
1123 "%d chars)",
1124 (int)PJ_ARRAY_SIZE(uri_test_array), (int)avg_len);
1125
1126 report_ival("uri-print-per-sec", max, "URI/sec", desc);
1127
1128 /* Print maximum detect/sec */
1129 for (i=0, max=0; i<COUNT; ++i)
1130 if (run[i].cmp > max) max = run[i].cmp;
1131
1132 PJ_LOG(3,("", " Maximum URI comparison/sec=%u", max));
1133
1134 pj_ansi_snprintf(desc, sizeof(desc),
1135 "Number of SIP/TEL URIs that can be <B>compared</B> with "
1136 "<tt>pjsip_uri_cmp()</tt> per second "
1137 "(tested with %d URI set, with average length of "
1138 "%d chars)",
1139 (int)PJ_ARRAY_SIZE(uri_test_array), (int)avg_len);
1140
1141 report_ival("uri-cmp-per-sec", max, "URI/sec", desc);
1142
1143 #endif /* INCLUDE_BENCHMARKS */
1144
1145 return PJ_SUCCESS;
1146 }
1147