resolv: Implement faking A and AAAA DNS replies
[obnox/cwrap/resolv_wrapper.git] / tests / test_dns_fake.c
1 /*
2  * Copyright (C) Jakub Hrozek 2014 <jakub.hrozek@gmail.com>
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the author nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include <stdarg.h>
35 #include <stddef.h>
36 #include <setjmp.h>
37 #include <cmocka.h>
38
39 #include "config.h"
40
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <stdio.h>
45
46 #include <netinet/in.h>
47 #include <arpa/nameser.h>
48 #include <arpa/inet.h>
49 #include <resolv.h>
50
51 #define ANSIZE 256
52
53 static void test_res_fake_a_query(void **state)
54 {
55         int rv;
56         struct __res_state dnsstate;
57         unsigned char answer[ANSIZE];
58         char addr[INET_ADDRSTRLEN];
59         ns_msg handle;
60         ns_rr rr;   /* expanded resource record */
61
62         (void) state; /* unused */
63
64         memset(&dnsstate, 0, sizeof(struct __res_state));
65         rv = res_ninit(&dnsstate);
66         assert_int_equal(rv, 0);
67
68         rv = res_nquery(&dnsstate, "cwrap.org", ns_c_in, ns_t_a,
69                         answer, ANSIZE);
70         assert_int_not_equal(rv, -1);
71
72         ns_initparse(answer, 256, &handle);
73         /* The query must finish w/o an error, have one answer and the answer
74          * must be a parseable RR of type A and have the address that our
75          * fake hosts file contains
76          */
77         assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
78         assert_int_equal(ns_msg_count(handle, ns_s_an), 1);
79         assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0);
80         assert_int_equal(ns_rr_type(rr), ns_t_a);
81         assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr), addr, 256));
82         assert_string_equal(addr, "127.0.0.21");
83 }
84
85 static void test_res_fake_a_query_notfound(void **state)
86 {
87         int rv;
88         struct __res_state dnsstate;
89         unsigned char answer[ANSIZE];
90         ns_msg handle;
91
92         (void) state; /* unused */
93
94         memset(&dnsstate, 0, sizeof(struct __res_state));
95         rv = res_ninit(&dnsstate);
96         assert_int_equal(rv, 0);
97
98         rv = res_nquery(&dnsstate, "nosuchentry.org", ns_c_in, ns_t_a,
99                         answer, ANSIZE);
100         assert_int_not_equal(rv, -1);
101
102         ns_initparse(answer, 256, &handle);
103         /* The query must finish w/o an error and have no answer */
104         assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
105         assert_int_equal(ns_msg_count(handle, ns_s_an), 0);
106 }
107
108 static void test_res_fake_aaaa_query(void **state)
109 {
110         int rv;
111         struct __res_state dnsstate;
112         unsigned char answer[ANSIZE];
113         char addr[INET6_ADDRSTRLEN];
114         ns_msg handle;
115         ns_rr rr;   /* expanded resource record */
116
117         (void) state; /* unused */
118
119         memset(&dnsstate, 0, sizeof(struct __res_state));
120         rv = res_ninit(&dnsstate);
121         assert_int_equal(rv, 0);
122
123         rv = res_nquery(&dnsstate, "cwrap6.org", ns_c_in, ns_t_aaaa,
124                         answer, ANSIZE);
125         assert_int_not_equal(rv, -1);
126
127         ns_initparse(answer, 256, &handle);
128         /* The query must finish w/o an error, have one answer and the answer
129          * must be a parseable RR of type AAAA and have the address that our
130          * fake hosts file contains
131          */
132         assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
133         assert_int_equal(ns_msg_count(handle, ns_s_an), 1);
134         assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0);
135         assert_int_equal(ns_rr_type(rr), ns_t_aaaa);
136         assert_non_null(inet_ntop(AF_INET6, ns_rr_rdata(rr), addr, 256));
137         assert_string_equal(addr, "2a00:1450:4013:c01::63");
138 }
139
140 static void test_res_fake_aaaa_query_notfound(void **state)
141 {
142         int rv;
143         struct __res_state dnsstate;
144         unsigned char answer[ANSIZE];
145         ns_msg handle;
146
147         (void) state; /* unused */
148
149         memset(&dnsstate, 0, sizeof(struct __res_state));
150         rv = res_ninit(&dnsstate);
151         assert_int_equal(rv, 0);
152
153         rv = res_nquery(&dnsstate, "nosuchentry.org", ns_c_in, ns_t_aaaa,
154                         answer, ANSIZE);
155         assert_int_not_equal(rv, -1);
156
157         ns_initparse(answer, 256, &handle);
158         /* The query must finish w/o an error and have no answer */
159         assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
160         assert_int_equal(ns_msg_count(handle, ns_s_an), 0);
161 }
162
163 int main(void)
164 {
165         int rc;
166
167         const UnitTest tests[] = {
168                 unit_test(test_res_fake_a_query),
169                 unit_test(test_res_fake_a_query_notfound),
170                 unit_test(test_res_fake_aaaa_query),
171                 unit_test(test_res_fake_aaaa_query_notfound),
172         };
173
174         rc = run_tests(tests);
175
176         return rc;
177 }