| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#include "test.h" |
| 42 |
#include <pjsip_ua.h> |
| 43 |
#include <pjsip.h> |
| 44 |
#include <pjlib.h> |
| 45 |
|
| 46 |
#define THIS_FILE "auth_async_test.c" |
| 47 |
|
| 48 |
|
| 49 |
#define TEST_USER "async-auth-test" |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static pj_bool_t registrar_rx_request(pjsip_rx_data *rdata); |
| 61 |
|
| 62 |
static struct { |
| 63 |
pjsip_module mod; |
| 64 |
unsigned req_count; |
| 65 |
unsigned auth_count; |
| 66 |
} g_registrar = { |
| 67 |
{ |
| 68 |
NULL, NULL, |
| 69 |
{ "mod-async-auth-reg", 19 }, |
| 70 |
-1, |
| 71 |
PJSIP_MOD_PRIORITY_APPLICATION, |
| 72 |
NULL, NULL, NULL, NULL, |
| 73 |
®istrar_rx_request, |
| 74 |
NULL, NULL, NULL, NULL, |
| 75 |
} |
| 76 |
}; |
| 77 |
|
| 78 |
static pj_bool_t registrar_rx_request(pjsip_rx_data *rdata) |
| 79 |
{ |
| 80 |
pjsip_msg *msg = rdata->msg_info.msg; |
| 81 |
pjsip_hdr hdr_list; |
| 82 |
int code; |
| 83 |
pj_status_t status; |
| 84 |
|
| 85 |
if (msg->line.req.method.id != PJSIP_REGISTER_METHOD || |
| 86 |
!is_user_equal(rdata->msg_info.from, TEST_USER)) |
| 87 |
{ |
| 88 |
return PJ_FALSE; |
| 89 |
} |
| 90 |
|
| 91 |
g_registrar.req_count++; |
| 92 |
pj_list_init(&hdr_list); |
| 93 |
|
| 94 |
if (pjsip_msg_find_hdr(msg, PJSIP_H_AUTHORIZATION, NULL) == NULL) { |
| 95 |
|
| 96 |
const pj_str_t hname = pj_str("WWW-Authenticate"); |
| 97 |
const pj_str_t hvalue = |
| 98 |
pj_str("Digest realm=\"test\", nonce=\"testnonce\""); |
| 99 |
pjsip_generic_string_hdr *hwww = |
| 100 |
pjsip_generic_string_hdr_create(rdata->tp_info.pool, |
| 101 |
&hname, &hvalue); |
| 102 |
pj_list_push_back(&hdr_list, hwww); |
| 103 |
code = 401; |
| 104 |
} else { |
| 105 |
|
| 106 |
g_registrar.auth_count++; |
| 107 |
code = 200; |
| 108 |
} |
| 109 |
|
| 110 |
status = pjsip_endpt_respond(endpt, NULL, rdata, code, NULL, |
| 111 |
&hdr_list, NULL, NULL); |
| 112 |
pj_assert(status == PJ_SUCCESS); |
| 113 |
PJ_UNUSED_ARG(status); |
| 114 |
return PJ_TRUE; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
typedef struct { |
| 126 |
pj_bool_t pending; |
| 127 |
pjsip_auth_clt_sess *sess; |
| 128 |
void *token; |
| 129 |
pjsip_tx_data *tdata; |
| 130 |
} deferred_send_t; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
typedef struct { |
| 136 |
|
| 137 |
pj_bool_t sync; |
| 138 |
|
| 139 |
|
| 140 |
deferred_send_t deferred; |
| 141 |
|
| 142 |
|
| 143 |
unsigned challenge_count; |
| 144 |
pj_status_t reinit_status; |
| 145 |
|
| 146 |
|
| 147 |
pj_bool_t done; |
| 148 |
int reg_code; |
| 149 |
} test_ctx_t; |
| 150 |
|
| 151 |
static test_ctx_t g_ctx; |
| 152 |
|
| 153 |
|
| 154 |
static pj_status_t dummy_send_impl(pjsip_auth_clt_sess *sess, |
| 155 |
void *user_data, |
| 156 |
pjsip_tx_data *tdata) |
| 157 |
{ |
| 158 |
PJ_UNUSED_ARG(sess); |
| 159 |
PJ_UNUSED_ARG(user_data); |
| 160 |
PJ_UNUSED_ARG(tdata); |
| 161 |
|
| 162 |
pj_assert(!"dummy_send_impl should not be called"); |
| 163 |
return PJ_EBUG; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
static pj_bool_t on_auth_challenge(pjsip_auth_clt_sess *sess, |
| 173 |
void *token, |
| 174 |
const pjsip_auth_clt_async_on_chal_param *param) |
| 175 |
{ |
| 176 |
test_ctx_t *ctx = (test_ctx_t *)param->user_data; |
| 177 |
pjsip_tx_data *new_tdata = NULL; |
| 178 |
pj_status_t status; |
| 179 |
|
| 180 |
ctx->challenge_count++; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
status = pjsip_auth_clt_reinit_req(sess, |
| 186 |
param->rdata, |
| 187 |
param->tdata, |
| 188 |
&new_tdata); |
| 189 |
ctx->reinit_status = status; |
| 190 |
|
| 191 |
if (status != PJ_SUCCESS || !new_tdata) |
| 192 |
return PJ_TRUE; |
| 193 |
|
| 194 |
if (ctx->sync) { |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
pjsip_auth_clt_async_send_req(sess, token, new_tdata); |
| 200 |
} else { |
| 201 |
|
| 202 |
ctx->deferred.pending = PJ_TRUE; |
| 203 |
ctx->deferred.sess = sess; |
| 204 |
ctx->deferred.token = token; |
| 205 |
ctx->deferred.tdata = new_tdata; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
} |
| 210 |
return PJ_TRUE; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
static void on_reg_complete(struct pjsip_regc_cbparam *param) |
| 215 |
{ |
| 216 |
test_ctx_t *ctx = (test_ctx_t *)param->token; |
| 217 |
|
| 218 |
ctx->done = PJ_TRUE; |
| 219 |
ctx->reg_code = param->code; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static int create_regc(const pj_str_t *registrar_uri, |
| 227 |
pjsip_auth_clt_async_on_challenge *challenge_cb, |
| 228 |
void *user_data, |
| 229 |
pjsip_regc **p_regc) |
| 230 |
{ |
| 231 |
pjsip_regc *regc; |
| 232 |
pjsip_auth_clt_async_setting async_cfg; |
| 233 |
pjsip_cred_info cred; |
| 234 |
pj_str_t aor = pj_str("<sip:" TEST_USER "@pjsip.org>"); |
| 235 |
pj_str_t contact = pj_str("<sip:" TEST_USER "@127.0.0.1>"); |
| 236 |
int rc; |
| 237 |
|
| 238 |
rc = pjsip_regc_create(endpt, &g_ctx, &on_reg_complete, ®c); |
| 239 |
if (rc != PJ_SUCCESS) return -1; |
| 240 |
|
| 241 |
rc = pjsip_regc_init(regc, registrar_uri, &aor, &aor, 1, &contact, 60); |
| 242 |
if (rc != PJ_SUCCESS) { pjsip_regc_destroy(regc); return -1; } |
| 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 |
rc = pjsip_regc_set_credentials(regc, 1, &cred); |
| 251 |
if (rc != PJ_SUCCESS) { pjsip_regc_destroy(regc); return -1; } |
| 252 |
|
| 253 |
pj_bzero(&async_cfg, sizeof(async_cfg)); |
| 254 |
async_cfg.cb = challenge_cb; |
| 255 |
async_cfg.user_data = user_data; |
| 256 |
rc = pjsip_auth_clt_async_configure(pjsip_regc_get_auth_sess(regc), |
| 257 |
&async_cfg); |
| 258 |
if (rc != PJ_SUCCESS) { pjsip_regc_destroy(regc); return -1; } |
| 259 |
|
| 260 |
*p_regc = regc; |
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
static int send_and_wait(pjsip_regc *regc) |
| 268 |
{ |
| 269 |
pjsip_tx_data *tdata; |
| 270 |
unsigned i; |
| 271 |
int rc; |
| 272 |
|
| 273 |
rc = pjsip_regc_register(regc, PJ_TRUE, &tdata); |
| 274 |
if (rc != PJ_SUCCESS) return -1; |
| 275 |
|
| 276 |
rc = pjsip_regc_send(regc, tdata); |
| 277 |
if (rc != PJ_SUCCESS) return -1; |
| 278 |
|
| 279 |
for (i = 0; i < 600 && !g_ctx.done; ++i) { |
| 280 |
flush_events(100); |
| 281 |
|
| 282 |
|
| 283 |
if (g_ctx.deferred.pending) { |
| 284 |
g_ctx.deferred.pending = PJ_FALSE; |
| 285 |
pjsip_auth_clt_async_send_req(g_ctx.deferred.sess, |
| 286 |
g_ctx.deferred.token, |
| 287 |
g_ctx.deferred.tdata); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
return g_ctx.done ? 0 : -1; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
static int sync_send_test(const pj_str_t *registrar_uri) |
| 303 |
{ |
| 304 |
pjsip_regc *regc = NULL; |
| 305 |
int rc; |
| 306 |
|
| 307 |
PJ_LOG(3, (THIS_FILE, " async auth: synchronous resend from callback")); |
| 308 |
|
| 309 |
pj_bzero(&g_ctx, sizeof(g_ctx)); |
| 310 |
g_registrar.req_count = 0; |
| 311 |
g_registrar.auth_count = 0; |
| 312 |
g_ctx.sync = PJ_TRUE; |
| 313 |
|
| 314 |
rc = create_regc(registrar_uri, &on_auth_challenge, &g_ctx, ®c); |
| 315 |
if (rc != 0) return -1000; |
| 316 |
|
| 317 |
rc = send_and_wait(regc); |
| 318 |
pjsip_regc_destroy(regc); |
| 319 |
|
| 320 |
if (rc != 0) { |
| 321 |
PJ_LOG(3, (THIS_FILE, " error: test timed out")); |
| 322 |
return -1010; |
| 323 |
} |
| 324 |
if (g_ctx.challenge_count == 0) { |
| 325 |
PJ_LOG(3, (THIS_FILE, " error: challenge callback never invoked")); |
| 326 |
return -1020; |
| 327 |
} |
| 328 |
if (g_ctx.reinit_status != PJ_SUCCESS) { |
| 329 |
PJ_LOG(3, (THIS_FILE, " error: reinit_req failed (%d)", |
| 330 |
g_ctx.reinit_status)); |
| 331 |
return -1030; |
| 332 |
} |
| 333 |
if (g_ctx.reg_code != 200) { |
| 334 |
PJ_LOG(3, (THIS_FILE, " error: expected 200, got %d", |
| 335 |
g_ctx.reg_code)); |
| 336 |
return -1040; |
| 337 |
} |
| 338 |
if (g_registrar.auth_count == 0) { |
| 339 |
PJ_LOG(3, (THIS_FILE, |
| 340 |
" error: no authenticated REGISTER received by server")); |
| 341 |
return -1050; |
| 342 |
} |
| 343 |
|
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
static int deferred_send_test(const pj_str_t *registrar_uri) |
| 356 |
{ |
| 357 |
pjsip_regc *regc = NULL; |
| 358 |
int rc; |
| 359 |
|
| 360 |
PJ_LOG(3, (THIS_FILE, " async auth: deferred resend from event loop")); |
| 361 |
|
| 362 |
pj_bzero(&g_ctx, sizeof(g_ctx)); |
| 363 |
g_registrar.req_count = 0; |
| 364 |
g_registrar.auth_count = 0; |
| 365 |
g_ctx.sync = PJ_FALSE; |
| 366 |
|
| 367 |
rc = create_regc(registrar_uri, &on_auth_challenge, &g_ctx, ®c); |
| 368 |
if (rc != 0) return -1100; |
| 369 |
|
| 370 |
rc = send_and_wait(regc); |
| 371 |
pjsip_regc_destroy(regc); |
| 372 |
|
| 373 |
if (rc != 0) { |
| 374 |
PJ_LOG(3, (THIS_FILE, " error: test timed out")); |
| 375 |
return -1110; |
| 376 |
} |
| 377 |
if (g_ctx.challenge_count == 0) { |
| 378 |
PJ_LOG(3, (THIS_FILE, " error: challenge callback never invoked")); |
| 379 |
return -1120; |
| 380 |
} |
| 381 |
if (g_ctx.reinit_status != PJ_SUCCESS) { |
| 382 |
PJ_LOG(3, (THIS_FILE, " error: reinit_req failed (%d)", |
| 383 |
g_ctx.reinit_status)); |
| 384 |
return -1130; |
| 385 |
} |
| 386 |
if (g_ctx.reg_code != 200) { |
| 387 |
PJ_LOG(3, (THIS_FILE, " error: expected 200, got %d", |
| 388 |
g_ctx.reg_code)); |
| 389 |
return -1140; |
| 390 |
} |
| 391 |
if (g_registrar.auth_count == 0) { |
| 392 |
PJ_LOG(3, (THIS_FILE, |
| 393 |
" error: no authenticated REGISTER received by server")); |
| 394 |
return -1150; |
| 395 |
} |
| 396 |
|
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
static struct { |
| 410 |
pj_bool_t fired; |
| 411 |
pjsip_auth_clt_sess *sess; |
| 412 |
void *token; |
| 413 |
} g_double_send; |
| 414 |
|
| 415 |
static pj_bool_t on_challenge_for_double_send( |
| 416 |
pjsip_auth_clt_sess *sess, |
| 417 |
void *token, |
| 418 |
const pjsip_auth_clt_async_on_chal_param *param) |
| 419 |
{ |
| 420 |
pjsip_tx_data *new_tdata = NULL; |
| 421 |
pj_status_t status; |
| 422 |
|
| 423 |
status = pjsip_auth_clt_reinit_req(sess, param->rdata, param->tdata, |
| 424 |
&new_tdata); |
| 425 |
if (status != PJ_SUCCESS || !new_tdata) |
| 426 |
return PJ_TRUE; |
| 427 |
|
| 428 |
|
| 429 |
g_double_send.sess = sess; |
| 430 |
g_double_send.token = token; |
| 431 |
|
| 432 |
|
| 433 |
pjsip_auth_clt_async_send_req(sess, token, new_tdata); |
| 434 |
g_double_send.fired = PJ_TRUE; |
| 435 |
return PJ_TRUE; |
| 436 |
} |
| 437 |
|
| 438 |
static int double_send_test(const pj_str_t *registrar_uri) |
| 439 |
{ |
| 440 |
pjsip_regc *regc = NULL; |
| 441 |
pjsip_tx_data *dummy_tdata = NULL; |
| 442 |
pj_status_t status; |
| 443 |
unsigned i; |
| 444 |
int rc; |
| 445 |
|
| 446 |
PJ_LOG(3, (THIS_FILE, " async auth: double-send prevention")); |
| 447 |
|
| 448 |
pj_bzero(&g_ctx, sizeof(g_ctx)); |
| 449 |
pj_bzero(&g_double_send, sizeof(g_double_send)); |
| 450 |
g_registrar.req_count = 0; |
| 451 |
g_registrar.auth_count = 0; |
| 452 |
g_ctx.sync = PJ_TRUE; |
| 453 |
|
| 454 |
rc = create_regc(registrar_uri, &on_challenge_for_double_send, |
| 455 |
NULL, ®c); |
| 456 |
if (rc != 0) return -1200; |
| 457 |
|
| 458 |
rc = pjsip_regc_register(regc, PJ_TRUE, &dummy_tdata); |
| 459 |
if (rc != PJ_SUCCESS) { pjsip_regc_destroy(regc); return -1210; } |
| 460 |
|
| 461 |
rc = pjsip_regc_send(regc, dummy_tdata); |
| 462 |
if (rc != PJ_SUCCESS) { pjsip_regc_destroy(regc); return -1220; } |
| 463 |
dummy_tdata = NULL; |
| 464 |
|
| 465 |
|
| 466 |
for (i = 0; i < 600 && (!g_double_send.fired || !g_ctx.done); ++i) |
| 467 |
flush_events(100); |
| 468 |
|
| 469 |
if (!g_double_send.fired) { |
| 470 |
PJ_LOG(3, (THIS_FILE, " error: challenge callback not invoked")); |
| 471 |
pjsip_regc_destroy(regc); |
| 472 |
return -1230; |
| 473 |
} |
| 474 |
if (!g_ctx.done || g_ctx.reg_code != 200) { |
| 475 |
PJ_LOG(3, (THIS_FILE, |
| 476 |
" error: registration did not complete (code=%d)", |
| 477 |
g_ctx.reg_code)); |
| 478 |
pjsip_regc_destroy(regc); |
| 479 |
return -1240; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
rc = pjsip_regc_register(regc, PJ_TRUE, &dummy_tdata); |
| 487 |
if (rc == PJ_SUCCESS) { |
| 488 |
pjsip_auth_clt_async_impl_token bad_token; |
| 489 |
|
| 490 |
|
| 491 |
pj_bzero(&bad_token, sizeof(bad_token)); |
| 492 |
bad_token.send_impl = &dummy_send_impl; |
| 493 |
|
| 494 |
status = pjsip_auth_clt_async_send_req(g_double_send.sess, |
| 495 |
&bad_token, |
| 496 |
dummy_tdata); |
| 497 |
pjsip_tx_data_dec_ref(dummy_tdata); |
| 498 |
|
| 499 |
if (status != PJ_EINVAL) { |
| 500 |
PJ_LOG(3, (THIS_FILE, |
| 501 |
" error: expected PJ_EINVAL for invalid token, " |
| 502 |
"got status=%d", status)); |
| 503 |
pjsip_regc_destroy(regc); |
| 504 |
return -1250; |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
rc = pjsip_regc_register(regc, PJ_TRUE, &dummy_tdata); |
| 510 |
if (rc == PJ_SUCCESS) { |
| 511 |
status = pjsip_auth_clt_async_send_req(g_double_send.sess, |
| 512 |
g_double_send.token, |
| 513 |
dummy_tdata); |
| 514 |
pjsip_tx_data_dec_ref(dummy_tdata); |
| 515 |
|
| 516 |
if (status != PJ_EINVAL) { |
| 517 |
PJ_LOG(3, (THIS_FILE, |
| 518 |
" error: expected PJ_EINVAL for consumed token, " |
| 519 |
"got status=%d", status)); |
| 520 |
pjsip_regc_destroy(regc); |
| 521 |
return -1260; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
pjsip_regc_destroy(regc); |
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
eq |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
static struct { |
| 544 |
pj_bool_t fired; |
| 545 |
} g_abandoned; |
| 546 |
|
| 547 |
static pj_bool_t on_challenge_no_send( |
| 548 |
pjsip_auth_clt_sess *sess, |
| 549 |
void *token, |
| 550 |
const pjsip_auth_clt_async_on_chal_param *param) |
| 551 |
{ |
| 552 |
PJ_UNUSED_ARG(sess); |
| 553 |
PJ_UNUSED_ARG(token); |
| 554 |
PJ_UNUSED_ARG(param); |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
g_abandoned.fired = PJ_TRUE; |
| 560 |
return PJ_TRUE; |
| 561 |
} |
| 562 |
|
| 563 |
static int abandoned_token_test(const pj_str_t *registrar_uri) |
| 564 |
{ |
| 565 |
pjsip_regc *regc = NULL; |
| 566 |
pjsip_tx_data *tdata; |
| 567 |
unsigned i; |
| 568 |
int rc; |
| 569 |
|
| 570 |
PJ_LOG(3, (THIS_FILE, " async auth: abandoned token (no send_req call)")); |
| 571 |
|
| 572 |
pj_bzero(&g_ctx, sizeof(g_ctx)); |
| 573 |
pj_bzero(&g_abandoned, sizeof(g_abandoned)); |
| 574 |
g_registrar.req_count = 0; |
| 575 |
g_registrar.auth_count = 0; |
| 576 |
|
| 577 |
rc = create_regc(registrar_uri, &on_challenge_no_send, NULL, ®c); |
| 578 |
if (rc != 0) return -1300; |
| 579 |
|
| 580 |
rc = pjsip_regc_register(regc, PJ_TRUE, &tdata); |
| 581 |
if (rc != PJ_SUCCESS) { pjsip_regc_destroy(regc); return -1310; } |
| 582 |
|
| 583 |
rc = pjsip_regc_send(regc, tdata); |
| 584 |
if (rc != PJ_SUCCESS) { pjsip_regc_destroy(regc); return -1320; } |
| 585 |
|
| 586 |
|
| 587 |
for (i = 0; i < 50 && !g_abandoned.fired; ++i) |
| 588 |
flush_events(100); |
| 589 |
|
| 590 |
if (!g_abandoned.fired) { |
| 591 |
PJ_LOG(3, (THIS_FILE, " error: challenge callback not invoked")); |
| 592 |
pjsip_regc_destroy(regc); |
| 593 |
return -1330; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
flush_events(500); |
| 598 |
|
| 599 |
|
| 600 |
if (g_registrar.auth_count != 0) { |
| 601 |
PJ_LOG(3, (THIS_FILE, |
| 602 |
" error: authenticated REGISTER sent unexpectedly")); |
| 603 |
pjsip_regc_destroy(regc); |
| 604 |
return -1340; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
if (g_ctx.done && g_ctx.reg_code == 200) { |
| 609 |
PJ_LOG(3, (THIS_FILE, |
| 610 |
" error: registration unexpectedly completed with 200")); |
| 611 |
pjsip_regc_destroy(regc); |
| 612 |
return -1350; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
pjsip_regc_destroy(regc); |
| 621 |
return 0; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
() |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
static struct { |
| 637 |
pj_bool_t fired; |
| 638 |
pjsip_auth_clt_sess *sess; |
| 639 |
void *token; |
| 640 |
} g_abandon_api; |
| 641 |
|
| 642 |
static pj_bool_t on_challenge_do_abandon( |
| 643 |
pjsip_auth_clt_sess *sess, |
| 644 |
void *token, |
| 645 |
const pjsip_auth_clt_async_on_chal_param *param) |
| 646 |
{ |
| 647 |
PJ_UNUSED_ARG(param); |
| 648 |
|
| 649 |
|
| 650 |
g_abandon_api.sess = sess; |
| 651 |
g_abandon_api.token = token; |
| 652 |
g_abandon_api.fired = PJ_TRUE; |
| 653 |
|
| 654 |
|
| 655 |
pjsip_auth_clt_async_abandon(sess, token); |
| 656 |
return PJ_TRUE; |
| 657 |
} |
| 658 |
|
| 659 |
static int abandon_api_test(const pj_str_t *registrar_uri) |
| 660 |
{ |
| 661 |
pjsip_regc *regc = NULL; |
| 662 |
pjsip_tx_data *tdata = NULL; |
| 663 |
pj_status_t status; |
| 664 |
unsigned i; |
| 665 |
int rc; |
| 666 |
|
| 667 |
PJ_LOG(3, (THIS_FILE, " async auth: abandon API (pjsip_auth_clt_async_abandon)")); |
| 668 |
|
| 669 |
pj_bzero(&g_ctx, sizeof(g_ctx)); |
| 670 |
pj_bzero(&g_abandon_api, sizeof(g_abandon_api)); |
| 671 |
g_registrar.req_count = 0; |
| 672 |
g_registrar.auth_count = 0; |
| 673 |
|
| 674 |
rc = create_regc(registrar_uri, &on_challenge_do_abandon, NULL, ®c); |
| 675 |
if (rc != 0) return -1400; |
| 676 |
|
| 677 |
rc = pjsip_regc_register(regc, PJ_TRUE, &tdata); |
| 678 |
if (rc != PJ_SUCCESS) { pjsip_regc_destroy(regc); return -1410; } |
| 679 |
|
| 680 |
rc = pjsip_regc_send(regc, tdata); |
| 681 |
if (rc != PJ_SUCCESS) { pjsip_regc_destroy(regc); return -1420; } |
| 682 |
|
| 683 |
|
| 684 |
for (i = 0; i < 50 && (!g_abandon_api.fired || !g_ctx.done); ++i) |
| 685 |
flush_events(100); |
| 686 |
|
| 687 |
if (!g_abandon_api.fired) { |
| 688 |
PJ_LOG(3, (THIS_FILE, " error: challenge callback not invoked")); |
| 689 |
pjsip_regc_destroy(regc); |
| 690 |
return -1430; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
if (!g_ctx.done) { |
| 695 |
PJ_LOG(3, (THIS_FILE, |
| 696 |
" error: on_reg_complete not called after abandon")); |
| 697 |
pjsip_regc_destroy(regc); |
| 698 |
return -1440; |
| 699 |
} |
| 700 |
if (g_ctx.reg_code == 200) { |
| 701 |
PJ_LOG(3, (THIS_FILE, |
| 702 |
" error: on_reg_complete reported 200 after abandon")); |
| 703 |
pjsip_regc_destroy(regc); |
| 704 |
return -1450; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
if (g_registrar.auth_count != 0) { |
| 709 |
PJ_LOG(3, (THIS_FILE, |
| 710 |
" error: authenticated REGISTER sent after abandon")); |
| 711 |
pjsip_regc_destroy(regc); |
| 712 |
return -1460; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
rc = pjsip_regc_register(regc, PJ_TRUE, &tdata); |
| 717 |
if (rc == PJ_SUCCESS) { |
| 718 |
status = pjsip_auth_clt_async_send_req(g_abandon_api.sess, |
| 719 |
g_abandon_api.token, |
| 720 |
tdata); |
| 721 |
pjsip_tx_data_dec_ref(tdata); |
| 722 |
|
| 723 |
if (status != PJ_EINVAL) { |
| 724 |
PJ_LOG(3, (THIS_FILE, |
| 725 |
" error: expected PJ_EINVAL for abandoned token, " |
| 726 |
"got status=%d", status)); |
| 727 |
pjsip_regc_destroy(regc); |
| 728 |
return -1470; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
pjsip_regc_destroy(regc); |
| 733 |
return 0; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
int auth_async_test(void) |
| 741 |
{ |
| 742 |
pjsip_transport *udp = NULL; |
| 743 |
char reg_uri_buf[64]; |
| 744 |
pj_str_t registrar_uri; |
| 745 |
pj_uint16_t port; |
| 746 |
int rc = 0; |
| 747 |
|
| 748 |
PJ_LOG(3, (THIS_FILE, "Auth async test")); |
| 749 |
|
| 750 |
|
| 751 |
rc = pjsip_udp_transport_start(endpt, NULL, NULL, 1, &udp); |
| 752 |
if (rc != PJ_SUCCESS) { |
| 753 |
app_perror(" error creating UDP transport", rc); |
| 754 |
rc = -1; |
| 755 |
goto on_return; |
| 756 |
} |
| 757 |
|
| 758 |
port = pj_sockaddr_get_port(&udp->local_addr); |
| 759 |
pj_ansi_snprintf(reg_uri_buf, sizeof(reg_uri_buf), |
| 760 |
"sip:127.0.0.1:%d", (int)port); |
| 761 |
registrar_uri = pj_str(reg_uri_buf); |
| 762 |
|
| 763 |
|
| 764 |
rc = pjsip_endpt_register_module(endpt, &g_registrar.mod); |
| 765 |
if (rc != PJ_SUCCESS) { |
| 766 |
app_perror(" error registering mock registrar", rc); |
| 767 |
rc = -2; |
| 768 |
goto on_return; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
rc = sync_send_test(®istrar_uri); |
| 773 |
if (rc != 0) goto on_return; |
| 774 |
|
| 775 |
pj_thread_sleep(200); |
| 776 |
|
| 777 |
|
| 778 |
rc = deferred_send_test(®istrar_uri); |
| 779 |
if (rc != 0) goto on_return; |
| 780 |
|
| 781 |
pj_thread_sleep(200); |
| 782 |
|
| 783 |
|
| 784 |
rc = double_send_test(®istrar_uri); |
| 785 |
if (rc != 0) goto on_return; |
| 786 |
|
| 787 |
pj_thread_sleep(200); |
| 788 |
|
| 789 |
|
| 790 |
rc = abandoned_token_test(®istrar_uri); |
| 791 |
if (rc != 0) goto on_return; |
| 792 |
|
| 793 |
pj_thread_sleep(200); |
| 794 |
|
| 795 |
|
| 796 |
rc = abandon_api_test(®istrar_uri); |
| 797 |
if (rc != 0) goto on_return; |
| 798 |
|
| 799 |
on_return: |
| 800 |
if (g_registrar.mod.id != -1) |
| 801 |
pjsip_endpt_unregister_module(endpt, &g_registrar.mod); |
| 802 |
if (udp) |
| 803 |
pjsip_transport_dec_ref(udp); |
| 804 |
return rc; |
| 805 |
} |