Skip to content

Commit 113c211

Browse files
jhrozekcryptomilk
authored andcommitted
tests: Add tests for res_query and res_search
Signed-off-by: Jakub Hrozek <jakub.hrozek@posteo.se> Reviewed-by: Andreas Schneider <asn@samba.org> Reviewed-by: Stefan Metzmacher <metze@samba.org>
1 parent c78b81b commit 113c211

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

tests/test_res_query_search.c

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int teardown(void **state)
6666
return 0;
6767
}
6868

69-
static void test_res_query(void **state)
69+
static void test_res_nquery(void **state)
7070
{
7171
int rv;
7272
struct __res_state dnsstate;
@@ -102,7 +102,36 @@ static void test_res_query(void **state)
102102
res_nclose(&dnsstate);
103103
}
104104

105-
static void test_res_search(void **state)
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+
134+
static void test_res_nsearch(void **state)
106135
{
107136
int rv;
108137
struct __res_state dnsstate;
@@ -137,14 +166,48 @@ static void test_res_search(void **state)
137166
res_nclose(&dnsstate);
138167
}
139168

169+
static void test_res_search(void **state)
170+
{
171+
int rv;
172+
unsigned char answer[ANSIZE];
173+
char addr[INET_ADDRSTRLEN];
174+
ns_msg handle;
175+
ns_rr rr; /* expanded resource record */
176+
177+
(void) state; /* unused */
178+
179+
rv = res_search("www.cwrap.org", ns_c_in, ns_t_a,
180+
answer, sizeof(answer));
181+
assert_int_not_equal(rv, -1);
182+
183+
ns_initparse(answer, sizeof(answer), &handle);
184+
/* The query must finish w/o an error, have one answer and the answer
185+
* must be a parseable RR of type A and have the address that our
186+
* test server sends
187+
*/
188+
assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
189+
assert_int_equal(ns_msg_count(handle, ns_s_an), 1);
190+
assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0);
191+
assert_int_equal(ns_rr_type(rr), ns_t_a);
192+
assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr),
193+
addr, sizeof(addr)));
194+
assert_string_equal(addr, "127.0.10.10");
195+
}
196+
140197
int main(void)
141198
{
142199
int rc;
143200

144201
const struct CMUnitTest res_tests[] = {
202+
cmocka_unit_test_setup_teardown(test_res_nquery,
203+
setup_dns_srv_ipv4,
204+
teardown),
145205
cmocka_unit_test_setup_teardown(test_res_query,
146206
setup_dns_srv_ipv4,
147207
teardown),
208+
cmocka_unit_test_setup_teardown(test_res_nsearch,
209+
setup_dns_srv_ipv4,
210+
teardown),
148211
cmocka_unit_test_setup_teardown(test_res_search,
149212
setup_dns_srv_ipv4,
150213
teardown),

0 commit comments

Comments
 (0)