Debian Codegraph @

tests/test_res_query_search.c

raw symbols SCIP
1 /*
2 * Copyright (C) Jakub Hrozek 2014 <[email protected]>
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 #include "torture.h"
41
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <stdio.h>
46
47 #include <netinet/in.h>
48 #include <arpa/nameser.h>
49 #include <arpa/inet.h>
50 #include <resolv.h>
51
52 #define ANSIZE 256
53
54 static int setup_dns_srv_ipv4(void **state)
55 {
56 torture_setup_dns_srv_ipv4(state);
57 setenv("RESOLV_WRAPPER_CONF", torture_server_resolv_conf(state), 1);
58
59 return 0;
60 }
61
62 static int teardown(void **state)
63 {
64 torture_teardown_dns_srv(state);
65
66 return 0;
67 }
68
69 static void test_res_nquery(void **state)
70 {
71 int rv;
72 struct __res_state dnsstate;
73 unsigned char answer[ANSIZE];
74 char addr[INET_ADDRSTRLEN];
75 ns_msg handle;
76 ns_rr rr; /* expanded resource record */
77
78 (void) state; /* unused */
79
80 memset(&dnsstate, 0, sizeof(struct __res_state));
81 rv = res_ninit(&dnsstate);
82 assert_int_equal(rv, 0);
83
84 rv = res_nquery(&dnsstate, "www.cwrap.org", ns_c_in, ns_t_a,
85 answer, sizeof(answer));
86 assert_int_not_equal(rv, -1);
87
88 ns_initparse(answer, sizeof(answer), &handle);
89 /*
90 * The query must finish w/o an error, have one answer and the answer
91 * must be a parseable RR of type A and have the address that our
92 * test server sends.
93 */
94 assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
95 assert_int_equal(ns_msg_count(handle, ns_s_an), 1);
96 assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0);
97 assert_int_equal(ns_rr_type(rr), ns_t_a);
98 assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr),
99 addr, sizeof(addr)));
100 assert_string_equal(addr, "127.0.10.10");
101
102 res_nclose(&dnsstate);
103 }
104
105 static void test_res_query(void **state)
106 {
107 int rv;
108 unsigned char answer[ANSIZE];
109 char addr[INET_ADDRSTRLEN];
110 ns_msg handle;
111 ns_rr rr; /* expanded resource record */
112
113 (void) state; /* unused */
114
115 rv = res_query("www.cwrap.org", ns_c_in, ns_t_a,
116 answer, sizeof(answer));
117 assert_int_not_equal(rv, -1);
118
119 ns_initparse(answer, sizeof(answer), &handle);
120 /*
121 * The query must finish w/o an error, have one answer and the answer
122 * must be a parseable RR of type A and have the address that our
123 * test server sends.
124 */
125 assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
126 assert_int_equal(ns_msg_count(handle, ns_s_an), 1);
127 assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0);
128 assert_int_equal(ns_rr_type(rr), ns_t_a);
129 assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr),
130 addr, sizeof(addr)));
131 assert_string_equal(addr, "127.0.10.10");
132
133 res_close();
134 }
135
136 static void test_res_nsearch(void **state)
137 {
138 int rv;
139 struct __res_state dnsstate;
140 unsigned char answer[ANSIZE];
141 char addr[INET_ADDRSTRLEN];
142 ns_msg handle;
143 ns_rr rr; /* expanded resource record */
144
145 (void) state; /* unused */
146
147 memset(&dnsstate, 0, sizeof(struct __res_state));
148 rv = res_ninit(&dnsstate);
149 assert_int_equal(rv, 0);
150
151 rv = res_nsearch(&dnsstate, "www.cwrap.org", ns_c_in, ns_t_a,
152 answer, sizeof(answer));
153 assert_int_not_equal(rv, -1);
154
155 ns_initparse(answer, sizeof(answer), &handle);
156 /* The query must finish w/o an error, have one answer and the answer
157 * must be a parseable RR of type A and have the address that our
158 * test server sends
159 */
160 assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
161 assert_int_equal(ns_msg_count(handle, ns_s_an), 1);
162 assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0);
163 assert_int_equal(ns_rr_type(rr), ns_t_a);
164 assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr),
165 addr, sizeof(addr)));
166 assert_string_equal(addr, "127.0.10.10");
167
168 res_nclose(&dnsstate);
169 }
170
171 static void test_res_search(void **state)
172 {
173 int rv;
174 unsigned char answer[ANSIZE];
175 char addr[INET_ADDRSTRLEN];
176 ns_msg handle;
177 ns_rr rr; /* expanded resource record */
178
179 (void) state; /* unused */
180
181 rv = res_search("www.cwrap.org", ns_c_in, ns_t_a,
182 answer, sizeof(answer));
183 assert_int_not_equal(rv, -1);
184
185 ns_initparse(answer, sizeof(answer), &handle);
186 /* The query must finish w/o an error, have one answer and the answer
187 * must be a parseable RR of type A and have the address that our
188 * test server sends
189 */
190 assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
191 assert_int_equal(ns_msg_count(handle, ns_s_an), 1);
192 assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0);
193 assert_int_equal(ns_rr_type(rr), ns_t_a);
194 assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr),
195 addr, sizeof(addr)));
196 assert_string_equal(addr, "127.0.10.10");
197
198 res_close();
199 }
200
201 int main(void)
202 {
203 int rc;
204
205 const struct CMUnitTest res_tests[] = {
206 cmocka_unit_test_setup_teardown(test_res_nquery,
207 setup_dns_srv_ipv4,
208 teardown),
209 cmocka_unit_test_setup_teardown(test_res_query,
210 setup_dns_srv_ipv4,
211 teardown),
212 cmocka_unit_test_setup_teardown(test_res_nsearch,
213 setup_dns_srv_ipv4,
214 teardown),
215 cmocka_unit_test_setup_teardown(test_res_search,
216 setup_dns_srv_ipv4,
217 teardown),
218 };
219
220 rc = cmocka_run_group_tests(res_tests, NULL, NULL);
221
222 return rc;
223 }