test_sresolv.c 49.6 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 28
 * 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
 * 
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 40 41 42 43 44
#if HAVE_STDINT_H
#include <stdint.h>
#elif HAVE_INTTYPES_H
#include <inttypes.h>
#endif

#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
Pekka Pessi's avatar
Pekka Pessi committed
45 46 47 48
#if HAVE_WINSOCK2_H
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
49 50

#include <sofia-resolv/sres.h>
Pekka Pessi's avatar
Pekka Pessi committed
51
#include <sofia-resolv/sres_async.h>
52
#include <sofia-resolv/sres_record.h>
Pekka Pessi's avatar
Pekka Pessi committed
53

54
#include <sofia-sip/su_alloc.h>
Pekka Pessi's avatar
Pekka Pessi committed
55 56 57 58 59 60

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
61
#include <errno.h>
62

63 64
#define TSTFLAGS tstflags
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
65

66 67
#if HAVE_POLL
#include <poll.h>
Pekka Pessi's avatar
Pekka Pessi committed
68
#elif HAVE_SYS_SELECT_H
69 70 71 72 73 74 75
#include <sys/select.h>
#endif

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

Pekka Pessi's avatar
Pekka Pessi committed
76 77 78 79 80
#if HAVE_ALARM
#include <unistd.h>
#include <signal.h>
#endif

Pekka Pessi's avatar
Pekka Pessi committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
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;

  pthread_mutex_t  mutex[1];

  int              timeout;
  int              sink;
  int              sinkidx;
  char const      *sinkconf;
  
  int              ready;
  int              n_sockets;
99 100 101
  int              sockets[SRES_MAX_NAMESERVERS];
#if HAVE_POLL
  struct pollfd    fds[SRES_MAX_NAMESERVERS];
Pekka Pessi's avatar
Pekka Pessi committed
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
#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;

#if 0
static
int setblocking(int s, int blocking)
{
  unsigned mode = fcntl(s, F_GETFL, 0);

  if (mode < 0)
     return -1;

  if (blocking) 
    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;
  int s1, s2, s3, s4;
  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 *sin1 = (void *)a1, *sin2 = (void *)a2;
  struct sockaddr_in *sin3 = (void *)a3, *sin4 = (void *)a4;
  struct sockaddr *sa = (void *)a;
  struct sockaddr *sa1 = (void *)a1, *sa2 = (void *)a2;
  struct sockaddr *sa3 = (void *)a3, *sa4 = (void *)a4;
  int alen, a1len, a2len, a3len, a4len;
  char buf[16];

  BEGIN();

  af = AF_INET;

  for (;;) {
    TEST_1((s1 = socket(af, SOCK_DGRAM, 0)) != -1);
    TEST_1((s2 = socket(af, SOCK_DGRAM, 0)) != -1);
    TEST_1((s3 = socket(af, SOCK_DGRAM, 0)) != -1);
    TEST_1((s4 = socket(af, SOCK_DGRAM, 0)) != -1);

    TEST_1(setblocking(s1, 0) == 0);
    TEST_1(setblocking(s2, 0) == 0);
    TEST_1(setblocking(s3, 0) == 0);
    TEST_1(setblocking(s4, 0) == 0);
    
    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) {
      TEST_1(inet_pton(af, "127.0.0.1", &sin3->sin_addr) > 0);
      TEST_1(inet_pton(af, "127.0.0.1", &sin4->sin_addr) > 0);
    } else {
    }

    TEST(bind(s3, (struct sockaddr *)a3, a3len), 0);
    TEST(bind(s4, (struct sockaddr *)a4, a4len), 0);
    
    alen = sizeof a;
    TEST(getsockname(s3, (struct sockaddr *)a, &alen), 0);
    sin3->sin_port = sin->sin_port; 
    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);

    TEST(sendto(s1, "foo", 3, 0, sa4, a4len), 3);
    TEST(recvfrom(s4, buf, sizeof buf, 0, sa, &alen), 3);
    TEST(sendto(s4, "bar", 3, 0, sa, alen), 3);
    TEST(recvfrom(s2, buf, sizeof buf, 0, sa, &alen), -1);
    TEST(recvfrom(s1, buf, sizeof buf, 0, sa, &alen), 3);

209
    su_close(s1), su_close(s2), su_close(s3), su_close(s4);
Pekka Pessi's avatar
Pekka Pessi committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

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

225 226
static
int poll_sockets(sres_context_t *ctx)
Pekka Pessi's avatar
Pekka Pessi committed
227 228 229 230 231
{
  int i, n, events;

  n = ctx->n_sockets;

232
  events = poll(ctx->fds, ctx->n_sockets, ctx->timeout ? 50 : 500);
Pekka Pessi's avatar
Pekka Pessi committed
233
      
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
  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);
  }
  
  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
252 253 254 255 256 257 258

    if (ctx->timeout) {
      ctx->timeout <<= 1;
      offset += ctx->timeout;
    }
    
    /* No harm is done (except wasted CPU) if timer is called more often */
259
    sres_resolver_timer(ctx->resolver, -1); 
Pekka Pessi's avatar
Pekka Pessi committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
  }
}

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

276
  TEST_1(sres_query(res, test_answer, ctx, sres_type_soa, domain));
Pekka Pessi's avatar
Pekka Pessi committed
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
  TEST_RUN(ctx);

  TEST_1(result = sres_cached_answers(res, sres_type_soa, domain));
  
  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();

311
  TEST_1(sres_query(res, test_answer, ctx, sres_type_naptr, domain));
Pekka Pessi's avatar
Pekka Pessi committed
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 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 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 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  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;
                                        
    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();
}

char const longname[1026] = 
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.";

char name2048[2049] = 
  "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";
  char name1025[1026] = 
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
		  
		  ".";

  BEGIN();

501
  TEST_1(!sres_query(res, test_answer, ctx, sres_type_a, name1025));
Pekka Pessi's avatar
Pekka Pessi committed
502 503 504

  name1025[1024] = '\0';

505
  TEST_1(!sres_query(res, test_answer, ctx, sres_type_a, name1025));
Pekka Pessi's avatar
Pekka Pessi committed
506 507 508

  name1025[1023] = '\0';

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

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

513
  query = sres_query(res, test_answer, ctx, sres_type_a, longname);
Pekka Pessi's avatar
Pekka Pessi committed
514 515 516
  TEST_1(query);
  sres_query_bind(query, NULL, NULL);
  
517
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a, domain));
Pekka Pessi's avatar
Pekka Pessi committed
518 519 520 521 522 523 524 525 526

  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  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);
527

Pekka Pessi's avatar
Pekka Pessi committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
  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 */
543
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a, "sip00"));
Pekka Pessi's avatar
Pekka Pessi committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

  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 */
563
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a, 
Pekka Pessi's avatar
Pekka Pessi committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577
			 "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);

  /* Try domain without A record => 
     we should get a record with SRES_RECORD_ERR */
578
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a, 
Pekka Pessi's avatar
Pekka Pessi committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
			 "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);

  END();
}

594
#if HAVE_SIN6
Pekka Pessi's avatar
Pekka Pessi committed
595 596 597 598 599 600 601 602 603 604
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();

605
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a6, domain));
Pekka Pessi's avatar
Pekka Pessi committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  TEST_1(result[0]);
  
  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);

  TEST_S(inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)), 
	 "3ffe:1200:3012:c000:210:a4ff:fe8d:6a46");

  TEST(rr_a6->a6_prename, NULL);

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

  TEST_S(inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)), 
	 "3ffe:1200:3012:c000:210:a4ff:fe8d:6a46");

  TEST(rr_a6->a6_prename, NULL);

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

651
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a6, domain));
Pekka Pessi's avatar
Pekka Pessi committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
  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, 48);

  TEST_S(inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)), 
	 "::c006:a08:20ff:fe7d:e7ac");

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

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

  TEST_S(inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)), 
	 "::c006:a08:20ff:fe7d:e7ac");

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

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

696
  TEST_1(sres_query(res, test_answer, ctx, sres_type_aaaa, domain));
Pekka Pessi's avatar
Pekka Pessi committed
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
  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);

  TEST_S(inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)), 
	 "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));
  
  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);

  TEST_S(inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)), 
	 "3ffe:1200:3012:c000:206:5bff:fe55:4630");

  sres_free_answers(res, result), result = NULL;

  END();
}
724
#endif /* HAVE_SIN6 */
Pekka Pessi's avatar
Pekka Pessi committed
725 726 727 728 729 730 731 732 733 734 735

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

736
  TEST_1(sres_query(res, test_answer, ctx, sres_type_srv, domain));
Pekka Pessi's avatar
Pekka Pessi committed
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
  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);
    
    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);
    
    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;
  sres_record_t **result;
  const sres_cname_record_t *rr;
  char const *domain = "sip.example.com";

  BEGIN();

831
  TEST_1(sres_query(res, test_answer, ctx, sres_type_a, domain));
Pekka Pessi's avatar
Pekka Pessi committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
  TEST_RUN(ctx);

  TEST_1(result = ctx->result);
  TEST_1(rr = result[0]->sr_cname);
  TEST(rr->cname_record->r_class, sres_class_in);
  TEST(rr->cname_record->r_type, sres_type_cname);
  TEST(rr->cname_record->r_ttl, 60);
  TEST_S(rr->cn_cname, "sip00.example.com.");

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

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

855
  TEST_1(sres_query(res, test_answer, ctx, sres_type_ptr, domain));
Pekka Pessi's avatar
Pekka Pessi committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
  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};
  
  inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
  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;
  
  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();
}

904
#if HAVE_SIN6
Pekka Pessi's avatar
Pekka Pessi committed
905 906 907 908 909 910 911 912 913 914
int test_ptr_ipv6(sres_context_t *ctx)
{
  sres_resolver_t *res = ctx->resolver;
  sres_record_t **result;
  const sres_ptr_record_t *rr;
  char const *domain = 
    "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();

915
  TEST_1(sres_query(res, test_answer, ctx, sres_type_ptr, domain));
Pekka Pessi's avatar
Pekka Pessi committed
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
  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_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();

  inet_pton(AF_INET6, "3ffe:1200:3012:c000:0a08:20ff:fe7d:e7ac", 
            &sin6.sin6_addr);

  sin6.sin6_family = AF_INET6;

  ctx->query = 
947
    sres_query_sockaddr(res, test_answer, ctx,
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
			     sres_type_ptr, (struct sockaddr*)&sin6);
  TEST_1(ctx->query != NULL);

  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_type, sres_type_ptr);
  TEST(rr->ptr_record->r_class, sres_class_in);
  TEST_S(rr->ptr_domain, "sip01.example.com.");

  END();
}
966
#endif /* HAVE_SIN6 */
Pekka Pessi's avatar
Pekka Pessi committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980

/* 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;
981
#if HAVE_SIN6
Pekka Pessi's avatar
Pekka Pessi committed
982
  struct sockaddr_in6 sin6 = {0};
983
#endif
Pekka Pessi's avatar
Pekka Pessi committed
984 985 986 987 988 989
  char const *domain;
  char buf[50] = {0};
  int i, j;

  BEGIN();

990
  sres_query(res, test_answer_multi, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
991 992
		  sres_qtype_any, "example.com");

993
  sres_query(res, test_answer_multi, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
994 995
		  sres_qtype_any, "_sips._tcp.example.com");

996
  sres_query(res, test_answer_multi, ctx, 
Pekka Pessi's avatar
Pekka Pessi committed
997 998
		  sres_qtype_any, "sip.example.com");

999
  sres_query(res, test_answer_multi, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1000 1001
		  sres_qtype_any, "subnet.example.com");

1002
#if HAVE_SIN6
1003
  sres_query(res, test_answer_multi, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1004 1005 1006 1007 1008 1009 1010 1011
		  sres_type_aaaa, "mgw02.example.com");

  inet_pton(AF_INET6, 
            "3ffe:1200:3012:c000:0a08:20ff:fe7d:e7ac", 
            &sin6.sin6_addr);

  sin6.sin6_family = AF_INET6;

1012
  query = sres_query_sockaddr(res, test_answer_multi, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1013 1014 1015
				   sres_qtype_any, (struct sockaddr *)&sin6);

  TEST_1(query != NULL);
1016
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

  TEST_RUN(ctx);

  /* For a chance, a fully qualified domain name with final "." */
  domain = "example.com.";
  
  result = sres_cached_answers(res, 
			       sres_qtype_any,
			       domain);
  
  TEST_1(result != NULL);

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

    if (rr->sr_record->r_type == sres_type_naptr) {
      sres_naptr_record_t *rr_naptr = rr->sr_naptr;
      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);
        // TEST(rr_naptr->na_record->r_ttl, 60);
        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");

        TEST_S(rr_naptr->na_regexp, 
	       "/(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);
      }
    }
  }
  
  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);
  }
  
  /* Test free */ 
  for (i = 0; result[i]; i++) {
    sres_free_answer(res, result[i]);
    result[i] = NULL;
  }

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

  result = sres_cached_answers(res, 
			       sres_qtype_any,
			       "_sips._tcp.example.com");
  
  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";
  result = sres_cached_answers(res, 
			       sres_qtype_any,
			       domain);

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

  rr_cname = result[0]->sr_cname;
  TEST(rr_cname->cname_record->r_type, sres_type_cname);
  TEST(rr_cname->cname_record->r_class, sres_class_in);
  TEST(rr_cname->cname_record->r_ttl, 60);
  TEST_S(rr_cname->cn_cname, "sip00.example.com.");
  // XXX - this also returns AAAA record for sip00
  //TEST(result[1], NULL);

  sres_free_answers(res, result);

1200
#if HAVE_SIN6
Pekka Pessi's avatar
Pekka Pessi committed
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
  domain = "subnet.example.com";
  result = sres_cached_answers(res, 
			       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);

  TEST_S(inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)), "3ff0::");

  TEST(rr_a6->a6_prename, NULL);

  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);
  TEST_S(inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)), 
	 "3ffe:1200:3012:c000:206:5bff:fe55:462f");
  sres_free_answers(res, result);

  result = sres_cached_answers_sockaddr(res, 
                                        sres_type_ptr,
                                        (struct sockaddr *)&sin6);
  
  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);
  TEST_S(rr_ptr->ptr_domain, "sip01.example.com.");    

  sres_free_answers(res, result);
1245
#endif /* HAVE_SIN6 */
Pekka Pessi's avatar
Pekka Pessi committed
1246 1247 1248 1249

  END();
}

1250
#if HAVE_SIN6
Pekka Pessi's avatar
Pekka Pessi committed
1251 1252 1253 1254 1255 1256 1257 1258 1259
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};
  
  BEGIN();

1260
  TEST_1(sres_query(res, test_answer, ctx,
Pekka Pessi's avatar
Pekka Pessi committed
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
			 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);
  TEST_S(inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)), 
	 "3ffe:1200:3012:c000:206:5bff:fe55:462f");

  TEST(result[1], NULL);

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

1279 1280 1281 1282
#ifdef _WIN32
#include <fcntl.h>
#endif

Pekka Pessi's avatar
Pekka Pessi committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
int sink_make(sres_context_t *ctx)
{
  char *tmpdir = getenv("TMPDIR");
  char *template;
  int fd, sink;
  struct sockaddr_in sin[1];
  socklen_t sinsize = sizeof sin;
  FILE *f;
  
  BEGIN();

  sink = socket(AF_INET, SOCK_DGRAM, 0); TEST_1(sink != -1);
  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;

  template = su_sprintf(ctx->home, "%s/test_sresolv.XXXXXX", 
			tmpdir ? tmpdir : "/tmp");
  TEST_1(template);
1305 1306

#ifndef _WIN32  
Pekka Pessi's avatar
Pekka Pessi committed
1307
  fd = mkstemp(template); TEST_1(fd != -1);
1308 1309 1310 1311
#else
  fd = open(template, O_WRONLY); TEST_1(fd != -1);
#endif  

Pekka Pessi's avatar
Pekka Pessi committed
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
  f = fdopen(fd, "w"); TEST_1(f);
  fprintf(f, 
	  "domain example.com\n"
	  "search foo.bar.com\n"
	  "port %u\n", 
	  ntohs(sin->sin_port));
  fclose(f);

  ctx->sinkconf = template;

  END();
}

1325
#if 0
Pekka Pessi's avatar
Pekka Pessi committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
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;
1358
  su_close(ctx->sink), ctx->sink = -1;
Pekka Pessi's avatar
Pekka Pessi committed
1359 1360 1361

  END();
}
1362
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

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

1373
  sres_query(res, test_answer, ctx, sres_type_a, domain);
Pekka Pessi's avatar
Pekka Pessi committed
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434

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

  TEST(ctx->result, NULL);

  result = sres_cached_answers(res, sres_type_a, domain);

#if 0
  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
  /* Currently, we do not create error records */
  TEST_1(result == NULL);
#endif

  END();
}

void test_answer(sres_context_t *ctx,
		 sres_query_t *q,
		 sres_record_t **answer)
{
  ctx->query = q;
  if (ctx->result)
    sres_free_answers(ctx->resolver, ctx->result);
  ctx->result = answer; 
  BREAK(ctx);
}

void test_answer_multi(sres_context_t *ctx,
		       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>

/* Fake time() implementation */
time_t time(time_t *tp)
{
  struct timeval tv[1];

1435
#ifndef _WIN32
Pekka Pessi's avatar
Pekka Pessi committed
1436
  gettimeofday(tv, NULL);
1437 1438 1439
#else
  return 0;
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456

  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.. */

1457
  sres_resolver_timer(res, -1);
Pekka Pessi's avatar
Pekka Pessi committed
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 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 1568 1569 1570 1571 1572 1573 1574

  result = sres_cached_answers(res, sres_qtype_any, domain);

  TEST(result, NULL);  /* the cache should be empty after 15 secs */

  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,
	      char const *hex1, char const *hex2, unsigned *binsize)
{
  char output[2048];
  char *bin;
  char const *b;
  int j;

  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;
  int c = ctx->sink;
  struct sockaddr_storage ss[1];
  struct sockaddr *sa = (void *)ss;
  socklen_t salen = sizeof ss, binlen;
  char *bin;
  int i, n;
  char const *domain = "example.com";
  char query[512];

  BEGIN();

  TEST_1(ctx->sink != -1 && ctx->sink != 0);

  /* Prepare for test_answer() callback */
  sres_free_answers(ctx->resolver, ctx->result); 
  ctx->result = NULL;
  ctx->query = NULL; 

  /* Get canned response */
  TEST_1(bin = hex2bin(__func__, hextest, NULL, &binlen));

  /* Send responses with one erroneus byte */
  for (i = 1; i < binlen; i++) {
    if (!q) {
      /* We got an error => make new query */
1575
      TEST_1(q = sres_query(res, test_answer, ctx, /* Send query */
Pekka Pessi's avatar
Pekka Pessi committed
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
				 sres_type_naptr, domain));
      TEST_1((n = recvfrom(c, query, sizeof query, 0, sa, &salen)) != -1);
      memcpy(bin, query, 2); /* Copy ID */
    }
    if (i != 1)
      bin[i] ^= 0xff;
    else
      bin[3] ^= SRES_FORMAT_ERR; /* format error -> EDNS0 failure */
    n = sendto(c, bin, binlen, 0, sa, salen);
    if (i != 1)
      bin[i] ^= 0xff;
    else
      bin[3] ^= SRES_FORMAT_ERR;
    if (n == -1)
      perror("sendto");
1591 1592 1593 1594

    while (!poll_sockets(ctx))
      ;

Pekka Pessi's avatar
Pekka Pessi committed
1595 1596 1597 1598 1599 1600 1601 1602
    if (ctx->query)
      q = NULL;
  }

  /* Send runt responses */
  for (i = 1; i <= binlen; i++) {
    if (!q) {
      /* We got an error => make new query */
1603
      TEST_1(q = sres_query(res, test_answer, ctx, /* Send query */
Pekka Pessi's avatar
Pekka Pessi committed
1604 1605 1606 1607 1608 1609 1610
				 sres_type_naptr, domain));
      TEST_1((n = recvfrom(c, query, sizeof query, 0, sa, &salen)) != -1);
      memcpy(bin, query, 2); /* Copy ID */
    }
    n = sendto(c, bin, i, 0, sa, salen);
    if (n == -1)
      perror("sendto");
1611 1612 1613
    while (!poll_sockets(ctx))
      ;

Pekka Pessi's avatar
Pekka Pessi committed
1614 1615 1616 1617 1618 1619 1620 1621 1622
    if (ctx->query)
      q = NULL;
  }

  free(bin);

  END();
}

1623

Pekka Pessi's avatar
Pekka Pessi committed
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
/* Test API function argument validation */
static
int test_api_errors(sres_context_t *noctx)
{
  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, ".test_sresolv_api.conf.XXXXXX");
  TEST_1(template);
  
  TEST_1(res = sres_resolver_new(NULL));
1644
  TEST_VOID(sres_resolver_unref(res));
Pekka Pessi's avatar
Pekka Pessi committed
1645

1646
#ifndef _WIN32
Pekka Pessi's avatar
Pekka Pessi committed
1647
  fd = mkstemp(template); TEST_1(fd != -1);
1648 1649 1650 1651
#else
  fd = open(template, O_WRONLY); TEST_1(fd != -1);
#endif  

Pekka Pessi's avatar
Pekka Pessi committed
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
  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));

  unlink(template);
  
  s = sockets[0];
  
  TEST(sres_resolver_ref(NULL), NULL);
1666
  TEST(errno, EFAULT);
Pekka Pessi's avatar
Pekka Pessi committed
1667 1668
  sres_resolver_unref(NULL);

1669
  TEST(sres_resolver_set_userdata(NULL, NULL), NULL);
1670
  TEST(errno, EFAULT);
Pekka Pessi's avatar
Pekka Pessi committed
1671

1672
  TEST(sres_resolver_get_userdata(NULL), NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1673

1674
  TEST(sres_resolver_get_userdata(res), NULL);
1675
  TEST(sres_resolver_set_userdata(res, sa), NULL);
1676
  TEST(sres_resolver_get_userdata(res), sa);
1677
  TEST(sres_resolver_set_userdata(res, NULL), sa); 
1678
  TEST(sres_resolver_get_userdata(res), NULL);
1679

Pekka Pessi's avatar
Pekka Pessi committed
1680
  errno = 0;
1681
  TEST(sres_query(NULL, test_answer, ctx, sres_type_a, "com"), NULL);
1682
  TEST(errno, EFAULT); errno = 0;
1683
  TEST(sres_query(</