Debian Codegraph @

Xpjproject/pjsip/src/test/auth_async_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2026 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /*
20 * Unit tests for the manual/asynchronous client authentication feature
21 * (pjsip_auth_clt_async_configure / pjsip_auth_clt_async_send_req).
22 *
23 * Scenarios covered:
24 * 1. Synchronous resend: application calls pjsip_auth_clt_async_send_req
25 * from within the challenge callback (deadlock-fix path).
26 * 2. Asynchronous (deferred) resend: application stores the token and tdata
27 * during the callback, then sends from the event loop afterwards.
28 * 3. Double-send prevention: calling pjsip_auth_clt_async_send_req a second
29 * time with the same token returns PJ_EINVAL because the signature is
30 * cleared after a successful send.
31 * 4. Abandoned token: challenge callback fires but the application never
32 * calls pjsip_auth_clt_async_send_req. pjsip_regc_destroy must complete
33 * cleanly; the server must never receive authenticated credentials.
34 * 5. Abandon API: application calls pjsip_auth_clt_async_abandon() from
35 * within the challenge callback. The regc abandon_impl must fire the
36 * on_reg_complete callback with an error code, and the token must be
37 * invalidated so that any subsequent pjsip_auth_clt_async_send_req()
38 * call returns PJ_EINVAL.
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 /* SIP user name used to filter requests belonging to this test module */
49 #define TEST_USER "async-auth-test"
50
51
52 /*****************************************************************************
53 * Mock registrar
54 *
55 * Behaviour:
56 * - First REGISTER without an Authorization header -> 401 + WWW-Authenticate
57 * - REGISTER with an Authorization header present -> 200 OK
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; /* total REGISTER messages received */
65 unsigned auth_count; /* REGISTER messages received with auth */
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 &registrar_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 /* No credentials - issue a 401 challenge */
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 /* Credentials present - accept the registration */
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 * Test context
120 *****************************************************************************/
121
122 /* State for a deferred (async) send - populated in the callback, consumed
123 * by the event loop.
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 /* Per-test context shared between the challenge callback, the regc callback,
133 * and the test runner.
134 */
135 typedef struct {
136 /* Test knobs */
137 pj_bool_t sync; /* PJ_TRUE: send from callback; PJ_FALSE: defer */
138
139 /* Deferred-send state (valid only when !sync) */
140 deferred_send_t deferred;
141
142 /* Challenge-callback diagnostics */
143 unsigned challenge_count;
144 pj_status_t reinit_status;
145
146 /* Registration result (filled by on_reg_complete) */
147 pj_bool_t done;
148 int reg_code;
149 } test_ctx_t;
150
151 static test_ctx_t g_ctx;
152
153 /* Dummy send implementation used by the invalid-token sub-test. */
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 /* Should never be reached in the invalid-token test */
162 pj_assert(!"dummy_send_impl should not be called");
163 return PJ_EBUG;
164 }
165
166
167 /*****************************************************************************
168 * Callbacks
169 *****************************************************************************/
170
171 /* Async challenge callback - used by the sync and deferred send tests. */
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 /* Build the authenticated request. rdata is only valid inside this
183 * callback, so pjsip_auth_clt_reinit_req must always be called here.
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 /* Synchronous path: send from within the callback.
196 * The lock has been released by sip_reg.c before this callback
197 * fires, so there is no deadlock risk.
198 */
199 pjsip_auth_clt_async_send_req(sess, token, new_tdata);
200 } else {
201 /* Asynchronous path: stash everything and let the event loop send. */
202 ctx->deferred.pending = PJ_TRUE;
203 ctx->deferred.sess = sess;
204 ctx->deferred.token = token;
205 ctx->deferred.tdata = new_tdata;
206 /* new_tdata holds a refcount from reinit_req; released by
207 * async_auth_send_impl -> pjsip_regc_send.
208 */
209 }
210 return PJ_TRUE;
211 }
212
213 /* Registration completion callback. */
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 * Helper: create a regc instance wired with async auth and credentials.
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, &regc);
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 /* Send initial REGISTER and wait for the test to complete. Services any
265 * deferred sends from the event loop on each iteration.
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 /* Service a pending deferred send (async scenario) */
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 * Sub-test 1: Synchronous resend from within the challenge callback
297 *
298 * Exercises the path where the application calls pjsip_auth_clt_async_send_req
299 * directly inside on_auth_challenge. Because sip_reg.c releases regc->lock
300 * before invoking the callback, there must be no deadlock.
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, &regc);
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 * Sub-test 2: Deferred (asynchronous) resend from the event loop
350 *
351 * The challenge callback stores the token and prepared tdata without sending.
352 * The event loop detects the pending state and calls
353 * pjsip_auth_clt_async_send_req outside the callback context.
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; /* deferred */
366
367 rc = create_regc(registrar_uri, &on_auth_challenge, &g_ctx, &regc);
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 * Sub-test 3: Double-send prevention - token is invalidated after first send
403 *
404 * After pjsip_auth_clt_async_send_req succeeds the token's signature is
405 * cleared. A subsequent call with the same token must return PJ_EINVAL.
406 *****************************************************************************/
407
408 /* State used by the challenge callback in this sub-test only */
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 /* Remember the session and token before consuming them */
429 g_double_send.sess = sess;
430 g_double_send.token = token;
431
432 /* First (and only legitimate) send - this clears token->signature */
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, &regc);
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 /* Wait until both the challenge fires and the registration completes */
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 /* Now create a fresh tdata to supply a non-NULL argument.
483 * Use the now-invalidated token (signature zeroed after the first send).
484 * pjsip_auth_clt_async_send_req must refuse with PJ_EINVAL.
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 /* Build an impl token whose signature is all zeros (invalid) */
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 /* Also verify the consumed token from the first send is rejected */
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 * Sub-test 4: Abandoned token — app never calls pjsip_auth_clt_async_send_req
532 *
533 * The challenge callback fires and receives the token, but the application
534 * deliberately never calls pjsip_auth_clt_async_send_req (e.g. credentials
535 * not available, request cancelled). The key assertions are:
536 * a) The server never receives an authenticated REGISTER.
537 * b) pjsip_regc_destroy completes without crash or assertion failure.
538 * pj_bzero inside regc_destroy clears the token's signature, ensuring
539 * that any out-of-scope reference to the token cannot invoke send_impl
540 * with a dangling user_data pointer.
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 /* Intentionally do nothing: simulate an app that cannot supply
556 * credentials and abandons the token without calling
557 * pjsip_auth_clt_async_send_req.
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, &regc);
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 /* Wait for the challenge callback to fire (up to 5 s) */
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 /* Let the transaction settle without servicing the pending token */
597 flush_events(500);
598
599 /* The server must not have received authenticated credentials */
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 /* Registration must not have completed successfully */
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 /* Destroy with a live (un-serviced) token. pj_bzero in regc_destroy
616 * clears the signature so any stale reference to the token after this
617 * point would be rejected by pjsip_auth_clt_async_send_req.
618 * Reaching here without crash is the key assertion.
619 */
620 pjsip_regc_destroy(regc);
621 return 0;
622 }
623
624
625 /*****************************************************************************
626 * Sub-test 5: Abandon API — app calls pjsip_auth_clt_async_abandon()
627 *
628 * The application calls pjsip_auth_clt_async_abandon() from within the
629 * challenge callback. Assertions:
630 * a) The regc abandon_impl fires on_reg_complete with a non-200 code.
631 * b) The server never receives authenticated credentials.
632 * c) The token is invalidated: a subsequent pjsip_auth_clt_async_send_req()
633 * with the same token returns PJ_EINVAL.
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 /* Remember for later invalidation check */
650 g_abandon_api.sess = sess;
651 g_abandon_api.token = token;
652 g_abandon_api.fired = PJ_TRUE;
653
654 /* Explicitly abandon: regc's abandon_impl will call on_reg_complete */
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, &regc);
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 /* Wait until the challenge callback fires and abandon_impl runs (up to 5 s) */
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 /* abandon_impl must have invoked on_reg_complete with a non-200 code */
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 /* Server must not have received authenticated credentials */
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 /* The token must now be invalid (signature zeroed by abandon) */
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 * Main entry point
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 /* Create a dedicated UDP transport so this test is self-contained */
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 /* Register the mock registrar */
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 /* 1. Synchronous resend */
772 rc = sync_send_test(&registrar_uri);
773 if (rc != 0) goto on_return;
774
775 pj_thread_sleep(200);
776
777 /* 2. Deferred (asynchronous) resend */
778 rc = deferred_send_test(&registrar_uri);
779 if (rc != 0) goto on_return;
780
781 pj_thread_sleep(200);
782
783 /* 3. Double-send / token invalidation */
784 rc = double_send_test(&registrar_uri);
785 if (rc != 0) goto on_return;
786
787 pj_thread_sleep(200);
788
789 /* 4. Abandoned token */
790 rc = abandoned_token_test(&registrar_uri);
791 if (rc != 0) goto on_return;
792
793 pj_thread_sleep(200);
794
795 /* 5. Abandon API */
796 rc = abandon_api_test(&registrar_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 }