test_tport.c 42.3 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3
/*
 * This file is part of the Sofia-SIP package
 *
4
 * Copyright (C) 2006 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
5 6 7
 *
 * 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 26 27 28 29 30 31 32 33 34 35
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE tport_test.c
 *
 * Test functions for transports
 *
 * @internal
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Apr  3 11:25:13 2002 ppessi
 */

36 37 38
/* always assert()s */
#undef NDEBUG

Pekka Pessi's avatar
Pekka Pessi committed
39 40 41 42 43 44 45 46 47 48 49 50
#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <assert.h>

typedef struct tp_test_s tp_test_t;

#define TP_STACK_T tp_test_t
51
#define TP_CLIENT_T struct called
52

53 54
#include <sofia-sip/su_wait.h>
#include <sofia-sip/su_md5.h>
55
#include <sofia-sip/su_localinfo.h>
Pekka Pessi's avatar
Pekka Pessi committed
56

57 58
#include "tport_internal.h"	/* Get SU_DEBUG_*() */

59 60
#include "test_class.h"
#include "test_protos.h"
61 62 63
#include "sofia-sip/msg.h"
#include "sofia-sip/msg_mclass.h"
#include "sofia-sip/msg_addr.h"
Pekka Pessi's avatar
Pekka Pessi committed
64 65 66 67 68

#if HAVE_SIGCOMP
#include <sigcomp.h>
#endif

69
#include <sofia-sip/base64.h>
Pekka Pessi's avatar
Pekka Pessi committed
70

71
#include <sofia-sip/su_log.h>
Pekka Pessi's avatar
Pekka Pessi committed
72

73
#include "sofia-sip/tport.h"
Pekka Pessi's avatar
Pekka Pessi committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

struct tp_test_s {
  su_home_t  tt_home[1];
  int        tt_flags;
  su_root_t *tt_root;
  msg_mclass_t *tt_mclass;
  tport_t   *tt_srv_tports;
  tport_t   *tt_tports;

  tport_t   *tt_rtport;

  tp_name_t tt_udp_name[1];
  tp_name_t tt_udp_comp[1];

  tp_name_t tt_tcp_name[1];
  tp_name_t tt_tcp_comp[1];

  tp_name_t tt_sctp_name[1];
  tp_name_t tt_sctp_comp[1];

  tp_name_t tt_tls_name[1];
  tp_name_t tt_tls_comp[1];

#if HAVE_SIGCOMP
  struct sigcomp_state_handler *state_handler;
  struct sigcomp_algorithm const *algorithm;
  struct sigcomp_compartment *master_cc;

#define IF_SIGCOMP_TPTAG_COMPARTMENT(cc) TAG_IF(cc, TPTAG_COMPARTMENT(cc)),
#else
#define IF_SIGCOMP_TPTAG_COMPARTMENT(cc)
#endif

  int        tt_status;
  int        tt_received;
  msg_t     *tt_rmsg;
  uint8_t    tt_digest[SU_MD5_DIGEST_SIZE];
111 112 113

  su_addrinfo_t const *tt_tcp_addr;
  tport_t   *tt_tcp;
Pekka Pessi's avatar
Pekka Pessi committed
114 115
};

116 117
int tstflags;
#define TSTFLAGS tstflags
Pekka Pessi's avatar
Pekka Pessi committed
118

119
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
120 121 122

char const name[] = "tport_test";

Pekka Pessi's avatar
Pekka Pessi committed
123
SOFIAPUBVAR su_log_t tport_log[];
Pekka Pessi's avatar
Pekka Pessi committed
124 125 126

static int name_test(tp_test_t *tt)
{
127 128 129 130 131 132
  tp_name_t tpn[1];

  su_home_t home[1] = { SU_HOME_INIT(home) };

  su_sockaddr_t su[1];

Pekka Pessi's avatar
Pekka Pessi committed
133 134
  BEGIN();

135 136 137 138 139 140 141 142 143 144 145 146
  memset(su, 0, sizeof su);

  su->su_port = htons(5060);
  su->su_family = AF_INET;

  TEST(tport_convert_addr(home, tpn, "tcp", "localhost", su), 0);

  su->su_family = AF_INET;

  TEST(tport_convert_addr(home, tpn, "tcp", "localhost", su), 0);

#if SU_HAVE_IN6
147
  su->su_family = AF_INET6;
148 149 150
  TEST(tport_convert_addr(home, tpn, "tcp", "localhost", su), 0);
#endif

Pekka Pessi's avatar
Pekka Pessi committed
151 152 153 154 155 156 157 158 159 160 161
  END();
}

/* Count number of transports in chain */
static
int count_tports(tport_t *tp)
{
  int n = 0;

  for (tp = tport_primaries(tp); tp; tp = tport_next(tp))
    n++;
162

Pekka Pessi's avatar
Pekka Pessi committed
163 164 165 166 167 168 169
  return n;
}

static int check_msg(tp_test_t *tt, msg_t *msg, char const *ident)
{
  msg_test_t *tst;
  msg_payload_t *pl;
170
  usize_t i, len;
Pekka Pessi's avatar
Pekka Pessi committed
171 172

  BEGIN();
173

174
  TEST_1(tst = msg_test_public(msg));
Pekka Pessi's avatar
Pekka Pessi committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  TEST_1(pl = tst->msg_payload);

  if (ident) {
    if (!tst->msg_content_location ||
	strcmp(ident, tst->msg_content_location->g_string))
      return 1;
  }

  len = pl->pl_len;

  for (i = 0; i < len; i++) {
    if (pl->pl_data[i] != (char) (i % 240))
      break;
  }

190
  if (pl)
Pekka Pessi's avatar
Pekka Pessi committed
191 192 193 194 195 196 197 198 199 200
  return i != len;

  END();
}

static int test_create_md5(tp_test_t *tt, msg_t *msg)
{
  msg_test_t *tst;
  msg_payload_t *pl;
  su_md5_t md5[1];
201

Pekka Pessi's avatar
Pekka Pessi committed
202 203
  BEGIN();

204
  TEST_1(tst = msg_test_public(msg));
Pekka Pessi's avatar
Pekka Pessi committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  TEST_1(pl = tst->msg_payload);

  su_md5_init(md5);
  su_md5_update(md5, pl->pl_data, pl->pl_len);
  su_md5_digest(md5, tt->tt_digest);

  END();
}

static int test_check_md5(tp_test_t *tt, msg_t *msg)
{
  msg_test_t *tst;
  msg_payload_t *pl;
  su_md5_t md5[1];
  uint8_t digest[SU_MD5_DIGEST_SIZE];

  BEGIN();

223
  TEST_1(tst = msg_test_public(msg));
Pekka Pessi's avatar
Pekka Pessi committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  TEST_1(pl = tst->msg_payload);

  su_md5_init(md5);
  su_md5_update(md5, pl->pl_data, pl->pl_len);
  su_md5_digest(md5, digest);

  TEST(memcmp(digest, tt->tt_digest, sizeof digest), 0);

  END();
}

static int test_msg_md5(tp_test_t *tt, msg_t *msg)
{
  msg_test_t *tst;

  BEGIN();

241
  TEST_1(tst = msg_test_public(msg));
Pekka Pessi's avatar
Pekka Pessi committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

  if (tst->msg_content_md5) {
    su_md5_t md5sum[1];
    uint8_t digest[SU_MD5_DIGEST_SIZE];
    char b64[BASE64_SIZE(SU_MD5_DIGEST_SIZE) + 1];

    msg_payload_t *pl =tst->msg_payload;

    su_md5_init(md5sum);
    su_md5_update(md5sum, pl->pl_data, pl->pl_len);
    su_md5_digest(md5sum, digest);

    base64_e(b64, sizeof(b64), digest, sizeof(digest));

    if (strcmp(b64, tst->msg_content_md5->g_string)) {
      ;
    }
259

Pekka Pessi's avatar
Pekka Pessi committed
260 261 262 263 264 265 266 267
    TEST_S(b64, tst->msg_content_md5->g_string);
  } else {
    TEST_1(tst->msg_content_md5);
  }

  END();
}

268
#define TPORT_TEST_VERSION MSG_TEST_VERSION_CURRENT
269

270
static int new_test_msg(tp_test_t *tt, msg_t **retval,
Pekka Pessi's avatar
Pekka Pessi committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
			char const *ident,
			int N, int len)
{
  msg_t *msg;
  msg_test_t *tst;
  su_home_t *home;
  msg_request_t *rq;
  msg_unknown_t *u;
  msg_content_location_t *cl;
  msg_content_md5_t *md5;
  msg_content_length_t *l;
  msg_separator_t *sep;
  msg_payload_t payload[1];
  msg_header_t *h;
  int i;

  su_md5_t md5sum[1];
  uint8_t digest[SU_MD5_DIGEST_SIZE];
  char b64[BASE64_SIZE(SU_MD5_DIGEST_SIZE) + 1];

  BEGIN();

  TEST_1(msg = msg_create(tt->tt_mclass, 0));
294
  TEST_1(tst = msg_test_public(msg));
Pekka Pessi's avatar
Pekka Pessi committed
295 296
  TEST_1(home = msg_home(msg));

297
  TEST_SIZE(msg_maxsize(msg, 1024 + N * len), 0);
Pekka Pessi's avatar
Pekka Pessi committed
298

299
  TEST_1(rq = msg_request_make(home, "DO im:foo@faa " TPORT_TEST_VERSION));
300
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)rq), 0);
Pekka Pessi's avatar
Pekka Pessi committed
301 302

  TEST_1(u = msg_unknown_make(home, "Foo: faa"));
303
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)u), 0);
Pekka Pessi's avatar
Pekka Pessi committed
304 305

  TEST_1(u = msg_unknown_make(home, "Foo: faa"));
306
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)u), 0);
Pekka Pessi's avatar
Pekka Pessi committed
307

308 309 310 311
  if (ident) {
    TEST_1(cl = msg_content_location_make(home, ident));
    TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)cl), 0);
  }
Pekka Pessi's avatar
Pekka Pessi committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

  msg_payload_init(payload);

  payload->pl_len = len;
  TEST_1(payload->pl_data = su_zalloc(home, payload->pl_len));

  for (i = 0; i < len; i++) {
    payload->pl_data[i] = (char) (i % 240);
  }

  su_md5_init(md5sum);

  for (i = 0; i < N; i++) {
    h = msg_header_dup(home, (msg_header_t*)payload);
    TEST_1(h);
327
    TEST(msg_header_insert(msg, (void *)tst, h), 0);
Pekka Pessi's avatar
Pekka Pessi committed
328 329 330
    su_md5_update(md5sum, payload->pl_data, payload->pl_len);
  }

331
  TEST_1(l = msg_content_length_format(home, MOD_ZU, (size_t)(N * payload->pl_len)));
332
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)l), 0);
Pekka Pessi's avatar
Pekka Pessi committed
333 334 335 336 337 338

  su_md5_digest(md5sum, digest);

  base64_e(b64, sizeof(b64), digest, sizeof(digest));

  TEST_1(md5 = msg_content_md5_make(home, b64));
339
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)md5), 0);
340

Pekka Pessi's avatar
Pekka Pessi committed
341
  TEST_1(sep = msg_separator_create(home));
342
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)sep), 0);
Pekka Pessi's avatar
Pekka Pessi committed
343

344
  TEST(msg_serialize(msg, (void *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
345 346 347 348 349 350 351 352

  *retval = msg;

  END();
}

static
struct sigcomp_compartment *
353
test_sigcomp_compartment(tp_test_t *tt, tport_t *tp, tp_name_t const *tpn);
Pekka Pessi's avatar
Pekka Pessi committed
354 355 356 357 358 359 360 361 362 363

static void tp_test_recv(tp_test_t *tt,
			 tport_t *tp,
			 msg_t *msg,
			 tp_magic_t *magic,
			 su_time_t now)
{
  tp_name_t frm[1];

  if (tport_delivered_from(tp, msg, frm) != -1 && frm->tpn_comp) {
364
    struct sigcomp_compartment *cc = test_sigcomp_compartment(tt, tp, frm);
365

Pekka Pessi's avatar
Pekka Pessi committed
366 367 368 369 370 371
    tport_sigcomp_accept(tp, cc, msg);
  }

  tt->tt_status = 1;
  tt->tt_received++;

372 373 374 375 376
  if (msg_has_error(msg)) {
    tt->tt_status = -1;
    tt->tt_rtport = tp;
  }
  else if (test_msg_md5(tt, msg))
Pekka Pessi's avatar
Pekka Pessi committed
377
    msg_destroy(msg);
378
  else if (tt->tt_rmsg)
Pekka Pessi's avatar
Pekka Pessi committed
379 380 381 382 383 384 385 386 387 388 389 390 391
    msg_destroy(msg);
  else {
    tt->tt_rmsg = msg;
    tt->tt_rtport = tp;
  }
}

static void tp_test_error(tp_test_t *tt,
			  tport_t *tp,
			  int errcode,
			  char const *remote)
{
  tt->tt_status = -1;
392 393
  fprintf(stderr, "tp_test_error(%p): error %d (%s) from %s\n",
	  (void *)tp, errcode, su_strerror(errcode),
Pekka Pessi's avatar
Pekka Pessi committed
394 395 396 397
	  remote ? remote : "<unknown destination>");
}

msg_t *tp_test_msg(tp_test_t *tt, int flags,
398
		   char const data[], usize_t size,
399
		   tport_t const *tp,
Pekka Pessi's avatar
Pekka Pessi committed
400 401 402 403 404 405 406 407 408 409 410 411
		   tp_client_t *tpc)
{
  msg_t *msg = msg_create(tt->tt_mclass, flags);

  msg_maxsize(msg, 2 * 1024 * 1024);

  return msg;
}


static
struct sigcomp_compartment *
412 413
test_sigcomp_compartment(tp_test_t *tt,
			 tport_t *tp,
414
			 tp_name_t const *tpn)
Pekka Pessi's avatar
Pekka Pessi committed
415 416 417 418 419
{
  struct sigcomp_compartment *cc = NULL;
#if HAVE_SIGCOMP
  char name[256];
  int namesize;
420

Pekka Pessi's avatar
Pekka Pessi committed
421 422 423 424 425 426 427 428
  namesize = snprintf(name, sizeof name, "TEST_%s/%s:%s",
		     tpn->tpn_proto,
		     tpn->tpn_host,
		     tpn->tpn_port);

  if (namesize <= 0 || namesize >= sizeof name)
    return NULL;

429
  cc = sigcomp_compartment_access(tt->state_handler,
Pekka Pessi's avatar
Pekka Pessi committed
430 431 432
				  0, name, namesize, NULL, 0);

  if (cc == NULL) {
433
    cc = sigcomp_compartment_create(tt->algorithm, tt->state_handler,
Pekka Pessi's avatar
Pekka Pessi committed
434 435 436 437 438 439 440 441 442 443
				    0, name, namesize, NULL, 0);

    sigcomp_compartment_option(cc, "dms=32768");
  }
#endif

  return cc;
}

/* Accept/reject early SigComp message */
444
int test_sigcomp_accept(tp_stack_t *tt, tport_t *tp, msg_t *msg)
Pekka Pessi's avatar
Pekka Pessi committed
445 446 447
{
  struct sigcomp_compartment *cc = NULL;

448
  cc = test_sigcomp_compartment(tt, tp, tport_name(tp));
Pekka Pessi's avatar
Pekka Pessi committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

  if (cc)
    tport_sigcomp_assign(tp, cc);

  return tport_sigcomp_accept(tp, cc, msg);
}


tp_stack_class_t const tp_test_class[1] =
  {{
      /* tpac_size */ sizeof(tp_test_class),
      /* tpac_recv */  tp_test_recv,
      /* tpac_error */ tp_test_error,
      /* tpac_alloc */ tp_test_msg,
  }};

static int init_test(tp_test_t *tt)
{
  tp_name_t myname[1] = {{ "*", "*", "*", "*", "sigcomp" }};
468
#if HAVE_SCTP
Pekka Pessi's avatar
Pekka Pessi committed
469
  char const * transports[] = { "udp", "tcp", "sctp", NULL };
Martti Mela's avatar
Martti Mela committed
470 471 472
#else
  char const * transports[] = { "udp", "tcp", NULL };
#endif
Pekka Pessi's avatar
Pekka Pessi committed
473 474
  tp_name_t const *tpn;
  tport_t *tp;
475
  unsigned idle;
476
  int logging = -1;
Pekka Pessi's avatar
Pekka Pessi committed
477 478 479

  BEGIN();

Pekka Pessi's avatar
Pekka Pessi committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  int mask = AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST;

#ifdef AI_ALL
  mask |= AI_ALL;
#endif
#ifdef AI_V4MAPPED_CFG
  mask |= AI_V4MAPPED_CFG;
#endif
#ifdef AI_ADDRCONFIG
  mask |= AI_ADDRCONFIG;
#endif
#ifdef AI_V4MAPPED
  mask |= AI_V4MAPPED;
#endif

  /* Test that we have no common flags with underlying getaddrinfo() */
  TEST(mask & TP_AI_MASK, 0);

Pekka Pessi's avatar
Pekka Pessi committed
498 499 500 501 502 503 504 505 506 507 508 509 510
  TEST_1(tt->tt_root = su_root_create(NULL));

  myname->tpn_host = "127.0.0.1";
  myname->tpn_ident = "client";

  /* Create message class */
  TEST_1(tt->tt_mclass = msg_mclass_clone(msg_test_mclass, 0, 0));

  /* Try to insert Content-Length header (expecting failure) */
  TEST(msg_mclass_insert(tt->tt_mclass, msg_content_length_href), -1);

#if HAVE_SIGCOMP
  TEST_1(tt->state_handler = sigcomp_state_handler_create());
511
  TEST_1(tt->algorithm =
Pekka Pessi's avatar
Pekka Pessi committed
512
	 sigcomp_algorithm_by_name(getenv("SIGCOMP_ALGORITHM")));
513 514
  TEST_1(tt->master_cc =
	 sigcomp_compartment_create(tt->algorithm, tt->state_handler,
Pekka Pessi's avatar
Pekka Pessi committed
515 516 517 518 519
				    0, "", 0, NULL, 0));
  TEST(sigcomp_compartment_option(tt->master_cc, "stateless"), 1);
#endif

  /* Create client transport */
520
  TEST_1(tt->tt_tports =
Pekka Pessi's avatar
Pekka Pessi committed
521 522 523 524 525 526
	 tport_tcreate(tt, tp_test_class, tt->tt_root,
		       IF_SIGCOMP_TPTAG_COMPARTMENT(tt->master_cc)
		       TAG_END()));

  /* Bind client transports */
  TEST(tport_tbind(tt->tt_tports, myname, transports,
527
		   TPTAG_SERVER(0), TAG_END()),
Pekka Pessi's avatar
Pekka Pessi committed
528 529 530 531 532 533 534 535 536 537 538 539 540
       0);

  if (getenv("TPORT_TEST_HOST"))
    myname->tpn_host = getenv("TPORT_TEST_HOST");
  else
    myname->tpn_host = "*";

  if (getenv("TPORT_TEST_PORT"))
    myname->tpn_port = getenv("TPORT_TEST_PORT");

  myname->tpn_ident = "server";

  /* Create server transport */
541
  TEST_1(tt->tt_srv_tports =
Pekka Pessi's avatar
Pekka Pessi committed
542 543 544 545 546
	 tport_tcreate(tt, tp_test_class, tt->tt_root,
		       IF_SIGCOMP_TPTAG_COMPARTMENT(tt->master_cc)
		       TAG_END()));

  /* Bind server transports */
547
  TEST(tport_tbind(tt->tt_srv_tports, myname, transports,
Pekka Pessi's avatar
Pekka Pessi committed
548
		   TPTAG_SERVER(1),
549
		   TAG_END()),
Pekka Pessi's avatar
Pekka Pessi committed
550 551
       0);

552
  /* Check that the master transport has idle parameter */
553 554 555 556
  TEST(tport_get_params(tt->tt_srv_tports,
			TPTAG_IDLE_REF(idle),
			TAG_END()), 1);

557 558 559 560 561 562 563 564 565
  /* Check that logging tag works */
  TEST(tport_get_params(tt->tt_srv_tports,
			TPTAG_LOG_REF(logging),
			TAG_END()), 1);
  TEST(tport_set_params(tt->tt_srv_tports,
			TPTAG_LOG(logging),
			TAG_END()), 1);


Pekka Pessi's avatar
Pekka Pessi committed
566 567 568 569
  for (tp = tport_primaries(tt->tt_srv_tports); tp; tp = tport_next(tp))
    TEST_S(tport_name(tp)->tpn_ident, "server");

  {
570
    su_sockaddr_t *su;
Pekka Pessi's avatar
Pekka Pessi committed
571 572 573 574 575 576
    socklen_t sulen;
    int s;
    int i, before, after;
    char port[8];

    tp_name_t rname[1];
577
    su_localinfo_t *li, hints[1] = {{ 0 }};
Pekka Pessi's avatar
Pekka Pessi committed
578 579 580

    *rname = *myname;

581 582
    /* Check that we cannot bind to an already used socket */

583 584 585 586 587 588 589
    /* Windows allows concurrent binding to a wildcard */
    memset(hints, 0, sizeof hints);
    if (strcmp(rname->tpn_host, "*"))
      hints->li_canonname = (char *) rname->tpn_host;
    TEST_1(su_getlocalinfo(hints, &li) == 0);
    su = li->li_addr, sulen = li->li_addrlen;

590
    s = su_socket(su->su_family = AF_INET, SOCK_STREAM, 0); TEST_1(s != -1);
Pekka Pessi's avatar
Pekka Pessi committed
591 592 593 594 595 596 597 598
    TEST_1(bind(s, &su->su_sa, sulen) != -1);
    TEST_1(listen(s, 5) != -1);
    TEST_1(getsockname(s, &su->su_sa, &sulen) != -1);

    sprintf(port, "%u", ntohs(su->su_port));

    rname->tpn_port = port;
    rname->tpn_ident = "failure";
599

Pekka Pessi's avatar
Pekka Pessi committed
600 601
    before = count_tports(tt->tt_srv_tports);

602
    /* Bind server transports to an reserved port - this should fail */
603
    TEST(tport_tbind(tt->tt_srv_tports, rname, transports,
Pekka Pessi's avatar
Pekka Pessi committed
604
		     TPTAG_SERVER(1),
605
		     TAG_END()),
Pekka Pessi's avatar
Pekka Pessi committed
606 607 608 609
	 -1);

    after = count_tports(tt->tt_srv_tports);

610 611
    su_freelocalinfo(li);

612
    /* Check that no new primary transports has been added by failed call */
Pekka Pessi's avatar
Pekka Pessi committed
613 614
    TEST(before, after);

615 616
    /* Add new transports to an ephemeral port with new identity */

Pekka Pessi's avatar
Pekka Pessi committed
617 618 619 620 621 622 623
    for (tp = tport_primaries(tt->tt_srv_tports); tp; tp = tport_next(tp))
      TEST_S(tport_name(tp)->tpn_ident, "server");

    rname->tpn_port = "*";
    rname->tpn_ident = "server2";

    /* Bind server transports to another port */
624
    TEST(tport_tbind(tt->tt_srv_tports, rname, transports,
Pekka Pessi's avatar
Pekka Pessi committed
625
		     TPTAG_SERVER(1),
626
		     TAG_END()),
Pekka Pessi's avatar
Pekka Pessi committed
627 628
	 0);

629 630 631 632
    /* Check that new transports are after old ones. */
    for (i = 0, tp = tport_primaries(tt->tt_srv_tports);
	 i < before;
	 i++, tp = tport_next(tp))
Pekka Pessi's avatar
Pekka Pessi committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
      TEST_S(tport_name(tp)->tpn_ident, "server");

    for (; tp; tp = tport_next(tp))
      TEST_S(tport_name(tp)->tpn_ident, "server2");
  }

#if HAVE_TLS
  {
    tp_name_t tlsname[1] = {{ "tls", "*", "*", "*", NULL }};
    char const * transports[] = { "tls", NULL };

    char const *srcdir = getenv("srcdir");

    if (srcdir == NULL)
      srcdir = ".";

    tlsname->tpn_host = myname->tpn_host;
    tlsname->tpn_ident = "server";

    /* Bind client transports */
    TEST(tport_tbind(tt->tt_tports, tlsname, transports,
654
		     TPTAG_SERVER(0),
Pekka Pessi's avatar
Pekka Pessi committed
655
		     TPTAG_CERTIFICATE(srcdir),
656
		     TAG_END()),
Pekka Pessi's avatar
Pekka Pessi committed
657 658 659
	 0);

    /* Bind tls server transport */
660
    TEST(tport_tbind(tt->tt_srv_tports, tlsname, transports,
Pekka Pessi's avatar
Pekka Pessi committed
661 662
		     TPTAG_SERVER(1),
		     TPTAG_CERTIFICATE(srcdir),
663
		     TAG_END()),
Pekka Pessi's avatar
Pekka Pessi committed
664 665 666 667 668 669 670
	 0);
  }
#endif

  for (tp = tport_primaries(tt->tt_srv_tports); tp; tp = tport_next(tp)) {
    TEST_1(tpn = tport_name(tp));

671
    if (tt->tt_flags & tst_verbatim) {
672 673
      char const *host = tpn->tpn_host != tpn->tpn_canon ? tpn->tpn_host : "";
      printf("bound transport to %s/%s:%s%s%s%s%s\n",
674
	     tpn->tpn_proto, tpn->tpn_canon, tpn->tpn_port,
675
	     host[0] ? ";maddr=" : "", host,
676
	     tpn->tpn_comp ? ";comp=" : "",
Pekka Pessi's avatar
Pekka Pessi committed
677
	     tpn->tpn_comp ? tpn->tpn_comp : "");
678
    }
Pekka Pessi's avatar
Pekka Pessi committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

    /* Ignore server2 tports for now */
    if (strcmp(tpn->tpn_ident, "server"))
      continue;

    if (strcmp(tpn->tpn_proto, "udp") == 0) {
      *tt->tt_udp_name = *tpn;
      tt->tt_udp_name->tpn_comp = NULL;
      tt->tt_udp_name->tpn_ident = NULL;
      *tt->tt_udp_comp = *tpn;
      tt->tt_udp_comp->tpn_ident = NULL;
    }
    else if (strcmp(tpn->tpn_proto, "tcp") == 0) {
      *tt->tt_tcp_name = *tpn;
      tt->tt_tcp_name->tpn_comp = NULL;
      tt->tt_tcp_name->tpn_ident = NULL;
      *tt->tt_tcp_comp = *tpn;
      tt->tt_tcp_comp->tpn_ident = NULL;
697 698

      if (tt->tt_tcp_addr == NULL) {
699
	tt->tt_tcp_addr = tport_get_address(tp);
700 701
	tt->tt_tcp = tp;
      }
702
    }
Pekka Pessi's avatar
Pekka Pessi committed
703 704 705 706 707 708 709 710 711 712 713 714 715
    else if (strcmp(tpn->tpn_proto, "sctp") == 0) {
      *tt->tt_sctp_name = *tpn;
      tt->tt_sctp_name->tpn_ident = NULL;
    }
    else if (strcmp(tpn->tpn_proto, "tls") == 0) {
      *tt->tt_tls_name = *tpn;
      tt->tt_tls_name->tpn_ident = NULL;
    }
  }

  END();
}

716
char const payload[] =
Pekka Pessi's avatar
Pekka Pessi committed
717 718 719 720 721
"Some data\n"
"More data\n";

#include <time.h>

722
int
Pekka Pessi's avatar
Pekka Pessi committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
tport_test_run(tp_test_t *tt, unsigned timeout)
{
  time_t now = time(NULL);

  tt->tt_status = 0;

  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
  tt->tt_rtport = NULL;

  while (!tt->tt_status) {
    if (tt->tt_flags & tst_verbatim) {
      fputs(".", stdout); fflush(stdout);
    }
    su_root_step(tt->tt_root, 500L);

738
    if (!getenv("TPORT_TEST_DEBUG") &&
739
	time(NULL) > (time_t)(now + timeout))
Pekka Pessi's avatar
Pekka Pessi committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
      return 0;
  }

  return tt->tt_status;
}

static int udp_test(tp_test_t *tt)
{
  tport_t *tp;
  msg_t *msg;
  msg_test_t *tst;
  su_home_t *home;
  msg_request_t *rq;
  msg_unknown_t *u;
  msg_content_length_t *l;
  msg_content_md5_t *md5;
  msg_separator_t *sep;
  msg_payload_t *pl;

  BEGIN();

  TEST_1(msg = msg_create(tt->tt_mclass, 0));
762
  TEST_1(tst = msg_test_public(msg));
Pekka Pessi's avatar
Pekka Pessi committed
763 764
  TEST_1(home = msg_home(msg));

765
  TEST_1(rq = msg_request_make(home, "DO im:foo@faa " TPORT_TEST_VERSION));
766
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)rq), 0);
Pekka Pessi's avatar
Pekka Pessi committed
767 768

  TEST_1(u = msg_unknown_make(home, "Foo: faa"));
769
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)u), 0);
Pekka Pessi's avatar
Pekka Pessi committed
770 771

  TEST_1(pl = msg_payload_make(home, payload));
772
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)pl), 0);
Pekka Pessi's avatar
Pekka Pessi committed
773

774
  TEST_1(l = msg_content_length_format(home, MOD_ZU, (size_t)pl->pl_len));
775
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)l), 0);
Pekka Pessi's avatar
Pekka Pessi committed
776 777

  TEST_1(md5 = msg_content_md5_make(home, "R6nitdrtJFpxYzrPaSXfrA=="));
778
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)md5), 0);
779

Pekka Pessi's avatar
Pekka Pessi committed
780
  TEST_1(sep = msg_separator_create(home));
781
  TEST(msg_header_insert(msg, (void *)tst, (msg_header_t *)sep), 0);
Pekka Pessi's avatar
Pekka Pessi committed
782

783
  TEST(msg_serialize(msg, (void *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818

  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_udp_name, TAG_END()));

  TEST_S(tport_name(tp)->tpn_ident, "client");

  TEST(tport_test_run(tt, 5), 1);

  msg_destroy(msg);

#if 0
  tp_name_t tpn[1] = {{ NULL }};

  TEST_1(msg = tt->tt_rmsg); tt->tt_rmsg = NULL;

  TEST_1(home = msg_home(msg));

  TEST_1(tport_convert_addr(home, tpn, "udp", NULL, msg_addr(msg)) == 0);

  tpn->tpn_comp = tport_name(tt->tt_rtport)->tpn_comp;

  /* reply */
  TEST_1(tport_tsend(tt->tt_rtport, msg, tpn, TAG_END()) != NULL);

  msg_destroy(msg);

  TEST(tport_test_run(tt, 5), 1);

  msg_destroy(msg);
#endif

  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

  END();
}

819 820 821
int pending_server_close, pending_client_close;

void server_closed_callback(tp_stack_t *tt, tp_client_t *client,
822
			    tport_t *tp, msg_t *msg, int error)
823 824 825 826 827 828 829 830 831 832
{
  assert(msg == NULL);
  assert(client == NULL);
  if (msg == NULL) {
    tport_release(tp, pending_server_close, NULL, NULL, client, 0);
    pending_server_close = 0;
  }
}

void client_closed_callback(tp_stack_t *tt, tp_client_t *client,
833
			    tport_t *tp, msg_t *msg, int error)
834 835 836 837 838 839 840 841 842
{
  assert(msg == NULL);
  assert(client == NULL);
  if (msg == NULL) {
    tport_release(tp, pending_client_close, NULL, NULL, client, 0);
    pending_client_close = 0;
  }
}

Pekka Pessi's avatar
Pekka Pessi committed
843 844
static int tcp_test(tp_test_t *tt)
{
845 846
  BEGIN();

Pekka Pessi's avatar
Pekka Pessi committed
847
  msg_t *msg = NULL;
848
  int i, N;
Pekka Pessi's avatar
Pekka Pessi committed
849 850
  tport_t *tp, *tp0;
  char ident[16];
851 852 853 854 855 856 857 858 859 860
  su_time_t started;

  /* Send a single message */
  TEST_1(!new_test_msg(tt, &msg, "tcp-first", 1, 1024));
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END()));
  TEST_S(tport_name(tp)->tpn_ident, "client");
  tp0 = tport_incref(tp);
  msg_destroy(msg);

  tport_set_params(tp,
861
       	    TPTAG_KEEPALIVE(100),
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
       	    TPTAG_PINGPONG(500),
       	    TPTAG_IDLE(500),
       	    TAG_END());

  TEST(tport_test_run(tt, 5), 1);
  TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-first"));
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

  /* Ask for notification upon close */
  pending_client_close = tport_pend(tp0, NULL, client_closed_callback, NULL);
  TEST_1(pending_client_close > 0);
  tp = tt->tt_rtport;
  pending_server_close = tport_pend(tp, NULL, server_closed_callback, NULL);
  TEST_1(pending_server_close > 0);

877 878
  N = 0; tt->tt_received = 0;

879
#ifndef WIN32			/* Windows seems to be buffering too much */
Pekka Pessi's avatar
Pekka Pessi committed
880 881 882 883 884

  /* Create a large message, just to force queueing in sending end */
  TEST(new_test_msg(tt, &msg, "tcp-0", 1, 16 * 64 * 1024), 0);
  test_create_md5(tt, msg);
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END()));
885
  N++;
Pekka Pessi's avatar
Pekka Pessi committed
886
  TEST_S(tport_name(tp)->tpn_ident, "client");
887
  TEST_P(tport_incref(tp), tp0); tport_decref(&tp);
Pekka Pessi's avatar
Pekka Pessi committed
888 889 890 891 892 893
  msg_destroy(msg);

  /* Fill up the queue */
  for (i = 1; i < TPORT_QUEUESIZE; i++) {
    snprintf(ident, sizeof ident, "tcp-%u", i);

894
    TEST(new_test_msg(tt, &msg, ident, 1, 64 * 1024), 0);
Pekka Pessi's avatar
Pekka Pessi committed
895
    TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END()));
896
    N++;
Pekka Pessi's avatar
Pekka Pessi committed
897
    TEST_S(tport_name(tp)->tpn_ident, "client");
898
    TEST_P(tport_incref(tp), tp0); tport_decref(&tp);
Pekka Pessi's avatar
Pekka Pessi committed
899 900 901 902 903 904 905 906 907 908 909 910 911
    msg_destroy(msg);
  }

  /* This overflows the queue */
  TEST(new_test_msg(tt, &msg, "tcp-overflow", 1, 1024), 0);
  TEST_1(!tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END()));
  msg_destroy(msg);

  TEST(tport_test_run(tt, 60), 1);
  TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-0"));
  test_check_md5(tt, tt->tt_rmsg);
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

912 913 914 915 916 917
  if (tt->tt_received < TPORT_QUEUESIZE) { /* We have not received it all */
    snprintf(ident, sizeof ident, "tcp-%u", tt->tt_received);
    TEST(tport_test_run(tt, 5), 1);
    TEST_1(!check_msg(tt, tt->tt_rmsg, ident));
    msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
  }
918 919
#else
 (void)i; (void)ident;
920 921
#endif

Pekka Pessi's avatar
Pekka Pessi committed
922 923
  /* This uses a new connection */
  TEST_1(!new_test_msg(tt, &msg, "tcp-no-reuse", 1, 1024));
924
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name,
925
       		   TPTAG_REUSE(0), TAG_END()));
926
  N++;
Pekka Pessi's avatar
Pekka Pessi committed
927 928 929 930 931 932
  TEST_S(tport_name(tp)->tpn_ident, "client");
  TEST_1(tport_incref(tp) != tp0); tport_decref(&tp);
  msg_destroy(msg);

  /* This uses the old connection */
  TEST_1(!new_test_msg(tt, &msg, "tcp-reuse", 1, 1024));
933
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name,
934
       		   TPTAG_REUSE(1), TAG_END()));
935
  N++;
Pekka Pessi's avatar
Pekka Pessi committed
936 937 938 939 940
  TEST_S(tport_name(tp)->tpn_ident, "client");
  TEST_1(tport_incref(tp) == tp0); tport_decref(&tp);
  msg_destroy(msg);

  /* Receive every message from queue */
941
  while (tt->tt_received < N) {
Pekka Pessi's avatar
Pekka Pessi committed
942 943 944 945 946 947 948 949 950 951
    TEST(tport_test_run(tt, 5), 1);
    /* Validate message */
    TEST_1(!check_msg(tt, tt->tt_rmsg, NULL));
    msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
  }

  /* Try to send a single message */
  TEST_1(!new_test_msg(tt, &msg, "tcp-last", 1, 1024));
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END()));
  TEST_S(tport_name(tp)->tpn_ident, "client");
952
  TEST_P(tport_incref(tp), tp0); tport_decref(&tp);
Pekka Pessi's avatar
Pekka Pessi committed
953 954 955 956 957 958 959
  msg_destroy(msg);

  TEST(tport_test_run(tt, 5), 1);

  TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-last"));
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

960 961 962
  TEST_1(pending_server_close && pending_client_close);
  SU_DEBUG_3(("tport_test(%p): waiting for PONG timeout\n", (void *)tp0));

963 964
  /* Wait until notifications -
     client closes when no pong is received and notifys pending,
965 966 967 968
     then server closes and notifys pending */
  while (pending_server_close || pending_client_close)
    su_root_step(tt->tt_root, 50);

Pekka Pessi's avatar
Pekka Pessi committed
969 970
  tport_decref(&tp0);

971 972 973 974 975 976 977 978
  /* Again a single message */
  TEST_1(!new_test_msg(tt, &msg, "tcp-pingpong", 1, 512));
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END()));
  TEST_S(tport_name(tp)->tpn_ident, "client");
  tp0 = tport_incref(tp);
  msg_destroy(msg);

  tport_set_params(tp0,
979
       	    TPTAG_KEEPALIVE(250),
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
       	    TPTAG_PINGPONG(200),
       	    TAG_END());

  TEST(tport_test_run(tt, 5), 1);
  TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-pingpong"));
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

  /* Ask for notifications upon close */
  pending_client_close = tport_pend(tp0, NULL, client_closed_callback, NULL);
  TEST_1(pending_client_close > 0);

  tp = tt->tt_rtport;
  pending_server_close = tport_pend(tp, NULL, server_closed_callback, NULL);
  TEST_1(pending_server_close > 0);

  /* Now server responds with pong ... */
  TEST(tport_set_params(tp, TPTAG_PONG2PING(1), TAG_END()), 1);

  started = su_now();

  while (pending_server_close && pending_client_close) {
    su_root_step(tt->tt_root, 50);
    if (su_duration(su_now(), started) > 1000)
      break;
  }

  /* ... and we are still pending after a second */
  TEST_1(pending_client_close && pending_server_close);
  TEST_1(su_duration(su_now(), started) > 1000);

  tport_shutdown(tp0, 2);
  tport_unref(tp0);

  while (pending_server_close || pending_client_close)
    su_root_step(tt->tt_root, 50);

  END();
}

static int test_incomplete(tp_test_t *tt)
{
  BEGIN();
1022

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
  su_addrinfo_t const *ai = tt->tt_tcp_addr;
  su_socket_t s;
  int connected;

  TEST_1(ai != NULL);

  TEST(tport_set_params(tt->tt_tcp, TPTAG_TIMEOUT(500), TAG_END()), 1);

  s = su_socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  TEST_1(s != SOCKET_ERROR);
1033

1034
  su_setblocking(s, 1);
1035
  connected = connect(s, ai->ai_addr, (socklen_t)ai->ai_addrlen);
1036 1037

  su_root_step(tt->tt_root, 50);
1038

1039
  TEST(su_send(s, "F", 1, 0), 1);
1040
  su_root_step(tt->tt_root, 50);
1041
  TEST(su_send(s, "O", 1, 0), 1);
1042
  su_root_step(tt->tt_root, 50);
1043
  TEST(su_send(s, "O", 1, 0), 1);
1044
  su_root_step(tt->tt_root, 50);
1045
  TEST(su_send(s, " ", 1, 0), 1);
1046
  su_root_step(tt->tt_root, 50);
1047

1048 1049 1050 1051
  tt->tt_received = 0;
  TEST(tport_test_run(tt, 5), -1);
  TEST(tt->tt_received, 1);
  TEST_P(tt->tt_rmsg, NULL);
1052

1053
  su_close(s);
1054

Pekka Pessi's avatar
Pekka Pessi committed
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
  END();
}

static int reuse_test(tp_test_t *tt)
{
  msg_t *msg = NULL;
  int i, reuse = -1;
  tport_t *tp, *tp0, *tp1;
  tp_name_t tpn[1];

  BEGIN();

  /* Flush existing connections */
  *tpn = *tt->tt_tcp_name;
  tpn->tpn_port = "*";
  TEST_1(tp = tport_by_name(tt->tt_tports, tpn));
  TEST_1(tport_is_primary(tp));
  TEST(tport_flush(tp), 0);
1073

Pekka Pessi's avatar
Pekka Pessi committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
  for (i = 0; i < 10; i++)
    su_root_step(tt->tt_root, 10L);

  TEST(tport_set_params(tp, TPTAG_REUSE(0), TAG_END()), 1);

  /* Send two messages */
  TEST(new_test_msg(tt, &msg, "reuse-1", 1, 1024), 0);
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END()));
  TEST_S(tport_name(tp)->tpn_ident, "client");
  TEST_1(tp0 = tport_incref(tp));
  TEST(tport_get_params(tp, TPTAG_REUSE_REF(reuse), TAG_END()), 1);
  TEST(reuse, 0);
  msg_destroy(msg), msg = NULL;

  TEST(new_test_msg(tt, &msg, "reuse-2", 1, 1024), 0);
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name, TAG_END()));
  TEST_S(tport_name(tp)->tpn_ident, "client");
  TEST_1(tp1 = tport_incref(tp)); TEST_1(tp0 != tp1);
  TEST(tport_get_params(tp, TPTAG_REUSE_REF(reuse), TAG_END()), 1);
  TEST(reuse, 0);
  msg_destroy(msg), msg = NULL;

  /* Receive every message from queue */
  for (tt->tt_received = 0;
       tt->tt_received < 2;) {
    TEST(tport_test_run(tt, 5), 1);
    msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
  }

  /* Enable reuse on single connection */
  TEST(tport_set_params(tp1, TPTAG_REUSE(1), TAG_END()), 1);
  TEST(new_test_msg(tt, &msg, "reuse-3", 1, 1024), 0);
1106
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name,
Pekka Pessi's avatar
Pekka Pessi committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
			  TPTAG_REUSE(1),
			  TAG_END()));
  TEST_S(tport_name(tp)->tpn_ident, "client");
  TEST_1(tp1 == tp);
  TEST(tport_get_params(tp, TPTAG_REUSE_REF(reuse), TAG_END()), 1);
  TEST(reuse, 1);
  msg_destroy(msg), msg = NULL;

  TEST(tport_test_run(tt, 5), 1);
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

1118 1119 1120 1121 1122 1123
  TEST_1(tp = tport_by_name(tt->tt_tports, tpn));
  TEST_1(tport_is_primary(tp));
  TEST(tport_set_params(tp, TPTAG_REUSE(1), TAG_END()), 1);

  /* Send a single message with different connection */
  TEST_1(!new_test_msg(tt, &msg, "fresh-1", 1, 1024));
1124
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_tcp_name,
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
			  TPTAG_FRESH(1),
			  TPTAG_REUSE(1),
			  TAG_END()));
  TEST_S(tport_name(tp)->tpn_ident, "client");
  TEST_1(tport_incref(tp) != tp1);  tport_decref(&tp);
  msg_destroy(msg);

  TEST(tport_test_run(tt, 5), 1);

  TEST_1(!check_msg(tt, tt->tt_rmsg, "fresh-1"));
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

1137 1138 1139
  TEST_1(tport_shutdown(tp0, 2) >= 0);
  TEST_1(tport_shutdown(tp1, 2) >= 0);
  TEST_1(tport_shutdown(tp0, 1) >= 0);
1140

1141 1142
  TEST(tport_shutdown(NULL, 0), -1);

Pekka Pessi's avatar
Pekka Pessi committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
  tport_decref(&tp0);
  tport_decref(&tp1);

  END();
}

static int sctp_test(tp_test_t *tt)
{
  BEGIN();

  msg_t *msg = NULL;
  int i, n;
1155
  tport_t *tp, *tp0;
1156
  char buffer[32];
Pekka Pessi's avatar
Pekka Pessi committed
1157

1158
  if (!tt->tt_sctp_name->tpn_proto)
Pekka Pessi's avatar
Pekka Pessi committed
1159 1160 1161
    return 0;

  /* Just a small and nice message first */
1162
  TEST_1(!new_test_msg(tt, &msg, "cid:sctp-first", 1, 1024));
Pekka Pessi's avatar
Pekka Pessi committed
1163 1164 1165 1166
  test_create_md5(tt, msg);
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, tt->tt_sctp_name, TAG_END()));
  TEST_S(tport_name(tp)->tpn_ident, "client");
  msg_destroy(msg);
1167 1168 1169

  tport_set_params(tp, TPTAG_KEEPALIVE(100), TPTAG_IDLE(500), TAG_END());

Pekka Pessi's avatar
Pekka Pessi committed
1170
  TEST(tport_test_run(tt, 5), 1);
1171

Pekka Pessi's avatar
Pekka Pessi committed
1172 1173 1174
  TEST_1(!check_msg(tt, tt->tt_rmsg, NULL));
  test_check_md5(tt, tt->tt_rmsg);
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
1175

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
  tp0 = tport_ref(tp);

  pending_server_close = pending_client_close = 0;

  /* Ask for notification upon close */
  pending_client_close = tport_pend(tp, NULL, client_closed_callback, NULL);
  TEST_1(pending_client_close > 0);
  tp = tt->tt_rtport;
  pending_server_close = tport_pend(tp, NULL, server_closed_callback, NULL);
  TEST_1(pending_server_close > 0);

  if (0) { /* SCTP does not work reliably. Really. */

  tt->tt_received = 0;
1190

Pekka Pessi's avatar
Pekka Pessi committed
1191 1192
  /* Create large messages, just to force queueing in sending end */
  for (n = 0; !tport_queuelen(tp); n++) {
1193 1194
    snprintf(buffer, sizeof buffer, "cid:sctp-%u", n);
    TEST_1(!new_test_msg(tt, &msg, buffer, 1, 32000));
Pekka Pessi's avatar
Pekka Pessi committed
1195
    test_create_md5(tt, msg);
1196
    TEST_1(tp = tport_tsend(tp0, msg, tt->tt_sctp_name, TAG_END()));
Pekka Pessi's avatar
Pekka Pessi committed
1197 1198 1199
    TEST_S(tport_name(tp)->tpn_ident, "client");
    msg_destroy(msg);
  }
1200

Pekka Pessi's avatar
Pekka Pessi committed
1201 1202
  /* Fill up the queue */
  for (i = 1; i < TPORT_QUEUESIZE; i++) {
1203 1204
    snprintf(buffer, sizeof buffer, "cid:sctp-%u", n + i);
    TEST_1(!new_test_msg(tt, &msg, buffer, 1, 1024));
1205
    TEST_1(tp = tport_tsend(tp0, msg, tt->tt_sctp_name, TAG_END()));
Pekka Pessi's avatar
Pekka Pessi committed
1206 1207 1208
    msg_destroy(msg);
  }

1209
  /* Try to overflow the queue */
1210 1211
  snprintf(buffer, sizeof buffer, "cid:sctp-%u", n + i);
  TEST_1(!new_test_msg(tt, &msg, buffer, 1, 1024));
Pekka Pessi's avatar
Pekka Pessi committed
1212 1213
  TEST_1(!tport_tsend(tt->tt_tports, msg, tt->tt_sctp_name, TAG_END()));
  msg_destroy(msg);
1214

Pekka Pessi's avatar
Pekka Pessi committed
1215
  TEST(tport_test_run(tt, 5), 1);
1216

Pekka Pessi's avatar
Pekka Pessi committed
1217 1218 1219 1220 1221
  TEST_1(!check_msg(tt, tt->tt_rmsg, NULL));
  test_check_md5(tt, tt->tt_rmsg);
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

  /* This uses a new connection */
1222
  TEST_1(!new_test_msg(tt, &msg, "cid:sctp-new", 1, 1024));
1223
  TEST_1(tport_tsend(tt->tt_tports, msg, tt->tt_sctp_name,
Pekka Pessi's avatar
Pekka Pessi committed
1224 1225 1226 1227
		     TPTAG_REUSE(0), TAG_END()));
  msg_destroy(msg);

  /* Receive every message from queue */
1228
  for (; tt->tt_received < n + TPORT_QUEUESIZE - 1;) {
1229
    TEST(tport_test_run(tt, 10), 1);
Pekka Pessi's avatar
Pekka Pessi committed
1230 1231 1232 1233
    /* Validate message */
    TEST_1(!check_msg(tt, tt->tt_rmsg, NULL));
    msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
  }
1234

Pekka Pessi's avatar
Pekka Pessi committed
1235
  /* Try to send a single message */
1236
  TEST_1(!new_test_msg(tt, &msg, "cid:sctp-final", 1, 512));
Pekka Pessi's avatar
Pekka Pessi committed
1237 1238
  TEST_1(tport_tsend(tt->tt_tports, msg, tt->tt_sctp_name, TAG_END()));
  msg_destroy(msg);
1239

1240
  TEST(tport_test_run(tt, 10), 1);
1241

Pekka Pessi's avatar
Pekka Pessi committed
1242 1243
  TEST_1(!check_msg(tt, tt->tt_rmsg, NULL));
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
1244 1245 1246 1247
  }

  tport_unref(tp0);

1248 1249
  /* Wait until notifications -
     client closes when idle and notifys pending,
1250 1251 1252
     then server closes and notifys pending */
  while (pending_server_close || pending_client_close)
    su_root_step(tt->tt_root, 50);
Pekka Pessi's avatar
Pekka Pessi committed
1253 1254 1255 1256

  END();
}

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
struct called {
  int n, error, pending, released;
};

static
void tls_error_callback(tp_stack_t *tt, tp_client_t *client,
			tport_t *tp, msg_t *msg, int error)
{
  struct called *called = (struct called *)client;

  tt->tt_status = -1;

  called->n++, called->error = error;

  if (called->pending) {
    called->released = tport_release(tp, called->pending, msg, NULL, client, 0);
    called->pending = 0;
  }
}

Pekka Pessi's avatar
Pekka Pessi committed
1277 1278
static int tls_test(tp_test_t *tt)
{
1279
  BEGIN();
Pekka Pessi's avatar
Pekka Pessi committed
1280 1281 1282 1283 1284 1285

#if HAVE_TLS
  tp_name_t const *dst = tt->tt_tls_name;
  msg_t *msg = NULL;
  int i;
  char ident[16];
1286
  tport_t *tp, *tp0;
1287
  struct called called[1] = {{ 0, 0, 0, 0 }};
Pekka Pessi's avatar
Pekka Pessi committed
1288 1289 1290

  TEST_S(dst->tpn_proto, "tls");

1291 1292 1293 1294
  /* Send a single message */
  TEST_1(!new_test_msg(tt, &msg, "tls-first", 1, 1024));
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, dst, TAG_END()));
  TEST_1(tp0 = tport_ref(tp));
1295 1296 1297
  TEST_1(called->pending = tport_pend(tp, msg, tls_error_callback, (tp_client_t *)called));

  i = tport_test_run(tt, 5);
1298
  msg_destroy(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1299

1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
  if (i < 0) {
    if (called->n) {
      TEST(called->released, 0);
      puts("test_tport: skipping TLS tests");
      tport_unref(tp0);
      return 0;
    }
  }

  TEST(i, 1);
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

  TEST_1(!check_msg(tt, tt->tt_rmsg, "tls-first"));
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

  tport_set_params(tp, TPTAG_KEEPALIVE(100), TPTAG_IDLE(500), TAG_END());

  /* Ask for notification upon close */
  pending_client_close = tport_pend(tp0, NULL, client_closed_callback, NULL);
  TEST_1(pending_client_close > 0);
  tp = tt->tt_rtport;
  pending_server_close = tport_pend(tp, NULL, server_closed_callback, NULL);
1321

1322 1323 1324 1325
  TEST_1(pending_server_close > 0);

  /* Send a largish message */
  TEST_1(!new_test_msg(tt, &msg, "tls-0", 16, 16 * 1024));
Pekka Pessi's avatar
Pekka Pessi committed
1326
  test_create_md5(tt, msg);
1327 1328
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, dst, TAG_END()));
  TEST_1(tp == tp0);
Pekka Pessi's avatar
Pekka Pessi committed
1329 1330 1331 1332 1333 1334 1335
  msg_destroy(msg);

  /* Fill up the queue */
  for (i = 1; i < TPORT_QUEUESIZE; i++) {
    snprintf(ident, sizeof ident, "tls-%u", i);

    TEST_1(!new_test_msg(tt, &msg, ident, 2, 512));
1336 1337
    TEST_1(tp = tport_tsend(tt->tt_tports, msg, dst, TAG_END()));
    TEST_1(tp == tp0);
Pekka Pessi's avatar
Pekka Pessi committed
1338 1339 1340 1341 1342
    msg_destroy(msg);
  }

  /* This uses a new connection */
  TEST_1(!new_test_msg(tt, &msg, "tls-no-reuse", 1, 1024));
1343
  TEST_1(tp = tport_tsend(tt->tt_tports, msg, dst,
Pekka Pessi's avatar
Pekka Pessi committed
1344
		     TPTAG_REUSE(0), TAG_END()));
1345
  TEST_1(tp != tp0);
Pekka Pessi's avatar
Pekka Pessi committed
1346 1347
  msg_destroy(msg);

1348 1349
  tt->tt_received = 0;

Pekka Pessi's avatar
Pekka Pessi committed
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
  /* Receive every message from queue */
  while (tt->tt_received < TPORT_QUEUESIZE + 1) {
    TEST(tport_test_run(tt, 5), 1);
    /* Validate message */
    msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
  }

  /* Try to send a single message */
  TEST_1(!new_test_msg(tt, &msg, "tls-last", 1, 1024));
  TEST_1(tport_tsend(tt->tt_tports, msg, dst, TAG_END()));
  msg_destroy(msg);

  TEST(tport_test_run(tt, 5), 1);

  TEST_1(!check_msg(tt, tt->tt_rmsg, "tls-last"));
  msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

1367 1368
  tport_decref(&tp0);

1369 1370
  /* Wait until notifications -
     client closes when idle and notifys pending,
1371 1372 1373
     then server closes and notifys pending */
  while (pending_server_close || pending_client_close)
    su_root_step(tt->tt_root, 50);
1374

Pekka Pessi's avatar
Pekka Pessi committed
1375 1376
#endif

1377
  END();
Pekka Pessi's avatar
Pekka Pessi committed
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
}

static int sigcomp_test(tp_test_t *tt)
{
  BEGIN();

#if HAVE_SIGCOMP
  su_home_t *home;
  tp_name_t tpn[1] = {{ NULL }};
  struct sigcomp_compartment *cc;
1388

Pekka Pessi's avatar
Pekka Pessi committed
1389 1390 1391
  if (tt->tt_udp_comp->tpn_comp) {
    msg_t *msg = NULL;

1392
    TEST_1(cc = test_sigcomp_compartment(tt, tt->tt_tports, tt->tt_udp_comp));
Pekka Pessi's avatar
Pekka Pessi committed
1393 1394 1395

    TEST_1(!new_test_msg(tt, &msg, "udp-sigcomp", 1, 1200));
    test_create_md5(tt, msg);
1396 1397
    TEST_1(tport_tsend(tt->tt_tports,
		       msg,
Pekka Pessi's avatar
Pekka Pessi committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
		       tt->tt_udp_comp,
		       TPTAG_COMPARTMENT(cc),
		       TAG_END()));
    msg_destroy(msg);

    TEST(tport_test_run(tt, 5), 1);

    TEST_1(!check_msg(tt, tt->tt_rmsg, NULL));

    test_check_md5(tt, tt->tt_rmsg);

    TEST_1(msg = tt->tt_rmsg); tt->tt_rmsg = NULL;
1410

Pekka Pessi's avatar
Pekka Pessi committed
1411
    TEST_1(home = msg_home(msg));
1412

Pekka Pessi's avatar
Pekka Pessi committed
1413
    TEST_1(tport_convert_addr(home, tpn, "udp", NULL, msg_addr(msg)) == 0);
1414

Pekka Pessi's avatar
Pekka Pessi committed
1415
    tpn->tpn_comp = tport_name(tt->tt_rtport)->tpn_comp;
1416

Pekka Pessi's avatar
Pekka Pessi committed
1417
    /* reply */
1418
    TEST_1(cc = test_sigcomp_compartment(tt, tt->tt_tports, tpn));
1419
    TEST_1(tport_tsend(tt->tt_rtport, msg, tpn,
Pekka Pessi's avatar
Pekka Pessi committed
1420 1421
		       TPTAG_COMPARTMENT(cc),
		       TAG_END()) != NULL);
1422

Pekka Pessi's avatar
Pekka Pessi committed
1423
    msg_destroy(msg);
1424

Pekka Pessi's avatar
Pekka Pessi committed
1425 1426
    TEST(tport_test_run(tt, 5), 1);

1427 1428
    TEST_1(!check_msg(tt, tt->tt_rmsg, NULL));
    test_check_md5(tt, tt->tt_rmsg);
Pekka Pessi's avatar
Pekka Pessi committed
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
    msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
  }

  if (tt->tt_tcp_comp->tpn_comp) {
    tport_t *tp;
    msg_t *msg = NULL;

    *tpn = *tt->tt_tcp_comp;

    TEST_1(!new_test_msg(tt, &msg, "tcp-sigcomp", 1, 1500));
    test_create_md5(tt, msg);

    tport_log->log_level = 9;

1443
    TEST_1(cc = test_sigcomp_compartment(tt, tt->tt_tports, tpn));
1444 1445 1446
    TEST_1(tp = tport_tsend(tt->tt_tports,
			    msg,
			    tpn,
Pekka Pessi's avatar
Pekka Pessi committed
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
			    TPTAG_COMPARTMENT(cc),
			    TAG_END()));
    TEST_1(tport_incref(tp)); tport_decref(&tp);
    msg_destroy(msg);

    TEST(tport_test_run(tt, 5), 1);

    TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-sigcomp"));
    test_check_md5(tt, tt->tt_rmsg);
    msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

    TEST_1(!new_test_msg(tt, &msg, "tcp-sigcomp-2", 1, 3000));
    test_create_md5(tt, msg);
1460 1461 1462
    TEST_1(tp = tport_tsend(tt->tt_tports,
			    msg,
			    tt->tt_tcp_comp,
Pekka Pessi's avatar
Pekka Pessi committed
1463 1464 1465 1466 1467 1468 1469 1470
			    TPTAG_COMPARTMENT(cc),
			    TAG_END()));
    TEST_1(tport_incref(tp)); tport_decref(&tp);
    msg_destroy(msg);

    TEST(tport_test_run(tt, 5), 1);

    TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-sigcomp-2"));
1471
    test_check_md5(tt, tt->tt_rmsg);
Pekka Pessi's avatar
Pekka Pessi committed
1472 1473 1474 1475 1476

    msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;

    TEST_1(!new_test_msg(tt, &msg, "tcp-sigcomp-3", 1, 45500));
    test_create_md5(tt, msg);
1477 1478 1479
    TEST_1(tp = tport_tsend(tt->tt_tports,
			    msg,
			    tt->tt_tcp_comp,
Pekka Pessi's avatar
Pekka Pessi committed
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
			    TPTAG_COMPARTMENT(cc),
			    TAG_END()));
    TEST_1(tport_incref(tp));
    msg_destroy(msg);

    TEST(tport_test_run(tt, 5), 1);

    tport_decref(&tp);
    TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-sigcomp-3"));
    test_check_md5(tt, tt->tt_rmsg);
    msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1491 1492 1493 1494 1495 1496 1497 1498 1499

    {
      tp_name_t tpn[1];
      tport_t *ctp, *rtp;

      *tpn = *tt->tt_tcp_comp; tpn->tpn_comp = NULL;

      TEST_1(!new_test_msg(tt, &msg, "tcp-sigcomp-4", 1, 1000));
      test_create_md5(tt, msg);
1500 1501 1502
      TEST_1(tp = tport_tsend(tt->tt_tports,
			      msg,
			      tpn,
Pekka Pessi's avatar
Pekka Pessi committed
1503 1504 1505 1506 1507 1508 1509
			      TPTAG_COMPARTMENT(cc),
			      TPTAG_FRESH(1),
			      TAG_END()));
      TEST_1(ctp = tport_incref(tp));
      msg_destroy(msg);

      TEST(tport_test_run(tt, 5), 1);
1510

Pekka Pessi's avatar
Pekka Pessi committed
1511 1512 1513 1514 1515 1516 1517 1518 1519
      TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-sigcomp-4"));
      test_check_md5(tt, tt->tt_rmsg);
      TEST_1((msg_addrinfo(tt->tt_rmsg)->ai_flags & TP_AI_COMPRESSED) == 0);
      msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
      TEST_1(rtp = tport_incref(tt->tt_rtport));

      TEST_1(!new_test_msg(tt, &msg, "tcp-sigcomp-5", 1, 1000));
      test_create_md5(tt, msg);
      {
1520
	/* Mess with internal data structures in order to
Pekka Pessi's avatar
Pekka Pessi committed
1521 1522 1523 1524
	   force tport to use SigComp on this connection */
	tp_name_t *tpn = (tp_name_t *)tport_name(rtp);
	tpn->tpn_comp = "sigcomp";
      }
1525 1526 1527
      TEST_1(tp = tport_tsend(rtp,
			      msg,
			      tt->tt_tcp_comp,
Pekka Pessi's avatar
Pekka Pessi committed
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
			      TPTAG_COMPARTMENT(cc),
			      TAG_END()));
      TEST_1(tport_incref(tp));
      msg_destroy(msg);

      TEST(tp, rtp);

      TEST(tport_test_run(tt, 5), 1);

      tport_decref(&tp);
      TEST_1(!check_msg(tt, tt->tt_rmsg, "tcp-sigcomp-5"));
      test_check_md5(tt, tt->tt_rmsg);
      TEST_1((msg_addrinfo(tt->tt_rmsg)->ai_flags & TP_AI_COMPRESSED) != 0);
      msg_destroy(tt->tt_rmsg), tt->tt_rmsg = NULL;
      TEST(ctp, tt->tt_rtport);
      tport_decref(&ctp);
    }
Pekka Pessi's avatar
Pekka Pessi committed
1545 1546 1547 1548 1549 1550
  }
#endif

  END();
}

1551 1552
#if HAVE_SOFIA_STUN

1553
#include <sofia-sip/stun_tag.h>
1554 1555 1556 1557 1558 1559 1560

static int stun_test(tp_test_t *tt)
{
  BEGIN();

  tport_t *mr;
  tp_name_t tpn[1] = {{ "*", "*", "*", "*", NULL }};
Martti Mela's avatar
Martti Mela committed
1561
#if HAVE_NETINET_SCTP_H
1562
  char const * transports[] = { "udp", "tcp", "sctp", NULL };
Martti Mela's avatar
Martti Mela committed
1563 1564 1565
#else
  char const * transports[] = { "udp", "tcp", NULL };
#endif