Debian Codegraph @

tests/test_gethostby_name_addr.c

raw
1 #include "config.h"
2
3 #include <pthread.h>
4
5 #include <stdarg.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <setjmp.h>
9 #include <cmocka.h>
10
11 #include <errno.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <netdb.h>
21
22 #define NSS_WRAPPER_HOSTNAME_ENV "NSS_WRAPPER_HOSTNAME"
23
24 static void test_nwrap_gethostname(void **state)
25 {
26 const char *hostname = "milliways";
27 char sys_host[256] = {0};
28 char host[16] = {0};
29 int rc;
30
31 (void) state; /* unused */
32
33 rc = setenv(NSS_WRAPPER_HOSTNAME_ENV, hostname, 1);
34 assert_int_equal(rc, 0);
35
36 rc = gethostname(host, sizeof(host));
37 assert_int_equal(rc, 0);
38
39 assert_string_equal(host, hostname);
40
41 rc = setenv(NSS_WRAPPER_HOSTNAME_ENV, "this_hostname_is_too_long", 1);
42 assert_int_equal(rc, 0);
43
44 rc = gethostname(host, sizeof(host));
45 assert_int_equal(rc, -1);
46 assert_int_equal(errno, ENAMETOOLONG);
47
48 unsetenv(NSS_WRAPPER_HOSTNAME_ENV);
49
50 rc = gethostname(sys_host, sizeof(sys_host));
51 assert_int_equal(rc, 0);
52
53 }
54
55 static void *thread_test_gethostbyname(void *u)
56 {
57 struct hostent *he;
58
59 (void) u; /* unused */
60
61 he = gethostbyname("magrathea");
62 assert_non_null(he);
63 assert_non_null(he->h_name);
64 assert_string_equal(he->h_name, "magrathea.galaxy.site");
65 pthread_exit(NULL);
66 }
67
68 static void test_nwrap_gethostbyname_thread(void **state)
69 {
70 struct hostent *he;
71 pthread_t th;
72
73 (void) state; /* unused */
74
75 he = gethostbyname("maximegalon.galaxy.site");
76 assert_non_null(he);
77 assert_non_null(he->h_name);
78 assert_string_equal(he->h_name, "maximegalon.galaxy.site");
79
80 pthread_create(&th, NULL, &thread_test_gethostbyname, NULL);
81 pthread_join(th, NULL);
82
83 assert_non_null(he);
84 assert_non_null(he->h_name);
85 #ifdef BSD
86 /*
87 * On *BSD (and Mac OS X) systems,
88 * data is stored in thread local storage.
89 */
90 assert_string_equal(he->h_name, "maximegalon.galaxy.site");
91 #else
92 /*
93 * Glibc doesn't store data in thread local storage, so calling
94 * gethostbyname from a thread overwrites the parent thread's data.
95 */
96 assert_string_equal(he->h_name, "magrathea.galaxy.site");
97 #endif
98 }
99
100 static void test_nwrap_gethostbyname(void **state)
101 {
102 char ip[INET_ADDRSTRLEN];
103 struct hostent *he;
104 const char *a;
105
106 (void) state; /* unused */
107
108 he = gethostbyname("magrathea.galaxy.site");
109 assert_non_null(he);
110 assert_non_null(he->h_name);
111 assert_non_null(he->h_addr_list);
112
113 assert_string_equal(he->h_name, "magrathea.galaxy.site");
114 assert_int_equal(he->h_addrtype, AF_INET);
115
116 a = inet_ntop(AF_INET, he->h_addr_list[0], ip, sizeof(ip));
117 assert_non_null(a);
118
119 assert_string_equal(ip, "127.0.0.11");
120 }
121
122 static void test_nwrap_gethostbyname_multiple(void **state)
123 {
124 struct hostent *he;
125 char **list;
126
127 /* For inet_ntop call */
128 char buf[4096];
129 const char *result;
130 char *p = buf;
131
132 /* List of ips in hosts file - order matters */
133 const char *const result_ips[] = { "127.0.0.11", "127.0.0.12", NULL };
134 const char *actual_ip = result_ips[0];
135 unsigned int ac;
136
137 (void) state; /* unused */
138
139 he = gethostbyname("magrathea.galaxy.site");
140 assert_non_null(he);
141 assert_non_null(he->h_name);
142 assert_non_null(he->h_addr_list);
143
144 list = he->h_addr_list;
145 for (ac = 0; *list != NULL; ++ac, ++list) {
146 actual_ip = result_ips[ac];
147 /* When test fails here more records are returned */
148 assert_non_null(actual_ip);
149 result = inet_ntop(AF_INET, *list, p, 4096);
150 assert_non_null(p);
151 assert_string_equal(actual_ip, result);
152 }
153 }
154
155 #ifdef HAVE_GETHOSTBYNAME2
156 static void test_nwrap_gethostbyname2(void **state)
157 {
158 char ip[INET6_ADDRSTRLEN];
159 struct hostent *he;
160 const char *a;
161
162 (void) state; /* unused */
163
164 he = gethostbyname2("magrathea.galaxy.site", AF_INET6);
165 assert_non_null(he);
166
167 he = gethostbyname2("magrathea.galaxy.site", AF_INET);
168 assert_non_null(he);
169
170 /* Check ipv6 he */
171 he = gethostbyname2("krikkit.galaxy.site", AF_INET6);
172 assert_non_null(he);
173 assert_non_null(he->h_name);
174 assert_non_null(he->h_addr_list);
175
176 assert_string_equal(he->h_name, "krikkit.galaxy.site");
177 assert_int_equal(he->h_addrtype, AF_INET6);
178
179 a = inet_ntop(AF_INET6, he->h_addr_list[0], ip, sizeof(ip));
180 assert_non_null(a);
181
182 assert_string_equal(ip, "::14");
183
184 /* Check ipv4 he */
185 he = gethostbyname2("krikkit.galaxy.site", AF_INET);
186 assert_non_null(he);
187 assert_non_null(he->h_name);
188 assert_non_null(he->h_addr_list);
189
190 assert_string_equal(he->h_name, "krikkit.galaxy.site");
191 assert_int_equal(he->h_addrtype, AF_INET);
192
193 a = inet_ntop(AF_INET, he->h_addr_list[0], ip, sizeof(ip));
194 assert_non_null(a);
195
196 assert_string_equal(ip, "127.0.0.14");
197 }
198 #endif /* HAVE_GETHOSTBYNAME2 */
199
200 static void test_nwrap_gethostbyaddr(void **state)
201 {
202 struct hostent *he;
203 struct in_addr in;
204 int rc;
205
206 (void) state; /* unused */
207
208 rc = inet_aton("127.0.0.11", &in);
209 assert_int_equal(rc, 1);
210
211 he = gethostbyaddr(&in, sizeof(struct in_addr), AF_INET);
212 assert_non_null(he);
213 assert_non_null(he->h_name);
214 assert_non_null(he->h_addr_list);
215
216 assert_string_equal(he->h_name, "magrathea.galaxy.site");
217 assert_int_equal(he->h_addrtype, AF_INET);
218 assert_memory_equal(&in, he->h_addr_list[0], he->h_length);
219 }
220
221 #ifdef HAVE_GETHOSTBYNAME_R
222 static void test_nwrap_gethostbyname_r(void **state)
223 {
224 char buf[1024] = {0};
225 char ip[INET_ADDRSTRLEN];
226 struct hostent hb, *he;
227 const char *a;
228 int herr = 0;
229 int rc;
230
231 (void) state; /* unused */
232
233 rc = gethostbyname_r("magrathea.galaxy.site",
234 &hb,
235 buf, sizeof(buf),
236 &he,
237 &herr);
238 assert_int_equal(rc, 0);
239 assert_non_null(he);
240 assert_non_null(he->h_name);
241 assert_non_null(he->h_addr_list);
242
243 assert_string_equal(he->h_name, "magrathea.galaxy.site");
244 assert_int_equal(he->h_addrtype, AF_INET);
245
246 a = inet_ntop(AF_INET, he->h_addr_list[0], ip, sizeof(ip));
247 assert_non_null(a);
248
249 assert_string_equal(ip, "127.0.0.11");
250 }
251
252 static void test_nwrap_gethostbyname_r_null(void **state)
253 {
254 char buf[2];
255 struct hostent hb, *he;
256 int herr = 0;
257 int rc;
258
259 (void) state; /* unused */
260
261 buf[0] = 'A';
262 buf[1] = '\0';
263
264 /* Check that the returned buffer is zeroed */
265 rc = gethostbyname_r("wurst",
266 &hb,
267 buf, sizeof(buf),
268 &he,
269 &herr);
270 assert_int_equal(rc, ENOENT);
271 assert_null(he);
272 assert_null(hb.h_name);
273 assert_null(hb.h_addr_list);
274 assert_string_equal(buf, "");
275 }
276 #endif
277
278 #ifdef HAVE_GETHOSTBYNAME2_R
279 static void test_nwrap_gethostbyname2_r_v4(void **state)
280 {
281 char *buf = NULL;
282 size_t buflen = 2;
283 char ip[INET_ADDRSTRLEN];
284 struct hostent hb, *he;
285 const char *a;
286 int herr = 0;
287 int rc;
288
289 (void) state; /* unused */
290
291 for (rc = ERANGE, buflen = 2; rc == ERANGE; buflen *= 2) {
292 if (buf != NULL) {
293 free(buf);
294 }
295 buf = calloc(1, buflen);
296 assert_non_null(buf);
297
298 rc = gethostbyname2_r("magrathea.galaxy.site", AF_INET,
299 &hb,
300 buf, buflen,
301 &he,
302 &herr);
303 }
304
305 assert_int_equal(rc, 0);
306 assert_int_equal(herr, 0);
307 assert_non_null(he);
308 assert_non_null(he->h_name);
309 assert_non_null(he->h_addr_list);
310
311 assert_string_equal(he->h_name, "magrathea.galaxy.site");
312 assert_int_equal(he->h_addrtype, AF_INET);
313
314 assert_non_null(he->h_addr_list[0]);
315 a = inet_ntop(AF_INET, he->h_addr_list[0], ip, sizeof(ip));
316 assert_non_null(a);
317 assert_string_equal(a, "127.0.0.11");
318
319 assert_non_null(he->h_addr_list[1]);
320 a = inet_ntop(AF_INET, he->h_addr_list[1], ip, sizeof(ip));
321 assert_non_null(a);
322 assert_string_equal(a, "127.0.0.12");
323
324 assert_null(he->h_addr_list[2]);
325
326 free(buf);
327 }
328
329 static void test_nwrap_gethostbyname2_r_v6(void **state)
330 {
331 char *buf = NULL;
332 size_t buflen = 2;
333 char ip[INET6_ADDRSTRLEN];
334 struct hostent hb, *he;
335 const char *a;
336 int herr = 0;
337 int rc;
338
339 (void) state; /* unused */
340
341 for (rc = ERANGE, buflen = 2; rc == ERANGE; buflen *= 2) {
342 if (buf != NULL) {
343 free(buf);
344 }
345 buf = calloc(1, buflen);
346 assert_non_null(buf);
347
348 rc = gethostbyname2_r("magrathea.galaxy.site", AF_INET6,
349 &hb,
350 buf, buflen,
351 &he,
352 &herr);
353 }
354
355 assert_int_equal(rc, 0);
356 assert_int_equal(herr, 0);
357 assert_non_null(he);
358 assert_non_null(he->h_name);
359 assert_non_null(he->h_addr_list);
360
361 assert_string_equal(he->h_name, "magrathea.galaxy.site");
362 assert_int_equal(he->h_addrtype, AF_INET6);
363
364 assert_non_null(he->h_addr_list[0]);
365 a = inet_ntop(AF_INET6, he->h_addr_list[0], ip, sizeof(ip));
366 assert_non_null(a);
367 assert_string_equal(a, "::29a");
368
369 assert_null(he->h_addr_list[1]);
370
371 free(buf);
372 }
373 #endif
374
375 #ifdef HAVE_GETHOSTBYADDR_R
376 static void test_nwrap_gethostbyaddr_r(void **state)
377 {
378 char buf[1024] = {0};
379 struct hostent hb, *he;
380 struct in_addr in;
381 int herr = 0;
382 int rc;
383
384 (void) state; /* unused */
385
386 rc = inet_aton("127.0.0.11", &in);
387 assert_int_equal(rc, 1);
388
389 rc = gethostbyaddr_r(&in, sizeof(struct in_addr),
390 AF_INET,
391 &hb,
392 buf, sizeof(buf),
393 &he,
394 &herr);
395 assert_int_equal(rc, 0);
396 assert_non_null(he);
397 assert_non_null(he->h_name);
398 assert_non_null(he->h_addr_list);
399
400 assert_string_equal(he->h_name, "magrathea.galaxy.site");
401 assert_int_equal(he->h_addrtype, AF_INET);
402 assert_memory_equal(&in, he->h_addr_list[0], he->h_length);
403 }
404 #endif
405
406 int main(void) {
407 int rc;
408
409 const struct CMUnitTest tests[] = {
410 cmocka_unit_test(test_nwrap_gethostname),
411 cmocka_unit_test(test_nwrap_gethostbyname),
412 cmocka_unit_test(test_nwrap_gethostbyname_thread),
413 #ifdef HAVE_GETHOSTBYNAME2
414 cmocka_unit_test(test_nwrap_gethostbyname2),
415 #endif
416 cmocka_unit_test(test_nwrap_gethostbyaddr),
417 #ifdef HAVE_GETHOSTBYNAME_R
418 cmocka_unit_test(test_nwrap_gethostbyname_r),
419 cmocka_unit_test(test_nwrap_gethostbyname_r_null),
420 #endif
421 #ifdef HAVE_GETHOSTBYNAME2_R
422 cmocka_unit_test(test_nwrap_gethostbyname2_r_v4),
423 cmocka_unit_test(test_nwrap_gethostbyname2_r_v6),
424 #endif
425 #ifdef HAVE_GETHOSTBYADDR_R
426 cmocka_unit_test(test_nwrap_gethostbyaddr_r),
427 #endif
428 cmocka_unit_test(test_nwrap_gethostbyname_multiple),
429 };
430
431 rc = cmocka_run_group_tests(tests, NULL, NULL);
432
433 return rc;
434 }
435