test_nta.c 76.7 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 38 39 40 41 42
 *
 * 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;

#define SU_ROOT_MAGIC_T      agent_t

#include <su_wait.h>

43 44
#include <msg_internal.h>

Pekka Pessi's avatar
Pekka Pessi committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#define NTA_AGENT_MAGIC_T    agent_t
#define NTA_LEG_MAGIC_T      agent_t
#define NTA_OUTGOING_MAGIC_T agent_t
#define NTA_INCOMING_MAGIC_T agent_t
#define NTA_RELIABLE_MAGIC_T agent_t

#include "nta.h"
#include "nta_internal.h"
#include <sip_header.h>
#include <sip_tag.h>
#include <sip_status.h>
#include <tport.h>
#include <htable.h>
#include <sresolv.h>
#include <su_log.h>
60
#include <sofia_sip_features.h>
Pekka Pessi's avatar
Pekka Pessi committed
61 62 63

#include "string0.h"

64 65 66 67 68 69 70
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <assert.h>

Pekka Pessi's avatar
Pekka Pessi committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
extern su_log_t nta_log[];
extern su_log_t tport_log[];

int tstflags = 0;
#define TSTFLAGS tstflags

#include <tstdef.h>

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

86 87
#define NONE ((void *)-1)

Pekka Pessi's avatar
Pekka Pessi committed
88 89
struct sigcomp_compartment;

90
char const name[] = "test_nta";
Pekka Pessi's avatar
Pekka Pessi committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

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;
  int             ag_status;

  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_leg_t      *ag_tag_remote; /**< If this is set, outgoing_callback()
				  *   tags it with the tag from remote.
				  */
  int             ag_tag_status; /**< Which response established dialog */
140
  msg_param_t     ag_call_tag;	 /**< Tag used to establish dialog */
Pekka Pessi's avatar
Pekka Pessi committed
141 142 143 144 145 146 147 148 149

  nta_reliable_t *ag_reliable;

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

  sip_content_type_t *ag_content_type;
  sip_payload_t  *ag_payload;

150 151
  msg_t          *ag_probe_msg;

Pekka Pessi's avatar
Pekka Pessi committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  /* Dummy servers */
  char const     *ag_sink_port;
  int             ag_sink_socket;
  int             ag_down_socket;
};

static int test_init(agent_t *ag, char const *resolv_conf);
static int test_deinit(agent_t *ag);
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);
167 168 169 170 171 172 173

static int test_for_ack(agent_t *ag,
			nta_incoming_t *irq, 
			sip_t const *sip);
static int test_for_ack_or_timeout(agent_t *ag,
				   nta_incoming_t *irq, 
				   sip_t const *sip);
Pekka Pessi's avatar
Pekka Pessi committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

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",
	     name, __func__, sip->sip_request->rq_method_name, 
	     URL_PRINT_ARGS(sip->sip_request->rq_url),
	     sip->sip_request->rq_version);
    }
    else {
      printf("%s: %s: %s %03d %s\n", name, __func__, 
	     sip->sip_status->st_version, 
	     sip->sip_status->st_status, 
	     sip->sip_status->st_phrase);
    }
  }

195 196
  msg_destroy(msg);

Pekka Pessi's avatar
Pekka Pessi committed
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
  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";
  else if (leg == ag->ag_server_leg) 
    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";

  printf("%s: %s: %smatched with %s\n", name, func, 
	 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;
  else if (leg == ag->ag_server_leg) 
    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;
  else 
     printf("%s:%u: %s: did not exist\n", 
	    __FILE__, __LINE__, __func__);

  nta_leg_destroy(leg);
}

239

Pekka Pessi's avatar
Pekka Pessi committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 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 323 324 325 326 327 328 329 330
int leg_callback_200(agent_t *ag,
		     nta_leg_t *leg,
		     nta_incoming_t *irq,
		     sip_t const *sip)
{
  BEGIN();

  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
	   name, __func__, sip->sip_request->rq_method_name, 
	   URL_PRINT_ARGS(sip->sip_request->rq_url),
	   sip->sip_request->rq_version);
  }

  TEST_1(sip->sip_content_length);
  TEST_1(sip->sip_via);
  TEST_1(sip->sip_from && sip->sip_from->a_tag);

  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;

  END();
}

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",
	   name, __func__, sip->sip_request->rq_method_name, 
	   URL_PRINT_ARGS(sip->sip_request->rq_url),
	   sip->sip_request->rq_version);
  }

  return 500;
}

int new_leg_callback_200(agent_t *ag,
			 nta_leg_t *leg,
			 nta_incoming_t *irq,
			 sip_t const *sip)
{
  BEGIN();

  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
	   name, __func__, sip->sip_request->rq_method_name, 
	   URL_PRINT_ARGS(sip->sip_request->rq_url),
	   sip->sip_request->rq_version);
  }

  TEST_1(sip->sip_content_length);
  TEST_1(sip->sip_via);
  TEST_1(sip->sip_from && sip->sip_from->a_tag);

  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());
  TEST_1(ag->ag_bob_leg);
331
  TEST_1(nta_leg_tag(ag->ag_bob_leg, NULL));
Pekka Pessi's avatar
Pekka Pessi committed
332
  TEST_1(nta_leg_get_tag(ag->ag_bob_leg));
333
  TEST_1(nta_incoming_tag(irq, nta_leg_get_tag(ag->ag_bob_leg)));
Pekka Pessi's avatar
Pekka Pessi committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

  return 200;

  END();
}


int outgoing_callback(agent_t *ag,
		      nta_outgoing_t *orq,
		      sip_t const *sip)
{
  BEGIN();

  int status = sip->sip_status->st_status;

  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s %03d %s\n", name, __func__, 
	   sip->sip_status->st_version, 
	   sip->sip_status->st_status, 
	   sip->sip_status->st_phrase);
  }

  TEST(orq, ag->ag_orq);

  ag->ag_status = status;

  if (status < 200)
    return 0;

  if (ag->ag_comp) {
    nta_compartment_decref(&ag->ag_client_compartment);
    ag->ag_client_compartment = nta_outgoing_compartment(orq);
  }

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

  if (ag->ag_tag_remote) {
372 373
    TEST_S(nta_leg_rtag(ag->ag_tag_remote, sip->sip_to->a_tag), 
	   sip->sip_to->a_tag);
Pekka Pessi's avatar
Pekka Pessi committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    ag->ag_tag_remote = NULL;
  }

  TEST_1(sip->sip_to && sip->sip_to->a_tag);

  nta_outgoing_destroy(orq);
  ag->ag_orq = NULL;
  return 0;

  END();
}

static
int test_magic_branch(agent_t *ag, sip_t const *sip) 
{
  BEGIN();
  
  if (sip) {
    TEST_1(sip->sip_via);
    TEST_S(sip->sip_via->v_branch, "MagicalBranch");
  }

  END();
}

static
int magic_callback(agent_t *ag,
		   nta_outgoing_t *orq,
		   sip_t const *sip)
{
  test_magic_branch(ag, sip);
  return outgoing_callback(ag, orq, sip);
}

void 
nta_test_run(agent_t *ag)
{
  for (ag->ag_status = 0; ag->ag_status < 200;) {
    if (tstflags & tst_verbatim) {
      fputs(".", stdout); fflush(stdout);
    }
    su_root_step(ag->ag_root, 500L);
  }
}

#include <msg_mclass.h>

int test_init(agent_t *ag, char const *resolv_conf)
{
  char const *contact = "sip:*:*;comp=sigcomp";
  su_sockaddr_t su;
  socklen_t sulen;
  int s, af, sulen0;

  BEGIN();

  TEST_1(ag->ag_root = su_root_create(ag));

  TEST_1(ag->ag_mclass = msg_mclass_clone(sip_default_mclass(), 0, 0));

434
#if SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
435
  if (str0cmp(getenv("ipv6"), "true") == 0) {
436
    contact = "sip:[::]:*;comp=sigcomp";
Pekka Pessi's avatar
Pekka Pessi committed
437 438 439 440 441 442
    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";
  }
443 444 445 446
#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
447 448 449 450 451 452 453 454 455 456 457 458 459

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

  /* Sink server */
  s = socket(af, SOCK_DGRAM, 0); TEST_1(s != -1);
  memset(&su, 0, sulen = sulen0);
  su.su_family = af;
  if (getenv("sink")) {
    su.su_port = htons(atoi(getenv("sink")));
  }
460
  TEST_1(bind(s, &su.su_sa, sulen) < 0 ? (perror("bind"), 0) : 1);
Pekka Pessi's avatar
Pekka Pessi committed
461 462 463 464 465 466
  TEST_1(getsockname(s, &su.su_sa, &sulen) == 0);

  ag->ag_sink_port = su_sprintf(ag->ag_home, "%u", ntohs(su.su_sin.sin_port));
  ag->ag_sink_socket = s;

  /* Down server */
467
  s = socket(af, SOCK_STREAM, 0); TEST_1(s != -1);
Pekka Pessi's avatar
Pekka Pessi committed
468 469 470 471 472
  memset(&su, 0, sulen = sulen0);
  su.su_family = af;
  if (getenv("down")) {
    su.su_port = htons(atoi(getenv("down")));
  }
473
  TEST_1(bind(s, &su.su_sa, sulen) < 0 ? (perror("bind"), 0) : 1);
Pekka Pessi's avatar
Pekka Pessi committed
474 475 476 477 478 479 480 481 482 483 484 485
  ag->ag_down_socket = s;
  
  /* Create agent */
  TEST_1(ag->ag_agent = nta_agent_create(ag->ag_root,
					 (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),
486
					 NTATAG_PRELOAD(2048),
Pekka Pessi's avatar
Pekka Pessi committed
487 488 489 490 491 492 493 494 495 496
					 TAG_END()));
  /* Create a default leg */
  TEST_1(ag->ag_default_leg = nta_leg_tcreate(ag->ag_agent, 
					     leg_callback_200,
					     ag,
					     NTATAG_NO_DIALOG(1),
					     TAG_END()));

  {
    /* Initialize our headers */
497
    // msg_param_t from_params[2] = { "tag=fdeadbeef", NULL };
Pekka Pessi's avatar
Pekka Pessi committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
    sip_from_t from[1];
    sip_to_t to[1];
    sip_contact_t m[1];

    sip_from_init(from);
    sip_to_init(to);
    sip_contact_init(m);

    TEST_1(ag->ag_contact = nta_agent_contact(ag->ag_agent));

    *m->m_url = *ag->ag_contact->m_url;
    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));

    // url_strip_transport(ag->ag_bob->a_url);

    *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;
    // from->a_params = from_params;
    TEST_1(ag->ag_alice = sip_from_dup(ag->ag_home, from));

    // url_strip_transport(ag->ag_alice->a_url);
  }
  {
    char const data[] = 
      "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;

    ag->ag_aliases = 
      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);
    TEST(ag->ag_aliases->m_next->m_next->m_next, NULL);

    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;

    TEST(nta_agent_set_params(ag->ag_agent, 
			      NTATAG_ALIASES(ag->ag_aliases),
			      NTATAG_REL100(1),
			      NTATAG_UA(1), 
			      NTATAG_USE_NAPTR(1),
			      NTATAG_USE_SRV(1),
			      TAG_END()),
	 5);
Pekka Pessi's avatar
Pekka Pessi committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

    TEST(nta_agent_set_params(ag->ag_agent, 
			      NTATAG_ALIASES(ag->ag_aliases),
			      NTATAG_DEFAULT_PROXY("sip:127.0.0.1"),
			      TAG_END()), 2);

    TEST(nta_agent_set_params(ag->ag_agent, 
			      NTATAG_ALIASES(ag->ag_aliases),
			      NTATAG_DEFAULT_PROXY(NULL),
			      TAG_END()), 2);

    TEST(nta_agent_set_params(ag->ag_agent, 
			      NTATAG_DEFAULT_PROXY("tel:+35878008000"),
			      TAG_END()), -1);

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
  }
  
  {
    url_t url[1];

    /* Create the server leg */
    *url = *ag->ag_aliases->m_url;
    url->url_user = "%";
    TEST_1(ag->ag_server_leg = nta_leg_tcreate(ag->ag_agent, 
					       leg_callback_200,
					       ag,
					       NTATAG_NO_DIALOG(1),
					       URLTAG_URL(url),
					       TAG_END()));
  }

  END();
}  

int test_deinit(agent_t *ag)
{
  BEGIN();

609 610 611 612
  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
613 614 615 616 617 618 619 620
  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);

621 622 623 624
  su_free(ag->ag_home, (void *)ag->ag_sink_port), ag->ag_sink_port = NULL;

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

Pekka Pessi's avatar
Pekka Pessi committed
625 626 627 628 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
  END();
}  


static unsigned char const code[] = 
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

#include <su_uniqueid.h>

sip_payload_t *test_payload(su_home_t *home, int size)
{
  sip_payload_t *pl = sip_payload_create(home, NULL, size);

  if (pl) {
    int i;
    char *data = (char *)pl->pl_data;
    
    for (i = 0; i < size; i++) {
      if ((i & 63) != 63)
	data[i] = code[su_randint(0, 63)];
      else
	data[i] = '\n';
    }
  }

  return pl;
}

/* 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;

  url_t url[1];

  BEGIN();

  *url = *ag->ag_contact->m_url;
  url->url_port = "*";
668 669
  url->url_params = "transport=tcp";

Pekka Pessi's avatar
Pekka Pessi committed
670 671 672 673 674 675 676 677 678 679 680 681 682
  url->url_params = "transport=udp";

  TEST_1(nta_agent_add_tport(ag->ag_agent, (url_string_t *)url, 
			     TAG_END()) == 0);

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

  for (; v; v = v->v_next) {
    if (strcasecmp(v->v_protocol, sip_transport_udp) == 0) {
      if (udp)
	v_udp_only = v;
      udp = 1;
      if (udp_comp == NULL)
683
	udp_comp = v->v_comp;
Pekka Pessi's avatar
Pekka Pessi committed
684 685 686 687
    }
    else if (strcasecmp(v->v_protocol, sip_transport_tcp) == 0) {
      tcp = 1;
      if (tcp_comp == NULL)
688
	tcp_comp = v->v_comp;
Pekka Pessi's avatar
Pekka Pessi committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
    }
    else if (strcasecmp(v->v_protocol, sip_transport_sctp) == 0) {
      sctp = 1;
    }
    else if (strcasecmp(v->v_protocol, sip_transport_tls) == 0) {
      tls = 1;
    }
  }

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

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

  {
    /* Test 0.1
     * Send a message from default leg to default leg 
     */
    char const p_acid[] = "P-Access-Network-Info: IEEE-802.11g\n";
Pekka Pessi's avatar
Pekka Pessi committed
709
    url_t url[1];
710

Pekka Pessi's avatar
Pekka Pessi committed
711 712
    *url = *ag->ag_contact->m_url;
    url->url_params = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
713
    ag->ag_expect_leg = ag->ag_default_leg;
Pekka Pessi's avatar
Pekka Pessi committed
714
    su_free(ag->ag_home, (void *)ag->ag_out_via), ag->ag_out_via = NULL;
715

Pekka Pessi's avatar
Pekka Pessi committed
716 717
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, 
718
			       outgoing_callback, ag,
Pekka Pessi's avatar
Pekka Pessi committed
719 720
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
Pekka Pessi's avatar
Pekka Pessi committed
721
			       (url_string_t *)url,
Pekka Pessi's avatar
Pekka Pessi committed
722 723 724 725 726 727
			       SIPTAG_SUBJECT_STR("Test 0.1"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       SIPTAG_HEADER_STR(p_acid),
			       TAG_END()));
Pekka Pessi's avatar
Pekka Pessi committed
728

Pekka Pessi's avatar
Pekka Pessi committed
729 730 731 732 733
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
    TEST_1(ag->ag_request);
734

Pekka Pessi's avatar
Pekka Pessi committed
735 736
    TEST_1(ag->ag_out_via->v_comp == NULL);

737
    nta_leg_bind(ag->ag_default_leg, leg_callback_200, ag);
Pekka Pessi's avatar
Pekka Pessi committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
  }

  /* Test 0.1.1
   * Send a message from Bob to Alice using SIGCOMP and TCP
   */
  if (tcp_comp) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 1024;

    *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
758 759
    su_free(ag->ag_home, (void *)ag->ag_out_via), ag->ag_out_via = NULL;

Pekka Pessi's avatar
Pekka Pessi committed
760 761 762 763 764 765 766 767 768 769 770 771
    TEST_1(ag->ag_orq = 
	   nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
				ag->ag_obp,
				SIP_METHOD_MESSAGE,
				(url_string_t *)url,
				NTATAG_COMP("sigcomp"),
				SIPTAG_SUBJECT_STR("Test 0.1.1"),
				SIPTAG_FROM(ag->ag_bob),
				SIPTAG_TO(ag->ag_alice),
				SIPTAG_CONTACT(ag->ag_m_bob),
				SIPTAG_PAYLOAD(pl),
				TAG_END()));
772 773
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
774 775 776 777 778 779
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST_1(ag->ag_client_compartment);
    nta_compartment_decref(&ag->ag_client_compartment);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
Pekka Pessi's avatar
Pekka Pessi committed
780
    TEST_S(ag->ag_out_via->v_comp, "sigcomp");
Pekka Pessi's avatar
Pekka Pessi committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794
  }

  /* Test 0.2
   * Send a message from Bob to Alice
   * This time specify a TCP URI, and include a large payload 
   * of 512 kB
   */
  if (tcp) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 512 * 1024;

    *url = *ag->ag_aliases->m_url;
    url->url_user = "alice";
Pekka Pessi's avatar
Pekka Pessi committed
795
    url->url_params = "transport=tcp";
Pekka Pessi's avatar
Pekka Pessi committed
796 797 798 799 800 801

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

    ag->ag_expect_leg = ag->ag_server_leg;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
Pekka Pessi's avatar
Pekka Pessi committed
802
			       NULL,
Pekka Pessi's avatar
Pekka Pessi committed
803 804 805 806 807 808 809
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.2"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
Pekka Pessi's avatar
Pekka Pessi committed
810
			       NTATAG_DEFAULT_PROXY(ag->ag_obp),
Pekka Pessi's avatar
Pekka Pessi committed
811
			       TAG_END()));
812 813
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
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
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
  }

  /* 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) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 512 * 1024;

    *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;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.3"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TAG_END()));
846 847
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
  }

  /* Test 0.4.1:
   * Send a message from Bob to Alice
   * This time include a payload of 2 kB, let NTA choose transport.
   */
  {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 2 * 1024;

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

    TEST_1(pl = test_payload(ag->ag_home, size));
    su_free(ag->ag_home, (void *)ag->ag_out_via), ag->ag_out_via = NULL;

    ag->ag_expect_leg = ag->ag_server_leg;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.4.1"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TAG_END()));
881 882
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
883 884 885 886 887 888 889
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
    TEST_1(ag->ag_out_via);
    TEST_1(strcasecmp(ag->ag_out_via->v_protocol, "SIP/2.0/TCP") == 0 ||
	   strcasecmp(ag->ag_out_via->v_protocol, "SIP/2.0/SCTP") == 0);
890
    su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
  }

  /* 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) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 2 * 1024;

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

    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.4.2"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TAG_END()));
925 926
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
927 928 929 930 931 932
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
    TEST_1(ag->ag_in_via);
    TEST_1(strcasecmp(ag->ag_in_via->v_protocol, "SIP/2.0/UDP") == 0);
933
    su_free(ag->ag_home, ag->ag_in_via), ag->ag_in_via = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
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
  }

  /* Test 0.5:
   * Send a message from Bob to Alice
   * This time include a payload of 2 kB, try to use UDP.
   */
  if (udp) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 2 * 1024;

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

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

    su_free(ag->ag_home, (void *)ag->ag_out_via), ag->ag_out_via = NULL;

    ag->ag_expect_leg = ag->ag_server_leg;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.5"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TPTAG_MTU(0xffffffff),
			       TAG_END()));
965 966
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
    TEST_1(ag->ag_out_via);
    TEST_S(ag->ag_out_via->v_protocol, "SIP/2.0/UDP");
  }

  if (udp) {
    /* Send a message from default leg to server leg 
     * using a prefilled Via header
     */
    sip_via_t via[1];

    sip_via_init(via);

    via->v_protocol = sip_transport_udp;
    
    via->v_host = ag->ag_contact->m_url->url_host;
    via->v_port = ag->ag_contact->m_url->url_port;
    
    sip_via_add_param(ag->ag_home, via, "branch=MagicalBranch");

    nta_agent_set_params(ag->ag_agent, 
991
			 NTATAG_ALIASES(ag->ag_aliases),
Pekka Pessi's avatar
Pekka Pessi committed
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 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
			 NTATAG_USER_VIA(1),
			 TAG_END());

    ag->ag_expect_leg = ag->ag_server_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, 
			       magic_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.6"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       SIPTAG_VIA(via),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);

    nta_agent_set_params(ag->ag_agent, 
			 NTATAG_USER_VIA(0),
			 TAG_END());
  }

  /* Test 0.7
   * Send a message from Bob to Alice using SCTP 
   */
  if (sctp) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 16 * 1024;

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

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

    ag->ag_expect_leg = ag->ag_server_leg;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.7"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TAG_END()));
1049 1050
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
1051 1052 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
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
  }

  /* Test 0.8: Send a too large message */
  if (tcp) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 128 * 1024;

    nta_agent_set_params(ag->ag_agent, 
			 NTATAG_MAXSIZE(65536),
			 TAG_END());

    *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;
    ag->ag_latest_leg = NULL;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.8"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TAG_END()));
1085 1086
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    nta_test_run(ag);
    TEST(ag->ag_status, 413);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, NULL);

    nta_agent_set_params(ag->ag_agent, 
			 NTATAG_MAXSIZE(2 * 1024 * 1024),
			 TAG_END());
  }

  /* Test 0.9: Timeout */
  {
    url_t url[1];

    printf("%s: starting MESSAGE timeout test, completing in 4 seconds\n",
	   name);

    nta_agent_set_params(ag->ag_agent, 
			 NTATAG_TIMEOUT_408(1),
			 NTATAG_SIP_T1(25), 
			 NTATAG_SIP_T1X64(64 * 25), 
Pekka Pessi's avatar
Pekka Pessi committed
1108 1109
			 NTATAG_SIP_T2(8 * 25),
			 NTATAG_SIP_T4(10 * 25),
Pekka Pessi's avatar
Pekka Pessi committed
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
			 TAG_END());

    *url = *ag->ag_aliases->m_url;
    url->url_user = "timeout";
    url->url_port = ag->ag_sink_port;

    ag->ag_expect_leg = ag->ag_server_leg;
    ag->ag_latest_leg = NULL;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.9"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       TAG_END()));

    nta_test_run(ag);
    TEST(ag->ag_status, 408);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, NULL);

    nta_agent_set_params(ag->ag_agent,
			 NTATAG_SIP_T1(500),
			 NTATAG_SIP_T1X64(64 * 500),
Pekka Pessi's avatar
Pekka Pessi committed
1137 1138
			 NTATAG_SIP_T2(NTA_SIP_T2),
			 NTATAG_SIP_T4(NTA_SIP_T4),
Pekka Pessi's avatar
Pekka Pessi committed
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 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
			 TAG_END());
  }
  

  END();
}

int test_resolv(agent_t *ag, char const *resolv_conf)
{
  int udp = 0, tcp = 0, sctp = 0, tls = 0;
  sip_via_t const *v;

  url_t *url;

  if (!resolv_conf)
    return 0;

  BEGIN();

  TEST_1(v = nta_agent_via(ag->ag_agent));
  for (; v; v = v->v_next) {
    if (strcasecmp(v->v_protocol, sip_transport_udp) == 0)
      udp = 1;
    else if (strcasecmp(v->v_protocol, sip_transport_tcp) == 0)
      tcp = 1;
    else if (strcasecmp(v->v_protocol, sip_transport_sctp) == 0)
      sctp = 1;
    else if (strcasecmp(v->v_protocol, sip_transport_tls) == 0)
      tls = 1;
  }

  url = url_hdup(ag->ag_home, (void *)"sip:example.org"); TEST_1(url);

  {
    /* Test 1.1
     * Send a message to sip:example.org
     */
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.1"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
  }

  {
    /* Test 1.2
     * Send a message to sip:srv.example.org
     */
    url->url_host = "srv.example.org";
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.2"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
  }

  {
    /* Test 1.3
     * Send a message to sip:ipv.example.org
     */
    url->url_host = "ipv.example.org";
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.3"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
  }

  {
    /* Test 1.4.1
     * Send a message to sip:down.example.org
     */
    url->url_host = "down.example.org";
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.4.1"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
  }

  {
    /* Test 1.4.2
     * Send a message to sip:na503.example.org
     */
    url->url_host = "na503.example.org";
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.4.2"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 503);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
  }

  {
    /* Test 1.4.3
     * Send a message to sip:nona.example.org
     */
    url->url_host = "nona.example.org";
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.4.3"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
  }

  {
    /* Test 1.4.4
     * Send a message to sip:nosrv.example.org
     * After failing to find _sip._udp.nosrv.example.org,
     * second SRV with _sip._udp.srv.example.org succeeds
     */
    url->url_host = "nosrv.example.org";
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.4.4"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
  }

  {
    /* Test 1.5.1 
     * Send a message to sip:srv.example.org;transport=tcp
     * Test outgoing_make_srv_query()
     */
    url->url_host = "srv.example.org";
    url->url_params = "transport=tcp";
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.5.1: outgoing_make_srv_query()"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
    url->url_params = NULL;
  }

  {
    /* Test 1.5.2
     * Send a message to sip:srv.example.org;transport=udp
     * Test outgoing_make_srv_query()
     */
    url->url_host = "srv.example.org";
    url->url_params = "transport=udp";
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.5.2: outgoing_make_srv_query()"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
    url->url_params = NULL;
  }

  {
    /* Test 1.5.3
     * Send a message to sip:srv2.example.org;transport=udp
     * Test outgoing_query_srv_a()
     */
    url->url_host = "srv2.example.org";
    url->url_params = "transport=udp";
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.5: outgoing_query_srv_a()"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
    url->url_params = NULL;
  }

  {
    /* Test 1.6.1
     * Send a message to sip:srv.example.org:$port
     * Test outgoing_make_a_aaaa_query()
     */
    url->url_host = "srv.example.org";
    url->url_port = ag->ag_contact->m_url->url_port;
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.6.1: outgoing_make_a_aaaa_query()"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 503);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
  }

  {
    /* Test 1.6.2
     * Send a message to sip:a.example.org:$port
     * Test outgoing_make_a_aaaa_query()
     */
    url->url_host = "a.example.org";
    url->url_port = ag->ag_contact->m_url->url_port;
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.6.2: outgoing_make_a_aaaa_query()"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
    url->url_port = NULL;
  }

#if 0				/* This must be run on host *without* proxy */
  {
    /* Test 1.6c
     * Send a message to sip:na.example.org
     * Test outgoing_query_all() with NAPTR "A" flag
     */
    url->url_host = "na.example.org";
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.6c"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 503);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
  }
#endif

  {
    /* Test 1.7
     * Send a message to sip:down2.example.org:$port
     * Test A record failover.
     */
    url->url_host = "down2.example.org";
    url->url_port = ag->ag_contact->m_url->url_port;
    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.7: outgoing_make_a_aaaa_query()"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
    url->url_params = NULL;
  }

  return 0;

#if 0
  /* Test 0.1.1
   * Send a message from Bob to Alice using SIGCOMP and TCP
   */
  if (tcp_comp) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 1024;

    *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;
    TEST_1(ag->ag_orq = 
	   nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
				ag->ag_obp,
				SIP_METHOD_MESSAGE,
				(url_string_t *)url,
				SIPTAG_SUBJECT_STR("Test 0.1.1"),
				SIPTAG_FROM(ag->ag_bob),
				SIPTAG_TO(ag->ag_alice),
				SIPTAG_CONTACT(ag->ag_m_bob),
				SIPTAG_PAYLOAD(pl),
				TAG_END()));
1532 1533
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
  }

  /* Test 0.2
   * Send a message from Bob to Alice
   * This time specify a TCP URI, and include a large payload 
   * of 512 kB
   */
  if (tcp) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 512 * 1024;

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


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

    ag->ag_expect_leg = ag->ag_server_leg;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.2"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TAG_END()));
1574 1575
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
  }

  /* 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) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 512 * 1024;

    *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;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.3"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TAG_END()));
1608 1609
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
  }

  /* Test 0.4:
   * Send a message from Bob to Alice
   * This time include a payload of 2 kB, let NTA choose transport.
   */
  {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 2 * 1024;

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

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

    su_free(ag->ag_home, (void *)ag->ag_out_via), ag->ag_out_via = NULL;

    ag->ag_expect_leg = ag->ag_server_leg;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.4"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TAG_END()));
1644 1645
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
    TEST_1(ag->ag_out_via);
    TEST_1(strcasecmp(ag->ag_out_via->v_protocol, "SIP/2.0/TCP") == 0 ||
	   strcasecmp(ag->ag_out_via->v_protocol, "SIP/2.0/SCTP") == 0);
  }

  /* Test 0.5:
   * Send a message from Bob to Alice
   * This time include a payload of 2 kB, try to use UDP.
   */
  if (udp) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 2 * 1024;

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

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

    su_free(ag->ag_home, (void *)ag->ag_out_via), ag->ag_out_via = NULL;

    ag->ag_expect_leg = ag->ag_server_leg;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.5"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TPTAG_MTU(0xffffffff),
			       TAG_END()));
1684 1685
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
    TEST_1(ag->ag_out_via);
    TEST_S(ag->ag_out_via->v_protocol, "SIP/2.0/UDP");
  }

  if (udp) {
    /* Send a message from default leg to server leg 
     * using a prefilled Via header
     */
    sip_via_t via[1];

    sip_via_init(via);

    via->v_protocol = sip_transport_udp;
    
    via->v_host = ag->ag_contact->m_url->url_host;
    via->v_port = ag->ag_contact->m_url->url_port;
    
    sip_via_add_param(ag->ag_home, via, "branch=MagicalBranch");

    nta_agent_set_params(ag->ag_agent, 
			 NTATAG_USER_VIA(1),
			 TAG_END());

    ag->ag_expect_leg = ag->ag_server_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, 
			       magic_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.6"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       SIPTAG_VIA(via),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);

    nta_agent_set_params(ag->ag_agent, 
			 NTATAG_USER_VIA(0),
			 TAG_END());
  }

  /* Test 0.7
   * Send a message from Bob to Alice using SCTP 
   */
  if (sctp) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 16 * 1024;

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

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

    ag->ag_expect_leg = ag->ag_server_leg;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.7"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TAG_END()));
1767 1768
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_server_leg);
  }

  /* Test 0.8: Send a too large message */
  if (tcp) {
    url_t url[1];
    sip_payload_t *pl;
    unsigned size = 128 * 1024;

    nta_agent_set_params(ag->ag_agent, 
			 NTATAG_MAXSIZE(65536),
			 TAG_END());

    *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;
    ag->ag_latest_leg = NULL;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.8"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       SIPTAG_PAYLOAD(pl),
			       TAG_END()));
1803 1804
    su_free(ag->ag_home, pl);

Pekka Pessi's avatar
Pekka Pessi committed
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
    nta_test_run(ag);
    TEST(ag->ag_status, 413);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, NULL);

    nta_agent_set_params(ag->ag_agent, 
			 NTATAG_MAXSIZE(2 * 1024 * 1024),
			 TAG_END());
  }

  /* Test 0.9: Timeout */
  {
    url_t url[1];

    printf("%s: starting MESSAGE timeout test, test will complete in 4 seconds\n",
	   name);

    nta_agent_set_params(ag->ag_agent, 
			 NTATAG_TIMEOUT_408(1),
			 NTATAG_SIP_T1(25), 
			 NTATAG_SIP_T1X64(64 * 25), 
			 TAG_END());

    *url = *ag->ag_aliases->m_url;
    url->url_user = "timeout";
    url->url_port = ag->ag_sink_port;

    ag->ag_expect_leg = ag->ag_server_leg;
    ag->ag_latest_leg = NULL;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 0.9"),
			       SIPTAG_FROM(ag->ag_bob),
			       SIPTAG_TO(ag->ag_alice),
			       SIPTAG_CONTACT(ag->ag_m_bob),
			       TAG_END()));

    nta_test_run(ag);
    TEST(ag->ag_status, 408);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, NULL);

    nta_agent_set_params(ag->ag_agent,
			 NTATAG_SIP_T1(500),
			 NTATAG_SIP_T1X64(64 * 500),
			 TAG_END());
  }
#endif  

  END();
}

/* Test default routing */

int test_routing(agent_t *ag)
{
  url_t url[1];

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

  nta_agent_set_params(ag->ag_agent, 
		       NTATAG_MAXSIZE(2 * 1024 * 1024),
		       TAG_END());

  BEGIN();

  {
    /* 
     * Send a message from default leg to default leg 
     *
     * We are now using url with an explicit port that does not match with
     * our own port number.
     */
    url_t url2[1];

    *url2 = *url;
    url2->url_port = "9";	/* discard service */

    ag->ag_expect_leg = ag->ag_default_leg;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       (url_string_t *)url,
			       SIP_METHOD_MESSAGE,
			       (url_string_t *)url2,
			       SIPTAG_SUBJECT_STR("Test 1.2"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_default_leg);
  }

  END();
}

/* Test dialogs and the tag handling */

int test_dialog(agent_t *ag)
{
  BEGIN();

  /*
   * Test establishing a dialog
   *
   * Alice sends a message to Bob, then Bob back to the Alice, and again
   * Alice to Bob.
   */
  TEST_1(ag->ag_alice_leg = nta_leg_tcreate(ag->ag_agent, 
					   leg_callback_200,
					   ag,
					   SIPTAG_FROM(ag->ag_alice),
					   SIPTAG_TO(ag->ag_bob),
					   TAG_END()));
1925
  TEST_1(nta_leg_tag(ag->ag_alice_leg, NULL));
Pekka Pessi's avatar
Pekka Pessi committed
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
  nta_leg_bind(ag->ag_server_leg, new_leg_callback_200, ag);

  /* Send message from Alice to Bob establishing the dialog */
  ag->ag_expect_leg = ag->ag_server_leg;
  ag->ag_tag_remote = ag->ag_alice_leg;
  TEST_1(ag->ag_orq = 
        nta_outgoing_tcreate(ag->ag_alice_leg, outgoing_callback, ag,
			     ag->ag_obp,
			     SIP_METHOD_MESSAGE,
			     (url_string_t *)ag->ag_m_bob->m_url,
			     SIPTAG_SUBJECT_STR("Test 2.1"),
			     SIPTAG_FROM(ag->ag_alice),
			     SIPTAG_TO(ag->ag_bob),
			     SIPTAG_CONTACT(ag->ag_m_alice),
			     TAG_END()));
  nta_test_run(ag);
  TEST(ag->ag_status, 200);
  TEST(ag->ag_orq, NULL);
  TEST(ag->ag_latest_leg, ag->ag_server_leg);
  TEST_1(ag->ag_bob_leg != NULL);

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

  /* Send message from Bob to Alice */
  ag->ag_expect_leg = ag->ag_alice_leg;
  TEST_1(ag->ag_orq = 
        nta_outgoing_tcreate(ag->ag_bob_leg, outgoing_callback, ag,
      		       NULL,
      		       SIP_METHOD_MESSAGE,
      		       (url_string_t *)ag->ag_m_alice->m_url,
      		       SIPTAG_SUBJECT_STR("Test 2.2"),
      		       TAG_END()));
  nta_test_run(ag);
  TEST(ag->ag_status, 200);
  TEST(ag->ag_orq, NULL);
  TEST(ag->ag_latest_leg, ag->ag_alice_leg);

  /* Send again message from Alice to Bob */
  ag->ag_expect_leg = ag->ag_bob_leg;
  TEST_1(ag->ag_orq = 
        nta_outgoing_tcreate(ag->ag_alice_leg, outgoing_callback, ag,
      		       NULL,
      		       SIP_METHOD_MESSAGE,
      		       (url_string_t *)ag->ag_m_bob->m_url,
      		       SIPTAG_SUBJECT_STR("Test 2.3"),
      		       TAG_END()));
  nta_test_run(ag);
  TEST(ag->ag_status, 200);
  TEST(ag->ag_orq, NULL);
  TEST(ag->ag_latest_leg, ag->ag_bob_leg);

  /* Send message from Bob to Alice
   * This time, however, specify request URI 
   */
  {
    ag->ag_expect_leg = ag->ag_alice_leg;
    TEST_1(ag->ag_orq = 
          nta_outgoing_tcreate(ag->ag_bob_leg, outgoing_callback, ag,
      			 NULL,
      			 SIP_METHOD_MESSAGE,
      			 (url_string_t *)ag->ag_m_alice->m_url,
      			 SIPTAG_SUBJECT_STR("Test 2.4"),
      			 TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 200);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, ag->ag_alice_leg);
  }

  nta_leg_destroy(ag->ag_alice_leg), ag->ag_alice_leg = NULL;
  nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;

  END();
}

/* ---------------------------------------------------------------------- */
/* Test INVITE, dialogs */

static
int test_for_ack(agent_t *ag,
		 nta_incoming_t *irq, 
		 sip_t const *sip)
{
  sip_method_t method;

  BEGIN();

  method = sip ? sip->sip_request->rq_method : sip_method_unknown;

  nta_incoming_destroy(irq);
  TEST(irq, ag->ag_irq);
  ag->ag_irq = NULL;

  TEST(method, sip_method_ack);
  
  ag->ag_status = 200;

  END();
}

static
int test_for_prack(agent_t *ag,
		   nta_reliable_t *rel,
		   nta_incoming_t *prack,
		   sip_t const *sip)
{
  sip_method_t method = sip ? sip->sip_request->rq_method : sip_method_unknown;

  nta_incoming_treply(ag->ag_irq, 
		      SIP_200_OK, 
		      SIPTAG_CONTACT(ag->ag_m_alice),
		      TAG_END());

  TEST(method, sip_method_prack);
		     
  return 200;
}

int alice_leg_callback(agent_t *ag,
		       nta_leg_t *leg,
		       nta_incoming_t *irq,
		       sip_t const *sip)
{
  BEGIN();

  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
	   name, __func__, sip->sip_request->rq_method_name, 
	   URL_PRINT_ARGS(sip->sip_request->rq_url),
	   sip->sip_request->rq_version);
  }

  TEST_1(sip->sip_content_length);
  TEST_1(sip->sip_via);
  TEST_1(sip->sip_from && sip->sip_from->a_tag);

  if (sip->sip_request->rq_method == sip_method_prack)
    return 481;

  ag->ag_latest_leg = leg;

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

  if (sip->sip_request->rq_method == sip_method_invite) {
    TEST_1(sip_has_feature(sip->sip_supported, "100rel"));
    nta_incoming_bind(irq, test_for_ack, ag);
    nta_incoming_treply(irq, SIP_100_TRYING, TAG_END());

    nta_agent_set_params(ag->ag_agent, 
			 NTATAG_DEBUG_DROP_PROB(ag->ag_drop),
			 TAG_END());

    ag->ag_reliable = 
      nta_reliable_treply(irq,
			  NULL, NULL,
			  SIP_183_SESSION_PROGRESS,
			  SIPTAG_CONTENT_TYPE(ag->ag_content_type),
			  SIPTAG_PAYLOAD(ag->ag_payload),
			  SIPTAG_CONTACT(ag->ag_m_alice),
			  TAG_END());
    TEST_1(ag->ag_reliable);
    ag->ag_reliable = 
      nta_reliable_treply(irq,
			  NULL, NULL,
			  184, "Next",
			  SIPTAG_CONTACT(ag->ag_m_alice),
			  TAG_END());
    TEST_1(ag->ag_reliable);
    ag->ag_reliable = 
      nta_reliable_treply(irq,
			  test_for_prack, ag,
			  185, "Last",
			  SIPTAG_CONTACT(ag->ag_m_alice),
			  TAG_END());
    TEST_1(ag->ag_reliable);
    ag->ag_irq = irq;
    return 0;
  } 

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

  return 200;

  END();
}


int bob_leg_callback(agent_t *ag,
		     nta_leg_t *leg,
		     nta_incoming_t *irq,
		     sip_t const *sip)
{
  BEGIN();

  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
	   name, __func__, sip->sip_request->rq_method_name, 
	   URL_PRINT_ARGS(sip->sip_request->rq_url),
	   sip->sip_request->rq_version);
  }

  TEST_1(sip->sip_content_length);
  TEST_1(sip->sip_via);
  TEST_1(sip->sip_from && sip->sip_from->a_tag);

  if (sip->sip_request->rq_method == sip_method_prack)
    return 481;

  ag->ag_latest_leg = leg;

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

  if (ag->ag_bob_leg == NULL) {
    nta_leg_bind(leg, leg_callback_500, ag);
    ag->ag_bob_leg = nta_leg_tcreate(ag->ag_agent,
				     bob_leg_callback,
				     ag,
				     SIPTAG_CALL_ID(sip->sip_call_id),
				     SIPTAG_FROM(sip->sip_to),
				     SIPTAG_TO(sip->sip_from),
				     TAG_END());
    TEST_1(ag->ag_bob_leg);
2156
    TEST_1(nta_leg_tag(ag->ag_bob_leg, NULL));
Pekka Pessi's avatar
Pekka Pessi committed
2157
    TEST_1(nta_leg_get_tag(ag->ag_bob_leg));
2158
    TEST_1(nta_incoming_tag(irq, nta_leg_get_tag(ag->ag_bob_leg)));
Pekka Pessi's avatar
Pekka Pessi committed
2159 2160 2161 2162 2163 2164 2165
    TEST(nta_leg_server_route(ag->ag_bob_leg, 
			      sip->sip_record_route, 
			      sip->sip_contact), 0);
  }

  if (sip->sip_request->rq_method == sip_method_invite) {
    nta_incoming_bind(irq, test_for_ack, ag); 
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
#if 1
    nta_incoming_treply(irq,
			SIP_180_RINGING,
			SIPTAG_CONTACT(ag->ag_m_bob),
			TAG_END());
    nta_incoming_treply(irq,
			SIP_180_RINGING,
			SIPTAG_CONTACT(ag->ag_m_bob),
			TAG_END());
#endif
Pekka Pessi's avatar
Pekka Pessi committed
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
    nta_incoming_treply(irq,
			SIP_200_OK,
			SIPTAG_CONTENT_TYPE(ag->ag_content_type),
			SIPTAG_PAYLOAD(ag->ag_payload),
			SIPTAG_CONTACT(ag->ag_m_bob),
			TAG_END());
    ag->ag_irq = irq;
    return 0;
  } else {
    return 200;
  }

  END();
}

int outgoing_invite_callback(agent_t *ag,
			     nta_outgoing_t *orq,
			     sip_t const *sip)
{
  BEGIN();

  int status = sip->sip_status->st_status;

  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s %03d %s\n", name, __func__, 
	   sip->sip_status->st_version, 
	   sip->sip_status->st_status, 
	   sip->sip_status->st_phrase);
  }

2206 2207 2208
  {
    msg_t *msg;

2209
    TEST_1(msg = nta_outgoing_getresponse(orq));
2210 2211 2212 2213 2214 2215 2216
    TEST_1(msg->m_refs == 2);
    TEST_1(sip_object(msg) == sip);
    if (ag->ag_probe_msg == NULL)
      ag->ag_probe_msg = msg;
    msg_destroy(msg);
  }

Pekka Pessi's avatar
Pekka Pessi committed
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232
  if (status < 200) {
    if (sip->sip_require && sip_has_feature(sip->sip_require, "100rel")) {
      TEST_1(sip->sip_rseq);
      orq = nta_outgoing_prack(ag->ag_call_leg, orq, NULL, NULL,
			       NULL,
			       sip, 
			       TAG_END());
      TEST_1(orq);
      nta_outgoing_destroy(orq);
    }
    return 0;
  }

  if (status < 300) {
    nta_outgoing_t *ack;

2233
    TEST_1(nta_leg_rtag(ag->ag_call_leg, sip->sip_to->a_tag));
Pekka Pessi's avatar
Pekka Pessi committed
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
    
    TEST(nta_leg_client_route(ag->ag_call_leg, 
			      sip->sip_record_route,
			      sip->sip_contact), 0);

    ack = nta_outgoing_tcreate(ag->ag_call_leg, NULL, NULL,
			       NULL,
			       SIP_METHOD_ACK,
			       NULL,
			       SIPTAG_CSEQ(sip->sip_cseq),
			       TAG_END());
    TEST_1(ack);
    nta_outgoing_destroy(ack);
  }
  else {
    ag->ag_status = status;
  }

  TEST_1(sip->sip_to && sip->sip_to->a_tag);

  nta_outgoing_destroy(orq);
  ag->ag_orq = NULL;
  ag->ag_call_leg = NULL;
  return 0;

  END();
}


int test_call(agent_t *ag)
{
  sip_content_type_t *c = ag->ag_content_type;
  sip_payload_t      *sdp = ag->ag_payload;
  nta_leg_t *old_leg;

  BEGIN();

  /*
   * Test establishing a call
   *
   * Alice sends a INVITE to Bob, then Bob sends 200 Ok.
   */
  TEST_1(ag->ag_alice_leg = nta_leg_tcreate(ag->ag_agent, 
					   alice_leg_callback,
					   ag,
					   SIPTAG_FROM(ag->ag_alice),
					   SIPTAG_TO(ag->ag_bob),
					   TAG_END()));
2282
  TEST_1(nta_leg_tag(ag->ag_alice_leg, NULL));
Pekka Pessi's avatar
Pekka Pessi committed
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
  nta_leg_bind(ag->ag_server_leg, bob_leg_callback, ag);
  
  /* Send INVITE */
  ag->ag_expect_leg = ag->ag_server_leg;
  TEST_1(ag->ag_orq = 
	 nta_outgoing_tcreate(ag->ag_call_leg = ag->ag_alice_leg, 
			      outgoing_invite_callback, ag,
			      ag->ag_obp,
			      SIP_METHOD_INVITE,
			      (url_string_t *)ag->ag_m_bob->m_url,
			      SIPTAG_SUBJECT_STR("Call 1"),
			      SIPTAG_CONTACT(ag->ag_m_alice),
			      SIPTAG_CONTENT_TYPE(c),
2296
			      SIPTAG_ACCEPT_CONTACT_STR("*;audio"),
Pekka Pessi's avatar
Pekka Pessi committed
2297
			      SIPTAG_PAYLOAD(sdp),
Pekka Pessi's avatar
Pekka Pessi committed
2298 2299
			      NTATAG_USE_TIMESTAMP(1),
			      NTATAG_PASS_100(1),
Pekka Pessi's avatar
Pekka Pessi committed
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
			      TAG_END()));

  /* Try to CANCEL it immediately */
  TEST_1(nta_outgoing_cancel(ag->ag_orq) == 0);
  /* As Bob immediately answers INVITE with 200 Ok, 
     cancel should be answered with 487. */

  nta_test_run(ag);
  TEST(ag->ag_status, 200);
  TEST(ag->ag_orq, NULL);
  TEST(ag->ag_latest_leg, ag->ag_server_leg);
  TEST_1(ag->ag_bob_leg != NULL);

  /* Re-INVITE from Bob to Alice.
   *
   * Alice first sends 183, waits for PRACK, then sends 184 and 185,
   * waits for PRACKs, then sends 200, waits for ACK.
   */
  ag->ag_expect_leg = ag->ag_alice_leg;
  TEST_1(ag->ag_orq = 
	nta_outgoing_tcreate(ag->ag_call_leg = ag->ag_bob_leg, 
			     outgoing_invite_callback, ag,
			     NULL,
			     SIP_METHOD_INVITE,
			     NULL,
			     SIPTAG_SUBJECT_STR("Re-INVITE"),
			     SIPTAG_CONTACT(ag->ag_m_bob),
			     SIPTAG_SUPPORTED_STR("foo"),
			     SIPTAG_CONTENT_TYPE(c),
			     SIPTAG_PAYLOAD(sdp),
			     TAG_END()));
  nta_test_run(ag);
  TEST(ag->ag_status, 200);
  TEST(ag->ag_orq, NULL);
  TEST(ag->ag_latest_leg, ag->ag_alice_leg);

  nta_agent_set_params(ag->ag_agent, 
		       NTATAG_DEBUG_DROP_PROB(0),
		       TAG_END());

  /* Send BYE from Bob to Alice */
  old_leg = ag->ag_expect_leg = ag->ag_alice_leg;
  TEST_1(ag->ag_orq = 
	nta_outgoing_tcreate(ag->ag_bob_leg, outgoing_callback, ag,
			     NULL,
			     SIP_METHOD_BYE,
			     NULL,
			     SIPTAG_SUBJECT_STR("Hangup"),
			     SIPTAG_FROM(ag->ag_alice),
			     SIPTAG_TO(ag->ag_bob),
			     SIPTAG_CONTACT(ag->ag_m_alice),
			     SIPTAG_CONTENT_TYPE(c),
			     SIPTAG_PAYLOAD(sdp),
			     TAG_END()));

  nta_test_run(ag);
  TEST(ag->ag_status, 200);
  TEST(ag->ag_orq, NULL);
  TEST(ag->ag_latest_leg, old_leg);
  TEST(ag->ag_alice_leg, NULL);

  nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;
  ag->ag_latest_leg = NULL;
  ag->ag_call_leg = NULL;

  END();
}

/* ============================================================================ */
/* Test early dialogs, PRACK */

int test_for_ack_or_timeout(agent_t *ag,
			    nta_incoming_t *irq, 
			    sip_t const *sip)
{
  BEGIN();

  sip_method_t method = sip ? sip->sip_request->rq_method : sip_method_unknown;

  nta_incoming_destroy(irq);
  TEST(irq, ag->ag_irq);
  ag->ag_irq = NULL;

  if (sip) {
    TEST(method, sip_method_ack);
  
    ag->ag_status = 200;
  }
  else {
    if (ag->ag_bob_leg) {
      nta_leg_destroy(ag->ag_bob_leg), ag->ag_bob_leg = NULL;
    }
  }
  return 0;

  END();
}

2398
/* */
Pekka Pessi's avatar
Pekka Pessi committed
2399
int bob_leg_callback2(agent_t *ag,
2400 2401 2402
		      nta_leg_t *leg,
		      nta_incoming_t *irq,
		      sip_t const *sip)
Pekka Pessi's avatar
Pekka Pessi committed
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
{
  BEGIN();

  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s " URL_PRINT_FORMAT " %s\n",
	   name, __func__, sip->sip_request->rq_method_name, 
	   URL_PRINT_ARGS(sip->sip_request->rq_url),
	   sip->sip_request->rq_version);
  }

  TEST_1(sip->sip_content_length);
  TEST_1(sip->sip_via);
  TEST_1(sip->sip_from && sip->sip_from->a_tag);

  ag->ag_latest_leg = leg;

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

  if (ag->ag_bob_leg == NULL) {
    nta_leg_bind(leg, leg_callback_500, ag);
    ag->ag_bob_leg = nta_leg_tcreate(ag->ag_agent,
				     bob_leg_callback,
				     ag,
				     SIPTAG_CALL_ID(sip->sip_call_id),
				     SIPTAG_FROM(sip->sip_to),
				     SIPTAG_TO(sip->sip_from),
				     TAG_END());
    TEST_1(ag->ag_bob_leg);
2434
    TEST_1(nta_leg_tag(ag->ag_bob_leg, NULL));
Pekka Pessi's avatar
Pekka Pessi committed
2435
    TEST_1(nta_leg_get_tag(ag->ag_bob_leg));
2436
    TEST_1(nta_incoming_tag(irq, nta_leg_get_tag(ag->ag_bob_leg)));
Pekka Pessi's avatar
Pekka Pessi committed
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
    TEST(nta_leg_server_route(ag->ag_bob_leg, 
			      sip->sip_record_route, 
			      sip->sip_contact), 0);
  }

  if (sip->sip_request->rq_method == sip_method_invite) {
    nta_incoming_bind(irq, test_for_ack_or_timeout, ag); 
    nta_incoming_treply(irq,
			SIP_183_SESSION_PROGRESS,
			SIPTAG_CONTENT_TYPE(ag->ag_content_type),
			SIPTAG_PAYLOAD(ag->ag_payload),
			SIPTAG_CONTACT(ag->ag_m_bob),
			TAG_END());
2450 2451 2452 2453 2454 2455 2456
    if (0)
    nta_incoming_treply(irq,
			SIP_180_RINGING,
			SIPTAG_CONTENT_TYPE(ag->ag_content_type),
			SIPTAG_PAYLOAD(ag->ag_payload),
			SIPTAG_CONTACT(ag->ag_m_bob),
			TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
    nta_incoming_treply(irq,
			SIP_200_OK,
			SIPTAG_CONTACT(ag->ag_m_bob),
			TAG_END());
    ag->ag_irq = irq;
    return 0;
  } else {
    return 200;
  }

  END();
}

int invite_prack_callback(agent_t *ag,
2471 2472
			  nta_outgoing_t *orq,
			  sip_t const *sip)
Pekka Pessi's avatar
Pekka Pessi committed
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490
{
  BEGIN();

  int status = sip->sip_status->st_status;

  if (tstflags & tst_verbatim) {
    printf("%s: %s: %s %03d %s\n", name, __func__, 
	   sip->sip_status->st_version, 
	   sip->sip_status->st_status, 
	   sip->sip_status->st_phrase);
  }

  if (!ag->ag_call_tag && (status >= 200 || (status > 100 && sip->sip_rseq))) {
    nta_outgoing_t *tagged;
    TEST_1(sip->sip_to->a_tag);
    ag->ag_tag_status = status;
    ag->ag_call_tag = su_strdup(ag->ag_home, sip->sip_to->a_tag);
    TEST_S(ag->ag_call_tag, sip->sip_to->a_tag);
2491
    TEST_S(nta_leg_rtag(ag->ag_call_leg, ag->ag_call_tag), ag->ag_call_tag);
Pekka Pessi's avatar
Pekka Pessi committed
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
    TEST(nta_leg_client_route(ag->ag_call_leg, 
			      sip->sip_record_route,
			      sip->sip_contact), 0);
    tagged = nta_outgoing_tagged(orq, 
				 invite_prack_callback,
				 ag,
				 ag->ag_call_tag,
				 sip->sip_rseq);
    TEST_1(tagged);
    nta_outgoing_destroy(orq);
2502 2503
    if (ag->ag_orq == orq)
      ag->ag_orq = tagged;
Pekka Pessi's avatar
Pekka Pessi committed
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522
    orq = tagged;
  }

  if (status > 100 && status < 200 && sip->sip_rseq) {
    nta_outgoing_t *prack;
    prack = nta_outgoing_prack(ag->ag_call_leg, orq, NULL, NULL,
			       NULL,
			       sip, 
			       TAG_END());
    TEST_1(prack);
    nta_outgoing_destroy(prack);
    return 0;
  }

  if (status < 200)
    return 0;

  if (status < 300) {
    nta_outgoing_t *ack;
Pekka Pessi's avatar
Pekka Pessi committed
2523 2524 2525
    msg_t *msg;
    sip_t *osip;

2526
    TEST_1(msg = nta_outgoing_getrequest(orq));
Pekka Pessi's avatar
Pekka Pessi committed
2527
    TEST_1(osip = sip_object(msg));
Pekka Pessi's avatar
Pekka Pessi committed
2528

2529
    TEST_1(nta_leg_rtag(ag->ag_call_leg, sip->sip_to->a_tag));
Pekka Pessi's avatar
Pekka Pessi committed
2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
    
    TEST(nta_leg_client_route(ag->ag_call_leg, 
			      sip->sip_record_route,
			      sip->sip_contact), 0);

    ack = nta_outgoing_tcreate(ag->ag_call_leg, NULL, NULL,
			       NULL,
			       SIP_METHOD_ACK,
			       NULL,
			       SIPTAG_CSEQ(sip->sip_cseq),
Pekka Pessi's avatar
Pekka Pessi committed
2540
			       NTATAG_ACK_BRANCH(osip->sip_via->v_branch),
Pekka Pessi's avatar
Pekka Pessi committed
2541 2542 2543
			       TAG_END());
    TEST_1(ack);
    nta_outgoing_destroy(ack);
2544
    msg_destroy(msg);
Pekka Pessi's avatar
Pekka Pessi committed
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601
  }
  else {
    ag->ag_status = status;
  }

  TEST_1(sip->sip_to && sip->sip_to->a_tag);

  nta_outgoing_destroy(orq);
  ag->ag_orq = NULL;
  ag->ag_call_leg = NULL;

  END();
}


/*
 * Test establishing a call with an early dialog / 100 rel / timeout
 *
 * Alice sends a INVITE to Bob, then Bob sends 183, Alice sends PRACK,
 * Bob sends 200 to PRACK, Bob sends 200 to INVITE.
 * Bob sends BYE, Alice 200.
 */

int test_prack(agent_t *ag)
{
  sip_content_type_t *c = ag->ag_content_type;
  sip_payload_t      *sdp = ag->ag_payload;
  nta_leg_t *old_leg;

  BEGIN();

  {
    /* Send a PRACK from default leg, NTA responds to it with error */
    url_t url[1];

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

    ag->ag_expect_leg = ag->ag_server_leg;
    ag->ag_latest_leg = NULL;
    TEST_1(ag->ag_orq = 
	  nta_outgoing_tcreate(ag->ag_default_leg, outgoing_callback, ag,
			       ag->ag_obp,
			       SIP_METHOD_PRACK,
			       (url_string_t *)url,
			       SIPTAG_SUBJECT_STR("Test 1.1"),
			       SIPTAG_FROM(ag->ag_alice),
			       SIPTAG_TO(ag->ag_bob),
			       SIPTAG_CONTACT(ag->ag_m_alice),
			       SIPTAG_RACK_STR("1432432 42332432 INVITE"),
			       TAG_END()));
    nta_test_run(ag);
    TEST(ag->ag_status, 481);
    TEST(ag->ag_orq, NULL);
    TEST(ag->ag_latest_leg, NULL);
  }

2602
  {
Pekka Pessi's avatar
Pekka Pessi committed
2603 2604 2605 2606 2607 2608
  TEST_1(ag->ag_alice_leg = nta_leg_tcreate(ag->ag_agent, 
					   alice_leg_callback,
					   ag,
					   SIPTAG_FROM(ag->ag_alice),
					   SIPTAG_TO(ag->ag_bob),
					   TAG_END()));
2609
  TEST_1(nta_leg_tag(ag->ag_alice_leg, NULL));
Pekka Pessi's avatar
Pekka Pessi committed
2610 2611 2612 2613 2614

  /* Send INVITE */
  nta_leg_bind(ag->ag_server_leg, bob_leg_callback2, ag);
  ag->ag_expect_leg = ag->ag_server_leg;
  TEST_1(ag->ag_orq =