Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $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 Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 32 | #define POOL_SIZE 8000 |
| 33 | #define LOOP_COUNT 10000 |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 34 | #define AVERAGE_URL_LEN 80 |
| 35 | #define THREAD_COUNT 4 |
| 36 | |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 37 | static pj_highprec_t parse_len, print_len, cmp_len; |
| 38 | static pj_timestamp parse_time, print_time, cmp_time; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | /* URI creator functions. */ |
| 42 | static pjsip_uri *create_uri0( pj_pool_t *pool ); |
| 43 | static pjsip_uri *create_uri1( pj_pool_t *pool ); |
| 44 | static pjsip_uri *create_uri2( pj_pool_t *pool ); |
| 45 | static pjsip_uri *create_uri3( pj_pool_t *pool ); |
| 46 | static pjsip_uri *create_uri4( pj_pool_t *pool ); |
| 47 | static pjsip_uri *create_uri5( pj_pool_t *pool ); |
| 48 | static pjsip_uri *create_uri6( pj_pool_t *pool ); |
| 49 | static pjsip_uri *create_uri7( pj_pool_t *pool ); |
| 50 | static pjsip_uri *create_uri8( pj_pool_t *pool ); |
| 51 | static pjsip_uri *create_uri9( pj_pool_t *pool ); |
| 52 | static pjsip_uri *create_uri10( pj_pool_t *pool ); |
| 53 | static pjsip_uri *create_uri11( pj_pool_t *pool ); |
| 54 | static pjsip_uri *create_uri12( pj_pool_t *pool ); |
| 55 | static pjsip_uri *create_uri13( pj_pool_t *pool ); |
| 56 | static pjsip_uri *create_uri14( pj_pool_t *pool ); |
| 57 | static pjsip_uri *create_uri15( pj_pool_t *pool ); |
| 58 | static pjsip_uri *create_uri16( pj_pool_t *pool ); |
| 59 | static pjsip_uri *create_uri17( pj_pool_t *pool ); |
| 60 | static pjsip_uri *create_dummy( pj_pool_t *pool ); |
| 61 | |
| 62 | #define ERR_NOT_EQUAL -1001 |
| 63 | #define ERR_SYNTAX_ERR -1002 |
| 64 | |
| 65 | struct uri_test |
| 66 | { |
| 67 | pj_status_t status; |
| 68 | char str[PJSIP_MAX_URL_SIZE]; |
| 69 | pjsip_uri *(*creator)(pj_pool_t *pool); |
| 70 | pj_size_t len; |
| 71 | } uri_test_array[] = |
| 72 | { |
| 73 | { |
| 74 | PJ_SUCCESS, |
| 75 | "sip:localhost", |
| 76 | &create_uri0 |
| 77 | }, |
| 78 | { |
| 79 | PJ_SUCCESS, |
| 80 | "sip:user@localhost", |
| 81 | &create_uri1 |
| 82 | }, |
| 83 | { |
| 84 | PJ_SUCCESS, |
| 85 | "sip:user:password@localhost:5060", |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 86 | &create_uri2, }, |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 87 | { |
| 88 | /* Port is specified should not match unspecified port. */ |
| 89 | ERR_NOT_EQUAL, |
| 90 | "sip:localhost:5060", |
| 91 | &create_uri3 |
| 92 | }, |
| 93 | { |
| 94 | /* All recognized parameters. */ |
| 95 | PJ_SUCCESS, |
| 96 | "sip:localhost;transport=tcp;user=ip;ttl=255;lr;maddr=127.0.0.1;method=ACK", |
| 97 | &create_uri4 |
| 98 | }, |
| 99 | { |
| 100 | /* Params mixed with other params and header params. */ |
| 101 | PJ_SUCCESS, |
| 102 | "sip:localhost;pickup=hurry;user=phone;message=I%20am%20sorry" |
| 103 | "?Subject=Hello%20There&Server=SIP%20Server", |
| 104 | &create_uri5 |
| 105 | }, |
| 106 | { |
| 107 | /* SIPS. */ |
| 108 | PJ_SUCCESS, |
| 109 | "sips:localhost", |
| 110 | &create_uri6, |
| 111 | }, |
| 112 | { |
| 113 | /* Name address */ |
| 114 | PJ_SUCCESS, |
| 115 | "<sip:localhost>", |
| 116 | &create_uri7 |
| 117 | }, |
| 118 | { |
| 119 | /* Name address with display name and SIPS scheme with some redundant |
| 120 | * whitespaced. |
| 121 | */ |
| 122 | PJ_SUCCESS, |
| 123 | " Power Administrator <sips:localhost>", |
| 124 | &create_uri8 |
| 125 | }, |
| 126 | { |
| 127 | /* Name address. */ |
| 128 | PJ_SUCCESS, |
| 129 | " \"User\" <sip:user@localhost:5071>", |
| 130 | &create_uri9 |
| 131 | }, |
| 132 | { |
| 133 | /* Escaped sequence in display name (display=Strange User\"\\\"). */ |
| 134 | PJ_SUCCESS, |
| 135 | " \"Strange User\\\"\\\\\\\"\" <sip:localhost>", |
| 136 | &create_uri10, |
| 137 | }, |
| 138 | { |
| 139 | /* Errorneous escaping in display name. */ |
| 140 | ERR_SYNTAX_ERR, |
| 141 | " \"Rogue User\\\" <sip:localhost>", |
| 142 | &create_uri11, |
| 143 | }, |
| 144 | { |
| 145 | /* Dangling quote in display name, but that should be OK. */ |
| 146 | PJ_SUCCESS, |
| 147 | "Strange User\" <sip:localhost>", |
| 148 | &create_uri12, |
| 149 | }, |
| 150 | { |
| 151 | /* Special characters in parameter value must be quoted. */ |
| 152 | PJ_SUCCESS, |
| 153 | "sip:localhost;pvalue=\"hello world\"", |
| 154 | &create_uri13, |
| 155 | }, |
| 156 | { |
| 157 | /* Excercise strange character sets allowed in display, user, password, |
| 158 | * host, and port. |
| 159 | */ |
| 160 | PJ_SUCCESS, |
| 161 | "This is -. !% *_+`'~ me <sip:a19A&=+$,;?/%2c:%40a&Zz=+$,@" |
| 162 | "my_proxy09.MY-domain.com:9801>", |
| 163 | &create_uri14, |
| 164 | }, |
| 165 | { |
| 166 | /* Another excercise to the allowed character sets to the hostname. */ |
| 167 | PJ_SUCCESS, |
| 168 | "sip:" ALPHANUM "-_.com", |
| 169 | &create_uri15, |
| 170 | }, |
| 171 | { |
| 172 | /* Another excercise to the allowed character sets to the username |
| 173 | * and password. |
| 174 | */ |
| 175 | PJ_SUCCESS, |
| 176 | "sip:" USER_CHAR ":" PASS_CHAR "@host", |
| 177 | &create_uri16, |
| 178 | }, |
| 179 | { |
| 180 | /* Excercise to the pname and pvalue, and mixup of other-param |
| 181 | * between 'recognized' params. |
| 182 | */ |
| 183 | PJ_SUCCESS, |
| 184 | "sip:host;user=ip;" PARAM_CHAR "%21=" PARAM_CHAR "%21" |
| 185 | ";lr;other=1;transport=sctp;other2", |
| 186 | &create_uri17, |
| 187 | }, |
| 188 | { |
| 189 | /* 18: This should trigger syntax error. */ |
| 190 | ERR_SYNTAX_ERR, |
| 191 | "sip:", |
| 192 | &create_dummy, |
| 193 | }, |
| 194 | { |
| 195 | /* 19: Syntax error: whitespace after scheme. */ |
| 196 | ERR_SYNTAX_ERR, |
| 197 | "sip :host", |
| 198 | &create_dummy, |
| 199 | }, |
| 200 | { |
| 201 | /* 20: Syntax error: whitespace before hostname. */ |
| 202 | ERR_SYNTAX_ERR, |
| 203 | "sip: host", |
| 204 | &create_dummy, |
| 205 | }, |
| 206 | { |
| 207 | /* 21: Syntax error: invalid port. */ |
| 208 | ERR_SYNTAX_ERR, |
| 209 | "sip:user:password", |
| 210 | &create_dummy, |
| 211 | }, |
| 212 | { |
| 213 | /* 22: Syntax error: no host. */ |
| 214 | ERR_SYNTAX_ERR, |
| 215 | "sip:user@", |
| 216 | &create_dummy, |
| 217 | }, |
| 218 | { |
| 219 | /* 23: Syntax error: no user/host. */ |
| 220 | ERR_SYNTAX_ERR, |
| 221 | "sip:@", |
| 222 | &create_dummy, |
| 223 | }, |
| 224 | { |
| 225 | /* 24: Syntax error: empty string. */ |
| 226 | ERR_SYNTAX_ERR, |
| 227 | "", |
| 228 | &create_dummy, |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | static pjsip_uri *create_uri0(pj_pool_t *pool) |
| 233 | { |
| 234 | /* "sip:localhost" */ |
| 235 | pjsip_url *url = pjsip_url_create(pool, 0); |
| 236 | |
| 237 | pj_strdup2(pool, &url->host, "localhost"); |
| 238 | return (pjsip_uri*)url; |
| 239 | } |
| 240 | |
| 241 | static pjsip_uri *create_uri1(pj_pool_t *pool) |
| 242 | { |
| 243 | /* "sip:user@localhost" */ |
| 244 | pjsip_url *url = pjsip_url_create(pool, 0); |
| 245 | |
| 246 | pj_strdup2( pool, &url->user, "user"); |
| 247 | pj_strdup2( pool, &url->host, "localhost"); |
| 248 | |
| 249 | return (pjsip_uri*) url; |
| 250 | } |
| 251 | |
| 252 | static pjsip_uri *create_uri2(pj_pool_t *pool) |
| 253 | { |
| 254 | /* "sip:user:password@localhost:5060" */ |
| 255 | pjsip_url *url = pjsip_url_create(pool, 0); |
| 256 | |
| 257 | pj_strdup2( pool, &url->user, "user"); |
| 258 | pj_strdup2( pool, &url->passwd, "password"); |
| 259 | pj_strdup2( pool, &url->host, "localhost"); |
| 260 | url->port = 5060; |
| 261 | |
| 262 | return (pjsip_uri*) url; |
| 263 | } |
| 264 | |
| 265 | static pjsip_uri *create_uri3(pj_pool_t *pool) |
| 266 | { |
| 267 | /* Like: "sip:localhost:5060", but without the port. */ |
| 268 | pjsip_url *url = pjsip_url_create(pool, 0); |
| 269 | |
| 270 | pj_strdup2(pool, &url->host, "localhost"); |
| 271 | return (pjsip_uri*)url; |
| 272 | } |
| 273 | |
| 274 | static pjsip_uri *create_uri4(pj_pool_t *pool) |
| 275 | { |
| 276 | /* "sip:localhost;transport=tcp;user=ip;ttl=255;lr;maddr=127.0.0.1;method=ACK" */ |
| 277 | pjsip_url *url = pjsip_url_create(pool, 0); |
| 278 | |
| 279 | pj_strdup2(pool, &url->host, "localhost"); |
| 280 | pj_strdup2(pool, &url->transport_param, "tcp"); |
| 281 | pj_strdup2(pool, &url->user_param, "ip"); |
| 282 | url->ttl_param = 255; |
| 283 | url->lr_param = 1; |
| 284 | pj_strdup2(pool, &url->maddr_param, "127.0.0.1"); |
| 285 | pj_strdup2(pool, &url->method_param, "ACK"); |
| 286 | |
| 287 | return (pjsip_uri*)url; |
| 288 | } |
| 289 | |
| 290 | #define param_add(list,pname,pvalue) \ |
| 291 | do { \ |
| 292 | pjsip_param *param; \ |
| 293 | param=pj_pool_alloc(pool, sizeof(pjsip_param)); \ |
| 294 | param->name = pj_str(pname); \ |
| 295 | param->value = pj_str(pvalue); \ |
| 296 | pj_list_insert_before(&list, param); \ |
| 297 | } while (0) |
| 298 | |
| 299 | static pjsip_uri *create_uri5(pj_pool_t *pool) |
| 300 | { |
| 301 | /* "sip:localhost;pickup=hurry;user=phone;message=I%20am%20sorry" |
| 302 | "?Subject=Hello%20There&Server=SIP%20Server" |
| 303 | */ |
| 304 | pjsip_url *url = pjsip_url_create(pool, 0); |
| 305 | |
| 306 | pj_strdup2(pool, &url->host, "localhost"); |
| 307 | pj_strdup2(pool, &url->user_param, "phone"); |
| 308 | |
| 309 | //pj_strdup2(pool, &url->other_param, ";pickup=hurry;message=I%20am%20sorry"); |
| 310 | param_add(url->other_param, "pickup", "hurry"); |
| 311 | param_add(url->other_param, "message", "I am sorry"); |
| 312 | |
| 313 | //pj_strdup2(pool, &url->header_param, "?Subject=Hello%20There&Server=SIP%20Server"); |
| 314 | param_add(url->header_param, "Subject", "Hello There"); |
| 315 | param_add(url->header_param, "Server", "SIP Server"); |
| 316 | return (pjsip_uri*)url; |
| 317 | |
| 318 | } |
| 319 | |
| 320 | static pjsip_uri *create_uri6(pj_pool_t *pool) |
| 321 | { |
| 322 | /* "sips:localhost" */ |
| 323 | pjsip_url *url = pjsip_url_create(pool, 1); |
| 324 | |
| 325 | pj_strdup2(pool, &url->host, "localhost"); |
| 326 | return (pjsip_uri*)url; |
| 327 | } |
| 328 | |
| 329 | static pjsip_uri *create_uri7(pj_pool_t *pool) |
| 330 | { |
| 331 | /* "<sip:localhost>" */ |
| 332 | pjsip_name_addr *name_addr = pjsip_name_addr_create(pool); |
| 333 | pjsip_url *url; |
| 334 | |
| 335 | url = pjsip_url_create(pool, 0); |
| 336 | name_addr->uri = (pjsip_uri*) url; |
| 337 | |
| 338 | pj_strdup2(pool, &url->host, "localhost"); |
| 339 | return (pjsip_uri*)name_addr; |
| 340 | } |
| 341 | |
| 342 | static pjsip_uri *create_uri8(pj_pool_t *pool) |
| 343 | { |
| 344 | /* " Power Administrator <sips:localhost>" */ |
| 345 | pjsip_name_addr *name_addr = pjsip_name_addr_create(pool); |
| 346 | pjsip_url *url; |
| 347 | |
| 348 | url = pjsip_url_create(pool, 1); |
| 349 | name_addr->uri = (pjsip_uri*) url; |
| 350 | |
| 351 | pj_strdup2(pool, &name_addr->display, "Power Administrator"); |
| 352 | pj_strdup2(pool, &url->host, "localhost"); |
| 353 | return (pjsip_uri*)name_addr; |
| 354 | } |
| 355 | |
| 356 | static pjsip_uri *create_uri9(pj_pool_t *pool) |
| 357 | { |
| 358 | /* " \"User\" <sip:user@localhost:5071>" */ |
| 359 | pjsip_name_addr *name_addr = pjsip_name_addr_create(pool); |
| 360 | pjsip_url *url; |
| 361 | |
| 362 | url = pjsip_url_create(pool, 0); |
| 363 | name_addr->uri = (pjsip_uri*) url; |
| 364 | |
| 365 | pj_strdup2(pool, &name_addr->display, "User"); |
| 366 | pj_strdup2(pool, &url->user, "user"); |
| 367 | pj_strdup2(pool, &url->host, "localhost"); |
| 368 | url->port = 5071; |
| 369 | return (pjsip_uri*)name_addr; |
| 370 | } |
| 371 | |
| 372 | static pjsip_uri *create_uri10(pj_pool_t *pool) |
| 373 | { |
| 374 | /* " \"Strange User\\\"\\\\\\\"\" <sip:localhost>" */ |
| 375 | pjsip_name_addr *name_addr = pjsip_name_addr_create(pool); |
| 376 | pjsip_url *url; |
| 377 | |
| 378 | url = pjsip_url_create(pool, 0); |
| 379 | name_addr->uri = (pjsip_uri*) url; |
| 380 | |
| 381 | pj_strdup2(pool, &name_addr->display, "Strange User\\\"\\\\\\\""); |
| 382 | pj_strdup2(pool, &url->host, "localhost"); |
| 383 | return (pjsip_uri*)name_addr; |
| 384 | } |
| 385 | |
| 386 | static pjsip_uri *create_uri11(pj_pool_t *pool) |
| 387 | { |
| 388 | /* " \"Rogue User\\\" <sip:localhost>" */ |
| 389 | pjsip_name_addr *name_addr = pjsip_name_addr_create(pool); |
| 390 | pjsip_url *url; |
| 391 | |
| 392 | url = pjsip_url_create(pool, 0); |
| 393 | name_addr->uri = (pjsip_uri*) url; |
| 394 | |
| 395 | pj_strdup2(pool, &name_addr->display, "Rogue User\\"); |
| 396 | pj_strdup2(pool, &url->host, "localhost"); |
| 397 | return (pjsip_uri*)name_addr; |
| 398 | } |
| 399 | |
| 400 | static pjsip_uri *create_uri12(pj_pool_t *pool) |
| 401 | { |
| 402 | /* "Strange User\" <sip:localhost>" */ |
| 403 | pjsip_name_addr *name_addr = pjsip_name_addr_create(pool); |
| 404 | pjsip_url *url; |
| 405 | |
| 406 | url = pjsip_url_create(pool, 0); |
| 407 | name_addr->uri = (pjsip_uri*) url; |
| 408 | |
| 409 | pj_strdup2(pool, &name_addr->display, "Strange User\""); |
| 410 | pj_strdup2(pool, &url->host, "localhost"); |
| 411 | return (pjsip_uri*)name_addr; |
| 412 | } |
| 413 | |
| 414 | static pjsip_uri *create_uri13(pj_pool_t *pool) |
| 415 | { |
| 416 | /* "sip:localhost;pvalue=\"hello world\"" */ |
| 417 | pjsip_url *url; |
| 418 | url = pjsip_url_create(pool, 0); |
| 419 | pj_strdup2(pool, &url->host, "localhost"); |
| 420 | //pj_strdup2(pool, &url->other_param, ";pvalue=\"hello world\""); |
| 421 | param_add(url->other_param, "pvalue", "hello world"); |
| 422 | return (pjsip_uri*)url; |
| 423 | } |
| 424 | |
| 425 | static pjsip_uri *create_uri14(pj_pool_t *pool) |
| 426 | { |
| 427 | /* "This is -. !% *_+`'~ me <sip:a19A&=+$,;?/%2c:%40a&Zz=+$,@my_proxy09.my-domain.com:9801>" */ |
| 428 | pjsip_name_addr *name_addr = pjsip_name_addr_create(pool); |
| 429 | pjsip_url *url; |
| 430 | |
| 431 | url = pjsip_url_create(pool, 0); |
| 432 | name_addr->uri = (pjsip_uri*) url; |
| 433 | |
| 434 | pj_strdup2(pool, &name_addr->display, "This is -. !% *_+`'~ me"); |
| 435 | pj_strdup2(pool, &url->user, "a19A&=+$,;?/,"); |
| 436 | pj_strdup2(pool, &url->passwd, "@a&Zz=+$,"); |
| 437 | pj_strdup2(pool, &url->host, "my_proxy09.MY-domain.com"); |
| 438 | url->port = 9801; |
| 439 | return (pjsip_uri*)name_addr; |
| 440 | } |
| 441 | |
| 442 | static pjsip_uri *create_uri15(pj_pool_t *pool) |
| 443 | { |
| 444 | /* "sip:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.com" */ |
| 445 | pjsip_url *url; |
| 446 | url = pjsip_url_create(pool, 0); |
| 447 | pj_strdup2(pool, &url->host, ALPHANUM "-_.com"); |
| 448 | return (pjsip_uri*)url; |
| 449 | } |
| 450 | |
| 451 | static pjsip_uri *create_uri16(pj_pool_t *pool) |
| 452 | { |
| 453 | /* "sip:" USER_CHAR ":" PASS_CHAR "@host" */ |
| 454 | pjsip_url *url; |
| 455 | url = pjsip_url_create(pool, 0); |
| 456 | pj_strdup2(pool, &url->user, USER_CHAR); |
| 457 | pj_strdup2(pool, &url->passwd, PASS_CHAR); |
| 458 | pj_strdup2(pool, &url->host, "host"); |
| 459 | return (pjsip_uri*)url; |
| 460 | } |
| 461 | |
| 462 | static pjsip_uri *create_uri17(pj_pool_t *pool) |
| 463 | { |
| 464 | /* "sip:host;user=ip;" PARAM_CHAR "%21=" PARAM_CHAR "%21;lr;other=1;transport=sctp;other2" */ |
| 465 | pjsip_url *url; |
| 466 | url = pjsip_url_create(pool, 0); |
| 467 | pj_strdup2(pool, &url->host, "host"); |
| 468 | pj_strdup2(pool, &url->user_param, "ip"); |
| 469 | pj_strdup2(pool, &url->transport_param, "sctp"); |
| 470 | param_add(url->other_param, PARAM_CHAR "!", PARAM_CHAR "!"); |
| 471 | param_add(url->other_param, "other", "1"); |
| 472 | param_add(url->other_param, "other2", ""); |
| 473 | url->lr_param = 1; |
| 474 | return (pjsip_uri*)url; |
| 475 | } |
| 476 | |
| 477 | static pjsip_uri *create_dummy(pj_pool_t *pool) |
| 478 | { |
| 479 | PJ_UNUSED_ARG(pool); |
| 480 | return NULL; |
| 481 | } |
| 482 | |
| 483 | /*****************************************************************************/ |
| 484 | |
| 485 | /* |
| 486 | * Test one test entry. |
| 487 | */ |
| 488 | static pj_status_t do_uri_test(pj_pool_t *pool, struct uri_test *entry) |
| 489 | { |
| 490 | pj_status_t status; |
| 491 | int len; |
| 492 | pjsip_uri *parsed_uri, *ref_uri; |
| 493 | pj_str_t s1 = {NULL, 0}, s2 = {NULL, 0}; |
| 494 | pj_timestamp t1, t2; |
| 495 | |
| 496 | entry->len = pj_native_strlen(entry->str); |
| 497 | |
| 498 | /* Parse URI text. */ |
| 499 | pj_get_timestamp(&t1); |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 500 | parse_len = parse_len + entry->len; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 501 | parsed_uri = pjsip_parse_uri(pool, entry->str, entry->len, 0); |
| 502 | if (!parsed_uri) { |
| 503 | /* Parsing failed. If the entry says that this is expected, then |
| 504 | * return OK. |
| 505 | */ |
| 506 | status = entry->status==ERR_SYNTAX_ERR ? PJ_SUCCESS : -10; |
| 507 | if (status != 0) { |
| 508 | PJ_LOG(3,("", " uri parse error!\n" |
| 509 | " uri='%s'\n", |
| 510 | entry->str)); |
| 511 | } |
| 512 | goto on_return; |
| 513 | } |
| 514 | pj_get_timestamp(&t2); |
| 515 | pj_sub_timestamp(&t2, &t1); |
| 516 | pj_add_timestamp(&parse_time, &t2); |
| 517 | |
| 518 | /* Create the reference URI. */ |
| 519 | ref_uri = entry->creator(pool); |
| 520 | |
| 521 | /* Print both URI. */ |
| 522 | s1.ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE); |
| 523 | s2.ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE); |
| 524 | |
| 525 | pj_get_timestamp(&t1); |
| 526 | len = pjsip_uri_print( PJSIP_URI_IN_OTHER, parsed_uri, s1.ptr, PJSIP_MAX_URL_SIZE); |
| 527 | if (len < 1) { |
| 528 | status = -20; |
| 529 | goto on_return; |
| 530 | } |
| 531 | s1.ptr[len] = '\0'; |
| 532 | s1.slen = len; |
| 533 | |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 534 | print_len = print_len + len; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 535 | pj_get_timestamp(&t2); |
| 536 | pj_sub_timestamp(&t2, &t1); |
| 537 | pj_add_timestamp(&print_time, &t2); |
| 538 | |
| 539 | len = pjsip_uri_print( PJSIP_URI_IN_OTHER, ref_uri, s2.ptr, PJSIP_MAX_URL_SIZE); |
| 540 | if (len < 1) { |
| 541 | status = -30; |
| 542 | goto on_return; |
| 543 | } |
| 544 | s2.ptr[len] = '\0'; |
| 545 | s2.slen = len; |
| 546 | |
| 547 | /* Full comparison of parsed URI with reference URI. */ |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 548 | pj_get_timestamp(&t1); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 549 | status = pjsip_uri_cmp(PJSIP_URI_IN_OTHER, parsed_uri, ref_uri); |
| 550 | if (status != 0) { |
| 551 | /* Not equal. See if this is the expected status. */ |
| 552 | status = entry->status==ERR_NOT_EQUAL ? PJ_SUCCESS : -40; |
| 553 | if (status != 0) { |
| 554 | PJ_LOG(3,("", " uri comparison mismatch, status=%d:\n" |
| 555 | " uri1='%s'\n" |
| 556 | " uri2='%s'", |
| 557 | status, s1.ptr, s2.ptr)); |
| 558 | } |
| 559 | goto on_return; |
| 560 | |
| 561 | } else { |
| 562 | /* Equal. See if this is the expected status. */ |
| 563 | status = entry->status==PJ_SUCCESS ? PJ_SUCCESS : -50; |
| 564 | if (status != PJ_SUCCESS) { |
| 565 | goto on_return; |
| 566 | } |
| 567 | } |
| 568 | |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 569 | cmp_len = cmp_len + len; |
| 570 | pj_get_timestamp(&t2); |
| 571 | pj_sub_timestamp(&t2, &t1); |
| 572 | pj_add_timestamp(&cmp_time, &t2); |
| 573 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 574 | /* Compare text. */ |
| 575 | if (pj_strcmp(&s1, &s2) != 0) { |
| 576 | /* Not equal. */ |
| 577 | status = -60; |
| 578 | } |
| 579 | |
| 580 | on_return: |
| 581 | return status; |
| 582 | } |
| 583 | |
| 584 | pj_status_t uri_test() |
| 585 | { |
| 586 | unsigned i, loop; |
| 587 | pj_pool_t *pool; |
| 588 | pj_status_t status; |
| 589 | pj_timestamp zero; |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 590 | pj_time_val elapsed; |
| 591 | pj_highprec_t avg_parse, avg_print, avg_cmp, kbytes; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 592 | |
| 593 | zero.u32.hi = zero.u32.lo = 0; |
| 594 | |
| 595 | PJ_LOG(3,("", " simple test")); |
| 596 | pool = pjsip_endpt_create_pool(endpt, "", POOL_SIZE, POOL_SIZE); |
| 597 | for (i=0; i<PJ_ARRAY_SIZE(uri_test_array); ++i) { |
| 598 | status = do_uri_test(pool, &uri_test_array[i]); |
| 599 | if (status != PJ_SUCCESS) { |
| 600 | PJ_LOG(3,("uri_test", " error %d when testing entry %d", |
| 601 | status, i)); |
| 602 | goto on_return; |
| 603 | } |
| 604 | } |
| 605 | pjsip_endpt_destroy_pool(endpt, pool); |
| 606 | |
| 607 | PJ_LOG(3,("", " benchmarking...")); |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 608 | parse_len = print_len = cmp_len = 0; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 609 | parse_time.u32.hi = parse_time.u32.lo = 0; |
| 610 | print_time.u32.hi = print_time.u32.lo = 0; |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 611 | cmp_time.u32.hi = cmp_time.u32.lo = 0; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 612 | for (loop=0; loop<LOOP_COUNT; ++loop) { |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 613 | pool = pjsip_endpt_create_pool(endpt, "", POOL_SIZE, POOL_SIZE); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 614 | for (i=0; i<PJ_ARRAY_SIZE(uri_test_array); ++i) { |
| 615 | status = do_uri_test(pool, &uri_test_array[i]); |
| 616 | if (status != PJ_SUCCESS) { |
| 617 | PJ_LOG(3,("uri_test", " error %d when testing entry %d", |
| 618 | status, i)); |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 619 | pjsip_endpt_destroy_pool(endpt, pool); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 620 | goto on_return; |
| 621 | } |
| 622 | } |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 623 | pjsip_endpt_destroy_pool(endpt, pool); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 626 | kbytes = parse_len; |
| 627 | pj_highprec_mod(kbytes, 1000000); |
| 628 | pj_highprec_div(kbytes, 100000); |
| 629 | elapsed = pj_elapsed_time(&zero, &parse_time); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 630 | avg_parse = pj_elapsed_usec(&zero, &parse_time); |
| 631 | pj_highprec_mul(avg_parse, AVERAGE_URL_LEN); |
| 632 | pj_highprec_div(avg_parse, parse_len); |
| 633 | avg_parse = 1000000 / avg_parse; |
| 634 | |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 635 | PJ_LOG(3,("", " %u.%u MB of urls parsed in %d.%03ds (avg=%d urls/sec)", |
| 636 | (unsigned)(parse_len/1000000), (unsigned)kbytes, |
| 637 | elapsed.sec, elapsed.msec, |
| 638 | (unsigned)avg_parse)); |
| 639 | |
| 640 | kbytes = print_len; |
| 641 | pj_highprec_mod(kbytes, 1000000); |
| 642 | pj_highprec_div(kbytes, 100000); |
| 643 | elapsed = pj_elapsed_time(&zero, &print_time); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 644 | avg_print = pj_elapsed_usec(&zero, &print_time); |
| 645 | pj_highprec_mul(avg_print, AVERAGE_URL_LEN); |
| 646 | pj_highprec_div(avg_print, parse_len); |
| 647 | avg_print = 1000000 / avg_print; |
| 648 | |
Benny Prijono | 5b91744 | 2005-11-21 17:07:18 +0000 | [diff] [blame] | 649 | PJ_LOG(3,("", " %u.%u MB of urls printed in %d.%03ds (avg=%d urls/sec)", |
| 650 | (unsigned)(print_len/1000000), (unsigned)kbytes, |
| 651 | elapsed.sec, elapsed.msec, |
| 652 | (unsigned)avg_print)); |
| 653 | |
| 654 | kbytes = cmp_len; |
| 655 | pj_highprec_mod(kbytes, 1000000); |
| 656 | pj_highprec_div(kbytes, 100000); |
| 657 | elapsed = pj_elapsed_time(&zero, &cmp_time); |
| 658 | avg_cmp = pj_elapsed_usec(&zero, &cmp_time); |
| 659 | pj_highprec_mul(avg_cmp, AVERAGE_URL_LEN); |
| 660 | pj_highprec_div(avg_cmp, cmp_len); |
| 661 | avg_cmp = 1000000 / avg_cmp; |
| 662 | |
| 663 | PJ_LOG(3,("", " %u.%u MB of urls compared in %d.%03ds (avg=%d urls/sec)", |
| 664 | (unsigned)(cmp_len/1000000), (unsigned)kbytes, |
| 665 | elapsed.sec, elapsed.msec, |
| 666 | (unsigned)avg_cmp)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 667 | |
| 668 | PJ_LOG(3,("", " multithreaded test")); |
| 669 | |
| 670 | |
| 671 | on_return: |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 672 | return status; |
| 673 | } |
| 674 | |