test_init.c 9.64 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 71 72
/*
 * 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
 * @brief Init nua test context 
 *
 * @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"

#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;
  url_t const *p_uri, *a_uri;		/* Proxy URI */
  char const *a_bind, *a_bind2;

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

73 74 75 76 77 78 79
#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);
80

81
  /* Disable threading by command line switch -s */
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  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 */

98
    TEST_SIZE(write(temp, passwd, strlen(passwd)), strlen(passwd));
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

    TEST_1(close(temp) == 0);

    ctx->p = test_proxy_create(ctx->root,
			       AUTHTAG_METHOD("Digest"),
			       AUTHTAG_REALM("test-proxy"),
			       AUTHTAG_OPAQUE("kuik"),
			       AUTHTAG_DB(passwd_name),
			       AUTHTAG_QOP("auth-int"),
			       AUTHTAG_ALGORITHM("md5-sess"),
			       TAG_END());

    ctx->proxy_tests = ctx->p != NULL;

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

  p_uri = a_uri = test_proxy_uri(ctx->p);

  if (start_nat && p_uri == NULL)
    p_uri = url_hdup(ctx->home, (void *)o_proxy);

  if (start_nat && p_uri != NULL) {
    int family = 0;
    su_sockaddr_t su[1];
    socklen_t sulen = sizeof su;
    char b[64];
127
    size_t len;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    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",
		   inet_ntop(su->su_family, SU_ADDR(su), b, sizeof b),
		   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",
		   inet_ntop(su->su_family, SU_ADDR(su), b, sizeof b),
		   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';
      inet_pton(su->su_family, b, SU_ADDR(su));
    }
    else {
      su->su_len = sulen = (sizeof su->su_sin), su->su_family = AF_INET;
      inet_pton(su->su_family, p_uri->url_host, SU_ADDR(su));
    }
#else
    su->su_len = sulen = (sizeof su->su_sin), su->su_family = AF_INET;
    inet_pton(su->su_family, p_uri->url_host, SU_ADDR(su));
#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,
			  NUTAG_PROXY(a_uri ? a_uri : o_proxy),
			  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"),
			  NTATAG_SIP_T1X64(4000),
			  NUTAG_INSTANCE(ctx->a.instance),
			  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);
  TEST_1(e = ctx->a.events->head);
  TEST(tl_gets(e->data->e_tags,
	       NTATAG_CONTACT_REF(m),
	       SIPTAG_FROM_REF(sipaddress),
	       TAG_END()), 2); TEST_1(m);
  TEST_1(ctx->a.contact = sip_contact_dup(ctx->home, m));
  TEST_1(ctx->a.to = sip_to_dup(ctx->home, sipaddress));

  free_events_in_list(ctx, ctx->a.events);

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

  ctx->b.nua = nua_create(ctx->root, b_callback, ctx,
			  NUTAG_PROXY(p_uri ? p_uri : o_proxy),
			  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),
			  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);
  TEST_1(e = ctx->b.events->head);
  TEST(tl_gets(e->data->e_tags,
	       NTATAG_CONTACT_REF(m),
	       SIPTAG_FROM_REF(sipaddress),
	       TAG_END()), 2); TEST_1(m);
  TEST_1(ctx->b.contact = sip_contact_dup(ctx->home, m));
  TEST_1(ctx->b.to = sip_to_dup(ctx->home, sipaddress));
  free_events_in_list(ctx, ctx->b.events);

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

  ctx->c.nua = nua_create(ctx->root, c_callback, ctx,
			  NUTAG_PROXY(p_uri ? p_uri : o_proxy),
			  SIPTAG_FROM_STR("sip:charlie@example.net"),
			  NUTAG_URL("sip:0.0.0.0:*"),
			  SOATAG_USER_SDP_STR("m=audio 5400 RTP/AVP 8 0"),
			  NUTAG_INSTANCE(ctx->c.instance),
			  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);
  TEST_1(e = ctx->c.events->head);
  TEST(tl_gets(e->data->e_tags,
	       NTATAG_CONTACT_REF(m),
	       SIPTAG_FROM_REF(sipaddress),
	       TAG_END()), 2); TEST_1(m);
  TEST_1(ctx->c.contact = sip_contact_dup(ctx->home, m));
  TEST_1(ctx->c.to = sip_to_dup(ctx->home, sipaddress));
  free_events_in_list(ctx, ctx->c.events);

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

  su_root_destroy(ctx->root);

  END();
}