Debian Codegraph @

Xpjproject/pjsip/src/test/regc_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "test.h"
20 #include <pjsip_ua.h>
21 #include <pjsip.h>
22 #include <pjlib.h>
23
24 #define THIS_FILE "regc_test.c"
25
26
27 /************************************************************************/
28 /* A module to inject error into outgoing sending operation */
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, /* prev, next. */
40 { "mod-send", 8 }, /* Name. */
41 -1, /* Id */
42 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER, /* Priority */
43 NULL, /* load() */
44 NULL, /* start() */
45 NULL, /* stop() */
46 NULL, /* unload() */
47 NULL, /* on_rx_request() */
48 NULL, /* on_rx_response() */
49 &mod_send_on_tx_request, /* on_tx_request. */
50 NULL, /* on_tx_response() */
51 NULL, /* on_tsx_state() */
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 /* Registrar for testing */
76 static pj_bool_t regs_rx_request(pjsip_rx_data *rdata);
77
78 enum contact_op
79 {
80 NONE, /* don't put Contact header */
81 EXACT, /* return exact contact */
82 MODIFIED, /* return modified Contact header */
83 };
84
85 struct registrar_cfg
86 {
87 pj_bool_t respond; /* should it respond at all */
88 unsigned status_code; /* final response status code */
89 pj_bool_t authenticate; /* should we authenticate? */
90 enum contact_op contact_op; /* What should we do with Contact */
91 unsigned expires_param; /* non-zero to put in expires param */
92 unsigned expires; /* non-zero to put in Expires header*/
93
94 pj_str_t more_contacts; /* Additional Contact headers to put*/
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, /* prev, next. */
106 { "registrar", 9 }, /* Name. */
107 -1, /* Id */
108 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
109 NULL, /* load() */
110 NULL, /* start() */
111 NULL, /* stop() */
112 NULL, /* unload() */
113 &regs_rx_request, /* on_rx_request() */
114 NULL, /* on_rx_response() */
115 NULL, /* on_tx_request. */
116 NULL, /* on_tx_response() */
117 NULL, /* on_tsx_state() */
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 &registrar.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 /* Client registration test session */
218 struct client
219 {
220 /* Result/expected result */
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 /* Commands */
229 pj_bool_t destroy_on_cb;
230
231 /* Status */
232 pj_bool_t done;
233
234 /* Additional results */
235 unsigned interval;
236 unsigned next_reg;
237 };
238
239 /* regc callback */
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 /* Generic client test session */
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 /* Modify registrar settings */
292 pj_memcpy(&registrar.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, &regc);
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 /* Register */
326 status = pjsip_regc_register(regc, PJ_TRUE, &tdata);
327 if (status != PJ_SUCCESS) {
328 pjsip_regc_destroy(regc);
329 return -120;
330 }
331 /* Add additional ref counter to prevent premature destroy */
332 pjsip_tx_data_add_ref(tdata);
333
334 status = pjsip_regc_send(regc, tdata);
335
336 /* That's it, wait until the callback is sent */
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 /* Destroy the regc, we're done with the test, unless we're
349 * instructed to leave the session open.
350 */
351 if (!leave_session && !client_cfg->destroy_on_cb)
352 pjsip_regc_destroy(regc);
353
354 /* Compare results with expected results */
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 /* Looks like everything is okay. */
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 /* Customized tests */
414
415 /* Check that client is sending register refresh */
416 static int keep_alive_test(const pj_str_t *registrar_uri)
417 {
418 enum { TIMEOUT = 40 };
419 struct registrar_cfg server_cfg =
420 /* respond code auth contact exp_prm expires more_contacts */
421 { PJ_TRUE, 200, PJ_FALSE, EXACT, TIMEOUT, 0, {NULL, 0}};
422 struct client client_cfg =
423 /* error code have_reg expiration contact_cnt auth? destroy*/
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, &regc);
434 if (ret != 0)
435 return ret;
436
437 /* Reset server response_cnt */
438 registrar.response_cnt = 0;
439
440 /* Wait until keep-alive/refresh is done */
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 /* Success */
486 pjsip_regc_destroy(regc);
487 return 0;
488 }
489
490
491 /* Send error on refresh */
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 /* respond code auth contact exp_prm expires more_contacts */
498 { PJ_TRUE, 200, PJ_FALSE, EXACT, TIMEOUT, 0, {NULL, 0}};
499 struct client client_cfg =
500 /* error code have_reg expiration contact_cnt auth? destroy*/
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, &regc);
510 if (ret != 0)
511 return ret;
512
513 /* Reset server response_cnt */
514 registrar.response_cnt = 0;
515
516 /* inject error for transmission */
517 send_mod.count = 0;
518 send_mod.count_before_reject = 0;
519
520 /* reconfigure client */
521 client_result.done = PJ_FALSE;
522 client_result.destroy_on_cb = destroy_on_cb;
523
524 /* Wait until keep-alive/refresh is done */
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 /* Expecting error */
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 /* Send error on refresh */
550 static int update_test(const pj_str_t *registrar_uri)
551 {
552 enum { TIMEOUT = 40 };
553 struct registrar_cfg server_cfg =
554 /* respond code auth contact exp_prm expires more_contacts */
555 { PJ_TRUE, 200, PJ_FALSE, EXACT, TIMEOUT, 0, {NULL, 0}};
556 struct client client_cfg =
557 /* error code have_reg expiration contact_cnt auth? destroy*/
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 /* initially only has 1 contact */
574 ret = do_test("update test", &server_cfg, &client_cfg, registrar_uri,
575 1, &contacts[0], TIMEOUT, PJ_TRUE, &regc);
576 if (ret != 0) {
577 return -600;
578 }
579
580 /*****
581 * replace the contact with new one
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 /* Check that the REGISTER contains two Contacts:
597 * - <sip:a>;expires=0,
598 * - <sip:b>
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 /* must not have other Contact header */
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 /* Destroy tdata */
648 pjsip_tx_data_dec_ref(tdata);
649 tdata = NULL;
650
651
652
653 /**
654 * First loop, it will update with more contacts. Second loop
655 * should do nothing.
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 /* Check that the REGISTER contains two Contacts:
676 * - <sip:b>
677 * - <sip:c>
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 /* must not have other Contact header */
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 /* both contacts must not have expires=0 parameter */
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 /* Destroy tdata */
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 /* send error on authentication */
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 /* respond code auth contact exp_prm expires more_contacts */
731 { PJ_TRUE, 200, PJ_TRUE, EXACT, 75, 0, {NULL, 0}};
732 struct client client_cfg =
733 /* error code have_reg expiration contact_cnt auth? destroy*/
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 /* inject error for second request retry */
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, &regc);
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 /* immediate error */
781 {
782 OFF, /* check_contact */
783 OFF, /* add_xuid_param */
784 "immediate error", /* title */
785 "sip:unresolved-host-xyy", /* alt_registrar */
786 1, /* contact cnt */
787 { "sip:[email protected]:5060" }, /* contacts[] */
788 600, /* expires */
789
790 /* registrar config: */
791 /* respond code auth contact exp_prm expires more_contacts */
792 { PJ_FALSE, 200, PJ_FALSE, NONE, 0, 0, {NULL, 0}},
793
794 /* client expected results: */
795 /* error code have_reg expiration contact_cnt auth?*/
796 { PJ_FALSE, 502, PJ_FALSE, 600, 0, PJ_FALSE}
797 },
798
799 /* timeout test */
800 {
801 OFF, /* check_contact */
802 OFF, /* add_xuid_param */
803 "timeout test (takes ~32 secs)",/* title */
804 NULL, /* alt_registrar */
805 1, /* contact cnt */
806 { "sip:[email protected]:5060" }, /* contacts[] */
807 600, /* expires */
808
809 /* registrar config: */
810 /* respond code auth contact exp_prm expires more_contacts */
811 { PJ_FALSE, 200, PJ_FALSE, NONE, 0, 0, {NULL, 0}},
812
813 /* client expected results: */
814 /* error code have_reg expiration contact_cnt auth? */
815 { PJ_FALSE, 408, PJ_FALSE, 600, 0, PJ_FALSE}
816 },
817
818 /* Basic successful registration scenario:
819 * a good registrar returns the Contact header as is and
820 * add expires parameter. In this test no additional bindings
821 * are returned.
822 */
823 {
824 ON_OFF, /* check_contact */
825 ON_OFF, /* add_xuid_param */
826 "basic", /* title */
827 NULL, /* alt_registrar */
828 1, /* contact cnt */
829 { "<sip:[email protected]:5060;transport=udp;x-param=1234>" }, /* contacts[] */
830 600, /* expires */
831
832 /* registrar config: */
833 /* respond code auth contact exp_prm expires more_contacts */
834 { PJ_TRUE, 200, PJ_FALSE, EXACT, 75, 65, {NULL, 0}},
835
836 /* client expected results: */
837 /* error code have_reg expiration contact_cnt auth?*/
838 { PJ_FALSE, 200, PJ_TRUE, 75, 1, PJ_FALSE}
839 },
840
841 /* Basic successful registration scenario with authentication
842 */
843 {
844 ON_OFF, /* check_contact */
845 ON_OFF, /* add_xuid_param */
846 "authentication", /* title */
847 NULL, /* alt_registrar */
848 1, /* contact cnt */
849 { "<sip:[email protected]:5060;transport=udp;x-param=1234>" }, /* contacts[] */
850 600, /* expires */
851
852 /* registrar config: */
853 /* respond code auth contact exp_prm expires more_contacts */
854 { PJ_TRUE, 200, PJ_TRUE, EXACT, 75, 65, {NULL, 0}},
855
856 /* client expected results: */
857 /* error code have_reg expiration contact_cnt auth?*/
858 { PJ_FALSE, 200, PJ_TRUE, 75, 1, PJ_TRUE}
859 },
860
861 /* a good registrar returns the Contact header as is and
862 * add expires parameter. Also it adds bindings from other
863 * clients in this test.
864 */
865 {
866 ON_OFF, /* check_contact */
867 ON, /* add_xuid_param */
868 "more bindings in response", /* title */
869 NULL, /* alt_registrar */
870 1, /* contact cnt */
871 { "<sip:[email protected]:5060;transport=udp>" }, /* contacts[] */
872 600, /* expires */
873
874 /* registrar config: */
875 /* respond code auth contact exp_prm expires more_contacts */
876 { PJ_TRUE, 200, PJ_FALSE, EXACT, 75, 65, {"<sip:a@a>;expires=70", 0}},
877
878 /* client expected results: */
879 /* error code have_reg expiration contact_cnt auth?*/
880 { PJ_FALSE, 200, PJ_TRUE, 75, 2, PJ_FALSE}
881 },
882
883
884 /* a bad registrar returns modified Contact header, but it
885 * still returns all parameters intact. In this case
886 * the expiration is taken from the expires param because
887 * of matching xuid param or because the number of
888 * Contact header matches.
889 */
890 {
891 ON_OFF, /* check_contact */
892 ON_OFF, /* add_xuid_param */
893 "registrar modifies Contact header", /* title */
894 NULL, /* alt_registrar */
895 1, /* contact cnt */
896 { "<sip:[email protected]:5060>" }, /* contacts[] */
897 600, /* expires */
898
899 /* registrar config: */
900 /* respond code auth contact exp_prm expires more_contacts */
901 { PJ_TRUE, 200, PJ_FALSE, MODIFIED, 75, 65, {NULL, 0}},
902
903 /* client expected results: */
904 /* error code have_reg expiration contact_cnt auth?*/
905 { PJ_FALSE, 200, PJ_TRUE, 75, 1, PJ_FALSE}
906 },
907
908
909 /* a bad registrar returns modified Contact header, but it
910 * still returns all parameters intact. In addition it returns
911 * bindings from other clients.
912 *
913 * In this case the expiration is taken from the expires param
914 * because add_xuid_param is enabled.
915 */
916 {
917 ON_OFF, /* check_contact */
918 ON, /* add_xuid_param */
919 "registrar modifies Contact header and add bindings", /* title */
920 NULL, /* alt_registrar */
921 1, /* contact cnt */
922 { "<sip:[email protected]:5060>" }, /* contacts[] */
923 600, /* expires */
924
925 /* registrar config: */
926 /* respond code auth contact exp_prm expires more_contacts */
927 { PJ_TRUE, 200, PJ_FALSE, MODIFIED, 75, 65, {"<sip:a@a>;expires=70", 0}},
928
929 /* client expected results: */
930 /* error code have_reg expiration contact_cnt auth?*/
931 { PJ_FALSE, 200, PJ_TRUE, 75, 2, PJ_FALSE}
932 },
933
934
935 /* a bad registrar returns completely different Contact and
936 * all parameters are gone. In this case the expiration is
937 * also taken from the expires param since the number of
938 * header matches.
939 */
940 {
941 ON_OFF, /* check_contact */
942 ON_OFF, /* add_xuid_param */
943 "registrar replaces Contact header", /* title */
944 NULL, /* alt_registrar */
945 1, /* contact cnt */
946 { "<sip:[email protected]:5060>" }, /* contacts[] */
947 600, /* expires */
948
949 /* registrar config: */
950 /* respond code auth contact exp_prm expires more_contacts */
951 { PJ_TRUE, 202, PJ_FALSE, NONE, 0, 65, {"<sip:a@A>;expires=75", 0}},
952
953 /* client expected results: */
954 /* error code have_reg expiration contact_cnt auth?*/
955 { PJ_FALSE, 202, PJ_TRUE, 75, 1, PJ_FALSE}
956 },
957
958
959 /* a bad registrar returns completely different Contact (and
960 * all parameters are gone) and it also includes bindings from
961 * other clients.
962 * In this case the expiration is taken from the Expires header.
963 */
964 {
965 ON_OFF, /* check_contact */
966 ON_OFF, /* add_xuid_param */
967 " as above with additional bindings", /* title */
968 NULL, /* alt_registrar */
969 1, /* contact cnt */
970 { "<sip:[email protected]:5060>" }, /* contacts[] */
971 600, /* expires */
972
973 /* registrar config: */
974 /* respond code auth contact exp_prm expires more_contacts */
975 { PJ_TRUE, 200, PJ_FALSE, NONE, 0, 65, {"<sip:a@A>;expires=75, <sip:b@B;expires=70>", 0}},
976
977 /* client expected results: */
978 /* error code have_reg expiration contact_cnt auth?*/
979 { PJ_FALSE, 200, PJ_TRUE, 65, 2, PJ_FALSE}
980 },
981
982 /* the registrar doesn't return any bindings, but for some
983 * reason it includes an Expires header.
984 * In this case the expiration is taken from the Expires header.
985 */
986 {
987 ON_OFF, /* check_contact */
988 ON_OFF, /* add_xuid_param */
989 "no Contact but with Expires", /* title */
990 NULL, /* alt_registrar */
991 1, /* contact cnt */
992 { "<sip:[email protected]:5060>" }, /* contacts[] */
993 600, /* expires */
994
995 /* registrar config: */
996 /* respond code auth contact exp_prm expires more_contacts */
997 { PJ_TRUE, 200, PJ_FALSE, NONE, 0, 65, {NULL, 0}},
998
999 /* client expected results: */
1000 /* error code have_reg expiration contact_cnt auth?*/
1001 { PJ_FALSE, 200, PJ_TRUE, 65, 0, PJ_FALSE}
1002 },
1003
1004 /* Neither Contact header nor Expires header are present.
1005 * In this case the expiration is taken from the request.
1006 */
1007 {
1008 ON_OFF, /* check_contact */
1009 ON_OFF, /* add_xuid_param */
1010 "no Contact and no Expires", /* title */
1011 NULL, /* alt_registrar */
1012 1, /* contact cnt */
1013 { "<sip:[email protected]:5060>" },/* contacts[] */
1014 600, /* expires */
1015
1016 /* registrar config: */
1017 /* respond code auth contact exp_prm expires more_contacts */
1018 { PJ_TRUE, 200, PJ_FALSE, NONE, 0, 0, {NULL, 0}},
1019
1020 /* client expected results: */
1021 /* error code have_reg expiration contact_cnt auth?*/
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 /* Acquire existing transport, if any */
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 /* Register registrar module */
1054 rc = pjsip_endpt_register_module(endpt, &registrar.mod);
1055 if (rc != PJ_SUCCESS) {
1056 app_perror(" error registering module", rc);
1057 rc = -3;
1058 goto on_return;
1059 }
1060
1061 /* Register send module */
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 /* Fill in the registrar address if it's not specified */
1080 if (t->alt_registrar == NULL) {
1081 reg_uri = registrar_uri;
1082 } else {
1083 reg_uri = pj_str(t->alt_registrar);
1084 }
1085
1086 /* Build contact pj_str_t's */
1087 for (j=0; j<t->contact_cnt; ++j) {
1088 contacts[j] = pj_str(t->contacts[j]);
1089 }
1090
1091 /* Normalize more_contacts field */
1092 if (t->server_cfg.more_contacts.ptr)
1093 t->server_cfg.more_contacts.slen = strlen(t->server_cfg.more_contacts.ptr);
1094
1095 /* Do tests with three combinations:
1096 * - check_contact on/off
1097 * - add_xuid_param on/off
1098 * - destroy_on_callback on/off
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 &reg_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 /* Sleep between test groups to avoid using up too many
1136 * active transactions.
1137 */
1138 pj_thread_sleep(1000);
1139 }
1140
1141 /* keep-alive test */
1142 rc = keep_alive_test(&registrar_uri);
1143 if (rc != 0)
1144 goto on_return;
1145
1146 /* Send error on refresh without destroy on callback */
1147 rc = refresh_error(&registrar_uri, PJ_FALSE);
1148 if (rc != 0)
1149 goto on_return;
1150
1151 /* Send error on refresh, destroy on callback */
1152 rc = refresh_error(&registrar_uri, PJ_TRUE);
1153 if (rc != 0)
1154 goto on_return;
1155
1156 /* Updating contact */
1157 rc = update_test(&registrar_uri);
1158 if (rc != 0)
1159 goto on_return;
1160
1161 /* Send error during auth, don't destroy on callback */
1162 rc = auth_send_error(&registrar_uri, PJ_FALSE);
1163 if (rc != 0)
1164 goto on_return;
1165
1166 /* Send error during auth, destroy on callback */
1167 rc = auth_send_error(&registrar_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, &registrar.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