test_init.c 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE test_init.c
26
 * @brief Init nua test context
27 28 29 30 31 32 33 34 35 36 37
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Martti Mela <Martti Mela@nokia.com>
 *
 * @date Created: Wed Aug 17 12:12:12 EEST 2005 ppessi
 */

#include "config.h"

#include "test_nua.h"

38
#include <sofia-sip/tport_tag.h>
39
#include <sofia-sip/auth_module.h>
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
#define __func__ name
#endif

static char passwd_name[] = "tmp_sippasswd.XXXXXX";

static void remove_tmp(void)
{
  if (passwd_name[0])
    unlink(passwd_name);
}

static char const passwd[] =
  "alice:secret:\n"
  "bob:secret:\n"
  "charlie:secret:\n";

int test_nua_init(struct context *ctx,
		  int start_proxy,
		  url_t const *o_proxy,
		  int start_nat,
		  tag_type_t tag, tag_value_t value, ...)
{
  BEGIN();
  struct event *e;
  sip_contact_t const *m = NULL;
  sip_from_t const *sipaddress = NULL;
71 72 73
  sip_allow_t const *allow = NULL;
  sip_supported_t const *supported = NULL;
  char const *appl_method = NULL;
74
  url_t const *p_uri, *a_uri, *b_uri;		/* Proxy URI */
75
  char const *initial_route = NULL;	/* Initial route towards internal proxy */
76
  char const *a_bind, *a_bind2;
77
  url_t *e_proxy = NULL;
78
  int err = -1;
79
  url_t b_proxy[1];
80 81 82

  a_bind = a_bind2 = "sip:0.0.0.0:*";

83 84 85 86 87 88 89
#if SU_HAVE_OSX_CF_API
  if (ctx->osx_runloop)
    ctx->root = su_root_osx_runloop_create(NULL);
  else
#endif
  ctx->root = su_root_create(NULL);
  TEST_1(ctx->root);
90

91
  /* Disable threading by command line switch -s */
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  su_root_threading(ctx->root, ctx->threading);

  if (start_proxy && !o_proxy) {
    int temp;

    if (print_headings)
      printf("TEST NUA-2.1.1: init proxy P\n");

#ifndef _WIN32
    temp = mkstemp(passwd_name);
#else
    temp = open(passwd_name, O_WRONLY|O_CREAT|O_TRUNC, 666);
#endif
    TEST_1(temp != -1);
    atexit(remove_tmp);		/* Make sure temp file is unlinked */

108
    TEST_SIZE(write(temp, passwd, strlen(passwd)), strlen(passwd));
109 110 111 112

    TEST_1(close(temp) == 0);

    ctx->p = test_proxy_create(ctx->root,
113
			       TAG_IF(ctx->proxy_logging, TPTAG_LOG(1)),
114 115
			       TAG_END());

116
    if (ctx->p) {
117
      ctx->a.domain =
118 119 120 121 122 123 124 125 126 127 128
	test_proxy_add_domain(ctx->p,
			      URL_STRING_MAKE("sip:example.com")->us_url,
			      AUTHTAG_METHOD("Digest"),
			      AUTHTAG_REALM("test-proxy"),
			      AUTHTAG_OPAQUE("kuik"),
			      AUTHTAG_DB(passwd_name),
			      AUTHTAG_QOP("auth-int"),
			      AUTHTAG_ALGORITHM("md5"),
			      AUTHTAG_NEXT_EXPIRES(60),
			      TAG_END());

129
      ctx->b.domain =
130 131 132 133 134 135 136 137 138 139 140
	test_proxy_add_domain(ctx->p,
			      URL_STRING_MAKE("sip:example.org")->us_url,
			      AUTHTAG_METHOD("Digest"),
			      AUTHTAG_REALM("test-proxy"),
			      AUTHTAG_OPAQUE("kuik"),
			      AUTHTAG_DB(passwd_name),
			      AUTHTAG_QOP("auth-int"),
			      AUTHTAG_ALGORITHM("md5"),
			      AUTHTAG_NEXT_EXPIRES(60),
			      TAG_END());

141 142
      test_proxy_domain_set_outbound(ctx->b.domain, 1);

143
      ctx->c.domain =
144 145 146 147 148 149 150 151 152
	test_proxy_add_domain(ctx->p,
			      URL_STRING_MAKE("sip:example.net")->us_url,
			      AUTHTAG_METHOD("Digest"),
			      AUTHTAG_REALM("test-proxy"),
			      AUTHTAG_OPAQUE("kuik"),
			      AUTHTAG_DB(passwd_name),
			      AUTHTAG_QOP("auth-int"),
			      AUTHTAG_ALGORITHM("md5"),
			      AUTHTAG_NEXT_EXPIRES(60),
153
			      AUTHTAG_MAX_NCOUNT(1),
154
			      AUTHTAG_ALLOW("ACK, CANCEL"),
155 156
			      TAG_END());

157 158
      test_proxy_domain_set_record_route(ctx->c.domain, 1);

159 160 161
      ctx->proxy_tests = 1;
    }

162 163 164 165 166

    if (print_headings)
      printf("TEST NUA-2.1.1: PASSED\n");
  }

167
  p_uri = a_uri = b_uri = test_proxy_uri(ctx->p);
168

169 170 171 172 173
  if (o_proxy) {
    TEST_1(e_proxy = url_hdup(ctx->home, (void *)o_proxy));
    ctx->external_proxy = e_proxy;
  }

174
  if (start_nat && p_uri == NULL)
175
    p_uri = e_proxy;
176

177 178 179
  if (ctx->p)
    initial_route = test_proxy_route_uri(ctx->p, &ctx->lr);

180 181 182 183 184
  if (start_nat && p_uri != NULL) {
    int family = 0;
    su_sockaddr_t su[1];
    socklen_t sulen = sizeof su;
    char b[64];
185
    size_t len;
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    ta_list ta;

    if (print_headings)
      printf("TEST NUA-2.1.2: creating test NAT\n");

    /* Try to use different family than proxy. */
    if (p_uri->url_host[0] == '[')
      family = AF_INET;
#if defined(SU_HAVE_IN6)
    else
      family = AF_INET6;
#endif

    ta_start(ta, tag, value);
    ctx->nat = test_nat_create(ctx->root, family, ta_tags(ta));
    ta_end(ta);

    /*
     * NAT thingy works so that we set the outgoing proxy URI to point
     * towards its "private" address and give the real address of the proxy
     * as its "public" address. If we use different IP families here, we may
     * even manage to test real connectivity problems as proxy and endpoint
     * can not talk to each other.
     */

    if (test_nat_private(ctx->nat, su, &sulen) < 0) {
      printf("%s:%u: NUA-2.1.2: failed to get private NAT address\n",
	     __FILE__, __LINE__);
    }

#if defined(SU_HAVE_IN6)
    else if (su->su_family == AF_INET6) {
      a_uri = (void *)
	su_sprintf(ctx->home, "sip:[%s]:%u",
220
		   su_inet_ntop(su->su_family, SU_ADDR(su), b, sizeof b),
221 222 223 224 225 226 227
		   ntohs(su->su_port));
      a_bind = "sip:[::]:*";
    }
#endif
    else if (su->su_family == AF_INET) {
      a_uri = (void *)
	su_sprintf(ctx->home, "sip:%s:%u",
228
		   su_inet_ntop(su->su_family, SU_ADDR(su), b, sizeof b),
229 230 231 232 233 234 235 236
		   ntohs(su->su_port));
    }

#if defined(SU_HAVE_IN6)
    if (p_uri->url_host[0] == '[') {
      su->su_len = sulen = (sizeof su->su_sin6), su->su_family = AF_INET6;
      len = strcspn(p_uri->url_host + 1, "]"); assert(len < sizeof b);
      memcpy(b, p_uri->url_host + 1, len); b[len] = '\0';
237
      su_inet_pton(su->su_family, b, SU_ADDR(su));
238 239 240
    }
    else {
      su->su_len = sulen = (sizeof su->su_sin), su->su_family = AF_INET;
241
      su_inet_pton(su->su_family, p_uri->url_host, SU_ADDR(su));
242 243 244
    }
#else
    su->su_len = sulen = (sizeof su->su_sin), su->su_family = AF_INET;
245
    su_inet_pton(su->su_family, p_uri->url_host, SU_ADDR(su));
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
#endif

    su->su_port = htons(strtoul(url_port(p_uri), NULL, 10));

    if (test_nat_public(ctx->nat, su, sulen) < 0) {
      printf("%s:%u: NUA-2.1.2: failed to set public address\n",
	     __FILE__, __LINE__);
      a_uri = NULL;
    }

    if (print_headings) {
      if (ctx->nat && a_uri) {
	printf("TEST NUA-2.1.2: PASSED\n");
      } else {
	printf("TEST NUA-2.1.2: FAILED\n");
      }
    }
  }

  if (print_headings)
    printf("TEST NUA-2.2.1: init endpoint A\n");

  if (a_uri == NULL)
    a_uri = p_uri;

  ctx->a.instance = nua_generate_instance_identifier(ctx->home);

  ctx->a.nua = nua_create(ctx->root, a_callback, ctx,
274
			  NUTAG_PROXY(a_uri ? a_uri : e_proxy),
275
			  NUTAG_INITIAL_ROUTE_STR(initial_route),
276 277 278 279
			  SIPTAG_FROM_STR("sip:alice@example.com"),
			  NUTAG_URL(a_bind),
			  TAG_IF(a_bind != a_bind2, NUTAG_SIPS_URL(a_bind2)),
			  SOATAG_USER_SDP_STR("m=audio 5004 RTP/AVP 0 8"),
280
			  NTATAG_SIP_T1X64(2000),
281
			  NUTAG_INSTANCE(ctx->a.instance),
282
			  TAG_IF(ctx->a.logging, TPTAG_LOG(1)),
283 284 285 286 287
			  TAG_END());
  TEST_1(ctx->a.nua);

  nua_get_params(ctx->a.nua, TAG_ANY(), TAG_END());
  run_a_until(ctx, nua_r_get_params, save_until_final_response);
288
  TEST_1(e = ctx->a.specials->head);
289 290 291 292 293 294 295 296 297
  err = tl_gets(e->data->e_tags,
	            NTATAG_CONTACT_REF(m),
	            SIPTAG_FROM_REF(sipaddress),
	            SIPTAG_ALLOW_REF(allow),
	            NUTAG_APPL_METHOD_REF(appl_method),
	            SIPTAG_SUPPORTED_REF(supported),
	            TAG_END());
  TEST(err, 5);
  TEST_1(m);
298 299
  TEST_1(ctx->a.contact = sip_contact_dup(ctx->home, m));
  TEST_1(ctx->a.to = sip_to_dup(ctx->home, sipaddress));
300 301 302
  TEST_1(ctx->a.allow = sip_allow_dup(ctx->home, allow));
  TEST_1(ctx->a.appl_method = su_strdup(ctx->home, appl_method));
  TEST_1(ctx->a.supported = sip_supported_dup(ctx->home, supported));
303

304
  free_events_in_list(ctx, ctx->a.specials);
305 306 307 308 309 310 311 312 313

  if (print_headings)
    printf("TEST NUA-2.2.1: PASSED\n");

  if (print_headings)
    printf("TEST NUA-2.2.2: init endpoint B\n");

  ctx->b.instance = nua_generate_instance_identifier(ctx->home);

314 315 316 317 318 319 320
  if (ctx->p) {
    /* B uses TCP when talking with proxy */
    *b_proxy = *b_uri;
    b_uri = b_proxy;
    b_proxy->url_params = "transport=tcp";
  }

321
  ctx->b.nua = nua_create(ctx->root, b_callback, ctx,
322
			  NUTAG_PROXY(b_uri ? b_uri : e_proxy),
323 324 325 326
			  SIPTAG_FROM_STR("sip:bob@example.org"),
			  NUTAG_URL("sip:0.0.0.0:*"),
			  SOATAG_USER_SDP_STR("m=audio 5006 RTP/AVP 8 0"),
			  NUTAG_INSTANCE(ctx->b.instance),
327 328
			  /* Quicker timeout */
			  NTATAG_SIP_T1X64(2000),
329
			  TPTAG_KEEPALIVE(100),
330
			  TAG_IF(ctx->b.logging, TPTAG_LOG(1)),
331 332 333 334 335
			  TAG_END());
  TEST_1(ctx->b.nua);

  nua_get_params(ctx->b.nua, TAG_ANY(), TAG_END());
  run_b_until(ctx, nua_r_get_params, save_until_final_response);
336
  TEST_1(e = ctx->b.specials->head);
337 338 339 340 341 342 343 344
  err = tl_gets(e->data->e_tags,
	            NTATAG_CONTACT_REF(m),
	            SIPTAG_FROM_REF(sipaddress),
	            SIPTAG_ALLOW_REF(allow),
	            NUTAG_APPL_METHOD_REF(appl_method),
	            SIPTAG_SUPPORTED_REF(supported),
	            TAG_END());
  TEST(err, 5); TEST_1(m);
345

346 347
  TEST_1(ctx->b.contact = sip_contact_dup(ctx->home, m));
  TEST_1(ctx->b.to = sip_to_dup(ctx->home, sipaddress));
348 349 350 351 352
  TEST_1(ctx->b.allow = sip_allow_dup(ctx->home, allow));
  TEST_1(ctx->b.appl_method = su_strdup(ctx->home, appl_method));
  TEST_1(ctx->b.supported = sip_supported_dup(ctx->home, supported));

  free_events_in_list(ctx, ctx->b.specials);
353 354 355 356 357 358 359 360 361

  if (print_headings)
    printf("TEST NUA-2.2.2: PASSED\n");

  if (print_headings)
    printf("TEST NUA-2.2.3: init endpoint C\n");

  /* ctx->c.instance = nua_generate_instance_identifier(ctx->home); */

362 363
  ctx->c.to = sip_from_make(ctx->home, "Charlie <sip:charlie@example.net>");

364
  ctx->c.nua = nua_create(ctx->root, c_callback, ctx,
365
			  NUTAG_PROXY(p_uri ? p_uri : e_proxy),
366 367 368
			  NUTAG_URL("sip:0.0.0.0:*"),
			  SOATAG_USER_SDP_STR("m=audio 5400 RTP/AVP 8 0"),
			  NUTAG_INSTANCE(ctx->c.instance),
369
			  TAG_IF(ctx->c.logging, TPTAG_LOG(1)),
370 371 372 373 374
			  TAG_END());
  TEST_1(ctx->c.nua);

  nua_get_params(ctx->c.nua, TAG_ANY(), TAG_END());
  run_c_until(ctx, nua_r_get_params, save_until_final_response);
375
  TEST_1(e = ctx->c.specials->head);
376 377 378 379 380 381
  err = tl_gets(e->data->e_tags,
	            NTATAG_CONTACT_REF(m),
	            SIPTAG_ALLOW_REF(allow),
	            NUTAG_APPL_METHOD_REF(appl_method),
	            SIPTAG_SUPPORTED_REF(supported),
	            TAG_END());
382

383
  TEST(err, 4); TEST_1(m);
384
  TEST_1(ctx->c.contact = sip_contact_dup(ctx->home, m));
385 386 387 388
  TEST_1(ctx->c.allow = sip_allow_dup(ctx->home, allow));
  TEST_1(ctx->c.appl_method = su_strdup(ctx->home, appl_method));
  TEST_1(ctx->c.supported = sip_supported_dup(ctx->home, supported));
  free_events_in_list(ctx, ctx->c.specials);
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

  if (print_headings)
    printf("TEST NUA-2.2.3: PASSED\n");

  END();
}


/* ====================================================================== */

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

  struct call *call;

  if (!ctx->threading)
    su_root_step(ctx->root, 100);

  if (ctx->a.nua) {
    for (call = ctx->a.call; call; call = call->next)
      nua_handle_destroy(call->nh), call->nh = NULL;

    nua_shutdown(ctx->a.nua);
    run_a_until(ctx, nua_r_shutdown, until_final_response);
414
    free_events_in_list(ctx, ctx->a.events);
415 416 417 418 419 420 421 422 423 424
    free_events_in_list(ctx, ctx->a.specials);
    nua_destroy(ctx->a.nua), ctx->a.nua = NULL;
  }

  if (ctx->b.nua) {
    for (call = ctx->b.call; call; call = call->next)
      nua_handle_destroy(call->nh), call->nh = NULL;

    nua_shutdown(ctx->b.nua);
    run_b_until(ctx, nua_r_shutdown, until_final_response);
425
    free_events_in_list(ctx, ctx->b.events);
426 427 428 429 430 431 432 433 434 435
    free_events_in_list(ctx, ctx->b.specials);
    nua_destroy(ctx->b.nua), ctx->b.nua = NULL;
  }

  if (ctx->c.nua) {
    for (call = ctx->c.call; call; call = call->next)
      nua_handle_destroy(call->nh), call->nh = NULL;

    nua_shutdown(ctx->c.nua);
    run_c_until(ctx, nua_r_shutdown, until_final_response);
436
    free_events_in_list(ctx, ctx->c.events);
437 438 439 440 441 442 443 444
    free_events_in_list(ctx, ctx->c.specials);
    nua_destroy(ctx->c.nua), ctx->c.nua = NULL;
  }

  test_proxy_destroy(ctx->p), ctx->p = NULL;

  test_nat_destroy(ctx->nat), ctx->nat = NULL;

445
  su_root_destroy(ctx->root), ctx->root = NULL;
446 447 448

  END();
}