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
47 #include <netinet/in.h>
48 #include <arpa/nameser.h>
49 #include <arpa/inet.h>
54 static void setup_dns_srv_ipv4(void **state)
56 torture_setup_dns_srv_ipv4(state);
57 setenv("RESOLV_WRAPPER_CONF", torture_server_resolv_conf(state), 1);
60 static void teardown(void **state)
62 torture_teardown_dns_srv(state);
65 static void test_res_query(void **state)
68 struct __res_state dnsstate;
69 unsigned char answer[ANSIZE];
70 char addr[INET_ADDRSTRLEN];
72 ns_rr rr; /* expanded resource record */
74 (void) state; /* unused */
76 memset(&dnsstate, 0, sizeof(struct __res_state));
77 rv = res_ninit(&dnsstate);
78 assert_int_equal(rv, 0);
80 rv = res_nquery(&dnsstate, "www.cwrap.org", ns_c_in, ns_t_a,
82 assert_int_not_equal(rv, -1);
84 ns_initparse(answer, 256, &handle);
86 * The query must finish w/o an error, have one answer and the answer
87 * must be a parseable RR of type A and have the address that our
90 assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
91 assert_int_equal(ns_msg_count(handle, ns_s_an), 1);
92 assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0);
93 assert_int_equal(ns_rr_type(rr), ns_t_a);
94 assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr), addr, 256));
95 assert_string_equal(addr, "127.0.10.10");
97 res_nclose(&dnsstate);
100 static void test_res_search(void **state)
103 struct __res_state dnsstate;
104 unsigned char answer[ANSIZE];
105 char addr[INET_ADDRSTRLEN];
107 ns_rr rr; /* expanded resource record */
109 (void) state; /* unused */
111 memset(&dnsstate, 0, sizeof(struct __res_state));
112 rv = res_ninit(&dnsstate);
113 assert_int_equal(rv, 0);
115 rv = res_nsearch(&dnsstate, "www.cwrap.org", ns_c_in, ns_t_a,
117 assert_int_not_equal(rv, -1);
119 ns_initparse(answer, 256, &handle);
120 /* The query must finish w/o an error, have one answer and the answer
121 * must be a parseable RR of type A and have the address that our
124 assert_int_equal(ns_msg_getflag(handle, ns_f_rcode), ns_r_noerror);
125 assert_int_equal(ns_msg_count(handle, ns_s_an), 1);
126 assert_int_equal(ns_parserr(&handle, ns_s_an, 0, &rr), 0);
127 assert_int_equal(ns_rr_type(rr), ns_t_a);
128 assert_non_null(inet_ntop(AF_INET, ns_rr_rdata(rr), addr, 256));
129 assert_string_equal(addr, "127.0.10.10");
131 res_nclose(&dnsstate);
138 const UnitTest tests[] = {
139 unit_test_setup_teardown(test_res_query,
142 unit_test_setup_teardown(test_res_search,
147 rc = run_tests(tests);