| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include "test.h" |
| 37 |
#include <pjsua-lib/pjsua.h> |
| 38 |
#include <pjsua-lib/pjsua_internal.h> |
| 39 |
#include <pjsip.h> |
| 40 |
#include <pjsip/sip_transaction.h> |
| 41 |
#include <pjlib.h> |
| 42 |
|
| 43 |
#define THIS_FILE "pjsua_auth_test.c" |
| 44 |
|
| 45 |
|
| 46 |
#define TEST_USER "pjsua-auth-test" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static pj_bool_t mock_registrar_rx_request(pjsip_rx_data *rdata); |
| 58 |
|
| 59 |
static struct { |
| 60 |
pjsip_module mod; |
| 61 |
unsigned req_count; |
| 62 |
unsigned auth_count; |
| 63 |
} g_mock_reg = { |
| 64 |
{ |
| 65 |
NULL, NULL, |
| 66 |
{ "mod-pjsua-auth-reg", 19 }, |
| 67 |
-1, |
| 68 |
PJSIP_MOD_PRIORITY_APPLICATION, |
| 69 |
NULL, NULL, NULL, NULL, |
| 70 |
&mock_registrar_rx_request, |
| 71 |
NULL, NULL, NULL, NULL, |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
static pj_bool_t mock_registrar_rx_request(pjsip_rx_data *rdata) |
| 76 |
{ |
| 77 |
pjsip_msg *msg = rdata->msg_info.msg; |
| 78 |
pjsip_hdr hdr_list; |
| 79 |
int code; |
| 80 |
|
| 81 |
if (msg->line.req.method.id != PJSIP_REGISTER_METHOD || |
| 82 |
!is_user_equal(rdata->msg_info.from, TEST_USER)) |
| 83 |
{ |
| 84 |
return PJ_FALSE; |
| 85 |
} |
| 86 |
|
| 87 |
g_mock_reg.req_count++; |
| 88 |
pj_list_init(&hdr_list); |
| 89 |
|
| 90 |
if (pjsip_msg_find_hdr(msg, PJSIP_H_AUTHORIZATION, NULL) == NULL) { |
| 91 |
const pj_str_t hname = pj_str("WWW-Authenticate"); |
| 92 |
const pj_str_t hvalue = |
| 93 |
pj_str("Digest realm=\"test\", nonce=\"testnonce\""); |
| 94 |
pjsip_generic_string_hdr *hwww = |
| 95 |
pjsip_generic_string_hdr_create(rdata->tp_info.pool, |
| 96 |
&hname, &hvalue); |
| 97 |
pj_list_push_back(&hdr_list, hwww); |
| 98 |
code = 401; |
| 99 |
} else { |
| 100 |
g_mock_reg.auth_count++; |
| 101 |
code = 200; |
| 102 |
} |
| 103 |
|
| 104 |
pjsip_endpt_respond_stateless(pjsua_get_pjsip_endpt(), rdata, |
| 105 |
code, NULL, &hdr_list, NULL); |
| 106 |
return PJ_TRUE; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
typedef struct { |
| 115 |
pj_bool_t pending; |
| 116 |
pjsip_auth_clt_sess *auth_sess; |
| 117 |
void *token; |
| 118 |
pjsip_tx_data *tdata; |
| 119 |
pjsip_rx_data *cloned_rdata; |
| 120 |
pjsua_acc_id acc_id; |
| 121 |
} deferred_state_t; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
typedef enum { |
| 129 |
ACTION_RESPOND, |
| 130 |
ACTION_RESPOND_CREDS, |
| 131 |
ACTION_ABANDON, |
| 132 |
ACTION_DELETE_ACC |
| 133 |
} deferred_action_t; |
| 134 |
|
| 135 |
typedef struct { |
| 136 |
deferred_action_t action; |
| 137 |
deferred_state_t deferred; |
| 138 |
pj_bool_t done; |
| 139 |
int reg_code; |
| 140 |
unsigned challenge_count; |
| 141 |
pj_bool_t defer_enabled; |
| 142 |
} test_ctx_t; |
| 143 |
|
| 144 |
static test_ctx_t g_ctx; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static void on_auth_challenge(pjsua_on_auth_challenge_param *param) |
| 152 |
{ |
| 153 |
test_ctx_t *ctx = &g_ctx; |
| 154 |
deferred_state_t *ds = &ctx->deferred; |
| 155 |
pjsip_rx_data *cloned = NULL; |
| 156 |
pj_status_t status; |
| 157 |
|
| 158 |
ctx->challenge_count++; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if (!ctx->defer_enabled || ds->pending) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
status = pjsip_rx_data_clone(param->rdata, 0, &cloned); |
| 169 |
if (status != PJ_SUCCESS) { |
| 170 |
PJ_LOG(1, (THIS_FILE, " pjsip_rx_data_clone failed: %d", status)); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
pjsip_tx_data_add_ref(param->tdata); |
| 175 |
|
| 176 |
ds->auth_sess = param->auth_sess; |
| 177 |
ds->token = param->token; |
| 178 |
ds->tdata = param->tdata; |
| 179 |
ds->cloned_rdata = cloned; |
| 180 |
ds->acc_id = param->acc_id; |
| 181 |
ds->pending = PJ_TRUE; |
| 182 |
|
| 183 |
param->handled = PJ_TRUE; |
| 184 |
} |
| 185 |
|
| 186 |
static void on_reg_state2(pjsua_acc_id acc_id, pjsua_reg_info *info) |
| 187 |
{ |
| 188 |
PJ_UNUSED_ARG(acc_id); |
| 189 |
g_ctx.done = PJ_TRUE; |
| 190 |
g_ctx.reg_code = info->cbparam->code; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
static pj_status_t do_deferred_respond(deferred_state_t *ds) |
| 200 |
{ |
| 201 |
pjsip_tx_data *new_tdata = NULL; |
| 202 |
pj_status_t status; |
| 203 |
|
| 204 |
PJSUA_LOCK(); |
| 205 |
|
| 206 |
if (!pjsua_acc_is_valid(ds->acc_id)) { |
| 207 |
PJSUA_UNLOCK(); |
| 208 |
return PJ_EINVALIDOP; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
PJSUA_UNLOCK(); |
| 215 |
|
| 216 |
status = pjsip_auth_clt_reinit_req(ds->auth_sess, |
| 217 |
ds->cloned_rdata, |
| 218 |
ds->tdata, |
| 219 |
&new_tdata); |
| 220 |
if (status != PJ_SUCCESS || !new_tdata) { |
| 221 |
|
| 222 |
pjsip_auth_clt_async_abandon(ds->auth_sess, ds->token); |
| 223 |
goto cleanup; |
| 224 |
} |
| 225 |
|
| 226 |
status = pjsip_auth_clt_async_send_req(ds->auth_sess, ds->token, |
| 227 |
new_tdata); |
| 228 |
|
| 229 |
cleanup: |
| 230 |
pjsip_tx_data_dec_ref(ds->tdata); |
| 231 |
pjsip_rx_data_free_cloned(ds->cloned_rdata); |
| 232 |
ds->tdata = NULL; |
| 233 |
ds->cloned_rdata = NULL; |
| 234 |
ds->pending = PJ_FALSE; |
| 235 |
return status; |
| 236 |
} |
| 237 |
|
| 238 |
static pj_status_t do_deferred_respond_creds(deferred_state_t *ds) |
| 239 |
{ |
| 240 |
pjsip_cred_info cred; |
| 241 |
pj_status_t status; |
| 242 |
|
| 243 |
|
| 244 |
pj_bzero(&cred, sizeof(cred)); |
| 245 |
cred.realm = pj_str("*"); |
| 246 |
cred.scheme = pj_str("digest"); |
| 247 |
cred.username = pj_str(TEST_USER); |
| 248 |
cred.data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 249 |
cred.data = pj_str("secret"); |
| 250 |
|
| 251 |
PJSUA_LOCK(); |
| 252 |
if (!pjsua_acc_is_valid(ds->acc_id)) { |
| 253 |
PJSUA_UNLOCK(); |
| 254 |
return PJ_EINVALIDOP; |
| 255 |
} |
| 256 |
status = pjsip_auth_clt_set_credentials(ds->auth_sess, 1, &cred); |
| 257 |
PJSUA_UNLOCK(); |
| 258 |
|
| 259 |
if (status != PJ_SUCCESS) |
| 260 |
return status; |
| 261 |
|
| 262 |
return do_deferred_respond(ds); |
| 263 |
} |
| 264 |
|
| 265 |
static void do_deferred_abandon(deferred_state_t *ds) |
| 266 |
{ |
| 267 |
|
| 268 |
me |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
pjsip_tx_data_dec_ref(ds->tdata); |
| 274 |
pjsip_rx_data_free_cloned(ds->cloned_rdata); |
| 275 |
ds->tdata = NULL; |
| 276 |
ds->cloned_rdata = NULL; |
| 277 |
ds->pending = PJ_FALSE; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
#if 0 |
| 284 |
static void do_deferred_cleanup_after_acc_del(deferred_state_t *ds) |
| 285 |
{ |
| 286 |
pj_status_t lock_status; |
| 287 |
|
| 288 |
lock_status = PJSUA_TRY_LOCK(); |
| 289 |
if (lock_status == PJ_SUCCESS) { |
| 290 |
if (pjsua_acc_is_valid(ds->acc_id)) { |
| 291 |
|
| 292 |
|
| 293 |
pjsip_auth_clt_async_abandon(ds->auth_sess, ds->token); |
| 294 |
} |
| 295 |
|
| 296 |
PJSUA_UNLOCK(); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
pjsip_tx_data_dec_ref(ds->tdata); |
| 301 |
pjsip_rx_data_free_cloned(ds->cloned_rdata); |
| 302 |
ds->tdata = NULL; |
| 303 |
ds->cloned_rdata = NULL; |
| 304 |
ds->pending = PJ_FALSE; |
| 305 |
} |
| 306 |
#endif |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static int wait_and_service(unsigned max_ms) |
| 313 |
{ |
| 314 |
unsigned elapsed = 0; |
| 315 |
unsigned step = 50; |
| 316 |
|
| 317 |
while (elapsed < max_ms && !g_ctx.done) { |
| 318 |
pjsua_handle_events(step); |
| 319 |
elapsed += step; |
| 320 |
|
| 321 |
if (g_ctx.deferred.pending) { |
| 322 |
switch (g_ctx.action) { |
| 323 |
case ACTION_RESPOND: |
| 324 |
do_deferred_respond(&g_ctx.deferred); |
| 325 |
break; |
| 326 |
case ACTION_RESPOND_CREDS: |
| 327 |
do_deferred_respond_creds(&g_ctx.deferred); |
| 328 |
break; |
| 329 |
case ACTION_ABANDON: |
| 330 |
do_deferred_abandon(&g_ctx.deferred); |
| 331 |
break; |
| 332 |
case ACTION_DELETE_ACC: |
| 333 |
|
| 334 |
break; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
return g_ctx.done ? 0 : -1; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
static int deferred_respond_test(pjsua_transport_id tp_id, int port) |
| 349 |
{ |
| 350 |
pjsua_acc_config acc_cfg; |
| 351 |
pjsua_acc_id acc_id; |
| 352 |
char reg_uri[64]; |
| 353 |
pj_status_t status; |
| 354 |
int rc; |
| 355 |
|
| 356 |
PJ_LOG(3, (THIS_FILE, " pjsua auth: deferred respond")); |
| 357 |
|
| 358 |
pj_bzero(&g_ctx, sizeof(g_ctx)); |
| 359 |
g_ctx.action = ACTION_RESPOND; |
| 360 |
g_ctx.defer_enabled = PJ_TRUE; |
| 361 |
g_mock_reg.req_count = 0; |
| 362 |
g_mock_reg.auth_count = 0; |
| 363 |
|
| 364 |
pj_ansi_snprintf(reg_uri, sizeof(reg_uri), |
| 365 |
"sip:127.0.0.1:%d", port); |
| 366 |
|
| 367 |
pjsua_acc_config_default(&acc_cfg); |
| 368 |
acc_cfg.id = pj_str("sip:" TEST_USER "@pjsip.org"); |
| 369 |
acc_cfg.reg_uri = pj_str(reg_uri); |
| 370 |
acc_cfg.cred_count = 1; |
| 371 |
pj_bzero(&acc_cfg.cred_info[0], sizeof(pjsip_cred_info)); |
| 372 |
acc_cfg.cred_info[0].realm = pj_str("*"); |
| 373 |
acc_cfg.cred_info[0].scheme = pj_str("digest"); |
| 374 |
acc_cfg.cred_info[0].username = pj_str(TEST_USER); |
| 375 |
acc_cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 376 |
acc_cfg.cred_info[0].data = pj_str("secret"); |
| 377 |
acc_cfg.allow_contact_rewrite = PJ_FALSE; |
| 378 |
acc_cfg.transport_id = tp_id; |
| 379 |
|
| 380 |
status = pjsua_acc_add(&acc_cfg, PJ_FALSE, &acc_id); |
| 381 |
if (status != PJ_SUCCESS) { |
| 382 |
PJ_LOG(3, (THIS_FILE, " error: pjsua_acc_add failed (%d)", status)); |
| 383 |
return -2010; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
rc = wait_and_service(30000); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
g_ctx.defer_enabled = PJ_FALSE; |
| 394 |
pjsua_acc_del(acc_id); |
| 395 |
pjsua_handle_events(500); |
| 396 |
|
| 397 |
if (rc != 0) { |
| 398 |
PJ_LOG(3, (THIS_FILE, " error: timed out")); |
| 399 |
return -2020; |
| 400 |
} |
| 401 |
if (g_ctx.challenge_count == 0) { |
| 402 |
PJ_LOG(3, (THIS_FILE, " error: challenge callback never invoked")); |
| 403 |
return -2030; |
| 404 |
} |
| 405 |
if (g_ctx.reg_code != 200) { |
| 406 |
PJ_LOG(3, (THIS_FILE, " error: expected 200, got %d", |
| 407 |
g_ctx.reg_code)); |
| 408 |
return -2040; |
| 409 |
} |
| 410 |
if (g_mock_reg.req_count < 2) { |
| 411 |
PJ_LOG(3, (THIS_FILE, " error: expected >=2 REGISTERs, got %u", |
| 412 |
g_mock_reg.req_count)); |
| 413 |
return -2050; |
| 414 |
} |
| 415 |
if (g_mock_reg.auth_count == 0) { |
| 416 |
PJ_LOG(3, (THIS_FILE, |
| 417 |
" error: no authenticated REGISTER received")); |
| 418 |
return -2060; |
| 419 |
} |
| 420 |
|
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
static int deferred_respond_creds_test(pjsua_transport_id tp_id, int port) |
| 432 |
{ |
| 433 |
pjsua_acc_config acc_cfg; |
| 434 |
pjsua_acc_id acc_id; |
| 435 |
char reg_uri[64]; |
| 436 |
pj_status_t status; |
| 437 |
int rc; |
| 438 |
|
| 439 |
PJ_LOG(3, (THIS_FILE, " pjsua auth: deferred respond with credentials")); |
| 440 |
|
| 441 |
pj_bzero(&g_ctx, sizeof(g_ctx)); |
| 442 |
g_ctx.action = ACTION_RESPOND_CREDS; |
| 443 |
g_ctx.defer_enabled = PJ_TRUE; |
| 444 |
g_mock_reg.req_count = 0; |
| 445 |
g_mock_reg.auth_count = 0; |
| 446 |
|
| 447 |
pj_ansi_snprintf(reg_uri, sizeof(reg_uri), |
| 448 |
"sip:127.0.0.1:%d", port); |
| 449 |
|
| 450 |
pjsua_acc_config_default(&acc_cfg); |
| 451 |
acc_cfg.id = pj_str("sip:" TEST_USER "@pjsip.org"); |
| 452 |
acc_cfg.reg_uri = pj_str(reg_uri); |
| 453 |
|
| 454 |
acc_cfg.cred_count = 0; |
| 455 |
acc_cfg.allow_contact_rewrite = PJ_FALSE; |
| 456 |
acc_cfg.transport_id = tp_id; |
| 457 |
|
| 458 |
status = pjsua_acc_add(&acc_cfg, PJ_FALSE, &acc_id); |
| 459 |
if (status != PJ_SUCCESS) { |
| 460 |
PJ_LOG(3, (THIS_FILE, " error: pjsua_acc_add failed (%d)", status)); |
| 461 |
return -2110; |
| 462 |
} |
| 463 |
|
| 464 |
rc = wait_and_service(30000); |
| 465 |
|
| 466 |
g_ctx.defer_enabled = PJ_FALSE; |
| 467 |
pjsua_acc_del(acc_id); |
| 468 |
pjsua_handle_events(500); |
| 469 |
|
| 470 |
if (rc != 0) { |
| 471 |
PJ_LOG(3, (THIS_FILE, " error: timed out")); |
| 472 |
return -2120; |
| 473 |
} |
| 474 |
if (g_ctx.challenge_count == 0) { |
| 475 |
PJ_LOG(3, (THIS_FILE, " error: challenge callback never invoked")); |
| 476 |
return -2130; |
| 477 |
} |
| 478 |
if (g_ctx.reg_code != 200) { |
| 479 |
PJ_LOG(3, (THIS_FILE, " error: expected 200, got %d", |
| 480 |
g_ctx.reg_code)); |
| 481 |
return -2140; |
| 482 |
} |
| 483 |
if (g_mock_reg.auth_count == 0) { |
| 484 |
PJ_LOG(3, (THIS_FILE, |
| 485 |
" error: no authenticated REGISTER received")); |
| 486 |
return -2150; |
| 487 |
} |
| 488 |
|
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
static int deferred_abandon_test(pjsua_transport_id tp_id, int port) |
| 500 |
{ |
| 501 |
pjsua_acc_config acc_cfg; |
| 502 |
pjsua_acc_id acc_id; |
| 503 |
char reg_uri[64]; |
| 504 |
pj_status_t status; |
| 505 |
unsigned elapsed; |
| 506 |
|
| 507 |
PJ_LOG(3, (THIS_FILE, " pjsua auth: deferred abandon")); |
| 508 |
|
| 509 |
pj_bzero(&g_ctx, sizeof(g_ctx)); |
| 510 |
g_ctx.action = ACTION_ABANDON; |
| 511 |
g_ctx.defer_enabled = PJ_TRUE; |
| 512 |
g_mock_reg.req_count = 0; |
| 513 |
g_mock_reg.auth_count = 0; |
| 514 |
|
| 515 |
pj_ansi_snprintf(reg_uri, sizeof(reg_uri), |
| 516 |
"sip:127.0.0.1:%d", port); |
| 517 |
|
| 518 |
pjsua_acc_config_default(&acc_cfg); |
| 519 |
acc_cfg.id = pj_str("sip:" TEST_USER "@pjsip.org"); |
| 520 |
acc_cfg.reg_uri = pj_str(reg_uri); |
| 521 |
acc_cfg.cred_count = 1; |
| 522 |
pj_bzero(&acc_cfg.cred_info[0], sizeof(pjsip_cred_info)); |
| 523 |
acc_cfg.cred_info[0].realm = pj_str("*"); |
| 524 |
acc_cfg.cred_info[0].scheme = pj_str("digest"); |
| 525 |
acc_cfg.cred_info[0].username = pj_str(TEST_USER); |
| 526 |
acc_cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 527 |
acc_cfg.cred_info[0].data = pj_str("secret"); |
| 528 |
acc_cfg.allow_contact_rewrite = PJ_FALSE; |
| 529 |
acc_cfg.transport_id = tp_id; |
| 530 |
|
| 531 |
status = pjsua_acc_add(&acc_cfg, PJ_FALSE, &acc_id); |
| 532 |
if (status != PJ_SUCCESS) { |
| 533 |
PJ_LOG(3, (THIS_FILE, " error: pjsua_acc_add failed (%d)", status)); |
| 534 |
return -2210; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
elapsed = 0; |
| 539 |
while (elapsed < 10000 && !g_ctx.challenge_count) { |
| 540 |
pjsua_handle_events(50); |
| 541 |
elapsed += 50; |
| 542 |
|
| 543 |
if (g_ctx.deferred.pending) { |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
g_ctx.defer_enabled = PJ_FALSE; |
| 549 |
do_deferred_abandon(&g_ctx.deferred); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
pjsua_acc_del(acc_id); |
| 555 |
|
| 556 |
|
| 557 |
pjsua_handle_events(500); |
| 558 |
|
| 559 |
if (g_ctx.challenge_count == 0) { |
| 560 |
PJ_LOG(3, (THIS_FILE, " error: challenge callback never invoked")); |
| 561 |
return -2220; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
return 0; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
#if 0 |
| 583 |
static int acc_delete_deferred_test(pjsua_transport_id tp_id, int port) |
| 584 |
{ |
| 585 |
pjsua_acc_config acc_cfg; |
| 586 |
pjsua_acc_id acc_id; |
| 587 |
char reg_uri[64]; |
| 588 |
pj_status_t status; |
| 589 |
unsigned elapsed; |
| 590 |
|
| 591 |
PJ_LOG(3, (THIS_FILE, |
| 592 |
" pjsua auth: account delete with deferred challenge")); |
| 593 |
|
| 594 |
pj_bzero(&g_ctx, sizeof(g_ctx)); |
| 595 |
g_ctx.action = ACTION_DELETE_ACC; |
| 596 |
g_ctx.defer_enabled = PJ_TRUE; |
| 597 |
g_mock_reg.req_count = 0; |
| 598 |
g_mock_reg.auth_count = 0; |
| 599 |
|
| 600 |
pj_ansi_snprintf(reg_uri, sizeof(reg_uri), |
| 601 |
"sip:127.0.0.1:%d", port); |
| 602 |
|
| 603 |
pjsua_acc_config_default(&acc_cfg); |
| 604 |
acc_cfg.id = pj_str("sip:" TEST_USER "@pjsip.org"); |
| 605 |
acc_cfg.reg_uri = pj_str(reg_uri); |
| 606 |
acc_cfg.cred_count = 1; |
| 607 |
pj_bzero(&acc_cfg.cred_info[0], sizeof(pjsip_cred_info)); |
| 608 |
acc_cfg.cred_info[0].realm = pj_str("*"); |
| 609 |
acc_cfg.cred_info[0].scheme = pj_str("digest"); |
| 610 |
acc_cfg.cred_info[0].username = pj_str(TEST_USER); |
| 611 |
acc_cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 612 |
acc_cfg.cred_info[0].data = pj_str("secret"); |
| 613 |
acc_cfg.allow_contact_rewrite = PJ_FALSE; |
| 614 |
acc_cfg.transport_id = tp_id; |
| 615 |
|
| 616 |
status = pjsua_acc_add(&acc_cfg, PJ_FALSE, &acc_id); |
| 617 |
if (status != PJ_SUCCESS) { |
| 618 |
PJ_LOG(3, (THIS_FILE, " error: pjsua_acc_add failed (%d)", status)); |
| 619 |
return -2310; |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
elapsed = 0; |
| 624 |
while (elapsed < 10000 && !g_ctx.deferred.pending) { |
| 625 |
pjsua_handle_events(50); |
| 626 |
elapsed += 50; |
| 627 |
} |
| 628 |
|
| 629 |
if (!g_ctx.deferred.pending) { |
| 630 |
PJ_LOG(3, (THIS_FILE, " error: challenge callback never invoked")); |
| 631 |
g_ctx.defer_enabled = PJ_FALSE; |
| 632 |
pjsua_acc_del(acc_id); |
| 633 |
return -2320; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
g_ctx.defer_enabled = PJ_FALSE; |
| 640 |
status = pjsua_acc_del(acc_id); |
| 641 |
if (status != PJ_SUCCESS) { |
| 642 |
PJ_LOG(3, (THIS_FILE, " error: pjsua_acc_del failed (%d)", status)); |
| 643 |
return -2330; |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
if (pjsua_acc_is_valid(acc_id)) { |
| 648 |
PJ_LOG(3, (THIS_FILE, " error: account still valid after delete")); |
| 649 |
return -2340; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
do_deferred_cleanup_after_acc_del(&g_ctx.deferred); |
| 656 |
|
| 657 |
|
| 658 |
pjsua_handle_events(500); |
| 659 |
|
| 660 |
|
| 661 |
return 0; |
| 662 |
} |
| 663 |
#endif |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
static void restore_endpt(void) |
| 668 |
{ |
| 669 |
pj_status_t status; |
| 670 |
|
| 671 |
status = pjsip_endpt_create(&caching_pool.factory, "endpt", &endpt); |
| 672 |
if (status != PJ_SUCCESS) { |
| 673 |
PJ_PERROR(1, (THIS_FILE, status, "Error creating endpoint")); |
| 674 |
return; |
| 675 |
} |
| 676 |
status = pjsip_tsx_layer_init_module(endpt); |
| 677 |
if (status != PJ_SUCCESS) { |
| 678 |
PJ_PERROR(1, (THIS_FILE, status, "Error initializing tsx layer")); |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
int pjsua_auth_test(void) |
| 687 |
{ |
| 688 |
pjsua_config ua_cfg; |
| 689 |
pjsua_logging_config log_cfg; |
| 690 |
pjsua_transport_config tp_cfg; |
| 691 |
pjsua_transport_id tp_id; |
| 692 |
pj_uint16_t port; |
| 693 |
pj_status_t status; |
| 694 |
int rc = 0; |
| 695 |
|
| 696 |
PJ_LOG(3, (THIS_FILE, "pjsua auth challenge test")); |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
— |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
pjsip_endpt_destroy(endpt); |
| 706 |
endpt = NULL; |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
status = pjsua_create(); |
| 711 |
if (status != PJ_SUCCESS) { |
| 712 |
PJ_LOG(1, (THIS_FILE, " pjsua_create failed (%d)", status)); |
| 713 |
restore_endpt(); |
| 714 |
return -2001; |
| 715 |
} |
| 716 |
|
| 717 |
pjsua_config_default(&ua_cfg); |
| 718 |
ua_cfg.cb.on_auth_challenge = &on_auth_challenge; |
| 719 |
ua_cfg.cb.on_reg_state2 = &on_reg_state2; |
| 720 |
|
| 721 |
pjsua_logging_config_default(&log_cfg); |
| 722 |
log_cfg.level = 3; |
| 723 |
log_cfg.console_level = 3; |
| 724 |
|
| 725 |
status = pjsua_init(&ua_cfg, &log_cfg, NULL); |
| 726 |
if (status != PJ_SUCCESS) { |
| 727 |
PJ_LOG(1, (THIS_FILE, " pjsua_init failed (%d)", status)); |
| 728 |
pjsua_destroy(); |
| 729 |
restore_endpt(); |
| 730 |
return -2002; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
pjsua_transport_config_default(&tp_cfg); |
| 735 |
tp_cfg.port = 0; |
| 736 |
|
| 737 |
status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &tp_cfg, &tp_id); |
| 738 |
if (status != PJ_SUCCESS) { |
| 739 |
PJ_LOG(1, (THIS_FILE, " pjsua_transport_create failed (%d)", status)); |
| 740 |
pjsua_destroy(); |
| 741 |
restore_endpt(); |
| 742 |
return -2003; |
| 743 |
} |
| 744 |
|
| 745 |
status = pjsua_start(); |
| 746 |
if (status != PJ_SUCCESS) { |
| 747 |
PJ_LOG(1, (THIS_FILE, " pjsua_start failed (%d)", status)); |
| 748 |
pjsua_destroy(); |
| 749 |
restore_endpt(); |
| 750 |
return -2004; |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
{ |
| 755 |
pjsua_transport_info ti; |
| 756 |
pjsua_transport_get_info(tp_id, &ti); |
| 757 |
port = pj_sockaddr_get_port(&ti.local_addr); |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(), |
| 762 |
&g_mock_reg.mod); |
| 763 |
if (status != PJ_SUCCESS) { |
| 764 |
PJ_LOG(1, (THIS_FILE, " register mock registrar failed (%d)", |
| 765 |
status)); |
| 766 |
pjsua_destroy(); |
| 767 |
restore_endpt(); |
| 768 |
return -2005; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
rc = deferred_respond_test(tp_id, (int)port); |
| 774 |
if (rc != 0) goto on_return; |
| 775 |
|
| 776 |
|
| 777 |
pjsua_handle_events(1000); |
| 778 |
|
| 779 |
rc = deferred_respond_creds_test(tp_id, (int)port); |
| 780 |
if (rc != 0) goto on_return; |
| 781 |
|
| 782 |
pjsua_handle_events(1000); |
| 783 |
|
| 784 |
rc = deferred_abandon_test(tp_id, (int)port); |
| 785 |
if (rc != 0) goto on_return; |
| 786 |
|
| 787 |
pjsua_handle_events(1000); |
| 788 |
|
| 789 |
en |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
on_return: |
| 799 |
if (g_mock_reg.mod.id != -1) |
| 800 |
pjsip_endpt_unregister_module(pjsua_get_pjsip_endpt(), |
| 801 |
&g_mock_reg.mod); |
| 802 |
|
| 803 |
pjsua_handle_events(500); |
| 804 |
pjsua_destroy2(PJSUA_DESTROY_NO_RX_MSG); |
| 805 |
|
| 806 |
restore_endpt(); |
| 807 |
|
| 808 |
return rc; |
| 809 |
} |