test_register.c 28.4 KB
Newer Older
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 42 43 44 45 46 47 48
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE test_register.c
 * @brief Test registering, outbound, nat traversal.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Martti Mela <Martti Mela@nokia.com>
 *
 * @date Created: Wed Aug 17 12:12:12 EEST 2005 ppessi
 */

#include "config.h"

#include "test_nua.h"
#include <sofia-sip/su_tag_class.h>

#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
#define __func__ "test_register"
#endif

/* ======================================================================== */
/* Test REGISTER */

49 50 51 52 53 54 55
int test_clear_registrations(struct context *ctx);
int test_outbound_cases(struct context *ctx);
int test_register_a(struct context *ctx);
int test_register_b(struct context *ctx);
int test_register_c(struct context *ctx);
int test_register_refresh(struct context *ctx);

56
int test_register_to_proxy(struct context *ctx)
57 58 59 60 61 62 63 64 65 66 67
{
  return
    test_clear_registrations(ctx) ||
    test_outbound_cases(ctx) ||
    test_register_a(ctx) ||
    test_register_b(ctx) ||
    test_register_c(ctx) ||
    test_register_refresh(ctx);
}

int test_clear_registrations(struct context *ctx)
68 69 70
{
  BEGIN();

71
  struct endpoint *a = &ctx->a,  *b = &ctx->b, *c = &ctx->c;
72 73 74 75 76 77
  struct call *a_reg = a->reg, *b_reg = b->reg, *c_reg = c->reg;

  if (print_headings)
    printf("TEST NUA-2.3.0.1: un-REGISTER a\n");

  TEST_1(a_reg->nh = nua_handle(a->nua, a_reg, TAG_END()));
78
  UNREGISTER(a, a_reg, a_reg->nh, SIPTAG_TO(a->to),
79 80
	     SIPTAG_CONTACT_STR("*"),
	     TAG_END());
81
  run_a_until(ctx, -1, until_final_response);
82 83 84 85 86 87 88 89 90 91 92 93
  AUTHENTICATE(a, a_reg, a_reg->nh,
	       NUTAG_AUTH("Digest:\"test-proxy\":alice:secret"), TAG_END());
  run_a_until(ctx, -1, until_final_response);
  nua_handle_destroy(a_reg->nh);

  if (print_headings)
    printf("TEST NUA-2.3.0.1: PASSED\n");

  if (print_headings)
    printf("TEST NUA-2.3.0.2: un-REGISTER b\n");

  TEST_1(b_reg->nh = nua_handle(b->nua, b_reg, TAG_END()));
94
  UNREGISTER(b, b_reg, b_reg->nh, SIPTAG_TO(b->to),
95 96
	     SIPTAG_CONTACT_STR("*"),
	     TAG_END());
97
  run_b_until(ctx, -1, until_final_response);
98 99 100 101 102 103 104 105 106 107 108 109
  AUTHENTICATE(b, b_reg, b_reg->nh,
	       NUTAG_AUTH("Digest:\"test-proxy\":bob:secret"), TAG_END());
  run_b_until(ctx, -1, until_final_response);
  nua_handle_destroy(b_reg->nh);

  if (print_headings)
    printf("TEST NUA-2.3.0.2: PASSED\n");

  if (print_headings)
    printf("TEST NUA-2.3.0.3: un-REGISTER c\n");

  TEST_1(c_reg->nh = nua_handle(c->nua, c_reg, TAG_END()));
110 111
  UNREGISTER(c, c_reg, c_reg->nh,
	     SIPTAG_FROM(c->to), SIPTAG_TO(c->to),
112 113
	     SIPTAG_CONTACT_STR("*"),
	     TAG_END());
114
  run_c_until(ctx, -1, until_final_response);
115 116 117 118 119 120 121 122
  AUTHENTICATE(c, c_reg, c_reg->nh,
	       NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"), TAG_END());
  run_c_until(ctx, -1, until_final_response);
  nua_handle_destroy(c_reg->nh);

  if (print_headings)
    printf("TEST NUA-2.3.0.3: PASSED\n");

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  END();
}

int test_outbound_cases(struct context *ctx)
{
  BEGIN();

#if 0

  struct endpoint *a = &ctx->a, *x;
  struct call *a_reg = a->reg;
  struct event *e;
  sip_t const *sip;
  sip_contact_t m[1];

138 139
/* REGISTER test

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
   A			R
   |------REGISTER----->|
   |<-------401---------|
   |------REGISTER----->|
   |<-------200---------|
   |			|

*/

  if (print_headings)
    printf("TEST NUA-2.3.1: REGISTER a\n");

  test_proxy_domain_set_expiration(ctx->a.domain, 5, 5, 10);

  TEST_1(a_reg->nh = nua_handle(a->nua, a_reg, TAG_END()));

  sip_contact_init(m);
  m->m_display = "Lissu";
  *m->m_url = *a->contact->m_url;
  m->m_url->url_user = "a";
  m->m_url->url_params = "transport=udp";

  REGISTER(a, a_reg, a_reg->nh, SIPTAG_TO(a->to),
	   NUTAG_OUTBOUND("use-rport no-options-keepalive"),
	   SIPTAG_CONTACT(m),
	   TAG_END());
  run_a_until(ctx, -1, save_until_final_response);

  TEST_1(e = a->events->head);
  TEST_E(e->data->e_event, nua_r_register);
  TEST(e->data->e_status, 401);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST(sip->sip_status->st_status, 401);
  TEST_1(!sip->sip_contact);
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

  AUTHENTICATE(a, a_reg, a_reg->nh,
	       NUTAG_AUTH("Digest:\"test-proxy\":alice:secret"), TAG_END());
  run_a_until(ctx, -1, save_until_final_response);

  TEST_1(e = a->events->head);
  TEST_E(e->data->e_event, nua_r_register);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_contact);
  TEST_S(sip->sip_contact->m_display, "Lissu");
  TEST_S(sip->sip_contact->m_url->url_user, "a");
  TEST_1(strstr(sip->sip_contact->m_url->url_params, "transport=udp"));

  if (ctx->nat) {
    TEST_1(e = a->specials->head);
  }

  test_proxy_domain_set_expiration(ctx->a.domain, 600, 3600, 36000);

  if (print_headings)
    printf("TEST NUA-2.3.1: PASSED\n");

  if (print_headings)
    printf("TEST NUA-2.3.4: refresh REGISTER\n");

  if (!ctx->p) {
    free_events_in_list(ctx, a->events);
    return 0;
  }

  /* Wait for A to refresh its registrations */

  /*
   * Avoid race condition: if X has already refreshed registration
   * with expiration time of 3600 seconds, do not wait for new refresh
   */
  a->next_condition = save_until_final_response;

  for (x = a; x; x = NULL) {
    for (e = x->events->head; e; e = e->next) {
      if (e->data->e_event == nua_r_register &&
	  e->data->e_status == 200 &&
	  (sip = sip_object(e->data->e_msg)) &&
	  sip->sip_contact &&
	  sip->sip_contact->m_expires &&
	  strcmp(sip->sip_contact->m_expires, "3600") == 0) {
	x->next_condition = NULL;
	break;
      }
    }
  }

  run_a_until(ctx, -1, a->next_condition);
230

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  for (e = a->events->head; e; e = e->next) {
    TEST_E(e->data->e_event, nua_r_register);
    TEST(e->data->e_status, 200);
    TEST_1(sip = sip_object(e->data->e_msg));
    TEST_1(sip->sip_contact);
    if (!e->next)
      break;
  }
  TEST_1(e);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_S(sip->sip_contact->m_expires, "3600");
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

  if (print_headings)
    printf("TEST NUA-2.3.4: PASSED\n");

  TEST_1(0);

#endif

  END();
}

int test_register_a(struct context *ctx)
{
  BEGIN();

  struct endpoint *a = &ctx->a;
  struct call *a_reg = a->reg;
  struct event *e;
  sip_t const *sip;
  sip_cseq_t cseq[1];

/* REGISTER test

   A			R
268 269 270 271 272 273 274 275 276 277 278
   |------REGISTER----->|
   |<-------401---------|
   |------REGISTER----->|
   |<-------200---------|
   |			|

*/

  if (print_headings)
    printf("TEST NUA-2.3.1: REGISTER a\n");

279 280
  test_proxy_domain_set_expiration(ctx->a.domain, 5, 5, 10);

281 282 283 284 285 286 287 288 289
  TEST_1(a_reg->nh = nua_handle(a->nua, a_reg, TAG_END()));

  sip_cseq_init(cseq)->cs_seq = 12;
  cseq->cs_method = sip_method_register;
  cseq->cs_method_name = sip_method_name_register;

  REGISTER(a, a_reg, a_reg->nh, SIPTAG_TO(a->to),
	   NUTAG_OUTBOUND("natify options-keepalive validate"),
	   NUTAG_KEEPALIVE(1000),
290 291
	   NUTAG_M_DISPLAY("A&A"),
	   NUTAG_M_USERNAME("a"),
292 293
	   NUTAG_M_PARAMS("foo=bar"),
	   NUTAG_M_FEATURES("q=0.9"),
294 295 296 297 298 299
	   SIPTAG_CSEQ(cseq),
	   TAG_END());
  run_a_until(ctx, -1, save_until_final_response);

  TEST_1(e = a->events->head);
  TEST_1(sip = sip_object(e->data->e_msg));
300
  if (ctx->nat && e->data->e_status == 100) {
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    TEST_E(e->data->e_event, nua_r_register);
    TEST(e->data->e_status, 100);
    TEST(sip->sip_status->st_status, 406);
    /* Check that CSeq included in tags is actually used in the request */
    TEST(sip->sip_cseq->cs_seq, 13);
    TEST_1(!sip->sip_contact);
    TEST_1(e = e->next);
    TEST_1(sip = sip_object(e->data->e_msg));
    TEST(sip->sip_cseq->cs_seq, 14);
  }
  else {
    /* Check that CSeq included in tags is actually used in the request */
    TEST(sip->sip_cseq->cs_seq, 13);
  }
  TEST_E(e->data->e_event, nua_r_register);
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
  TEST(e->data->e_status, 401);
  TEST(sip->sip_status->st_status, 401);
  TEST_1(!sip->sip_contact);
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

  AUTHENTICATE(a, a_reg, a_reg->nh,
	       NUTAG_AUTH("Digest:\"test-proxy\":alice:secret"), TAG_END());
  run_a_until(ctx, -1, save_until_final_response);

  TEST_1(e = a->events->head);
  TEST_E(e->data->e_event, nua_r_register);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_contact);
Pekka Pessi's avatar
Pekka Pessi committed
331 332 333
  { char const *expect_m_display = "\"A&A\"";
    /* VC does not dig \" with TEST_S() */
  TEST_S(sip->sip_contact->m_display, expect_m_display); }
334
  TEST_S(sip->sip_contact->m_url->url_user, "a");
335 336
  TEST_1(strstr(sip->sip_contact->m_url->url_params, "foo=bar"));
  TEST_S(sip->sip_contact->m_q, "0.9");
337 338 339 340 341

  if (ctx->nat) {
    TEST_1(e = a->specials->head);
  }

342 343
  test_proxy_domain_set_expiration(ctx->a.domain, 600, 3600, 36000);

344 345 346
  if (print_headings)
    printf("TEST NUA-2.3.1: PASSED\n");

347 348 349 350 351 352 353 354 355 356 357 358
  END();
}

int test_register_b(struct context *ctx)
{
  BEGIN();

  struct endpoint  *b = &ctx->b;
  struct call *b_reg = b->reg;
  struct event *e;
  sip_t const *sip;

359 360 361
  if (print_headings)
    printf("TEST NUA-2.3.2: REGISTER b\n");

362 363
  test_proxy_domain_set_expiration(ctx->b.domain, 5, 5, 10);

364 365
  TEST_1(b_reg->nh = nua_handle(b->nua, b_reg, TAG_END()));

366 367 368 369 370 371 372 373
  /* Test application-supplied contact */
  {
    sip_contact_t m[1];
    sip_contact_init(m)->m_url[0] = b->contact->m_url[0];

    m->m_display = "B";
    m->m_url->url_user = "b";

374 375 376 377
    /* Include "tcp" transport parameter in Contact */
    if (ctx->p)
      m->m_url->url_params = "transport=tcp";

378
    REGISTER(b, b_reg, b_reg->nh, SIPTAG_TO(b->to),
379
	     SIPTAG_CONTACT(m),
380 381
	     /* Do not include credentials unless challenged */
	     NUTAG_AUTH_CACHE(nua_auth_cache_challenged),
382 383
	     TAG_END());
  }
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  run_ab_until(ctx, -1, save_events, -1, save_until_final_response);

  TEST_1(e = b->events->head);
  TEST_E(e->data->e_event, nua_r_register);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST(e->data->e_status, 401);
  TEST(sip->sip_status->st_status, 401);
  TEST_1(!sip->sip_contact);
  TEST_1(!e->next);
  free_events_in_list(ctx, b->events);

  AUTHENTICATE(b, b_reg, b_reg->nh,
	       NUTAG_AUTH("Digest:\"test-proxy\":bob:secret"), TAG_END());
  run_ab_until(ctx, -1, save_events, -1, save_until_final_response);

  TEST_1(e = b->events->head);
  TEST_E(e->data->e_event, nua_r_register);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_contact);
404 405
  TEST_S(sip->sip_contact->m_display, "B");
  TEST_S(sip->sip_contact->m_url->url_user, "b");
406
  free_events_in_list(ctx, b->events);
407

408 409
  test_proxy_domain_set_expiration(ctx->b.domain, 600, 3600, 36000);

410 411 412
  if (print_headings)
    printf("TEST NUA-2.3.2: PASSED\n");

413 414 415 416 417 418 419 420 421 422 423
  END();
}

int test_register_c(struct context *ctx)
{
  BEGIN();

  struct endpoint *c = &ctx->c;
  struct call *c_reg = c->reg;
  struct event *e;
  sip_t const *sip;
424 425 426 427

  if (print_headings)
    printf("TEST NUA-2.3.3: REGISTER c\n");

428
  test_proxy_domain_set_expiration(ctx->c.domain, 600, 3600, 36000);
429
  test_proxy_domain_set_authorize(ctx->c.domain, "test-proxy-0");
430

431 432
  TEST_1(c_reg->nh = nua_handle(c->nua, c_reg, TAG_END()));

433 434
  REGISTER(c, c_reg, c_reg->nh, SIPTAG_TO(c->to),
	   SIPTAG_FROM(c->to),
435
	   NUTAG_OUTBOUND(NULL),
436 437
	   NUTAG_M_DISPLAY("C"),
	   NUTAG_M_USERNAME("c"),
438 439 440
	   NUTAG_M_PARAMS("c=1"),
	   NUTAG_M_FEATURES("q=0.987;expires=5"),
	   NUTAG_CALLEE_CAPS(1),
441 442
	   SIPTAG_EXPIRES_STR("5"), /* Test 423 negotiation */
	   TAG_END());
443
  run_abc_until(ctx, -1, save_events, -1, save_events,
444 445 446 447 448 449 450 451 452 453 454 455
		-1, save_until_final_response);

  TEST_1(e = c->events->head);
  TEST_E(e->data->e_event, nua_r_register);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST(e->data->e_status, 401);
  TEST(sip->sip_status->st_status, 401);
  TEST_1(!sip->sip_contact);
  TEST_1(!e->next);
  free_events_in_list(ctx, c->events);

  AUTHENTICATE(c, c_reg, c_reg->nh,
456
	       NUTAG_AUTH("Digest:\"test-proxy-0\":charlie:secret"), TAG_END());
457
  run_abc_until(ctx, -1, save_events, -1, save_events,
458 459 460 461 462 463 464 465
		-1, save_until_final_response);

  TEST_1(e = c->events->head);
  TEST_E(e->data->e_event, nua_r_register);
  TEST(e->data->e_status, 100);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST(sip->sip_status->st_status, 423);
  TEST_1(e = e->next);
466 467 468 469 470 471
  if (e->data->e_status == 100 && e->data->e_event == nua_r_register) {
    TEST_1(sip = sip_object(e->data->e_msg));
    TEST(sip->sip_status->st_status, 401);
    TEST_1(e = e->next);
  }
  TEST(e->data->e_status, 200); TEST_E(e->data->e_event, nua_r_register);
472 473
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_contact);
474 475
  TEST_S(sip->sip_contact->m_display, "C");
  TEST_S(sip->sip_contact->m_url->url_user, "c");
476 477 478
  TEST_1(strstr(sip->sip_contact->m_url->url_params, "c=1"));
  TEST_S(sip->sip_contact->m_q, "0.987");
  TEST_1(msg_header_find_param(sip->sip_contact->m_common, "methods="));
479 480 481 482 483 484
  TEST_1(!e->next);
  free_events_in_list(ctx, c->events);

  if (print_headings)
    printf("TEST NUA-2.3.3: PASSED\n");

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  END();
}

int test_register_refresh(struct context *ctx)
{
  BEGIN();

  struct endpoint *a = &ctx->a,  *b = &ctx->b, *x;
  struct event *e;
  sip_t const *sip;
  int seen_401;

  if (print_headings)
    printf("TEST NUA-2.3.4: refresh REGISTER\n");

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  if (!ctx->p) {
    free_events_in_list(ctx, a->events);
    free_events_in_list(ctx, b->events);
    return 0;
  }

  /* Wait for A and B to refresh their registrations */

  /*
   * Avoid race condition: if X has already refreshed registration
   * with expiration time of 3600 seconds, do not wait for new refresh
   */
  a->next_condition = save_until_final_response;
  b->next_condition = save_until_final_response;

  for (x = a; x; x = x == a ? b : NULL) {
    for (e = x->events->head; e; e = e->next) {
      if (e->data->e_event == nua_r_register &&
	  e->data->e_status == 200 &&
	  (sip = sip_object(e->data->e_msg)) &&
	  sip->sip_contact &&
	  sip->sip_contact->m_expires &&
	  strcmp(sip->sip_contact->m_expires, "3600") == 0) {
	x->next_condition = NULL;
	break;
      }
    }
  }

  run_ab_until(ctx, -1, a->next_condition, -1, b->next_condition);
530

531 532 533 534 535 536 537 538 539
  for (e = a->events->head; e; e = e->next) {
    TEST_E(e->data->e_event, nua_r_register);
    TEST(e->data->e_status, 200);
    TEST_1(sip = sip_object(e->data->e_msg));
    TEST_1(sip->sip_contact);
    if (!e->next)
      break;
  }
  TEST_1(e);
540
  TEST_1(sip = sip_object(e->data->e_msg));
541 542 543 544
  TEST_S(sip->sip_contact->m_expires, "3600");
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

545 546
  seen_401 = 0;

547 548 549
  for (e = b->events->head; e; e = e->next) {
    TEST_E(e->data->e_event, nua_r_register);
    TEST_1(sip = sip_object(e->data->e_msg));
550 551 552 553 554 555 556 557 558 559

    if (e->data->e_status == 200) {
      TEST(e->data->e_status, 200);
      TEST_1(seen_401);
      TEST_1(sip->sip_contact);
    }
    else if (sip->sip_status && sip->sip_status->st_status == 401) {
      seen_401 = 1;
    }

560 561 562 563 564 565 566 567 568 569 570
    if (!e->next)
      break;
  }
  TEST_1(e);
  TEST_S(sip->sip_contact->m_expires, "3600");
  TEST_1(!e->next);
  free_events_in_list(ctx, b->events);

  if (print_headings)
    printf("TEST NUA-2.3.4: PASSED\n");

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
  if (!ctx->p)
    return 0;

  if (print_headings)
    printf("TEST NUA-2.3.5: re-REGISTER when TCP connection is closed\n");

  test_proxy_close_tports(ctx->p);

  run_b_until(ctx, -1, save_until_final_response);

  TEST_1(e = b->events->head);
  TEST_E(e->data->e_event, nua_r_register);
  if (e->data->e_status == 100)
    TEST_1(e = e->next);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_contact);
  TEST_S(sip->sip_contact->m_expires, "3600");
  TEST_1(!e->next);

  free_events_in_list(ctx, b->events);

  if (print_headings)
    printf("TEST NUA-2.3.5: PASSED\n");

595 596 597
  END();
}

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
int registrar_299(CONDITION_PARAMS)
{
  msg_t *request = nua_current_request(nua);

  save_event_in_list(ctx, event, ep, ep->call);

  if (event == nua_i_register) {
    RESPOND(ep, call, nh, 299, "YES", NUTAG_WITH(request), TAG_END());
    return 1;
  }

  return 0;
}

int test_register_to_c(struct context *ctx)
{
  BEGIN();

  struct endpoint *b = &ctx->b, *c = &ctx->c;
  struct call *b_call = b->call, *c_call = c->call;
  struct event *e;
  sip_t const *sip;

  if (print_headings)
622
    printf("TEST NUA-2.6.1: REGISTER b to c\n");
623 624 625 626 627 628 629 630

  nua_set_params(ctx->c.nua,
		 NUTAG_ALLOW("REGISTER"),
		 TAG_END());
  run_c_until(ctx, nua_r_set_params, until_final_response);

  TEST_1(b_call->nh = nua_handle(b->nua, b_call, TAG_END()));

631
  REGISTER(b, b_call, b_call->nh,
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
	   NUTAG_REGISTRAR((url_string_t *)c->contact->m_url),
	   SIPTAG_TO(b->to),
	   NUTAG_OUTBOUND(NULL),
	   SIPTAG_CONTACT_STR(NULL),
	   TAG_END());
  run_bc_until(ctx, -1, save_until_final_response, -1, registrar_299);

  TEST_1(e = b->events->head);
  TEST_E(e->data->e_event, nua_r_register);
  TEST(e->data->e_status, 299);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(!sip->sip_contact);

  free_events_in_list(ctx, b->events);
  nua_handle_destroy(b_call->nh), b_call->nh = NULL;

648 649 650 651 652 653
  TEST_1(e = c->events->head);
  TEST_E(e->data->e_event, nua_i_register);
  TEST(e->data->e_status, 100);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(!sip->sip_contact);

654 655 656 657
  free_events_in_list(ctx, c->events);
  nua_handle_destroy(c_call->nh), c_call->nh = NULL;

  if (print_headings)
658
    printf("TEST NUA-2.6.1: PASSED\n");
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

  END();
}


int test_register(struct context *ctx)
{
  if (test_register_to_c(ctx))
    return 1;

  if (ctx->proxy_tests)
    if (test_register_to_proxy(ctx)) return 1;

  return 0;
}


676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
int test_connectivity(struct context *ctx)
{
  if (!ctx->proxy_tests)
    return 0;			/* No proxy */

  BEGIN();

  struct endpoint *a = &ctx->a,  *b = &ctx->b, *c = &ctx->c;
  struct call *a_call = a->call, *b_call = b->call, *c_call = c->call;
  struct event *e;
  sip_t const *sip;

  /* Connectivity test using OPTIONS */

  if (print_headings)
    printf("TEST NUA-2.4.1: OPTIONS from A to B\n");

  TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));

  OPTIONS(a, a_call, a_call->nh,
	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
697
	  NUTAG_ALLOW("OPTIONS"),
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	  TAG_END());

  run_ab_until(ctx, -1, save_until_final_response, -1, save_until_received);

  /* Client events: nua_options(), nua_r_options */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_options);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_allow); TEST_1(sip->sip_accept); TEST_1(sip->sip_supported);
  /* TEST_1(sip->sip_content_type); */
  /* TEST_1(sip->sip_payload); */
  TEST_1(!e->next);

  free_events_in_list(ctx, a->events);
  nua_handle_destroy(a_call->nh), a_call->nh = NULL;

  /* Server events: nua_i_options */
  TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_options);
  TEST(e->data->e_status, 200);
  TEST_1(!e->next);

  free_events_in_list(ctx, b->events);
  nua_handle_destroy(b_call->nh), b_call->nh = NULL;

  if (print_headings)
    printf("TEST NUA-2.4.1: PASSED\n");

  if (print_headings)
    printf("TEST NUA-2.4.2: OPTIONS from B to C\n");

  TEST_1(b_call->nh = nua_handle(b->nua, b_call, SIPTAG_TO(c->to), TAG_END()));

  OPTIONS(b, b_call, b_call->nh,
	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(c->contact->m_url)),
	  TAG_END());

  run_abc_until(ctx, -1, NULL,
		-1, save_until_final_response,
		-1, save_until_received);

  /* Client events: nua_options(), nua_r_options */
  TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_r_options);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_allow); TEST_1(sip->sip_accept); TEST_1(sip->sip_supported);
  /* TEST_1(sip->sip_content_type); */
  /* TEST_1(sip->sip_payload); */
  TEST_1(!e->next);

  free_events_in_list(ctx, b->events);
  nua_handle_destroy(b_call->nh), b_call->nh = NULL;

  /* Server events: nua_i_options */
  TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_i_options);
  TEST(e->data->e_status, 200);
  TEST_1(!e->next);

  free_events_in_list(ctx, c->events);
  nua_handle_destroy(c_call->nh), c_call->nh = NULL;

  if (print_headings)
    printf("TEST NUA-2.4.2: PASSED\n");

  if (print_headings)
    printf("TEST NUA-2.4.3: OPTIONS from C to A\n");

  TEST_1(c_call->nh = nua_handle(c->nua, c_call, SIPTAG_TO(a->to), TAG_END()));

  OPTIONS(c, c_call, c_call->nh,
767
	  SIPTAG_FROM(c->to),
768 769 770 771 772 773 774 775 776 777 778 779 780
	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(a->contact->m_url)),
	  TAG_END());

  if (ctx->proxy_tests) {
    run_abc_until(ctx, -1, NULL, -1, NULL, -1, save_until_final_response);

    /* Client events: nua_options(), nua_r_options */
    TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_r_options);
    TEST(e->data->e_status, 407);
    TEST_1(!e->next);

    free_events_in_list(ctx, c->events);

781
    /* Sneakily change the realm */
782

783
    TEST(test_proxy_domain_set_authorize(ctx->c.domain, "test-proxy"), 0);
784

785
    AUTHENTICATE(c, c_call, c_call->nh,
786
		 NUTAG_AUTH("Digest:\"test-proxy-0\":charlie:secret"),
787
		 TAG_END());
788 789 790 791 792 793 794 795 796 797 798

    run_abc_until(ctx, -1, NULL, -1, NULL, -1, save_until_final_response);

    /* Client events: nua_options(), nua_r_options */
    TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_r_options);
    TEST(e->data->e_status, 407);
    TEST_1(!e->next);

    free_events_in_list(ctx, c->events);

    AUTHENTICATE(c, c_call, c_call->nh,
799
		 NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"),
800
		 TAG_END());
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
  }

  run_abc_until(ctx, -1, save_until_received,
		-1, NULL,
		-1, save_until_final_response);

  /* Client events: nua_options(), nua_r_options */
  TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_r_options);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_allow); TEST_1(sip->sip_accept); TEST_1(sip->sip_supported);
  /* TEST_1(sip->sip_content_type); */
  /* TEST_1(sip->sip_payload); */
  TEST_1(!e->next);

  free_events_in_list(ctx, c->events);
  nua_handle_destroy(c_call->nh), c_call->nh = NULL;

  /* Server events: nua_i_options */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_i_options);
  TEST(e->data->e_status, 200);
  TEST_1(!e->next);

  free_events_in_list(ctx, a->events);
  nua_handle_destroy(a_call->nh), a_call->nh = NULL;

  if (print_headings)
    printf("TEST NUA-2.4.3: PASSED\n");

  END();
}

int test_nat_timeout(struct context *ctx)
{
  if (!ctx->proxy_tests || !ctx->nat)
    return 0;			/* No proxy */

  BEGIN();

840 841
  struct endpoint *a = &ctx->a,  *b = &ctx->b;
  struct call *a_call = a->call, *b_call = b->call;
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
  struct event *e;
  sip_t const *sip;

  /* Test what happens when NAT bindings go away */

  if (print_headings)
    printf("TEST NUA-2.5.1: NAT binding change\n");

  free_events_in_list(ctx, a->specials);

  test_nat_flush(ctx->nat);	/* Break our connections */

  /* Run until we get final response to REGISTER */
  run_a_until(ctx, -1, save_until_final_response);

  TEST_1(e = a->specials->head);
  TEST_E(e->data->e_event, nua_i_outbound);
  TEST(e->data->e_status, 102);
  TEST_S(e->data->e_phrase, "NAT binding changed");
  TEST_1(!e->next);

  free_events_in_list(ctx, a->specials);

  TEST_1(e = a->events->head);
  TEST_E(e->data->e_event, nua_r_register);
  TEST(e->data->e_status, 200);
  TEST_1(!e->next);

  free_events_in_list(ctx, a->events);

  if (print_headings)
    printf("TEST NUA-2.5.1: PASSED\n");
874

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
  if (print_headings)
    printf("TEST NUA-2.5.2: OPTIONS from B to A\n");

  TEST_1(b_call->nh = nua_handle(b->nua, b_call, SIPTAG_TO(a->to), TAG_END()));

  OPTIONS(b, b_call, b_call->nh, TAG_END());

  run_ab_until(ctx, -1, save_until_received,
	       -1, save_until_final_response);

  /* Client events: nua_options(), nua_r_options */
  TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_r_options);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_allow); TEST_1(sip->sip_accept); TEST_1(sip->sip_supported);
  /* TEST_1(sip->sip_content_type); */
  /* TEST_1(sip->sip_payload); */
  TEST_1(!e->next);

  free_events_in_list(ctx, b->events);
  nua_handle_destroy(b_call->nh), b_call->nh = NULL;

  /* Server events: nua_i_options */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_i_options);
  TEST(e->data->e_status, 200);
  TEST_1(!e->next);

  free_events_in_list(ctx, a->events);
  nua_handle_destroy(a_call->nh), a_call->nh = NULL;

  if (print_headings)
    printf("TEST NUA-2.5.2: PASSED\n");
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938

  END();
}

int test_unregister(struct context *ctx)
{
  if (!ctx->proxy_tests)
    return 0;			/* No proxy */

  BEGIN();

  struct endpoint *a = &ctx->a,  *b = &ctx->b, *c = &ctx->c;
  struct event *e;
  sip_t const *sip;

/* un-REGISTER test

   A			B
   |----un-REGISTER---->|
   |<-------200---------|
   |			|

*/
  if (print_headings)
    printf("TEST NUA-13.1: un-REGISTER a\n");

  if (a->reg->nh) {
    free_events_in_list(ctx, a->events);
    UNREGISTER(a, NULL, a->reg->nh, TAG_END());
    run_a_until(ctx, -1, save_until_final_response);
    TEST_1(e = a->events->head);
    TEST_E(e->data->e_event, nua_r_unregister);
939 940 941 942
    if (e->data->e_status == 100) {
      TEST_1(e = e->next);
      TEST_E(e->data->e_event, nua_r_unregister);
    }
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
    TEST(e->data->e_status, 200);
    TEST_1(sip = sip_object(e->data->e_msg));
    TEST_1(!sip->sip_contact);
    TEST_1(!e->next);
    free_events_in_list(ctx, a->events);
    nua_handle_destroy(a->reg->nh), a->reg->nh = NULL;
  }

  if (print_headings)
    printf("TEST NUA-13.1: PASSED\n");

  if (print_headings)
    printf("TEST NUA-13.2: un-REGISTER b\n");

  if (b->reg->nh) {
    free_events_in_list(ctx, b->events);
    UNREGISTER(b, NULL, b->reg->nh, TAG_END());
    run_b_until(ctx, -1, save_until_final_response);
    TEST_1(e = b->events->head);
    TEST_E(e->data->e_event, nua_r_unregister);
963 964 965 966
    if (e->data->e_status == 100) {
      TEST_1(e = e->next);
      TEST_E(e->data->e_event, nua_r_unregister);
    }
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
    TEST(e->data->e_status, 200);
    TEST_1(sip = sip_object(e->data->e_msg));
    TEST_1(!sip->sip_contact);
    TEST_1(!e->next);
    free_events_in_list(ctx, b->events);
    nua_handle_destroy(b->reg->nh), b->reg->nh = NULL;
  }
  if (print_headings)
    printf("TEST NUA-13.2: PASSED\n");

  if (print_headings)
    printf("TEST NUA-13.3: un-REGISTER c\n");

  /* Unregister using another handle */
  free_events_in_list(ctx, c->events);
  TEST_1(c->call->nh = nua_handle(c->nua, c->call, TAG_END()));
983
  UNREGISTER(c, c->call, c->call->nh, SIPTAG_TO(c->to), SIPTAG_FROM(c->to),
984 985
	     NUTAG_M_DISPLAY("C"),
	     NUTAG_M_USERNAME("c"),
986
	     NUTAG_M_PARAMS("c=1"),
987
	     TAG_END());
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
  run_c_until(ctx, -1, save_until_final_response);

  TEST_1(e = c->events->head);
  TEST_E(e->data->e_event, nua_r_unregister);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST(e->data->e_status, 401);
  TEST(sip->sip_status->st_status, 401);
  TEST_1(!sip->sip_contact);
  TEST_1(!e->next);
  free_events_in_list(ctx, c->events);

  AUTHENTICATE(c, c->call, c->call->nh,
	       NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"), TAG_END());
  run_c_until(ctx, -1, save_until_final_response);

  TEST_1(e = c->events->head);
  TEST_E(e->data->e_event, nua_r_unregister);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(!sip->sip_contact);
  TEST_1(!e->next);
  free_events_in_list(ctx, c->events);
  nua_handle_destroy(c->call->nh), c->call->nh = NULL;

  if (c->reg->nh) {
    UNREGISTER(c, NULL, c->reg->nh, TAG_END());
    run_c_until(ctx, -1, save_until_final_response);
    TEST_1(e = c->events->head);
    TEST_E(e->data->e_event, nua_r_unregister);
1017 1018 1019 1020
    if (e->data->e_status == 100) {
      TEST_1(e = e->next);
      TEST_E(e->data->e_event, nua_r_unregister);
    }
1021 1022 1023 1024 1025 1026 1027 1028 1029
    if (e->data->e_status == 401) {
      TEST_1(!e->next);
      free_events_in_list(ctx, c->events);
      AUTHENTICATE(c, NULL, c->reg->nh,
		   NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"), TAG_END());
      run_c_until(ctx, -1, save_until_final_response);
      TEST_1(e = c->events->head);
      TEST_E(e->data->e_event, nua_r_unregister);
    }
1030 1031
    TEST(e->data->e_status, 200);
    TEST_1(sip = sip_object(e->data->e_msg));
1032
    TEST_1(sip->sip_from->a_url->url_user);
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
    TEST_1(!sip->sip_contact);
    TEST_1(!e->next);
    free_events_in_list(ctx, c->events);
    nua_handle_destroy(c->reg->nh), c->reg->nh = NULL;
  }

  if (print_headings)
    printf("TEST NUA-13.3: PASSED\n");

  END();
}