| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "test.h" |
| 20 |
#include <pjsip_ua.h> |
| 21 |
#include <pjsip.h> |
| 22 |
#include <pjlib.h> |
| 23 |
|
| 24 |
#define THIS_FILE "regc_test.c" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
static pj_status_t mod_send_on_tx_request(pjsip_tx_data *tdata); |
| 30 |
|
| 31 |
static struct |
| 32 |
{ |
| 33 |
pjsip_module mod; |
| 34 |
unsigned count; |
| 35 |
unsigned count_before_reject; |
| 36 |
} send_mod = |
| 37 |
{ |
| 38 |
{ |
| 39 |
NULL, NULL, |
| 40 |
{ "mod-send", 8 }, |
| 41 |
-1, |
| 42 |
PJSIP_MOD_PRIORITY_TRANSPORT_LAYER, |
| 43 |
NULL, |
| 44 |
NULL, |
| 45 |
NULL, |
| 46 |
NULL, |
| 47 |
NULL, |
| 48 |
NULL, |
| 49 |
&mod_send_on_tx_request, |
| 50 |
NULL, |
| 51 |
NULL, |
| 52 |
}, |
| 53 |
0, |
| 54 |
0xFFFF |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
static pj_status_t mod_send_on_tx_request(pjsip_tx_data *tdata) |
| 59 |
{ |
| 60 |
const pjsip_from_hdr *from_hdr = (const pjsip_from_hdr*) |
| 61 |
pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, NULL); |
| 62 |
|
| 63 |
if ((tdata->msg->line.req.method.id != PJSIP_REGISTER_METHOD) || |
| 64 |
!is_user_equal(from_hdr, "regc-test")) |
| 65 |
return PJ_FALSE; |
| 66 |
|
| 67 |
if (++send_mod.count > send_mod.count_before_reject) |
| 68 |
return PJ_ECANCELLED; |
| 69 |
else |
| 70 |
return PJ_SUCCESS; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static pj_bool_t regs_rx_request(pjsip_rx_data *rdata); |
| 77 |
|
| 78 |
enum contact_op |
| 79 |
{ |
| 80 |
NONE, |
| 81 |
EXACT, |
| 82 |
MODIFIED, |
| 83 |
}; |
| 84 |
|
| 85 |
struct registrar_cfg |
| 86 |
{ |
| 87 |
pj_bool_t respond; |
| 88 |
unsigned status_code; |
| 89 |
pj_bool_t authenticate; |
| 90 |
enum contact_op contact_op; |
| 91 |
unsigned expires_param; |
| 92 |
unsigned expires; |
| 93 |
|
| 94 |
pj_str_t more_contacts; |
| 95 |
}; |
| 96 |
|
| 97 |
static struct registrar |
| 98 |
{ |
| 99 |
pjsip_module mod; |
| 100 |
struct registrar_cfg cfg; |
| 101 |
unsigned response_cnt; |
| 102 |
} registrar = |
| 103 |
{ |
| 104 |
{ |
| 105 |
NULL, NULL, |
| 106 |
{ "registrar", 9 }, |
| 107 |
-1, |
| 108 |
PJSIP_MOD_PRIORITY_APPLICATION, |
| 109 |
NULL, |
| 110 |
NULL, |
| 111 |
NULL, |
| 112 |
NULL, |
| 113 |
®s_rx_request, |
| 114 |
NULL, |
| 115 |
NULL, |
| 116 |
NULL, |
| 117 |
NULL, |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
static pj_bool_t regs_rx_request(pjsip_rx_data *rdata) |
| 122 |
{ |
| 123 |
pjsip_msg *msg = rdata->msg_info.msg; |
| 124 |
pjsip_hdr hdr_list; |
| 125 |
int code; |
| 126 |
pj_status_t status; |
| 127 |
|
| 128 |
if ((rdata->msg_info.msg->line.req.method.id != PJSIP_REGISTER_METHOD) || |
| 129 |
!is_user_equal(rdata->msg_info.from, "regc-test")) |
| 130 |
return PJ_FALSE; |
| 131 |
|
| 132 |
if (!registrar.cfg.respond) |
| 133 |
return PJ_TRUE; |
| 134 |
|
| 135 |
pj_list_init(&hdr_list); |
| 136 |
|
| 137 |
if (registrar.cfg.authenticate && |
| 138 |
pjsip_msg_find_hdr(msg, PJSIP_H_AUTHORIZATION, NULL)==NULL) |
| 139 |
{ |
| 140 |
pjsip_generic_string_hdr *hwww; |
| 141 |
const pj_str_t hname = pj_str("WWW-Authenticate"); |
| 142 |
const pj_str_t hvalue = pj_str("Digest realm=\"test\""); |
| 143 |
|
| 144 |
hwww = pjsip_generic_string_hdr_create(rdata->tp_info.pool, &hname, |
| 145 |
&hvalue); |
| 146 |
pj_list_push_back(&hdr_list, hwww); |
| 147 |
|
| 148 |
code = 401; |
| 149 |
|
| 150 |
} else { |
| 151 |
if (registrar.cfg.contact_op == EXACT || |
| 152 |
registrar.cfg.contact_op == MODIFIED) |
| 153 |
{ |
| 154 |
pjsip_hdr *hsrc; |
| 155 |
|
| 156 |
for (hsrc=msg->hdr.next; hsrc!=&msg->hdr; hsrc=hsrc->next) { |
| 157 |
pjsip_contact_hdr *hdst; |
| 158 |
|
| 159 |
if (hsrc->type != PJSIP_H_CONTACT) |
| 160 |
continue; |
| 161 |
|
| 162 |
hdst = (pjsip_contact_hdr*) |
| 163 |
pjsip_hdr_clone(rdata->tp_info.pool, hsrc); |
| 164 |
|
| 165 |
if (hdst->expires==0) |
| 166 |
continue; |
| 167 |
|
| 168 |
if (registrar.cfg.contact_op == MODIFIED) { |
| 169 |
if (PJSIP_URI_SCHEME_IS_SIP(hdst->uri) || |
| 170 |
PJSIP_URI_SCHEME_IS_SIPS(hdst->uri)) |
| 171 |
{ |
| 172 |
pjsip_sip_uri *sip_uri = (pjsip_sip_uri*) |
| 173 |
pjsip_uri_get_uri(hdst->uri); |
| 174 |
sip_uri->host = pj_str("x-modified-host"); |
| 175 |
sip_uri->port = 1; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if (registrar.cfg.expires_param) |
| 180 |
hdst->expires = registrar.cfg.expires_param; |
| 181 |
|
| 182 |
pj_list_push_back(&hdr_list, hdst); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (registrar.cfg.more_contacts.slen) { |
| 187 |
pjsip_generic_string_hdr *hcontact; |
| 188 |
const pj_str_t hname = pj_str("Contact"); |
| 189 |
|
| 190 |
hcontact = pjsip_generic_string_hdr_create(rdata->tp_info.pool, &hname, |
| 191 |
®istrar.cfg.more_contacts); |
| 192 |
pj_list_push_back(&hdr_list, hcontact); |
| 193 |
} |
| 194 |
|
| 195 |
if (registrar.cfg.expires) { |
| 196 |
pjsip_expires_hdr *hexp; |
| 197 |
|
| 198 |
hexp = pjsip_expires_hdr_create(rdata->tp_info.pool, |
| 199 |
registrar.cfg.expires); |
| 200 |
pj_list_push_back(&hdr_list, hexp); |
| 201 |
} |
| 202 |
|
| 203 |
registrar.response_cnt++; |
| 204 |
|
| 205 |
code = registrar.cfg.status_code; |
| 206 |
} |
| 207 |
|
| 208 |
status = pjsip_endpt_respond(endpt, NULL, rdata, code, NULL, |
| 209 |
&hdr_list, NULL, NULL); |
| 210 |
pj_assert(status == PJ_SUCCESS); |
| 211 |
|
| 212 |
return (status == PJ_SUCCESS); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
struct client |
| 219 |
{ |
| 220 |
|
| 221 |
int error; |
| 222 |
int code; |
| 223 |
pj_bool_t have_reg; |
| 224 |
unsigned expiration; |
| 225 |
unsigned contact_cnt; |
| 226 |
pj_bool_t auth; |
| 227 |
|
| 228 |
|
| 229 |
pj_bool_t destroy_on_cb; |
| 230 |
|
| 231 |
|
| 232 |
pj_bool_t done; |
| 233 |
|
| 234 |
|
| 235 |
unsigned interval; |
| 236 |
unsigned next_reg; |
| 237 |
}; |
| 238 |
|
| 239 |
|
| 240 |
static void client_cb(struct pjsip_regc_cbparam *param) |
| 241 |
{ |
| 242 |
struct client *client = (struct client*) param->token; |
| 243 |
pjsip_regc_info info; |
| 244 |
pj_status_t status; |
| 245 |
|
| 246 |
client->done = PJ_TRUE; |
| 247 |
|
| 248 |
status = pjsip_regc_get_info(param->regc, &info); |
| 249 |
pj_assert(status == PJ_SUCCESS); |
| 250 |
PJ_UNUSED_ARG(status); |
| 251 |
|
| 252 |
client->error = (param->status != PJ_SUCCESS); |
| 253 |
client->code = param->code; |
| 254 |
|
| 255 |
if (client->error) |
| 256 |
return; |
| 257 |
|
| 258 |
client->have_reg = info.auto_reg && |
| 259 |
info.interval != PJSIP_EXPIRES_NOT_SPECIFIED && |
| 260 |
param->expiration != PJSIP_EXPIRES_NOT_SPECIFIED; |
| 261 |
client->expiration = param->expiration; |
| 262 |
client->contact_cnt = param->contact_cnt; |
| 263 |
client->interval = info.interval; |
| 264 |
client->next_reg = info.next_reg; |
| 265 |
|
| 266 |
if (client->destroy_on_cb) |
| 267 |
pjsip_regc_destroy(param->regc); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
static struct client client_result; |
| 273 |
static int do_test(const char *title, |
| 274 |
const struct registrar_cfg *srv_cfg, |
| 275 |
const struct client *client_cfg, |
| 276 |
const pj_str_t *registrar_uri, |
| 277 |
unsigned contact_cnt, |
| 278 |
const pj_str_t contacts[], |
| 279 |
unsigned expires, |
| 280 |
pj_bool_t leave_session, |
| 281 |
pjsip_regc **p_regc) |
| 282 |
{ |
| 283 |
pjsip_regc *regc; |
| 284 |
unsigned i; |
| 285 |
const pj_str_t aor = pj_str("<sip:[email protected]>"); |
| 286 |
pjsip_tx_data *tdata; |
| 287 |
pj_status_t status; |
| 288 |
|
| 289 |
PJ_LOG(3,(THIS_FILE, " %s", title)); |
| 290 |
|
| 291 |
|
| 292 |
pj_memcpy(®istrar.cfg, srv_cfg, sizeof(*srv_cfg)); |
| 293 |
|
| 294 |
pj_bzero(&client_result, sizeof(client_result)); |
| 295 |
client_result.destroy_on_cb = client_cfg->destroy_on_cb; |
| 296 |
|
| 297 |
status = pjsip_regc_create(endpt, &client_result, &client_cb, ®c); |
| 298 |
if (status != PJ_SUCCESS) |
| 299 |
return -100; |
| 300 |
|
| 301 |
status = pjsip_regc_init(regc, registrar_uri, &aor, &aor, contact_cnt, |
| 302 |
contacts, expires ? expires : 60); |
| 303 |
if (status != PJ_SUCCESS) { |
| 304 |
pjsip_regc_destroy(regc); |
| 305 |
return -110; |
| 306 |
} |
| 307 |
|
| 308 |
if (client_cfg->auth) { |
| 309 |
pjsip_cred_info cred; |
| 310 |
|
| 311 |
pj_bzero(&cred, sizeof(cred)); |
| 312 |
cred.realm = pj_str("*"); |
| 313 |
cred.scheme = pj_str("digest"); |
| 314 |
cred.username = pj_str("user"); |
| 315 |
cred.data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 316 |
cred.data = pj_str("password"); |
| 317 |
|
| 318 |
status = pjsip_regc_set_credentials(regc, 1, &cred); |
| 319 |
if (status != PJ_SUCCESS) { |
| 320 |
pjsip_regc_destroy(regc); |
| 321 |
return -115; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
status = pjsip_regc_register(regc, PJ_TRUE, &tdata); |
| 327 |
if (status != PJ_SUCCESS) { |
| 328 |
pjsip_regc_destroy(regc); |
| 329 |
return -120; |
| 330 |
} |
| 331 |
|
| 332 |
pjsip_tx_data_add_ref(tdata); |
| 333 |
|
| 334 |
status = pjsip_regc_send(regc, tdata); |
| 335 |
|
| 336 |
|
| 337 |
for (i=0; i<600 && !client_result.done; ++i) { |
| 338 |
flush_events(100); |
| 339 |
} |
| 340 |
|
| 341 |
if (!client_result.done) { |
| 342 |
PJ_LOG(3,(THIS_FILE, " error: test has timed out")); |
| 343 |
pjsip_regc_destroy(regc); |
| 344 |
pjsip_tx_data_dec_ref(tdata); |
| 345 |
return -200; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
if (!leave_session && !client_cfg->destroy_on_cb) |
| 352 |
pjsip_regc_destroy(regc); |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
if (client_result.error != client_cfg->error) { |
| 357 |
PJ_LOG(3,(THIS_FILE, " error: expecting err=%d, got err=%d", |
| 358 |
client_cfg->error, client_result.error)); |
| 359 |
pjsip_tx_data_dec_ref(tdata); |
| 360 |
return -210; |
| 361 |
} |
| 362 |
if (client_result.code != client_cfg->code && |
| 363 |
client_cfg->code != 502 && client_cfg->code != 503 && |
| 364 |
client_result.code != 502 && client_result.code != 503) |
| 365 |
{ |
| 366 |
PJ_LOG(3,(THIS_FILE, " error: expecting code=%d, got code=%d", |
| 367 |
client_cfg->code, client_result.code)); |
| 368 |
pjsip_tx_data_dec_ref(tdata); |
| 369 |
return -220; |
| 370 |
} |
| 371 |
if (client_result.expiration != client_cfg->expiration) { |
| 372 |
PJ_LOG(3,(THIS_FILE, " error: expecting expiration=%d, got expiration=%d", |
| 373 |
client_cfg->expiration, client_result.expiration)); |
| 374 |
pjsip_tx_data_dec_ref(tdata); |
| 375 |
return -240; |
| 376 |
} |
| 377 |
if (client_result.contact_cnt != client_cfg->contact_cnt) { |
| 378 |
PJ_LOG(3,(THIS_FILE, " error: expecting contact_cnt=%d, got contact_cnt=%d", |
| 379 |
client_cfg->contact_cnt, client_result.contact_cnt)); |
| 380 |
pjsip_tx_data_dec_ref(tdata); |
| 381 |
return -250; |
| 382 |
} |
| 383 |
if (client_result.have_reg != client_cfg->have_reg) { |
| 384 |
PJ_LOG(3,(THIS_FILE, " error: expecting have_reg=%d, got have_reg=%d", |
| 385 |
client_cfg->have_reg, client_result.have_reg)); |
| 386 |
pjsip_tx_data_dec_ref(tdata); |
| 387 |
return -260; |
| 388 |
} |
| 389 |
if (client_result.have_reg && client_result.interval != client_result.expiration) { |
| 390 |
PJ_LOG(3,(THIS_FILE, " error: interval (%d) is different than expiration (%d)", |
| 391 |
client_result.interval, client_result.expiration)); |
| 392 |
pjsip_tx_data_dec_ref(tdata); |
| 393 |
return -270; |
| 394 |
} |
| 395 |
if (client_result.interval > 0 && client_result.next_reg < 1) { |
| 396 |
PJ_LOG(3,(THIS_FILE, " error: next_reg=%d, expecting positive number because interval is %d", |
| 397 |
client_result.next_reg, client_result.interval)); |
| 398 |
pjsip_tx_data_dec_ref(tdata); |
| 399 |
return -280; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
if (leave_session) { |
| 404 |
*p_regc = regc; |
| 405 |
} |
| 406 |
|
| 407 |
pjsip_tx_data_dec_ref(tdata); |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
static int keep_alive_test(const pj_str_t *registrar_uri) |
| 417 |
{ |
| 418 |
enum { TIMEOUT = 40 }; |
| 419 |
struct registrar_cfg server_cfg = |
| 420 |
|
| 421 |
{ PJ_TRUE, 200, PJ_FALSE, EXACT, TIMEOUT, 0, {NULL, 0}}; |
| 422 |
struct client client_cfg = |
| 423 |
|
| 424 |
{ PJ_FALSE, 200, PJ_TRUE, TIMEOUT, 1, PJ_FALSE,PJ_FALSE}; |
| 425 |
pj_str_t contact = pj_str("<sip:c@C>"); |
| 426 |
|
| 427 |
|
| 428 |
pjsip_regc *regc; |
| 429 |
unsigned i; |
| 430 |
int ret; |
| 431 |
|
| 432 |
ret = do_test("register refresh (takes ~40 secs)", &server_cfg, &client_cfg, registrar_uri, |
| 433 |
1, &contact, TIMEOUT, PJ_TRUE, ®c); |
| 434 |
if (ret != 0) |
| 435 |
return ret; |
| 436 |
|
| 437 |
|
| 438 |
registrar.response_cnt = 0; |
| 439 |
|
| 440 |
|
| 441 |
for (i=0; i<(TIMEOUT-1)*10 && registrar.response_cnt==0; ++i) { |
| 442 |
flush_events(100); |
| 443 |
} |
| 444 |
|
| 445 |
if (registrar.response_cnt==0) { |
| 446 |
PJ_LOG(3,(THIS_FILE, " error: no refresh is received")); |
| 447 |
return -400; |
| 448 |
} |
| 449 |
|
| 450 |
if (client_result.error) { |
| 451 |
PJ_LOG(3,(THIS_FILE, " error: got error")); |
| 452 |
return -410; |
| 453 |
} |
| 454 |
if (client_result.code != 200) { |
| 455 |
PJ_LOG(3,(THIS_FILE, " error: expecting code=%d, got code=%d", |
| 456 |
200, client_result.code)); |
| 457 |
return -420; |
| 458 |
} |
| 459 |
if (client_result.expiration != TIMEOUT) { |
| 460 |
PJ_LOG(3,(THIS_FILE, " error: expecting expiration=%d, got expiration=%d", |
| 461 |
TIMEOUT, client_result.expiration)); |
| 462 |
return -440; |
| 463 |
} |
| 464 |
if (client_result.contact_cnt != 1) { |
| 465 |
PJ_LOG(3,(THIS_FILE, " error: expecting contact_cnt=%d, got contact_cnt=%d", |
| 466 |
TIMEOUT, client_result.contact_cnt)); |
| 467 |
return -450; |
| 468 |
} |
| 469 |
if (client_result.have_reg == 0) { |
| 470 |
PJ_LOG(3,(THIS_FILE, " error: expecting have_reg=%d, got have_reg=%d", |
| 471 |
1, client_result.have_reg)); |
| 472 |
return -460; |
| 473 |
} |
| 474 |
if (client_result.interval != TIMEOUT) { |
| 475 |
PJ_LOG(3,(THIS_FILE, " error: interval (%d) is different than expiration (%d)", |
| 476 |
client_result.interval, TIMEOUT)); |
| 477 |
return -470; |
| 478 |
} |
| 479 |
if (client_result.expiration > 0 && client_result.next_reg < 1) { |
| 480 |
PJ_LOG(3,(THIS_FILE, " error: next_reg=%d, expecting positive number because expiration is %d", |
| 481 |
client_result.next_reg, client_result.expiration)); |
| 482 |
return -480; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
pjsip_regc_destroy(regc); |
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
static int refresh_error(const pj_str_t *registrar_uri, |
| 493 |
pj_bool_t destroy_on_cb) |
| 494 |
{ |
| 495 |
enum { TIMEOUT = 40 }; |
| 496 |
struct registrar_cfg server_cfg = |
| 497 |
|
| 498 |
{ PJ_TRUE, 200, PJ_FALSE, EXACT, TIMEOUT, 0, {NULL, 0}}; |
| 499 |
struct client client_cfg = |
| 500 |
|
| 501 |
{ PJ_FALSE, 200, PJ_TRUE, TIMEOUT, 1, PJ_FALSE,PJ_FALSE}; |
| 502 |
pj_str_t contact = pj_str("<sip:c@C>"); |
| 503 |
|
| 504 |
pjsip_regc *regc; |
| 505 |
unsigned i; |
| 506 |
int ret; |
| 507 |
|
| 508 |
ret = do_test("refresh error (takes ~40 secs)", &server_cfg, &client_cfg, registrar_uri, |
| 509 |
1, &contact, TIMEOUT, PJ_TRUE, ®c); |
| 510 |
if (ret != 0) |
| 511 |
return ret; |
| 512 |
|
| 513 |
|
| 514 |
registrar.response_cnt = 0; |
| 515 |
|
| 516 |
|
| 517 |
send_mod.count = 0; |
| 518 |
send_mod.count_before_reject = 0; |
| 519 |
|
| 520 |
|
| 521 |
client_result.done = PJ_FALSE; |
| 522 |
client_result.destroy_on_cb = destroy_on_cb; |
| 523 |
|
| 524 |
|
| 525 |
for (i=0; i<TIMEOUT*10 && !client_result.done; ++i) { |
| 526 |
flush_events(100); |
| 527 |
} |
| 528 |
|
| 529 |
send_mod.count_before_reject = 0xFFFF; |
| 530 |
|
| 531 |
if (!destroy_on_cb) |
| 532 |
pjsip_regc_destroy(regc); |
| 533 |
|
| 534 |
if (!client_result.done) { |
| 535 |
PJ_LOG(3,(THIS_FILE, " error: test has timed out")); |
| 536 |
return -500; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
if (client_result.error==PJ_FALSE && client_result.code/100==2) { |
| 541 |
PJ_LOG(3,(THIS_FILE, " error: expecting error got successfull result")); |
| 542 |
return -510; |
| 543 |
} |
| 544 |
|
| 545 |
return PJ_SUCCESS; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
static int update_test(const pj_str_t *registrar_uri) |
| 551 |
{ |
| 552 |
enum { TIMEOUT = 40 }; |
| 553 |
struct registrar_cfg server_cfg = |
| 554 |
|
| 555 |
{ PJ_TRUE, 200, PJ_FALSE, EXACT, TIMEOUT, 0, {NULL, 0}}; |
| 556 |
struct client client_cfg = |
| 557 |
|
| 558 |
{ PJ_FALSE, 200, PJ_TRUE, TIMEOUT, 1, PJ_FALSE,PJ_FALSE}; |
| 559 |
pj_str_t contacts[] = { |
| 560 |
{ "<sip:a>", 7 }, |
| 561 |
{ "<sip:b>", 7 }, |
| 562 |
{ "<sip:c>", 7 } |
| 563 |
}; |
| 564 |
|
| 565 |
pjsip_regc *regc; |
| 566 |
pjsip_contact_hdr *h1, *h2; |
| 567 |
pjsip_sip_uri *u1; |
| 568 |
unsigned i; |
| 569 |
pj_status_t status; |
| 570 |
pjsip_tx_data *tdata = NULL; |
| 571 |
int ret = 0; |
| 572 |
|
| 573 |
|
| 574 |
ret = do_test("update test", &server_cfg, &client_cfg, registrar_uri, |
| 575 |
1, &contacts[0], TIMEOUT, PJ_TRUE, ®c); |
| 576 |
if (ret != 0) { |
| 577 |
return -600; |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
PJ_LOG(3,(THIS_FILE, " replacing contact")); |
| 584 |
status = pjsip_regc_update_contact(regc, 1, &contacts[1]); |
| 585 |
if (status != PJ_SUCCESS) { |
| 586 |
ret = -610; |
| 587 |
goto on_return; |
| 588 |
} |
| 589 |
|
| 590 |
status = pjsip_regc_register(regc, PJ_TRUE, &tdata); |
| 591 |
if (status != PJ_SUCCESS) { |
| 592 |
ret = -620; |
| 593 |
goto on_return; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
h1 = (pjsip_contact_hdr*) |
| 601 |
pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, NULL); |
| 602 |
if (!h1) { |
| 603 |
ret = -630; |
| 604 |
goto on_return; |
| 605 |
} |
| 606 |
if ((void*)h1->next == (void*)&tdata->msg->hdr) |
| 607 |
h2 = NULL; |
| 608 |
else |
| 609 |
h2 = (pjsip_contact_hdr*) |
| 610 |
pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, h1->next); |
| 611 |
if (!h2) { |
| 612 |
ret = -640; |
| 613 |
goto on_return; |
| 614 |
} |
| 615 |
|
| 616 |
if ((void*)h2->next != (void*)&tdata->msg->hdr && |
| 617 |
pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, h2->next) != NULL) |
| 618 |
{ |
| 619 |
ret = -645; |
| 620 |
goto on_return; |
| 621 |
} |
| 622 |
|
| 623 |
u1 = (pjsip_sip_uri*) pjsip_uri_get_uri(h1->uri); |
| 624 |
|
| 625 |
if (*u1->host.ptr == 'a') { |
| 626 |
if (h1->expires != 0) { |
| 627 |
ret = -650; |
| 628 |
goto on_return; |
| 629 |
} |
| 630 |
if (h2->expires == 0) { |
| 631 |
ret = -660; |
| 632 |
goto on_return; |
| 633 |
} |
| 634 |
|
| 635 |
} else { |
| 636 |
pj_assert(*u1->host.ptr == 'b'); |
| 637 |
if (h1->expires == 0) { |
| 638 |
ret = -670; |
| 639 |
goto on_return; |
| 640 |
} |
| 641 |
if (h2->expires != 0) { |
| 642 |
ret = -680; |
| 643 |
goto on_return; |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
pjsip_tx_data_dec_ref(tdata); |
| 649 |
tdata = NULL; |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
for (i=0; i<2; ++i) { |
| 658 |
if (i==0) |
| 659 |
PJ_LOG(3,(THIS_FILE, " replacing with more contacts")); |
| 660 |
else |
| 661 |
PJ_LOG(3,(THIS_FILE, " updating contacts with same contacts")); |
| 662 |
|
| 663 |
status = pjsip_regc_update_contact(regc, 2, &contacts[1]); |
| 664 |
if (status != PJ_SUCCESS) { |
| 665 |
ret = -710; |
| 666 |
goto on_return; |
| 667 |
} |
| 668 |
|
| 669 |
status = pjsip_regc_register(regc, PJ_TRUE, &tdata); |
| 670 |
if (status != PJ_SUCCESS) { |
| 671 |
ret = -720; |
| 672 |
goto on_return; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
h1 = (pjsip_contact_hdr*) |
| 680 |
pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, NULL); |
| 681 |
if (!h1) { |
| 682 |
ret = -730; |
| 683 |
goto on_return; |
| 684 |
} |
| 685 |
if ((void*)h1->next == (void*)&tdata->msg->hdr) |
| 686 |
h2 = NULL; |
| 687 |
else |
| 688 |
h2 = (pjsip_contact_hdr*) |
| 689 |
pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, h1->next); |
| 690 |
if (!h2) { |
| 691 |
ret = -740; |
| 692 |
goto on_return; |
| 693 |
} |
| 694 |
|
| 695 |
if ((void*)h2->next != (void*)&tdata->msg->hdr && |
| 696 |
pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, h2->next) != NULL) |
| 697 |
{ |
| 698 |
ret = -745; |
| 699 |
goto on_return; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
if (h1->expires == 0) { |
| 704 |
ret = -750; |
| 705 |
goto on_return; |
| 706 |
} |
| 707 |
if (h2->expires == 0) { |
| 708 |
ret = -760; |
| 709 |
goto on_return; |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
pjsip_tx_data_dec_ref(tdata); |
| 714 |
tdata = NULL; |
| 715 |
} |
| 716 |
|
| 717 |
on_return: |
| 718 |
if (tdata) pjsip_tx_data_dec_ref(tdata); |
| 719 |
pjsip_regc_destroy(regc); |
| 720 |
return ret; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
static int auth_send_error(const pj_str_t *registrar_uri, |
| 726 |
pj_bool_t destroy_on_cb) |
| 727 |
{ |
| 728 |
enum { TIMEOUT = 40 }; |
| 729 |
struct registrar_cfg server_cfg = |
| 730 |
|
| 731 |
{ PJ_TRUE, 200, PJ_TRUE, EXACT, 75, 0, {NULL, 0}}; |
| 732 |
struct client client_cfg = |
| 733 |
|
| 734 |
{ PJ_TRUE, 401, PJ_FALSE, TIMEOUT, 0, PJ_TRUE, PJ_TRUE}; |
| 735 |
pj_str_t contact = pj_str("<sip:c@C>"); |
| 736 |
|
| 737 |
pjsip_regc *regc; |
| 738 |
int ret; |
| 739 |
|
| 740 |
client_cfg.destroy_on_cb = destroy_on_cb; |
| 741 |
|
| 742 |
|
| 743 |
send_mod.count = 0; |
| 744 |
send_mod.count_before_reject = 1; |
| 745 |
|
| 746 |
ret = do_test("auth send error", &server_cfg, &client_cfg, registrar_uri, |
| 747 |
1, &contact, TIMEOUT, PJ_TRUE, ®c); |
| 748 |
|
| 749 |
send_mod.count_before_reject = 0xFFFF; |
| 750 |
|
| 751 |
return ret; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
enum |
| 759 |
{ |
| 760 |
OFF = 1, |
| 761 |
ON = 2, |
| 762 |
ON_OFF = 3, |
| 763 |
}; |
| 764 |
|
| 765 |
int regc_test(void) |
| 766 |
{ |
| 767 |
struct test_rec { |
| 768 |
unsigned check_contact; |
| 769 |
unsigned add_xuid_param; |
| 770 |
|
| 771 |
const char *title; |
| 772 |
char *alt_registrar; |
| 773 |
unsigned contact_cnt; |
| 774 |
char *contacts[4]; |
| 775 |
unsigned expires; |
| 776 |
struct registrar_cfg server_cfg; |
| 777 |
struct client client_cfg; |
| 778 |
} test_rec[] = |
| 779 |
{ |
| 780 |
|
| 781 |
{ |
| 782 |
OFF, |
| 783 |
OFF, |
| 784 |
"immediate error", |
| 785 |
"sip:unresolved-host-xyy", |
| 786 |
1, |
| 787 |
{ "sip:[email protected]:5060" }, |
| 788 |
600, |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
{ PJ_FALSE, 200, PJ_FALSE, NONE, 0, 0, {NULL, 0}}, |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
{ PJ_FALSE, 502, PJ_FALSE, 600, 0, PJ_FALSE} |
| 797 |
}, |
| 798 |
|
| 799 |
|
| 800 |
{ |
| 801 |
OFF, |
| 802 |
OFF, |
| 803 |
"timeout test (takes ~32 secs)", |
| 804 |
NULL, |
| 805 |
1, |
| 806 |
{ "sip:[email protected]:5060" }, |
| 807 |
600, |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
{ PJ_FALSE, 200, PJ_FALSE, NONE, 0, 0, {NULL, 0}}, |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
{ PJ_FALSE, 408, PJ_FALSE, 600, 0, PJ_FALSE} |
| 816 |
}, |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
{ |
| 824 |
ON_OFF, |
| 825 |
ON_OFF, |
| 826 |
"basic", |
| 827 |
NULL, |
| 828 |
1, |
| 829 |
{ "<sip:[email protected]:5060;transport=udp;x-param=1234>" }, |
| 830 |
600, |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
{ PJ_TRUE, 200, PJ_FALSE, EXACT, 75, 65, {NULL, 0}}, |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
{ PJ_FALSE, 200, PJ_TRUE, 75, 1, PJ_FALSE} |
| 839 |
}, |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
{ |
| 844 |
ON_OFF, |
| 845 |
ON_OFF, |
| 846 |
"authentication", |
| 847 |
NULL, |
| 848 |
1, |
| 849 |
{ "<sip:[email protected]:5060;transport=udp;x-param=1234>" }, |
| 850 |
600, |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
{ PJ_TRUE, 200, PJ_TRUE, EXACT, 75, 65, {NULL, 0}}, |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
{ PJ_FALSE, 200, PJ_TRUE, 75, 1, PJ_TRUE} |
| 859 |
}, |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
{ |
| 866 |
ON_OFF, |
| 867 |
ON, |
| 868 |
"more bindings in response", |
| 869 |
NULL, |
| 870 |
1, |
| 871 |
{ "<sip:[email protected]:5060;transport=udp>" }, |
| 872 |
600, |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
{ PJ_TRUE, 200, PJ_FALSE, EXACT, 75, 65, {"<sip:a@a>;expires=70", 0}}, |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
{ PJ_FALSE, 200, PJ_TRUE, 75, 2, PJ_FALSE} |
| 881 |
}, |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
{ |
| 891 |
ON_OFF, |
| 892 |
ON_OFF, |
| 893 |
"registrar modifies Contact header", |
| 894 |
NULL, |
| 895 |
1, |
| 896 |
{ "<sip:[email protected]:5060>" }, |
| 897 |
600, |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
{ PJ_TRUE, 200, PJ_FALSE, MODIFIED, 75, 65, {NULL, 0}}, |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
{ PJ_FALSE, 200, PJ_TRUE, 75, 1, PJ_FALSE} |
| 906 |
}, |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
{ |
| 917 |
ON_OFF, |
| 918 |
ON, |
| 919 |
"registrar modifies Contact header and add bindings", |
| 920 |
NULL, |
| 921 |
1, |
| 922 |
{ "<sip:[email protected]:5060>" }, |
| 923 |
600, |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
{ PJ_TRUE, 200, PJ_FALSE, MODIFIED, 75, 65, {"<sip:a@a>;expires=70", 0}}, |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
{ PJ_FALSE, 200, PJ_TRUE, 75, 2, PJ_FALSE} |
| 932 |
}, |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
{ |
| 941 |
ON_OFF, |
| 942 |
ON_OFF, |
| 943 |
"registrar replaces Contact header", |
| 944 |
NULL, |
| 945 |
1, |
| 946 |
{ "<sip:[email protected]:5060>" }, |
| 947 |
600, |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
{ PJ_TRUE, 202, PJ_FALSE, NONE, 0, 65, {"<sip:a@A>;expires=75", 0}}, |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
{ PJ_FALSE, 202, PJ_TRUE, 75, 1, PJ_FALSE} |
| 956 |
}, |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
{ |
| 965 |
ON_OFF, |
| 966 |
ON_OFF, |
| 967 |
" as above with additional bindings", |
| 968 |
NULL, |
| 969 |
1, |
| 970 |
{ "<sip:[email protected]:5060>" }, |
| 971 |
600, |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
{ PJ_TRUE, 200, PJ_FALSE, NONE, 0, 65, {"<sip:a@A>;expires=75, <sip:b@B;expires=70>", 0}}, |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
{ PJ_FALSE, 200, PJ_TRUE, 65, 2, PJ_FALSE} |
| 980 |
}, |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
{ |
| 987 |
ON_OFF, |
| 988 |
ON_OFF, |
| 989 |
"no Contact but with Expires", |
| 990 |
NULL, |
| 991 |
1, |
| 992 |
{ "<sip:[email protected]:5060>" }, |
| 993 |
600, |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
{ PJ_TRUE, 200, PJ_FALSE, NONE, 0, 65, {NULL, 0}}, |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
{ PJ_FALSE, 200, PJ_TRUE, 65, 0, PJ_FALSE} |
| 1002 |
}, |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
{ |
| 1008 |
ON_OFF, |
| 1009 |
ON_OFF, |
| 1010 |
"no Contact and no Expires", |
| 1011 |
NULL, |
| 1012 |
1, |
| 1013 |
{ "<sip:[email protected]:5060>" }, |
| 1014 |
600, |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
{ PJ_TRUE, 200, PJ_FALSE, NONE, 0, 0, {NULL, 0}}, |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
{ PJ_FALSE, 200, PJ_TRUE, 600, 0, PJ_FALSE} |
| 1023 |
}, |
| 1024 |
}; |
| 1025 |
|
| 1026 |
unsigned i; |
| 1027 |
pj_sockaddr_in addr; |
| 1028 |
pjsip_transport *udp = NULL; |
| 1029 |
pj_uint16_t port; |
| 1030 |
char registrar_uri_buf[80]; |
| 1031 |
pj_str_t registrar_uri; |
| 1032 |
int rc = 0; |
| 1033 |
|
| 1034 |
pj_sockaddr_in_init(&addr, 0, 0); |
| 1035 |
|
| 1036 |
|
| 1037 |
rc = pjsip_endpt_acquire_transport(endpt, PJSIP_TRANSPORT_UDP, &addr, sizeof(addr), NULL, &udp); |
| 1038 |
if (rc == PJ_SUCCESS) { |
| 1039 |
port = pj_sockaddr_get_port(&udp->local_addr); |
| 1040 |
pjsip_transport_dec_ref(udp); |
| 1041 |
udp = NULL; |
| 1042 |
} else { |
| 1043 |
rc = pjsip_udp_transport_start(endpt, NULL, NULL, 1, &udp); |
| 1044 |
if (rc != PJ_SUCCESS) { |
| 1045 |
app_perror(" error creating UDP transport", rc); |
| 1046 |
rc = -2; |
| 1047 |
goto on_return; |
| 1048 |
} |
| 1049 |
|
| 1050 |
port = pj_sockaddr_get_port(&udp->local_addr); |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
rc = pjsip_endpt_register_module(endpt, ®istrar.mod); |
| 1055 |
if (rc != PJ_SUCCESS) { |
| 1056 |
app_perror(" error registering module", rc); |
| 1057 |
rc = -3; |
| 1058 |
goto on_return; |
| 1059 |
} |
| 1060 |
|
| 1061 |
|
| 1062 |
rc = pjsip_endpt_register_module(endpt, &send_mod.mod); |
| 1063 |
if (rc != PJ_SUCCESS) { |
| 1064 |
app_perror(" error registering module", rc); |
| 1065 |
rc = -3; |
| 1066 |
goto on_return; |
| 1067 |
} |
| 1068 |
|
| 1069 |
pj_ansi_snprintf(registrar_uri_buf, sizeof(registrar_uri_buf), |
| 1070 |
"sip:127.0.0.1:%d", (int)port); |
| 1071 |
registrar_uri = pj_str(registrar_uri_buf); |
| 1072 |
|
| 1073 |
for (i=0; i<PJ_ARRAY_SIZE(test_rec); ++i) { |
| 1074 |
struct test_rec *t = &test_rec[i]; |
| 1075 |
unsigned j, x; |
| 1076 |
pj_str_t reg_uri; |
| 1077 |
pj_str_t contacts[8]; |
| 1078 |
|
| 1079 |
|
| 1080 |
if (t->alt_registrar == NULL) { |
| 1081 |
reg_uri = registrar_uri; |
| 1082 |
} else { |
| 1083 |
reg_uri = pj_str(t->alt_registrar); |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
for (j=0; j<t->contact_cnt; ++j) { |
| 1088 |
contacts[j] = pj_str(t->contacts[j]); |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
if (t->server_cfg.more_contacts.ptr) |
| 1093 |
t->server_cfg.more_contacts.slen = strlen(t->server_cfg.more_contacts.ptr); |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
for (x=1; x<=2; ++x) { |
| 1101 |
unsigned y; |
| 1102 |
|
| 1103 |
if ((t->check_contact & x) == 0) |
| 1104 |
continue; |
| 1105 |
|
| 1106 |
pjsip_cfg()->regc.check_contact = (x-1); |
| 1107 |
|
| 1108 |
for (y=1; y<=2; ++y) { |
| 1109 |
unsigned z; |
| 1110 |
|
| 1111 |
if ((t->add_xuid_param & y) == 0) |
| 1112 |
continue; |
| 1113 |
|
| 1114 |
pjsip_cfg()->regc.add_xuid_param = (y-1); |
| 1115 |
|
| 1116 |
for (z=0; z<=1; ++z) { |
| 1117 |
char new_title[200]; |
| 1118 |
|
| 1119 |
t->client_cfg.destroy_on_cb = z; |
| 1120 |
|
| 1121 |
pj_ansi_snprintf(new_title, sizeof(new_title), |
| 1122 |
"%s [check=%d, xuid=%d, destroy=%d]", |
| 1123 |
t->title, pjsip_cfg()->regc.check_contact, |
| 1124 |
pjsip_cfg()->regc.add_xuid_param, z); |
| 1125 |
rc = do_test(new_title, &t->server_cfg, &t->client_cfg, |
| 1126 |
®_uri, t->contact_cnt, contacts, |
| 1127 |
t->expires, PJ_FALSE, NULL); |
| 1128 |
if (rc != 0) |
| 1129 |
goto on_return; |
| 1130 |
} |
| 1131 |
|
| 1132 |
} |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
pj_thread_sleep(1000); |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
rc = keep_alive_test(®istrar_uri); |
| 1143 |
if (rc != 0) |
| 1144 |
goto on_return; |
| 1145 |
|
| 1146 |
|
| 1147 |
rc = refresh_error(®istrar_uri, PJ_FALSE); |
| 1148 |
if (rc != 0) |
| 1149 |
goto on_return; |
| 1150 |
|
| 1151 |
|
| 1152 |
rc = refresh_error(®istrar_uri, PJ_TRUE); |
| 1153 |
if (rc != 0) |
| 1154 |
goto on_return; |
| 1155 |
|
| 1156 |
|
| 1157 |
rc = update_test(®istrar_uri); |
| 1158 |
if (rc != 0) |
| 1159 |
goto on_return; |
| 1160 |
|
| 1161 |
|
| 1162 |
rc = auth_send_error(®istrar_uri, PJ_FALSE); |
| 1163 |
if (rc != 0) |
| 1164 |
goto on_return; |
| 1165 |
|
| 1166 |
|
| 1167 |
rc = auth_send_error(®istrar_uri, PJ_FALSE); |
| 1168 |
if (rc != 0) |
| 1169 |
goto on_return; |
| 1170 |
|
| 1171 |
on_return: |
| 1172 |
if (registrar.mod.id != -1) { |
| 1173 |
pjsip_endpt_unregister_module(endpt, ®istrar.mod); |
| 1174 |
} |
| 1175 |
if (send_mod.mod.id != -1) { |
| 1176 |
pjsip_endpt_unregister_module(endpt, &send_mod.mod); |
| 1177 |
} |
| 1178 |
if (udp) { |
| 1179 |
pjsip_transport_dec_ref(udp); |
| 1180 |
} |
| 1181 |
return rc; |
| 1182 |
} |
| 1183 |
|
| 1184 |
|