test_nua.c 125 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 49 50

struct call;
#define NUA_HMAGIC_T struct call
Pekka Pessi's avatar
Pekka Pessi committed
51 52 53

#include "nua.h"
#include "nua_tag.h"
54
#include "sip_status.h"
Pekka Pessi's avatar
Pekka Pessi committed
55 56

#include <sdp.h>
57
#include <sip_header.h>
Pekka Pessi's avatar
Pekka Pessi committed
58 59

#include <su_log.h>
Pekka Pessi's avatar
Pekka Pessi committed
60
#include <su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
61
#include <su_tag_io.h>
Pekka Pessi's avatar
Pekka Pessi committed
62 63

extern su_log_t nua_log[];
64 65 66
extern su_log_t soa_log[];
extern su_log_t nta_log[];
extern su_log_t tport_log[];
Pekka Pessi's avatar
Pekka Pessi committed
67 68 69
extern su_log_t su_log_default[];

char const name[] = "test_nua";
70 71

int print_headings = 1;
Pekka Pessi's avatar
Pekka Pessi committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85
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)

86 87
#define TEST_E(a, b) TEST_S(nua_event_name(a), nua_event_name(b))

88 89 90
struct endpoint;

typedef
Pekka Pessi's avatar
Pekka Pessi committed
91 92 93
int condition_function(nua_event_t event,
		       int status, char const *phrase,
		       nua_t *nua, struct context *ctx,
94
		       struct endpoint *ep,
Pekka Pessi's avatar
Pekka Pessi committed
95
		       nua_handle_t *nh, struct call *call,
Pekka Pessi's avatar
Pekka Pessi committed
96 97 98
		       sip_t const *sip,
		       tagi_t tags[]);

Pekka Pessi's avatar
Pekka Pessi committed
99 100 101 102 103 104 105 106 107 108
typedef
void printer_function(nua_event_t event,
		      char const *operation,
		      int status, char const *phrase,
		      nua_t *nua, struct context *ctx,
		      struct endpoint *ep,
		      nua_handle_t *nh, struct call *call,
		      sip_t const *sip,
		      tagi_t tags[]);

109
struct context
Pekka Pessi's avatar
Pekka Pessi committed
110 111 112 113
{
  su_home_t home[1];
  su_root_t *root;

Pekka Pessi's avatar
Pekka Pessi committed
114 115
  int threading;

116
  struct endpoint {
Pekka Pessi's avatar
Pekka Pessi committed
117
    char name[4];
Pekka Pessi's avatar
Pekka Pessi committed
118
    struct context *ctx;	/* Backpointer */
119 120 121

    int running;

122 123
    condition_function *next_condition;
    nua_event_t next_event, last_event;
Pekka Pessi's avatar
Pekka Pessi committed
124
    nua_t *nua;
125
    sip_contact_t *contact;
Pekka Pessi's avatar
Pekka Pessi committed
126
    sip_from_t *to;
Pekka Pessi's avatar
Pekka Pessi committed
127

Pekka Pessi's avatar
Pekka Pessi committed
128
    printer_function *printer;
Pekka Pessi's avatar
Pekka Pessi committed
129

Pekka Pessi's avatar
Pekka Pessi committed
130
    /* Per-call stuff */
Pekka Pessi's avatar
Pekka Pessi committed
131 132 133 134 135 136
    struct call {
      struct call *next;
      nua_handle_t *nh;
      struct {
	struct event *head, **tail;
      } events;
137
      char const *sdp;
Pekka Pessi's avatar
Pekka Pessi committed
138
    } call[1];
Pekka Pessi's avatar
Pekka Pessi committed
139 140 141 142 143 144 145 146 147 148

    /* State flags for complex scenarios */
    union {
      struct {
	unsigned bit0:1, bit1:1, bit2:1, bit3:1;
	unsigned bit4:1, bit5:1, bit6:1, bit7:1;
      } b;
      unsigned n;
    } flags;

149
  } a, b, c;
Pekka Pessi's avatar
Pekka Pessi committed
150 151
};

152
struct event
153 154 155
{
  struct event *next, **prev;
  nua_saved_event_t saved_event[1];
Pekka Pessi's avatar
Pekka Pessi committed
156
  nua_event_data_t const *data;
157 158
};

Pekka Pessi's avatar
Pekka Pessi committed
159 160 161 162 163 164
static int save_event_in_list(struct context *,
			      nua_event_t nevent,
			      struct endpoint *,
			      struct call *);
static void free_events_in_list(struct context *,
				struct call *);
165

166 167 168 169 170
#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
171
	   nua_handle_t *nh, struct call *call, \
172 173 174
	   sip_t const *sip,			\
	   tagi_t tags[])

175 176
CONDITION_FUNCTION(until_final_response){ return status >= 200; }
CONDITION_FUNCTION(save_until_final_response)
177
{
Pekka Pessi's avatar
Pekka Pessi committed
178 179
  save_event_in_list(ctx, event, ep, ep->call);
  return event >= nua_r_set_params && status >= 200;
180 181
}

Pekka Pessi's avatar
Pekka Pessi committed
182 183 184
/** Save events (except nua_i_active or terminated).
 * Terminate when a event is saved.
 */
185 186
CONDITION_FUNCTION(save_until_received)
{
Pekka Pessi's avatar
Pekka Pessi committed
187
  save_event_in_list(ctx, event, ep, ep->call);
188 189 190
  return 1;
}

Pekka Pessi's avatar
Pekka Pessi committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
/* 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;
}

Pekka Pessi's avatar
Pekka Pessi committed
240 241 242 243 244 245 246 247 248
static
void print_event(nua_event_t event,
		 char const *operation,
		 int status, char const *phrase,
		 nua_t *nua, struct context *ctx,
		 struct endpoint *ep,
		 nua_handle_t *nh, struct call *call,
		 sip_t const *sip,
		 tagi_t tags[])
Pekka Pessi's avatar
Pekka Pessi committed
249 250 251
{
  if (event == nua_i_state) {
    fprintf(stderr, "%s.nua(%p): event %s %s\n",
252
	    ep->name, nh, nua_event_name(event),
Pekka Pessi's avatar
Pekka Pessi committed
253 254
	    nua_callstate_name(callstate(tags)));
  }
Pekka Pessi's avatar
Pekka Pessi committed
255
  else if ((int)event >= nua_r_set_params) {
Pekka Pessi's avatar
Pekka Pessi committed
256 257 258
    fprintf(stderr, "%s.nua(%p): event %s status %u %s\n",
	    ep->name, nh, nua_event_name(event), status, phrase);
  }
Pekka Pessi's avatar
Pekka Pessi committed
259 260 261 262
  else if ((int)event >= 0) {
    fprintf(stderr, "%s.nua(%p): event %s %s\n",
	    ep->name, nh, nua_event_name(event), phrase);
  }
Pekka Pessi's avatar
Pekka Pessi committed
263 264
  else if (status > 0) {
    fprintf(stderr, "%s.nua(%p): call %s() with status %u %s\n",
265
	    ep->name, nh, operation, status, phrase);
Pekka Pessi's avatar
Pekka Pessi committed
266 267
  }
  else {
Pekka Pessi's avatar
Pekka Pessi committed
268 269 270 271 272
    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",
Pekka Pessi's avatar
Pekka Pessi committed
273
	      ep->name, nh, operation, subject);
Pekka Pessi's avatar
Pekka Pessi committed
274 275 276
    }
    else
      fprintf(stderr, "%s.nua(%p): call %s()\n",
Pekka Pessi's avatar
Pekka Pessi committed
277
	      ep->name, nh, operation);
Pekka Pessi's avatar
Pekka Pessi committed
278 279 280 281 282
  }

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

284 285 286 287
void ep_callback(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
288
		 nua_handle_t *nh, struct call *call,
289 290
		 sip_t const *sip,
		 tagi_t tags[])
291
{
Pekka Pessi's avatar
Pekka Pessi committed
292
  if (ep->printer)
Pekka Pessi's avatar
Pekka Pessi committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    ep->printer(event, "", status, phrase, nua, ctx, ep, nh, call, sip, tags);

  if (call == NULL) {
    for (call = ep->call; call; call = call->next) {
      if (!call->nh)
	break;
      if (nh == call->nh)
	break;
    }

    if (call && call->nh == NULL) {
      call->nh = nh;
      nua_handle_bind(nh, call);
    }
  }
Pekka Pessi's avatar
Pekka Pessi committed
308 309 310 311 312

  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)))
313
    ep->running = 0;
314

Pekka Pessi's avatar
Pekka Pessi committed
315
  ep->last_event = event;
316 317
}

318
void a_callback(nua_event_t event,
319 320
		int status, char const *phrase,
		nua_t *nua, struct context *ctx,
Pekka Pessi's avatar
Pekka Pessi committed
321
		nua_handle_t *nh, struct call *call,
322 323
		sip_t const *sip,
		tagi_t tags[])
Pekka Pessi's avatar
Pekka Pessi committed
324
{
325 326
  ep_callback(event, status, phrase, nua, ctx, &ctx->a, nh, call, sip, tags);
}
327

328 329 330
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
331
		nua_handle_t *nh, struct call *call,
332 333 334 335 336
		sip_t const *sip,
		tagi_t tags[])
{
  ep_callback(event, status, phrase, nua, ctx, &ctx->b, nh, call, sip, tags);
}
Pekka Pessi's avatar
Pekka Pessi committed
337

338 339 340
void c_callback(nua_event_t event,
		int status, char const *phrase,
		nua_t *nua, struct context *ctx,
Pekka Pessi's avatar
Pekka Pessi committed
341
		nua_handle_t *nh, struct call *call,
342 343 344 345
		sip_t const *sip,
		tagi_t tags[])
{
  ep_callback(event, status, phrase, nua, ctx, &ctx->c, nh, call, sip, tags);
Pekka Pessi's avatar
Pekka Pessi committed
346 347
}

348 349 350 351
void run_abc_until(struct context *ctx,
		   nua_event_t a_event, condition_function *a_condition,
		   nua_event_t b_event, condition_function *b_condition,
		   nua_event_t c_event, condition_function *c_condition)
Pekka Pessi's avatar
Pekka Pessi committed
352
{
353
  struct endpoint *a = &ctx->a, *b = &ctx->b, *c = &ctx->c;
354 355 356 357 358

  a->next_event = a_event;
  a->next_condition = a_condition;
  a->last_event = -1;
  a->running = a_condition != NULL || a_event != -1;
Pekka Pessi's avatar
Pekka Pessi committed
359
  a->flags.n = 0;
360 361 362 363 364

  b->next_event = b_event;
  b->next_condition = b_condition;
  b->last_event = -1;
  b->running = b_condition != NULL || b_event != -1;
Pekka Pessi's avatar
Pekka Pessi committed
365
  b->flags.n = 0;
Pekka Pessi's avatar
Pekka Pessi committed
366

367 368 369 370
  c->next_event = c_event;
  c->next_condition = c_condition;
  c->last_event = -1;
  c->running = c_condition != NULL || c_event != -1;
Pekka Pessi's avatar
Pekka Pessi committed
371
  c->flags.n = 0;
372 373

  for (; a->running || b->running || c->running;) {
Pekka Pessi's avatar
Pekka Pessi committed
374 375
    su_root_step(ctx->root, 1000);
  }
376
}
Pekka Pessi's avatar
Pekka Pessi committed
377

378 379 380 381 382 383 384
void run_ab_until(struct context *ctx,
		  nua_event_t a_event, condition_function *a_condition,
		  nua_event_t b_event, condition_function *b_condition)
{
  run_abc_until(ctx, a_event, a_condition, b_event, b_condition, -1, NULL);
}

385 386 387 388
int run_a_until(struct context *ctx,
		nua_event_t a_event,
		condition_function *a_condition)
{
389
  run_abc_until(ctx, a_event, a_condition, -1, NULL, -1, NULL);
390 391 392 393 394 395 396
  return ctx->a.last_event;
}

int run_b_until(struct context *ctx,
		nua_event_t b_event,
		condition_function *b_condition)
{
397
  run_abc_until(ctx, -1, NULL, b_event, b_condition, -1, NULL);
398
  return ctx->b.last_event;
Pekka Pessi's avatar
Pekka Pessi committed
399 400
}

Pekka Pessi's avatar
Pekka Pessi committed
401 402 403 404 405 406 407 408
int run_c_until(struct context *ctx,
		nua_event_t event,
		condition_function *condition)
{
  run_abc_until(ctx, -1, NULL, -1, NULL, event, condition);
  return ctx->c.last_event;
}

Pekka Pessi's avatar
Pekka Pessi committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
#define OPERATION(x) \
int x(struct endpoint *ep, \
      struct call *call, 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, "nua_" #x, 0, "", ep->nua, ep->ctx, ep, \
		nh, call, NULL, ta_args(ta)); \
\
  nua_##x(nh, ta_tags(ta)); \
\
  ta_end(ta); \
  return 0; \
} extern int dummy

OPERATION(invite);
429
OPERATION(ack);
Pekka Pessi's avatar
Pekka Pessi committed
430 431 432
OPERATION(bye);
OPERATION(cancel);
OPERATION(authenticate);
433
OPERATION(update);
434
OPERATION(info);
Pekka Pessi's avatar
Pekka Pessi committed
435 436 437 438
OPERATION(refer);
OPERATION(message);
OPERATION(options);
OPERATION(publish);
439 440
OPERATION(unpublish);
OPERATION(unregister);
Pekka Pessi's avatar
Pekka Pessi committed
441
OPERATION(subscribe);
Pekka Pessi's avatar
Pekka Pessi committed
442
OPERATION(unsubscribe);
443 444 445
OPERATION(notify);
OPERATION(notifier);
OPERATION(terminate);
446

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
int do_register(struct endpoint *ep,
		struct call *call, 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, "nua_" "register", 0, "", ep->nua, ep->ctx, ep,
		nh, call, NULL, ta_args(ta));

  nua_register(nh, ta_tags(ta));

  ta_end(ta);
  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
465
/* Respond via endpoint and handle */
Pekka Pessi's avatar
Pekka Pessi committed
466 467 468
int respond(struct endpoint *ep,
	    struct call *call,
	    nua_handle_t *nh,
Pekka Pessi's avatar
Pekka Pessi committed
469 470 471 472 473 474 475
	    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
476 477

  if (ep->printer)
Pekka Pessi's avatar
Pekka Pessi committed
478 479
    ep->printer(-1, "nua_respond", status, phrase, ep->nua, ep->ctx, ep,
		nh, call, NULL, ta_args(ta));
Pekka Pessi's avatar
Pekka Pessi committed
480

Pekka Pessi's avatar
Pekka Pessi committed
481 482 483 484 485 486
  nua_respond(nh, status, phrase, ta_tags(ta));
  ta_end(ta);

  return 0;
}

487

Pekka Pessi's avatar
Pekka Pessi committed
488 489 490 491 492
/* Reject all but currently used handles */
struct call *check_handle(struct endpoint *ep,
			  struct call *call,
			  nua_handle_t *nh,
			  int status, char const *phrase)
493
{
Pekka Pessi's avatar
Pekka Pessi committed
494 495
  if (call)
    return call;
496

Pekka Pessi's avatar
Pekka Pessi committed
497 498
  if (status)
    respond(ep, call, nh, status, phrase, TAG_END());
499

Pekka Pessi's avatar
Pekka Pessi committed
500 501
  nua_handle_destroy(nh);
  return NULL;
502 503
}

Pekka Pessi's avatar
Pekka Pessi committed
504

Pekka Pessi's avatar
Pekka Pessi committed
505 506
static void
call_init(struct call *call)
Pekka Pessi's avatar
Pekka Pessi committed
507
{
Pekka Pessi's avatar
Pekka Pessi committed
508
  call->events.tail = &call->events.head;
Pekka Pessi's avatar
Pekka Pessi committed
509 510
}

Pekka Pessi's avatar
Pekka Pessi committed
511
/* Save nua event in call-specific list */
512
static
513
int save_event_in_list(struct context *ctx,
Pekka Pessi's avatar
Pekka Pessi committed
514 515 516 517
		       nua_event_t nevent,
		       struct endpoint *ep,
		       struct call *call)

518
{
Pekka Pessi's avatar
Pekka Pessi committed
519 520 521 522 523 524
  struct event *e;

  if (nevent == nua_i_active || nevent == nua_i_terminated)
    return 0;

  e = su_zalloc(ctx->home, sizeof *e);
525 526

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

528 529
  if (!nua_save_event(ep->nua, e->saved_event)) {
    su_free(ctx->home, e);
Pekka Pessi's avatar
Pekka Pessi committed
530
    return -1;
531
  }
Pekka Pessi's avatar
Pekka Pessi committed
532

533 534
  *(e->prev = call->events.tail) = e;
  call->events.tail = &e->next;
Pekka Pessi's avatar
Pekka Pessi committed
535
  e->data = nua_event_data(e->saved_event);
536

Pekka Pessi's avatar
Pekka Pessi committed
537
  return 1;
538
}
Pekka Pessi's avatar
Pekka Pessi committed
539

Pekka Pessi's avatar
Pekka Pessi committed
540
/* Save nua event in endpoint list */
541
static
Pekka Pessi's avatar
Pekka Pessi committed
542 543
void free_events_in_list(struct context *ctx,
			 struct call *call)
Pekka Pessi's avatar
Pekka Pessi committed
544 545 546
{
  struct event *e;

Pekka Pessi's avatar
Pekka Pessi committed
547
  while ((e = call->events.head)) {
Pekka Pessi's avatar
Pekka Pessi committed
548 549 550 551 552
    if ((*e->prev = e->next))
      e->next->prev = e->prev;
    nua_destroy_event(e->saved_event);
    su_free(ctx->home, e);
  }
Pekka Pessi's avatar
Pekka Pessi committed
553
  call->events.tail = &call->events.head;
Pekka Pessi's avatar
Pekka Pessi committed
554 555
}

Pekka Pessi's avatar
Pekka Pessi committed
556 557
void nolog(void *stream, char const *fmt, va_list ap) {}

Pekka Pessi's avatar
Pekka Pessi committed
558 559 560 561 562
int check_set_status(int status, char const *phrase)
{
  return status == 200 && strcmp(phrase, sip_200_OK) == 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
563 564 565 566
int test_api_errors(struct context *ctx)
{
  BEGIN();

Pekka Pessi's avatar
Pekka Pessi committed
567
  /* Invoke every API function with invalid arguments */
Pekka Pessi's avatar
Pekka Pessi committed
568

Pekka Pessi's avatar
Pekka Pessi committed
569 570
  int level;

Pekka Pessi's avatar
Pekka Pessi committed
571 572
  int status; char const *phrase;

573 574 575
  if (print_headings)
    printf("TEST NUA-1.0: test API\n");

Pekka Pessi's avatar
Pekka Pessi committed
576 577 578 579 580
  /* This is a nasty macro. Test it. */
#define SET_STATUS1(x) ((status = x), status), (phrase = ((void)x))
  TEST_1(check_set_status(SET_STATUS1(SIP_200_OK)));
  TEST(status, 200); TEST_S(phrase, sip_200_OK);

Pekka Pessi's avatar
Pekka Pessi committed
581 582 583 584 585
  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
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

  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
627

Pekka Pessi's avatar
Pekka Pessi committed
628 629
  TEST_1(!nua_handle_home(NULL));
  TEST_1(!nua_save_event(NULL, NULL));
630
  TEST_1(!nua_event_data(NULL));
Pekka Pessi's avatar
Pekka Pessi committed
631
  TEST_VOID(nua_destroy_event(NULL));
632

Pekka Pessi's avatar
Pekka Pessi committed
633 634 635 636
  {
    nua_saved_event_t event[1];

    memset(event, 0, sizeof event);
637

Pekka Pessi's avatar
Pekka Pessi committed
638
    TEST_1(!nua_save_event(NULL, event));
639
    TEST_1(!nua_event_data(event));
Pekka Pessi's avatar
Pekka Pessi committed
640 641 642
    TEST_VOID(nua_destroy_event(event));
  }

Pekka Pessi's avatar
Pekka Pessi committed
643
  su_log_set_level(nua_log, level);
Pekka Pessi's avatar
Pekka Pessi committed
644

645 646 647
  if (print_headings)
    printf("TEST NUA-1.0: PASSED\n");

Pekka Pessi's avatar
Pekka Pessi committed
648 649 650 651 652 653
  END();
}

int test_params(struct context *ctx)
{
  BEGIN();
654 655 656 657 658 659 660 661

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

662 663 664
  if (print_headings)
    printf("TEST NUA-2.0: PARAMETERS\n");

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

Pekka Pessi's avatar
Pekka Pessi committed
667
  /* Disable threading by command line switch? */
Pekka Pessi's avatar
Pekka Pessi committed
668
  su_root_threading(ctx->root, ctx->threading);
Pekka Pessi's avatar
Pekka Pessi committed
669

670 671
  ctx->a.nua = nua_create(ctx->root, a_callback, ctx,
			  SIPTAG_FROM_STR("sip:alice@example.com"),
Pekka Pessi's avatar
Pekka Pessi committed
672
			  NUTAG_URL("sip:*:*;transport=udp"),
673
			  TAG_END());
674

675 676
  TEST_1(ctx->a.nua);

677
  from = sip_from_make(tmphome, Alice);
678 679 680

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

681
  nua_set_hparams(nh, NUTAG_INVITE_TIMER(90), TAG_END());
682
  run_a_until(ctx, nua_r_set_params, until_final_response);
683

684 685 686 687 688 689 690 691 692 693 694 695 696 697
  /* 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());

698
  run_a_until(ctx, nua_r_set_params, until_final_response);
699

700 701 702
  /* Modify everything from their default value */
  nua_set_params(ctx->a.nua,
		 SIPTAG_FROM(from),
703
		 NUTAG_RETRY_COUNT(9),
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
		 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),

727 728 729
		 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")),
730

731
		 SIPTAG_ORGANIZATION(sip_organization_make(tmphome, "Pussy Galore's Flying Circus")),
732 733

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

736 737
		 TAG_END());

738
  run_a_until(ctx, nua_r_set_params, until_final_response);
739

740 741 742 743
  /* Modify something... */
  nua_set_params(ctx->a.nua,
		 NUTAG_RETRY_COUNT(5),
		 TAG_END());
744
  run_a_until(ctx, nua_r_set_params, until_final_response);
745 746 747 748 749

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

Pekka Pessi's avatar
Pekka Pessi committed
750 751
    unsigned retry_count = -1;
    unsigned max_subscriptions = -1;
752

753 754 755 756 757
    int invite_enable = -1;
    int auto_alert = -1;
    int early_media = -1;
    int auto_answer = -1;
    int auto_ack = -1;
Pekka Pessi's avatar
Pekka Pessi committed
758
    unsigned invite_timeout = -1;
759

Pekka Pessi's avatar
Pekka Pessi committed
760 761
    unsigned session_timer = -1;
    unsigned min_se = -1;
762 763
    int refresher = -1;
    int update_refresh = -1;
764

765 766 767
    int message_enable = -1;
    int win_messenger_enable = -1;
    int message_auto_respond = -1;
768

769 770 771 772 773 774
    int callee_caps = -1;
    int media_features = -1;
    int service_route_enable = -1;
    int path_enable = -1;

    sip_allow_t const *allow = NONE;
775
    char const *allow_str = "NONE";
776 777 778 779 780 781 782 783 784 785
    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;
786
    struct event *e;
787

Pekka Pessi's avatar
Pekka Pessi committed
788
    nua_get_params(ctx->a.nua, TAG_ANY(), TAG_END());
789
    run_a_until(ctx, nua_r_get_params, save_until_final_response);
790

Pekka Pessi's avatar
Pekka Pessi committed
791
    TEST_1(e = ctx->a.call->events.head);
792
    TEST_E(e->data->e_event, nua_r_get_params);
793

794
    n = tl_gets(e->data->e_tags,
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
	       	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");
870
    TEST_S(sip_header_as_string(tmphome, (void *)organization),
871 872 873
	   "Pussy Galore's Flying Circus");
    TEST_S(organization_str, "Pussy Galore's Flying Circus");

874
    TEST_S(url_as_string(tmphome, registrar->us_url),
875
	   "sip:sip.wonderland.org");
876

Pekka Pessi's avatar
Pekka Pessi committed
877
    free_events_in_list(ctx, ctx->a.call);
878
  }
879

880 881 882 883
  {
    sip_from_t const *from = NONE;
    char const *from_str = "NONE";

Pekka Pessi's avatar
Pekka Pessi committed
884 885
    unsigned retry_count = -1;
    unsigned max_subscriptions = -1;
886

887 888 889 890 891
    int invite_enable = -1;
    int auto_alert = -1;
    int early_media = -1;
    int auto_answer = -1;
    int auto_ack = -1;
Pekka Pessi's avatar
Pekka Pessi committed
892
    unsigned invite_timeout = -1;
893

Pekka Pessi's avatar
Pekka Pessi committed
894 895
    unsigned session_timer = -1;
    unsigned min_se = -1;
896 897
    int refresher = -1;
    int update_refresh = -1;
898

899 900 901
    int message_enable = -1;
    int win_messenger_enable = -1;
    int message_auto_respond = -1;
902

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    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;
920
    struct event *e;
921

922
    nua_get_hparams(nh, TAG_ANY(), TAG_END());
923
    run_a_until(ctx, nua_r_get_params, save_until_final_response);
924

Pekka Pessi's avatar
Pekka Pessi committed
925
    TEST_1(e = ctx->a.call->events.head);
926
    TEST_E(e->data->e_event, nua_r_get_params);
927

928
    n = tl_gets(e->data->e_tags,
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
	       	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());
969
    TEST(n, 3);
970 971 972

    TEST(invite_timeout, 90);

973 974
    TEST_1(from != NULL && from != NONE);
    TEST_1(strcmp(from_str, "NONE"));
975

976
    /* Nothing else should be set */
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    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
1010

Pekka Pessi's avatar
Pekka Pessi committed
1011
    free_events_in_list(ctx, ctx->a.call);
1012 1013 1014
  }

  nua_handle_destroy(nh);
1015

1016
  nua_shutdown(ctx->a.nua);
1017
  run_a_until(ctx, nua_r_shutdown, until_final_response);
1018 1019 1020 1021 1022
  nua_destroy(ctx->a.nua), ctx->a.nua = NULL;

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

  su_home_deinit(tmphome);
1023

1024 1025 1026
  if (print_headings)
    printf("TEST NUA-2.0: PASSED\n");

Pekka Pessi's avatar
Pekka Pessi committed
1027 1028 1029
  END();
}

1030
int test_init(struct context *ctx, char *argv[])
Pekka Pessi's avatar
Pekka Pessi committed
1031 1032
{
  BEGIN();
1033
  struct event *e;
1034
  sip_contact_t const *m = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1035
  sip_from_t const *sipaddress = NULL;
1036 1037 1038 1039

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

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

1042 1043 1044
  if (print_headings)
    printf("TEST NUA-3.0.1: init endpoint A\n");

1045 1046 1047
  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
1048
			  SOATAG_USER_SDP_STR("m=audio 5004 RTP/AVP 0 8"),
1049 1050 1051 1052
			  TAG_END());
  TEST_1(ctx->a.nua);

  nua_get_params(ctx->a.nua, TAG_ANY(), TAG_END());
1053
  run_a_until(ctx, nua_r_get_params, save_until_final_response);
Pekka Pessi's avatar
Pekka Pessi committed
1054
  TEST_1(e = ctx->a.call->events.head);
1055
  TEST(tl_gets(e->data->e_tags,