Debian Codegraph @

tests/test_res_init.c

raw symbols SCIP
1 #include "config.h"
2
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <setjmp.h>
6 #include <cmocka.h>
7
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <stdio.h>
13
14 #include <netinet/in.h>
15 #include <arpa/nameser.h>
16 #include <arpa/inet.h>
17 #include <resolv.h>
18
19 #define RWRAP_RESOLV_CONF_TMPL "rwrap_resolv_conf_XXXXXX"
20
21 struct resolv_conf_test_state {
22 int rc_fd;
23 FILE *resolv_conf;
24 char *resolv_conf_path;
25 };
26
27 static int setup(void **state)
28 {
29 struct resolv_conf_test_state *test_state;
30
31 test_state = malloc(sizeof(struct resolv_conf_test_state));
32 assert_non_null(test_state);
33 test_state->rc_fd = -1;
34 test_state->resolv_conf = NULL;
35
36 test_state->resolv_conf_path = strdup(RWRAP_RESOLV_CONF_TMPL);
37 assert_non_null(test_state->resolv_conf_path);
38 test_state->rc_fd = mkstemp(test_state->resolv_conf_path);
39 assert_return_code(test_state->rc_fd, errno);
40 assert_non_null(test_state->resolv_conf_path);
41 test_state->resolv_conf = fdopen(test_state->rc_fd, "a");
42 assert_non_null(test_state->resolv_conf);
43
44 *state = test_state;
45
46 return 0;
47 }
48
49 static int teardown(void **state)
50 {
51 struct resolv_conf_test_state *test_state;
52
53 test_state = (struct resolv_conf_test_state *) *state;
54
55 if (test_state == NULL) return -1;
56
57 if (test_state->resolv_conf) {
58 fclose(test_state->resolv_conf);
59 }
60
61 if (test_state->rc_fd != -1) {
62 close(test_state->rc_fd);
63 }
64
65 if (test_state->resolv_conf_path) {
66 unlink(test_state->resolv_conf_path);
67 free(test_state->resolv_conf_path);
68 }
69
70 free(test_state);
71
72 return 0;
73 }
74
75 static void test_res_ninit(void **state)
76 {
77 struct resolv_conf_test_state *test_state;
78 struct __res_state dnsstate;
79 /*
80 * libc resolver only supports 3 name servers. Make sure the
81 * extra are skipped for both v4 and v6. Also make sure there's
82 * 'too many' nameservers even on platforms where v6 is not
83 * supported.
84 */
85 const char *nameservers[] = {
86 "127.0.0.1",
87 "10.10.10.1",
88 "fd53:53:53:53::1011",
89 "10.10.10.2",
90 "10.10.10.3",
91 "fd53:53:53:53::1012",
92 NULL,
93 };
94 int i;
95 int rv;
96 char straddr[INET6_ADDRSTRLEN] = { '\0' };
97 #ifdef HAVE_RES_SOCKADDR_UNION_SIN
98 union res_sockaddr_union set[MAXNS*5];
99 #endif
100 #ifdef HAVE_RES_STATE_U_EXT_NSADDRS
101 struct sockaddr_in6 *sa6;
102 #endif
103
104 test_state = (struct resolv_conf_test_state *) *state;
105
106 /*
107 * Write a valid resolv.conf.
108 * Make sure it's possible to skip comments
109 */
110 fputs("# Hello world\n", test_state->resolv_conf);
111 fputs("; This is resolv_wrapper\n", test_state->resolv_conf);
112 for (i = 0; nameservers[i]; i++) {
113 fputs("nameserver ", test_state->resolv_conf);
114 fputs(nameservers[i], test_state->resolv_conf);
115 fputs("\n", test_state->resolv_conf);
116 }
117 fflush(test_state->resolv_conf);
118
119 rv = setenv("RESOLV_WRAPPER_CONF", test_state->resolv_conf_path, 1);
120 assert_int_equal(rv, 0);
121
122 memset(&dnsstate, 0, sizeof(dnsstate));
123 rv = res_ninit(&dnsstate);
124 unsetenv("RESOLV_WRAPPER_CONF");
125 assert_int_equal(rv, 0);
126
127 /*
128 * Validate the number of parsed name servers.
129 *
130 * On platforms that don't support IPv6, the v6 address is skipped
131 * and we end up reading three v4 addresses.
132 *
133 * test we have two v4 and one v6 server
134 *
135 * Note: This test assumes MAXNS == 3, which is the
136 * case on all systems encountered so far.
137 */
138 assert_int_equal(MAXNS, 3);
139 #ifdef HAVE_RES_SOCKADDR_UNION_SIN
140 memset(set, 0, sizeof(set));
141 rv = res_getservers(&dnsstate, set, MAXNS+5);
142 assert_int_equal(rv, MAXNS);
143
144 /* IPv4 */
145 assert_int_equal(set[0].sin.sin_family, AF_INET);
146 assert_int_equal(set[0].sin.sin_port, htons(53));
147 inet_ntop(AF_INET, &(set[0].sin.sin_addr),
148 straddr, INET6_ADDRSTRLEN);
149 assert_string_equal(nameservers[0], straddr);
150
151 assert_int_equal(set[1].sin.sin_family, AF_INET);
152 assert_int_equal(set[1].sin.sin_port, htons(53));
153 inet_ntop(AF_INET, &(set[1].sin.sin_addr), straddr, INET6_ADDRSTRLEN);
154 assert_string_equal(nameservers[1], straddr);
155
156 #ifdef HAVE_RES_SOCKADDR_UNION_SIN6
157 /* IPv6 */
158 assert_int_equal(set[2].sin6.sin6_family, AF_INET6);
159 assert_int_equal(set[2].sin6.sin6_port, htons(53));
160 inet_ntop(AF_INET6, &(set[2].sin6.sin6_addr), straddr, INET6_ADDRSTRLEN);
161 assert_string_equal(nameservers[2], straddr);
162 #else /* ! HAVE_RES_SOCKADDR_UNION_SIN6 */
163 /*
164 * On platforms that don't support IPv6, the v6 address is skipped
165 * and we end up reading three v4 addresses.
166 */
167 assert_int_equal(set[2].sin.sin_family, AF_INET);
168 assert_int_equal(set[2].sin.sin_port, htons(53));
169 inet_ntop(AF_INET, &(set[2].sin.sin_addr), straddr, INET6_ADDRSTRLEN);
170 assert_string_equal(nameservers[3], straddr);
171 #endif /* ! HAVE_RES_SOCKADDR_UNION_SIN6 */
172
173 #else /* ! HAVE_RES_SOCKADDR_UNION_SIN */
174 assert_int_equal(dnsstate.nscount, MAXNS);
175
176 /* Validate the servers. */
177
178 /* IPv4 */
179 assert_int_equal(dnsstate.nsaddr_list[0].sin_family, AF_INET);
180 assert_int_equal(dnsstate.nsaddr_list[0].sin_port, htons(53));
181 inet_ntop(AF_INET, &(dnsstate.nsaddr_list[0].sin_addr),
182 straddr, INET6_ADDRSTRLEN);
183 assert_string_equal(nameservers[0], straddr);
184 #ifdef HAVE_RES_STATE_U_EXT_NSADDRS
185 assert_null(dnsstate._u._ext.nsaddrs[0]);
186 #endif
187
188 assert_int_equal(dnsstate.nsaddr_list[1].sin_family, AF_INET);
189 assert_int_equal(dnsstate.nsaddr_list[1].sin_port, htons(53));
190 inet_ntop(AF_INET, &(dnsstate.nsaddr_list[1].sin_addr),
191 straddr, INET6_ADDRSTRLEN);
192 assert_string_equal(nameservers[1], straddr);
193 #ifdef HAVE_RES_STATE_U_EXT_NSADDRS
194 assert_null(dnsstate._u._ext.nsaddrs[1]);
195 #endif
196
197 #ifdef HAVE_RES_STATE_U_EXT_NSADDRS
198 /* IPv6 */
199 assert_non_null(dnsstate._u._ext.nsaddrs[2]);
200 sa6 = dnsstate._u._ext.nsaddrs[2];
201 assert_int_equal(sa6->sin6_family, AF_INET6);
202 assert_int_equal(sa6->sin6_port, htons(53));
203 inet_ntop(AF_INET6, &(sa6->sin6_addr), straddr, INET6_ADDRSTRLEN);
204 assert_string_equal(nameservers[2], straddr);
205 #else
206 /*
207 * On platforms that don't support IPv6, the v6 address is skipped
208 * and we end up reading three v4 addresses.
209 */
210 assert_int_equal(dnsstate.nsaddr_list[2].sin_family, AF_INET);
211 assert_int_equal(dnsstate.nsaddr_list[2].sin_port, htons(53));
212 inet_ntop(AF_INET, &(dnsstate.nsaddr_list[2].sin_addr),
213 straddr, INET6_ADDRSTRLEN);
214 assert_string_equal(nameservers[3], straddr);
215 #endif
216
217 #endif /* ! HAVE_RES_SOCKADDR_UNION_SIN */
218
219 res_nclose(&dnsstate);
220 }
221
222 static void test_res_ninit_enoent(void **state)
223 {
224 int rv;
225 struct __res_state dnsstate;
226
227 (void) state; /* unused */
228
229 rv = setenv("RESOLV_WRAPPER_CONF", "/no/such/file", 1);
230 assert_int_equal(rv, 0);
231
232 /* Just make sure we don't crash, error is fine */
233 memset(&dnsstate, 0, sizeof(dnsstate));
234 rv = res_ninit(&dnsstate);
235 unsetenv("RESOLV_WRAPPER_CONF");
236 assert_int_equal(rv, -1);
237 }
238
239 int main(void) {
240 int rc;
241
242 const struct CMUnitTest init_tests[] = {
243 cmocka_unit_test_setup_teardown(test_res_ninit, setup, teardown),
244 cmocka_unit_test(test_res_ninit_enoent),
245 };
246
247 rc = cmocka_run_group_tests(init_tests, NULL, NULL);
248
249 return rc;
250 }