test_soa.c 11.9 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
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
/*
 * 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_soa.c
 * @brief High-level tester for Sofia SDP Offer/Answer Engine
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Aug 17 12:12:12 EEST 2005 ppessi
 * $Date: 2005/08/17 14:51:23 $
 */

#include "config.h"

const char test_soa_c_id[] =
"$Id: test_soa.c,v 1.1 2005/08/17 14:51:23 ppessi Exp $";

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include <assert.h>

#if HAVE_ALARM
#include <unistd.h>
#include <signal.h>
#endif

struct context;
#define SOA_MAGIC_T struct context

#include "soa.h"
Pekka Pessi's avatar
Pekka Pessi committed
54
#include "soa_add.h"
Pekka Pessi's avatar
Pekka Pessi committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

#include <su_log.h>

extern su_log_t soa_log[];

char const name[] = "test_soa";
int tstflags = 0;
#define TSTFLAGS tstflags

#include <tstdef.h>

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

Pekka Pessi's avatar
Pekka Pessi committed
73 74
#define NONE ((void*)-1)

Pekka Pessi's avatar
Pekka Pessi committed
75 76 77
struct context 
{
  su_home_t home[1];
Pekka Pessi's avatar
Pekka Pessi committed
78 79 80 81 82 83 84 85
  su_root_t *root;

  struct {
    soa_session_t *a;
    soa_session_t *b;
  } synch, asynch;

  soa_session_t *completed;
Pekka Pessi's avatar
Pekka Pessi committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
};

int test_api_completed(struct context *arg, soa_session_t *session)
{
  return 0;
}

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

  char const *phrase = NULL;
  char const *null = NULL;
  char const * const *featurelist;

Pekka Pessi's avatar
Pekka Pessi committed
101 102
  TEST_1(!soa_create("default", NULL, NULL));
  TEST_1(!soa_clone(NULL, NULL, NULL));
Pekka Pessi's avatar
Pekka Pessi committed
103 104 105 106 107 108 109 110 111 112 113 114
  TEST_VOID(soa_destroy(NULL));

  TEST_1(-1 == soa_set_params(NULL, TAG_END()));
  TEST_1(-1 == soa_get_params(NULL, TAG_END()));

  TEST_1(!soa_get_paramlist(NULL));

  TEST(soa_error_as_sip_response(NULL, &phrase), 500);
  TEST_S(phrase, "Internal Server Error");

  TEST_1(soa_error_as_sip_reason(NULL));

Pekka Pessi's avatar
Pekka Pessi committed
115
  TEST_1(-1 == soa_parse_sdp(NULL, 0, NULL, 0));
Pekka Pessi's avatar
Pekka Pessi committed
116 117 118 119 120 121 122 123 124 125 126 127 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

  TEST_VOID(soa_clear_sdp(NULL));

  TEST_1(-1 == soa_print_sdp(NULL, 0, NULL, NULL, NULL));

  TEST_1(!soa_media_features(NULL, 0, NULL));

  TEST_1(!soa_sip_required(NULL));
  TEST_1(!soa_sip_support(NULL));

  TEST_1(-1 == soa_remote_sip_features(NULL, &null, &null));

  TEST_1(-1 == soa_offer(NULL, 0, test_api_completed)); 

  TEST_1(-1 == soa_offer_answer(NULL, 0, test_api_completed)); 

  TEST_1(-1 == soa_answer(NULL, 0, test_api_completed)); 

  TEST_VOID(soa_activate(NULL, "both"));
  TEST_VOID(soa_terminate(NULL, "both"));

  TEST_1(!soa_is_complete(NULL));

  TEST_1(!soa_init_offer_answer(NULL));

  TEST_1(SOA_ACTIVE_DISABLED == soa_is_audio_active(NULL));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_video_active(NULL));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_image_active(NULL));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_chat_active(NULL));

  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_audio_active(NULL));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_video_active(NULL));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_image_active(NULL));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_chat_active(NULL));

  END();
}

int test_init(struct context *ctx, char *argv[])
{
  BEGIN();

Pekka Pessi's avatar
Pekka Pessi committed
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
  int n;

  ctx->root = su_root_create(ctx); TEST_1(ctx->root);

  ctx->asynch.a = soa_create("asynch", ctx->root, ctx); 
  TEST_1(!ctx->asynch.a);

  TEST_1(!soa_find("asynch"));
  TEST_1(soa_find("default"));

  n = soa_add("asynch", &soa_asynch_actions); TEST(n, 0);

  TEST_1(soa_find("asynch"));

  ctx->asynch.a = soa_create("asynch", ctx->root, ctx); 
  TEST_1(ctx->asynch.a);

  ctx->asynch.b = soa_create("asynch", ctx->root, ctx);
  TEST_1(ctx->asynch.b);

  /* Create asynchronous endpoints */

  ctx->synch.a = soa_create("static", ctx->root, ctx); 
  TEST_1(!ctx->synch.a);

  TEST_1(!soa_find("static"));
  TEST_1(soa_find("default"));

  n = soa_add("static", &soa_static_actions); TEST(n, 0);

  TEST_1(soa_find("static"));

  ctx->synch.a = soa_create("static", ctx->root, ctx);
  TEST_1(ctx->synch.a);

  ctx->synch.b = soa_create("static", ctx->root, ctx);
  TEST_1(ctx->synch.b);
Pekka Pessi's avatar
Pekka Pessi committed
195 196 197 198 199 200 201

  END();
}

int test_params(struct context *ctx)
{
  BEGIN();
Pekka Pessi's avatar
Pekka Pessi committed
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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  int n;

  n = soa_set_params(ctx->asynch.a, TAG_END()); TEST(n, 0);
  n = soa_set_params(ctx->asynch.b, TAG_END()); TEST(n, 0);

  END();
}

int test_completed(struct context *ctx, soa_session_t *session)
{
  ctx->completed = session;
  su_root_break(ctx->root);
  return 0;
}

int test_static_offer_answer(struct context *ctx)
{
  BEGIN();
  int n;
  
  su_home_t home[1] = { SU_HOME_INIT(home) };

  char *caps = NONE, *offer = NONE, *answer = NONE;
  int capslen = -1, offerlen = -1, answerlen = -1;

  char const a[] = 
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
    "m=audio 5004 RTP/AVP 0 8\r\n";

  char const b[] = 
    "v=0\n"
    "o=right 93298573265 321974 IN IP4 127.0.0.3\n"
    "c=IN IP4 127.0.0.3\n"
    "m=audio 5006 RTP/AVP 96\n"
    "m=rtpmap:96 GSM/8000\n";

  n = soa_parse_sdp(ctx->synch.a, 0, "m=audio 5004 RTP/AVP 0 8", -1); 
  TEST(n, 1);

  n = soa_parse_sdp(ctx->synch.a, 0, a, strlen(a)); TEST(n, 1);
  n = soa_print_sdp(ctx->synch.a, 0, home, &caps, &capslen); TEST(n, 1);

  TEST_1(caps != NULL && caps != NONE);
  TEST_1(capslen > 0);

  su_free(home, caps);

  n = soa_parse_sdp(ctx->synch.b, 0, b, strlen(b)); TEST(n, 1);

  n = soa_offer(ctx->synch.a, 1, test_completed); TEST(n, 0);

  n = soa_print_sdp(ctx->synch.a, 1, home, &offer, &offerlen); TEST(n, 1);

  n = soa_parse_sdp(ctx->synch.b, 1, offer, offerlen); TEST(n, 1);

  n = soa_offer_answer(ctx->synch.b, 0, test_completed); TEST(n, 0);

  TEST_VOID(soa_activate(ctx->synch.b, NULL));

  n = soa_print_sdp(ctx->synch.b, 1, home, &answer, &answerlen); TEST(n, 1);

  n = soa_parse_sdp(ctx->synch.a, 1, answer, answerlen); TEST(n, 1);

  n = soa_offer_answer(ctx->synch.a, 0, test_completed); TEST(n, 0);

  TEST_VOID(soa_activate(ctx->synch.a, NULL));

  TEST_1(SOA_ACTIVE_SENDRECV == soa_is_audio_active(ctx->synch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_video_active(ctx->synch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_image_active(ctx->synch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_chat_active(ctx->synch.a));

  TEST_1(SOA_ACTIVE_SENDRECV == soa_is_remote_audio_active(ctx->synch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_video_active(ctx->synch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_image_active(ctx->synch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_chat_active(ctx->synch.a));

  su_home_deinit(home);

  TEST_VOID(soa_terminate(ctx->synch.a, NULL));

  TEST_1(SOA_ACTIVE_DISABLED == soa_is_audio_active(ctx->synch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_audio_active(ctx->synch.a));

  TEST_VOID(soa_terminate(ctx->synch.b, NULL));
  
  END();
}


int test_asynch_offer_answer(struct context *ctx)
{
  BEGIN();
  int n;
  
  su_home_t home[1] = { SU_HOME_INIT(home) };

  char *caps = NONE, *offer = NONE, *answer = NONE;
  int capslen = -1, offerlen = -1, answerlen = -1;

  char const a[] = 
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
    "m=audio 5004 RTP/AVP 0 8\r\n";

  char const b[] = 
    "v=0\n"
    "o=right 93298573265 321974 IN IP4 127.0.0.3\n"
    "c=IN IP4 127.0.0.3\n"
    "m=audio 5006 RTP/AVP 96\n"
    "m=rtpmap:96 GSM/8000\n";

  n = soa_parse_sdp(ctx->asynch.a, 0, a, strlen(a)); TEST(n, 1);
  n = soa_print_sdp(ctx->asynch.a, 0, home, &caps, &capslen); TEST(n, 1);

  TEST_1(caps != NULL && caps != NONE);
  TEST_1(capslen > 0);

  su_free(home, caps);

  n = soa_parse_sdp(ctx->asynch.b, 0, b, strlen(b)); TEST(n, 1);

  n = soa_offer(ctx->asynch.a, 1, test_completed); TEST(n, 1);

  su_root_run(ctx->root); TEST(ctx->completed, ctx->asynch.a); ctx->completed = NULL;

  n = soa_print_sdp(ctx->asynch.a, 1, home, &offer, &offerlen); TEST(n, 1);

  n = soa_parse_sdp(ctx->asynch.b, 1, offer, offerlen); TEST(n, 1);

  n = soa_offer_answer(ctx->asynch.b, 0, test_completed); TEST(n, 1);

  su_root_run(ctx->root); TEST(ctx->completed, ctx->asynch.b); ctx->completed = NULL;

  TEST_VOID(soa_activate(ctx->asynch.b, NULL));

  n = soa_print_sdp(ctx->asynch.b, 1, home, &answer, &answerlen); TEST(n, 1);

  n = soa_parse_sdp(ctx->asynch.a, 1, answer, answerlen); TEST(n, 1);

  n = soa_offer_answer(ctx->asynch.a, 0, test_completed); TEST(n, 0);

  TEST_VOID(soa_activate(ctx->asynch.a, NULL));

  su_home_deinit(home);

  TEST_1(SOA_ACTIVE_SENDRECV == soa_is_audio_active(ctx->asynch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_video_active(ctx->asynch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_image_active(ctx->asynch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_chat_active(ctx->asynch.a));

  TEST_1(SOA_ACTIVE_SENDRECV == soa_is_remote_audio_active(ctx->asynch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_video_active(ctx->asynch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_image_active(ctx->asynch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_chat_active(ctx->asynch.a));

  TEST_VOID(soa_terminate(ctx->asynch.a, NULL));

  TEST_1(SOA_ACTIVE_DISABLED == soa_is_audio_active(ctx->asynch.a));
  TEST_1(SOA_ACTIVE_DISABLED == soa_is_remote_audio_active(ctx->asynch.a));

  TEST_VOID(soa_terminate(ctx->asynch.b, NULL));
  
Pekka Pessi's avatar
Pekka Pessi committed
368 369 370 371 372 373 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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
  END();
}

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

void sig_alarm(int s)
{
  fprintf(stderr, "%s: FAIL! test timeout!\n", name);
  exit(1);
}

void usage(void)
{
  fprintf(stderr, 
	  "usage: %s [-v|-q] [-l level] [-p outbound-proxy-uri]\n", 
	  name);
  exit(1);
}

int main(int argc, char *argv[])
{
  int retval = 0, quit_on_single_failure = 0;
  int i, o_attach = 0, o_alarm = 1;

  struct context ctx[1] = {{ SU_HOME_INIT(ctx) }};

  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      tstflags |= tst_verbatim;
    else if (strcmp(argv[i], "-q") == 0)
      tstflags &= ~tst_verbatim;
    else if (strcmp(argv[i], "-1") == 0)
      quit_on_single_failure = 1;
    else if (strncmp(argv[i], "-l", 2) == 0) {
      int level = 3;
      char *rest = NULL;

      if (argv[i][2])
	level = strtol(argv[i] + 2, &rest, 10);
      else if (argv[i + 1])
	level = strtol(argv[i + 1], &rest, 10), i++;
      else
	level = 3, rest = "";

      if (rest == NULL || *rest)
	usage();
      
      su_log_set_level(soa_log, level);
    }
    else if (strcmp(argv[i], "--attach") == 0) {
      o_attach = 1;
    }
    else if (strcmp(argv[i], "--no-alarm") == 0) {
      o_alarm = 0;
    }
    else if (strcmp(argv[i], "-") == 0) {
      i++; break;
    }
    else if (argv[i][0] != '-') {
      break;
    }
    else
      usage();
  }

  if (o_attach) {
    char line[10];
    printf("%s: pid %u\n", name, getpid());
    printf("<Press RETURN to continue>\n");
    fgets(line, sizeof line, stdin);
  }
#if HAVE_ALARM
  else if (o_alarm) {
    alarm(60);
    signal(SIGALRM, sig_alarm);
  }
#endif

  su_init();

  if (!(TSTFLAGS & tst_verbatim)) {
    su_log_soft_set_level(soa_log, 0);
  }

#define SINGLE_FAILURE_CHECK()						\
Pekka Pessi's avatar
Pekka Pessi committed
457 458
  do { fflush(stdout);							\
    if (retval && quit_on_single_failure) { su_deinit(); return retval; } \
Pekka Pessi's avatar
Pekka Pessi committed
459 460 461
  } while(0)

  retval |= test_api_errors(ctx); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
462
  retval |= test_init(ctx, argv + i); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
463
  if (retval == 0) {
Pekka Pessi's avatar
Pekka Pessi committed
464 465 466
    retval |= test_params(ctx); SINGLE_FAILURE_CHECK();
    retval |= test_static_offer_answer(ctx); SINGLE_FAILURE_CHECK();
    retval |= test_asynch_offer_answer(ctx); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
467
  }
Pekka Pessi's avatar
Pekka Pessi committed
468
  retval |= test_deinit(ctx); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
469 470 471 472 473

  su_deinit();

  return retval;
}