test_register.c 23.2 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
int test_register_to_proxy(struct context *ctx)
50 51 52 53 54 55 56 57
{
  BEGIN();

  struct endpoint *a = &ctx->a,  *b = &ctx->b, *c = &ctx->c, *x;
  struct call *a_reg = a->reg, *b_reg = b->reg, *c_reg = c->reg;
  struct event *e;
  sip_t const *sip;
  sip_cseq_t cseq[1];
58
  int seen_401;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

  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()));
  UNREGISTER(a, a_reg, a_reg->nh, SIPTAG_TO(a->to), 
	     SIPTAG_CONTACT_STR("*"),
	     TAG_END());
  run_a_until(ctx, -1, until_final_response);  
  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()));
  UNREGISTER(b, b_reg, b_reg->nh, SIPTAG_TO(b->to), 
	     SIPTAG_CONTACT_STR("*"),
	     TAG_END());
  run_b_until(ctx, -1, until_final_response);  
  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()));
  UNREGISTER(c, c_reg, c_reg->nh, SIPTAG_TO(c->to), 
	     SIPTAG_CONTACT_STR("*"),
	     TAG_END());
  run_c_until(ctx, -1, until_final_response);  
  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");

/* REGISTER test

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

*/

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

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

124 125 126 127 128 129 130 131 132
  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),
133 134
	   NUTAG_M_DISPLAY("A&A"),
	   NUTAG_M_USERNAME("a"),
135 136
	   NUTAG_M_PARAMS("foo=bar"),
	   NUTAG_M_FEATURES("q=0.9"),
137 138 139 140 141 142
	   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));
143
  if (ctx->nat && e->data->e_status == 100) {
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    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);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  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
174 175 176
  { char const *expect_m_display = "\"A&A\"";
    /* VC does not dig \" with TEST_S() */
  TEST_S(sip->sip_contact->m_display, expect_m_display); }
177
  TEST_S(sip->sip_contact->m_url->url_user, "a");
178 179
  TEST_1(strstr(sip->sip_contact->m_url->url_params, "foo=bar"));
  TEST_S(sip->sip_contact->m_q, "0.9");
180 181 182 183 184

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

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

187 188 189 190 191 192
  if (print_headings)
    printf("TEST NUA-2.3.1: PASSED\n");

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

193 194
  test_proxy_domain_set_expiration(ctx->b.domain, 5, 5, 10);

195 196
  TEST_1(b_reg->nh = nua_handle(b->nua, b_reg, TAG_END()));

197 198 199 200 201 202 203 204
  /* 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";

205 206 207 208
    /* Include "tcp" transport parameter in Contact */
    if (ctx->p)
      m->m_url->url_params = "transport=tcp";

209 210
    REGISTER(b, b_reg, b_reg->nh, SIPTAG_TO(b->to), 
	     SIPTAG_CONTACT(m),
211 212
	     /* Do not include credentials unless challenged */
	     NUTAG_AUTH_CACHE(nua_auth_cache_challenged),
213 214
	     TAG_END());
  }
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  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);
235 236
  TEST_S(sip->sip_contact->m_display, "B");
  TEST_S(sip->sip_contact->m_url->url_user, "b");
237
  free_events_in_list(ctx, b->events);
238

239 240 241
  if (print_headings)
    printf("TEST NUA-2.3.2: PASSED\n");

242
  test_proxy_domain_set_expiration(ctx->b.domain, 600, 3600, 36000);
243 244 245 246

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

247 248 249
  test_proxy_domain_set_expiration(ctx->c.domain, 600, 3600, 36000);
  test_proxy_domain_set_authorize(ctx->c.domain, 2);

250 251 252
  TEST_1(c_reg->nh = nua_handle(c->nua, c_reg, TAG_END()));

  REGISTER(c, c_reg, c_reg->nh, SIPTAG_TO(c->to), 
253
	   NUTAG_OUTBOUND(NULL),
254 255
	   NUTAG_M_DISPLAY("C"),
	   NUTAG_M_USERNAME("c"),
256 257 258
	   NUTAG_M_PARAMS("c=1"),
	   NUTAG_M_FEATURES("q=0.987;expires=5"),
	   NUTAG_CALLEE_CAPS(1),
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	   SIPTAG_EXPIRES_STR("5"), /* Test 423 negotiation */
	   TAG_END());
  run_abc_until(ctx, -1, save_events, -1, save_events, 
		-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,
	       NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"), TAG_END());
  run_abc_until(ctx, -1, save_events, -1, save_events, 
		-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);
284 285 286 287 288 289
  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);
290 291
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_contact);
292 293
  TEST_S(sip->sip_contact->m_display, "C");
  TEST_S(sip->sip_contact->m_url->url_user, "c");
294 295 296
  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="));
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  TEST_1(!e->next);
  free_events_in_list(ctx, c->events);

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

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

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

  /* 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);
  
  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_S(sip->sip_contact->m_expires, "3600");
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

350 351
  seen_401 = 0;

352 353 354
  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));
355 356 357 358 359 360 361 362 363 364

    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;
    }

365 366 367 368 369 370 371 372 373 374 375
    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");

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  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");

400 401 402
  END();
}

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
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)
427
    printf("TEST NUA-2.6.1: REGISTER b to c\n");
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

  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()));

  REGISTER(b, b_call, b_call->nh, 
	   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;

453 454 455 456 457 458
  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);

459 460 461 462
  free_events_in_list(ctx, c->events);
  nua_handle_destroy(c_call->nh), c_call->nh = NULL;

  if (print_headings)
463
    printf("TEST NUA-2.6.1: PASSED\n");
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

  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;
}


481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
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)),
502
	  NUTAG_ALLOW("OPTIONS"),
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 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
	  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,
	  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);

    AUTHENTICATE(c, c_call, c_call->nh,
		 NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"),
		 TAG_END());
  }

  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();

627 628
  struct endpoint *a = &ctx->a,  *b = &ctx->b;
  struct call *a_call = a->call, *b_call = b->call;
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
  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");
  
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
  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");
694 695 696 697 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

  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);
726 727 728 729
    if (e->data->e_status == 100) {
      TEST_1(e = e->next);
      TEST_E(e->data->e_event, nua_r_unregister);
    }
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    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);
750 751 752 753
    if (e->data->e_status == 100) {
      TEST_1(e = e->next);
      TEST_E(e->data->e_event, nua_r_unregister);
    }
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
    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()));
770 771 772
  UNREGISTER(c, c->call, c->call->nh, SIPTAG_TO(c->to), 
	     NUTAG_M_DISPLAY("C"),
	     NUTAG_M_USERNAME("c"),
773
	     NUTAG_M_PARAMS("c=1"),
774
	     TAG_END());
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
  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);
804 805 806 807
    if (e->data->e_status == 100) {
      TEST_1(e = e->next);
      TEST_E(e->data->e_event, nua_r_unregister);
    }
808 809 810 811 812 813 814 815 816 817 818 819 820
    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->reg->nh), c->reg->nh = NULL;
  }

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

  END();
}