test_nta.c 99.4 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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
 *
 */

/**@internal
26
 * @CFILE test_nta.c
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29 30 31 32 33 34 35 36 37
 *
 * Test functions for NTA.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Aug 21 15:18:26 2001 ppessi
 */

#include "config.h"

typedef struct agent_t agent_t;
38
typedef struct client_t client_t;
Pekka Pessi's avatar
Pekka Pessi committed
39 40 41

#define SU_ROOT_MAGIC_T      agent_t

42
#include <sofia-sip/su_wait.h>
Pekka Pessi's avatar
Pekka Pessi committed
43

44 45
#include <msg_internal.h>

Pekka Pessi's avatar
Pekka Pessi committed
46 47
#define NTA_AGENT_MAGIC_T    agent_t
#define NTA_LEG_MAGIC_T      agent_t
48
#define NTA_OUTGOING_MAGIC_T client_t
Pekka Pessi's avatar
Pekka Pessi committed
49 50 51
#define NTA_INCOMING_MAGIC_T agent_t
#define NTA_RELIABLE_MAGIC_T agent_t

52
#include "sofia-sip/nta.h"
53
#include "sofia-sip/nta_tport.h"
54 55 56 57 58 59 60
#include <sofia-sip/sip_header.h>
#include <sofia-sip/sip_tag.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/tport.h>
#include <sofia-sip/htable.h>
#include <sofia-sip/sresolv.h>
#include <sofia-sip/su_log.h>
61
#include <sofia-sip/sofia_features.h>
62
#include <sofia-sip/hostdomain.h>
63
#include <sofia-sip/tport.h>
Pekka Pessi's avatar
Pekka Pessi committed
64

65
#include <sofia-sip/su_string.h>
Pekka Pessi's avatar
Pekka Pessi committed
66

67 68 69 70 71 72
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <assert.h>
73
#include <unistd.h>
74

75 76 77 78
#if HAVE_OPEN_C
#include <sys/param.h>
#endif

Pekka Pessi's avatar
Pekka Pessi committed
79 80
SOFIAPUBVAR su_log_t nta_log[];
SOFIAPUBVAR su_log_t tport_log[];
Pekka Pessi's avatar
Pekka Pessi committed
81 82 83 84

int tstflags = 0;
#define TSTFLAGS tstflags

85
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
86 87 88 89 90 91 92 93

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

94 95
#define NONE ((void *)-1)

96 97 98 99
int expensive_checks;

#define EXPENSIVE_CHECKS (expensive_checks)

Pekka Pessi's avatar
Pekka Pessi committed
100 101
struct sigcomp_compartment;

102
char const name[] = "test_nta";
Pekka Pessi's avatar
Pekka Pessi committed
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
typedef struct invite_client_t invite_client_t;

typedef int client_check_f(client_t *, nta_outgoing_t *, sip_t const *);
typedef int client_deinit_f(client_t *);

struct client_t {
  agent_t *c_ag;
  char const *c_name;
  client_check_f * c_check;
  client_check_f * const * c_checks;
  client_deinit_f * c_deinit;
  void *c_extra;
  nta_outgoing_t *c_orq;
  int c_status;
  int c_final;
  int c_errors;
};

122 123 124 125 126 127 128
struct invite_client_t {
  client_t ic_client[1];
  nta_outgoing_t *ic_orq;	/* Original INVITE transaction */
  int ic_tag_status;		/* Status for current branch */
  char *ic_tag;
};

Pekka Pessi's avatar
Pekka Pessi committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
struct agent_t {
  su_home_t       ag_home[1];
  int             ag_flags;
  su_root_t      *ag_root;
  msg_mclass_t   *ag_mclass;
  nta_agent_t    *ag_agent;

  url_string_t   *ag_obp;	/**< Outbound proxy. */

  nta_leg_t      *ag_server_leg; /**< Leg for sip:%@% */
  nta_leg_t      *ag_default_leg; /**< Leg for rest */

  unsigned        ag_drop;

  nta_outgoing_t *ag_orq;
144
  unsigned        ag_running :1, ag_canceled:1, ag_acked:1, :0;
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

  char const     *ag_comp;
  struct sigcomp_compartment *ag_client_compartment;

  /* Server side */
  int             ag_response;	/**< What we answer by default */
  nta_incoming_t *ag_irq;

  struct sigcomp_compartment *ag_server_compartment;

  char const     *ag_m;

  sip_contact_t const *ag_contact;
  sip_from_t     *ag_alice;
  sip_to_t       *ag_bob;

  sip_contact_t  *ag_m_alice;
  sip_contact_t  *ag_m_bob;
  sip_contact_t  *ag_aliases;

  nta_leg_t      *ag_alice_leg;
  nta_leg_t      *ag_bob_leg;

  msg_t          *ag_request;

  nta_leg_t      *ag_expect_leg;
  nta_leg_t      *ag_latest_leg;
  nta_leg_t      *ag_call_leg;
  nta_reliable_t *ag_reliable;

  sip_via_t      *ag_in_via;	/**< Incoming via */

  sip_content_type_t *ag_content_type;
  sip_payload_t  *ag_payload;

180 181
  msg_t          *ag_probe_msg;

182 183
  su_sockaddr_t   ag_su_nta[1];
  socklen_t       ag_su_nta_len;
184

Pekka Pessi's avatar
Pekka Pessi committed
185 186
  /* Dummy servers */
  char const     *ag_sink_port;
187
  su_socket_t     ag_sink_socket, ag_down_socket;
188
  su_wait_t       ag_sink_wait[1];
Pekka Pessi's avatar
Pekka Pessi committed
189 190 191 192
};

static int test_init(agent_t *ag, char const *resolv_conf);
static int test_deinit(agent_t *ag);
193
static int test_bad_messages(agent_t *ag);
Pekka Pessi's avatar
Pekka Pessi committed
194 195 196 197 198 199 200
static int test_routing(agent_t *ag);
static int test_tports(agent_t *ag);
static int test_resolv(agent_t *ag, char const *resolv_conf);
static int test_dialog(agent_t *ag);
static int test_call(agent_t *ag);
static int test_prack(agent_t *ag);
static int test_fix_467(agent_t *ag);
201 202

static int test_for_ack(agent_t *ag,
203
			nta_incoming_t *irq,
204 205
			sip_t const *sip);
static int test_for_ack_or_timeout(agent_t *ag,
206
				   nta_incoming_t *irq,
207
				   sip_t const *sip);
Pekka Pessi's avatar
Pekka Pessi committed
208

209
static int wait_for_ack_or_cancel(agent_t *ag,
210
				  nta_incoming_t *irq,
211 212
				  sip_t const *sip);

Pekka Pessi's avatar
Pekka Pessi committed
213 214 215 216 217 218 219 220
int agent_callback(agent_t *ag,
		   nta_agent_t *nta,
		   msg_t *msg,
		   sip_t *sip)
{
  if (tstflags & tst_verbatim) {
    if (sip->sip_request) {
      printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
221
	     name, __func__, sip->sip_request->rq_method_name,
Pekka Pessi's avatar
Pekka Pessi committed
222 223 224 225
	     URL_PRINT_ARGS(sip->sip_request->rq_url),
	     sip->sip_request->rq_version);
    }
    else {
226 227 228
      printf("%s: %s: %s %03d %s\n", name, __func__,
	     sip->sip_status->st_version,
	     sip->sip_status->st_status,
Pekka Pessi's avatar
Pekka Pessi committed
229 230 231 232
	     sip->sip_status->st_phrase);
    }
  }

233 234
  msg_destroy(msg);

Pekka Pessi's avatar
Pekka Pessi committed
235 236 237 238 239 240 241 242 243 244 245 246 247
  return 0;
}

static
void leg_match(agent_t *ag, nta_leg_t *leg, int always, char const *func)
{
  char const *match = "unknown leg";

  if (!always && (tstflags & tst_verbatim) != tst_verbatim)
    return;

  if (leg == ag->ag_default_leg)
    match = "ag_default_leg";
248
  else if (leg == ag->ag_server_leg)
Pekka Pessi's avatar
Pekka Pessi committed
249 250 251 252 253 254
    match = "ag_server_leg";
  else if (leg == ag->ag_alice_leg)
    match = "ag_alice_leg";
  else if (leg == ag->ag_bob_leg)
    match = "ag_bob_leg";

255
  printf("%s: %s: %smatched with %s\n", name, func,
Pekka Pessi's avatar
Pekka Pessi committed
256 257 258 259 260 261 262 263
	 always ? "mis" : "", match);
}

static
void leg_zap(agent_t *ag, nta_leg_t *leg)
{
  if (leg == ag->ag_default_leg)
    ag->ag_default_leg = NULL;
264
  else if (leg == ag->ag_server_leg)
Pekka Pessi's avatar
Pekka Pessi committed
265 266 267 268 269
    ag->ag_server_leg = NULL;
  else if (leg == ag->ag_alice_leg)
    ag->ag_alice_leg = NULL;
  else if (leg == ag->ag_bob_leg)
    ag->ag_bob_leg = NULL;
270 271
  else
    printf("%s:%u: %s: did not exist\n",
272
	   __FILE__, __LINE__, __func__);
Pekka Pessi's avatar
Pekka Pessi committed
273 274 275 276

  nta_leg_destroy(leg);
}

277

Pekka Pessi's avatar
Pekka Pessi committed
278 279 280 281 282 283 284
int leg_callback_200(agent_t *ag,
		     nta_leg_t *leg,
		     nta_incoming_t *irq,
		     sip_t const *sip)
{
  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
285
	   name, __func__, sip->sip_request->rq_method_name,
Pekka Pessi's avatar
Pekka Pessi committed
286 287 288 289
	   URL_PRINT_ARGS(sip->sip_request->rq_url),
	   sip->sip_request->rq_version);
  }

290 291 292 293
  if (!sip->sip_content_length ||
      !sip->sip_via ||
      !sip->sip_from || !sip->sip_from->a_tag)
    return 500;
Pekka Pessi's avatar
Pekka Pessi committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

  if (ag->ag_in_via == NULL)
    ag->ag_in_via = sip_via_dup(ag->ag_home, sip->sip_via);

  if (ag->ag_request == NULL)
    ag->ag_request = nta_incoming_getrequest(irq);

  ag->ag_latest_leg = leg;

  if (ag->ag_expect_leg && leg != ag->ag_expect_leg) {
    leg_match(ag, leg, 1, __func__);
    return 500;
  }
  leg_match(ag, leg, 0, __func__);

  if (sip->sip_request->rq_method == sip_method_bye) {
    leg_zap(ag, leg);
  }

  return 200;
}

int leg_callback_500(agent_t *ag,
		     nta_leg_t *leg,
		     nta_incoming_t *irq,
		     sip_t const *sip)
{
  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
323
	   name, __func__, sip->sip_request->rq_method_name,
Pekka Pessi's avatar
Pekka Pessi committed
324 325 326 327 328 329 330
	   URL_PRINT_ARGS(sip->sip_request->rq_url),
	   sip->sip_request->rq_version);
  }

  return 500;
}

331

Pekka Pessi's avatar
Pekka Pessi committed
332 333 334 335 336 337 338
int new_leg_callback_200(agent_t *ag,
			 nta_leg_t *leg,
			 nta_incoming_t *irq,
			 sip_t const *sip)
{
  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
339
	   name, __func__, sip->sip_request->rq_method_name,
Pekka Pessi's avatar
Pekka Pessi committed
340 341 342 343
	   URL_PRINT_ARGS(sip->sip_request->rq_url),
	   sip->sip_request->rq_version);
  }

344 345 346 347
  if (!sip->sip_content_length ||
      !sip->sip_via ||
      !sip->sip_from || !sip->sip_from->a_tag)
    return 500;
Pekka Pessi's avatar
Pekka Pessi committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

  ag->ag_latest_leg = leg;

  if (ag->ag_expect_leg && leg != ag->ag_expect_leg) {
    leg_match(ag, leg, 1, __func__);
    return 500;
  }
  leg_match(ag, leg, 0, __func__);

  ag->ag_bob_leg = nta_leg_tcreate(ag->ag_agent,
				   leg_callback_200,
				   ag,
				   URLTAG_URL(sip->sip_request->rq_url),
				   SIPTAG_CALL_ID(sip->sip_call_id),
				   SIPTAG_FROM(sip->sip_to),
				   SIPTAG_TO(sip->sip_from),
				   TAG_END());
365 366 367 368 369
  if (!ag->ag_bob_leg ||
      !nta_leg_tag(ag->ag_bob_leg, NULL) ||
      !nta_leg_get_tag(ag->ag_bob_leg) ||
      !nta_incoming_tag(irq, nta_leg_get_tag(ag->ag_bob_leg)))
    return 500;
Pekka Pessi's avatar
Pekka Pessi committed
370 371 372 373

  return 200;
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387
int new_leg_callback_180(agent_t *ag,
			 nta_leg_t *leg,
			 nta_incoming_t *irq,
			 sip_t const *sip)
{
  int status = new_leg_callback_200(ag, leg, irq, sip);
  if (status == 200) {
    ag->ag_irq = irq;
    status = 180;
  }
  return status;
}


388
static client_check_f client_check_to_tag;
Pekka Pessi's avatar
Pekka Pessi committed
389

390 391 392 393 394 395 396 397
static client_check_f * const default_checks[] = {
  client_check_to_tag,
  NULL
};

static client_check_f * const no_default_checks[] = {
  NULL
};
398

399
/** Callback from client transaction */
400
int outgoing_callback(client_t *ctx,
Pekka Pessi's avatar
Pekka Pessi committed
401 402 403
		      nta_outgoing_t *orq,
		      sip_t const *sip)
{
404 405 406
  agent_t *ag = ctx->c_ag;
  int status = nta_outgoing_status(orq);
  client_check_f * const *checks;
Pekka Pessi's avatar
Pekka Pessi committed
407 408

  if (tstflags & tst_verbatim) {
409
    if (sip)
410 411 412
      printf("%s: %s: response %s %03d %s\n", name, ctx->c_name,
	     sip->sip_status->st_version,
	     sip->sip_status->st_status,
413 414 415 416
	     sip->sip_status->st_phrase);
    else
      printf("%s: %s: callback %03d\n", name, ctx->c_name,
	     status);
Pekka Pessi's avatar
Pekka Pessi committed
417 418
  }

419 420 421 422
  if (status >= 200 && ag->ag_comp) {		/* XXX */
    nta_compartment_decref(&ag->ag_client_compartment);
    ag->ag_client_compartment = nta_outgoing_compartment(ctx->c_orq);
  }
Pekka Pessi's avatar
Pekka Pessi committed
423

424 425 426 427
  if (status > ctx->c_status)
    ctx->c_status = status;
  if (status >= 200)
    ctx->c_final = 1;
Pekka Pessi's avatar
Pekka Pessi committed
428

429 430
  if (ctx->c_check && ctx->c_check(ctx, orq, sip))
    ctx->c_errors++;
Pekka Pessi's avatar
Pekka Pessi committed
431

432
  checks = ctx->c_checks;
Pekka Pessi's avatar
Pekka Pessi committed
433

434 435 436
  for (checks = checks ? checks : default_checks; *checks; checks++)
    if ((*checks)(ctx, ctx->c_orq, sip))
      ctx->c_errors++;
Pekka Pessi's avatar
Pekka Pessi committed
437

438 439 440 441
  return 0;
}

/** Deinit client. Return nonzero if client checks failed. */
442
static
443 444 445
int client_deinit(client_t *c)
{
  int errors = c->c_errors;
Pekka Pessi's avatar
Pekka Pessi committed
446

447 448
  if (c->c_deinit && c->c_deinit(c))
    errors++;
Pekka Pessi's avatar
Pekka Pessi committed
449

450
  if (c->c_orq) nta_outgoing_destroy(c->c_orq), c->c_orq = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
451

452
  c->c_errors = 0;
453 454 455
  c->c_status = 0;

  return errors;
Pekka Pessi's avatar
Pekka Pessi committed
456 457
}

458

459
static
460
void nta_test_run(agent_t *ag)
Pekka Pessi's avatar
Pekka Pessi committed
461
{
462 463 464 465 466
  for (ag->ag_running = 1; ag->ag_running;) {
    if (tstflags & tst_verbatim) {
      fputs(".", stdout); fflush(stdout);
    }
    su_root_step(ag->ag_root, 500L);
Pekka Pessi's avatar
Pekka Pessi committed
467 468 469
  }
}

470 471

/** Run client test. Return nonzero if client checks failed. */
472
static
473
int client_run_with(client_t *c, int expected, void (*runner)(client_t *c))
Pekka Pessi's avatar
Pekka Pessi committed
474
{
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
  int resulting;

  TEST_1(c->c_orq != NULL);

  runner(c);

  resulting = c->c_status;

  if (client_deinit(c))
    return 1;

  if (expected)
    TEST(resulting, expected);

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
490 491
}

492
static
493
void until_final_received(client_t *c)
Pekka Pessi's avatar
Pekka Pessi committed
494
{
495
  for (c->c_final = 0; !c->c_final; ) {
Pekka Pessi's avatar
Pekka Pessi committed
496 497 498
    if (tstflags & tst_verbatim) {
      fputs(".", stdout); fflush(stdout);
    }
499
    su_root_step(c->c_ag->ag_root, 500L);
Pekka Pessi's avatar
Pekka Pessi committed
500 501 502
  }
}

503
static
504 505 506 507 508 509 510
int client_run(client_t *c, int expected)
{
  return client_run_with(c, expected, until_final_received);
}

static
void until_server_acked(client_t *c)
511
{
512 513
  agent_t *ag = c->c_ag;

514 515 516 517 518 519 520 521
  for (ag->ag_acked = 0; !ag->ag_acked;) {
    if (tstflags & tst_verbatim) {
      fputs(".", stdout); fflush(stdout);
    }
    su_root_step(ag->ag_root, 500L);
  }
}

522
static
523 524 525 526 527
int client_run_until_acked(client_t *c, int expected)
{
  return client_run_with(c, expected, until_server_acked);
}

528
void
529
until_server_canceled(client_t *c)
530
{
531 532
  agent_t *ag = c->c_ag;

533 534 535 536 537 538 539 540
  for (ag->ag_canceled = 0; !ag->ag_canceled;) {
    if (tstflags & tst_verbatim) {
      fputs(".", stdout); fflush(stdout);
    }
    su_root_step(ag->ag_root, 500L);
  }
}

541
static
542 543 544 545 546 547
int client_run_until_canceled(client_t *c, int expected)
{
  return client_run_with(c, expected, until_server_canceled);
}


548
#include <sofia-sip/msg_mclass.h>
Pekka Pessi's avatar
Pekka Pessi committed
549 550 551 552 553

int test_init(agent_t *ag, char const *resolv_conf)
{
  char const *contact = "sip:*:*;comp=sigcomp";
  su_sockaddr_t su;
554
  socklen_t sulen, sulen0;
555
  su_socket_t s;
556
  int af, err = -1;
Pekka Pessi's avatar
Pekka Pessi committed
557 558 559

  BEGIN();

560 561
  ag->ag_root = su_root_create(ag);
  TEST_1(ag->ag_root);
Pekka Pessi's avatar
Pekka Pessi committed
562

563 564
  ag->ag_mclass = msg_mclass_clone(sip_default_mclass(), 0, 0);
  TEST_1(ag->ag_mclass);
Pekka Pessi's avatar
Pekka Pessi committed
565

566
#if SU_HAVE_IN6
567
  if (su_strmatch(getenv("ipv6"), "true")) {
568
    contact = "sip:[::]:*;comp=sigcomp";
Pekka Pessi's avatar
Pekka Pessi committed
569 570 571 572 573 574
    af = AF_INET6, sulen0 = sizeof (struct sockaddr_in6);
  }
  else {
    af = AF_INET, sulen0 = sizeof (struct sockaddr_in);
    contact = "sip:0.0.0.0:*;comp=sigcomp";
  }
575 576 577 578
#else
  af = AF_INET, sulen0 = sizeof (struct sockaddr_in);
  contact = "sip:0.0.0.0:*;comp=sigcomp";
#endif
Pekka Pessi's avatar
Pekka Pessi committed
579 580 581 582 583 584 585

  if (ag->ag_m)
    contact = ag->ag_m;
  else if (getenv("SIPCONTACT"))
    contact = getenv("SIPCONTACT");

  /* Sink server */
586
  s = su_socket(af, SOCK_DGRAM, 0); TEST_1(s != INVALID_SOCKET);
Pekka Pessi's avatar
Pekka Pessi committed
587 588 589 590 591
  memset(&su, 0, sulen = sulen0);
  su.su_family = af;
  if (getenv("sink")) {
    su.su_port = htons(atoi(getenv("sink")));
  }
592
  TEST_1(bind(s, &su.su_sa, sulen) < 0 ? (perror("bind"), 0) : 1);
Pekka Pessi's avatar
Pekka Pessi committed
593 594 595
  TEST_1(getsockname(s, &su.su_sa, &sulen) == 0);

  ag->ag_sink_socket = s;
596 597 598 599
  su_wait_init(ag->ag_sink_wait);
  su_wait_create(ag->ag_sink_wait, ag->ag_sink_socket, SU_WAIT_IN);

  ag->ag_sink_port = su_sprintf(ag->ag_home, "%u", ntohs(su.su_sin.sin_port));
Pekka Pessi's avatar
Pekka Pessi committed
600 601

  /* Down server */
602
  s = su_socket(af, SOCK_STREAM, 0); TEST_1(s != INVALID_SOCKET);
Pekka Pessi's avatar
Pekka Pessi committed
603 604 605 606 607
  memset(&su, 0, sulen = sulen0);
  su.su_family = af;
  if (getenv("down")) {
    su.su_port = htons(atoi(getenv("down")));
  }
608
  TEST_1(bind(s, &su.su_sa, sulen) < 0 ? (perror("bind"), 0) : 1);
Pekka Pessi's avatar
Pekka Pessi committed
609
  ag->ag_down_socket = s;
610

Pekka Pessi's avatar
Pekka Pessi committed
611
  /* Create agent */
612
  ag->ag_agent = nta_agent_create(ag->ag_root,
613 614 615 616 617 618 619 620 621 622
				  (url_string_t *)contact,
				  NULL,
				  NULL,
				  NTATAG_MCLASS(ag->ag_mclass),
				  NTATAG_USE_TIMESTAMP(1),
				  SRESTAG_RESOLV_CONF(resolv_conf),
				  NTATAG_USE_NAPTR(0),
				  NTATAG_USE_SRV(0),
				  NTATAG_PRELOAD(2048),
				  TAG_END());
623
  TEST_1(ag->ag_agent);
Pekka Pessi's avatar
Pekka Pessi committed
624 625 626 627 628 629 630

  {
    /* Initialize our headers */
    sip_from_t from[1];
    sip_to_t to[1];
    sip_contact_t m[1];

631 632
    su_sockaddr_t *su = ag->ag_su_nta;

Pekka Pessi's avatar
Pekka Pessi committed
633 634 635 636
    sip_from_init(from);
    sip_to_init(to);
    sip_contact_init(m);

637

Pekka Pessi's avatar
Pekka Pessi committed
638 639 640
    TEST_1(ag->ag_contact = nta_agent_contact(ag->ag_agent));

    *m->m_url = *ag->ag_contact->m_url;
641 642

    if (host_is_ip4_address(m->m_url->url_host)) {
643 644 645
      su_inet_pton(su->su_family = AF_INET,
		   m->m_url->url_host,
		   &su->su_sin.sin_addr);
646 647 648 649
      ag->ag_su_nta_len = (sizeof su->su_sin);
    }
    else {
      TEST_1(host_is_ip_address(m->m_url->url_host));
650 651 652
      su_inet_pton(su->su_family = AF_INET6,
		   m->m_url->url_host,
		   &su->su_sin6.sin6_addr);
653 654 655 656 657 658 659 660 661 662
      ag->ag_su_nta_len = (sizeof su->su_sin6);
    }

    su->su_port = htons(5060);
    if (m->m_url->url_port && strlen(m->m_url->url_port)) {
      unsigned long port = strtoul(m->m_url->url_port, NULL, 10);
      su->su_port = htons(port);
    }
    TEST_1(su->su_port != 0);

Pekka Pessi's avatar
Pekka Pessi committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
    m->m_url->url_user = "bob";
    TEST_1(ag->ag_m_bob = sip_contact_dup(ag->ag_home, m));

    to->a_display = "Bob";
    *to->a_url = *ag->ag_contact->m_url;
    to->a_url->url_user = "bob";
    to->a_url->url_port = NULL;
    TEST_1(ag->ag_bob = sip_to_dup(ag->ag_home, to));

    *m->m_url = *ag->ag_contact->m_url;
    m->m_url->url_user = "alice";
    TEST_1(ag->ag_m_alice = sip_contact_dup(ag->ag_home, m));

    from->a_display = "Alice";
    *from->a_url = *ag->ag_contact->m_url;
    from->a_url->url_user = "alice";
    from->a_url->url_port = NULL;
    TEST_1(ag->ag_alice = sip_from_dup(ag->ag_home, from));
  }
  {
683
    char const data[] =
Pekka Pessi's avatar
Pekka Pessi committed
684 685 686 687 688 689 690 691 692 693 694 695 696
      "v=0\r\n"
      "o=- 425432 423412 IN IP4 127.0.0.1\r\n"
      "s= \r\n"
      "c=IN IP4 127.0.0.1\r\n"
      "m=5004 audio 8 0\r\n";

    ag->ag_content_type = sip_content_type_make(ag->ag_home, "application/sdp");
    ag->ag_payload = sip_payload_make(ag->ag_home, data);
  }

  {
    sip_contact_t *m;

697
    ag->ag_aliases =
Pekka Pessi's avatar
Pekka Pessi committed
698 699 700 701
      sip_contact_make(ag->ag_home, "sip:127.0.0.1, sip:localhost, sip:[::1]");
    TEST_1(ag->ag_aliases);
    TEST_1(ag->ag_aliases->m_next);
    TEST_1(ag->ag_aliases->m_next->m_next);
702
    TEST_P(ag->ag_aliases->m_next->m_next->m_next, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
703 704 705 706 707 708 709 710 711

    for (m = ag->ag_aliases; m; m = m->m_next)
      m->m_url->url_port = ag->ag_contact->m_url->url_port;

    TEST_1(m = sip_contact_dup(ag->ag_home, ag->ag_contact));

    m->m_next = ag->ag_aliases;
    ag->ag_aliases = m;

712
    err = nta_agent_set_params(ag->ag_agent,
713 714
			       NTATAG_ALIASES(ag->ag_aliases),
			       NTATAG_REL100(1),
715 716
			       NTATAG_UA(1),
			       NTATAG_MERGE_482(1),
717 718 719 720
			       NTATAG_USE_NAPTR(1),
			       NTATAG_USE_SRV(1),
			       NTATAG_MAX_FORWARDS(20),
			       TAG_END());
721
    TEST(err, 7);
Pekka Pessi's avatar
Pekka Pessi committed
722

723
    err = nta_agent_set_params(ag->ag_agent,
724 725 726
			       NTATAG_ALIASES(ag->ag_aliases),
			       NTATAG_DEFAULT_PROXY("sip:127.0.0.1"),
			       TAG_END());
727
    TEST(err, 2);
Pekka Pessi's avatar
Pekka Pessi committed
728

729
    err = nta_agent_set_params(ag->ag_agent,
730 731 732
			       NTATAG_ALIASES(ag->ag_aliases),
			       NTATAG_DEFAULT_PROXY(NULL),
			       TAG_END());
733
    TEST(err, 2);
Pekka Pessi's avatar
Pekka Pessi committed
734

735
    err = nta_agent_set_params(ag->ag_agent,
736 737
			       NTATAG_DEFAULT_PROXY("tel:+35878008000"),
			       TAG_END());
738
    TEST(err, -1);
Pekka Pessi's avatar
Pekka Pessi committed
739

Pekka Pessi's avatar
Pekka Pessi committed
740
  }
741

Pekka Pessi's avatar
Pekka Pessi committed
742 743 744 745 746 747
  {
    url_t url[1];

    /* Create the server leg */
    *url = *ag->ag_aliases->m_url;
    url->url_user = "%";
748
    ag->ag_server_leg = nta_leg_tcreate(ag->ag_agent,
749 750 751 752 753
					leg_callback_200,
					ag,
					NTATAG_NO_DIALOG(1),
					URLTAG_URL(url),
					TAG_END());
754
    TEST_1(ag->ag_server_leg);
Pekka Pessi's avatar
Pekka Pessi committed
755 756 757
  }

  END();
758
}
Pekka Pessi's avatar
Pekka Pessi committed
759

760 761 762 763 764
int test_reinit(agent_t *ag)
{
  BEGIN();
  /* Create a new default leg */
  nta_leg_destroy(ag->ag_default_leg), ag->ag_default_leg = NULL;
765
  TEST_1(ag->ag_default_leg = nta_leg_tcreate(ag->ag_agent,
766 767 768 769
					      leg_callback_200,
					      ag,
					      NTATAG_NO_DIALOG(1),
					      TAG_END()));
770 771 772
  END();
}

Pekka Pessi's avatar
Pekka Pessi committed
773 774 775 776
int test_deinit(agent_t *ag)
{
  BEGIN();

777 778 779 780
  if (ag->ag_request) msg_destroy(ag->ag_request), ag->ag_request = NULL;

  su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;

Pekka Pessi's avatar
Pekka Pessi committed
781 782 783 784 785 786 787 788
  nta_leg_destroy(ag->ag_alice_leg);
  nta_leg_destroy(ag->ag_bob_leg);
  nta_leg_destroy(ag->ag_default_leg);
  nta_leg_destroy(ag->ag_server_leg);

  nta_agent_destroy(ag->ag_agent);
  su_root_destroy(ag->ag_root);

789 790 791 792 793
  if (ag->ag_sink_port) {
    su_free(ag->ag_home, (void *)ag->ag_sink_port), ag->ag_sink_port = NULL;
    su_wait_destroy(ag->ag_sink_wait);
    su_close(ag->ag_sink_socket);
  }
794 795 796

  free(ag->ag_mclass), ag->ag_mclass = NULL;

Pekka Pessi's avatar
Pekka Pessi committed
797
  END();
798
}
Pekka Pessi's avatar
Pekka Pessi committed
799

800 801 802 803 804 805
static
int readfile(FILE *f, void **contents)
{
  /* Read in whole (binary!) file */
  char *buffer = NULL;
  long size;
Pekka Pessi's avatar
Pekka Pessi committed
806
  size_t len;
807

808 809 810 811
  /* Read whole file in */
  if (fseek(f, 0, SEEK_END) < 0 ||
      (size = ftell(f)) < 0 ||
      fseek(f, 0, SEEK_SET) < 0 ||
Pekka Pessi's avatar
Pekka Pessi committed
812
      (long)(len = (size_t)size) != size) {
813
    fprintf(stderr, "%s: unable to determine file size (%s)\n",
814 815 816 817 818
	    __func__, strerror(errno));
    return -1;
  }

  if (!(buffer = malloc(len + 2)) ||
Pekka Pessi's avatar
Pekka Pessi committed
819
      fread(buffer, 1, len, f) != len) {
820 821 822 823 824 825 826 827 828 829
    fprintf(stderr, "%s: unable to read file (%s)\n", __func__, strerror(errno));
    if (buffer)
      free(buffer);
    return -1;
  }

  buffer[len] = '\0';

  *contents = buffer;

Michael Jerris's avatar
Michael Jerris committed
830
  return (int)len;
831 832
}

833
#if HAVE_DIRENT_H
834
#include <dirent.h>
835
#endif
836 837 838

static int test_bad_messages(agent_t *ag)
{
839 840
  BEGIN();

841
#if HAVE_DIRENT_H
842
  DIR *dir;
843
  struct dirent *d;
844 845
  char name[PATH_MAX + 1] = "../sip/tests/";
  size_t offset;
846 847 848 849 850
  char const *host, *port;
  su_addrinfo_t *ai,  hints[1];
  su_socket_t s;
  su_sockaddr_t su[1];
  socklen_t sulen;
851
  char via[64];
852 853 854
  size_t vlen;
  int i;

855 856 857 858 859 860 861 862
  dir = opendir(name);
  if (dir == NULL && getenv("srcdir")) {
    strncpy(name, getenv("srcdir"), PATH_MAX);
    strncat(name, "/../sip/tests/", PATH_MAX);
    dir = opendir(name);
  }

  if (dir == NULL) {
863 864
    fprintf(stderr, "test_nta: cannot find sip torture messages\n");
    fprintf(stderr, "test_nta: tried %s\n", name);
865 866 867
  }

  offset = strlen(name);
868

869
  TEST_1(ag->ag_default_leg = nta_leg_tcreate(ag->ag_agent,
870 871 872 873 874
					      leg_callback_500,
					      ag,
					      NTATAG_NO_DIALOG(1),
					      TAG_END()));

875 876 877 878 879 880 881
  host = ag->ag_contact->m_url->url_host;
  if (host_is_ip6_reference(host)) {
    host = strcpy(via, host + 1);
    via[strlen(via) - 1] = '\0';
  }
  port = url_port(ag->ag_contact->m_url);

882 883 884
  memset(hints, 0, sizeof hints);
  hints->ai_socktype = SOCK_DGRAM;
  hints->ai_protocol = IPPROTO_UDP;
885

886
  TEST(su_getaddrinfo(host, port, hints, &ai), 0); TEST_1(ai);
887
  s = su_socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); TEST_1(s != -1);
888
  memset(su, 0, sulen = ai->ai_addrlen);
889 890 891 892 893 894
  su->su_len = sizeof su; su->su_family = ai->ai_family;
  TEST_1(bind(s, &su->su_sa, sulen) == 0);
  TEST_1(getsockname(s, &su->su_sa, &sulen) == 0);
  sprintf(via, "v: SIP/2.0/UDP is.invalid:%u\r\n", ntohs(su->su_port));
  vlen = strlen(via);

895
  for (d = dir ? readdir(dir) : NULL; d; d = readdir(dir)) {
896 897 898 899 900 901 902 903 904 905 906 907
    size_t len = strlen(d->d_name);
    FILE *f;
    int blen, n;
    void *buffer; char *r;

    if (len < strlen(".txt"))
      continue;
    if (strcmp(d->d_name + len - strlen(".txt"), ".txt"))
      continue;
    strncpy(name + offset, d->d_name, PATH_MAX - offset);
    TEST_1(f = fopen(name, "rb"));
    TEST_1((blen = readfile(f, &buffer)) > 0);
908
    fclose(f);
909 910 911
    r = buffer;

    if (strncmp(r, "JUNK ", 5) == 0) {
912
      TEST_SIZE(su_sendto(s, r, blen, 0, ai->ai_addr, ai->ai_addrlen), blen);
913 914 915 916 917 918 919
    }
    else if (strncmp(r, "INVITE ", 7) != 0) {
      su_iovec_t vec[3];
      n = strcspn(r, "\r\n"); n += strspn(r + n, "\r\n");
      vec[0].siv_base = r, vec[0].siv_len = n;
      vec[1].siv_base = via, vec[1].siv_len = vlen;
      vec[2].siv_base = r + n, vec[2].siv_len = blen - n;
920 921
      TEST_SIZE(su_vsend(s, vec, 3, 0, (void *)ai->ai_addr, ai->ai_addrlen),
		blen + vlen);
922 923 924 925 926
    }
    free(buffer);
    su_root_step(ag->ag_root, 1);
  }

927 928 929 930
  TEST_SIZE(su_sendto(s, "\r\n\r\n", 4, 0, (void *)ai->ai_addr, ai->ai_addrlen), 4);

  su_root_step(ag->ag_root, 1);

931 932
  TEST_SIZE(su_sendto(s, "", 0, 0, ai->ai_addr, ai->ai_addrlen), 0);

933 934 935 936 937
  su_close(s);

  for (i = 0; i < 20; i++)
    su_root_step(ag->ag_root, 1);

938
  nta_leg_destroy(ag->ag_default_leg), ag->ag_default_leg = NULL;
939

940 941
  if (dir)
    closedir(dir);
Pekka Pessi's avatar
Pekka Pessi committed
942

943
#endif /* HAVE_DIRENT_H */
944 945

  END();
946
}
Pekka Pessi's avatar
Pekka Pessi committed
947

948
static unsigned char const code[] =
Pekka Pessi's avatar
Pekka Pessi committed
949 950
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

951
#include <sofia-sip/su_uniqueid.h>
Pekka Pessi's avatar
Pekka Pessi committed
952

Pekka Pessi's avatar
Pekka Pessi committed
953
sip_payload_t *test_payload(su_home_t *home, size_t size)
Pekka Pessi's avatar
Pekka Pessi committed
954
{
Pekka Pessi's avatar
Pekka Pessi committed
955
  sip_payload_t *pl = sip_payload_create(home, NULL, (isize_t)size);
Pekka Pessi's avatar
Pekka Pessi committed
956 957

  if (pl) {
958
    size_t i;
Pekka Pessi's avatar
Pekka Pessi committed
959
    char *data = (char *)pl->pl_data;
960

Pekka Pessi's avatar
Pekka Pessi committed
961 962 963 964 965 966 967 968 969 970 971
    for (i = 0; i < size; i++) {
      if ((i & 63) != 63)
	data[i] = code[su_randint(0, 63)];
      else
	data[i] = '\n';
    }
  }

  return pl;
}

972
static
973 974
int client_check_to_tag(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
{
975
  if (sip)
976 977 978 979
    TEST_1(sip->sip_to && sip->sip_to->a_tag);
  return 0;
}

980
static
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 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
int check_magic_branch(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
{
  if (sip) {
    TEST_1(sip->sip_via);
    TEST_S(sip->sip_via->v_branch, "MagicalBranch");
  }
  return 0;
}

static
int check_via_with_sigcomp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
{
  if (sip && sip->sip_via) {
    TEST_S(sip->sip_via->v_comp, "sigcomp");
  }
  return 0;
}

static
int check_via_without_sigcomp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
{
  if (sip && sip->sip_via) {
    TEST_1(sip->sip_via->v_comp == NULL);
  }
  return 0;
}

static
int check_via_with_tcp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
{
  if (sip && sip->sip_via) {
    TEST_S(sip->sip_via->v_protocol, "SIP/2.0/TCP");
  }
  return 0;
}

static
int check_via_with_sctp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
{
  if (sip && sip->sip_via) {
    TEST_S(sip->sip_via->v_protocol, "SIP/2.0/SCTP");
  }
  return 0;
}

static
int check_via_with_udp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
{
  if (sip && sip->sip_via) {
    TEST_S(sip->sip_via->v_protocol, "SIP/2.0/UDP");
  }
  return 0;
}

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
static
int save_and_check_tcp(client_t *ctx, nta_outgoing_t *orq, sip_t const *sip)
{
  if (ctx->c_status >= 200 && ctx->c_extra) {
    tport_t *tport = nta_outgoing_transport(orq);
    TEST_1(tport);
    *(tport_t **)ctx->c_extra = tport;
  }

  return check_via_with_tcp(ctx, orq, sip);
}

1047 1048 1049



Pekka Pessi's avatar
Pekka Pessi committed
1050 1051 1052 1053 1054 1055 1056 1057
/* Test transports */

int test_tports(agent_t *ag)
{
  int udp = 0, tcp = 0, sctp = 0, tls = 0;
  sip_via_t const *v, *v_udp_only = NULL;
  char const *udp_comp = NULL;
  char const *tcp_comp = NULL;
1058
  tport_t *tcp_tport = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1059 1060 1061 1062 1063

  url_t url[1];

  BEGIN();

1064 1065
  nta_leg_bind(ag->ag_server_leg, leg_callback_200, ag);

Pekka Pessi's avatar
Pekka Pessi committed
1066 1067
  *url = *ag->ag_contact->m_url;
  url->url_port = "*";
1068 1069
  url->url_params = "transport=tcp";

Pekka Pessi's avatar
Pekka Pessi committed
1070 1071
  url->url_params = "transport=udp";

1072
  TEST_1(nta_agent_add_tport(ag->ag_agent, (url_string_t *)url,
Pekka Pessi's avatar
Pekka Pessi committed
1073 1074 1075 1076 1077
			     TAG_END()) == 0);

  TEST_1(v = nta_agent_via(ag->ag_agent));

  for (; v; v = v->v_next) {
1078
    if (su_casematch(v->v_protocol, sip_transport_udp)) {
Pekka Pessi's avatar
Pekka Pessi committed
1079 1080 1081 1082
      if (udp)
	v_udp_only = v;
      udp = 1;
      if (udp_comp == NULL)
1083
	udp_comp = v->v_comp;
Pekka Pessi's avatar
Pekka Pessi committed
1084
    }
1085
    else if (su_casematch(v->v_protocol, sip_transport_tcp)) {
Pekka Pessi's avatar
Pekka Pessi committed
1086 1087
      tcp = 1;
      if (tcp_comp == NULL)
1088
	tcp_comp = v->v_comp;
Pekka Pessi's avatar
Pekka Pessi committed
1089
    }
1090
    else if (su_casematch(v->v_protocol, sip_transport_sctp)) {
Pekka Pessi's avatar
Pekka Pessi committed
1091 1092
      sctp = 1;
    }
1093
    else if (su_casematch(v->v_protocol, sip_transport_tls)) {
Pekka Pessi's avatar
Pekka Pessi committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
      tls = 1;
    }
  }

  *url = *ag->ag_aliases->m_url;
  url->url_user = "bob";

  if (udp_comp || tcp_comp)
    ag->ag_comp = "sigcomp";

  {
    /* Test 0.1
1106
     * Send a message from default leg to default leg
Pekka Pessi's avatar
Pekka Pessi committed
1107 1108
     */
    char const p_acid[] = "P-Access-Network-Info: IEEE-802.11g\n";
Pekka Pessi's avatar
Pekka Pessi committed
1109
    url_t url[1];
1110
    client_t ctx[1] = {{ ag, "Test 0.1", check_via_without_sigcomp }};
1111

Pekka Pessi's avatar
Pekka Pessi committed
1112 1113
    *url = *ag->ag_contact->m_url;
    url->url_params = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1114
    ag->ag_expect_leg = ag->ag_default_leg;
Pekka Pessi's avatar
Pekka Pessi committed
1115

1116 1117
    ctx->c_orq =
      nta_outgoing_tcreate(ag->ag_default_leg,
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
			   outgoing_callback, ctx,
			   ag->ag_obp,
			   SIP_METHOD_MESSAGE,
			   (url_string_t *)url,
			   SIPTAG_SUBJECT_STR(ctx->c_name),
			   SIPTAG_FROM(ag->ag_alice),
			   SIPTAG_TO(ag->ag_bob),
			   SIPTAG_CONTACT(ag->ag_m_alice),
			   SIPTAG_HEADER_STR(p_acid),
			   TAG_END());

    TEST_1(!client_run(ctx, 200));

1131
    TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
1132

1133
    TEST_1(ag->ag_request);
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
    msg_destroy(ag->ag_request), ag->ag_request = NULL;

    nta_leg_bind(ag->ag_default_leg, leg_callback_200, ag);
  }

  {
    /* Test 0.1.2: test url_headers
     *
     * Send a message from default leg to default leg.
     */
    url_t url[1];
    sip_t *sip;
1146
    client_t ctx[1] = {{ ag, "Test 0.1.2", check_via_without_sigcomp }};
1147

1148 1149 1150 1151 1152 1153
    *url = *ag->ag_contact->m_url;
    /* Test that method parameter is stripped and headers in query are used */
    url->url_params = "method=MESSAGE;user=IP";
    url->url_headers = "organization=United%20Testers";
    ag->ag_expect_leg = ag->ag_default_leg;

1154 1155
    ctx->c_orq =
      nta_outgoing_tcreate(ag->ag_default_leg,
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
			   outgoing_callback, ctx,
			   ag->ag_obp,
			   SIP_METHOD_MESSAGE,
			   (url_string_t *)url,
			   SIPTAG_SUBJECT_STR(ctx->c_name),
			   SIPTAG_FROM(ag->ag_alice),
			   SIPTAG_TO(ag->ag_bob),
			   SIPTAG_CONTACT(ag->ag_m_alice),
			   TAG_END());
    TEST_1(!client_run(ctx, 200));

1167
    TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
1168 1169 1170 1171 1172 1173
    TEST_1(ag->ag_request);
    TEST_1(sip = sip_object(ag->ag_request));

    TEST_1(sip->sip_organization);
    TEST_S(sip->sip_organization->g_string, "United Testers");
    TEST_S(sip->sip_request->rq_url->url_params, "user=IP");
1174

1175
    nta_leg_bind(ag->ag_default_leg, leg_callback_200, ag);
Pekka Pessi's avatar
Pekka Pessi committed
1176 1177
  }

1178
  /* Test 0.1.3
Pekka Pessi's avatar
Pekka Pessi committed
1179 1180 1181 1182 1183
   * Send a message from Bob to Alice using SIGCOMP and TCP
   */
  if (tcp_comp) {
    url_t url[1];
    sip_payload_t *pl;
Pekka Pessi's avatar
Pekka Pessi committed
1184
    size_t size = 1024;
1185
    client_t ctx[1] = {{ ag, "Test 0.1.3", check_via_with_sigcomp }};
Pekka Pessi's avatar
Pekka Pessi committed
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

    *url = *ag->ag_aliases->m_url;
    url->url_user = "alice";
    if (url->url_params)
      url->url_params = su_sprintf(NULL, "%s;transport=tcp", url->url_params);
    else
      url->url_params = "transport=tcp";

    TEST_1(pl = test_payload(ag->ag_home, size));

    ag->ag_expect_leg = ag->ag_server_leg;
Pekka Pessi's avatar
Pekka Pessi committed
1197

1198
    ctx->c_orq =
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
      nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
			   ag->ag_obp,
			   SIP_METHOD_MESSAGE,
			   (url_string_t *)url,
			   NTATAG_COMP("sigcomp"),
			   SIPTAG_SUBJECT_STR(ctx->c_name),
			   SIPTAG_FROM(ag->ag_bob),
			   SIPTAG_TO(ag->ag_alice),
			   SIPTAG_CONTACT(ag->ag_m_bob),
			   SIPTAG_PAYLOAD(pl),
			   TAG_END());
1210 1211
    su_free(ag->ag_home, pl);

1212 1213
    TEST_1(!client_run(ctx, 200));

Pekka Pessi's avatar
Pekka Pessi committed
1214 1215
    TEST_1(ag->ag_client_compartment);
    nta_compartment_decref(&ag->ag_client_compartment);
1216
    TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
Pekka Pessi's avatar
Pekka Pessi committed
1217 1218 1219 1220
  }

  /* Test 0.2
   * Send a message from Bob to Alice
1221
   * This time specify a TCP URI, and include a large payload
Pekka Pessi's avatar
Pekka Pessi committed
1222 1223 1224
   * of 512 kB
   */
  if (tcp) {
1225
    client_t ctx[1] = {{ ag, "Test 0.2", save_and_check_tcp, }};
Pekka Pessi's avatar
Pekka Pessi committed
1226 1227
    url_t url[1];
    sip_payload_t *pl;
Pekka Pessi's avatar
Pekka Pessi committed
1228
    usize_t size = 512 * 1024;
Pekka Pessi's avatar
Pekka Pessi committed
1229

1230 1231
    ctx->c_extra = &tcp_tport;

Pekka Pessi's avatar
Pekka Pessi committed
1232 1233
    *url = *ag->ag_aliases->m_url;
    url->url_user = "alice";
Pekka Pessi's avatar
Pekka Pessi committed
1234
    url->url_params = "transport=tcp";
Pekka Pessi's avatar
Pekka Pessi committed
1235 1236 1237 1238

    TEST_1(pl = test_payload(ag->ag_home, size));

    ag->ag_expect_leg = ag->ag_server_leg;
1239
    ctx->c_orq =
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
      nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
			   NULL,
			   SIP_METHOD_MESSAGE,
			   (url_string_t *)url,
			   SIPTAG_SUBJECT_STR(ctx->c_name),
			   SIPTAG_FROM(ag->ag_bob),
			   SIPTAG_TO(ag->ag_alice),
			   SIPTAG_CONTACT(ag->ag_m_bob),
			   SIPTAG_PAYLOAD(pl),
			   NTATAG_DEFAULT_PROXY(ag->ag_obp),
			   TAG_END());
1251
    su_free(ag->ag_home, pl);
1252
    TEST_1(!client_run(ctx, 200));
1253 1254 1255 1256 1257 1258 1259
    TEST_1(tcp_tport);
    TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
  }

  if (tcp_tport) {
    /* Test 0.2.1 - always use transport connection from NTATAG_TPORT()
     *
1260
     * Test bug reported by geaaru
1261 1262 1263 1264 1265 1266
     * - NTATAG_TPORT() is not used if NTATAG_DEFAULT_PROXY() is given
     */
    client_t ctx[1] = {{ ag, "Test 0.2.1", save_and_check_tcp }};
    url_t url[1];
    sip_payload_t *pl;
    tport_t *used_tport = NULL;
1267

1268
    ctx->c_extra = &used_tport;
1269

1270 1271 1272
    *url = *ag->ag_aliases->m_url;
    url->url_user = "alice";

1273 1274 1275 1276 1277
    TEST(tport_shutdown(tcp_tport, 1), 0); /* Not going to send anymore */

    TEST_1(pl = test_payload(ag->ag_home, 512));

    ag->ag_expect_leg = ag->ag_server_leg;
1278
    ctx->c_orq =
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
      nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
			   NULL,
			   SIP_METHOD_MESSAGE,
			   (url_string_t *)url,
			   SIPTAG_SUBJECT_STR(ctx->c_name),
			   SIPTAG_FROM(ag->ag_bob),
			   SIPTAG_TO(ag->ag_alice),
			   SIPTAG_CONTACT(ag->ag_m_bob),
			   SIPTAG_PAYLOAD(pl),
			   NTATAG_DEFAULT_PROXY(ag->ag_obp),
			   NTATAG_TPORT(tcp_tport),
			   TAG_END());
    su_free(ag->ag_home, pl);
    TEST_1(!client_run(ctx, 503));
1293

1294
    TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313

    TEST_1(used_tport == tcp_tport);

    tport_unref(tcp_tport), tcp_tport = NULL;

    if (v_udp_only)		/* Prepare for next test */
      TEST_1(tcp_tport = tport_ref(tport_parent(used_tport)));
    tport_unref(used_tport);
  }

  if (tcp_tport) {
    /* test 0.2.2 - select transport protocol using NTATAG_TPORT()
     *
     * Use primary NTATAG_TPORT() to select transport
     */
    client_t ctx[1] = {{ ag, "Test 0.2.2", save_and_check_tcp }};
    url_t url[1];
    sip_payload_t *pl;
    tport_t *used_tport = NULL;
1314

1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
    ctx->c_extra = &used_tport;
    TEST_1(tport_is_primary(tcp_tport));

    TEST_1(pl = test_payload(ag->ag_home, 512));

    *url = *ag->ag_aliases->m_url;
    url->url_user = "alice";
    url->url_host = v_udp_only->v_host;
    url->url_port = v_udp_only->v_port;
    url->url_params = NULL;	/* No sigcomp */

    ag->ag_expect_leg = ag->ag_server_leg;
1327
    ctx->c_orq =
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
      nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
			   (url_string_t *)url,
			   SIP_METHOD_MESSAGE,
			   (url_string_t *)url,
			   SIPTAG_SUBJECT_STR(ctx->c_name),
			   SIPTAG_FROM(ag->ag_bob),
			   SIPTAG_TO(ag->ag_alice),
			   SIPTAG_CONTACT(ag->ag_m_bob),
			   SIPTAG_PAYLOAD(pl),
			   NTATAG_TPORT(tcp_tport),
			   TAG_END());
    su_free(ag->ag_home, pl);
    TEST_1(!client_run(ctx, 503));

    TEST_P(ag->ag_latest_leg, ag->ag_server_leg);

    TEST_1(used_tport);
    TEST_1(tport_is_tcp(used_tport));
    tport_unref(used_tport);
    tport_unref(tcp_tport), tcp_tport = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1348 1349 1350 1351 1352 1353 1354
  }

  /* Test 0.3
   * Send a message from Bob to Alice
   * This time include a large payload of 512 kB, let NTA choose transport.
   */
  if (tcp) {
1355
    client_t ctx[1] = {{ ag, "Test 0.3" }};
Pekka Pessi's avatar
Pekka Pessi committed
1356 1357
    url_t url[1];
    sip_payload_t *pl;
Pekka Pessi's avatar
Pekka Pessi committed
1358
    usize_t size = 512 * 1024;
Pekka Pessi's avatar
Pekka Pessi committed
1359 1360 1361 1362 1363 1364 1365

    *url = *ag->ag_aliases->m_url;
    url->url_user = "alice";

    TEST_1(pl = test_payload(ag->ag_home, size));

    ag->ag_expect_leg = ag->ag_server_leg;
1366
    ctx->c_orq =
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
      nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
			   ag->ag_obp,
			   SIP_METHOD_MESSAGE,
			   (url_string_t *)url,
			   SIPTAG_SUBJECT_STR(ctx->c_name),
			   SIPTAG_FROM(ag->ag_bob),
			   SIPTAG_TO(ag->ag_alice),
			   SIPTAG_CONTACT(ag->ag_m_bob),
			   SIPTAG_PAYLOAD(pl),
			   TAG_END());
1377
    su_free(ag->ag_home, pl);
1378
    TEST_1(!client_run(ctx, 200));
1379
    TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
Pekka Pessi's avatar
Pekka Pessi committed
1380 1381 1382 1383 1384 1385 1386
  }

  /* Test 0.4.1:
   * Send a message from Bob to Alice
   * This time include a payload of 2 kB, let NTA choose transport.
   */
  {
1387
    client_t ctx[1] = {{ ag, "Test 0.4.1", check_via_with_tcp }};
Pekka Pessi's avatar
Pekka Pessi committed
1388 1389
    url_t url[1];
    sip_payload_t *pl;
Pekka Pessi's avatar
Pekka Pessi committed
1390
    usize_t size = 2 * 1024;
Pekka Pessi's avatar
Pekka Pessi committed
1391 1392 1393 1394 1395 1396 1397

    *url = *ag->ag_aliases->m_url;
    url->url_user = "alice";

    TEST_1(pl = test_payload(ag->ag_home, size));

    ag->ag_expect_leg = ag->ag_server_leg;
1398
    ctx->c_orq =
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
      nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
			   ag->ag_obp,
			   SIP_METHOD_MESSAGE,
			   (url_string_t *)url,
			   SIPTAG_SUBJECT_STR(ctx->c_name),
			   SIPTAG_FROM(ag->ag_bob),
			   SIPTAG_TO(ag->ag_alice),
			   SIPTAG_CONTACT(ag->ag_m_bob),
			   SIPTAG_PAYLOAD(pl),
			   TAG_END());
1409 1410
    su_free(ag->ag_home, pl);

1411 1412
    TEST_1(!client_run(ctx, 200));

1413
    TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
1414
    su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1415 1416 1417 1418 1419 1420 1421
  }

  /* Test 0.4.2:
   * Send a message from Bob to Alices UDP-only address
   * This time include a payload of 2 kB, let NTA choose transport.
   */
  if (v_udp_only) {
1422
    client_t ctx[1] = {{ ag, "Test 0.4.2", check_via_with_udp }};
Pekka Pessi's avatar
Pekka Pessi committed
1423 1424
    url_t url[1];
    sip_payload_t *pl;
Pekka Pessi's avatar
Pekka Pessi committed
1425
    usize_t size = 2 * 1024;
Pekka Pessi's avatar
Pekka Pessi committed
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438

    *url = *ag->ag_aliases->m_url;
    url->url_user = "alice";
    url->url_host = v_udp_only->v_host;
    url->url_port = v_udp_only->v_port;
    url->url_params = NULL;	/* No sigcomp */

    TEST_1(pl = test_payload(ag->ag_home, size));

    ag->ag_expect_leg = ag->ag_default_leg;

    su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;

1439
    ctx->c_orq =
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
      nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
			   ag->ag_obp,
			   SIP_METHOD_MESSAGE,
			   (url_string_t *)url,
			   SIPTAG_SUBJECT_STR(ctx->c_name),
			   SIPTAG_FROM(ag->ag_bob),
			   SIPTAG_TO(ag->ag_alice),
			   SIPTAG_CONTACT(ag->ag_m_bob),
			   SIPTAG_PAYLOAD(pl),
			   TAG_END());
1450 1451
    su_free(ag->ag_home, pl);

1452 1453
    TEST_1(!client_run(ctx, 200));

1454
    TEST_P(ag->ag_latest_leg, ag->ag_default_leg);
1455

Pekka Pessi's avatar
Pekka Pessi committed
1456
    TEST_1(ag->ag_in_via);
1457
    TEST_1(su_casematch(ag->ag_in_via->v_protocol, "SIP/2.0/UDP"));
1458
    su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1459 1460 1461 1462 1463 1464 1465
  }

  /* Test 0.5:
   * Send a message from Bob to Alice
   * This time include a payload of 2 kB, try to use UDP.
   */
  if (udp) {
1466
    client_t ctx[1] = {{ ag, "Test 0.5", check_via_with_udp }};
Pekka Pessi's avatar
Pekka Pessi committed
1467 1468
    url_t url[1];
    sip_payload_t *pl;
Pekka Pessi's avatar
Pekka Pessi committed
1469
    usize_t size = 2 * 1024;
Pekka Pessi's avatar
Pekka Pessi committed
1470 1471 1472 1473 1474 1475 1476

    *url = *ag->ag_aliases->m_url;
    url->url_user = "alice";

    TEST_1(pl = test_payload(ag->ag_home, size));

    ag->ag_expect_leg = ag->ag_server_leg;
1477
    ctx->c_orq =
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
      nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ctx,
			   ag->ag_obp,
			   SIP_METHOD_MESSAGE,
			   (url_string_t *)url,
			   SIPTAG_SUBJECT_STR(ctx->c_name),
			   SIPTAG_FROM(ag->ag_bob),
			   SIPTAG_TO(ag->ag_alice),
			   SIPTAG_CONTACT(ag->ag_m_bob),
			   SIPTAG_PAYLOAD(pl),
			   TPTAG_MTU(0xffffffff),
			   TAG_END());
1489 1490
    su_free(ag->ag_home, pl);

1491 1492
    TEST_1(!client_run(ctx, 200));

1493
    TEST_P(ag->ag_latest_leg, ag->ag_server_leg);
Pekka Pessi's avatar
Pekka Pessi committed
1494 1495 1496
  }

  if (udp) {
1497
    /* Test 0.6