1 /*
2  * This file is part of the Sofia-SIP package
3  *
4  * Copyright (C) 2006 Nokia Corporation.
5  *
6  * Contact: Pekka Pessi <pekka.pessi@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 /**@internal
26  *
27  * @CFILE test_sresolv.c Test module for sresolv
28  *
29  * @author Mikko Haataja
30  * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
31  *
32  */
33 
34 #include "config.h"
35 
36 #if HAVE_STDINT_H
37 #include <stdint.h>
38 #elif HAVE_INTTYPES_H
39 #include <inttypes.h>
40 #else
41 #if defined(_WIN32)
42 typedef unsigned _int8 uint8_t;
43 typedef unsigned _int16 uint16_t;
44 typedef unsigned _int32 uint32_t;
45 #endif
46 #endif
47 
48 #if HAVE_FCNTL_H
49 #include <fcntl.h>
50 #endif
51 
52 #if HAVE_NETINET_IN_H
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #endif
57 
58 #if HAVE_WINSOCK2_H
59 #include <winsock2.h>
60 #include <ws2tcpip.h>
61 #endif
62 
63 #include <sofia-resolv/sres.h>
64 #include <sofia-resolv/sres_async.h>
65 #include <sofia-resolv/sres_record.h>
66 
67 #include <sofia-sip/su_alloc.h>
68 
69 #include <assert.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <stdio.h>
73 #include <errno.h>
74 
75 #define TSTFLAGS tstflags
76 #include <sofia-sip/tstdef.h>
77 
78 #if HAVE_POLL
79 #include <poll.h>
80 #elif HAVE_SYS_SELECT_H
81 #include <sys/select.h>
82 #endif
83 
84 #if HAVE_ARPA_INET_H
85 #include <arpa/inet.h>
86 #endif
87 
88 #if HAVE_ALARM
89 #include <unistd.h>
90 #include <signal.h>
91 #endif
92 
93 #include <sofia-sip/su_log.h>
94 
95 extern su_log_t sresolv_log[];
96 
97 char const name[] = "test_sresolv";
98 
99 struct sres_context_s
100 {
101   su_home_t        home[1];
102   sres_resolver_t *resolver;
103   sres_query_t    *query;
104   sres_record_t  **result;
105 
106   int              timeout;
107   sres_socket_t    sink;
108   int              sinkidx;
109   char const      *sinkconf;
110 
111   int              ready;
112   int              n_sockets;
113   sres_socket_t    sockets[SRES_MAX_NAMESERVERS];
114 #if HAVE_POLL
115   struct pollfd    fds[SRES_MAX_NAMESERVERS];
116 #endif
117 };
118 
119 static void test_answer(sres_context_t *ctx, sres_query_t *query,
120 			sres_record_t **answer);
121 static void test_answer_multi(sres_context_t *ctx, sres_query_t *query,
122 			      sres_record_t **answer);
123 
124 static int tstflags = 0;
125 
126 #if HAVE_WINSOCK2_H
127 
128 /* Posix send() */
129 su_inline
sres_send(sres_socket_t s,void * b,size_t length,int flags)130 ssize_t sres_send(sres_socket_t s, void *b, size_t length, int flags)
131 {
132   if (length > INT_MAX)
133     length = INT_MAX;
134   return (ssize_t)send(s, b, (int)length, flags);
135 }
136 
137 su_inline
sres_sendto(sres_socket_t s,void * b,size_t length,int flags,struct sockaddr const * sa,socklen_t salen)138 ssize_t sres_sendto(sres_socket_t s, void *b, size_t length, int flags,
139 		    struct sockaddr const *sa, socklen_t salen)
140 {
141   if (length > INT_MAX)
142     length = INT_MAX;
143   return (ssize_t)sendto(s, b, (int)length, flags, (void *)sa, (int)salen);
144 }
145 
146 /* Posix recvfrom() */
147 su_inline
sres_recvfrom(sres_socket_t s,void * buffer,size_t length,int flags,struct sockaddr * from,socklen_t * fromlen)148 ssize_t sres_recvfrom(sres_socket_t s, void *buffer, size_t length, int flags,
149 		      struct sockaddr *from, socklen_t *fromlen)
150 {
151   int retval, ilen;
152 
153   if (fromlen)
154     ilen = *fromlen;
155 
156   if (length > INT_MAX)
157     length = INT_MAX;
158 
159   retval = recvfrom(s, buffer, (int)length, flags,
160 		    (void *)from, fromlen ? &ilen : NULL);
161 
162   if (fromlen)
163     *fromlen = ilen;
164 
165   return (ssize_t)retval;
166 }
167 
sres_socket(int af,int socktype,int protocol)168 static sres_socket_t sres_socket(int af, int socktype, int protocol)
169 {
170   return socket(af, socktype, protocol);
171 }
172 
173 su_inline
sres_close(sres_socket_t s)174 int sres_close(sres_socket_t s)
175 {
176   return closesocket(s);
177 }
178 
179 #if !defined(IPPROTO_IPV6)
180 #if HAVE_SIN6
181 #include <tpipv6.h>
182 #else
183 #if !defined(__MINGW32__)
184 struct sockaddr_storage {
185     short ss_family;
186     char ss_pad[126];
187 };
188 #endif
189 #endif
190 #endif
191 
192 SOFIAPUBFUN int su_inet_pton(int af, char const *src, void *dst);
193 SOFIAPUBFUN const char *su_inet_ntop(int af, void const *src,
194 				  char *dst, size_t size);
195 
196 #else
197 
198 #define sres_send(s,b,len,flags) send((s),(b),(len),(flags))
199 #define sres_sendto(s,b,len,flags,a,alen) \
200   sendto((s),(b),(len),(flags),(a),(alen))
201 #define sres_recvfrom(s,b,len,flags,a,alen) \
202   recvfrom((s),(b),(len),(flags),(a),(alen))
203 #define sres_close(s) close((s))
204 #define SOCKET_ERROR   (-1)
205 #define INVALID_SOCKET ((sres_socket_t)-1)
206 #define sres_socket(x,y,z) socket((x),(y),(z))
207 
208 #define su_inet_pton inet_pton
209 #define su_inet_ntop inet_ntop
210 
211 #endif
212 
213 #if 1
214 
215 #if HAVE_POLL && 0
216 
217 static
setblocking(sres_socket_t s,int blocking)218 int setblocking(sres_socket_t s, int blocking)
219 {
220   unsigned mode = fcntl(s, F_GETFL, 0);
221 
222   if (mode < 0)
223      return -1;
224 
225   if (blocking)
226     mode &= ~(O_NDELAY | O_NONBLOCK);
227   else
228     mode |= O_NDELAY | O_NONBLOCK;
229 
230   return fcntl(s, F_SETFL, mode);
231 }
232 
233 /** Test few assumptions about sockets */
234 static
test_socket(sres_context_t * ctx)235 int test_socket(sres_context_t *ctx)
236 {
237   int af;
238   sres_socket_t s1, s2, s3, s4;
239   struct sockaddr_storage a[1];
240   struct sockaddr_storage a1[1], a2[1], a3[1], a4[1];
241   struct sockaddr_in *sin = (void *)a;
242   struct sockaddr_in *sin3 = (void *)a3, *sin4 = (void *)a4;
243   struct sockaddr *sa = (void *)a;
244   struct sockaddr *sa3 = (void *)a3, *sa4 = (void *)a4;
245   socklen_t alen, a1len, a2len, a3len, a4len;
246   char buf[16];
247 
248   BEGIN();
249 
250   af = AF_INET;
251 
252   for (;;) {
253     TEST_1((s1 = sres_socket(af, SOCK_DGRAM, 0)) != INVALID_SOCKET);
254     TEST_1((s2 = sres_socket(af, SOCK_DGRAM, 0)) != INVALID_SOCKET);
255     TEST_1((s3 = sres_socket(af, SOCK_DGRAM, 0)) != INVALID_SOCKET);
256     TEST_1((s4 = sres_socket(af, SOCK_DGRAM, 0)) != INVALID_SOCKET);
257 
258     TEST_1(setblocking(s1, 0) == 0);
259     TEST_1(setblocking(s2, 0) == 0);
260     TEST_1(setblocking(s3, 0) == 0);
261     TEST_1(setblocking(s4, 0) == 0);
262 
263     memset(a, 0, sizeof a);
264     memset(a1, 0, sizeof a1);
265     memset(a2, 0, sizeof a2);
266     memset(a3, 0, sizeof a3);
267     memset(a4, 0, sizeof a4);
268 
269 #if HAVE_SA_LEN
270     a1->ss_len = a2->ss_len = a3->ss_len = a4->ss_len = sizeof a1;
271 #endif
272     a1->ss_family = a2->ss_family = a3->ss_family = a4->ss_family = af;
273 
274     if (af == AF_INET)
275       a1len = a2len = a3len = a4len = sizeof (struct sockaddr_in);
276     else
277       a1len = a2len = a3len = a4len = sizeof (struct sockaddr_in6);
278 
279     if (af == AF_INET) {
280       TEST_1(su_inet_pton(af, "127.0.0.1", &sin3->sin_addr) > 0);
281       TEST_1(su_inet_pton(af, "127.0.0.1", &sin4->sin_addr) > 0);
282     } else {
283     }
284 
285     TEST(bind(s3, (struct sockaddr *)a3, a3len), 0);
286     TEST(bind(s4, (struct sockaddr *)a4, a4len), 0);
287 
288     alen = sizeof a;
289     TEST(getsockname(s3, (struct sockaddr *)a, &alen), 0);
290     sin3->sin_port = sin->sin_port;
291     memset(sin->sin_zero, 0, sizeof sin->sin_zero);
292     TEST(alen, a3len); TEST_M(a, a3, a3len);
293 
294     alen = sizeof a;
295     TEST(getsockname(s4, (struct sockaddr *)a, &alen), 0);
296     sin4->sin_port = sin->sin_port;
297     memset(sin->sin_zero, 0, sizeof sin->sin_zero);
298     TEST(alen, a4len); TEST_M(a, a4, a4len);
299 
300     TEST(connect(s1, sa3, a3len), 0);
301     TEST(getsockname(s1, (struct sockaddr *)a1, &a1len), 0);
302     TEST(connect(s2, sa4, a4len), 0);
303     TEST(getsockname(s2, (struct sockaddr *)a2, &a2len), 0);
304 
305     TEST(sres_sendto(s1, "foo", 3, 0, sa4, a4len), 3);
306     TEST(sres_recvfrom(s4, buf, sizeof buf, 0, sa, &alen), 3);
307     TEST(sres_sendto(s4, "bar", 3, 0, sa, alen), 3);
308     TEST(sres_recvfrom(s2, buf, sizeof buf, 0, sa, &alen), -1);
309     TEST(sres_recvfrom(s1, buf, sizeof buf, 0, sa, &alen), 3);
310 
311     sres_close(s1), sres_close(s2), sres_close(s3), sres_close(s4);
312 
313     break;
314   }
315 
316   END();
317 }
318 
319 #endif
320 
321 static unsigned offset;
322 
323 #define TEST_RUN(ctx) \
324   { sres_free_answers(ctx->resolver, ctx->result); ctx->result = NULL;	\
325     ctx->query = NULL; run(ctx); TEST_1(ctx->query); }
326 
327 
328 static
poll_sockets(sres_context_t * ctx)329 int poll_sockets(sres_context_t *ctx)
330 {
331   int i, n, events;
332 
333   n = ctx->n_sockets;
334 
335   events = poll(ctx->fds, ctx->n_sockets, ctx->timeout ? 50 : 500);
336 
337   if (!events)
338     return events;
339 
340   for (i = 0; i < n; i++) {
341     if (ctx->fds[i].revents & POLLERR)
342       sres_resolver_error(ctx->resolver, ctx->fds[i].fd);
343     if (ctx->fds[i].revents & POLLIN)
344       sres_resolver_receive(ctx->resolver, ctx->fds[i].fd);
345   }
346 
347   return events;
348 }
349 
350 #define BREAK(ctx) (ctx->ready = 1)
run(sres_context_t * ctx)351 static void run(sres_context_t *ctx)
352 {
353   for (ctx->ready = 0; !ctx->ready; ) {
354     poll_sockets(ctx);
355 
356     if (ctx->timeout) {
357       ctx->timeout <<= 1;
358       offset += ctx->timeout;
359     }
360 
361     /* No harm is done (except wasted CPU) if timer is called more often */
362     sres_resolver_timer(ctx->resolver, -1);
363   }
364 }
365 
test_soa(sres_context_t * ctx)366 int test_soa(sres_context_t *ctx)
367 {
368   sres_resolver_t *res = ctx->resolver;
369   sres_record_t **result;
370   const sres_soa_record_t *rr_soa;
371 
372   char const *domain = "example.com";
373 
374   BEGIN();
375 
376   TEST_1(sres_query(res, test_answer, ctx, sres_type_soa, domain));
377   TEST_RUN(ctx);
378 
379   TEST_1(sres_query(res, test_answer, ctx, sres_type_soa, domain));
380   TEST_RUN(ctx);
381 
382   TEST_1(result = sres_cached_answers(res, sres_type_soa, domain));
383 
384   TEST_1(result != NULL);
385   TEST_1(result[0] != NULL);
386 
387   rr_soa = result[0]->sr_soa;
388   TEST(rr_soa->soa_record->r_type, sres_type_soa);
389   TEST(rr_soa->soa_record->r_class, sres_class_in);
390 
391   TEST_S(rr_soa->soa_mname, "ns.example.com.");
392   TEST_S(rr_soa->soa_rname, "root.example.com.");
393   TEST(rr_soa->soa_serial, 2002042901);
394   TEST(rr_soa->soa_refresh, 7200);
395   TEST(rr_soa->soa_retry, 600);
396   TEST(rr_soa->soa_expire, 36000000);
397   TEST(rr_soa->soa_minimum, 60);
398 
399   sres_free_answers(res, result);
400 
401   END();
402 }
403 
test_naptr(sres_context_t * ctx)404 int test_naptr(sres_context_t *ctx)
405 {
406   sres_resolver_t *res = ctx->resolver;
407   sres_record_t **result;
408   const sres_naptr_record_t *rr;
409   char const *domain = "example.com";
410   int i;
411 
412   BEGIN();
413 
414   TEST_1(sres_query(res, test_answer, ctx, sres_type_naptr, domain));
415   TEST_RUN(ctx);
416 
417   TEST_1(result = ctx->result);
418   TEST_1(result[0]);
419 
420   for (i = 0; result[i] != NULL; i++) {
421     rr = (sres_naptr_record_t *) result[i]->sr_naptr;
422 
423     switch(rr->na_order) {
424     case 20:
425       TEST(rr->na_record->r_type, sres_type_naptr);
426       TEST(rr->na_record->r_class, sres_class_in);
427       TEST(rr->na_record->r_ttl, 60);
428       TEST(rr->na_order, 20);
429       TEST(rr->na_prefer, 50);
430       TEST_S(rr->na_flags, "s");
431       TEST_S(rr->na_services, "SIPS+D2T");
432       TEST_S(rr->na_regexp, "");
433       TEST_S(rr->na_replace, "_sips._tcp.example.com.");
434       break;
435 
436     case 40:
437       TEST(rr->na_record->r_type, sres_type_naptr);
438       TEST(rr->na_record->r_class, sres_class_in);
439       TEST(rr->na_record->r_ttl, 60);
440       TEST(rr->na_order, 40);
441       TEST(rr->na_prefer, 15);
442       TEST_S(rr->na_flags, "s");
443       TEST_S(rr->na_services, "SIP+D2U");
444       TEST_S(rr->na_regexp, "");
445       TEST_S(rr->na_replace, "_sip._udp.example.com.");
446       break;
447 
448     case 50:
449       TEST(rr->na_record->r_type, sres_type_naptr);
450       TEST(rr->na_record->r_class, sres_class_in);
451       TEST(rr->na_record->r_ttl, 60);
452       TEST(rr->na_order, 50);
453       TEST(rr->na_prefer, 15);
454       TEST_S(rr->na_flags, "u");
455       TEST_S(rr->na_services, "TEST+D2U");
456       TEST_S(rr->na_regexp, "/(tst:([^@]+@))?example.com$/\\1operator.com/i");
457       TEST_S(rr->na_replace, ".");
458       break;
459 
460     case 80:
461       TEST(rr->na_record->r_type, sres_type_naptr);
462       TEST(rr->na_record->r_class, sres_class_in);
463       TEST(rr->na_record->r_ttl, 60);
464       TEST(rr->na_order, 80);
465       TEST(rr->na_prefer, 25);
466       TEST_S(rr->na_flags, "s");
467       TEST_S(rr->na_services, "SIP+D2T");
468       TEST_S(rr->na_regexp, "");
469       TEST_S(rr->na_replace, "_sip._tcp.example.com.");
470       break;
471 
472     default:
473       TEST_1(0);
474     }
475   }
476 
477   sres_free_answers(res, ctx->result), ctx->result = NULL;
478 
479   END();
480 }
481 
482 char const longname[1026] =
483   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
484   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
485   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
486   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
487   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
488   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
489   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
490   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
491 
492   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
493   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
494   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
495   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
496   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
497   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
498   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
499   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
500 
501   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
502   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
503   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
504   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
505   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
506   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
507   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
508   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
509 
510   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
511   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
512   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
513   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
514   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
515   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
516   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
517   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.";
518 
519 char name2048[2049] =
520   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
521   "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
522   "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
523   "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
524   "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
525   "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
526   "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
527   "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
528 
529   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
530   "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
531   "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
532   "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
533   "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
534   "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
535   "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
536   "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
537 
538   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
539   "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
540   "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
541   "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
542   "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
543   "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
544   "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
545   "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
546 
547   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
548   "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
549   "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
550   "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
551   "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
552   "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
553   "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
554   "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh";
555 
test_a(sres_context_t * ctx)556 int test_a(sres_context_t *ctx)
557 {
558   sres_resolver_t *res = ctx->resolver;
559   sres_query_t *query;
560   sres_record_t **result;
561   const sres_a_record_t *rr_a;
562   char const *domain = "sip00.example.com";
563   char name1025[1026] =
564 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
565 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
566 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
567 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
568 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
569 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
570 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
571 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
572 
573 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
574 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
575 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
576 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
577 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
578 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
579 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
580 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
581 
582 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
583 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
584 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
585 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
586 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
587 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
588 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
589 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
590 
591 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
592 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
593 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
594 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
595 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
596 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
597 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
598 		  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
599 
600 		  ".";
601 
602   BEGIN();
603 
604   TEST_1(!sres_query(res, test_answer, ctx, sres_type_a, name1025));
605 
606   name1025[1024] = '\0';
607 
608   TEST_1(!sres_query(res, test_answer, ctx, sres_type_a, name1025));
609 
610   name1025[1023] = '\0';
611 
612   TEST_1(!sres_query(res, test_answer, ctx, sres_type_a, name1025));
613 
614   TEST_1(!sres_query(res, test_answer, ctx, sres_type_a, name2048));
615 
616   query = sres_query(res, test_answer, ctx, sres_type_a, longname);
617   TEST_1(query);
618   sres_query_bind(query, NULL, NULL);
619 
620   TEST_1(sres_query(res, test_answer, ctx, sres_type_a, domain));
621 
622   TEST_RUN(ctx);
623 
624   TEST_1(result = ctx->result);
625   TEST_1(result[0]);
626   TEST(result[0]->sr_record->r_status, 0);
627   TEST_1(rr_a = result[0]->sr_a);
628   TEST(rr_a->a_record->r_type, sres_type_a);
629   TEST(rr_a->a_record->r_class, sres_class_in);
630   TEST(rr_a->a_record->r_ttl, 60);
631 
632   TEST_S(inet_ntoa(rr_a->a_addr), "194.2.188.133");
633 
634   sres_free_answers(res, ctx->result), ctx->result = NULL;
635 
636   TEST_1(result = sres_cached_answers(res, sres_type_a, domain));
637   TEST(result[0]->sr_record->r_status, 0);
638   TEST_1(rr_a = result[0]->sr_a);
639   TEST(rr_a->a_record->r_type, sres_type_a);
640   TEST(rr_a->a_record->r_class, sres_class_in);
641   TEST(rr_a->a_record->r_ttl, 60);
642   TEST_S(inet_ntoa(rr_a->a_addr), "194.2.188.133");
643 
644   sres_free_answers(res, result);
645 
646   /* Try sub-queries */
647   TEST_1(sres_search(res, test_answer, ctx, sres_type_a, "sip00"));
648 
649   TEST_RUN(ctx);
650 
651   TEST_1(result = ctx->result);
652 
653   for (;*result; result++)
654     if (result[0]->sr_a->a_record->r_type == sres_type_a)
655       break;
656 
657   TEST(result[0]->sr_record->r_status, 0);
658   TEST_1(rr_a = result[0]->sr_a);
659   TEST(rr_a->a_record->r_type, sres_type_a);
660   TEST(rr_a->a_record->r_class, sres_class_in);
661   TEST(rr_a->a_record->r_ttl, 60);
662   TEST_S(inet_ntoa(rr_a->a_addr), "194.2.188.133");
663 
664   sres_free_answers(res, ctx->result), ctx->result = NULL;
665 
666   /* Try missing domain */
667   TEST_1(sres_query(res, test_answer, ctx, sres_type_a,
668 			 "no-sip.example.com"));
669 
670   TEST_RUN(ctx);
671 
672   TEST_1(result = ctx->result);
673   TEST(result[0]->sr_record->r_status, SRES_NAME_ERR);
674   TEST_1(rr_a = result[0]->sr_a);
675   TEST(rr_a->a_record->r_type, sres_type_a);
676   TEST(rr_a->a_record->r_class, sres_class_in);
677   /* Error gets TTL from example.com SOA record minimum time */
678   TEST(rr_a->a_record->r_ttl, 60);
679 
680   sres_free_answers(res, ctx->result), ctx->result = NULL;
681 
682   /* Try domain without A record =>
683      we should get a record with SRES_RECORD_ERR */
684   TEST_1(sres_query(res, test_answer, ctx, sres_type_a,
685 			 "aaaa.example.com"));
686 
687   TEST_RUN(ctx);
688 
689   TEST_1(result = ctx->result);
690   TEST(result[0]->sr_record->r_status, SRES_RECORD_ERR);
691   TEST_1(rr_a = result[0]->sr_a);
692   TEST(rr_a->a_record->r_type, sres_type_a);
693   TEST(rr_a->a_record->r_class, sres_class_in);
694   /* Error gets TTL from example.com SOA record minimum time */
695   TEST(rr_a->a_record->r_ttl, 60);
696 
697   sres_free_answers(res, ctx->result), ctx->result = NULL;
698 
699   /* Cached search */
700   TEST_1(result = sres_search_cached_answers(res, sres_type_a, "sip00"));
701   TEST_1(rr_a = result[0]->sr_a);
702   TEST(rr_a->a_record->r_status, 0);
703   TEST_S(rr_a->a_record->r_name, "sip00.example.com.");
704   TEST(rr_a->a_record->r_type, sres_type_a);
705   TEST(rr_a->a_record->r_class, sres_class_in);
706   TEST(rr_a->a_record->r_ttl, 60);
707   TEST_S(inet_ntoa(rr_a->a_addr), "194.2.188.133");
708 
709   if (result[1]) {
710     TEST(result[1]->sr_a->a_record->r_type, sres_type_a);
711   }
712 
713   sres_free_answers(res, result), result = NULL;
714 
715   /* Cached search */
716   TEST_1(result = sres_cached_answers(res, sres_type_a, "no-sip.example.com"));
717   TEST_1(rr_a = result[0]->sr_a);
718   TEST(rr_a->a_record->r_status, SRES_NAME_ERR);
719   TEST(rr_a->a_record->r_type, sres_type_a);
720   TEST(rr_a->a_record->r_class, sres_class_in);
721 
722   sres_free_answers(res, result), result = NULL;
723 
724   END();
725 }
726 
727 #if HAVE_SIN6
test_a6(sres_context_t * ctx)728 int test_a6(sres_context_t *ctx)
729 {
730   sres_resolver_t *res = ctx->resolver;
731   sres_record_t **result;
732   const sres_a6_record_t *rr_a6;
733   char buf[50] = {0};
734   char const *domain = "oldns.example.com";
735 
736   BEGIN();
737 
738   TEST_1(sres_query(res, test_answer, ctx, sres_type_a6, domain));
739   TEST_RUN(ctx);
740 
741   TEST_1(result = ctx->result);
742   TEST_1(result[0]);
743 
744   rr_a6 = result[0]->sr_a6;
745   TEST(rr_a6->a6_record->r_type, sres_type_a6);
746   TEST(rr_a6->a6_record->r_class, sres_class_in);
747   TEST(rr_a6->a6_record->r_ttl, 60);
748   TEST(rr_a6->a6_prelen, 0);
749 
750   TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)),
751 	 "3ffe:1200:3012:c000:210:a4ff:fe8d:6a46");
752 
753   TEST_P(rr_a6->a6_prename, NULL);
754 
755   sres_free_answers(res, ctx->result), ctx->result = NULL;
756 
757   TEST_1(result = sres_cached_answers(res, sres_type_a6, domain));
758   TEST_1(rr_a6 = result[0]->sr_a6);
759   TEST(rr_a6->a6_record->r_type, sres_type_a6);
760   TEST(rr_a6->a6_record->r_class, sres_class_in);
761   TEST(rr_a6->a6_record->r_ttl, 60);
762   TEST(rr_a6->a6_prelen, 0);
763 
764   TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)),
765 	 "3ffe:1200:3012:c000:210:a4ff:fe8d:6a46");
766 
767   TEST_P(rr_a6->a6_prename, NULL);
768 
769   sres_free_answers(res, result), result = NULL;
770 
771   END();
772 }
773 
test_a6_prefix(sres_context_t * ctx)774 int test_a6_prefix(sres_context_t *ctx)
775 {
776   sres_resolver_t *res = ctx->resolver;
777   sres_record_t **result;
778   const sres_a6_record_t *rr_a6;
779   char buf[50] = {0};
780   char const *domain = "a6.example.com";
781 
782   BEGIN();
783 
784   TEST_1(sres_query(res, test_answer, ctx, sres_type_a6, domain));
785   TEST_RUN(ctx);
786 
787   TEST_1(result = ctx->result);
788   TEST_1(rr_a6 = result[0]->sr_a6);
789   TEST(rr_a6->a6_record->r_type, sres_type_a6);
790   TEST(rr_a6->a6_record->r_class, sres_class_in);
791   TEST(rr_a6->a6_record->r_ttl, 60);
792   TEST(rr_a6->a6_prelen, 64);
793 
794   TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)),
795 	 "::a08:20ff:fe7d:e7ac");
796 
797   TEST_S(rr_a6->a6_prename, "mynet.example.com.");
798 
799   sres_free_answers(res, ctx->result), ctx->result = NULL;
800 
801   /* Check parsing of special case: no prefix */
802   TEST_1(sres_query(res, test_answer, ctx, sres_type_a6, "full.example.com"));
803   TEST_RUN(ctx);
804 
805   TEST_1(result = ctx->result);
806   TEST_1(rr_a6 = result[0]->sr_a6);
807   TEST(rr_a6->a6_record->r_type, sres_type_a6);
808   TEST(rr_a6->a6_record->r_class, sres_class_in);
809   TEST(rr_a6->a6_record->r_ttl, 60);
810   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;
816 
817   /* Check parsing of special case: no suffix */
818   TEST_1(sres_query(res, test_answer, ctx, sres_type_a6, "alias6.example.com"));
819   TEST_RUN(ctx);
820 
821   TEST_1(result = ctx->result);
822   TEST_1(rr_a6 = result[0]->sr_a6);
823   TEST(rr_a6->a6_record->r_type, sres_type_a6);
824   TEST(rr_a6->a6_record->r_class, sres_class_in);
825   TEST(rr_a6->a6_record->r_ttl, 60);
826   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 
830   sres_free_answers(res, ctx->result), ctx->result = NULL;
831 
832   TEST_1(result = sres_cached_answers(res, sres_type_a6, domain));
833 
834   TEST_1(rr_a6 = result[0]->sr_a6);
835   TEST(rr_a6->a6_record->r_type, sres_type_a6);
836   TEST(rr_a6->a6_record->r_class, sres_class_in);
837   TEST(rr_a6->a6_record->r_ttl, 60);
838   TEST(rr_a6->a6_prelen, 64);
839 
840   TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)),
841 	 "::a08:20ff:fe7d:e7ac");
842 
843   TEST_S(rr_a6->a6_prename, "mynet.example.com.");
844 
845   sres_free_answers(res, result), result = NULL;
846 
847   END();
848 }
849 
test_aaaa(sres_context_t * ctx)850 int test_aaaa(sres_context_t *ctx)
851 {
852   sres_resolver_t *res = ctx->resolver;
853   sres_record_t **result;
854   const sres_aaaa_record_t *rr_aaaa;
855   char buf[50] = {0};
856   char const *domain = "sip03.example.com";
857 
858   BEGIN();
859 
860   TEST_1(sres_query(res, test_answer, ctx, sres_type_aaaa, domain));
861   TEST_RUN(ctx);
862 
863   TEST_1(result = ctx->result);
864   TEST_1(rr_aaaa = result[0]->sr_aaaa);
865   TEST(rr_aaaa->aaaa_record->r_type, sres_type_aaaa);
866   TEST(rr_aaaa->aaaa_record->r_class, sres_class_in);
867   TEST(rr_aaaa->aaaa_record->r_ttl, 60);
868 
869   TEST_S(su_inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)),
870 	 "3ffe:1200:3012:c000:206:5bff:fe55:4630");
871 
872   sres_free_answers(res, ctx->result), ctx->result = NULL;
873 
874   TEST_1(result = sres_cached_answers(res, sres_type_aaaa, domain));
875 
876   TEST_1(rr_aaaa = result[0]->sr_aaaa);
877   TEST(rr_aaaa->aaaa_record->r_type, sres_type_aaaa);
878   TEST(rr_aaaa->aaaa_record->r_class, sres_class_in);
879   TEST(rr_aaaa->aaaa_record->r_ttl, 60);
880 
881   TEST_S(su_inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)),
882 	 "3ffe:1200:3012:c000:206:5bff:fe55:4630");
883 
884   sres_free_answers(res, result), result = NULL;
885 
886   END();
887 }
888 #endif /* HAVE_SIN6 */
889 
test_srv(sres_context_t * ctx)890 int test_srv(sres_context_t *ctx)
891 {
892   sres_resolver_t *res = ctx->resolver;
893   sres_record_t **result;
894   const sres_srv_record_t *rr;
895   char const *domain = "_sips._tcp.example.com";
896   int i;
897 
898   BEGIN();
899 
900   TEST_1(sres_query(res, test_answer, ctx, sres_type_srv, domain));
901   TEST_RUN(ctx);
902 
903   TEST_1(result = ctx->result);
904 
905   for (i = 0; result[i] != NULL; i++) {
906     TEST_1(rr = (sres_srv_record_t *) result[i]->sr_srv);
907 
908     switch(rr->srv_priority) {
909     case 3:
910       TEST(rr->srv_record->r_type, sres_type_srv);
911       TEST(rr->srv_record->r_class, sres_class_in);
912       TEST(rr->srv_record->r_ttl, 60);
913       TEST(rr->srv_weight, 100);
914       TEST(rr->srv_port, 5061);
915       TEST_S(rr->srv_target, "sip00.example.com.");
916       break;
917 
918     case 4:
919       TEST(rr->srv_record->r_type, sres_type_srv);
920       TEST(rr->srv_record->r_class, sres_class_in);
921       TEST(rr->srv_record->r_ttl, 60);
922       TEST(rr->srv_weight, 50);
923       TEST(rr->srv_port, 5050);
924       TEST_S(rr->srv_target, "sip02.example.com.");
925       break;
926 
927     case 5:
928       TEST(rr->srv_record->r_type, sres_type_srv);
929       TEST(rr->srv_record->r_class, sres_class_in);
930       TEST(rr->srv_record->r_ttl, 60);
931       TEST(rr->srv_weight, 10);
932       TEST(rr->srv_port, 5060);
933       TEST_S(rr->srv_target, "sip01.example.com.");
934       break;
935 
936     default:
937       TEST_1(0);
938     }
939   }
940 
941   sres_free_answers(res, ctx->result), ctx->result = NULL;
942 
943   TEST_1(result = sres_cached_answers(res, sres_type_srv, domain));
944 
945   for (i = 0; result[i] != NULL; i++) {
946     TEST_1(rr = (sres_srv_record_t *) result[i]->sr_srv);
947 
948     switch(rr->srv_priority) {
949     case 3:
950       TEST(rr->srv_record->r_type, sres_type_srv);
951       TEST(rr->srv_record->r_class, sres_class_in);
952       TEST(rr->srv_record->r_ttl, 60);
953       TEST(rr->srv_weight, 100);
954       TEST(rr->srv_port, 5061);
955       TEST_S(rr->srv_target, "sip00.example.com.");
956       break;
957 
958     case 4:
959       TEST(rr->srv_record->r_type, sres_type_srv);
960       TEST(rr->srv_record->r_class, sres_class_in);
961       TEST(rr->srv_record->r_ttl, 60);
962       TEST(rr->srv_weight, 50);
963       TEST(rr->srv_port, 5050);
964       TEST_S(rr->srv_target, "sip02.example.com.");
965       break;
966 
967     case 5:
968       TEST(rr->srv_record->r_type, sres_type_srv);
969       TEST(rr->srv_record->r_class, sres_class_in);
970       TEST(rr->srv_record->r_ttl, 60);
971       TEST(rr->srv_weight, 10);
972       TEST(rr->srv_port, 5060);
973       TEST_S(rr->srv_target, "sip01.example.com.");
974       break;
975 
976     default:
977       TEST_1(0);
978     }
979   }
980 
981   sres_free_answers(res, result), result = NULL;
982 
983   END();
984 }
985 
test_cname(sres_context_t * ctx)986 int test_cname(sres_context_t *ctx)
987 {
988   sres_resolver_t *res = ctx->resolver;
989   sres_record_t **result, *sr;
990   const sres_cname_record_t *cn;
991   char const *domain = "sip.example.com";
992 
993   BEGIN();
994 
995   TEST_1(sres_query(res, test_answer, ctx, sres_type_naptr, domain));
996   TEST_RUN(ctx);
997 
998   TEST_1(result = ctx->result);
999   TEST_1(sr = result[0]);
1000   TEST_1(sr->sr_record->r_status == SRES_RECORD_ERR);
1001 
1002   sres_free_answers(res, ctx->result), ctx->result = NULL;
1003 
1004   TEST_1(result = sres_cached_answers(res, sres_type_naptr, domain));
1005   TEST_1(sr = result[0]);
1006   TEST_1(sr->sr_record->r_status == SRES_RECORD_ERR);
1007 
1008   sres_free_answers(res, result), ctx->result = NULL;
1009 
1010   TEST_1(result = sres_cached_answers(res, sres_qtype_any, domain));
1011   TEST_1(cn = result[0]->sr_cname);
1012   TEST(cn->cn_record->r_class, sres_class_in);
1013   TEST(cn->cn_record->r_type, sres_type_cname);
1014   TEST(cn->cn_record->r_ttl, 60);
1015   TEST_S(cn->cn_cname, "sip00.example.com.");
1016   /* We might have A record, or then not */
1017 
1018   sres_free_answers(res, result), ctx->result = NULL;
1019 
1020   TEST_1(sres_query(res, test_answer, ctx, sres_type_a, domain));
1021   TEST_RUN(ctx);
1022 
1023   TEST_1(result = ctx->result);
1024   TEST_1(sr = result[0]);
1025   TEST(sr->sr_record->r_class, sres_class_in);
1026   TEST(sr->sr_record->r_type, sres_type_a);
1027 
1028   sres_free_answers(res, ctx->result), ctx->result = NULL;
1029 
1030   TEST_1(result = sres_cached_answers(res, sres_type_a, domain));
1031   TEST_1(sr = result[0]);
1032   TEST(sr->sr_record->r_class, sres_class_in);
1033   TEST(sr->sr_record->r_type, sres_type_a);
1034 
1035   sres_free_answers(res, result);
1036 
1037   domain = "cloop.example.com";
1038 
1039   TEST_1(sres_query(res, test_answer, ctx, sres_type_a, domain));
1040   TEST_RUN(ctx);
1041 
1042   TEST_1(result = ctx->result);
1043   TEST_1(sr = result[0]);
1044   TEST_1(sr->sr_record->r_status == SRES_RECORD_ERR);
1045 
1046   sres_free_answers(res, ctx->result), ctx->result = NULL;
1047 
1048   TEST_1(result = sres_cached_answers(res, sres_type_a, domain));
1049   TEST_1(sr = result[0]);
1050   TEST_1(sr->sr_record->r_status == SRES_RECORD_ERR);
1051 
1052   sres_free_answers(res, result);
1053 
1054   END();
1055 }
1056 
test_ptr_ipv4(sres_context_t * ctx)1057 int test_ptr_ipv4(sres_context_t *ctx)
1058 {
1059   sres_resolver_t *res = ctx->resolver;
1060   sres_record_t **result;
1061   const sres_ptr_record_t *rr;
1062   char const *domain = "1.0.0.127.in-addr.arpa";
1063 
1064   BEGIN();
1065 
1066   TEST_1(sres_query(res, test_answer, ctx, sres_type_ptr, domain));
1067   TEST_RUN(ctx);
1068 
1069   result = ctx->result;
1070   TEST_1(result != NULL);
1071   TEST_1(result[0] != NULL);
1072 
1073   rr = result[0]->sr_ptr;
1074   TEST_1(rr != NULL);
1075   TEST(rr->ptr_record->r_class, sres_class_in);
1076   TEST(rr->ptr_record->r_type, sres_type_ptr);
1077   TEST_S(rr->ptr_domain, "localhost.");
1078 
1079   END();
1080 }
1081 
test_ptr_ipv4_sockaddr(sres_context_t * ctx)1082 int test_ptr_ipv4_sockaddr(sres_context_t *ctx)
1083 {
1084   sres_resolver_t *res = ctx->resolver;
1085   sres_record_t **result;
1086   sres_query_t *query;
1087   const sres_ptr_record_t *rr;
1088   struct sockaddr_in sin = {0};
1089 
1090   su_inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
1091   sin.sin_family = AF_INET;
1092 
1093   BEGIN();
1094 
1095   query = sres_query_sockaddr(res, test_answer, ctx,
1096 				   sres_qtype_any, (struct sockaddr*)&sin);
1097   TEST_1(query != NULL);
1098 
1099   TEST_RUN(ctx);
1100 
1101   result = ctx->result;
1102 
1103   TEST_1(result != NULL);
1104   TEST_1(result[0] != NULL);
1105 
1106   rr = result[0]->sr_ptr;
1107   TEST_1(rr != NULL);
1108   TEST(rr->ptr_record->r_type, sres_type_ptr);
1109   TEST(rr->ptr_record->r_class, sres_class_in);
1110   TEST_S(rr->ptr_domain, "localhost.");
1111 
1112   END();
1113 }
1114 
1115 #if HAVE_SIN6
test_ptr_ipv6(sres_context_t * ctx)1116 int test_ptr_ipv6(sres_context_t *ctx)
1117 {
1118   sres_resolver_t *res = ctx->resolver;
1119   sres_record_t **result;
1120   const sres_ptr_record_t *rr;
1121   char const *domain =
1122     "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";
1123 
1124   BEGIN();
1125 
1126   TEST_1(sres_query(res, test_answer, ctx, sres_type_ptr, domain));
1127   TEST_RUN(ctx);
1128 
1129   result = ctx->result;
1130 
1131   TEST_1(result != NULL);
1132   TEST_1(result[0] != NULL);
1133 
1134   rr = result[0]->sr_ptr;
1135   TEST_1(rr != NULL);
1136   TEST(rr->ptr_record->r_type, sres_type_ptr);
1137   TEST(rr->ptr_record->r_class, sres_class_in);
1138   TEST_S(rr->ptr_domain, "sip01.example.com.");
1139 
1140   END();
1141 }
1142 
test_ptr_ipv6_sockaddr(sres_context_t * ctx)1143 int test_ptr_ipv6_sockaddr(sres_context_t *ctx)
1144 {
1145   sres_resolver_t *res = ctx->resolver;
1146   sres_record_t **result;
1147   const sres_ptr_record_t *rr;
1148   struct sockaddr_in6 sin6 = {0};
1149 
1150   BEGIN();
1151 
1152   su_inet_pton(AF_INET6, "3ffe:1200:3012:c000:0a08:20ff:fe7d:e7ac",
1153 	       &sin6.sin6_addr);
1154 
1155   sin6.sin6_family = AF_INET6;
1156 
1157   ctx->query =
1158     sres_query_sockaddr(res, test_answer, ctx,
1159 			     sres_type_ptr, (struct sockaddr*)&sin6);
1160   TEST_1(ctx->query != NULL);
1161 
1162   TEST_RUN(ctx);
1163 
1164   result = ctx->result;
1165 
1166   TEST_1(result != NULL);
1167   TEST_1(result[0] != NULL);
1168 
1169   rr = result[0]->sr_ptr;
1170   TEST_1(rr != NULL);
1171   TEST(rr->ptr_record->r_type, sres_type_ptr);
1172   TEST(rr->ptr_record->r_class, sres_class_in);
1173   TEST_S(rr->ptr_domain, "sip01.example.com.");
1174 
1175   END();
1176 }
1177 #endif /* HAVE_SIN6 */
1178 
1179 /* Test sres_cached_answers(), sres_sort_answers(), sres_free_answers() */
test_cache(sres_context_t * ctx)1180 int test_cache(sres_context_t *ctx)
1181 {
1182   sres_resolver_t *res = ctx->resolver;
1183   int ok = 0;
1184   sres_record_t *sort_array[3];
1185   sres_record_t **result;
1186   sres_query_t *query;
1187   const sres_record_t *rr = NULL;
1188   const sres_a6_record_t *rr_a6;
1189   const sres_aaaa_record_t *rr_aaaa;
1190   const sres_cname_record_t *rr_cname;
1191   const sres_ptr_record_t *rr_ptr;
1192 #if HAVE_SIN6
1193   struct sockaddr_in6 sin6 = {0};
1194 #endif
1195   char const *domain;
1196   char buf[50] = {0};
1197   int i, j;
1198 
1199   BEGIN();
1200 
1201   sres_query(res, test_answer_multi, ctx,
1202 		  sres_qtype_any, "example.com");
1203 
1204   sres_query(res, test_answer_multi, ctx,
1205 		  sres_qtype_any, "_sips._tcp.example.com");
1206 
1207   sres_query(res, test_answer_multi, ctx,
1208 		  sres_qtype_any, "sip.example.com");
1209 
1210   sres_query(res, test_answer_multi, ctx,
1211 		  sres_qtype_any, "subnet.example.com");
1212 
1213 #if HAVE_SIN6
1214   sres_query(res, test_answer_multi, ctx,
1215 	     sres_type_aaaa, "mgw02.example.com");
1216 
1217   su_inet_pton(AF_INET6,
1218 	       "3ffe:1200:3012:c000:0a08:20ff:fe7d:e7ac",
1219 	       &sin6.sin6_addr);
1220 
1221   sin6.sin6_family = AF_INET6;
1222 
1223   query = sres_query_sockaddr(res, test_answer_multi, ctx,
1224 			      sres_qtype_any, (struct sockaddr *)&sin6);
1225 
1226   TEST_1(query != NULL);
1227 #endif
1228 
1229   TEST_RUN(ctx);
1230 
1231   /* For a chance, a fully qualified domain name with final "." */
1232   domain = "example.com.";
1233 
1234   result = sres_cached_answers(res,
1235 			       sres_qtype_any,
1236 			       domain);
1237 
1238   TEST_1(result != NULL);
1239 
1240   for (i = 0; result[i] != NULL; i++) {
1241     rr = result[i];
1242 
1243     if (rr->sr_record->r_type == sres_type_naptr) {
1244       const sres_naptr_record_t *rr_naptr = rr->sr_naptr;
1245       switch(rr_naptr->na_order) {
1246       case 20:
1247         TEST(rr_naptr->na_record->r_type, sres_type_naptr);
1248         TEST(rr_naptr->na_record->r_class, sres_class_in);
1249         TEST(rr_naptr->na_record->r_ttl, 60);
1250         TEST(rr_naptr->na_order, 20);
1251         TEST(rr_naptr->na_prefer, 50);
1252         TEST_S(rr_naptr->na_flags, "s");
1253         TEST_S(rr_naptr->na_services, "SIPS+D2T");
1254         TEST_S(rr_naptr->na_regexp, "");
1255         TEST_S(rr_naptr->na_replace, "_sips._tcp.example.com.");
1256         ok |= 1;
1257         break;
1258 
1259       case 40:
1260         TEST(rr_naptr->na_record->r_type, sres_type_naptr);
1261         TEST(rr_naptr->na_record->r_class, sres_class_in);
1262         TEST(rr_naptr->na_record->r_ttl, 60);
1263         TEST(rr_naptr->na_order, 40);
1264         TEST(rr_naptr->na_prefer, 15);
1265         TEST_S(rr_naptr->na_flags, "s");
1266         TEST_S(rr_naptr->na_services, "SIP+D2U");
1267         TEST_S(rr_naptr->na_regexp, "");
1268         TEST_S(rr_naptr->na_replace, "_sip._udp.example.com.");
1269         ok |= 2;
1270         break;
1271 
1272       case 50:
1273         TEST(rr_naptr->na_record->r_type, sres_type_naptr);
1274         TEST(rr_naptr->na_record->r_class, sres_class_in);
1275         TEST(rr_naptr->na_record->r_ttl, 60);
1276         TEST(rr_naptr->na_order, 50);
1277         TEST(rr_naptr->na_prefer, 15);
1278         TEST_S(rr_naptr->na_flags, "u");
1279         TEST_S(rr_naptr->na_services, "TEST+D2U");
1280 
1281         TEST_S(rr_naptr->na_regexp,
1282 	       "/(tst:([^@]+@))?example.com$/\\1operator.com/i");
1283 
1284         TEST_S(rr_naptr->na_replace, ".");
1285         break;
1286 
1287       case 80:
1288         TEST(rr_naptr->na_record->r_type, sres_type_naptr);
1289         TEST(rr_naptr->na_record->r_class, sres_class_in);
1290         TEST(rr_naptr->na_record->r_ttl, 60);
1291         TEST(rr_naptr->na_order, 80);
1292         TEST(rr_naptr->na_prefer, 25);
1293         TEST_S(rr_naptr->na_flags, "s");
1294         TEST_S(rr_naptr->na_services, "SIP+D2T");
1295         TEST_S(rr_naptr->na_regexp, "");
1296         TEST_S(rr_naptr->na_replace, "_sip._tcp.example.com.");
1297         ok |= 4;
1298         break;
1299 
1300       default:
1301         TEST_1(0);
1302       }
1303     }
1304   }
1305 
1306   TEST(ok, 7);
1307 
1308   /* Reverse order before sorting */
1309   for (j = 0; j < --i; j++) {
1310     sres_record_t *swap = result[j];
1311     result[j] = result[i];
1312     result[i] = swap;
1313   }
1314 
1315   /* Test sorting */
1316   sres_sort_answers(res, result);
1317 
1318   /* Sort all records with themselves */
1319   for (i = 0; result[i]; i++) {
1320     sort_array[0] = result[i], sort_array[1] = result[i], sort_array[2] = NULL;
1321     sres_sort_answers(res, sort_array);
1322   }
1323 
1324   /* Test free */
1325   for (i = 0; result[i]; i++) {
1326     sres_free_answer(res, result[i]);
1327     result[i] = NULL;
1328   }
1329 
1330   /* Test sres_free_answers() */
1331   sres_free_answers(res, result);
1332 
1333   result = sres_cached_answers(res,
1334 			       sres_qtype_any,
1335 			       "_sips._tcp.example.com");
1336 
1337   TEST_1(result != NULL);
1338 
1339   ok = 0;
1340 
1341   for (i = 0; result[i] != NULL; i++) {
1342     sres_srv_record_t *rr_srv = result[i]->sr_srv;
1343 
1344     TEST(rr_srv->srv_record->r_type, sres_type_srv);
1345     switch(rr_srv->srv_priority) {
1346     case 3:
1347       TEST(rr_srv->srv_record->r_type, sres_type_srv);
1348       TEST(rr_srv->srv_record->r_class, sres_class_in);
1349       TEST(rr_srv->srv_record->r_ttl, 60);
1350       TEST(rr_srv->srv_weight, 100);
1351       TEST(rr_srv->srv_port, 5061);
1352       TEST_S(rr_srv->srv_target, "sip00.example.com.");
1353       ok |= 1;
1354       break;
1355 
1356     case 4:
1357       TEST(rr_srv->srv_record->r_type, sres_type_srv);
1358       TEST(rr_srv->srv_record->r_class, sres_class_in);
1359       TEST(rr_srv->srv_record->r_ttl, 60);
1360       TEST(rr_srv->srv_weight, 50);
1361       TEST(rr_srv->srv_port, 5050);
1362       TEST_S(rr_srv->srv_target, "sip02.example.com.");
1363       ok |= 2;
1364       break;
1365 
1366     case 5:
1367       TEST(rr_srv->srv_record->r_type, sres_type_srv);
1368       TEST(rr_srv->srv_record->r_class, sres_class_in);
1369       TEST(rr_srv->srv_record->r_ttl, 60);
1370       TEST(rr_srv->srv_weight, 10);
1371       TEST(rr_srv->srv_port, 5060);
1372       TEST_S(rr_srv->srv_target, "sip01.example.com.");
1373       ok |= 4;
1374       break;
1375 
1376     default:
1377       TEST_1(0);
1378     }
1379   }
1380 
1381   TEST(ok, 7);
1382 
1383   /* Reverse order before sorting */
1384   for (j = 0; j < --i; j++) {
1385     sres_record_t *swap = result[j];
1386     result[j] = result[i];
1387     result[i] = swap;
1388   }
1389 
1390   sres_sort_answers(res, result);
1391   sres_free_answers(res, result);
1392 
1393   domain = "sip.example.com";
1394   result = sres_cached_answers(res,
1395 			       sres_qtype_any,
1396 			       domain);
1397 
1398   TEST_1(result != NULL);
1399   TEST_1(result[0] != NULL);
1400 
1401   rr_cname = result[0]->sr_cname;
1402   TEST(rr_cname->cn_record->r_type, sres_type_cname);
1403   TEST(rr_cname->cn_record->r_class, sres_class_in);
1404   TEST(rr_cname->cn_record->r_ttl, 60);
1405   TEST_S(rr_cname->cn_cname, "sip00.example.com.");
1406 
1407   sres_free_answers(res, result);
1408 
1409 #if HAVE_SIN6
1410   domain = "subnet.example.com";
1411   result = sres_cached_answers(res,
1412 			       sres_qtype_any,
1413 			       domain);
1414 
1415   TEST_1(result != NULL);
1416   TEST_1(result[0] != NULL);
1417 
1418   rr_a6 = result[0]->sr_a6;
1419   TEST(rr_a6->a6_record->r_type, sres_type_a6);
1420   TEST(rr_a6->a6_record->r_class, sres_class_in);
1421   TEST(rr_a6->a6_record->r_ttl, 60);
1422   TEST(rr_a6->a6_prelen, 0);
1423 
1424   TEST_S(su_inet_ntop(AF_INET6, &rr_a6->a6_suffix, buf, sizeof(buf)), "3ff0::");
1425 
1426   TEST_P(rr_a6->a6_prename, NULL);
1427 
1428   sres_free_answers(res, result);
1429 
1430   domain = "mgw02.example.com";
1431   TEST_1(result = sres_cached_answers(res, sres_type_aaaa, domain));
1432   TEST_1(rr_aaaa = result[0]->sr_aaaa);
1433   TEST(rr_aaaa->aaaa_record->r_type, sres_type_aaaa);
1434   TEST(rr_aaaa->aaaa_record->r_class, sres_class_in);
1435   TEST(rr_aaaa->aaaa_record->r_ttl, 60);
1436   TEST_S(su_inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)),
1437 	 "3ffe:1200:3012:c000:206:5bff:fe55:462f");
1438   sres_free_answers(res, result);
1439 
1440   result = sres_cached_answers_sockaddr(res,
1441                                         sres_type_ptr,
1442                                         (struct sockaddr *)&sin6);
1443 
1444   TEST_1(result != NULL);
1445 
1446   rr_ptr = result[0]->sr_ptr;
1447   TEST_1(rr_ptr != NULL);
1448 
1449   TEST(rr_ptr->ptr_record->r_type, sres_type_ptr);
1450   TEST(rr_ptr->ptr_record->r_class, sres_class_in);
1451   TEST_S(rr_ptr->ptr_domain, "sip01.example.com.");
1452 
1453   sres_free_answers(res, result);
1454 #endif /* HAVE_SIN6 */
1455 
1456   END();
1457 }
1458 
1459 #if HAVE_SIN6
test_query_one_type(sres_context_t * ctx)1460 int test_query_one_type(sres_context_t *ctx)
1461 {
1462   sres_resolver_t *res = ctx->resolver;
1463   sres_record_t **result;
1464   const sres_aaaa_record_t *rr_aaaa;
1465   char buf[50] = {0};
1466 
1467   BEGIN();
1468 
1469   TEST_1(sres_query(res, test_answer, ctx,
1470 			 sres_type_aaaa, "mgw02.example.com"));
1471   TEST_RUN(ctx);
1472   TEST_1(result = ctx->result);
1473   TEST_1(result[0] != NULL);
1474 
1475   TEST_1(rr_aaaa = result[0]->sr_aaaa);
1476   TEST(rr_aaaa->aaaa_record->r_type, sres_type_aaaa);
1477   TEST(rr_aaaa->aaaa_record->r_class, sres_class_in);
1478   TEST(rr_aaaa->aaaa_record->r_ttl, 60);
1479   TEST_S(su_inet_ntop(AF_INET6, &rr_aaaa->aaaa_addr, buf, sizeof(buf)),
1480 	 "3ffe:1200:3012:c000:206:5bff:fe55:462f");
1481 
1482   TEST_P(result[1], NULL);
1483 
1484   END();
1485 }
1486 #endif /* HAVE_SIN6*/
1487 
1488 #ifdef _WIN32
1489 #include <fcntl.h>
1490 #endif
1491 
sink_make(sres_context_t * ctx)1492 int sink_make(sres_context_t *ctx)
1493 {
1494   char *tmpdir = getenv("TMPDIR");
1495   char *template;
1496   int fd;
1497   sres_socket_t sink;
1498   struct sockaddr_in sin[1];
1499   socklen_t sinsize = sizeof sin;
1500   FILE *f;
1501 
1502   BEGIN();
1503 
1504   sink = socket(AF_INET, SOCK_DGRAM, 0); TEST_1(sink != INVALID_SOCKET);
1505   TEST(getsockname(sink, (struct sockaddr *)sin, &sinsize), 0);
1506   sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1507   TEST(bind(sink, (struct sockaddr *)sin, sinsize), 0);
1508   TEST(getsockname(sink, (struct sockaddr *)sin, &sinsize), 0);
1509 
1510   ctx->sink = sink;
1511 
1512   template = su_sprintf(ctx->home, "%s/test_sresolv.XXXXXX",
1513 			tmpdir ? tmpdir : "/tmp");
1514   TEST_1(template);
1515 
1516 #ifndef _WIN32
1517   fd = mkstemp(template); TEST_1(fd != -1);
1518 #else
1519   fd = open(template, O_WRONLY); TEST_1(fd != -1);
1520 #endif
1521 
1522   f = fdopen(fd, "w"); TEST_1(f);
1523   fprintf(f,
1524 	  "domain example.com\n"
1525 	  "search foo.bar.com\n"
1526 	  "port %u\n",
1527 	  ntohs(sin->sin_port));
1528   fclose(f);
1529 
1530   ctx->sinkconf = template;
1531 
1532   END();
1533 }
1534 
1535 #if 0
1536 int recv_sink(su_root_magic_t *rm, su_wait_t *w, sres_context_t *ctx)
1537 {
1538   union {
1539     char bytes[8192];
1540     unsigned short shorts[4096];
1541   } buffer[1];
1542 
1543   su_wait_events(w, ctx->sink);
1544   recv(ctx->sink, buffer->bytes, sizeof buffer, 0);
1545 
1546   return 0;
1547 }
1548 
1549 int sink_init(sres_context_t *ctx)
1550 {
1551   su_wait_t w[1];
1552   BEGIN();
1553 
1554   TEST(su_wait_create(w, ctx->sink, SU_WAIT_IN), 0);
1555   ctx->sinkidx = su_root_register(ctx->root, w, recv_sink, ctx, 0);
1556   TEST_1(ctx->sinkidx != 0);
1557 
1558   END();
1559 }
1560 
1561 int sink_deinit(sres_context_t *ctx)
1562 {
1563   BEGIN();
1564 
1565   if (ctx->sinkidx)
1566     su_root_deregister(ctx->root, ctx->sinkidx);
1567   ctx->sinkidx = 0;
1568   sres_close(ctx->sink), ctx->sink = INVALID_SOCKET;
1569 
1570   END();
1571 }
1572 #endif
1573 
test_timeout(sres_context_t * ctx)1574 int test_timeout(sres_context_t *ctx)
1575 {
1576   sres_resolver_t *res = ctx->resolver;
1577   sres_record_t **result;
1578   /* const sres_soa_record_t *rr_soa; */
1579   char const *domain = "test";
1580 
1581   BEGIN();
1582 
1583   sres_query(res, test_answer, ctx, sres_type_a, domain);
1584 
1585   ctx->timeout = 1;
1586   TEST_RUN(ctx);
1587   ctx->timeout = 0;
1588 
1589   TEST_P(ctx->result, NULL);
1590 
1591   result = sres_cached_answers(res, sres_type_a, domain);
1592 
1593 #if 0  /* Currently, we do not create error records */
1594   TEST_1(result); TEST_1(result[0] != NULL);
1595 
1596   rr_soa = result[0]->sr_soa;
1597   TEST(rr_soa->soa_record->r_type, sres_type_soa);
1598   TEST(rr_soa->soa_record->r_class, sres_class_in);
1599   TEST_S(rr_soa->soa_record->r_name, "example.com.");
1600   TEST(rr_soa->soa_record->r_status, SRES_TIMEOUT_ERR);
1601 
1602   sres_free_answers(res, result);
1603 #else
1604   TEST_1(result == NULL);
1605 #endif
1606 
1607   END();
1608 }
1609 
test_answer(sres_context_t * ctx,sres_query_t * q,sres_record_t ** answer)1610 static void test_answer(sres_context_t *ctx,
1611 		 sres_query_t *q,
1612 		 sres_record_t **answer)
1613 {
1614   ctx->query = q;
1615   if (ctx->result)
1616     sres_free_answers(ctx->resolver, ctx->result);
1617   ctx->result = answer;
1618   BREAK(ctx);
1619 }
1620 
test_answer_multi(sres_context_t * ctx,sres_query_t * q,sres_record_t ** answer)1621 static void test_answer_multi(sres_context_t *ctx,
1622 		       sres_query_t *q,
1623 		       sres_record_t **answer)
1624 {
1625   static int count = 0;
1626 
1627   ctx->query = q;
1628 
1629   count++;
1630 
1631   sres_free_answers(ctx->resolver, answer);
1632 
1633   if (count == 6)
1634     BREAK(ctx);
1635 }
1636 
1637 #include <sys/time.h>
1638 
1639 /* Fake time() implementation, used by sresolv library */
time(time_t * tp)1640 time_t time(time_t *tp)
1641 {
1642   struct timeval tv[1];
1643 
1644 #ifndef _WIN32
1645   gettimeofday(tv, NULL);
1646 #else
1647   return 0;
1648 #endif
1649 
1650   if (tp)
1651     *tp = tv->tv_sec + offset;
1652 
1653   return tv->tv_sec + offset;
1654 }
1655 
test_expiration(sres_context_t * ctx)1656 int test_expiration(sres_context_t *ctx)
1657 {
1658   sres_resolver_t *res = ctx->resolver;
1659   sres_record_t **result;
1660   char const *domain = "example.com";
1661 
1662   BEGIN();
1663 
1664   offset = 3600;		/* Time suddenly proceeds by an hour.. */
1665 
1666   sres_resolver_timer(res, -1);
1667 
1668   result = sres_cached_answers(res, sres_qtype_any, domain);
1669 
1670   TEST_P(result, NULL);  /* the cache should be empty after 15 secs */
1671 
1672   END();
1673 }
1674 
1675 #define is_hexdigit(c) ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))
1676 #define hex(c) ((c >= '0' && c <= '9') ? (c - '0') : (c - 'a' + 10))
1677 
1678 /* Convert lowercase hex to binary */
1679 static
hex2bin(char const * test_name,char const * hex1,char const * hex2,size_t * binsize)1680 void *hex2bin(char const *test_name,
1681 	      char const *hex1, char const *hex2, size_t *binsize)
1682 {
1683   char output[2048];
1684   char *bin;
1685   char const *b;
1686   size_t j;
1687 
1688   if (hex1 == NULL || binsize == NULL)
1689     return NULL;
1690 
1691   for (b = hex1, j = 0; b;) {
1692     while (b[0]) {
1693       if (is_hexdigit(b[0])) {
1694 	if (!is_hexdigit(b[1])) {
1695 	  fprintf(stderr, "%s: hex2bin: invalid hex '%c'\n", test_name, b[1]);
1696 	  exit(2);
1697 	}
1698 
1699 	output[j++] = (hex(b[0]) << 4) | hex(b[1]);
1700 	if (j == sizeof(output)) {
1701 	  fprintf(stderr, "%s:%u: hex2bin: buffer too small\n",
1702 		  __FILE__, __LINE__);
1703 	  exit(2);
1704 	}
1705 	b++;
1706       } else if (b[0] != ' ') {
1707 	fprintf(stderr, "%s: hex2bin: invalid nonhex '%c'\n", test_name,
1708 		b[0]);
1709 	exit(2);
1710       }
1711       b++;
1712     }
1713     b = hex2, hex2 = NULL;
1714   }
1715 
1716   bin = malloc(j);
1717   if (bin == NULL)
1718     perror("malloc"), exit(2);
1719 
1720   return memcpy(bin, output, *binsize = j);
1721 }
1722 
1723 static char const hextest[] =
1724   "                        34 44 85 80 00 01 00 04 "
1725   "00 01 00 08 07 65 78 61 6d 70 6c 65 03 63 6f 6d "
1726   "00 00 23 00 01 c0 0c 00 23 00 01 00 00 00 3c 00 "
1727   "26 00 28 00 0f 01 73 07 53 49 50 2b 44 32 55 00 "
1728   "04 5f 73 69 70 04 5f 75 64 70 07 65 78 61 6d 70 "
1729   "6c 65 03 63 6f 6d 00 c0 42 00 23 00 01 00 00 00 "
1730   "3c 00 3e 00 32 00 0f 01 75 08 54 45 53 54 2b 44 "
1731   "32 55 2d 2f 28 74 73 74 3a 28 5b 5e 40 5d 2b 40 "
1732   "29 29 3f 65 78 61 6d 70 6c 65 2e 63 6f 6d 24 2f "
1733   "5c 31 6f 70 65 72 61 74 6f 72 2e 63 6f 6d 2f 69 "
1734   "00 c0 42 00 23 00 01 00 00 00 3c 00 26 00 50 00 "
1735   "19 01 73 07 53 49 50 2b 44 32 54 00 04 5f 73 69 "
1736   "70 04 5f 74 63 70 07 65 78 61 6d 70 6c 65 03 63 "
1737   "6f 6d 00 c0 be 00 23 00 01 00 00 00 3c 00 28 00 "
1738   "14 00 32 01 73 08 53 49 50 53 2b 44 32 54 00 05 "
1739   "5f 73 69 70 73 04 5f 74 63 70 07 65 78 61 6d 70 "
1740   "6c 65 03 63 6f 6d 00 c0 f2 00 02 00 01 00 00 00 "
1741   "3c 00 05 02 6e 73 c0 f2 05 73 69 70 30 30 c0 f2 "
1742   "00 01 00 01 00 00 00 3c 00 04 c2 02 bc 85 c1 10 "
1743   "00 1c 00 01 00 00 00 3c 00 10 3f f0 00 10 30 12 "
1744   "c0 00 02 c0 95 ff fe e2 4b 78 05 73 69 70 30 32 "
1745   "c0 f2 00 01 00 01 00 00 00 3c 00 04 c2 02 bc 87 "
1746   "c1 42 00 1c 00 01 00 00 00 3c 00 10 3f fe 12 00 "
1747   "30 12 c0 06 02 06 5b ff fe 55 46 2f 05 73 69 70 "
1748   "30 31 c0 f2 00 01 00 01 00 00 00 3c 00 04 c2 02 "
1749   "bc 86 c1 74 00 1c 00 01 00 00 00 3c 00 10 3f f0 "
1750   "00 12 30 12 c0 06 0a 08 20 ff fe 7d e7 ac c1 0b "
1751   "00 01 00 01 00 00 00 3c 00 04 c2 02 bc 85 c1 0b "
1752   "00 26 00 01 00 00 00 3c 00 11 00 3f fe 12 00 30 "
1753   "12 c0 00 02 10 a4 ff fe 8d 6a 46 ";
1754 
test_net(sres_context_t * ctx)1755 int test_net(sres_context_t *ctx)
1756 {
1757   sres_resolver_t *res = ctx->resolver;
1758   sres_query_t *q = NULL;
1759   sres_socket_t c = ctx->sink;
1760   struct sockaddr_storage ss[1];
1761   struct sockaddr *sa = (void *)ss;
1762   socklen_t salen = sizeof ss;
1763   char *bin;
1764   size_t i, binlen;
1765   ssize_t n;
1766   char const *domain = "example.com";
1767   char query[512];
1768 
1769   BEGIN();
1770 
1771   TEST_1(ctx->sink != INVALID_SOCKET && ctx->sink != (sres_socket_t)0);
1772 
1773   /* Prepare for test_answer() callback */
1774   sres_free_answers(ctx->resolver, ctx->result);
1775   ctx->result = NULL;
1776   ctx->query = NULL;
1777 
1778   /* Get canned response */
1779   TEST_1(bin = hex2bin(__func__, hextest, NULL, &binlen));
1780 
1781   /* Send responses with one erroneus byte */
1782   for (i = 1; i < binlen; i++) {
1783     if (!q) {
1784       /* We got an error => make new query */
1785       TEST_1(q = sres_query(res, test_answer, ctx, /* Send query */
1786 				 sres_type_naptr, domain));
1787       TEST_1((n = sres_recvfrom(c, query, sizeof query, 0, sa, &salen)) != -1);
1788       memcpy(bin, query, 2); /* Copy ID */
1789     }
1790     if (i != 1)
1791       bin[i] ^= 0xff;
1792     else
1793       bin[3] ^= SRES_FORMAT_ERR; /* format error -> EDNS0 failure */
1794     n = sres_sendto(c, bin, binlen, 0, sa, salen);
1795     if (i != 1)
1796       bin[i] ^= 0xff;
1797     else
1798       bin[3] ^= SRES_FORMAT_ERR;
1799     if (n == -1)
1800       perror("sendto");
1801 
1802     while (!poll_sockets(ctx))
1803       ;
1804 
1805     if (ctx->query)
1806       q = NULL;
1807   }
1808 
1809   /* Send runt responses */
1810   for (i = 1; i <= binlen; i++) {
1811     if (!q) {
1812       /* We got an error => make new query */
1813       TEST_1(q = sres_query(res, test_answer, ctx, /* Send query */
1814 				 sres_type_naptr, domain));
1815       TEST_1((n = sres_recvfrom(c, query, sizeof query, 0, sa, &salen)) != -1);
1816       memcpy(bin, query, 2); /* Copy ID */
1817     }
1818     n = sres_sendto(c, bin, i, 0, sa, salen);
1819     if (n == -1)
1820       perror("sendto");
1821     while (!poll_sockets(ctx))
1822       ;
1823 
1824     if (ctx->query)
1825       q = NULL;
1826   }
1827 
1828   free(bin);
1829 
1830   END();
1831 }
1832 
1833 static
test_init(sres_context_t * ctx,char const * conf_file)1834 int test_init(sres_context_t *ctx, char const *conf_file)
1835 {
1836   BEGIN();
1837 
1838   sres_resolver_t *res;
1839   int i, n;
1840 
1841   ctx->query = NULL, ctx->result = NULL;
1842 
1843   TEST_1(ctx->resolver = res = sres_resolver_new(conf_file));
1844   TEST(su_home_threadsafe((su_home_t *)ctx->resolver), 0);
1845   n = sres_resolver_sockets(res, NULL, 0);
1846   ctx->n_sockets = n;
1847   TEST_1(n < SRES_MAX_NAMESERVERS);
1848   TEST(sres_resolver_sockets(res, ctx->sockets, n), n);
1849 
1850   for (i = 0; i < n; i++) {
1851     ctx->fds[i].fd = ctx->sockets[i];
1852     ctx->fds[i].events = POLLIN | POLLERR;
1853   }
1854 
1855   TEST_P(sres_resolver_ref(ctx->resolver), ctx->resolver);
1856   sres_resolver_unref(ctx->resolver);
1857 
1858   END();
1859 }
1860 
1861 static
test_deinit(sres_context_t * ctx)1862 int test_deinit(sres_context_t *ctx)
1863 {
1864   offset += 2 * 36000000;
1865 
1866   sres_resolver_timer(ctx->resolver, -1); /* Zap everything */
1867 
1868   sres_free_answers(ctx->resolver, ctx->result); ctx->result = NULL;
1869 
1870   su_free(ctx->home, (void *)ctx->sinkconf); ctx->sinkconf = NULL;
1871 
1872   sres_resolver_unref(ctx->resolver); ctx->resolver = NULL;
1873 
1874   offset = 0;
1875   memset(ctx, 0, sizeof ctx);
1876   ctx->home->suh_size = sizeof ctx;
1877 
1878   return 0;
1879 }
1880 
1881 static
test_conf_errors(sres_context_t * ctx,char const * conf_file)1882 int test_conf_errors(sres_context_t *ctx, char const *conf_file)
1883 {
1884   sres_resolver_t *res;
1885   sres_socket_t socket;
1886   int n;
1887 
1888   BEGIN();
1889 
1890   TEST_1(res = sres_resolver_new(conf_file));
1891   n = sres_resolver_sockets(res, NULL, 0);
1892   TEST_1(n > 0);
1893 
1894   TEST(sres_resolver_sockets(res, &socket, 1), n);
1895 
1896 #if !__linux
1897   /* We fail this test in most systems */
1898   /* conf_file looks like this:
1899 --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--
1900 nameserver 0.0.0.2
1901 nameserver 1.1.1.1.1
1902 search example.com
1903 port $port
1904 -->8-->8-->8-->8-->8-->8-->8-->8-->8-->8-->8-->8--
1905   */
1906   printf("%s:%u: %s test should be updated\n",
1907 	 __FILE__, __LINE__, __func__);
1908 #else
1909   TEST_P(sres_query(res, test_answer, ctx, sres_type_a, "example.com"), NULL);
1910 #endif
1911 
1912   sres_resolver_unref(res);
1913 
1914   END();
1915 }
1916 
1917 void
fill_stack(void)1918 fill_stack(void)
1919 {
1920   int i,array[32768];
1921 
1922   for (i = 0; i < 32768; i++)
1923     array[i] = i ^ 0xdeadbeef;
1924 }
1925 
1926 #if HAVE_ALARM
sig_alarm(int s)1927 static RETSIGTYPE sig_alarm(int s)
1928 {
1929   fprintf(stderr, "%s: FAIL! test timeout!\n", name);
1930   exit(1);
1931 }
1932 #endif
1933 
usage(int exitcode)1934 void usage(int exitcode)
1935 {
1936   fprintf(stderr,
1937 	  "usage: %s OPTIONS [-] [conf-file] [error-conf-file]\n"
1938 	  "\twhere OPTIONS are\n"
1939 	  "\t    -v be verbose\n"
1940 	  "\t    -a abort on error\n"
1941 	  "\t    -l level\n",
1942 	  name);
1943   exit(exitcode);
1944 }
1945 
main(int argc,char ** argv)1946 int main(int argc, char **argv)
1947 {
1948   int i;
1949   int error = 0;
1950   int o_attach = 0, o_alarm = 1;
1951   sres_context_t ctx[1] = {{{SU_HOME_INIT(ctx)}}};
1952 
1953   for (i = 1; argv[i]; i++) {
1954     if (argv[i][0] != '-')
1955       break;
1956     else if (strcmp(argv[i], "-") == 0) {
1957       i++; break;
1958     }
1959     else if (strcmp(argv[i], "-v") == 0)
1960       tstflags |= tst_verbatim;
1961     else if (strcmp(argv[i], "-a") == 0)
1962       tstflags |= tst_abort;
1963     else if (strcmp(argv[i], "--no-alarm") == 0) {
1964       o_alarm = 0;
1965     }
1966     else if (strcmp(argv[i], "--attach") == 0) {
1967       o_attach = 1;
1968     }
1969     else if (strncmp(argv[i], "-l", 2) == 0) {
1970       int level = 3;
1971       char *rest = NULL;
1972 
1973       if (argv[i][2])
1974 	level = strtol(argv[i] + 2, &rest, 10);
1975       else if (argv[i + 1])
1976 	level = strtol(argv[i + 1], &rest, 10), i++;
1977       else
1978 	level = 3, rest = "";
1979 
1980       if (rest == NULL || *rest)
1981 	usage(1);
1982 
1983       su_log_set_level(sresolv_log, level);
1984     } else
1985       usage(1);
1986   }
1987 
1988   if (o_attach) {
1989     char buf[8], *line;
1990 
1991     fprintf(stderr, "test_sresolv: started with pid %u"
1992 	    " (press enter to continue)\n", getpid());
1993 
1994     line = fgets(buf, sizeof buf, stdin); (void) line;
1995   }
1996 #if HAVE_ALARM
1997   else if (o_alarm) {
1998     alarm(60);
1999     signal(SIGALRM, sig_alarm);
2000   }
2001 #endif
2002 
2003   if (!(TSTFLAGS & tst_verbatim)) {
2004     su_log_soft_set_level(sresolv_log, 0);
2005   }
2006 
2007 #if 0
2008   if (sink_make(ctx) == 0)
2009   {
2010     error |= test_init(ctx, ctx->sinkconf);
2011     error |= sink_init(ctx);
2012     error |= test_net(ctx);
2013     error |= test_timeout(ctx);
2014     error |= sink_deinit(ctx);
2015     error |= test_deinit(ctx);
2016   }
2017 #endif
2018 
2019   offset = 0;
2020 
2021   if (argv[i]) {
2022     /* These tests are run with (local) nameserver,
2023      */
2024     int initerror = test_init(ctx, argv[i]);
2025     if (!initerror) {
2026       error |= test_a(ctx);
2027       error |= test_soa(ctx);
2028       error |= test_naptr(ctx);
2029 #if HAVE_SIN6
2030       error |= test_a6(ctx);
2031       error |= test_a6_prefix(ctx);
2032       error |= test_aaaa(ctx);
2033 #endif
2034       error |= test_srv(ctx);
2035       error |= test_cname(ctx);
2036       error |= test_ptr_ipv4(ctx);
2037       error |= test_ptr_ipv4_sockaddr(ctx);
2038 #if HAVE_SIN6
2039       error |= test_ptr_ipv6(ctx);
2040       error |= test_ptr_ipv6_sockaddr(ctx);
2041 #endif
2042       error |= test_cache(ctx);
2043 #if HAVE_SIN6
2044       error |= test_query_one_type(ctx);
2045 #endif
2046       error |= test_expiration(ctx);
2047     }
2048     error |= test_deinit(ctx) | initerror;
2049 
2050     if (argv[i + 1]) {
2051       error |= test_conf_errors(ctx, argv[i + 1]);
2052     }
2053   }
2054 
2055   return error;
2056 }
2057 
2058 
2059 #else /* HAVE_POLL */
2060 
main(int argc,char ** argv)2061 int main(int argc, char **argv)
2062 {
2063   printf("*** Test not supported without POLL API ***\n");
2064   return 0;
2065 }
2066 #endif /* HAVE_POLL */
2067