test_nua.c 10.1 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
/*
 * 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_nua.c
 * @brief High-level tester for Sofia SIP User Agent Engine
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
Pekka Pessi's avatar
Pekka Pessi committed
29
 * @author Martti Mela <Martti Mela@nokia.com>
Pekka Pessi's avatar
Pekka Pessi committed
30 31 32 33 34 35
 *
 * @date Created: Wed Aug 17 12:12:12 EEST 2005 ppessi
 */

#include "config.h"

36
#include "test_nua.h"
37 38 39 40 41

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

42 43 44 45
#if defined(_WIN32)
#include <fcntl.h>
#endif

46 47 48 49 50 51
SOFIAPUBVAR su_log_t nua_log[];
SOFIAPUBVAR su_log_t soa_log[];
SOFIAPUBVAR su_log_t nea_log[];
SOFIAPUBVAR su_log_t nta_log[];
SOFIAPUBVAR su_log_t tport_log[];
SOFIAPUBVAR su_log_t su_log_default[];
Pekka Pessi's avatar
Pekka Pessi committed
52 53

char const name[] = "test_nua";
54
int print_headings = 1;
Pekka Pessi's avatar
Pekka Pessi committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
int tstflags = 0;

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

#if HAVE_ALARM
static RETSIGTYPE sig_alarm(int s)
{
  fprintf(stderr, "%s: FAIL! test timeout!\n", name);
  exit(1);
}
#endif

72
static char const options_usage[] =
73 74
  "   -v | --verbose    be verbose\n"
  "   -q | --quiet      be quiet\n"
75
  "   -a | --abort      abort on error\n" 
Pekka Pessi's avatar
Pekka Pessi committed
76
  "   -s                use only single thread\n"
77 78 79 80
  "   -l level          set logging level (0 by default)\n"
  "   -e | --events     print nua events\n"
  "   -A                print nua events for A\n"
  "   -B                print nua events for B\n"
81
  "   -C                print nua events for C\n"
82 83 84 85
  "   --log=a           log messages for A\n"
  "   --log=b           log messages for B\n"
  "   --log=c           log messages for C\n"
  "   --log=proxy       log messages for proxy\n"
86
  "   --attach          print pid, wait for a debugger to be attached\n"
87
  "   --no-proxy        do not use internal proxy\n"
Pekka Pessi's avatar
Pekka Pessi committed
88
  "   --no-nat          do not use internal \"nat\"\n"
Pekka Pessi's avatar
Pekka Pessi committed
89 90
  "   --symmetric       run internal \"nat\" in symmetric mode\n"
  "   -N                print events from internal \"nat\"\n"
91
  "   --loop            loop main tests for ever\n"
92
  "   --no-alarm        don't ask for guard ALARM\n"
93
  "   -p uri            specify uri of outbound proxy (implies --no-proxy)\n"
94
  "   --proxy-tests     run tests involving proxy, too\n"
95 96 97
#if SU_HAVE_OSX_CF_API /* If compiled with CoreFoundation events */
  "   --osx-runloop     use OSX CoreFoundation runloop instead of poll() loop\n"
#endif
98
  "   -k                do not exit after first error\n"
99 100 101
  ;

void usage(int exitcode)
Pekka Pessi's avatar
Pekka Pessi committed
102
{
103
  fprintf(stderr, "usage: %s OPTIONS\n   where OPTIONS are\n%s",
104
	    name, options_usage);
105
  exit(exitcode);
Pekka Pessi's avatar
Pekka Pessi committed
106 107 108 109
}

int main(int argc, char *argv[])
{
110
  int retval = 0;
111
  int i, o_quiet = 0, o_attach = 0, o_alarm = 1, o_loop = 0;
112 113
  int o_events_init = 0, o_events_a = 0, o_events_b = 0, o_events_c = 0;
  int o_iproxy = 1, o_inat = 1;
114
  int o_inat_symmetric = 0, o_inat_logging = 0, o_expensive = 0;
Pekka Pessi's avatar
Pekka Pessi committed
115
  url_t const *o_proxy = NULL;
116
  int level = 0;
Pekka Pessi's avatar
Pekka Pessi committed
117 118 119

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

120 121 122
  if (getenv("EXPENSIVE_CHECKS"))
    o_expensive = 1;

Pekka Pessi's avatar
Pekka Pessi committed
123
  ctx->threading = 1;
124
  ctx->quit_on_single_failure = 1;
Pekka Pessi's avatar
Pekka Pessi committed
125

Pekka Pessi's avatar
Pekka Pessi committed
126 127 128
  endpoint_init(ctx, &ctx->a, 'a');
  endpoint_init(ctx, &ctx->b, 'b');
  endpoint_init(ctx, &ctx->c, 'c');
Pekka Pessi's avatar
Pekka Pessi committed
129

Pekka Pessi's avatar
Pekka Pessi committed
130
  for (i = 1; argv[i]; i++) {
131
    if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0)
Pekka Pessi's avatar
Pekka Pessi committed
132
      tstflags |= tst_verbatim;
Pekka Pessi's avatar
Pekka Pessi committed
133 134
    else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--abort") == 0)
      tstflags |= tst_abort;
135 136
    else if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0)
      tstflags &= ~tst_verbatim, o_quiet = 1;
137
    else if (strcmp(argv[i], "-k") == 0)
138
      ctx->quit_on_single_failure = 0;
Pekka Pessi's avatar
Pekka Pessi committed
139 140 141 142 143 144 145 146 147 148 149
    else if (strncmp(argv[i], "-l", 2) == 0) {
      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)
150
	usage(1);
151

Pekka Pessi's avatar
Pekka Pessi committed
152
      su_log_set_level(nua_log, level);
153
      su_log_soft_set_level(soa_log, level);
154
      su_log_soft_set_level(nea_log, level);
155 156 157 158
      su_log_soft_set_level(nta_log, level);
      su_log_soft_set_level(tport_log, level);
    }
    else if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--events") == 0) {
159 160 161 162
      o_events_init = o_events_a = o_events_b = o_events_c = 1;
    }
    else if (strcmp(argv[i], "-I") == 0) {
      o_events_init = 1;
163 164 165 166 167 168 169
    }
    else if (strcmp(argv[i], "-A") == 0) {
      o_events_a = 1;
    }
    else if (strcmp(argv[i], "-B") == 0) {
      o_events_b = 1;
    }
170 171 172
    else if (strcmp(argv[i], "-C") == 0) {
      o_events_c = 1;
    }
Pekka Pessi's avatar
Pekka Pessi committed
173 174 175
    else if (strcmp(argv[i], "-s") == 0) {
      ctx->threading = 0;
    }
176 177 178
    else if (strcmp(argv[i], "--attach") == 0) {
      o_attach = 1;
    }
179 180
    else if (strncmp(argv[i], "-p", 2) == 0) {
      if (argv[i][2])
Pekka Pessi's avatar
Pekka Pessi committed
181 182
	o_proxy = URL_STRING_MAKE(argv[i] + 2)->us_url;
      else if (!argv[++i] || argv[i][0] == '-')
183
	usage(1);
Pekka Pessi's avatar
Pekka Pessi committed
184
      else
Pekka Pessi's avatar
Pekka Pessi committed
185
	o_proxy = URL_STRING_MAKE(argv[i])->us_url;
Pekka Pessi's avatar
Pekka Pessi committed
186
    }
187 188 189
    else if (strcmp(argv[i], "--proxy-tests") == 0) {
      ctx->proxy_tests = 1;
    }
190 191
    else if (strcmp(argv[i], "--no-proxy") == 0) {
      o_iproxy = 0;
Pekka Pessi's avatar
Pekka Pessi committed
192
    }
Pekka Pessi's avatar
Pekka Pessi committed
193 194 195 196 197 198
    else if (strcmp(argv[i], "--no-nat") == 0) {
      o_inat = 0;
    }
    else if (strcmp(argv[i], "--nat") == 0) {
      o_inat = 1;
    }
Pekka Pessi's avatar
Pekka Pessi committed
199 200 201 202 203 204
    else if (strcmp(argv[i], "--symmetric") == 0) {
      o_inat_symmetric = 1;
    }
    else if (strcmp(argv[i], "-N") == 0) {
      o_inat_logging = 1;
    }
205 206 207
    else if (strcmp(argv[i], "--expensive") == 0) {
      o_expensive = 1;
    }
Pekka Pessi's avatar
Pekka Pessi committed
208 209 210
    else if (strcmp(argv[i], "--no-alarm") == 0) {
      o_alarm = 0;
    }
211 212 213
    else if (strcmp(argv[i], "--loop") == 0) {
      o_alarm = 0, o_loop = 1;
    }
214 215 216 217 218 219 220 221 222 223 224 225
    else if (strcmp(argv[i], "--log=a") == 0) {
      ctx->a.logging = 1;
    }
    else if (strcmp(argv[i], "--log=b") == 0) {
      ctx->b.logging = 1;
    }
    else if (strcmp(argv[i], "--log=c") == 0) {
      ctx->c.logging = 1;
    }
    else if (strcmp(argv[i], "--log=proxy") == 0) {
      ctx->proxy_logging = 1;
    }
226 227 228 229 230
#if SU_HAVE_OSX_CF_API /* If compiled with CoreFoundation events */
    else if (strcmp(argv[i], "--osx-runloop") == 0) {
      ctx->osx_runloop = 1;
    }
#endif
Pekka Pessi's avatar
Pekka Pessi committed
231 232 233 234 235 236
    else if (strcmp(argv[i], "-") == 0) {
      i++; break;
    }
    else if (argv[i][0] != '-') {
      break;
    }
237 238
    else {
      fprintf(stderr, "test_nua: unknown argument \"%s\"\n\n", argv[i]);
239
      usage(1);
240
    }
Pekka Pessi's avatar
Pekka Pessi committed
241 242 243
  }

  if (o_attach) {
244
    char line[10], *l;
245
    printf("%s: pid %lu\n", name, (unsigned long)getpid());
Pekka Pessi's avatar
Pekka Pessi committed
246
    printf("<Press RETURN to continue>\n");
247
    l = fgets(line, sizeof line, stdin);
Pekka Pessi's avatar
Pekka Pessi committed
248 249 250
  }
#if HAVE_ALARM
  else if (o_alarm) {
251
    alarm(o_expensive ? 60 : 120);
Pekka Pessi's avatar
Pekka Pessi committed
252 253 254 255 256 257 258
    signal(SIGALRM, sig_alarm);
  }
#endif

  su_init();

  if (!(TSTFLAGS & tst_verbatim)) {
Pekka Pessi's avatar
Pekka Pessi committed
259 260
    if (level == 0 && !o_quiet)
      level = 1;
261 262
    su_log_soft_set_level(nua_log, level);
    su_log_soft_set_level(soa_log, level);
263
    su_log_soft_set_level(nea_log, level);
264 265
    su_log_soft_set_level(nta_log, level);
    su_log_soft_set_level(tport_log, level);
Pekka Pessi's avatar
Pekka Pessi committed
266 267
  }

Pekka Pessi's avatar
Pekka Pessi committed
268
  if (!o_quiet || (TSTFLAGS & tst_verbatim)
269
      || o_events_a || o_events_b || o_events_c)
270 271
    print_headings = 1;

Pekka Pessi's avatar
Pekka Pessi committed
272 273
#define SINGLE_FAILURE_CHECK()						\
  do { fflush(stdout);							\
274 275
    if (retval && ctx->quit_on_single_failure) {			\
      su_deinit(); return retval; }					\
Pekka Pessi's avatar
Pekka Pessi committed
276 277
  } while(0)

278 279
  ctx->a.printer = o_events_init ? print_event : NULL;

Pekka Pessi's avatar
Pekka Pessi committed
280
  retval |= test_nua_api_errors(ctx); SINGLE_FAILURE_CHECK();
281
  retval |= test_tag_filter(); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
282
  retval |= test_nua_params(ctx); SINGLE_FAILURE_CHECK();
283

Pekka Pessi's avatar
Pekka Pessi committed
284 285 286 287
  retval |= test_nua_init(ctx, o_iproxy, o_proxy, o_inat,
			  TESTNATTAG_SYMMETRIC(o_inat_symmetric),
			  TESTNATTAG_LOGGING(o_inat_logging),
			  TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
288

289 290
  ctx->expensive = o_expensive;

Pekka Pessi's avatar
Pekka Pessi committed
291
  if (retval == 0) {
292
    ctx->a.printer = o_events_a ? print_event : NULL;
293 294
    if (o_events_b)
      ctx->b.printer = print_event;
295 296
    if (o_events_c)
      ctx->c.printer = print_event;
297

298 299
    retval |= test_stack_errors(ctx); SINGLE_FAILURE_CHECK();

300
    retval |= test_register(ctx);
Pekka Pessi's avatar
Pekka Pessi committed
301 302 303 304

    if (retval == 0)
      retval |= test_connectivity(ctx);

305 306 307
    if (retval == 0 && o_inat)
      retval |= test_nat_timeout(ctx);

308
    while (retval == 0) {
Pekka Pessi's avatar
Pekka Pessi committed
309 310 311 312 313
      retval |= test_basic_call(ctx); SINGLE_FAILURE_CHECK();
      retval |= test_reject_a(ctx); SINGLE_FAILURE_CHECK();
      retval |= test_reject_b(ctx); SINGLE_FAILURE_CHECK();
      retval |= test_reject_302(ctx); SINGLE_FAILURE_CHECK();
      retval |= test_reject_401(ctx); SINGLE_FAILURE_CHECK();
314
      retval |= test_mime_negotiation(ctx); SINGLE_FAILURE_CHECK();
315
      retval |= test_call_timeouts(ctx); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
316
      retval |= test_reject_401_aka(ctx); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
317
      retval |= test_call_cancel(ctx); SINGLE_FAILURE_CHECK();
318
      retval |= test_call_destroy(ctx); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
319
      retval |= test_early_bye(ctx); SINGLE_FAILURE_CHECK();
320
      retval |= test_reinvites(ctx); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
321 322
      retval |= test_session_timer(ctx); SINGLE_FAILURE_CHECK();
      retval |= test_refer(ctx); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
323
      retval |= test_100rel(ctx); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
324
      retval |= test_events(ctx); SINGLE_FAILURE_CHECK();
325
      retval |= test_simple(ctx); SINGLE_FAILURE_CHECK();
326
      retval |= test_extension(ctx); SINGLE_FAILURE_CHECK();
327 328
      if (!o_loop)
	break;
Pekka Pessi's avatar
Pekka Pessi committed
329 330
    }

331
    if (ctx->proxy_tests && (retval == 0 || !ctx->p))
332
      retval |= test_unregister(ctx); SINGLE_FAILURE_CHECK();
Pekka Pessi's avatar
Pekka Pessi committed
333
  }
334
  retval |= test_deinit(ctx);
Pekka Pessi's avatar
Pekka Pessi committed
335

336 337
  su_home_deinit(ctx->home);

Pekka Pessi's avatar
Pekka Pessi committed
338 339 340 341
  su_deinit();

  return retval;
}