test_sresolv.c 53.2 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3
/*
 * This file is part of the Sofia-SIP package
 *
Pekka Pessi's avatar
Pekka Pessi committed
4
 * Copyright (C) 2006 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
5 6 7
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * 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 test_sresolv.c Test module for sresolv
28
 *
Pekka Pessi's avatar
Pekka Pessi committed
29
 * @author Mikko Haataja
Pekka Pessi's avatar
Pekka Pessi committed
30 31 32 33 34 35
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
 *
 */

#include "config.h"

36 37 38 39
#if HAVE_STDINT_H
#include <stdint.h>
#elif HAVE_INTTYPES_H
#include <inttypes.h>
40 41 42 43 44 45
#else
#if defined(_WIN32)
typedef unsigned _int8 uint8_t;
typedef unsigned _int16 uint16_t;
typedef unsigned _int32 uint32_t;
#endif
46 47
#endif

48 49 50 51
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif

52
#if HAVE_NETINET_IN_H
Pekka Pessi's avatar
Pekka Pessi committed
53 54
#include <sys/types.h>
#include <sys/socket.h>
55 56
#include <netinet/in.h>
#endif
Pekka Pessi's avatar
Pekka Pessi committed
57

Pekka Pessi's avatar
Pekka Pessi committed
58 59 60 61
#if HAVE_WINSOCK2_H
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
62 63

#include <sofia-resolv/sres.h>
Pekka Pessi's avatar
Pekka Pessi committed
64
#include <sofia-resolv/sres_async.h>
65
#include <sofia-resolv/sres_record.h>
Pekka Pessi's avatar
Pekka Pessi committed
66

67
#include <sofia-sip/su_alloc.h>
Pekka Pessi's avatar
Pekka Pessi committed
68 69 70 71 72

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
73
#include <errno.h>
74

75 76
#define TSTFLAGS tstflags
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
77

78 79
#if HAVE_POLL
#include <poll.h>
Pekka Pessi's avatar
Pekka Pessi committed
80
#elif HAVE_SYS_SELECT_H
81 82 83 84 85 86 87
#include <sys/select.h>
#endif

#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif

Pekka Pessi's avatar
Pekka Pessi committed
88 89 90 91 92
#if HAVE_ALARM
#include <unistd.h>
#include <signal.h>
#endif

93 94 95 96
#include <sofia-sip/su_log.h>

extern su_log_t sresolv_log[];

Pekka Pessi's avatar
Pekka Pessi committed
97 98 99 100 101 102 103 104 105 106
char const name[] = "test_sresolv";

struct sres_context_s
{
  su_home_t        home[1];
  sres_resolver_t *resolver;
  sres_query_t    *query;
  sres_record_t  **result;

  int              timeout;
107
  sres_socket_t    sink;
Pekka Pessi's avatar
Pekka Pessi committed
108 109
  int              sinkidx;
  char const      *sinkconf;
110

Pekka Pessi's avatar
Pekka Pessi committed
111 112
  int              ready;
  int              n_sockets;
113
  sres_socket_t    sockets[SRES_MAX_NAMESERVERS];
114 115
#if HAVE_POLL
  struct pollfd    fds[SRES_MAX_NAMESERVERS];
Pekka Pessi's avatar
Pekka Pessi committed
116 117 118 119 120 121 122 123 124 125
#endif
};

static void test_answer(sres_context_t *ctx, sres_query_t *query,
			sres_record_t **answer);
static void test_answer_multi(sres_context_t *ctx, sres_query_t *query,
			      sres_record_t **answer);

static int tstflags = 0;

126 127 128
#if HAVE_WINSOCK2_H

/* Posix send() */
129
su_inline
130 131 132 133 134 135 136
ssize_t sres_send(sres_socket_t s, void *b, size_t length, int flags)
{
  if (length > INT_MAX)
    length = INT_MAX;
  return (ssize_t)send(s, b, (int)length, flags);
}

137
su_inline
138 139 140 141 142 143 144 145 146
ssize_t sres_sendto(sres_socket_t s, void *b, size_t length, int flags,
		    struct sockaddr const *sa, socklen_t salen)
{
  if (length > INT_MAX)
    length = INT_MAX;
  return (ssize_t)sendto(s, b, (int)length, flags, (void *)sa, (int)salen);
}

/* Posix recvfrom() */
147
su_inline
148 149 150 151 152 153 154 155 156 157 158
ssize_t sres_recvfrom(sres_socket_t s, void *buffer, size_t length, int flags,
		      struct sockaddr *from, socklen_t *fromlen)
{
  int retval, ilen;

  if (fromlen)
    ilen = *fromlen;

  if (length > INT_MAX)
    length = INT_MAX;

159
  retval = recvfrom(s, buffer, (int)length, flags,
160 161 162 163 164 165 166 167 168 169 170 171 172
		    (void *)from, fromlen ? &ilen : NULL);

  if (fromlen)
    *fromlen = ilen;

  return (ssize_t)retval;
}

static sres_socket_t sres_socket(int af, int socktype, int protocol)
{
  return socket(af, socktype, protocol);
}

173
su_inline
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
int sres_close(sres_socket_t s)
{
  return closesocket(s);
}

#if !defined(IPPROTO_IPV6)
#if HAVE_SIN6
#include <tpipv6.h>
#else
#if !defined(__MINGW32__)
struct sockaddr_storage {
    short ss_family;
    char ss_pad[126];
};
#endif
#endif
#endif
191 192 193 194 195

SOFIAPUBFUN int su_inet_pton(int af, char const *src, void *dst);
SOFIAPUBFUN const char *su_inet_ntop(int af, void const *src,
				  char *dst, size_t size);

196 197 198 199 200 201 202 203 204 205 206
#else

#define sres_send(s,b,len,flags) send((s),(b),(len),(flags))
#define sres_sendto(s,b,len,flags,a,alen) \
  sendto((s),(b),(len),(flags),(a),(alen))
#define sres_recvfrom(s,b,len,flags,a,alen) \
  recvfrom((s),(b),(len),(flags),(a),(alen))
#define sres_close(s) close((s))
#define SOCKET_ERROR   (-1)
#define INVALID_SOCKET ((sres_socket_t)-1)
#define sres_socket(x,y,z) socket((x),(y),(z))
207 208 209 210

#define su_inet_pton inet_pton
#define su_inet_ntop inet_ntop

211 212
#endif

213
#if 1
214 215 216

#if HAVE_POLL && 0

Pekka Pessi's avatar
Pekka Pessi committed
217
static
218
int setblocking(sres_socket_t s, int blocking)
Pekka Pessi's avatar
Pekka Pessi committed
219 220 221 222 223 224
{
  unsigned mode = fcntl(s, F_GETFL, 0);

  if (mode < 0)
     return -1;

225
  if (blocking)
Pekka Pessi's avatar
Pekka Pessi committed
226 227 228 229 230 231 232 233 234 235 236 237
    mode &= ~(O_NDELAY | O_NONBLOCK);
  else
    mode |= O_NDELAY | O_NONBLOCK;

  return fcntl(s, F_SETFL, mode);
}

/** Test few assumptions about sockets */
static
int test_socket(sres_context_t *ctx)
{
  int af;
238
  sres_socket_t s1, s2, s3, s4;
Pekka Pessi's avatar
Pekka Pessi committed
239 240 241 242 243 244
  struct sockaddr_storage a[1];
  struct sockaddr_storage a1[1], a2[1], a3[1], a4[1];
  struct sockaddr_in *sin = (void *)a;
  struct sockaddr_in *sin3 = (void *)a3, *sin4 = (void *)a4;
  struct sockaddr *sa = (void *)a;
  struct sockaddr *sa3 = (void *)a3, *sa4 = (void *)a4;
245
  socklen_t alen, a1len, a2len, a3len, a4len;
Pekka Pessi's avatar
Pekka Pessi committed
246 247 248 249 250 251 252
  char buf[16];

  BEGIN();

  af = AF_INET;

  for (;;) {
253 254 255 256
    TEST_1((s1 = sres_socket(af, SOCK_DGRAM, 0)) != INVALID_SOCKET);
    TEST_1((s2 = sres_socket(af, SOCK_DGRAM, 0)) != INVALID_SOCKET);
    TEST_1((s3 = sres_socket(af, SOCK_DGRAM, 0)) != INVALID_SOCKET);
    TEST_1((s4 = sres_socket(af, SOCK_DGRAM, 0)) != INVALID_SOCKET);
Pekka Pessi's avatar
Pekka Pessi committed
257 258 259 260 261

    TEST_1(setblocking(s1, 0) == 0);
    TEST_1(setblocking(s2, 0) == 0);
    TEST_1(setblocking(s3, 0) == 0);
    TEST_1(setblocking(s4, 0) == 0);
262

Pekka Pessi's avatar
Pekka Pessi committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    memset(a, 0, sizeof a);
    memset(a1, 0, sizeof a1);
    memset(a2, 0, sizeof a2);
    memset(a3, 0, sizeof a3);
    memset(a4, 0, sizeof a4);

#if HAVE_SA_LEN
    a1->ss_len = a2->ss_len = a3->ss_len = a4->ss_len = sizeof a1;
#endif
    a1->ss_family = a2->ss_family = a3->ss_family = a4->ss_family = af;

    if (af == AF_INET)
      a1len = a2len = a3len = a4len = sizeof (struct sockaddr_in);
    else
      a1len = a2len = a3len = a4len = sizeof (struct sockaddr_in6);

    if (af == AF_INET) {
280 281
      TEST_1(su_inet_pton(af, "127.0.0.1", &sin3->sin_addr) > 0);
      TEST_1(su_inet_pton(af, "127.0.0.1", &sin4->sin_addr) > 0);
Pekka Pessi's avatar
Pekka Pessi committed
282 283 284 285 286
    } else {
    }

    TEST(bind(s3, (struct sockaddr *)a3, a3len), 0);
    TEST(bind(s4, (struct sockaddr *)a4, a4len), 0);
287

Pekka Pessi's avatar
Pekka Pessi committed
288 289
    alen = sizeof a;
    TEST(getsockname(s3, (struct sockaddr *)a, &alen), 0);
290
    sin3->sin_port = sin->sin_port;
Pekka Pessi's avatar
Pekka Pessi committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304
    memset(sin->sin_zero, 0, sizeof sin->sin_zero);
    TEST(alen, a3len); TEST_M(a, a3, a3len);

    alen = sizeof a;
    TEST(getsockname(s4, (struct sockaddr *)a, &alen), 0);
    sin4->sin_port = sin->sin_port;
    memset(sin->sin_zero, 0, sizeof sin->sin_zero);
    TEST(alen, a4len); TEST_M(a, a4, a4len);

    TEST(connect(s1, sa3, a3len), 0);
    TEST(getsockname(s1, (struct sockaddr *)a1, &a1len), 0);
    TEST(connect(s2, sa4, a4len), 0);
    TEST(getsockname(s2, (struct sockaddr *)a2, &a2len), 0);

305 306 307 308 309
    TEST(sres_sendto(s1, "foo", 3, 0, sa4, a4len), 3);
    TEST(sres_recvfrom(s4, buf, sizeof buf, 0, sa, &alen), 3);
    TEST(sres_sendto(s4, "bar", 3, 0, sa, alen), 3);
    TEST(sres_recvfrom(s2, buf, sizeof buf, 0, sa, &alen), -1);
    TEST(sres_recvfrom(s1, buf, sizeof buf, 0, sa, &alen), 3);
Pekka Pessi's avatar
Pekka Pessi committed
310

311
    sres_close(s1), sres_close(s2), sres_close(s3), sres_close(s4);
Pekka Pessi's avatar
Pekka Pessi committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

    break;
  }

  END();
}

#endif

static unsigned offset;

#define TEST_RUN(ctx) \
  { sres_free_answers(ctx->resolver, ctx->result); ctx->result = NULL;	\
    ctx->query = NULL; run(ctx); TEST_1(ctx->query); }

327

328 329
static
int poll_sockets(sres_context_t *ctx)
Pekka Pessi's avatar
Pekka Pessi committed
330 331 332 333 334
{
  int i, n, events;

  n = ctx->n_sockets;

335
  events = poll(ctx->fds, ctx->n_sockets, ctx->timeout ? 50 : 500);
336

337 338 339 340 341 342 343 344 345
  if (!events)
    return events;

  for (i = 0; i < n; i++) {
    if (ctx->fds[i].revents & POLLERR)
      sres_resolver_error(ctx->resolver, ctx->fds[i].fd);
    if (ctx->fds[i].revents & POLLIN)
      sres_resolver_receive(ctx->resolver, ctx->fds[i].fd);
  }
346

347 348 349 350 351 352 353 354
  return events;
}

#define BREAK(ctx) (ctx->ready = 1)
static void run(sres_context_t *ctx)
{
  for (ctx->ready = 0; !ctx->ready; ) {
    poll_sockets(ctx);
Pekka Pessi's avatar
Pekka Pessi committed
355 356 357 358 359

    if (ctx->timeout) {
      ctx->timeout <<= 1;
      offset += ctx->timeout;
    }
360

Pekka Pessi's avatar
Pekka Pessi committed
361
    /* No harm is done (except wasted CPU) if timer is called more often */
362
    sres_resolver_timer(ctx->resolver, -1);
Pekka Pessi's avatar
Pekka Pessi committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
  }
}

int test_soa(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_soa_record_t *rr_soa;

  char const *domain = "example.com";

  BEGIN();

  TEST_1(sres_query(res, test_answer, ctx, sres_type_soa, domain));
  TEST_RUN(ctx);

379
  TEST_1(sres_query(res, test_answer, ctx, sres_type_soa, domain));
Pekka Pessi's avatar
Pekka Pessi committed
380 381 382
  TEST_RUN(ctx);

  TEST_1(result = sres_cached_answers(res, sres_type_soa, domain));
383

Pekka Pessi's avatar
Pekka Pessi committed
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
  TEST_1(result != NULL);
  TEST_1(result[0] != NULL);

  rr_soa = result[0]->sr_soa;
  TEST(rr_soa->soa_record->r_type, sres_type_soa);
  TEST(rr_soa->soa_record->r_class, sres_class_in);

  TEST_S(rr_soa->soa_mname, "ns.example.com.");
  TEST_S(rr_soa->soa_rname, "root.example.com.");
  TEST(rr_soa->soa_serial, 2002042901);
  TEST(rr_soa->soa_refresh, 7200);
  TEST(rr_soa->soa_retry, 600);
  TEST(rr_soa->soa_expire, 36000000);
  TEST(rr_soa->soa_minimum, 60);

  sres_free_answers(res, result);

  END();
}

int test_naptr(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_naptr_record_t *rr;
  char const *domain = "example.com";
  int i;

  BEGIN();

414
  TEST_1(sres_query(res, test_answer, ctx, sres_type_naptr, domain));
Pekka Pessi's avatar
Pekka Pessi committed
415 416 417 418 419 420 421
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  TEST_1(result[0]);

  for (i = 0; result[i] != NULL; i++) {
    rr = (sres_naptr_record_t *) result[i]->sr_naptr;
422

Pekka Pessi's avatar
Pekka Pessi committed
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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    switch(rr->na_order) {
    case 20:
      TEST(rr->na_record->r_type, sres_type_naptr);
      TEST(rr->na_record->r_class, sres_class_in);
      TEST(rr->na_record->r_ttl, 60);
      TEST(rr->na_order, 20);
      TEST(rr->na_prefer, 50);
      TEST_S(rr->na_flags, "s");
      TEST_S(rr->na_services, "SIPS+D2T");
      TEST_S(rr->na_regexp, "");
      TEST_S(rr->na_replace, "_sips._tcp.example.com.");
      break;

    case 40:
      TEST(rr->na_record->r_type, sres_type_naptr);
      TEST(rr->na_record->r_class, sres_class_in);
      TEST(rr->na_record->r_ttl, 60);
      TEST(rr->na_order, 40);
      TEST(rr->na_prefer, 15);
      TEST_S(rr->na_flags, "s");
      TEST_S(rr->na_services, "SIP+D2U");
      TEST_S(rr->na_regexp, "");
      TEST_S(rr->na_replace, "_sip._udp.example.com.");
      break;

    case 50:
      TEST(rr->na_record->r_type, sres_type_naptr);
      TEST(rr->na_record->r_class, sres_class_in);
      TEST(rr->na_record->r_ttl, 60);
      TEST(rr->na_order, 50);
      TEST(rr->na_prefer, 15);
      TEST_S(rr->na_flags, "u");
      TEST_S(rr->na_services, "TEST+D2U");
      TEST_S(rr->na_regexp, "/(tst:([^@]+@))?example.com$/\\1operator.com/i");
      TEST_S(rr->na_replace, ".");
      break;

    case 80:
      TEST(rr->na_record->r_type, sres_type_naptr);
      TEST(rr->na_record->r_class, sres_class_in);
      TEST(rr->na_record->r_ttl, 60);
      TEST(rr->na_order, 80);
      TEST(rr->na_prefer, 25);
      TEST_S(rr->na_flags, "s");
      TEST_S(rr->na_services, "SIP+D2T");
      TEST_S(rr->na_regexp, "");
      TEST_S(rr->na_replace, "_sip._tcp.example.com.");
      break;

    default:
      TEST_1(0);
    }
  }

  sres_free_answers(res, ctx->result), ctx->result = NULL;

  END();
}

482
char const longname[1026] =
Pekka Pessi's avatar
Pekka Pessi committed
483 484 485 486 487 488 489 490
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
491

Pekka Pessi's avatar
Pekka Pessi committed
492 493 494 495 496 497 498 499
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
500

Pekka Pessi's avatar
Pekka Pessi committed
501 502 503 504 505 506 507 508
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
509

Pekka Pessi's avatar
Pekka Pessi committed
510 511 512 513 514 515 516 517 518
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.";

519
char name2048[2049] =
Pekka Pessi's avatar
Pekka Pessi committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
  "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";

int test_a(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_query_t *query;
  sres_record_t **result;
  const sres_a_record_t *rr_a;
  char const *domain = "sip00.example.com";
563
  char name1025[1026] =
Pekka Pessi's avatar
Pekka Pessi committed
564 565 566 567 568 569 570 571
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
572

Pekka Pessi's avatar
Pekka Pessi committed
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
590

Pekka Pessi's avatar
Pekka Pessi committed
591 592 593 594 595 596 597 598
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
599

Pekka Pessi's avatar
Pekka Pessi committed
600 601 602 603
		  ".";

  BEGIN();

604
  TEST_1(!sres_query(res, test_answer, ctx, sres_type_a, name1025));
Pekka Pessi's avatar
Pekka Pessi committed
605 606 607

  name1025[1024] = '\0';

608
  TEST_1(!sres_query(res, test_answer, ctx, sres_type_a, name1025));
Pekka Pessi's avatar
Pekka Pessi committed
609 610 611

  name1025[1023] = '\0';

612
  TEST_1(!sres_query(res, test_answer, ctx, sres_type_a, name1025));
Pekka Pessi's avatar
Pekka Pessi committed
613

614
  TEST_1(!sres_query(res, test_answer, ctx, sres_type_a, name2048));
Pekka Pessi's avatar
Pekka Pessi committed
615

616
  query = sres_query(res, test_answer, ctx, sres_type_a, longname);
Pekka Pessi's avatar
Pekka Pessi committed
617 618
  TEST_1(query);
  sres_query_bind(query, NULL, NULL);
619

620
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a, domain));
Pekka Pessi's avatar
Pekka Pessi committed
621 622 623 624

  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
625
  TEST_1(result[0]);
Pekka Pessi's avatar
Pekka Pessi committed
626 627 628 629 630
  TEST(result[0]->sr_record->r_status, 0);
  TEST_1(rr_a = result[0]->sr_a);
  TEST(rr_a->a_record->r_type, sres_type_a);
  TEST(rr_a->a_record->r_class, sres_class_in);
  TEST(rr_a->a_record->r_ttl, 60);
631

Pekka Pessi's avatar
Pekka Pessi committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
  TEST_S(inet_ntoa(rr_a->a_addr), "194.2.188.133");

  sres_free_answers(res, ctx->result), ctx->result = NULL;

  TEST_1(result = sres_cached_answers(res, sres_type_a, domain));
  TEST(result[0]->sr_record->r_status, 0);
  TEST_1(rr_a = result[0]->sr_a);
  TEST(rr_a->a_record->r_type, sres_type_a);
  TEST(rr_a->a_record->r_class, sres_class_in);
  TEST(rr_a->a_record->r_ttl, 60);
  TEST_S(inet_ntoa(rr_a->a_addr), "194.2.188.133");

  sres_free_answers(res, result);

  /* Try sub-queries */
Pekka Pessi's avatar
Pekka Pessi committed
647
  TEST_1(sres_search(res, test_answer, ctx, sres_type_a, "sip00"));
Pekka Pessi's avatar
Pekka Pessi committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

  TEST_RUN(ctx);

  TEST_1(result = ctx->result);

  for (;*result; result++)
    if (result[0]->sr_a->a_record->r_type == sres_type_a)
      break;

  TEST(result[0]->sr_record->r_status, 0);
  TEST_1(rr_a = result[0]->sr_a);
  TEST(rr_a->a_record->r_type, sres_type_a);
  TEST(rr_a->a_record->r_class, sres_class_in);
  TEST(rr_a->a_record->r_ttl, 60);
  TEST_S(inet_ntoa(rr_a->a_addr), "194.2.188.133");

  sres_free_answers(res, ctx->result), ctx->result = NULL;

  /* Try missing domain */
667
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a,
Pekka Pessi's avatar
Pekka Pessi committed
668 669 670 671 672 673 674 675 676 677 678 679
			 "no-sip.example.com"));

  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  TEST(result[0]->sr_record->r_status, SRES_NAME_ERR);
  TEST_1(rr_a = result[0]->sr_a);
  TEST(rr_a->a_record->r_type, sres_type_a);
  TEST(rr_a->a_record->r_class, sres_class_in);
  /* Error gets TTL from example.com SOA record minimum time */
  TEST(rr_a->a_record->r_ttl, 60);

680 681
  sres_free_answers(res, ctx->result), ctx->result = NULL;

682
  /* Try domain without A record =>
Pekka Pessi's avatar
Pekka Pessi committed
683
     we should get a record with SRES_RECORD_ERR */
684
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a,
Pekka Pessi's avatar
Pekka Pessi committed
685 686 687 688 689 690 691 692 693 694 695 696
			 "aaaa.example.com"));

  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  TEST(result[0]->sr_record->r_status, SRES_RECORD_ERR);
  TEST_1(rr_a = result[0]->sr_a);
  TEST(rr_a->a_record->r_type, sres_type_a);
  TEST(rr_a->a_record->r_class, sres_class_in);
  /* Error gets TTL from example.com SOA record minimum time */
  TEST(rr_a->a_record->r_ttl, 60);

697 698
  sres_free_answers(res, ctx->result), ctx->result = NULL;

Pekka Pessi's avatar
Pekka Pessi committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712
  /* Cached search */
  TEST_1(result = sres_search_cached_answers(res, sres_type_a, "sip00"));
  TEST_1(rr_a = result[0]->sr_a);
  TEST(rr_a->a_record->r_status, 0);
  TEST_S(rr_a->a_record->r_name, "sip00.example.com.");
  TEST(rr_a->a_record->r_type, sres_type_a);
  TEST(rr_a->a_record->r_class, sres_class_in);
  TEST(rr_a->a_record->r_ttl, 60);
  TEST_S(inet_ntoa(rr_a->a_addr), "194.2.188.133");

  if (result[1]) {
    TEST(result[1]->sr_a->a_record->r_type, sres_type_a);
  }

713 714 715 716 717 718 719 720 721 722 723
  sres_free_answers(res, result), result = NULL;

  /* Cached search */
  TEST_1(result = sres_cached_answers(res, sres_type_a, "no-sip.example.com"));
  TEST_1(rr_a = result[0]->sr_a);
  TEST(rr_a->a_record->r_status, SRES_NAME_ERR);
  TEST(rr_a->a_record->r_type, sres_type_a);
  TEST(rr_a->a_record->r_class, sres_class_in);

  sres_free_answers(res, result), result = NULL;

Pekka Pessi's avatar
Pekka Pessi committed
724 725 726
  END();
}

727
#if HAVE_SIN6
Pekka Pessi's avatar
Pekka Pessi committed
728 729 730 731 732 733 734 735 736 737
int test_a6(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_a6_record_t *rr_a6;
  char buf[50] = {0};
  char const *domain = "oldns.example.com";

  BEGIN();

738
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a6, domain));
Pekka Pessi's avatar
Pekka Pessi committed
739 740 741 742
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  TEST_1(result[0]);
743

Pekka Pessi's avatar
Pekka Pessi committed
744 745 746 747 748 749
  rr_a6 = result[0]->sr_a6;
  TEST(rr_a6->a6_record->r_type, sres_type_a6);
  TEST(rr_a6->a6_record->r_class, sres_class_in);
  TEST(rr_a6->a6_record->r_ttl, 60);
  TEST(rr_a6->a6_prelen, 0);

750
  TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)),
Pekka Pessi's avatar
Pekka Pessi committed
751 752
	 "3ffe:1200:3012:c000:210:a4ff:fe8d:6a46");

753
  TEST_P(rr_a6->a6_prename, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
754 755 756 757 758 759 760 761 762 763

  sres_free_answers(res, ctx->result), ctx->result = NULL;

  TEST_1(result = sres_cached_answers(res, sres_type_a6, domain));
  TEST_1(rr_a6 = result[0]->sr_a6);
  TEST(rr_a6->a6_record->r_type, sres_type_a6);
  TEST(rr_a6->a6_record->r_class, sres_class_in);
  TEST(rr_a6->a6_record->r_ttl, 60);
  TEST(rr_a6->a6_prelen, 0);

764
  TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)),
Pekka Pessi's avatar
Pekka Pessi committed
765 766
	 "3ffe:1200:3012:c000:210:a4ff:fe8d:6a46");

767
  TEST_P(rr_a6->a6_prename, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783

  sres_free_answers(res, result), result = NULL;

  END();
}

int test_a6_prefix(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_a6_record_t *rr_a6;
  char buf[50] = {0};
  char const *domain = "a6.example.com";

  BEGIN();

784
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a6, domain));
Pekka Pessi's avatar
Pekka Pessi committed
785 786 787 788 789 790 791
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  TEST_1(rr_a6 = result[0]->sr_a6);
  TEST(rr_a6->a6_record->r_type, sres_type_a6);
  TEST(rr_a6->a6_record->r_class, sres_class_in);
  TEST(rr_a6->a6_record->r_ttl, 60);
792 793
  TEST(rr_a6->a6_prelen, 64);

794
  TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)),
795 796 797 798 799
	 "::a08:20ff:fe7d:e7ac");

  TEST_S(rr_a6->a6_prename, "mynet.example.com.");

  sres_free_answers(res, ctx->result), ctx->result = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
800

801 802 803 804 805 806 807 808 809 810
  /* Check parsing of special case: no prefix */
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a6, "full.example.com"));
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  TEST_1(rr_a6 = result[0]->sr_a6);
  TEST(rr_a6->a6_record->r_type, sres_type_a6);
  TEST(rr_a6->a6_record->r_class, sres_class_in);
  TEST(rr_a6->a6_record->r_ttl, 60);
  TEST(rr_a6->a6_prelen, 0);
811
  TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)),
812
	 "3ff0:12:3012:c006:a08:20ff:fe7d:e7ac");
813
  TEST_P(rr_a6->a6_prename, NULL);
814

815
  sres_free_answers(res, ctx->result), ctx->result = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
816

817 818 819
  /* Check parsing of special case: no suffix */
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a6, "alias6.example.com"));
  TEST_RUN(ctx);
Pekka Pessi's avatar
Pekka Pessi committed
820

821 822 823 824 825 826
  TEST_1(result = ctx->result);
  TEST_1(rr_a6 = result[0]->sr_a6);
  TEST(rr_a6->a6_record->r_type, sres_type_a6);
  TEST(rr_a6->a6_record->r_class, sres_class_in);
  TEST(rr_a6->a6_record->r_ttl, 60);
  TEST(rr_a6->a6_prelen, 128);
827
  TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)), "::");
828
  TEST_S(rr_a6->a6_prename, "a6.example.com.");
829

Pekka Pessi's avatar
Pekka Pessi committed
830 831 832 833 834 835 836 837
  sres_free_answers(res, ctx->result), ctx->result = NULL;

  TEST_1(result = sres_cached_answers(res, sres_type_a6, domain));

  TEST_1(rr_a6 = result[0]->sr_a6);
  TEST(rr_a6->a6_record->r_type, sres_type_a6);
  TEST(rr_a6->a6_record->r_class, sres_class_in);
  TEST(rr_a6->a6_record->r_ttl, 60);
838
  TEST(rr_a6->a6_prelen, 64);
Pekka Pessi's avatar
Pekka Pessi committed
839

840
  TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)),
841
	 "::a08:20ff:fe7d:e7ac");
Pekka Pessi's avatar
Pekka Pessi committed
842

843
  TEST_S(rr_a6->a6_prename, "mynet.example.com.");
Pekka Pessi's avatar
Pekka Pessi committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859

  sres_free_answers(res, result), result = NULL;

  END();
}

int test_aaaa(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_aaaa_record_t *rr_aaaa;
  char buf[50] = {0};
  char const *domain = "sip03.example.com";

  BEGIN();

860
  TEST_1(sres_query(res, test_answer, ctx, sres_type_aaaa, domain));
Pekka Pessi's avatar
Pekka Pessi committed
861 862 863 864 865 866 867 868
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  TEST_1(rr_aaaa = result[0]->sr_aaaa);
  TEST(rr_aaaa->aaaa_record->r_type, sres_type_aaaa);
  TEST(rr_aaaa->aaaa_record->r_class, sres_class_in);
  TEST(rr_aaaa->aaaa_record->r_ttl, 60);

869
  TEST_S(su_inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)),
Pekka Pessi's avatar
Pekka Pessi committed
870 871 872 873 874
	 "3ffe:1200:3012:c000:206:5bff:fe55:4630");

  sres_free_answers(res, ctx->result), ctx->result = NULL;

  TEST_1(result = sres_cached_answers(res, sres_type_aaaa, domain));
875

Pekka Pessi's avatar
Pekka Pessi committed
876 877 878 879 880
  TEST_1(rr_aaaa = result[0]->sr_aaaa);
  TEST(rr_aaaa->aaaa_record->r_type, sres_type_aaaa);
  TEST(rr_aaaa->aaaa_record->r_class, sres_class_in);
  TEST(rr_aaaa->aaaa_record->r_ttl, 60);

881
  TEST_S(su_inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)),
Pekka Pessi's avatar
Pekka Pessi committed
882 883 884 885 886 887
	 "3ffe:1200:3012:c000:206:5bff:fe55:4630");

  sres_free_answers(res, result), result = NULL;

  END();
}
888
#endif /* HAVE_SIN6 */
Pekka Pessi's avatar
Pekka Pessi committed
889 890 891 892 893 894 895 896 897 898 899

int test_srv(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_srv_record_t *rr;
  char const *domain = "_sips._tcp.example.com";
  int i;

  BEGIN();

900
  TEST_1(sres_query(res, test_answer, ctx, sres_type_srv, domain));
Pekka Pessi's avatar
Pekka Pessi committed
901 902 903 904 905 906
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);

  for (i = 0; result[i] != NULL; i++) {
    TEST_1(rr = (sres_srv_record_t *) result[i]->sr_srv);
907

Pekka Pessi's avatar
Pekka Pessi committed
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    switch(rr->srv_priority) {
    case 3:
      TEST(rr->srv_record->r_type, sres_type_srv);
      TEST(rr->srv_record->r_class, sres_class_in);
      TEST(rr->srv_record->r_ttl, 60);
      TEST(rr->srv_weight, 100);
      TEST(rr->srv_port, 5061);
      TEST_S(rr->srv_target, "sip00.example.com.");
      break;

    case 4:
      TEST(rr->srv_record->r_type, sres_type_srv);
      TEST(rr->srv_record->r_class, sres_class_in);
      TEST(rr->srv_record->r_ttl, 60);
      TEST(rr->srv_weight, 50);
      TEST(rr->srv_port, 5050);
      TEST_S(rr->srv_target, "sip02.example.com.");
      break;

    case 5:
      TEST(rr->srv_record->r_type, sres_type_srv);
      TEST(rr->srv_record->r_class, sres_class_in);
      TEST(rr->srv_record->r_ttl, 60);
      TEST(rr->srv_weight, 10);
      TEST(rr->srv_port, 5060);
      TEST_S(rr->srv_target, "sip01.example.com.");
      break;

    default:
      TEST_1(0);
    }
  }

  sres_free_answers(res, ctx->result), ctx->result = NULL;

  TEST_1(result = sres_cached_answers(res, sres_type_srv, domain));

  for (i = 0; result[i] != NULL; i++) {
    TEST_1(rr = (sres_srv_record_t *) result[i]->sr_srv);
947

Pekka Pessi's avatar
Pekka Pessi committed
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
    switch(rr->srv_priority) {
    case 3:
      TEST(rr->srv_record->r_type, sres_type_srv);
      TEST(rr->srv_record->r_class, sres_class_in);
      TEST(rr->srv_record->r_ttl, 60);
      TEST(rr->srv_weight, 100);
      TEST(rr->srv_port, 5061);
      TEST_S(rr->srv_target, "sip00.example.com.");
      break;

    case 4:
      TEST(rr->srv_record->r_type, sres_type_srv);
      TEST(rr->srv_record->r_class, sres_class_in);
      TEST(rr->srv_record->r_ttl, 60);
      TEST(rr->srv_weight, 50);
      TEST(rr->srv_port, 5050);
      TEST_S(rr->srv_target, "sip02.example.com.");
      break;

    case 5:
      TEST(rr->srv_record->r_type, sres_type_srv);
      TEST(rr->srv_record->r_class, sres_class_in);
      TEST(rr->srv_record->r_ttl, 60);
      TEST(rr->srv_weight, 10);
      TEST(rr->srv_port, 5060);
      TEST_S(rr->srv_target, "sip01.example.com.");
      break;

    default:
      TEST_1(0);
    }
  }

  sres_free_answers(res, result), result = NULL;

  END();
}

int test_cname(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
989 990
  sres_record_t **result, *sr;
  const sres_cname_record_t *cn;
Pekka Pessi's avatar
Pekka Pessi committed
991 992 993 994
  char const *domain = "sip.example.com";

  BEGIN();

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
  TEST_1(sres_query(res, test_answer, ctx, sres_type_naptr, domain));
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  TEST_1(sr = result[0]);
  TEST_1(sr->sr_record->r_status == SRES_RECORD_ERR);

  sres_free_answers(res, ctx->result), ctx->result = NULL;

  TEST_1(result = sres_cached_answers(res, sres_type_naptr, domain));
  TEST_1(sr = result[0]);
  TEST_1(sr->sr_record->r_status == SRES_RECORD_ERR);

  sres_free_answers(res, result), ctx->result = NULL;

  TEST_1(result = sres_cached_answers(res, sres_qtype_any, domain));
  TEST_1(cn = result[0]->sr_cname);
  TEST(cn->cn_record->r_class, sres_class_in);
  TEST(cn->cn_record->r_type, sres_type_cname);
  TEST(cn->cn_record->r_ttl, 60);
  TEST_S(cn->cn_cname, "sip00.example.com.");
  /* We might have A record, or then not */

  sres_free_answers(res, result), ctx->result = NULL;

  TEST_1(sres_query(res, test_answer, ctx, sres_type_a, domain));
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
1024
  TEST_1(sr = result[0]);
1025 1026 1027 1028 1029 1030
  TEST(sr->sr_record->r_class, sres_class_in);
  TEST(sr->sr_record->r_type, sres_type_a);

  sres_free_answers(res, ctx->result), ctx->result = NULL;

  TEST_1(result = sres_cached_answers(res, sres_type_a, domain));
1031
  TEST_1(sr = result[0]);
1032 1033 1034 1035 1036 1037 1038
  TEST(sr->sr_record->r_class, sres_class_in);
  TEST(sr->sr_record->r_type, sres_type_a);

  sres_free_answers(res, result);

  domain = "cloop.example.com";

1039
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a, domain));
Pekka Pessi's avatar
Pekka Pessi committed
1040 1041 1042
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
1043 1044
  TEST_1(sr = result[0]);
  TEST_1(sr->sr_record->r_status == SRES_RECORD_ERR);
Pekka Pessi's avatar
Pekka Pessi committed
1045 1046 1047

  sres_free_answers(res, ctx->result), ctx->result = NULL;

1048 1049 1050 1051 1052 1053
  TEST_1(result = sres_cached_answers(res, sres_type_a, domain));
  TEST_1(sr = result[0]);
  TEST_1(sr->sr_record->r_status == SRES_RECORD_ERR);

  sres_free_answers(res, result);

Pekka Pessi's avatar
Pekka Pessi committed
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
  END();
}

int test_ptr_ipv4(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_ptr_record_t *rr;
  char const *domain = "1.0.0.127.in-addr.arpa";

  BEGIN();

1066
  TEST_1(sres_query(res, test_answer, ctx, sres_type_ptr, domain));
Pekka Pessi's avatar
Pekka Pessi committed
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
  TEST_RUN(ctx);

  result = ctx->result;
  TEST_1(result != NULL);
  TEST_1(result[0] != NULL);

  rr = result[0]->sr_ptr;
  TEST_1(rr != NULL);
  TEST(rr->ptr_record->r_class, sres_class_in);
  TEST(rr->ptr_record->r_type, sres_type_ptr);
  TEST_S(rr->ptr_domain, "localhost.");

  END();
}

int test_ptr_ipv4_sockaddr(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  sres_query_t *query;
  const sres_ptr_record_t *rr;
  struct sockaddr_in sin = {0};
1089

1090
  su_inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
Pekka Pessi's avatar
Pekka Pessi committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
  sin.sin_family = AF_INET;

  BEGIN();

  query = sres_query_sockaddr(res, test_answer, ctx,
				   sres_qtype_any, (struct sockaddr*)&sin);
  TEST_1(query != NULL);

  TEST_RUN(ctx);

  result = ctx->result;
1102

Pekka Pessi's avatar
Pekka Pessi committed
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
  TEST_1(result != NULL);
  TEST_1(result[0] != NULL);

  rr = result[0]->sr_ptr;
  TEST_1(rr != NULL);
  TEST(rr->ptr_record->r_type, sres_type_ptr);
  TEST(rr->ptr_record->r_class, sres_class_in);
  TEST_S(rr->ptr_domain, "localhost.");

  END();
}

1115
#if HAVE_SIN6
Pekka Pessi's avatar
Pekka Pessi committed
1116 1117 1118 1119 1120
int test_ptr_ipv6(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_ptr_record_t *rr;
1121
  char const *domain =
Pekka Pessi's avatar
Pekka Pessi committed
1122 1123 1124 1125
    "c.a.7.e.d.7.e.f.f.f.0.2.8.0.a.0.0.0.0.c.2.1.0.3.0.0.2.1.e.f.f.3.ip6.int";

  BEGIN();

1126
  TEST_1(sres_query(res, test_answer, ctx, sres_type_ptr, domain));
Pekka Pessi's avatar
Pekka Pessi committed
1127 1128 1129
  TEST_RUN(ctx);

  result = ctx->result;
1130

Pekka Pessi's avatar
Pekka Pessi committed
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
  TEST_1(result != NULL);
  TEST_1(result[0] != NULL);

  rr = result[0]->sr_ptr;
  TEST_1(rr != NULL);
  TEST(rr->ptr_record->r_type, sres_type_ptr);
  TEST(rr->ptr_record->r_class, sres_class_in);
  TEST_S(rr->ptr_domain, "sip01.example.com.");

  END();
}

int test_ptr_ipv6_sockaddr(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_ptr_record_t *rr;
  struct sockaddr_in6 sin6 = {0};

  BEGIN();

1152
  su_inet_pton(AF_INET6, "3ffe:1200:3012:c000:0a08:20ff:fe7d:e7ac",
1153
	       &sin6.sin6_addr);
Pekka Pessi's avatar
Pekka Pessi committed
1154 1155 1156

  sin6.sin6_family = AF_INET6;

1157
  ctx->query =
1158
    sres_query_sockaddr(res, test_answer, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1159 1160 1161 1162 1163 1164
			     sres_type_ptr, (struct sockaddr*)&sin6);
  TEST_1(ctx->query != NULL);

  TEST_RUN(ctx);

  result = ctx->result;
1165

Pekka Pessi's avatar
Pekka Pessi committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
  TEST_1(result != NULL);
  TEST_1(result[0] != NULL);

  rr = result[0]->sr_ptr;
  TEST_1(rr != NULL);
  TEST(rr->ptr_record->r_type, sres_type_ptr);
  TEST(rr->ptr_record->r_class, sres_class_in);
  TEST_S(rr->ptr_domain, "sip01.example.com.");

  END();
}
1177
#endif /* HAVE_SIN6 */
Pekka Pessi's avatar
Pekka Pessi committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

/* Test sres_cached_answers(), sres_sort_answers(), sres_free_answers() */
int test_cache(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  int ok = 0;
  sres_record_t *sort_array[3];
  sres_record_t **result;
  sres_query_t *query;
  const sres_record_t *rr = NULL;
  const sres_a6_record_t *rr_a6;
  const sres_aaaa_record_t *rr_aaaa;
  const sres_cname_record_t *rr_cname;
  const sres_ptr_record_t *rr_ptr;
1192
#if HAVE_SIN6
Pekka Pessi's avatar
Pekka Pessi committed
1193
  struct sockaddr_in6 sin6 = {0};
1194
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1195 1196 1197 1198 1199 1200
  char const *domain;
  char buf[50] = {0};
  int i, j;

  BEGIN();

1201
  sres_query(res, test_answer_multi, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1202 1203
		  sres_qtype_any, "example.com");

1204
  sres_query(res, test_answer_multi, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1205 1206
		  sres_qtype_any, "_sips._tcp.example.com");

1207
  sres_query(res, test_answer_multi, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1208 1209
		  sres_qtype_any, "sip.example.com");

1210
  sres_query(res, test_answer_multi, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1211 1212
		  sres_qtype_any, "subnet.example.com");

1213
#if HAVE_SIN6
1214
  sres_query(res, test_answer_multi, ctx,
1215
	     sres_type_aaaa, "mgw02.example.com");
Pekka Pessi's avatar
Pekka Pessi committed
1216

1217 1218 1219
  su_inet_pton(AF_INET6,
	       "3ffe:1200:3012:c000:0a08:20ff:fe7d:e7ac",
	       &sin6.sin6_addr);
Pekka Pessi's avatar
Pekka Pessi committed
1220 1221 1222

  sin6.sin6_family = AF_INET6;

1223
  query = sres_query_sockaddr(res, test_answer_multi, ctx,
1224
			      sres_qtype_any, (struct sockaddr *)&sin6);
Pekka Pessi's avatar
Pekka Pessi committed
1225 1226

  TEST_1(query != NULL);
1227
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1228 1229 1230 1231 1232

  TEST_RUN(ctx);

  /* For a chance, a fully qualified domain name with final "." */
  domain = "example.com.";
1233 1234

  result = sres_cached_answers(res,
Pekka Pessi's avatar
Pekka Pessi committed
1235 1236
			       sres_qtype_any,
			       domain);
1237

Pekka Pessi's avatar
Pekka Pessi committed
1238 1239 1240 1241 1242 1243
  TEST_1(result != NULL);

  for (i = 0; result[i] != NULL; i++) {
    rr = result[i];

    if (rr->sr_record->r_type == sres_type_naptr) {
1244
      const sres_naptr_record_t *rr_naptr = rr->sr_naptr;
Pekka Pessi's avatar
Pekka Pessi committed
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
      switch(rr_naptr->na_order) {
      case 20:
        TEST(rr_naptr->na_record->r_type, sres_type_naptr);
        TEST(rr_naptr->na_record->r_class, sres_class_in);
        TEST(rr_naptr->na_record->r_ttl, 60);
        TEST(rr_naptr->na_order, 20);
        TEST(rr_naptr->na_prefer, 50);
        TEST_S(rr_naptr->na_flags, "s");
        TEST_S(rr_naptr->na_services, "SIPS+D2T");
        TEST_S(rr_naptr->na_regexp, "");
        TEST_S(rr_naptr->na_replace, "_sips._tcp.example.com.");
        ok |= 1;
        break;

      case 40:
        TEST(rr_naptr->na_record->r_type, sres_type_naptr);
        TEST(rr_naptr->na_record->r_class, sres_class_in);
        TEST(rr_naptr->na_record->r_ttl, 60);
        TEST(rr_naptr->na_order, 40);
        TEST(rr_naptr->na_prefer, 15);
        TEST_S(rr_naptr->na_flags, "s");
        TEST_S(rr_naptr->na_services, "SIP+D2U");
        TEST_S(rr_naptr->na_regexp, "");
        TEST_S(rr_naptr->na_replace, "_sip._udp.example.com.");
        ok |= 2;
        break;

      case 50:
        TEST(rr_naptr->na_record->r_type, sres_type_naptr);
        TEST(rr_naptr->na_record->r_class, sres_class_in);
Pekka Pessi's avatar
Pekka Pessi committed
1275
        TEST(rr_naptr->na_record->r_ttl, 60);
Pekka Pessi's avatar
Pekka Pessi committed
1276 1277 1278 1279 1280
        TEST(rr_naptr->na_order, 50);
        TEST(rr_naptr->na_prefer, 15);
        TEST_S(rr_naptr->na_flags, "u");
        TEST_S(rr_naptr->na_services, "TEST+D2U");

1281
        TEST_S(rr_naptr->na_regexp,
Pekka Pessi's avatar
Pekka Pessi committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	       "/(tst:([^@]+@))?example.com$/\\1operator.com/i");

        TEST_S(rr_naptr->na_replace, ".");
        break;

      case 80:
        TEST(rr_naptr->na_record->r_type, sres_type_naptr);
        TEST(rr_naptr->na_record->r_class, sres_class_in);
        TEST(rr_naptr->na_record->r_ttl, 60);
        TEST(rr_naptr->na_order, 80);
        TEST(rr_naptr->na_prefer, 25);
        TEST_S(rr_naptr->na_flags, "s");
        TEST_S(rr_naptr->na_services, "SIP+D2T");
        TEST_S(rr_naptr->na_regexp, "");
        TEST_S(rr_naptr->na_replace, "_sip._tcp.example.com.");
        ok |= 4;
        break;

      default:
        TEST_1(0);
      }
    }
  }
1305

Pekka Pessi's avatar
Pekka Pessi committed
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
  TEST(ok, 7);

  /* Reverse order before sorting */
  for (j = 0; j < --i; j++) {
    sres_record_t *swap = result[j];
    result[j] = result[i];
    result[i] = swap;
  }

  /* Test sorting */
  sres_sort_answers(res, result);

  /* Sort all records with themselves */
  for (i = 0; result[i]; i++) {
    sort_array[0] = result[i], sort_array[1] = result[i], sort_array[2] = NULL;
    sres_sort_answers(res, sort_array);
  }
1323 1324

  /* Test free */
Pekka Pessi's avatar
Pekka Pessi committed
1325 1326 1327 1328 1329 1330 1331 1332
  for (i = 0; result[i]; i++) {
    sres_free_answer(res, result[i]);
    result[i] = NULL;
  }

  /* Test sres_free_answers() */
  sres_free_answers(res, result);

1333
  result = sres_cached_answers(res,
Pekka Pessi's avatar
Pekka Pessi committed
1334 1335
			       sres_qtype_any,
			       "_sips._tcp.example.com");
1336

Pekka Pessi's avatar
Pekka Pessi committed
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
  TEST_1(result != NULL);

  ok = 0;

  for (i = 0; result[i] != NULL; i++) {
    sres_srv_record_t *rr_srv = result[i]->sr_srv;

    TEST(rr_srv->srv_record->r_type, sres_type_srv);
    switch(rr_srv->srv_priority) {
    case 3:
      TEST(rr_srv->srv_record->r_type, sres_type_srv);
      TEST(rr_srv->srv_record->r_class, sres_class_in);
      TEST(rr_srv->srv_record->r_ttl, 60);
      TEST(rr_srv->srv_weight, 100);
      TEST(rr_srv->srv_port, 5061);
      TEST_S(rr_srv->srv_target, "sip00.example.com.");
      ok |= 1;
      break;

    case 4:
      TEST(rr_srv->srv_record->r_type, sres_type_srv);
      TEST(rr_srv->srv_record->r_class, sres_class_in);
      TEST(rr_srv->srv_record->r_ttl, 60);
      TEST(rr_srv->srv_weight, 50);
      TEST(rr_srv->srv_port, 5050);
      TEST_S(rr_srv->srv_target, "sip02.example.com.");
      ok |= 2;
      break;

    case 5:
      TEST(rr_srv->srv_record->r_type, sres_type_srv);
      TEST(rr_srv->srv_record->r_class, sres_class_in);
      TEST(rr_srv->srv_record->r_ttl, 60);
      TEST(rr_srv->srv_weight, 10);
      TEST(rr_srv->srv_port, 5060);
      TEST_S(rr_srv->srv_target, "sip01.example.com.");
      ok |= 4;
      break;

    default:
      TEST_1(0);
    }
  }

  TEST(ok, 7);

  /* Reverse order before sorting */
  for (j = 0; j < --i; j++) {
    sres_record_t *swap = result[j];
    result[j] = result[i];
    result[i] = swap;
  }

  sres_sort_answers(res, result);
  sres_free_answers(res, result);

  domain = "sip.example.com";
1394
  result = sres_cached_answers(res,
Pekka Pessi's avatar
Pekka Pessi committed
1395 1396 1397 1398 1399 1400 1401
			       sres_qtype_any,
			       domain);

  TEST_1(result != NULL);
  TEST_1(result[0] != NULL);

  rr_cname = result[0]->sr_cname;
Pekka Pessi's avatar
Pekka Pessi committed
1402 1403 1404
  TEST(rr_cname->cn_record->r_type, sres_type_cname);
  TEST(rr_cname->cn_record->r_class, sres_class_in);
  TEST(rr_cname->cn_record->r_ttl, 60);
Pekka Pessi's avatar
Pekka Pessi committed
1405 1406 1407 1408
  TEST_S(rr_cname->cn_cname, "sip00.example.com.");

  sres_free_answers(res, result);

1409
#if HAVE_SIN6
Pekka Pessi's avatar
Pekka Pessi committed
1410
  domain = "subnet.example.com";
1411
  result = sres_cached_answers(res,
Pekka Pessi's avatar
Pekka Pessi committed
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
			       sres_qtype_any,
			       domain);

  TEST_1(result != NULL);
  TEST_1(result[0] != NULL);

  rr_a6 = result[0]->sr_a6;
  TEST(rr_a6->a6_record->r_type, sres_type_a6);
  TEST(rr_a6->a6_record->r_class, sres_class_in);
  TEST(rr_a6->a6_record->r_ttl, 60);
  TEST(rr_a6->a6_prelen, 0);

1424
  TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)), "3ff0::");
Pekka Pessi's avatar
Pekka Pessi committed
1425

1426
  TEST_P(rr_a6->a6_prename, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1427 1428 1429 1430 1431 1432 1433 1434 1435

  sres_free_answers(res, result);

  domain = "mgw02.example.com";
  TEST_1(result = sres_cached_answers(res, sres_type_aaaa, domain));
  TEST_1(rr_aaaa = result[0]->sr_aaaa);
  TEST(rr_aaaa->aaaa_record->r_type, sres_type_aaaa);
  TEST(rr_aaaa->aaaa_record->r_class, sres_class_in);
  TEST(rr_aaaa->aaaa_record->r_ttl, 60);
1436
  TEST_S(su_inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)),
Pekka Pessi's avatar
Pekka Pessi committed
1437 1438 1439
	 "3ffe:1200:3012:c000:206:5bff:fe55:462f");
  sres_free_answers(res, result);

1440
  result = sres_cached_answers_sockaddr(res,
Pekka Pessi's avatar
Pekka Pessi committed
1441 1442
                                        sres_type_ptr,
                                        (struct sockaddr *)&sin6);
1443

Pekka Pessi's avatar
Pekka Pessi committed
1444 1445 1446 1447 1448 1449 1450
  TEST_1(result != NULL);

  rr_ptr = result[0]->sr_ptr;
  TEST_1(rr_ptr != NULL);

  TEST(rr_ptr->ptr_record->r_type, sres_type_ptr);
  TEST(rr_ptr->ptr_record->r_class, sres_class_in);
1451
  TEST_S(rr_ptr->ptr_domain, "sip01.example.com.");
Pekka Pessi's avatar
Pekka Pessi committed
1452 1453

  sres_free_answers(res, result);
1454
#endif /* HAVE_SIN6 */
Pekka Pessi's avatar
Pekka Pessi committed
1455 1456 1457 1458

  END();
}

1459
#if HAVE_SIN6
Pekka Pessi's avatar
Pekka Pessi committed
1460 1461 1462 1463 1464 1465
int test_query_one_type(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_aaaa_record_t *rr_aaaa;
  char buf[50] = {0};
1466

Pekka Pessi's avatar
Pekka Pessi committed
1467 1468
  BEGIN();

1469
  TEST_1(sres_query(res, test_answer, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1470 1471 1472 1473 1474 1475 1476 1477 1478
			 sres_type_aaaa, "mgw02.example.com"));
  TEST_RUN(ctx);
  TEST_1(result = ctx->result);
  TEST_1(result[0] != NULL);

  TEST_1(rr_aaaa = result[0]->sr_aaaa);
  TEST(rr_aaaa->aaaa_record->r_type, sres_type_aaaa);
  TEST(rr_aaaa->aaaa_record->r_class, sres_class_in);
  TEST(rr_aaaa->aaaa_record->r_ttl, 60);
1479
  TEST_S(su_inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)),
Pekka Pessi's avatar
Pekka Pessi committed
1480 1481
	 "3ffe:1200:3012:c000:206:5bff:fe55:462f");

1482
  TEST_P(result[1], NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1483 1484 1485

  END();
}
1486
#endif /* HAVE_SIN6*/
Pekka Pessi's avatar
Pekka Pessi committed
1487

1488 1489 1490 1491
#ifdef _WIN32
#include <fcntl.h>
#endif

Pekka Pessi's avatar
Pekka Pessi committed
1492 1493 1494 1495
int sink_make(sres_context_t *ctx)
{
  char *tmpdir = getenv("TMPDIR");
  char *template;
1496 1497
  int fd;
  sres_socket_t sink;
Pekka Pessi's avatar
Pekka Pessi committed
1498 1499 1500
  struct sockaddr_in sin[1];
  socklen_t sinsize = sizeof sin;
  FILE *f;
1501

Pekka Pessi's avatar
Pekka Pessi committed
1502 1503
  BEGIN();

1504
  sink = socket(AF_INET, SOCK_DGRAM, 0); TEST_1(sink != INVALID_SOCKET);
Pekka Pessi's avatar
Pekka Pessi committed
1505 1506 1507 1508 1509 1510 1511
  TEST(getsockname(sink, (struct sockaddr *)sin, &sinsize), 0);
  sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  TEST(bind(sink, (struct sockaddr *)sin, sinsize), 0);
  TEST(getsockname(sink, (struct sockaddr *)sin, &sinsize), 0);

  ctx->sink = sink;

1512
  template = su_sprintf(ctx->home, "%s/test_sresolv.XXXXXX",
Pekka Pessi's avatar
Pekka Pessi committed
1513 1514
			tmpdir ? tmpdir : "/tmp");
  TEST_1(template);
1515

1516
#ifndef _WIN32
Pekka Pessi's avatar
Pekka Pessi committed
1517
  fd = mkstemp(template); TEST_1(fd != -1);
1518 1519
#else
  fd = open(template, O_WRONLY); TEST_1(fd != -1);
1520
#endif
1521

Pekka Pessi's avatar
Pekka Pessi committed
1522
  f = fdopen(fd, "w"); TEST_1(f);
1523
  fprintf(f,
Pekka Pessi's avatar
Pekka Pessi committed
1524 1525
	  "domain example.com\n"
	  "search foo.bar.com\n"
1526
	  "port %u\n",
Pekka Pessi's avatar
Pekka Pessi committed
1527 1528 1529 1530 1531 1532 1533 1534
	  ntohs(sin->sin_port));
  fclose(f);

  ctx->sinkconf = template;

  END();
}

1535
#if 0
Pekka Pessi's avatar
Pekka Pessi committed
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
int recv_sink(su_root_magic_t *rm, su_wait_t *w, sres_context_t *ctx)
{
  union {
    char bytes[8192];
    unsigned short shorts[4096];
  } buffer[1];

  su_wait_events(w, ctx->sink);
  recv(ctx->sink, buffer->bytes, sizeof buffer, 0);

  return 0;
}

int sink_init(sres_context_t *ctx)
{
  su_wait_t w[1];
  BEGIN();

  TEST(su_wait_create(w, ctx->sink, SU_WAIT_IN), 0);
  ctx->sinkidx = su_root_register(ctx->root, w, recv_sink, ctx, 0);
  TEST_1(ctx->sinkidx != 0);

  END();
}

int sink_deinit(sres_context_t *ctx)
{
  BEGIN();

  if (ctx->sinkidx)
    su_root_deregister(ctx->root, ctx->sinkidx);
  ctx->sinkidx = 0;
1568
  sres_close(ctx->sink), ctx->sink = INVALID_SOCKET;
Pekka Pessi's avatar
Pekka Pessi committed
1569 1570 1571

  END();
}
1572
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582

int test_timeout(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  /* const sres_soa_record_t *rr_soa; */
  char const *domain = "test";

  BEGIN();

1583
  sres_query(res, test_answer, ctx, sres_type_a, domain);
Pekka Pessi's avatar
Pekka Pessi committed
1584 1585 1586 1587 1588

  ctx->timeout = 1;
  TEST_RUN(ctx);
  ctx->timeout = 0;

1589
  TEST_P(ctx->result, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1590 1591 1592

  result = sres_cached_answers(res, sres_type_a, domain);

1593
#if 0  /* Currently, we do not create error records */
Pekka Pessi's avatar
Pekka Pessi committed
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
  TEST_1(result); TEST_1(result[0] != NULL);

  rr_soa = result[0]->sr_soa;
  TEST(rr_soa->soa_record->r_type, sres_type_soa);
  TEST(rr_soa->soa_record->r_class, sres_class_in);
  TEST_S(rr_soa->soa_record->r_name, "example.com.");
  TEST(rr_soa->soa_record->r_status, SRES_TIMEOUT_ERR);

  sres_free_answers(res, result);
#else
  TEST_1(result == NULL);
#endif

  END();
}

1610
static void test_answer(sres_context_t *ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1611 1612 1613 1614 1615 1616
		 sres_query_t *q,
		 sres_record_t **answer)
{
  ctx->query = q;
  if (ctx->result)
    sres_free_answers(ctx->resolver, ctx->result);
1617
  ctx->result = answer;
Pekka Pessi's avatar
Pekka Pessi committed
1618 1619 1620
  BREAK(ctx);
}

1621
static void test_answer_multi(sres_context_t *ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
		       sres_query_t *q,
		       sres_record_t **answer)
{
  static int count = 0;

  ctx->query = q;

  count++;

  sres_free_answers(ctx->resolver, answer);

  if (count == 6)
    BREAK(ctx);
}

#include <sys/time.h>

1639
/* Fake time() implementation, used by sresolv library */
Pekka Pessi's avatar
Pekka Pessi committed
1640 1641 1642 1643
time_t time(time_t *tp)
{
  struct timeval tv[1];

1644
#ifndef _WIN32
Pekka Pessi's avatar
Pekka Pessi committed
1645
  gettimeofday(tv, NULL);
1646 1647 1648
#else
  return 0;
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665

  if (tp)
    *tp = tv->tv_sec + offset;

  return tv->tv_sec + offset;
}

int test_expiration(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  char const *domain = "example.com";

  BEGIN();

  offset = 3600;		/* Time suddenly proceeds by an hour.. */

1666
  sres_resolver_timer(res, -1);
Pekka Pessi's avatar
Pekka Pessi committed
1667 1668 1669

  result = sres_cached_answers(res, sres_qtype_any, domain);

1670
  TEST_P(result, NULL);  /* the cache should be empty after 15 secs */
Pekka Pessi's avatar
Pekka Pessi committed
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680

  END();
}

#define is_hexdigit(c) ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))
#define hex(c) ((c >= '0' && c <= '9') ? (c - '0') : (c - 'a' + 10))

/* Convert lowercase hex to binary */
static
void *hex2bin(char const *test_name,
1681
	      char const *hex1, char const *hex2, size_t *binsize)
Pekka Pessi's avatar
Pekka Pessi committed
1682 1683 1684 1685
{
  char output[2048];
  char *bin;
  char const *b;
1686
  size_t j;
Pekka Pessi's avatar
Pekka Pessi committed
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758

  if (hex1 == NULL || binsize == NULL)
    return NULL;

  for (b = hex1, j = 0; b;) {
    while (b[0]) {
      if (is_hexdigit(b[0])) {
	if (!is_hexdigit(b[1])) {
	  fprintf(stderr, "%s: hex2bin: invalid hex '%c'\n", test_name, b[1]);
	  exit(2);
	}

	output[j++] = (hex(b[0]) << 4) | hex(b[1]);
	if (j == sizeof(output)) {
	  fprintf(stderr, "%s:%u: hex2bin: buffer too small\n",
		  __FILE__, __LINE__);
	  exit(2);
	}
	b++;
      } else if (b[0] != ' ') {
	fprintf(stderr, "%s: hex2bin: invalid nonhex '%c'\n", test_name,
		b[0]);
	exit(2);
      }
      b++;
    }
    b = hex2, hex2 = NULL;
  }

  bin = malloc(j);
  if (bin == NULL)
    perror("malloc"), exit(2);

  return memcpy(bin, output, *binsize = j);
}

static char const hextest[] =
  "                        34 44 85 80 00 01 00 04 "
  "00 01 00 08 07 65 78 61 6d 70 6c 65 03 63 6f 6d "
  "00 00 23 00 01 c0 0c 00 23 00 01 00 00 00 3c 00 "
  "26 00 28 00 0f 01 73 07 53 49 50 2b 44 32 55 00 "
  "04 5f 73 69 70 04 5f 75 64 70 07 65 78 61 6d 70 "
  "6c 65 03 63 6f 6d 00 c0 42 00 23 00 01 00 00 00 "
  "3c 00 3e 00 32 00 0f 01 75 08 54 45 53 54 2b 44 "
  "32 55 2d 2f 28 74 73 74 3a 28 5b 5e 40 5d 2b 40 "
  "29 29 3f 65 78 61 6d 70 6c 65 2e 63 6f 6d 24 2f "
  "5c 31 6f 70 65 72 61 74 6f 72 2e 63 6f 6d 2f 69 "
  "00 c0 42 00 23 00 01 00 00 00 3c 00 26 00 50 00 "
  "19 01 73 07 53 49 50 2b 44 32 54 00 04 5f 73 69 "
  "70 04 5f 74 63 70 07 65 78 61 6d 70 6c 65 03 63 "
  "6f 6d 00 c0 be 00 23 00 01 00 00 00 3c 00 28 00 "
  "14 00 32 01 73 08 53 49 50 53 2b 44 32 54 00 05 "
  "5f 73 69 70 73 04 5f 74 63 70 07 65 78 61 6d 70 "
  "6c 65 03 63 6f 6d 00 c0 f2 00 02 00 01 00 00 00 "
  "3c 00 05 02 6e 73 c0 f2 05 73 69 70 30 30 c0 f2 "
  "00 01 00 01 00 00 00 3c 00 04 c2 02 bc 85 c1 10 "
  "00 1c 00 01 00 00 00 3c 00 10 3f f0 00 10 30 12 "
  "c0 00 02 c0 95 ff fe e2 4b 78 05 73 69 70 30 32 "
  "c0 f2 00 01 00 01 00 00 00 3c 00 04 c2 02 bc 87 "
  "c1 42 00 1c 00 01 00 00 00 3c 00 10 3f fe 12 00 "
  "30 12 c0 06 02 06 5b ff fe 55 46 2f 05 73 69 70 "
  "30 31 c0 f2 00 01 00 01 00 00 00 3c 00 04 c2 02 "
  "bc 86 c1 74 00 1c 00 01 00 00 00 3c 00 10 3f f0 "
  "00 12 30 12 c0 06 0a 08 20 ff fe 7d e7 ac c1 0b "
  "00 01 00 01 00 00 00 3c 00 04 c2 02 bc 85 c1 0b "
  "00 26 00 01 00 00 00 3c 00 11 00 3f fe 12 00 30 "
  "12 c0 00 02 10 a4 ff fe 8d 6a 46 ";

int test_net(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_query_t *q = NULL;
1759
  sres_socket_t c = ctx->sink;
Pekka Pessi's avatar
Pekka Pessi committed
1760 1761
  struct sockaddr_storage ss[1];
  struct sockaddr *sa = (void *)ss;
1762
  socklen_t salen = sizeof ss;
Pekka Pessi's avatar
Pekka Pessi committed
1763
  char *bin;
1764 1765
  size_t i, binlen;
  ssize_t n;
Pekka Pessi's avatar
Pekka Pessi committed
1766 1767 1768 1769 1770
  char const *domain = "example.com";
  char query[512];

  BEGIN();

1771
  TEST_1(ctx->sink != INVALID_SOCKET && ctx->sink != (sres_socket_t)0);
Pekka Pessi's avatar
Pekka Pessi committed
1772 1773

  /* Prepare for test_answer() callback */
Pekka Pessi's avatar