test_refer.c 25.7 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
int test_refer0(struct context *ctx, char const *tests,
		int refer_with_id, int notify_by_appl);
int notify_until_terminated(CONDITION_PARAMS);
52
int test_challenge_refer(struct context *ctx);
53 54 55 56

int test_refer(struct context *ctx)
{
  /* test twice, once without id and once with id */
57
  return
58
    test_challenge_refer(ctx) ||
59 60 61 62
    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);
63 64
}

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

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

*/

108 109
int test_refer0(struct context *ctx, char const *tests,
		int refer_with_id, int notify_by_appl)
110 111 112 113 114
{
  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;
115 116
  struct call *a_refer, *a_c2, *b_refer;
  struct eventlist *a_revents, *b_revents;
117
  struct event *e, *notify_e;
118
  sip_t const *sip;
119
  sip_event_t const *a_event, *b_event;
120 121 122 123 124 125 126 127 128 129 130
  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)
131 132 133 134
    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" : "");
135 136 137 138 139 140 141

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

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  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;
  }

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
  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)
227
    printf("TEST %s.1: PASSED\n", tests);
228 229

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

  if (print_headings)
239 240 241 242 243
    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()));
244 245

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

249 250 251 252
  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());
253
  run_ab_until(ctx, -1, save_until_received,
254 255 256 257 258 259
	       -1, save_until_final_response);

  /*
    Events in A:
    nua_i_refer
  */
260
  TEST_1(e = a_revents->head); TEST_E(e->data->e_event, nua_i_refer);
261
  TEST(e->data->e_status, 202);
262 263 264 265 266
  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));
267 268 269 270 271 272 273 274 275 276
  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
  */
277
  TEST_1(e = b_revents->head); TEST_E(e->data->e_event, nua_r_refer);
278 279
  TEST(e->data->e_status, 100);
  TEST(tl_gets(e->data->e_tags,
280
	       NUTAG_REFER_EVENT_REF(b_event),
281
	       TAG_END()), 1);
282 283
  TEST_1(b_event); TEST_1(b_event->o_id);
  TEST_1(b_event = sip_event_dup(tmphome, b_event));
284 285 286 287 288 289 290 291 292

  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);
293 294
  TEST(e->data->e_status, 202);
  TEST_1(sip = sip_object(e->data->e_msg));
295
  TEST_SIZE(strtoul(b_event->o_id, NULL, 10), sip->sip_cseq->cs_seq);
296 297

  if (a_refer != a_call) {
298 299 300 301 302 303 304 305 306 307 308 309
    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)
310 311 312 313 314
      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);

315
    TEST_1(e = notify_e);
316 317 318 319 320 321 322 323 324
    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);
325 326
    TEST_M(sip->sip_payload->pl_data, "SIP/2.0 100 Trying\r\n",
	   sip->sip_payload->pl_len);
327 328 329 330
  }

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

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

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

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

  SUBSCRIBE(b, b_call, b_call->nh,
349
	    SIPTAG_EVENT(b_event),
350 351 352 353 354 355 356 357 358
	    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
  */
359 360 361 362
  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);
363 364 365 366 367 368 369 370 371 372 373
  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
  */
374 375 376 377 378 379 380 381 382 383
  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);
384 385
  TEST_M(sip->sip_payload->pl_data, "SIP/2.0 100 Trying\r\n",
	 sip->sip_payload->pl_len);
386 387 388
  TEST_1(e = e->next);
  }
  TEST_E(e->data->e_event, nua_r_subscribe);
389 390 391 392 393 394 395
  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);
396 397
  if (refer_with_id)
    TEST_S(sip->sip_event->o_id, b_event->o_id);
398 399 400 401 402 403
  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)
404
    printf("TEST %s.3: PASSED\n", tests);
405
#endif
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

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

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

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

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

  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), */
438 439
	 TAG_IF(!notify_by_appl, NUTAG_REFER_EVENT(a_event)),
	 TAG_IF(!notify_by_appl, NUTAG_NOTIFY_REFER(a_refer->nh)),
440 441 442 443 444
	 SOATAG_USER_SDP_STR(a_c2->sdp),
	 SIPTAG_REFERRED_BY(referred_by),
	 TAG_END());

  run_abc_until(ctx,
445
		-1, notify_by_appl ? notify_until_terminated : until_ready,
446
		-1, save_until_received,
447 448 449 450 451 452 453
		-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);
454 455 456 457

  /* Client A transitions:
     INIT -(C1)-> CALLING: nua_invite(), nua_i_state
     CALLING -(C2+C4)-> PROCEEDING: nua_r_invite, nua_i_state
458
     nua_r_notify
459
     PROCEEDING -(C3+C4)-> READY: nua_r_invite, nua_i_state
460
     nua_r_notify
461 462 463 464 465
  */
  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);
466 467 468 469
  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);
470 471 472 473 474 475 476
  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);

477 478 479 480 481 482 483 484 485 486
  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);
  }
487 488 489 490 491

  /*
     Events in B after nua_refer():
     nua_i_notify
  */
492 493 494 495 496 497
  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);
498
  TEST_M(sip->sip_payload->pl_data, "SIP/2.0 180 Ringing\r\n", sip->sip_payload->pl_len);
499 500 501 502
  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);
503 504 505 506 507
  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);
508
  TEST_M(sip->sip_payload->pl_data, "SIP/2.0 200 OK\r\n", sip->sip_payload->pl_len);
509
  TEST_1(sip->sip_event);
510 511
  if (refer_with_id)
    TEST_S(sip->sip_event->o_id, b_event->o_id);
512 513 514 515 516 517 518 519
  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);
  }
520 521 522 523

  /*
   C transitions:
   INIT -(S1)-> RECEIVED: nua_i_invite, nua_i_state
524 525
   RECEIVED -(S2a)-> EARLY: nua_respond(), nua_i_state
   EARLY -(S3b)-> COMPLETED: nua_respond(), nua_i_state
526 527 528 529 530 531 532 533
   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);
534 535
  TEST(callstate(e->data->e_tags), nua_callstate_early); /* EARLY */
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_state);
536 537 538 539 540 541 542 543 544
  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)
545
    printf("TEST %s.4: PASSED\n", tests);
546 547 548 549 550 551 552 553 554

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

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

557 558 559 560 561 562 563 564 565 566
  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);
  }
567 568

  /*
569 570 571
    Transitions of A:
    READY --(T2)--> TERMINATING: nua_bye()
    TERMINATING --(T3)--> TERMINATED: nua_r_bye, nua_i_state
572 573 574 575 576 577
  */
  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);
578
  
579 580 581 582 583 584 585 586 587 588 589
  /* 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)
590
    printf("TEST %s.5.1: PASSED\n", tests);
591 592 593 594 595 596 597 598 599 600 601 602 603

  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)
604
    printf("TEST %s.5.2: terminate call between A and C\n", tests);
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

  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 */
627
  TEST_1(!e->next);
628 629 630
  free_events_in_list(ctx, c->events);

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

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

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

638 639 640 641 642 643 644 645 646
  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);
  }
647 648

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

  su_home_deinit(tmphome);

  END();
}

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

/*
 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;
679 680 681 682 683 684 685 686
    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));
687 688 689 690

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

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

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

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

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

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

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

int authenticate_refer(CONDITION_PARAMS);
719
int reject_refer_after_notified(CONDITION_PARAMS);
720 721 722 723 724 725 726

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

  struct endpoint *a = &ctx->a, *b = &ctx->b, *c = &ctx->c;
  struct call *a_call = a->call, *c_call = c->call;
727
  struct event *e;
728 729 730 731
  sip_t const *sip;

  sip_refer_to_t r0[1];

732 733 734
  if (!ctx->proxy_tests)
    return 0;

735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
  if (print_headings)
    printf("TEST NUA-9.0.1: challenge REFER\n");

  nua_set_params(ctx->a.nua, NUTAG_APPL_METHOD("REFER"), TAG_END());
  run_a_until(ctx, nua_r_set_params, until_final_response);

  *sip_refer_to_init(r0)->r_url = *b->contact->m_url;
  r0->r_url->url_headers = "subject=referred";
  r0->r_display = "B";

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

  REFER(c, c_call, c_call->nh,
	SIPTAG_FROM(c->to),
	SIPTAG_REFER_TO(r0),
	TAG_END());

752
  run_abc_until(ctx, -1, reject_refer_after_notified, -1, NULL, -1, authenticate_refer);
753 754 755 756 757 758 759 760 761

  /*
    Events in A:
    nua_i_refer
  */
  TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_i_refer);
  TEST(e->data->e_status, 100);
  TEST_1(sip = sip_object(e->data->e_msg));
  TEST_1(sip->sip_refer_to);
762 763 764
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
  TEST(e->data->e_status, 200);
  TEST_1(!e->next); 
765 766 767 768 769 770 771 772 773 774 775
  /*
     Events in C after nua_refer():
     nua_r_refer
  */
  TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_r_refer);
  TEST(e->data->e_status, 100);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_refer);
  TEST(e->data->e_status, 407);
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_refer);
  TEST(e->data->e_status, 100);

776 777
  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_notify);
  TEST(e->data->e_status, 200);
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

  TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_refer);
  TEST(e->data->e_status, 480);

  TEST_1(!e->next);
  
  free_events_in_list(ctx, a->events);
  free_events_in_list(ctx, c->events);

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

  nua_set_params(ctx->a.nua,
		 NUTAG_APPL_METHOD(NULL),
		 NUTAG_APPL_METHOD("INVITE, REGISTER, PUBLISH, SUBSCRIBE"),
		 TAG_END());
  run_a_until(ctx, nua_r_set_params, until_final_response);

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

  END();
}

802
int reject_refer_after_notified(CONDITION_PARAMS)
803 804 805 806 807 808 809 810 811 812
{
  if (!(check_handle(ep, call, nh, SIP_500_INTERNAL_SERVER_ERROR)))
    return 0;

  save_event_in_list(ctx, event, ep, call);

  if (event == nua_i_refer) {
  }

  if (event == nua_r_notify) {
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
    /* Respond to refer only after initial notify has been responded */
    struct eventlist *list;
    struct event *e;

    if (call->events)
      list = call->events;
    else
      list = ep->events;

    for (e = list->head; e; e = e->next)
      if (e->data->e_event == nua_i_refer)
	break;

    if (e) {
      RESPOND(ep, call, nh, SIP_480_TEMPORARILY_UNAVAILABLE,
	      NUTAG_WITH(e->data->e_msg),
	      TAG_END());
      return 1;
    }
    return 0;
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
  }

  return 0;
}

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

  save_event_in_list(ctx, event, ep, call);

  if (status == 401 || status == 407) {
    AUTHENTICATE(ep, call, nh,
		 NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"),
		 TAG_END());
  }

  return status == 480;
}