blob: de9604bf912272d1e6a8627ce52a38ac8e5f878e [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
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_core.h>
21#include <pjlib.h>
22
23
24#define ALPHANUM "abcdefghijklmnopqrstuvwxyz" \
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
26 "0123456789"
27#define MARK "-_.!~*'()"
28#define USER_CHAR ALPHANUM MARK "&=+$,;?/"
29#define PASS_CHAR ALPHANUM MARK "&=+$,"
30#define PARAM_CHAR ALPHANUM MARK "[]/:&+$"
31
Benny Prijono5b917442005-11-21 17:07:18 +000032#define POOL_SIZE 8000
33#define LOOP_COUNT 10000
Benny Prijono5dcb38d2005-11-21 01:55:47 +000034#define AVERAGE_URL_LEN 80
35#define THREAD_COUNT 4
36
Benny Prijono5b917442005-11-21 17:07:18 +000037static pj_highprec_t parse_len, print_len, cmp_len;
38static pj_timestamp parse_time, print_time, cmp_time;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000039
40
41/* URI creator functions. */
42static pjsip_uri *create_uri0( pj_pool_t *pool );
43static pjsip_uri *create_uri1( pj_pool_t *pool );
44static pjsip_uri *create_uri2( pj_pool_t *pool );
45static pjsip_uri *create_uri3( pj_pool_t *pool );
46static pjsip_uri *create_uri4( pj_pool_t *pool );
47static pjsip_uri *create_uri5( pj_pool_t *pool );
48static pjsip_uri *create_uri6( pj_pool_t *pool );
49static pjsip_uri *create_uri7( pj_pool_t *pool );
50static pjsip_uri *create_uri8( pj_pool_t *pool );
51static pjsip_uri *create_uri9( pj_pool_t *pool );
52static pjsip_uri *create_uri10( pj_pool_t *pool );
53static pjsip_uri *create_uri11( pj_pool_t *pool );
54static pjsip_uri *create_uri12( pj_pool_t *pool );
55static pjsip_uri *create_uri13( pj_pool_t *pool );
56static pjsip_uri *create_uri14( pj_pool_t *pool );
57static pjsip_uri *create_uri15( pj_pool_t *pool );
58static pjsip_uri *create_uri16( pj_pool_t *pool );
59static pjsip_uri *create_uri17( pj_pool_t *pool );
Benny Prijono82458b12005-11-23 20:56:30 +000060static pjsip_uri *create_uri25( pj_pool_t *pool );
61static pjsip_uri *create_uri26( pj_pool_t *pool );
62static pjsip_uri *create_uri27( pj_pool_t *pool );
63static pjsip_uri *create_uri28( pj_pool_t *pool );
64static pjsip_uri *create_uri29( pj_pool_t *pool );
65static pjsip_uri *create_uri30( pj_pool_t *pool );
66static pjsip_uri *create_uri31( pj_pool_t *pool );
67static pjsip_uri *create_uri32( pj_pool_t *pool );
68static pjsip_uri *create_uri33( pj_pool_t *pool );
69static pjsip_uri *create_uri34( pj_pool_t *pool );
70static pjsip_uri *create_uri35( pj_pool_t *pool );
Benny Prijono5dcb38d2005-11-21 01:55:47 +000071static pjsip_uri *create_dummy( pj_pool_t *pool );
72
73#define ERR_NOT_EQUAL -1001
74#define ERR_SYNTAX_ERR -1002
75
76struct uri_test
77{
78 pj_status_t status;
79 char str[PJSIP_MAX_URL_SIZE];
Benny Prijono82458b12005-11-23 20:56:30 +000080 pjsip_uri *(*creator)(pj_pool_t *pool);
81 const char *printed;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000082 pj_size_t len;
83} uri_test_array[] =
84{
85 {
86 PJ_SUCCESS,
87 "sip:localhost",
88 &create_uri0
89 },
90 {
91 PJ_SUCCESS,
92 "sip:user@localhost",
93 &create_uri1
94 },
95 {
96 PJ_SUCCESS,
97 "sip:user:password@localhost:5060",
Benny Prijono5b917442005-11-21 17:07:18 +000098 &create_uri2, },
Benny Prijono5dcb38d2005-11-21 01:55:47 +000099 {
100 /* Port is specified should not match unspecified port. */
101 ERR_NOT_EQUAL,
102 "sip:localhost:5060",
103 &create_uri3
104 },
105 {
106 /* All recognized parameters. */
107 PJ_SUCCESS,
108 "sip:localhost;transport=tcp;user=ip;ttl=255;lr;maddr=127.0.0.1;method=ACK",
109 &create_uri4
110 },
111 {
112 /* Params mixed with other params and header params. */
113 PJ_SUCCESS,
114 "sip:localhost;pickup=hurry;user=phone;message=I%20am%20sorry"
115 "?Subject=Hello%20There&Server=SIP%20Server",
116 &create_uri5
117 },
118 {
119 /* SIPS. */
120 PJ_SUCCESS,
121 "sips:localhost",
122 &create_uri6,
123 },
124 {
125 /* Name address */
126 PJ_SUCCESS,
127 "<sip:localhost>",
128 &create_uri7
129 },
130 {
131 /* Name address with display name and SIPS scheme with some redundant
132 * whitespaced.
133 */
134 PJ_SUCCESS,
135 " Power Administrator <sips:localhost>",
136 &create_uri8
137 },
138 {
139 /* Name address. */
140 PJ_SUCCESS,
141 " \"User\" <sip:user@localhost:5071>",
142 &create_uri9
143 },
144 {
145 /* Escaped sequence in display name (display=Strange User\"\\\"). */
146 PJ_SUCCESS,
147 " \"Strange User\\\"\\\\\\\"\" <sip:localhost>",
148 &create_uri10,
149 },
150 {
151 /* Errorneous escaping in display name. */
152 ERR_SYNTAX_ERR,
153 " \"Rogue User\\\" <sip:localhost>",
154 &create_uri11,
155 },
156 {
157 /* Dangling quote in display name, but that should be OK. */
158 PJ_SUCCESS,
159 "Strange User\" <sip:localhost>",
160 &create_uri12,
161 },
162 {
163 /* Special characters in parameter value must be quoted. */
164 PJ_SUCCESS,
165 "sip:localhost;pvalue=\"hello world\"",
166 &create_uri13,
167 },
168 {
169 /* Excercise strange character sets allowed in display, user, password,
170 * host, and port.
171 */
172 PJ_SUCCESS,
173 "This is -. !% *_+`'~ me <sip:a19A&=+$,;?/%2c:%40a&Zz=+$,@"
174 "my_proxy09.MY-domain.com:9801>",
175 &create_uri14,
176 },
177 {
178 /* Another excercise to the allowed character sets to the hostname. */
179 PJ_SUCCESS,
180 "sip:" ALPHANUM "-_.com",
181 &create_uri15,
182 },
183 {
184 /* Another excercise to the allowed character sets to the username
185 * and password.
186 */
187 PJ_SUCCESS,
188 "sip:" USER_CHAR ":" PASS_CHAR "@host",
189 &create_uri16,
190 },
191 {
192 /* Excercise to the pname and pvalue, and mixup of other-param
193 * between 'recognized' params.
194 */
195 PJ_SUCCESS,
196 "sip:host;user=ip;" PARAM_CHAR "%21=" PARAM_CHAR "%21"
197 ";lr;other=1;transport=sctp;other2",
198 &create_uri17,
199 },
200 {
201 /* 18: This should trigger syntax error. */
202 ERR_SYNTAX_ERR,
203 "sip:",
204 &create_dummy,
205 },
206 {
207 /* 19: Syntax error: whitespace after scheme. */
208 ERR_SYNTAX_ERR,
209 "sip :host",
210 &create_dummy,
211 },
212 {
213 /* 20: Syntax error: whitespace before hostname. */
214 ERR_SYNTAX_ERR,
215 "sip: host",
216 &create_dummy,
217 },
218 {
219 /* 21: Syntax error: invalid port. */
220 ERR_SYNTAX_ERR,
221 "sip:user:password",
222 &create_dummy,
223 },
224 {
225 /* 22: Syntax error: no host. */
226 ERR_SYNTAX_ERR,
227 "sip:user@",
228 &create_dummy,
229 },
230 {
231 /* 23: Syntax error: no user/host. */
232 ERR_SYNTAX_ERR,
233 "sip:@",
234 &create_dummy,
235 },
236 {
237 /* 24: Syntax error: empty string. */
238 ERR_SYNTAX_ERR,
239 "",
240 &create_dummy,
Benny Prijono82458b12005-11-23 20:56:30 +0000241 },
242 {
243 /* 25: Simple tel: URI with global context */
244 PJ_SUCCESS,
245 "tel:+1-201-555-0123",
246 &create_uri25,
247 "tel:+1-201-555-0123"
248 },
249 {
250 /* 26: Simple tel: URI with local context */
251 PJ_SUCCESS,
252 "tel:7042;phone-context=example.com",
253 &create_uri26,
254 "tel:7042;phone-context=example.com"
255 },
256 {
257 /* 27: Simple tel: URI with local context */
258 PJ_SUCCESS,
259 "tel:863-1234;phone-context=+1-914-555",
260 &create_uri27,
261 "tel:863-1234;phone-context=+1-914-555"
262 },
263 {
264 /* 28: Comparison between local and global number */
265 ERR_NOT_EQUAL,
266 "tel:+1",
267 &create_uri28,
268 "tel:+1"
269 },
270 {
271 /* 29: tel: with some visual chars and spaces */
272 PJ_SUCCESS,
273 "tel:(44).1234-*#+Deaf",
274 &create_uri29,
275 "tel:(44).1234-*#+Deaf"
276 },
277 {
278 /* 30: isub parameters */
279 PJ_SUCCESS,
280 "tel:+1;isub=/:@&$,-_.!~*'()[]/:&$aA1%21+=",
281 &create_uri30,
282 "tel:+1;isub=/:@&$,-_.!~*'()[]/:&$aA1%21+%3d"
283 },
284 {
285 /* 31: extension number parsing and encoding */
286 PJ_SUCCESS,
287 "tel:+1;ext=+123",
288 &create_uri31,
289 "tel:+1;ext=%2b123"
290 },
291 {
292 /* 32: context parameter parsing and encoding */
293 PJ_SUCCESS,
294 "tel:911;phone-context=+1-911",
295 &create_uri32,
296 "tel:911;phone-context=+1-911"
297 },
298 {
299 /* 33: case-insensitive comparison */
300 PJ_SUCCESS,
301 "tel:911;phone-context=emergency.example.com",
302 &create_uri33,
303 "tel:911;phone-context=emergency.example.com"
304 },
305 {
306 /* 34: parameter only appears in one URL */
307 ERR_NOT_EQUAL,
308 "tel:911;p1=p1;p2=p2",
309 &create_uri34,
310 "tel:911;p1=p1;p2=p2"
311 },
312
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000313};
314
315static pjsip_uri *create_uri0(pj_pool_t *pool)
316{
317 /* "sip:localhost" */
318 pjsip_url *url = pjsip_url_create(pool, 0);
319
320 pj_strdup2(pool, &url->host, "localhost");
321 return (pjsip_uri*)url;
322}
323
324static pjsip_uri *create_uri1(pj_pool_t *pool)
325{
326 /* "sip:user@localhost" */
327 pjsip_url *url = pjsip_url_create(pool, 0);
328
329 pj_strdup2( pool, &url->user, "user");
330 pj_strdup2( pool, &url->host, "localhost");
331
332 return (pjsip_uri*) url;
333}
334
335static pjsip_uri *create_uri2(pj_pool_t *pool)
336{
337 /* "sip:user:password@localhost:5060" */
338 pjsip_url *url = pjsip_url_create(pool, 0);
339
340 pj_strdup2( pool, &url->user, "user");
341 pj_strdup2( pool, &url->passwd, "password");
342 pj_strdup2( pool, &url->host, "localhost");
343 url->port = 5060;
344
345 return (pjsip_uri*) url;
346}
347
348static pjsip_uri *create_uri3(pj_pool_t *pool)
349{
350 /* Like: "sip:localhost:5060", but without the port. */
351 pjsip_url *url = pjsip_url_create(pool, 0);
352
353 pj_strdup2(pool, &url->host, "localhost");
354 return (pjsip_uri*)url;
355}
356
357static pjsip_uri *create_uri4(pj_pool_t *pool)
358{
359 /* "sip:localhost;transport=tcp;user=ip;ttl=255;lr;maddr=127.0.0.1;method=ACK" */
360 pjsip_url *url = pjsip_url_create(pool, 0);
361
362 pj_strdup2(pool, &url->host, "localhost");
363 pj_strdup2(pool, &url->transport_param, "tcp");
364 pj_strdup2(pool, &url->user_param, "ip");
365 url->ttl_param = 255;
366 url->lr_param = 1;
367 pj_strdup2(pool, &url->maddr_param, "127.0.0.1");
368 pj_strdup2(pool, &url->method_param, "ACK");
369
370 return (pjsip_uri*)url;
371}
372
373#define param_add(list,pname,pvalue) \
374 do { \
375 pjsip_param *param; \
376 param=pj_pool_alloc(pool, sizeof(pjsip_param)); \
377 param->name = pj_str(pname); \
378 param->value = pj_str(pvalue); \
379 pj_list_insert_before(&list, param); \
380 } while (0)
381
382static pjsip_uri *create_uri5(pj_pool_t *pool)
383{
384 /* "sip:localhost;pickup=hurry;user=phone;message=I%20am%20sorry"
385 "?Subject=Hello%20There&Server=SIP%20Server"
386 */
387 pjsip_url *url = pjsip_url_create(pool, 0);
388
389 pj_strdup2(pool, &url->host, "localhost");
390 pj_strdup2(pool, &url->user_param, "phone");
391
392 //pj_strdup2(pool, &url->other_param, ";pickup=hurry;message=I%20am%20sorry");
393 param_add(url->other_param, "pickup", "hurry");
394 param_add(url->other_param, "message", "I am sorry");
395
396 //pj_strdup2(pool, &url->header_param, "?Subject=Hello%20There&Server=SIP%20Server");
397 param_add(url->header_param, "Subject", "Hello There");
398 param_add(url->header_param, "Server", "SIP Server");
399 return (pjsip_uri*)url;
400
401}
402
403static pjsip_uri *create_uri6(pj_pool_t *pool)
404{
405 /* "sips:localhost" */
406 pjsip_url *url = pjsip_url_create(pool, 1);
407
408 pj_strdup2(pool, &url->host, "localhost");
409 return (pjsip_uri*)url;
410}
411
412static pjsip_uri *create_uri7(pj_pool_t *pool)
413{
414 /* "<sip:localhost>" */
415 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
416 pjsip_url *url;
417
418 url = pjsip_url_create(pool, 0);
419 name_addr->uri = (pjsip_uri*) url;
420
421 pj_strdup2(pool, &url->host, "localhost");
422 return (pjsip_uri*)name_addr;
423}
424
425static pjsip_uri *create_uri8(pj_pool_t *pool)
426{
427 /* " Power Administrator <sips:localhost>" */
428 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
429 pjsip_url *url;
430
431 url = pjsip_url_create(pool, 1);
432 name_addr->uri = (pjsip_uri*) url;
433
434 pj_strdup2(pool, &name_addr->display, "Power Administrator");
435 pj_strdup2(pool, &url->host, "localhost");
436 return (pjsip_uri*)name_addr;
437}
438
439static pjsip_uri *create_uri9(pj_pool_t *pool)
440{
441 /* " \"User\" <sip:user@localhost:5071>" */
442 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
443 pjsip_url *url;
444
445 url = pjsip_url_create(pool, 0);
446 name_addr->uri = (pjsip_uri*) url;
447
448 pj_strdup2(pool, &name_addr->display, "User");
449 pj_strdup2(pool, &url->user, "user");
450 pj_strdup2(pool, &url->host, "localhost");
451 url->port = 5071;
452 return (pjsip_uri*)name_addr;
453}
454
455static pjsip_uri *create_uri10(pj_pool_t *pool)
456{
457 /* " \"Strange User\\\"\\\\\\\"\" <sip:localhost>" */
458 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
459 pjsip_url *url;
460
461 url = pjsip_url_create(pool, 0);
462 name_addr->uri = (pjsip_uri*) url;
463
464 pj_strdup2(pool, &name_addr->display, "Strange User\\\"\\\\\\\"");
465 pj_strdup2(pool, &url->host, "localhost");
466 return (pjsip_uri*)name_addr;
467}
468
469static pjsip_uri *create_uri11(pj_pool_t *pool)
470{
471 /* " \"Rogue User\\\" <sip:localhost>" */
472 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
473 pjsip_url *url;
474
475 url = pjsip_url_create(pool, 0);
476 name_addr->uri = (pjsip_uri*) url;
477
478 pj_strdup2(pool, &name_addr->display, "Rogue User\\");
479 pj_strdup2(pool, &url->host, "localhost");
480 return (pjsip_uri*)name_addr;
481}
482
483static pjsip_uri *create_uri12(pj_pool_t *pool)
484{
485 /* "Strange User\" <sip:localhost>" */
486 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
487 pjsip_url *url;
488
489 url = pjsip_url_create(pool, 0);
490 name_addr->uri = (pjsip_uri*) url;
491
492 pj_strdup2(pool, &name_addr->display, "Strange User\"");
493 pj_strdup2(pool, &url->host, "localhost");
494 return (pjsip_uri*)name_addr;
495}
496
497static pjsip_uri *create_uri13(pj_pool_t *pool)
498{
499 /* "sip:localhost;pvalue=\"hello world\"" */
500 pjsip_url *url;
501 url = pjsip_url_create(pool, 0);
502 pj_strdup2(pool, &url->host, "localhost");
503 //pj_strdup2(pool, &url->other_param, ";pvalue=\"hello world\"");
504 param_add(url->other_param, "pvalue", "hello world");
505 return (pjsip_uri*)url;
506}
507
508static pjsip_uri *create_uri14(pj_pool_t *pool)
509{
510 /* "This is -. !% *_+`'~ me <sip:a19A&=+$,;?/%2c:%40a&Zz=+$,@my_proxy09.my-domain.com:9801>" */
511 pjsip_name_addr *name_addr = pjsip_name_addr_create(pool);
512 pjsip_url *url;
513
514 url = pjsip_url_create(pool, 0);
515 name_addr->uri = (pjsip_uri*) url;
516
517 pj_strdup2(pool, &name_addr->display, "This is -. !% *_+`'~ me");
518 pj_strdup2(pool, &url->user, "a19A&=+$,;?/,");
519 pj_strdup2(pool, &url->passwd, "@a&Zz=+$,");
520 pj_strdup2(pool, &url->host, "my_proxy09.MY-domain.com");
521 url->port = 9801;
522 return (pjsip_uri*)name_addr;
523}
524
525static pjsip_uri *create_uri15(pj_pool_t *pool)
526{
527 /* "sip:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.com" */
528 pjsip_url *url;
529 url = pjsip_url_create(pool, 0);
530 pj_strdup2(pool, &url->host, ALPHANUM "-_.com");
531 return (pjsip_uri*)url;
532}
533
534static pjsip_uri *create_uri16(pj_pool_t *pool)
535{
536 /* "sip:" USER_CHAR ":" PASS_CHAR "@host" */
537 pjsip_url *url;
538 url = pjsip_url_create(pool, 0);
539 pj_strdup2(pool, &url->user, USER_CHAR);
540 pj_strdup2(pool, &url->passwd, PASS_CHAR);
541 pj_strdup2(pool, &url->host, "host");
542 return (pjsip_uri*)url;
543}
544
545static pjsip_uri *create_uri17(pj_pool_t *pool)
546{
547 /* "sip:host;user=ip;" PARAM_CHAR "%21=" PARAM_CHAR "%21;lr;other=1;transport=sctp;other2" */
548 pjsip_url *url;
549 url = pjsip_url_create(pool, 0);
550 pj_strdup2(pool, &url->host, "host");
551 pj_strdup2(pool, &url->user_param, "ip");
552 pj_strdup2(pool, &url->transport_param, "sctp");
553 param_add(url->other_param, PARAM_CHAR "!", PARAM_CHAR "!");
554 param_add(url->other_param, "other", "1");
555 param_add(url->other_param, "other2", "");
556 url->lr_param = 1;
557 return (pjsip_uri*)url;
558}
559
Benny Prijono82458b12005-11-23 20:56:30 +0000560
561static pjsip_uri *create_uri25(pj_pool_t *pool)
562{
563 /* "tel:+1-201-555-0123" */
564 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
565
566 uri->number = pj_str("+1-201-555-0123");
567 return (pjsip_uri*)uri;
568}
569
570static pjsip_uri *create_uri26(pj_pool_t *pool)
571{
572 /* tel:7042;phone-context=example.com */
573 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
574
575 uri->number = pj_str("7042");
576 uri->context = pj_str("example.com");
577 return (pjsip_uri*)uri;
578}
579
580static pjsip_uri *create_uri27(pj_pool_t *pool)
581{
582 /* "tel:863-1234;phone-context=+1-914-555" */
583 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
584
585 uri->number = pj_str("863-1234");
586 uri->context = pj_str("+1-914-555");
587 return (pjsip_uri*)uri;
588}
589
590/* "tel:1" */
591static pjsip_uri *create_uri28(pj_pool_t *pool)
592{
593 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
594
595 uri->number = pj_str("1");
596 return (pjsip_uri*)uri;
597}
598
599/* "tel:(44).1234-*#+Deaf" */
600static pjsip_uri *create_uri29(pj_pool_t *pool)
601{
602 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
603
604 uri->number = pj_str("(44).1234-*#+Deaf");
605 return (pjsip_uri*)uri;
606}
607
608/* "tel:+1;isub=/:@&$,-_.!~*'()[]/:&$aA1%21+=" */
609static pjsip_uri *create_uri30(pj_pool_t *pool)
610{
611 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
612
613 uri->number = pj_str("+1");
614 uri->isub_param = pj_str("/:@&$,-_.!~*'()[]/:&$aA1%21+=");
615 return (pjsip_uri*)uri;
616}
617
618/* "tel:+1;ext=+123" */
619static pjsip_uri *create_uri31(pj_pool_t *pool)
620{
621 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
622
623 uri->number = pj_str("+1");
624 uri->ext_param = pj_str("+123");
625 return (pjsip_uri*)uri;
626}
627
628/* "tel:911;phone-context=+1-911" */
629static pjsip_uri *create_uri32(pj_pool_t *pool)
630{
631 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
632
633 uri->number = pj_str("911");
634 uri->context = pj_str("+1-911");
635 return (pjsip_uri*)uri;
636}
637
638/* "tel:911;phone-context=emergency.example.com" */
639static pjsip_uri *create_uri33(pj_pool_t *pool)
640{
641 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
642
643 uri->number = pj_str("911");
644 uri->context = pj_str("EMERGENCY.EXAMPLE.COM");
645 return (pjsip_uri*)uri;
646}
647
648/* "tel:911;p1=p1;p2=p2" */
649static pjsip_uri *create_uri34(pj_pool_t *pool)
650{
651 pjsip_tel_uri *uri = pjsip_tel_uri_create(pool);
652 pjsip_param *p;
653
654 uri->number = pj_str("911");
655
656 p = pj_pool_alloc(pool, sizeof(*p));
657 p->name = p->value = pj_str("p1");
658 pj_list_insert_before(&uri->other_param, p);
659
660 return (pjsip_uri*)uri;
661}
662
663
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000664static pjsip_uri *create_dummy(pj_pool_t *pool)
665{
666 PJ_UNUSED_ARG(pool);
667 return NULL;
668}
669
670/*****************************************************************************/
671
672/*
673 * Test one test entry.
674 */
675static pj_status_t do_uri_test(pj_pool_t *pool, struct uri_test *entry)
676{
677 pj_status_t status;
678 int len;
679 pjsip_uri *parsed_uri, *ref_uri;
680 pj_str_t s1 = {NULL, 0}, s2 = {NULL, 0};
681 pj_timestamp t1, t2;
682
683 entry->len = pj_native_strlen(entry->str);
684
685 /* Parse URI text. */
686 pj_get_timestamp(&t1);
Benny Prijono5b917442005-11-21 17:07:18 +0000687 parse_len = parse_len + entry->len;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000688 parsed_uri = pjsip_parse_uri(pool, entry->str, entry->len, 0);
689 if (!parsed_uri) {
690 /* Parsing failed. If the entry says that this is expected, then
691 * return OK.
692 */
693 status = entry->status==ERR_SYNTAX_ERR ? PJ_SUCCESS : -10;
694 if (status != 0) {
695 PJ_LOG(3,("", " uri parse error!\n"
696 " uri='%s'\n",
697 entry->str));
698 }
699 goto on_return;
700 }
701 pj_get_timestamp(&t2);
702 pj_sub_timestamp(&t2, &t1);
703 pj_add_timestamp(&parse_time, &t2);
704
705 /* Create the reference URI. */
706 ref_uri = entry->creator(pool);
707
708 /* Print both URI. */
709 s1.ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
710 s2.ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
711
712 pj_get_timestamp(&t1);
713 len = pjsip_uri_print( PJSIP_URI_IN_OTHER, parsed_uri, s1.ptr, PJSIP_MAX_URL_SIZE);
714 if (len < 1) {
715 status = -20;
716 goto on_return;
717 }
718 s1.ptr[len] = '\0';
719 s1.slen = len;
720
Benny Prijono5b917442005-11-21 17:07:18 +0000721 print_len = print_len + len;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000722 pj_get_timestamp(&t2);
723 pj_sub_timestamp(&t2, &t1);
724 pj_add_timestamp(&print_time, &t2);
725
726 len = pjsip_uri_print( PJSIP_URI_IN_OTHER, ref_uri, s2.ptr, PJSIP_MAX_URL_SIZE);
727 if (len < 1) {
728 status = -30;
729 goto on_return;
730 }
731 s2.ptr[len] = '\0';
732 s2.slen = len;
733
734 /* Full comparison of parsed URI with reference URI. */
Benny Prijono5b917442005-11-21 17:07:18 +0000735 pj_get_timestamp(&t1);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000736 status = pjsip_uri_cmp(PJSIP_URI_IN_OTHER, parsed_uri, ref_uri);
737 if (status != 0) {
738 /* Not equal. See if this is the expected status. */
739 status = entry->status==ERR_NOT_EQUAL ? PJ_SUCCESS : -40;
740 if (status != 0) {
741 PJ_LOG(3,("", " uri comparison mismatch, status=%d:\n"
742 " uri1='%s'\n"
743 " uri2='%s'",
744 status, s1.ptr, s2.ptr));
745 }
746 goto on_return;
747
748 } else {
749 /* Equal. See if this is the expected status. */
750 status = entry->status==PJ_SUCCESS ? PJ_SUCCESS : -50;
751 if (status != PJ_SUCCESS) {
752 goto on_return;
753 }
754 }
755
Benny Prijono5b917442005-11-21 17:07:18 +0000756 cmp_len = cmp_len + len;
757 pj_get_timestamp(&t2);
758 pj_sub_timestamp(&t2, &t1);
759 pj_add_timestamp(&cmp_time, &t2);
760
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000761 /* Compare text. */
Benny Prijono82458b12005-11-23 20:56:30 +0000762 if (entry->printed) {
763 if (pj_strcmp2(&s1, entry->printed) != 0) {
764 /* Not equal. */
765 PJ_LOG(3,("", " uri print mismatch:\n"
766 " printed='%s'\n"
767 " expectd='%s'",
768 s1.ptr, entry->printed));
769 status = -60;
770 }
771 } else {
772 if (pj_strcmp(&s1, &s2) != 0) {
773 /* Not equal. */
774 PJ_LOG(3,("", " uri print mismatch:\n"
775 " uri1='%s'\n"
776 " uri2='%s'",
777 s1.ptr, s2.ptr));
778 status = -70;
779 }
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000780 }
781
782on_return:
783 return status;
784}
785
Benny Prijono0ca04b62005-12-30 23:50:15 +0000786int uri_test()
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000787{
788 unsigned i, loop;
789 pj_pool_t *pool;
790 pj_status_t status;
791 pj_timestamp zero;
Benny Prijono5b917442005-11-21 17:07:18 +0000792 pj_time_val elapsed;
793 pj_highprec_t avg_parse, avg_print, avg_cmp, kbytes;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000794
795 zero.u32.hi = zero.u32.lo = 0;
796
797 PJ_LOG(3,("", " simple test"));
798 pool = pjsip_endpt_create_pool(endpt, "", POOL_SIZE, POOL_SIZE);
799 for (i=0; i<PJ_ARRAY_SIZE(uri_test_array); ++i) {
800 status = do_uri_test(pool, &uri_test_array[i]);
801 if (status != PJ_SUCCESS) {
802 PJ_LOG(3,("uri_test", " error %d when testing entry %d",
803 status, i));
804 goto on_return;
805 }
806 }
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000807 pjsip_endpt_release_pool(endpt, pool);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000808
809 PJ_LOG(3,("", " benchmarking..."));
Benny Prijono5b917442005-11-21 17:07:18 +0000810 parse_len = print_len = cmp_len = 0;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000811 parse_time.u32.hi = parse_time.u32.lo = 0;
812 print_time.u32.hi = print_time.u32.lo = 0;
Benny Prijono5b917442005-11-21 17:07:18 +0000813 cmp_time.u32.hi = cmp_time.u32.lo = 0;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000814 for (loop=0; loop<LOOP_COUNT; ++loop) {
Benny Prijono5b917442005-11-21 17:07:18 +0000815 pool = pjsip_endpt_create_pool(endpt, "", POOL_SIZE, POOL_SIZE);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000816 for (i=0; i<PJ_ARRAY_SIZE(uri_test_array); ++i) {
817 status = do_uri_test(pool, &uri_test_array[i]);
818 if (status != PJ_SUCCESS) {
819 PJ_LOG(3,("uri_test", " error %d when testing entry %d",
820 status, i));
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000821 pjsip_endpt_release_pool(endpt, pool);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000822 goto on_return;
823 }
824 }
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000825 pjsip_endpt_release_pool(endpt, pool);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000826 }
827
Benny Prijono5b917442005-11-21 17:07:18 +0000828 kbytes = parse_len;
829 pj_highprec_mod(kbytes, 1000000);
830 pj_highprec_div(kbytes, 100000);
831 elapsed = pj_elapsed_time(&zero, &parse_time);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000832 avg_parse = pj_elapsed_usec(&zero, &parse_time);
833 pj_highprec_mul(avg_parse, AVERAGE_URL_LEN);
834 pj_highprec_div(avg_parse, parse_len);
835 avg_parse = 1000000 / avg_parse;
836
Benny Prijono5b917442005-11-21 17:07:18 +0000837 PJ_LOG(3,("", " %u.%u MB of urls parsed in %d.%03ds (avg=%d urls/sec)",
838 (unsigned)(parse_len/1000000), (unsigned)kbytes,
839 elapsed.sec, elapsed.msec,
840 (unsigned)avg_parse));
841
842 kbytes = print_len;
843 pj_highprec_mod(kbytes, 1000000);
844 pj_highprec_div(kbytes, 100000);
845 elapsed = pj_elapsed_time(&zero, &print_time);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000846 avg_print = pj_elapsed_usec(&zero, &print_time);
847 pj_highprec_mul(avg_print, AVERAGE_URL_LEN);
848 pj_highprec_div(avg_print, parse_len);
849 avg_print = 1000000 / avg_print;
850
Benny Prijono5b917442005-11-21 17:07:18 +0000851 PJ_LOG(3,("", " %u.%u MB of urls printed in %d.%03ds (avg=%d urls/sec)",
852 (unsigned)(print_len/1000000), (unsigned)kbytes,
853 elapsed.sec, elapsed.msec,
854 (unsigned)avg_print));
855
856 kbytes = cmp_len;
857 pj_highprec_mod(kbytes, 1000000);
858 pj_highprec_div(kbytes, 100000);
859 elapsed = pj_elapsed_time(&zero, &cmp_time);
860 avg_cmp = pj_elapsed_usec(&zero, &cmp_time);
861 pj_highprec_mul(avg_cmp, AVERAGE_URL_LEN);
862 pj_highprec_div(avg_cmp, cmp_len);
863 avg_cmp = 1000000 / avg_cmp;
864
865 PJ_LOG(3,("", " %u.%u MB of urls compared in %d.%03ds (avg=%d urls/sec)",
866 (unsigned)(cmp_len/1000000), (unsigned)kbytes,
867 elapsed.sec, elapsed.msec,
868 (unsigned)avg_cmp));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000869
870 PJ_LOG(3,("", " multithreaded test"));
871
872
873on_return:
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000874 return status;
875}
876