torture_sresolv.c 12.1 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
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2006 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
 *
 */

/**@internal
 *
 * @CFILE torture_sresolv.c Torture tests for Sofia resolver.
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 73 74 75 76 77 78 79 80 81
 * @author Mikko Haataja
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
 */

#include "config.h"

#if HAVE_STDINT_H
#include <stdint.h>
#elif HAVE_INTTYPES_H
#include <inttypes.h>
#else
#if defined(_WIN32)
typedef unsigned _int8 uint8_t;
typedef unsigned _int16 uint16_t;
typedef unsigned _int32 uint32_t;
#endif
#endif

#if HAVE_FCNTL_H
#include <fcntl.h>
#endif

#if HAVE_NETINET_IN_H
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif

#if HAVE_WINSOCK2_H
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#include <sofia-resolv/sres.h>
#include <sofia-resolv/sres_async.h>
#include <sofia-resolv/sres_record.h>
#include <sofia-resolv/sres_cache.h>

#include <sofia-sip/su_alloc.h>

#include <time.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

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

#define TSTFLAGS tstflags
82
int tstflags, o_timing;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

#include <sofia-sip/tstdef.h>

char const name[] = "torture_sresolv";

struct sres_context_s
{
  su_home_t        home[1];
};

static void test_answer(sres_context_t *ctx,
			sres_query_t *q,
			sres_record_t **answer)
{
}

99
static char name2048[2049] =
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 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
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
  "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
  "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
  "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
  "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
  "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"

  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
  "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
  "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
  "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
  "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
  "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"

  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
  "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
  "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
  "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
  "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
  "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"

  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
  "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
  "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
  "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
  "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
  "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh";

/* Test API function argument validation */
static
int test_api_errors(void)
{
  sres_context_t ctx[1];
  sres_resolver_t *res;
  int s, fd;
  int sockets[20];
  struct sockaddr sa[1] = {{ 0 }};
  char *template = NULL;
  FILE *f;

  BEGIN();

  memset(ctx, 0, sizeof ctx);

  template = su_sprintf(ctx->home, ".torture_sresolv_api.conf.XXXXXX");
  TEST_1(template);
154

155 156 157 158 159 160 161 162
  TEST_1(res = sres_resolver_new(NULL));
  TEST(su_home_threadsafe((su_home_t *)res), 0);
  TEST_VOID(sres_resolver_unref(res));

#ifndef _WIN32
  fd = mkstemp(template); TEST_1(fd != -1);
#else
  fd = open(template, O_WRONLY); TEST_1(fd != -1);
163
#endif
164 165 166 167 168 169 170 171 172 173 174 175

  f = fdopen(fd, "w"); TEST_1(f);
  fprintf(f, "domain example.com\n");
  fclose(f);

  /* Test also LOCALDOMAIN handling */
  putenv("LOCALDOMAIN=localdomain");

  TEST_1(res = sres_resolver_new(template));
  TEST(su_home_threadsafe((su_home_t *)res), 0);

  unlink(template);
176

177
  s = sockets[0];
178

179 180 181 182 183 184 185 186 187 188 189 190
  TEST_P(sres_resolver_ref(NULL), NULL);
  TEST(errno, EFAULT);
  sres_resolver_unref(NULL);

  TEST_P(sres_resolver_set_userdata(NULL, NULL), NULL);
  TEST(errno, EFAULT);

  TEST_P(sres_resolver_get_userdata(NULL), NULL);

  TEST_P(sres_resolver_get_userdata(res), NULL);
  TEST_P(sres_resolver_set_userdata(res, sa), NULL);
  TEST_P(sres_resolver_get_userdata(res), sa);
191
  TEST_P(sres_resolver_set_userdata(res, NULL), sa);
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
  TEST_P(sres_resolver_get_userdata(res), NULL);

  errno = 0;
  TEST_P(sres_query(NULL, test_answer, ctx, sres_type_a, "com"), NULL);
  TEST(errno, EFAULT); errno = 0;
  TEST_P(sres_query(res, test_answer, ctx, sres_type_a, NULL), NULL);
  TEST(errno, EFAULT); errno = 0;
  TEST_P(sres_query_sockaddr(res, test_answer, ctx,
			     sres_qtype_any, sa), NULL);
  TEST(errno, EAFNOSUPPORT); errno = 0;

  TEST_P(sres_cached_answers(NULL, sres_qtype_any, "example.com"), NULL);
  TEST(errno, EFAULT); errno = 0;
  TEST_P(sres_cached_answers(res, sres_qtype_any, NULL), NULL);
  TEST(errno, EFAULT); errno = 0;
  TEST_P(sres_cached_answers(res, sres_qtype_any, name2048), NULL);
  TEST(errno, ENAMETOOLONG); errno = 0;
  TEST_P(sres_cached_answers_sockaddr(res, sres_qtype_any, sa), NULL);
  TEST(errno, EAFNOSUPPORT); errno = 0;

  sres_free_answer(res, NULL);
  sres_free_answers(res, NULL);
  sres_sort_answers(res, NULL);

  sres_free_answer(NULL, NULL);
  sres_free_answers(NULL, NULL);
  sres_sort_answers(NULL, NULL);

  sres_resolver_unref(res);
221

222 223 224
  END();
}

225 226
extern void sres_cache_clean(sres_cache_t *cache, time_t now);

227 228 229 230
#ifndef CLOCK_PROCESS_CPUTIME_ID
#define CLOCK_PROCESS_CPUTIME_ID CLOCK_REALTIME
#endif

231 232 233 234
static
int test_cache(void)
{
  BEGIN();
235

236
  sres_a_record_t *a, a0[1], **all;
237
  sres_record_t **copy;
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
  char host[128];
  sres_cache_t *cache;
  time_t now, base;
  struct timespec t0, t1, t2;

  size_t i, N, N1 = 1000, N3 = 1000000;

  time(&base);

  cache = sres_cache_new(N1);
  TEST_1(cache);

  all = calloc(N3, sizeof *all); if (!all) perror("calloc"), exit(2);

  memset(a0, 0, sizeof a0);

  a0->a_record->r_refcount = 1;
  a0->a_record->r_size = sizeof *a;
  a0->a_record->r_type = sres_type_a;
  a0->a_record->r_class = sres_class_in;
  a0->a_record->r_ttl = 3600;
  a0->a_record->r_rdlen = sizeof a->a_addr;
  a0->a_record->r_parsed = 1;

  for (i = 0, N = N3; i < N; i++) {
    a0->a_record->r_name = host;

    snprintf(host, sizeof host, "%u.example.com.", (unsigned)i);

    a = (sres_a_record_t *)
      sres_cache_alloc_record(cache, (sres_record_t *)a0, 0);

    if (!a)
      perror("sres_cache_alloc_record"), exit(2);

    all[i] = a, a->a_record->r_refcount = 1;
  }

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0);

  for (i = 0, N = N3; i < N; i++) {
    now = base + (3600 * i + N / 2) / N;
    a->a_record->r_ttl = 60 + (i * 60) % 3600;
    sres_cache_store(cache, (sres_record_t *)all[i], now);
  }

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  if (o_timing) {
    t2.tv_sec = t1.tv_sec - t0.tv_sec, t2.tv_nsec = t1.tv_nsec - t0.tv_nsec;
    if (t1.tv_nsec < t0.tv_nsec)
      t2.tv_sec--, t2.tv_nsec += 1000000000;
289
    printf("sres_cache: stored %u entries: %lu.%09lu sec\n",
290
	   (unsigned)N, (long unsigned)t2.tv_sec, t2.tv_nsec);
291 292 293 294 295
  }

  for (i = 0, N; i < N; i++)
    TEST(all[i]->a_record->r_refcount, 2);

296 297 298 299 300
  TEST_1(copy = sres_cache_copy_answers(cache, (sres_record_t **)all));

  for (i = 0, N; i < N; i++)
    TEST(all[i]->a_record->r_refcount, 3);

301 302 303 304 305 306 307 308 309 310
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0);

  for (now = base; now <= base + 3660; now += 30)
    sres_cache_clean(cache, now + 3600);

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  if (o_timing) {
    t2.tv_sec = t1.tv_sec - t0.tv_sec, t2.tv_nsec = t1.tv_nsec - t0.tv_nsec;
    if (t1.tv_nsec < t0.tv_nsec)
      t2.tv_sec--, t2.tv_nsec += 1000000000;
311
    printf("sres_cache: cleaned %u entries: %lu.%09lu sec\n",
312
	   (unsigned)N, (long unsigned)t2.tv_sec, t2.tv_nsec);
313 314
  }

315 316 317 318 319
  for (i = 0, N; i < N; i++)
    TEST(all[i]->a_record->r_refcount, 2);

  sres_cache_free_answers(cache, copy), copy = NULL;

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  for (i = 0, N; i < N; i++)
    TEST(all[i]->a_record->r_refcount, 1);

  base += 24 * 3600;

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0);

  for (i = 0, N; i < N; i++) {
    now = base + (3600 * i + N / 2) / N;
    a->a_record->r_ttl = 60 + (i * 60) % 3600;
    sres_cache_store(cache, (sres_record_t *)all[i], now);
  }

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  if (o_timing) {
    t2.tv_sec = t1.tv_sec - t0.tv_sec, t2.tv_nsec = t1.tv_nsec - t0.tv_nsec;
    if (t1.tv_nsec < t0.tv_nsec)
      t2.tv_sec--, t2.tv_nsec += 1000000000;
338
    printf("sres_cache: stored %u entries: %lu.%09lu sec\n",
339
	   (unsigned)N, (long unsigned)t2.tv_sec, t2.tv_nsec);
340 341 342 343 344 345 346 347 348 349 350
  }

  for (i = 0, N; i < N; i++)
    TEST(all[i]->a_record->r_refcount, 2);

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0);

  for (now = base; now <= base + 3660; now += 1)
    sres_cache_clean(cache, now + 3600);

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
351

352 353 354 355
  if (o_timing) {
    t2.tv_sec = t1.tv_sec - t0.tv_sec, t2.tv_nsec = t1.tv_nsec - t0.tv_nsec;
    if (t1.tv_nsec < t0.tv_nsec)
      t2.tv_sec--, t2.tv_nsec += 1000000000;
356
    printf("sres_cache: cleaned %u entries: %lu.%09lu sec\n",
357
	   (unsigned)N, (long unsigned)t2.tv_sec, t2.tv_nsec);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  }

  for (i = 0, N; i < N; i++) {
    TEST(all[i]->a_record->r_refcount, 1);
    sres_cache_free_one(cache, (sres_record_t *)all[i]);
  }

  sres_cache_unref(cache);

  free(all);

  END();
}


373 374 375 376 377 378 379 380 381 382
#if HAVE_ALARM
static RETSIGTYPE sig_alarm(int s)
{
  fprintf(stderr, "%s: FAIL! test timeout!\n", name);
  exit(1);
}
#endif

void usage(int exitcode)
{
383
  fprintf(stderr,
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
	  "usage: %s OPTIONS [-]\n"
	  "\twhere OPTIONS are\n"
	  "\t    -v be verbose\n"
	  "\t    -a abort on error\n"
	  "\t    -t show timing\n"
	  "\r    --no-alarm  disable timeout\n"
	  "\t    -llevel set debugging level\n",
	  name);
  exit(exitcode);
}

#include <sofia-sip/su_log.h>

extern su_log_t sresolv_log[];

int main(int argc, char **argv)
{
  int i;
  int error = 0;
  int o_alarm = 1;

  for (i = 1; argv[i]; i++) {
    if (argv[i][0] != '-')
      break;
    else if (strcmp(argv[i], "-") == 0) {
      i++; break;
    }
    else if (strcmp(argv[i], "-v") == 0)
      tstflags |= tst_verbatim;
    else if (strcmp(argv[i], "-a") == 0)
      tstflags |= tst_abort;
415 416
    else if (strcmp(argv[i], "-t") == 0)
      o_timing = 1;
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    else if (strcmp(argv[i], "--no-alarm") == 0) {
      o_alarm = 0;
    }
    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(1);
433

434 435 436 437 438 439 440 441 442
      su_log_set_level(sresolv_log, level);
    }
    else
      usage(1);
  }

#if HAVE_ALARM
  if (o_alarm) {
    alarm(60);
443
    signal(SIGALRM, sig_alarm);
444 445 446 447 448 449 450 451
  }
#endif

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

  error |= test_api_errors();
452
  error |= test_cache();
453 454 455

  return error;
}