test_refer.c 22.3 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_nua_re_invite.c
 * @brief Test re_inviteing, 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_call_hold"
#endif

/* ======================================================================== */
/* NUA-9 tests: REFER */

49 50 51 52
int test_refer0(struct context *ctx, char const *tests,
		int refer_with_id, int notify_by_appl);

int notify_until_terminated(CONDITION_PARAMS);
53 54 55 56

int test_refer(struct context *ctx)
{
  /* test twice, once without id and once with id */
57
  return
58 59 60 61
    test_refer0(ctx, "NUA-9.1", 0, 0) ||
    test_refer0(ctx, "NUA-9.2", 1, 0) ||
    test_refer0(ctx, "NUA-9.3", 0, 1) ||
    test_refer0(ctx, "NUA-9.4", 1, 1);
62 63
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77
/* Referred call:

   A			B
   |			|
   |-------INVITE------>|
   |<----100 Trying-----|
   |			|
   |<----180 Ringing----|
   |			|
   |<------200 OK-------|
   |--------ACK-------->|
   |			|
   |<------REFER--------|
   |-------200 OK------>|			C
78 79
  [|-------NOTIFY------>|]			|
  [|<------200 OK-------|]			|
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
   |			|			|
   |			|			|
   |<-----SUBSCRIBE-----|                       |
   |-------200 OK------>|			|
   |			|			|
   |			|			|
   |-----------------INVITE-------------------->|
   |			|			|
   |<------------------180----------------------|
   |-------NOTIFY------>|			|
   |<------200 OK-------|			|
   |			|			|
   |<------------------200----------------------|
   |-------NOTIFY------>|			|
   |<------200 OK-------|			|
   |-------------------ACK--------------------->|
   |			|			|
   |--------BYE-------->|			|
   |<------200 OK-------|			|
   |			X			|
   |			 			|
   |-------------------BYE--------------------->|
   |<------------------200----------------------|
   |						|

*/

107 108
int test_refer0(struct context *ctx, char const *tests,
		int refer_with_id, int notify_by_appl)
109 110 111 112 113
{
  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;
114 115
  struct call *a_refer, *a_c2, *b_refer;
  struct eventlist *a_revents, *b_revents;
116
  struct event *e, *notify_e;
117
  sip_t const *sip;
118
  sip_event_t const *a_event, *b_event;
119 120 121 122 123 124 125 126 127 128 129
  sip_refer_to_t const *refer_to;
  sip_referred_by_t const *referred_by;

  sip_refer_to_t r0[1];
  sip_to_t to[1];

  su_home_t tmphome[SU_HOME_AUTO_SIZE(16384)];

  su_home_auto(tmphome, sizeof(tmphome));

  if (print_headings)
130 131 132 133
    printf("TEST %s: REFER: refer A to C%s%s%s\n", tests,
	   refer_with_id ? " with Event id" : "",
	   refer_with_id && !notify_by_appl ? " and" : "",
	   !notify_by_appl ? " nua generating the NOTIFYs" : "");
134 135 136 137 138 139 140

  if (print_headings)
    printf("TEST %s.1: REFER: make a call between A and B\n", tests);

  /* Do (not) include id with first implicit Event: refer */
  nua_set_params(ctx->a.nua, NUTAG_REFER_WITH_ID(refer_with_id), TAG_END());
  run_a_until(ctx, nua_r_set_params, until_final_response);
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  if (refer_with_id) {
    TEST_1(a_refer = calloc(1, (sizeof *a_refer) + (sizeof *a_refer->events)));
    call_init(a_refer);
    a_refer->events = (void *)(a_refer + 1);
    eventlist_init(a_refer->events);

    a_call->next = a_refer;
    a_revents = a_refer->events;

    TEST_1(b_refer = calloc(1, (sizeof *b_refer) + (sizeof *b_refer->events)));
    call_init(b_refer);
    b_refer->events = (void *)(b_refer + 1);
    eventlist_init(b_refer->events);

    b_call->next = b_refer;
    b_revents = b_refer->events;
  }
  else {
    a_refer = a_call, b_refer = b_call;
    a_revents = a->events, b_revents = b->events;
  }

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
  TEST_1(a_c2 = calloc(1, (sizeof *a_c2) + (sizeof *a_c2->events)));
  call_init(a_c2);
  a_c2->events = (void *)(a_c2 + 1);
  eventlist_init(a_c2->events);

  a_call->sdp = "m=audio 5008 RTP/AVP 8";
  b_call->sdp = "m=audio 5010 RTP/AVP 0 8";
  a_c2->sdp   = "m=audio 5012 RTP/AVP 8";
  c_call->sdp = "m=audio 5014 RTP/AVP 0 8";

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

  INVITE(a, a_call, a_call->nh,
	 TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
	 SOATAG_USER_SDP_STR(a_call->sdp),
	 TAG_END());

  run_ab_until(ctx, -1, until_ready, -1, accept_call);

  /* Client transitions:
     INIT -(C1)-> CALLING: nua_invite(), nua_i_state
     CALLING -(C2)-> PROCEEDING: nua_r_invite, nua_i_state
     PROCEEDING -(C3+C4)-> READY: nua_r_invite, nua_i_state
  */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_calling); /* CALLING */
  TEST_1(is_offer_sent(e->data->e_tags));
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_invite);
  TEST(e->data->e_status, 180);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_proceeding); /* PROCEEDING */
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_invite);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_ready); /* READY */
  TEST_1(is_answer_recv(e->data->e_tags));
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

  /*
   Server transitions:
   INIT -(S1)-> RECEIVED: nua_i_invite, nua_i_state
   RECEIVED -(S2a)-> EARLY: nua_respond(), nua_i_state
   EARLY -(S3b)-> COMPLETED: nua_respond(), nua_i_state
   COMPLETED -(S4)-> READY: nua_i_ack, nua_i_state
  */
  TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_invite);
  TEST(e->data->e_status, 100);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_received); /* RECEIVED */
  TEST_1(is_offer_recv(e->data->e_tags));
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_early); /* EARLY */
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_completed); /* COMPLETED */
  TEST_1(is_answer_sent(e->data->e_tags));
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_ack);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_ready); /* READY */
  TEST_1(!e->next);
  free_events_in_list(ctx, b->events);

  if (print_headings)
226
    printf("TEST %s.1: PASSED\n", tests);
227 228

  /* ---------------------------------------------------------------------- */
229
  /* REFER (initial NOTIFY is no more sent unless REFER creates a new dialog)
230 231 232
   A                    B
   |<------REFER--------|
   |-------200 OK------>|
233 234
  [|-------NOTIFY------>|]			|
  [|<------200 OK-------|]			|
235 236 237
   */

  if (print_headings)
238 239 240 241 242
    printf("TEST %s.2: B refers A to C\n", tests);

  if (b_refer != b_call)
    TEST_1(b_refer->nh = 
	   nua_handle(b->nua, b_refer, SIPTAG_TO(a->to), TAG_END()));
243 244

  *sip_refer_to_init(r0)->r_url = *c->contact->m_url;
245
  r0->r_url->url_headers = "subject=referred";
246 247
  r0->r_display = "C";

248 249 250 251
  REFER(b, b_refer, b_refer->nh, SIPTAG_REFER_TO(r0),
	TAG_IF(!ctx->proxy_tests && b_refer != b_call,
	       NUTAG_URL(a->contact->m_url)),
	TAG_END());
252
  run_ab_until(ctx, -1, save_until_received,
253 254 255 256 257 258
	       -1, save_until_final_response);

  /*
    Events in A:
    nua_i_refer
  */
259
  TEST_1(e = a_revents->head); TEST_E(e->data->e_event, nua_i_refer);
260
  TEST(e->data->e_status, 202);
261 262 263 264 265
  a_event = NULL;
  TEST(tl_gets(e->data->e_tags,
	       NUTAG_REFER_EVENT_REF(a_event),
	       TAG_END()), 1);
  TEST_1(a_event); TEST_1(a_event = sip_event_dup(tmphome, a_event));
266 267 268 269 270 271 272 273 274 275
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_refer_to);
  TEST_1(refer_to = sip_refer_to_dup(tmphome, sip->sip_refer_to));
  TEST_1(sip->sip_referred_by);
  TEST_1(referred_by = sip_referred_by_dup(tmphome, sip->sip_referred_by));

  /*
     Events in B after nua_refer():
     nua_r_refer
  */
276
  TEST_1(e = b_revents->head); TEST_E(e->data->e_event, nua_r_refer);
277 278
  TEST(e->data->e_status, 100);
  TEST(tl_gets(e->data->e_tags,
279
	       NUTAG_REFER_EVENT_REF(b_event),
280
	       TAG_END()), 1);
281 282
  TEST_1(b_event); TEST_1(b_event->o_id);
  TEST_1(b_event = sip_event_dup(tmphome, b_event));
283 284 285 286 287 288 289 290 291

  notify_e = NULL;

  TEST_1(e = e->next);
  if (e->data->e_event == nua_i_notify) {
    notify_e = e;
    TEST_1(e = e->next);
  }
  TEST_E(e->data->e_event, nua_r_refer);
292 293
  TEST(e->data->e_status, 202);
  TEST_1(sip = sip_object(e->data->e_msg));
294
  TEST_SIZE(strtoul(b_event->o_id, NULL, 10), sip->sip_cseq->cs_seq);
295 296

  if (a_refer != a_call) {
297 298 299 300 301 302 303 304 305 306 307 308
    while (!notify_e) {
      for (e = b_revents->head; e; e = e->next) {
	if (e->data->e_event == nua_i_notify) {
	  notify_e = e;
	  break;
	}
      }
      if (!notify_e)
	run_ab_until(ctx, -1, save_until_received, nua_i_notify, save_events);
    }

    if (a_revents->head->next == NULL)
309 310 311 312 313
      run_a_until(ctx, -1, save_until_received);

    TEST_1(e = a_revents->head->next); TEST_E(e->data->e_event, nua_r_notify);
    TEST_1(!e->next);

314
    TEST_1(e = notify_e);
315 316 317 318 319 320 321 322 323
    TEST_E(e->data->e_event, nua_i_notify);
    TEST(e->data->e_status, 200);
    TEST_1(sip = sip_object(e->data->e_msg));
    TEST_1(sip->sip_event);
    if (refer_with_id)
      TEST_S(sip->sip_event->o_id, b_event->o_id);
    TEST_1(sip->sip_subscription_state);
    TEST_S(sip->sip_subscription_state->ss_substate, "pending");
    TEST_1(sip->sip_payload && sip->sip_payload->pl_data);
324 325
    TEST_M(sip->sip_payload->pl_data, "SIP/2.0 100 Trying\r\n",
	   sip->sip_payload->pl_len);
326 327 328 329
  }

  free_events_in_list(ctx, a_revents);
  free_events_in_list(ctx, b_revents);
330 331

  if (print_headings)
332
    printf("TEST %s.2: PASSED\n", tests);
333

334
#if 0
335 336 337 338 339 340 341 342 343 344
  /* ---------------------------------------------------------------------- */
  /*
   A                    B
   |<-----SUBSCRIBE-----|
   |-------200 OK------>|
   |-------NOTIFY------>|			|
   |<------200 OK-------|			|
   */

  if (print_headings)
345
    printf("TEST %s.3: extend expiration time for implied subscription\n", tests);
346 347

  SUBSCRIBE(b, b_call, b_call->nh,
348
	    SIPTAG_EVENT(b_event),
349 350 351 352 353 354 355 356 357
	    SIPTAG_EXPIRES_STR("3600"),
	    TAG_END());
  run_ab_until(ctx, -1, save_until_final_response,
	       -1, save_until_final_response);

  /*
    Events in A:
    nua_i_subscribe, nua_r_notify
  */
358 359 360 361
  TEST_1(e = a->events->head); 
  if (e->data->e_event == nua_r_notify)
    TEST_1(e = e->next);
  TEST_E(e->data->e_event, nua_i_subscribe);
362 363 364 365 366 367 368 369 370 371 372
  TEST(e->data->e_status, 202);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_event);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);

  /*
     Events in B after nua_subscribe():
     nua_r_subscribe, nua_i_notify
  */
373 374 375 376 377 378 379 380 381 382
  TEST_1(e = b->events->head); 
  if (e->data->e_event == nua_i_notify) {
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_event);
  if (refer_with_id)
    TEST_S(sip->sip_event->o_id, b_event->o_id);
  TEST_1(sip->sip_subscription_state);
  TEST_S(sip->sip_subscription_state->ss_substate, "pending");
  TEST_1(sip->sip_payload && sip->sip_payload->pl_data);
383 384
  TEST_M(sip->sip_payload->pl_data, "SIP/2.0 100 Trying\r\n",
	 sip->sip_payload->pl_len);
385 386 387
  TEST_1(e = e->next);
  }
  TEST_E(e->data->e_event, nua_r_subscribe);
388 389 390 391 392 393 394
  TEST(e->data->e_status, 202);
  if (!e->next)
    run_b_until(ctx, -1, save_until_received);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_notify);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_event);
395 396
  if (refer_with_id)
    TEST_S(sip->sip_event->o_id, b_event->o_id);
397 398 399 400 401 402
  TEST_1(sip->sip_subscription_state);
  TEST_S(sip->sip_subscription_state->ss_substate, "pending");
  TEST_1(!e->next);
  free_events_in_list(ctx, b->events);

  if (print_headings)
403
    printf("TEST %s.3: PASSED\n", tests);
404
#endif
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

  /* ---------------------------------------------------------------------- */
  /*
   A                    B                       C
   |			|			|
   |-----------------INVITE-------------------->|
   |			|			|
  XXX			|			|
   | 			|			|
   |<------------------180----------------------|
   |-------NOTIFY------>|			|
   |<------200 OK-------|			|
   | 			|			|
  XXX			|			|
   |			|			|
   |<------------------200----------------------|
   |-------NOTIFY------>|			|
   |<------200 OK-------|			|
   |-------------------ACK--------------------->|
   */

  if (print_headings)
427
    printf("TEST %s.4: A invites C\n", tests);
428 429 430 431

  *sip_to_init(to)->a_url = *refer_to->r_url;
  to->a_display = refer_to->r_display;

432
  a_refer->next = a_c2;
433 434 435 436

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

  INVITE(a, a_c2, a_c2->nh, /* NUTAG_URL(refer_to->r_url), */
437 438
	 TAG_IF(!notify_by_appl, NUTAG_REFER_EVENT(a_event)),
	 TAG_IF(!notify_by_appl, NUTAG_NOTIFY_REFER(a_refer->nh)),
439 440 441 442 443
	 SOATAG_USER_SDP_STR(a_c2->sdp),
	 SIPTAG_REFERRED_BY(referred_by),
	 TAG_END());

  run_abc_until(ctx,
444
		-1, notify_by_appl ? notify_until_terminated : until_ready,
445
		-1, save_until_received,
446 447 448 449 450 451 452
		-1, accept_call);

  /* Wait until both NOTIFY has been responded */
  while (a_revents->head == NULL || a_revents->head->next == NULL)
    run_ab_until(ctx, -1, save_until_received, -1, save_events);
  while (b_revents->head == NULL || b_revents->head->next == NULL)
    run_ab_until(ctx, -1, save_events, -1, save_until_received);
453 454 455 456

  /* Client A transitions:
     INIT -(C1)-> CALLING: nua_invite(), nua_i_state
     CALLING -(C2+C4)-> PROCEEDING: nua_r_invite, nua_i_state
457
     nua_r_notify
458
     PROCEEDING -(C3+C4)-> READY: nua_r_invite, nua_i_state
459
     nua_r_notify
460 461 462 463 464
  */
  TEST_1(e = a_c2->events->head); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_calling); /* CALLING */
  TEST_1(is_offer_sent(e->data->e_tags));
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_invite);
465 466 467 468
  TEST(e->data->e_status, 180);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_proceeding); /* PROCEEDING */
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_invite);
469 470 471 472 473 474 475
  TEST(e->data->e_status, 200);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_ready); /* READY */
  TEST_1(is_answer_recv(e->data->e_tags));
  TEST_1(!e->next);
  free_events_in_list(ctx, a_c2->events);

476 477 478 479 480 481 482 483 484 485
  TEST_1(e = a_revents->head); TEST_E(e->data->e_event, nua_r_notify);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
  if (a_refer == a_call && notify_by_appl) {
    free_event_in_list(ctx, a_revents, a_revents->head);
    free_event_in_list(ctx, a_revents, a_revents->head);
  }
  else {
    TEST_1(!e->next);
    free_events_in_list(ctx, a_revents);
  }
486 487 488 489 490

  /*
     Events in B after nua_refer():
     nua_i_notify
  */
491 492 493 494 495 496
  TEST_1(e = b_revents->head); TEST_E(e->data->e_event, nua_i_notify);
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_subscription_state);
  TEST_S(sip->sip_subscription_state->ss_substate, "active");
  TEST_1(sip->sip_payload && sip->sip_payload->pl_data);
497
  TEST_M(sip->sip_payload->pl_data, "SIP/2.0 180 Ringing\r\n", sip->sip_payload->pl_len);
498 499 500 501
  TEST_1(sip->sip_event);
  if (refer_with_id)
    TEST_S(sip->sip_event->o_id, b_event->o_id);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_notify);
502 503 504 505 506
  TEST(e->data->e_status, 200);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_subscription_state);
  TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
  TEST_1(sip->sip_payload && sip->sip_payload->pl_data);
507
  TEST_M(sip->sip_payload->pl_data, "SIP/2.0 200 OK\r\n", sip->sip_payload->pl_len);
508
  TEST_1(sip->sip_event);
509 510
  if (refer_with_id)
    TEST_S(sip->sip_event->o_id, b_event->o_id);
511 512 513 514 515 516 517 518
  if (b_refer == b_call && notify_by_appl) {
    free_event_in_list(ctx, b_revents, b_revents->head);
    free_event_in_list(ctx, b_revents, b_revents->head);
  }
  else {
    TEST_1(!e->next);
    free_events_in_list(ctx, b_revents);
  }
519 520 521 522

  /*
   C transitions:
   INIT -(S1)-> RECEIVED: nua_i_invite, nua_i_state
523 524
   RECEIVED -(S2a)-> EARLY: nua_respond(), nua_i_state
   EARLY -(S3b)-> COMPLETED: nua_respond(), nua_i_state
525 526 527 528 529 530 531 532
   COMPLETED -(S4)-> READY: nua_i_ack, nua_i_state
  */
  TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_i_invite);
  TEST(e->data->e_status, 100);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_received); /* RECEIVED */
  TEST_1(is_offer_recv(e->data->e_tags));
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
533 534
  TEST(callstate(e->data->e_tags), nua_callstate_early); /* EARLY */
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
535 536 537 538 539 540 541 542 543
  TEST(callstate(e->data->e_tags), nua_callstate_completed); /* COMPLETED */
  TEST_1(is_answer_sent(e->data->e_tags));
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_ack);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_ready); /* READY */
  TEST_1(!e->next);
  free_events_in_list(ctx, c->events);

  if (print_headings)
544
    printf("TEST %s.4: PASSED\n", tests);
545 546 547 548 549 550 551 552 553

  /* ---------------------------------------------------------------------- */
  /*
 A                    B
 |---------BYE------->|
 |<--------200--------|
   */

  if (print_headings)
554
    printf("TEST %s.5.1: terminate call between A and B\n", tests);
555

556 557 558 559 560 561 562 563 564 565
  if (notify_by_appl) {
    if (!a->events->head || !a->events->head->next)
      run_ab_until(ctx, -1, until_terminated, -1, save_events);
    if (!b->events->head || !b->events->head->next)
      run_ab_until(ctx, -1, save_events, -1, until_terminated);
  }
  else {
    BYE(a, a_call, a_call->nh, TAG_END());
    run_ab_until(ctx, -1, until_terminated, -1, until_terminated);
  }
566 567

  /*
568 569 570
    Transitions of A:
    READY --(T2)--> TERMINATING: nua_bye()
    TERMINATING --(T3)--> TERMINATED: nua_r_bye, nua_i_state
571 572 573 574 575 576
  */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_bye);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_terminated); /* TERMINATED */
  TEST_1(!e->next);
  free_events_in_list(ctx, a->events);
577
  
578 579 580 581 582 583 584 585 586 587 588
  /* Transitions of B:
     READY -(T1)-> TERMINATED: nua_i_bye, nua_i_state
  */
  TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_bye);
  TEST(e->data->e_status, 200);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_terminated); /* TERMINATED */
  TEST_1(!e->next);
  free_events_in_list(ctx, b->events);

  if (print_headings)
589
    printf("TEST %s.5.1: PASSED\n", tests);
590 591 592 593 594 595 596 597 598 599 600 601 602

  nua_handle_destroy(a_call->nh), a_call->nh = NULL;
  nua_handle_destroy(b_call->nh), b_call->nh = NULL;


  /* ---------------------------------------------------------------------- */
  /*
   A                                            C
   |-------------------BYE--------------------->|
   |<------------------200----------------------|
   */

  if (print_headings)
603
    printf("TEST %s.5.2: terminate call between A and C\n", tests);
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

  BYE(a, a_c2, a_c2->nh, TAG_END());
  run_abc_until(ctx, -1, until_terminated, -1, NULL, -1, until_terminated);

  /*
   Transitions of A:
   READY --(T2)--> TERMINATING: nua_bye()
   TERMINATING --(T3)--> TERMINATED: nua_r_bye, nua_i_state
  */
  TEST_1(e = a_c2->events->head); TEST_E(e->data->e_event, nua_r_bye);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_terminated); /* TERMINATED */
  TEST_1(!e->next);
  free_events_in_list(ctx, a_c2->events);

  /* Transitions of B:
     READY -(T1)-> TERMINATED: nua_i_bye, nua_i_state
  */
  TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_i_bye);
  TEST(e->data->e_status, 200);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
  TEST(callstate(e->data->e_tags), nua_callstate_terminated); /* TERMINATED */
626
  TEST_1(!e->next);
627 628 629
  free_events_in_list(ctx, c->events);

  if (print_headings)
630
    printf("TEST %s.5.2: PASSED\n", tests);
631

632 633
  nua_handle_destroy(c_call->nh), c_call->nh = NULL;

634
  nua_handle_destroy(a_c2->nh), a_c2->nh = NULL;
635
  a_refer->next = NULL; free(a_c2);
636

637 638 639 640 641 642 643 644 645
  if (a_refer != a_call) {
    nua_handle_destroy(a_refer->nh), a_refer->nh = NULL;
    a_call->next = NULL; free(a_refer);
  }

  if (b_refer != b_call) {
    nua_handle_destroy(b_refer->nh), b_refer->nh = NULL;
    b_call->next = NULL; free(b_refer);
  }
646 647

  if (print_headings)
648
    printf("TEST %s: PASSED\n", tests);
649 650 651 652 653 654

  su_home_deinit(tmphome);

  END();
}

655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

/*
 X      INVITE
 |                    |
 |-----------------INVITE-------------------->|
 |                    |                       |
 |                    |                       |
 |<------------------200----------------------|
 |-------NOTIFY------>|			      |
 |--------BYE-------->|			      |
 |-------------------ACK--------------------->|

*/
int notify_until_terminated(CONDITION_PARAMS)
{
  if (!(check_handle(ep, call, nh, SIP_500_INTERNAL_SERVER_ERROR)))
    return 0;

  save_event_in_list(ctx, event, ep, call);

  if (event == nua_r_invite) {
    sip_status_t *st = sip->sip_status;
    sip_payload_t *pl;
678 679 680 681 682 683 684 685
    struct call *r_call;

    if (!nua_handle_has_events(ep->call->nh))
      r_call = ep->call->next;
    else
      r_call = ep->call;

    assert(nua_handle_has_events(r_call->nh));
686 687 688 689

    pl = sip_payload_format(NULL, "SIP/2.0 %u %s\r\n", 
			    st->st_status, st->st_phrase);

690
    NOTIFY(ep, r_call, r_call->nh,
691 692
	   SIPTAG_CONTENT_TYPE_STR("message/sipfrag"),
	   SIPTAG_PAYLOAD(pl),
693 694 695
	   NUTAG_SUBSTATE(st->st_status >= 200
			  ? nua_substate_terminated
			  : nua_substate_active),
696 697
	   TAG_END());

698 699 700 701 702
    su_free(NULL, pl);

    if (st->st_status >= 200)
      BYE(ep, ep->call, ep->call->nh, TAG_END());

703 704 705 706 707 708 709 710 711 712 713 714 715
    return 0;
  }

  if (call != ep->call)
    return 0;

  switch (callstate(tags)) {
  case nua_callstate_terminated:
    return 1;
  default:
    return 0;
  }
}