test_nua.c 54 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
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
/*
 * 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.c
 * @brief High-level tester for Sofia SIP User Agent Engine
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Aug 17 12:12:12 EEST 2005 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include <assert.h>

#if HAVE_ALARM
#include <unistd.h>
#include <signal.h>
#endif

struct context;
#define NUA_MAGIC_T struct context
Pekka Pessi's avatar
Pekka Pessi committed
48
#define NUA_HMAGIC_T void
Pekka Pessi's avatar
Pekka Pessi committed
49 50 51

#include "nua.h"
#include "nua_tag.h"
52
#include "sip_status.h"
Pekka Pessi's avatar
Pekka Pessi committed
53 54

#include <sdp.h>
55
#include <sip_header.h>
Pekka Pessi's avatar
Pekka Pessi committed
56 57

#include <su_log.h>
Pekka Pessi's avatar
Pekka Pessi committed
58
#include <su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
59
#include <su_tag_io.h>
Pekka Pessi's avatar
Pekka Pessi committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

extern su_log_t nua_log[];
extern su_log_t su_log_default[];

char const name[] = "test_nua";
int tstflags = 0;
#define TSTFLAGS tstflags

#include <tstdef.h>

#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
#define __func__ name
#endif

#define NONE ((void*)-1)

79 80 81
struct endpoint;

typedef
Pekka Pessi's avatar
Pekka Pessi committed
82 83 84
int condition_function(nua_event_t event,
		       int status, char const *phrase,
		       nua_t *nua, struct context *ctx,
85
		       struct endpoint *ep,
Pekka Pessi's avatar
Pekka Pessi committed
86
		       nua_handle_t *nh, void *call,
Pekka Pessi's avatar
Pekka Pessi committed
87 88 89
		       sip_t const *sip,
		       tagi_t tags[]);

90
struct context
Pekka Pessi's avatar
Pekka Pessi committed
91 92 93 94 95 96
{
  su_home_t home[1];
  su_root_t *root;

  int running;

97
  struct endpoint {
Pekka Pessi's avatar
Pekka Pessi committed
98
    char name[4];
Pekka Pessi's avatar
Pekka Pessi committed
99
    struct context *ctx;	/* Backpointer */
100 101
    condition_function *next_condition;
    nua_event_t next_event, last_event;
Pekka Pessi's avatar
Pekka Pessi committed
102
    nua_t *nua;
103 104
    sip_contact_t *contact;
    sip_from_t *address;
Pekka Pessi's avatar
Pekka Pessi committed
105

Pekka Pessi's avatar
Pekka Pessi committed
106 107
    condition_function *printer;

Pekka Pessi's avatar
Pekka Pessi committed
108
    /* Per-call stuff */
109
    nua_handle_t *nh;
Pekka Pessi's avatar
Pekka Pessi committed
110
    char const *subject;
111
    nua_saved_event_t saved_event[1];
112

113 114 115
    struct {
      struct event *head, **tail;
    } events;
Pekka Pessi's avatar
Pekka Pessi committed
116 117 118
  } a, b;
};

119
struct event
120 121 122
{
  struct event *next, **prev;
  nua_saved_event_t saved_event[1];
Pekka Pessi's avatar
Pekka Pessi committed
123
  nua_event_data_t const *data;
124 125 126 127 128 129 130
};

#define CONDITION_FUNCTION(name)		\
  int name(nua_event_t event,			\
	   int status, char const *phrase,	\
	   nua_t *nua, struct context *ctx,	\
	   struct endpoint *ep,			\
Pekka Pessi's avatar
Pekka Pessi committed
131
	   nua_handle_t *nh, void *call, \
132 133 134 135 136 137 138 139 140 141 142 143 144
	   sip_t const *sip,			\
	   tagi_t tags[])

CONDITION_FUNCTION(condition_final_response){ return status >= 200; }
CONDITION_FUNCTION(never){ return 0; }
CONDITION_FUNCTION(save_final_response)
{
  if (status < 200)
    return 0;
  nua_save_event(ep->nua, ep->saved_event);
  return 1;
}

Pekka Pessi's avatar
Pekka Pessi committed
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
/* Return call state from event tag list */
int callstate(tagi_t const *tags)
{
  tagi_t const *ti = tl_find(tags, nutag_callstate);
  return ti ? ti->t_value : -1;
}

/* Return true if offer is sent */
int is_offer_sent(tagi_t const *tags)
{
  tagi_t const *ti = tl_find(tags, nutag_offer_sent);
  return ti ? ti->t_value : 0;
}

/* Return true if answer is sent */
int is_answer_sent(tagi_t const *tags)
{
  tagi_t const *ti = tl_find(tags, nutag_answer_sent);
  return ti ? ti->t_value : 0;
}

/* Return true if offer is recv */
int is_offer_recv(tagi_t const *tags)
{
  tagi_t const *ti = tl_find(tags, nutag_offer_recv);
  return ti ? ti->t_value : 0;
}

/* Return true if answer is recv */
int is_answer_recv(tagi_t const *tags)
{
  tagi_t const *ti = tl_find(tags, nutag_answer_recv);
  return ti ? ti->t_value : 0;
}

/* Return audio state from event tag list */
int audio_activity(tagi_t const *tags)
{
  tagi_t const *ti = tl_find(tags, soatag_active_audio);
  return ti ? ti->t_value : -1;
}

/* Return video state from event tag list */
int video_activity(tagi_t const *tags)
{
  tagi_t const *ti = tl_find(tags, soatag_active_video);
  return ti ? ti->t_value : -1;
}


CONDITION_FUNCTION(print_event)
{
  if (event == nua_i_state) {
    fprintf(stderr, "%s.nua(%p): event %s %s\n",
	    ep->name, nh, nua_event_name(event), 
	    nua_callstate_name(callstate(tags)));
  }
  else if ((int)event >= 0) {
    fprintf(stderr, "%s.nua(%p): event %s status %u %s\n",
	    ep->name, nh, nua_event_name(event), status, phrase);
  }
  else if (status > 0) {
    fprintf(stderr, "%s.nua(%p): call %s() with status %u %s\n",
	    ep->name, nh, (char const *)call, status, phrase);
  }
  else {
Pekka Pessi's avatar
Pekka Pessi committed
211 212 213 214 215 216 217 218 219 220
    tagi_t const *t;
    t = tl_find(tags, siptag_subject_str);
    if (t && t->t_value) {
      char const *subject = (char const *)t->t_value;
      fprintf(stderr, "%s.nua(%p): call %s() \"%s\"\n",
	      ep->name, nh, (char const *)call, subject);
    }
    else
      fprintf(stderr, "%s.nua(%p): call %s()\n",
	      ep->name, nh, (char const *)call);
Pekka Pessi's avatar
Pekka Pessi committed
221 222 223 224 225 226 227 228 229
  }

  if ((tstflags & tst_verbatim) && tags)
    tl_print(stderr, "", tags);

  return 0;
}
	       

230 231 232
void a_callback(nua_event_t event,
		int status, char const *phrase,
		nua_t *nua, struct context *ctx,
Pekka Pessi's avatar
Pekka Pessi committed
233
		nua_handle_t *nh, void *call,
234 235 236
		sip_t const *sip,
		tagi_t tags[])
{
Pekka Pessi's avatar
Pekka Pessi committed
237
  struct endpoint *ep = &ctx->a;
238

Pekka Pessi's avatar
Pekka Pessi committed
239 240
  if (ep->printer)
    ep->printer(event, status, phrase, nua, ctx, ep, nh, call, sip, tags);
Pekka Pessi's avatar
Pekka Pessi committed
241 242 243 244 245

  if ((ep->next_event == -1 || ep->next_event == event) &&
      (ep->next_condition == NULL ||
       ep->next_condition(event, status, phrase,
			  nua, ctx, ep, nh, call, sip, tags)))
246 247
    ctx->running = 0;

Pekka Pessi's avatar
Pekka Pessi committed
248
  ep->last_event = event;
249 250 251 252 253 254
  ctx->b.last_event = -1;
}

void b_callback(nua_event_t event,
		int status, char const *phrase,
		nua_t *nua, struct context *ctx,
Pekka Pessi's avatar
Pekka Pessi committed
255
		nua_handle_t *nh, void *call,
256 257
		sip_t const *sip,
		tagi_t tags[])
Pekka Pessi's avatar
Pekka Pessi committed
258
{
Pekka Pessi's avatar
Pekka Pessi committed
259 260
  struct endpoint *ep = &ctx->b;

Pekka Pessi's avatar
Pekka Pessi committed
261 262
  if (ep->printer)
    ep->printer(event, status, phrase, nua, ctx, ep, nh, call, sip, tags);
263

Pekka Pessi's avatar
Pekka Pessi committed
264 265 266 267
  if ((ep->next_event == -1 || ep->next_event == event) &&
      (ep->next_condition == NULL ||
       ep->next_condition(event, status, phrase,
			  nua, ctx, ep, nh, call, sip, tags)))
Pekka Pessi's avatar
Pekka Pessi committed
268 269
    ctx->running = 0;

Pekka Pessi's avatar
Pekka Pessi committed
270 271
  ep->last_event = -1;
  ep->last_event = event;
Pekka Pessi's avatar
Pekka Pessi committed
272 273
}

274 275 276
void run_until(struct context *ctx,
	       nua_event_t a_event, condition_function *a_condition,
	       nua_event_t b_event, condition_function *b_condition)
Pekka Pessi's avatar
Pekka Pessi committed
277
{
278 279 280 281 282 283
  ctx->a.next_event = a_event;
  ctx->a.next_condition = a_condition;
  ctx->a.last_event = -1;
  ctx->b.next_event = b_event;
  ctx->b.next_condition = b_condition;
  ctx->b.last_event = -1;
Pekka Pessi's avatar
Pekka Pessi committed
284 285 286 287

  for (ctx->running = 1; ctx->running;) {
    su_root_step(ctx->root, 1000);
  }
288
}
Pekka Pessi's avatar
Pekka Pessi committed
289

290 291 292 293
int run_a_until(struct context *ctx,
		nua_event_t a_event,
		condition_function *a_condition)
{
294
  run_until(ctx, a_event, a_condition, -1, never);
295 296 297 298 299 300 301
  return ctx->a.last_event;
}

int run_b_until(struct context *ctx,
		nua_event_t b_event,
		condition_function *b_condition)
{
302
  run_until(ctx, -1, never, b_event, b_condition);
303
  return ctx->b.last_event;
Pekka Pessi's avatar
Pekka Pessi committed
304 305
}

Pekka Pessi's avatar
Pekka Pessi committed
306 307 308 309 310 311 312
/* Invite via endpoint and handle */
int invite(struct endpoint *ep, nua_handle_t *nh,
	   tag_type_t tag, tag_value_t value,
	   ...)
{
  ta_list ta;
  ta_start(ta, tag, value);
Pekka Pessi's avatar
Pekka Pessi committed
313

Pekka Pessi's avatar
Pekka Pessi committed
314 315 316
  if (ep->printer)
    ep->printer(-1, 0, "", ep->nua, ep->ctx, ep, 
		nh, "nua_invite", NULL, ta_args(ta));
Pekka Pessi's avatar
Pekka Pessi committed
317

Pekka Pessi's avatar
Pekka Pessi committed
318 319
  nua_invite(nh, ta_tags(ta));

Pekka Pessi's avatar
Pekka Pessi committed
320
  ta_end(ta);
Pekka Pessi's avatar
Pekka Pessi committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  return 0;
}

/* bye via endpoint and handle */
int bye(struct endpoint *ep, nua_handle_t *nh,
	tag_type_t tag, tag_value_t value,
	...)
{
  ta_list ta;

  ta_start(ta, tag, value);
  if (ep->printer)
    ep->printer(-1, 0, "", ep->nua, ep->ctx, ep, 
		nh, "nua_bye", NULL, ta_args(ta));
  nua_bye(nh, ta_tags(ta));
  ta_end(ta);

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
341 342 343 344 345 346 347 348 349
/* Respond via endpoint and handle */
int respond(struct endpoint *ep, nua_handle_t *nh,
	    int status, char const *phrase,
	    tag_type_t tag, tag_value_t value,
	    ...)
{
  ta_list ta;

  ta_start(ta, tag, value);
Pekka Pessi's avatar
Pekka Pessi committed
350 351 352 353 354

  if (ep->printer)
    ep->printer(-1, status, phrase, ep->nua, ep->ctx, ep, 
		nh, "nua_respond", NULL, ta_args(ta));

Pekka Pessi's avatar
Pekka Pessi committed
355 356 357 358 359 360 361
  nua_respond(nh, status, phrase, ta_tags(ta));
  ta_end(ta);

  return 0;
}


Pekka Pessi's avatar
Pekka Pessi committed
362

Pekka Pessi's avatar
Pekka Pessi committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
/* Reject all but currently used handle */
int check_handle(struct endpoint *ep, nua_handle_t *nh, 
		 int status, char const *phrase)
{
  if (ep->nh && ep->nh != nh) {
    if (status) {
      respond(ep, nh, status, phrase, TAG_END());
    }
    nua_handle_destroy(nh);
    return 0;
  }
  ep->nh = nh;
  return 1;
}

378
/* Save nua event in endpoint list */
379 380
int save_event_in_list(struct context *ctx,
		       struct endpoint *ep)
381
{
382 383 384
  struct event *e = su_zalloc(ctx->home, sizeof *e);

  if (!e) { perror("su_zalloc"), abort(); }
385

386 387 388
  *(e->prev = ep->events.tail) = e;
  ep->events.tail = &e->next;

Pekka Pessi's avatar
Pekka Pessi committed
389
  if (!nua_save_event(ep->nua, e->saved_event))
Pekka Pessi's avatar
Pekka Pessi committed
390
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
391 392

  e->data = nua_event_data(e->saved_event);
393

Pekka Pessi's avatar
Pekka Pessi committed
394
  return 0;
395
}
Pekka Pessi's avatar
Pekka Pessi committed
396

Pekka Pessi's avatar
Pekka Pessi committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410
/* Save nua event in endpoint list */
void free_events_in_list(struct context *ctx, struct endpoint *ep)
{
  struct event *e;

  while ((e = ep->events.head)) {
    if ((*e->prev = e->next))
      e->next->prev = e->prev;
    nua_destroy_event(e->saved_event);
    su_free(ctx->home, e);
  }
  ep->events.tail = &ep->events.head;
}

Pekka Pessi's avatar
Pekka Pessi committed
411 412 413 414 415 416
void nolog(void *stream, char const *fmt, va_list ap) {}

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

Pekka Pessi's avatar
Pekka Pessi committed
417
  /* Invoke every API function with invalid arguments */
Pekka Pessi's avatar
Pekka Pessi committed
418

Pekka Pessi's avatar
Pekka Pessi committed
419 420 421 422 423 424 425
  int level;

  su_log_init(nua_log);

  level = nua_log->log_level;
  if (!(tstflags & tst_verbatim))
    su_log_set_level(nua_log, 0);
Pekka Pessi's avatar
Pekka Pessi committed
426 427 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 453 454 455 456 457 458 459 460 461 462 463 464 465 466

  TEST_1(!nua_create(NULL, NULL, NULL, TAG_END()));
  TEST_VOID(nua_shutdown(NULL));
  TEST_VOID(nua_destroy(NULL));
  TEST_VOID(nua_set_params(NULL, TAG_END()));
  TEST_VOID(nua_get_params(NULL, TAG_END()));
  TEST_1(!nua_default(NULL));
  TEST_1(!nua_handle(NULL, NULL, TAG_END()));
  TEST_VOID(nua_handle_destroy(NULL));
  TEST_VOID(nua_handle_bind(NULL, NULL));
  TEST_1(!nua_handle_has_invite(NULL));
  TEST_1(!nua_handle_has_subscribe(NULL));
  TEST_1(!nua_handle_has_register(NULL));
  TEST_1(!nua_handle_has_active_call(NULL));
  TEST_1(!nua_handle_has_call_on_hold(NULL));
  TEST_1(!nua_handle_has_events(NULL));
  TEST_1(!nua_handle_has_registrations(NULL));
  TEST_1(!nua_handle_remote(NULL));
  TEST_1(!nua_handle_local(NULL));
  TEST_S(nua_event_name(-1), "NUA_UNKNOWN");
  TEST_VOID(nua_register(NULL, TAG_END()));
  TEST_VOID(nua_unregister(NULL, TAG_END()));
  TEST_VOID(nua_invite(NULL, TAG_END()));
  TEST_VOID(nua_ack(NULL, TAG_END()));
  TEST_VOID(nua_options(NULL, TAG_END()));
  TEST_VOID(nua_publish(NULL, TAG_END()));
  TEST_VOID(nua_message(NULL, TAG_END()));
  TEST_VOID(nua_chat(NULL, TAG_END()));
  TEST_VOID(nua_info(NULL, TAG_END()));
  TEST_VOID(nua_subscribe(NULL, TAG_END()));
  TEST_VOID(nua_unsubscribe(NULL, TAG_END()));
  TEST_VOID(nua_notify(NULL, TAG_END()));
  TEST_VOID(nua_notifier(NULL, TAG_END()));
  TEST_VOID(nua_terminate(NULL, TAG_END()));
  TEST_VOID(nua_refer(NULL, TAG_END()));
  TEST_VOID(nua_update(NULL, TAG_END()));
  TEST_VOID(nua_bye(NULL, TAG_END()));
  TEST_VOID(nua_cancel(NULL, TAG_END()));
  TEST_VOID(nua_authenticate(NULL, TAG_END()));
  TEST_VOID(nua_redirect(NULL, TAG_END()));
  TEST_VOID(nua_respond(NULL, 0, "", TAG_END()));
Pekka Pessi's avatar
Pekka Pessi committed
467

Pekka Pessi's avatar
Pekka Pessi committed
468 469
  TEST_1(!nua_handle_home(NULL));
  TEST_1(!nua_save_event(NULL, NULL));
470
  TEST_1(!nua_event_data(NULL));
Pekka Pessi's avatar
Pekka Pessi committed
471
  TEST_VOID(nua_destroy_event(NULL));
472

Pekka Pessi's avatar
Pekka Pessi committed
473 474 475 476
  {
    nua_saved_event_t event[1];

    memset(event, 0, sizeof event);
477

Pekka Pessi's avatar
Pekka Pessi committed
478
    TEST_1(!nua_save_event(NULL, event));
479
    TEST_1(!nua_event_data(event));
Pekka Pessi's avatar
Pekka Pessi committed
480 481 482
    TEST_VOID(nua_destroy_event(event));
  }

Pekka Pessi's avatar
Pekka Pessi committed
483
  su_log_set_level(nua_log, level);
Pekka Pessi's avatar
Pekka Pessi committed
484 485 486 487 488 489 490

  END();
}

int test_params(struct context *ctx)
{
  BEGIN();
491 492 493 494 495 496 497 498

  char const Alice[] = "Alice <sip:a@wonderland.org>";
  sip_from_t const *from;
  su_home_t tmphome[SU_HOME_AUTO_SIZE(16384)];
  nua_handle_t *nh;

  su_home_auto(tmphome, sizeof(tmphome));

499 500
  ctx->root = su_root_create(ctx); TEST_1(ctx->root);

Pekka Pessi's avatar
Pekka Pessi committed
501 502 503
  /* Disable threading by command line switch? */
  su_root_threading(ctx->root, 1);

504 505
  ctx->a.nua = nua_create(ctx->root, a_callback, ctx,
			  SIPTAG_FROM_STR("sip:alice@example.com"),
Pekka Pessi's avatar
Pekka Pessi committed
506
			  NUTAG_URL("sip:*:*;transport=udp"),
507
			  TAG_END());
508

509 510
  TEST_1(ctx->a.nua);

511
  from = sip_from_make(tmphome, Alice);
512 513 514

  nh = nua_handle(ctx->a.nua, NULL, TAG_END());

515
  nua_set_hparams(nh, NUTAG_INVITE_TIMER(90), TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
516
  run_a_until(ctx, nua_r_set_params, condition_final_response);
517

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
  /* Modify all pointer values */
  nua_set_params(ctx->a.nua,
		 SIPTAG_FROM_STR(Alice),

		 SIPTAG_SUPPORTED_STR("test"),
		 SIPTAG_ALLOW_STR("DWIM, OPTIONS, INFO"),
		 SIPTAG_USER_AGENT_STR("test_nua/1.0"),

		 SIPTAG_ORGANIZATION_STR("Te-Ras y.r."),

		 NUTAG_REGISTRAR("sip:openlaboratory.net"),

		 TAG_END());

  run_a_until(ctx, nua_r_set_params, condition_final_response);

534 535 536
  /* Modify everything from their default value */
  nua_set_params(ctx->a.nua,
		 SIPTAG_FROM(from),
537
		 NUTAG_RETRY_COUNT(9),
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
		 NUTAG_MAX_SUBSCRIPTIONS(6),

		 NUTAG_ENABLEINVITE(0),
		 NUTAG_AUTOALERT(1),
		 NUTAG_EARLY_MEDIA(1),
		 NUTAG_AUTOANSWER(1),
		 NUTAG_AUTOACK(0),
		 NUTAG_INVITE_TIMER(60),

		 NUTAG_SESSION_TIMER(600),
		 NUTAG_MIN_SE(35),
		 NUTAG_SESSION_REFRESHER(nua_remote_refresher),
		 NUTAG_UPDATE_REFRESH(1),

		 NUTAG_ENABLEMESSAGE(0),
		 NUTAG_ENABLEMESSENGER(1),
		 /* NUTAG_MESSAGE_AUTOANSWER(0), */

		 NUTAG_CALLEE_CAPS(1),
		 NUTAG_MEDIA_FEATURES(1),
		 NUTAG_SERVICE_ROUTE_ENABLE(0),
		 NUTAG_PATH_ENABLE(0),

561 562 563
		 SIPTAG_SUPPORTED(sip_supported_make(tmphome, "humppaa,kuole")),
		 SIPTAG_ALLOW(sip_allow_make(tmphome, "OPTIONS, INFO")),
		 SIPTAG_USER_AGENT(sip_user_agent_make(tmphome, "test_nua")),
564

565
		 SIPTAG_ORGANIZATION(sip_organization_make(tmphome, "Pussy Galore's Flying Circus")),
566 567

		 NUTAG_MEDIA_ENABLE(0),
568
		 NUTAG_REGISTRAR(url_hdup(tmphome, (url_t *)"sip:sip.wonderland.org")),
569

570 571
		 TAG_END());

Pekka Pessi's avatar
Pekka Pessi committed
572
  run_a_until(ctx, nua_r_set_params, condition_final_response);
573

574 575 576 577 578
  /* Modify something... */
  nua_set_params(ctx->a.nua,
		 NUTAG_RETRY_COUNT(5),
		 TAG_END());
  run_a_until(ctx, nua_r_set_params, condition_final_response);
579 580 581 582 583 584 585

  {
    sip_from_t const *from = NONE;
    char const *from_str = "NONE";

    int retry_count = -1;
    int max_subscriptions = -1;
586

587 588 589 590 591 592
    int invite_enable = -1;
    int auto_alert = -1;
    int early_media = -1;
    int auto_answer = -1;
    int auto_ack = -1;
    int invite_timeout = -1;
593

594 595 596 597
    int session_timer = -1;
    int min_se = -1;
    int refresher = -1;
    int update_refresh = -1;
598

599 600 601
    int message_enable = -1;
    int win_messenger_enable = -1;
    int message_auto_respond = -1;
602

603 604 605 606 607 608
    int callee_caps = -1;
    int media_features = -1;
    int service_route_enable = -1;
    int path_enable = -1;

    sip_allow_t const *allow = NONE;
609
    char const *allow_str = "NONE";
610 611 612 613 614 615 616 617 618 619
    sip_supported_t const *supported = NONE;
    char const *supported_str = "NONE";
    sip_user_agent_t const *user_agent = NONE;
    char const *user_agent_str = "NONE";
    sip_organization_t const *organization = NONE;
    char const *organization_str = "NONE";

    url_string_t const *registrar = NONE;

    int n;
620 621
    nua_event_data_t const *e;

Pekka Pessi's avatar
Pekka Pessi committed
622 623
    nua_get_params(ctx->a.nua, TAG_ANY(), TAG_END());
    run_a_until(ctx, nua_r_get_params, save_final_response);
624
    TEST_1(e = nua_event_data(ctx->a.saved_event));
625

626
    TEST(e->e_event, nua_r_get_params);
627

628
    n = tl_gets(e->e_tags,
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 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 694 695 696 697 698 699 700 701 702 703
	       	SIPTAG_FROM_REF(from),
	       	SIPTAG_FROM_STR_REF(from_str),

	       	NUTAG_RETRY_COUNT_REF(retry_count),
	       	NUTAG_MAX_SUBSCRIPTIONS_REF(max_subscriptions),

	       	NUTAG_ENABLEINVITE_REF(invite_enable),
	       	NUTAG_AUTOALERT_REF(auto_alert),
	       	NUTAG_EARLY_MEDIA_REF(early_media),
	       	NUTAG_AUTOANSWER_REF(auto_answer),
	       	NUTAG_AUTOACK_REF(auto_ack),
	       	NUTAG_INVITE_TIMER_REF(invite_timeout),

	       	NUTAG_SESSION_TIMER_REF(session_timer),
	       	NUTAG_MIN_SE_REF(min_se),
	       	NUTAG_SESSION_REFRESHER_REF(refresher),
	       	NUTAG_UPDATE_REFRESH_REF(update_refresh),

	       	NUTAG_ENABLEMESSAGE_REF(message_enable),
	       	NUTAG_ENABLEMESSENGER_REF(win_messenger_enable),
	       	/* NUTAG_MESSAGE_AUTOANSWER(message_auto_respond), */

	       	NUTAG_CALLEE_CAPS_REF(callee_caps),
	       	NUTAG_MEDIA_FEATURES_REF(media_features),
	       	NUTAG_SERVICE_ROUTE_ENABLE_REF(service_route_enable),
	       	NUTAG_PATH_ENABLE_REF(path_enable),

	       	SIPTAG_SUPPORTED_REF(supported),
	       	SIPTAG_SUPPORTED_STR_REF(supported_str),
	       	SIPTAG_ALLOW_REF(allow),
	       	SIPTAG_ALLOW_STR_REF(allow_str),
	       	SIPTAG_USER_AGENT_REF(user_agent),
	       	SIPTAG_USER_AGENT_STR_REF(user_agent_str),

	       	SIPTAG_ORGANIZATION_REF(organization),
	       	SIPTAG_ORGANIZATION_STR_REF(organization_str),

	       	NUTAG_REGISTRAR_REF(registrar),

		TAG_END());
    TEST(n, 29);

    TEST_S(sip_header_as_string(tmphome, (void *)from), Alice);
    TEST_S(from_str, Alice);

    TEST(retry_count, 5);
    TEST(max_subscriptions, 6);

    TEST(invite_enable, 0);
    TEST(auto_alert, 1);
    TEST(early_media, 1);
    TEST(auto_answer, 1);
    TEST(auto_ack, 0);
    TEST(invite_timeout, 60);

    TEST(session_timer, 600);
    TEST(min_se, 35);
    TEST(refresher, nua_remote_refresher);
    TEST(update_refresh, 1);

    TEST(message_enable, 0);
    TEST(win_messenger_enable, 1);
    TEST(message_auto_respond, -1); /* XXX */

    TEST(callee_caps, 1);
    TEST(media_features, 1);
    TEST(service_route_enable, 0);
    TEST(path_enable, 0);

    TEST_S(sip_header_as_string(tmphome, (void *)allow), "OPTIONS, INFO");
    TEST_S(allow_str, "OPTIONS, INFO");
    TEST_S(sip_header_as_string(tmphome, (void *)supported), "humppaa, kuole");
    TEST_S(supported_str, "humppaa, kuole");
    TEST_S(sip_header_as_string(tmphome, (void *)user_agent), "test_nua");
    TEST_S(user_agent_str, "test_nua");
704
    TEST_S(sip_header_as_string(tmphome, (void *)organization),
705 706 707
	   "Pussy Galore's Flying Circus");
    TEST_S(organization_str, "Pussy Galore's Flying Circus");

708
    TEST_S(url_as_string(tmphome, registrar->us_url),
709
	   "sip:sip.wonderland.org");
710

Pekka Pessi's avatar
Pekka Pessi committed
711
    nua_destroy_event(ctx->a.saved_event);
712
  }
713

714 715 716 717 718 719
  {
    sip_from_t const *from = NONE;
    char const *from_str = "NONE";

    int retry_count = -1;
    int max_subscriptions = -1;
720

721 722 723 724 725 726
    int invite_enable = -1;
    int auto_alert = -1;
    int early_media = -1;
    int auto_answer = -1;
    int auto_ack = -1;
    int invite_timeout = -1;
727

728 729 730 731
    int session_timer = -1;
    int min_se = -1;
    int refresher = -1;
    int update_refresh = -1;
732

733 734 735
    int message_enable = -1;
    int win_messenger_enable = -1;
    int message_auto_respond = -1;
736

737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
    int callee_caps = -1;
    int media_features = -1;
    int service_route_enable = -1;
    int path_enable = -1;

    sip_allow_t const *allow = NONE;
    char const   *allow_str = "NONE";
    sip_supported_t const *supported = NONE;
    char const *supported_str = "NONE";
    sip_user_agent_t const *user_agent = NONE;
    char const *user_agent_str = "NONE";
    sip_organization_t const *organization = NONE;
    char const *organization_str = "NONE";

    url_string_t const *registrar = NONE;

    int n;
754
    nua_event_data_t const *e;
755

756
    nua_get_hparams(nh, TAG_ANY(), TAG_END());
757 758
    run_a_until(ctx, nua_r_get_params, save_final_response);

759
    TEST_1(e = nua_event_data(ctx->a.saved_event));
760

761 762 763
    TEST(e->e_event, nua_r_get_params);

    n = tl_gets(e->e_tags,
764 765 766 767 768 769 770 771 772 773 774 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
	       	SIPTAG_FROM_REF(from),
	       	SIPTAG_FROM_STR_REF(from_str),

	       	NUTAG_RETRY_COUNT_REF(retry_count),
	       	NUTAG_MAX_SUBSCRIPTIONS_REF(max_subscriptions),

	       	NUTAG_ENABLEINVITE_REF(invite_enable),
	       	NUTAG_AUTOALERT_REF(auto_alert),
	       	NUTAG_EARLY_MEDIA_REF(early_media),
	       	NUTAG_AUTOANSWER_REF(auto_answer),
	       	NUTAG_AUTOACK_REF(auto_ack),
	       	NUTAG_INVITE_TIMER_REF(invite_timeout),

	       	NUTAG_SESSION_TIMER_REF(session_timer),
	       	NUTAG_MIN_SE_REF(min_se),
	       	NUTAG_SESSION_REFRESHER_REF(refresher),
	       	NUTAG_UPDATE_REFRESH_REF(update_refresh),

	       	NUTAG_ENABLEMESSAGE_REF(message_enable),
	       	NUTAG_ENABLEMESSENGER_REF(win_messenger_enable),
	       	/* NUTAG_MESSAGE_AUTOANSWER(message_auto_respond), */

	       	NUTAG_CALLEE_CAPS_REF(callee_caps),
	       	NUTAG_MEDIA_FEATURES_REF(media_features),
	       	NUTAG_SERVICE_ROUTE_ENABLE_REF(service_route_enable),
	       	NUTAG_PATH_ENABLE_REF(path_enable),

	       	SIPTAG_SUPPORTED_REF(supported),
	       	SIPTAG_SUPPORTED_STR_REF(supported_str),
	       	SIPTAG_ALLOW_REF(allow),
	       	SIPTAG_ALLOW_STR_REF(allow_str),
	       	SIPTAG_USER_AGENT_REF(user_agent),
	       	SIPTAG_USER_AGENT_STR_REF(user_agent_str),

	       	SIPTAG_ORGANIZATION_REF(organization),
	       	SIPTAG_ORGANIZATION_STR_REF(organization_str),

	       	NUTAG_REGISTRAR_REF(registrar),

		TAG_END());
804
    TEST(n, 3);
805 806 807

    TEST(invite_timeout, 90);

808 809
    TEST_1(from != NULL && from != NONE);
    TEST_1(strcmp(from_str, "NONE"));
810

811
    /* Nothing else should be set */
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 840 841 842 843 844
    TEST(retry_count, -1);
    TEST(max_subscriptions, -1);

    TEST(invite_enable, -1);
    TEST(auto_alert, -1);
    TEST(early_media, -1);
    TEST(auto_answer, -1);
    TEST(auto_ack, -1);

    TEST(session_timer, -1);
    TEST(min_se, -1);
    TEST(refresher, -1);
    TEST(update_refresh, -1);

    TEST(message_enable, -1);
    TEST(win_messenger_enable, -1);
    TEST(message_auto_respond, -1); /* XXX */

    TEST(callee_caps, -1);
    TEST(media_features, -1);
    TEST(service_route_enable, -1);
    TEST(path_enable, -1);

    TEST(allow, NONE);
    TEST_S(allow_str, "NONE");
    TEST(supported, NONE);
    TEST_S(supported_str, "NONE");
    TEST(user_agent, NONE);
    TEST_S(user_agent_str, "NONE");
    TEST(organization, NONE);
    TEST_S(organization_str, "NONE");

    TEST(registrar->us_url, NONE);
Pekka Pessi's avatar
Pekka Pessi committed
845 846

    nua_destroy_event(ctx->a.saved_event);
847 848 849
  }

  nua_handle_destroy(nh);
850

851 852 853 854 855 856 857
  nua_shutdown(ctx->a.nua);
  run_a_until(ctx, nua_r_shutdown, condition_final_response);
  nua_destroy(ctx->a.nua), ctx->a.nua = NULL;

  su_root_destroy(ctx->root), ctx->root = NULL;

  su_home_deinit(tmphome);
858

Pekka Pessi's avatar
Pekka Pessi committed
859 860 861
  END();
}

862
int test_init(struct context *ctx, char *argv[])
Pekka Pessi's avatar
Pekka Pessi committed
863 864
{
  BEGIN();
865 866
  nua_event_data_t const *e;
  sip_contact_t const *m = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
867
  sip_from_t const *a = NULL;
868 869 870 871

  ctx->root = su_root_create(ctx); TEST_1(ctx->root);

  /* Disable threading by command line switch? */
872
  su_root_threading(ctx->root, 1);
873 874 875 876

  ctx->a.nua = nua_create(ctx->root, a_callback, ctx,
			  SIPTAG_FROM_STR("sip:alice@example.com"),
			  NUTAG_URL("sip:*:*"),
Pekka Pessi's avatar
Pekka Pessi committed
877
			  SOATAG_USER_SDP_STR("m=audio 5004 RTP/AVP 0 8"),
878 879 880 881 882 883
			  TAG_END());
  TEST_1(ctx->a.nua);

  nua_get_params(ctx->a.nua, TAG_ANY(), TAG_END());
  run_a_until(ctx, nua_r_get_params, save_final_response);
  TEST_1(e = nua_event_data(ctx->a.saved_event));
Pekka Pessi's avatar
Pekka Pessi committed
884 885 886 887
  TEST(tl_gets(e->e_tags,
	       NTATAG_CONTACT_REF(m),
	       SIPTAG_FROM_REF(a),
	       TAG_END()), 2); TEST_1(m);
888
  TEST_1(ctx->a.contact = sip_contact_dup(ctx->home, m));
Pekka Pessi's avatar
Pekka Pessi committed
889
  TEST_1(ctx->a.address = sip_to_dup(ctx->home, a));
890
  nua_destroy_event(ctx->a.saved_event);
891

892 893 894
  ctx->b.nua = nua_create(ctx->root, b_callback, ctx,
			  SIPTAG_FROM_STR("sip:bob@example.org"),
			  NUTAG_URL("sip:*:*"),
Pekka Pessi's avatar
Pekka Pessi committed
895
			  SOATAG_USER_SDP_STR("m=audio 5006 RTP/AVP 8 0"),
896 897 898 899 900 901
			  TAG_END());
  TEST_1(ctx->b.nua);

  nua_get_params(ctx->b.nua, TAG_ANY(), TAG_END());
  run_b_until(ctx, nua_r_get_params, save_final_response);
  TEST_1(e = nua_event_data(ctx->b.saved_event));
Pekka Pessi's avatar
Pekka Pessi committed
902 903 904 905 906 907 908
  TEST(tl_gets(e->e_tags,
	       NTATAG_CONTACT_REF(m),
	       SIPTAG_FROM_REF(a),
	       TAG_END()), 2); TEST_1(m);
  TEST_1(ctx->b.contact = sip_contact_dup(ctx->home, m));
  TEST_1(ctx->b.address = sip_to_dup(ctx->home, a));
  nua_destroy_event(ctx->b.saved_event);
909

Pekka Pessi's avatar
Pekka Pessi committed
910 911 912
  END();
}

Pekka Pessi's avatar
Pekka Pessi committed
913 914
CONDITION_FUNCTION(save_events)
{
915 916
  if (event != nua_i_active && event != nua_i_terminated)
    save_event_in_list(ctx, ep);
Pekka Pessi's avatar
Pekka Pessi committed
917 918 919
  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
920 921
/* ======================================================================== */

922
/* Basic call:
923

924 925 926 927 928 929 930 931 932 933 934 935
   A			B
   |-------INVITE------>|
   |<----100 Trying-----|
   |			|
   |<----180 Ringing----|
   |			|
   |<------200 OK-------|
   |--------ACK-------->|
   |			|
   |<-------BYE---------|
   |-------200 OK-------|
   |			|
936 937 938 939 940 941 942 943

   Client transitions:
   INIT -(C1)-> CALLING -(C2)-> PROCEEDING -(C3+C4)-> READY
   READY -(T1)-> TERMINATED

   Server transitions:
   INIT -(S1)-> RECEIVED -(S2a)-> EARLY -(S3a)-> COMPLETED -(S4)-> READY
   READY -(T2)-> TERMINATING -(T3)-> TERMINATED
Pekka Pessi's avatar
Pekka Pessi committed
944 945 946

   See @page nua_call_model in nua.docs for more information

947 948
*/

Pekka Pessi's avatar
Pekka Pessi committed
949
CONDITION_FUNCTION(receive_basic_call)
950 951 952
{
  int state = nua_callstate_init;

Pekka Pessi's avatar
Pekka Pessi committed
953
  if (!check_handle(ep, nh, SIP_486_BUSY_HERE))
Pekka Pessi's avatar
Pekka Pessi committed
954
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
955 956
  if (event != nua_i_active && event != nua_i_terminated)
    save_event_in_list(ctx, ep);
957

958
  if (event != nua_i_state)
959
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
960

961 962 963 964 965 966 967 968 969 970
  tl_gets(tags, NUTAG_CALLSTATE_REF(state), TAG_END());

  switch (state) {
  case nua_callstate_init:
    return 0;
  case nua_callstate_calling:
    return 0;
  case nua_callstate_proceeding:
    return 0;
  case nua_callstate_received:
Pekka Pessi's avatar
Pekka Pessi committed
971
    respond(ep, nh, SIP_180_RINGING, TAG_END());
972 973
    return 0;
  case nua_callstate_early:
Pekka Pessi's avatar
Pekka Pessi committed
974 975 976 977
    respond(ep, nh, SIP_200_OK,
	    SOATAG_USER_SDP_STR("m=audio 5010 RTP/AVP 8\n"
				"a=rtcp:5011"),
	    TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
978
    return 0;
979
  case nua_callstate_completed:
980 981
    return 0;
  case nua_callstate_ready:
Pekka Pessi's avatar
Pekka Pessi committed
982
    bye(ep, nh, TAG_END());
983 984 985 986 987 988 989 990 991 992 993 994 995 996
    return 0;
  case nua_callstate_terminating:
    return 0;
  case nua_callstate_terminated:
    return 1;
  default:
    return 0;
  }
}

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

Pekka Pessi's avatar
Pekka Pessi committed
997
  struct endpoint *a = &ctx->a,  *b = &ctx->b;
Pekka Pessi's avatar
Pekka Pessi committed
998 999
  struct event *e;

Pekka Pessi's avatar
Pekka Pessi committed
1000
  TEST_1(a->nh = nua_handle(a->nua, 0, SIPTAG_TO(b->address), TAG_END()));
Pekka Pessi's avatar
Pekka Pessi committed
1001

Pekka Pessi's avatar
Pekka Pessi committed
1002 1003 1004
  invite(a, a->nh, NUTAG_URL(b->contact->m_url),
	 SOATAG_USER_SDP_STR("m=audio 5008 RTP/AVP 8"),
	 TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
1005 1006 1007

  run_until(ctx, -1, save_events, -1, receive_basic_call);

1008 1009 1010 1011 1012 1013
  /* 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
     READY -(T1)-> TERMINATED: nua_i_bye, nua_i_state
  */
Pekka Pessi's avatar
Pekka Pessi committed
1014 1015 1016
  TEST_1(e = a->events.head); TEST(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)); 
1017 1018
  TEST_1(e = e->next); TEST(e->data->e_event, nua_r_invite);
  TEST_1(e = e->next); TEST(e->data->e_event, nua_i_state);
Pekka Pessi's avatar
Pekka Pessi committed
1019
  TEST(callstate(e->data->e_tags), nua_callstate_proceeding); /* PROCEEDING */
1020 1021
  TEST_1(e = e->next); TEST(e->data->e_event, nua_r_invite);
  TEST_1(e = e->next); TEST(e->data->e_event, nua_i_state);
Pekka Pessi's avatar
Pekka Pessi committed
1022 1023
  TEST(callstate(e->data->e_tags), nua_callstate_ready); /* READY */
  TEST_1(is_answer_recv(e->data->e_tags)); 
1024 1025
  TEST_1(e = e->next); TEST(e->data->e_event, nua_i_bye);
  TEST_1(e = e->next); TEST(e->data->e_event, nua_i_state);
Pekka Pessi's avatar
Pekka Pessi committed
1026
  TEST(callstate(e->data->e_tags), nua_callstate_terminated); /* TERMINATED */
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  TEST_1(!e->next);

  /*
   Server transitions:
   INIT -(S1)-> RECEIVED: nua_i_invite, nua_i_state
   RECEIVED -(S2a)-> EARLY: nua_respond(), nua_i_state
   EARLY -(S3a)-> COMPLETED: nua_respond(), nua_i_state
   COMPLETED -(S4)-> READY: nua_i_ack, nua_i_state

   READY --(T2)--> TERMINATING: nua_bye()
   TERMINATING --(T3)--> TERMINATED: nua_r_bye, nua_i_state
  */
Pekka Pessi's avatar
Pekka Pessi committed
1039
  TEST_1(e = b->events.head); TEST(e->data->e_event, nua_i_invite);
1040
  TEST_1(e = e->next); TEST(e->data->e_event, nua_i_state);
Pekka Pessi's avatar
Pekka Pessi committed
1041 1042
  TEST(callstate(e->data->e_tags), nua_callstate_received); /* RECEIVED */
  TEST_1(is_offer_recv(e->data->e_tags)); 
1043
  TEST_1(e = e->next); TEST(e->data->e_event, nua_i_state);
Pekka Pessi's avatar
Pekka Pessi committed
1044
  TEST(callstate(e->data->e_tags), nua_callstate_early); /* EARLY */
1045
  TEST_1(e = e->next); TEST(e->data->e_event, nua_i_state);
Pekka Pessi's avatar
Pekka Pessi committed
1046 1047
  TEST(callstate(e->data->e_tags), nua_callstate_completed); /* COMPLETED */
  TEST_1(is_answer_sent(e->data->e_tags)); 
1048 1049
  TEST_1(e = e->next); TEST(e->data->e_event, nua_i_ack);
  TEST_1(e = e->next); TEST(e->data->e_event, nua_i_state);
Pekka Pessi's avatar
Pekka Pessi committed
1050
  TEST(callstate(e->data->e_tags), nua_callstate_ready); /* READY */
1051 1052
  TEST_1(e = e->next); TEST(e->data->e_event, nua_r_bye);
  TEST_1(e = e->next); TEST(e->data->e_event, nua_i_state);
Pekka Pessi's avatar
Pekka Pessi committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
  TEST(callstate(e->data->e_tags), nua_callstate_terminated); /* TERMINATED */
  TEST_1(!e->next);

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

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

  END();
}

/* ======================================================================== */
/* Call rejections */

CONDITION_FUNCTION(until_terminated)
{
  if (!check_handle(ep, nh, 0, 0)) 
    return 0;

  if (event != nua_i_active && event != nua_i_terminated)
    save_event_in_list(ctx, ep);

  return event == nua_i_state && callstate(tags) == nua_callstate_terminated;
}

char const *call_subject(sip_t const *sip)
{
  if (sip && sip->sip_subject && sip->sip_subject->g_string)
    return sip->sip_subject->g_string;
  else
    return "unknown";
}

/*
 A      reject-1      B
 |                    |
 |-------INVITE------>|
 |<----100 Trying-----|
 |                    |
 |<--------486--------|
 |---------ACK------->|
*/
CONDITION_FUNCTION(reject_1)
{
  if (!check_handle(ep, nh, SIP_500_INTERNAL_SERVER_ERROR)) 
    return 0;

  if (event != nua_i_active && event != nua_i_terminated)
    save_event_in_list(ctx, ep);
  
  switch (callstate(tags)) {
  case nua_callstate_received:
    respond(ep, nh, SIP_486_BUSY_HERE, TAG_END());
    return 0;

  case nua_callstate_terminated:
    nua_handle_destroy(ep->nh), ep->nh = NULL;
    return 0;

  default:
    return 0;
  }
}

/*
 A      reject-2      B
 |                    |
 |-------INVITE------>|
 |<----100 Trying-----|
 |                    |
 |<----180 Ringing----|
 |                    |
 |<--------602--------|
 |---------ACK------->|
*/
CONDITION_FUNCTION(reject_2)
{
  if (!check_handle(ep, nh, SIP_500_INTERNAL_SERVER_ERROR)) 
    return 0;

  if (event != nua_i_active && event != nua_i_terminated)
    save_event_in_list(ctx, ep);
  
  switch (callstate(tags)) {
  case nua_callstate_received:
    respond(ep, nh, SIP_180_RINGING, TAG_END());
    return 0;
  case nua_callstate_early:
    respond(ep, nh, 602, "Rejected 2", TAG_END());
    return 0;
  case nua_callstate_terminated:
    nua_handle_destroy(ep->nh), ep->nh = NULL;
    return 0;
  default:
    return 0;
  }
}


/*
 A      reject-3      B
 |                    |
 |-------INVITE------>|
 |<----100 Trying-----|
 |                    |
 |<-----302 Other-----|
 |--------ACK-------->|
 |                    |
 |-------INVITE------>|
 |<----100 Trying-----|
 |                    |
 |<----180 Ringing----|
 |                    |
 |<---604 Nowhere-----|
 |--------ACK-------->|
*/
CONDITION_FUNCTION(reject_4);
CONDITION_FUNCTION(reject_3)
{
  if (!check_handle(ep, nh, SIP_500_INTERNAL_SERVER_ERROR)) 
    return 0;

  if (event != nua_i_active && event != nua_i_terminated)
    save_event_in_list(ctx, ep);

  switch (callstate(tags)) {
  case nua_callstate_received:
    {
      sip_contact_t m[1];
      *m = *ep->contact;
      m->m_url->url_user = "302";
      respond(ep, nh, SIP_302_MOVED_TEMPORARILY, 
	      SIPTAG_CONTACT(m), TAG_END());
      ep->next_condition = reject_4;
    }
    return 0;
  case nua_callstate_terminated:
    nua_handle_destroy(ep->nh), ep->nh = NULL;
    return 0;
  default:
    return 0;
  }
}

CONDITION_FUNCTION(reject_4)
{
  if (!check_handle(ep, nh, SIP_500_INTERNAL_SERVER_ERROR)) 
    return 0;

  if (event != nua_i_active && event !=<