2 * Copyright (C) Jakub Hrozek 2014 <jakub.hrozek@gmail.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
46 #include <netinet/in.h>
47 #include <arpa/nameser.h>
48 #include <arpa/inet.h>
53 static void test_res_fake_a_query(void **state)
56 struct __res_state dnsstate;
57 unsigned char answer[ANSIZE];
58 char addr[INET_ADDRSTRLEN];
60 ns_rr rr; /* expanded resource record */
62 (void) state; /* unused */
64 memset(&dnsstate, 0, sizeof(struct __res_state));
65 rv = res_ninit(&dnsstate);
66 assert_int_equal(rv, 0);
68 rv = res_nquery(&dnsstate, "cwrap.org", ns_c_in, ns_t_a,
70 assert_int_not_equal(rv, -1);
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
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");
85 static void test_res_fake_a_query_notfound(void **state)
88 struct __res_state dnsstate;
89 unsigned char answer[ANSIZE];
92 (void) state; /* unused */
94 memset(&dnsstate, 0, sizeof(struct __res_state));
95 rv = res_ninit(&dnsstate);
96 assert_int_equal(rv, 0);
98 rv = res_nquery(&dnsstate, "nosuchentry.org", ns_c_in, ns_t_a,
100 assert_int_not_equal(rv, -1);
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);
108 static void test_res_fake_aaaa_query(void **state)
111 struct __res_state dnsstate;
112 unsigned char answer[ANSIZE];
113 char addr[INET6_ADDRSTRLEN];
115 ns_rr rr; /* expanded resource record */
117 (void) state; /* unused */
119 memset(&dnsstate, 0, sizeof(struct __res_state));
120 rv = res_ninit(&dnsstate);
121 assert_int_equal(rv, 0);
123 rv = res_nquery(&dnsstate, "cwrap6.org", ns_c_in, ns_t_aaaa,
125 assert_int_not_equal(rv, -1);
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
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");
140 static void test_res_fake_aaaa_query_notfound(void **state)
143 struct __res_state dnsstate;
144 unsigned char answer[ANSIZE];
147 (void) state; /* unused */
149 memset(&dnsstate, 0, sizeof(struct __res_state));
150 rv = res_ninit(&dnsstate);
151 assert_int_equal(rv, 0);
153 rv = res_nquery(&dnsstate, "nosuchentry.org", ns_c_in, ns_t_aaaa,
155 assert_int_not_equal(rv, -1);
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);
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),
174 rc = run_tests(tests);