]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/test-resolved-etc-hosts.c
test-resolved-etc-hosts: when parsing an external file, skip other tests
[thirdparty/systemd.git] / src / resolve / test-resolved-etc-hosts.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "fd-util.h"
4 #include "fs-util.h"
5 #include "log.h"
6 #include "resolved-etc-hosts.h"
7 #include "tmpfile-util.h"
8
9 static void test_parse_etc_hosts_system(void) {
10 _cleanup_fclose_ FILE *f = NULL;
11
12 log_info("/* %s */", __func__);
13
14 f = fopen("/etc/hosts", "re");
15 if (!f) {
16 assert_se(errno == -ENOENT);
17 return;
18 }
19
20 _cleanup_(etc_hosts_free) EtcHosts hosts = {};
21 assert_se(etc_hosts_parse(&hosts, f) == 0);
22 }
23
24 static void test_parse_etc_hosts(void) {
25 _cleanup_(unlink_tempfilep) char
26 t[] = "/tmp/test-resolved-etc-hosts.XXXXXX";
27
28 log_info("/* %s */", __func__);
29
30 int fd;
31 _cleanup_fclose_ FILE *f;
32
33 fd = mkostemp_safe(t);
34 assert_se(fd >= 0);
35
36 f = fdopen(fd, "r+");
37 assert_se(f);
38 fputs("1.2.3.4 some.where\n", f);
39 fputs("1.2.3.5 some.where\n", f);
40 fputs("::0 some.where some.other\n", f);
41 fputs("0.0.0.0 black.listed\n", f);
42 fputs("::5 some.where some.other foobar.foo.foo\n", f);
43 fputs(" \n", f);
44 fflush(f);
45 rewind(f);
46
47 _cleanup_(etc_hosts_free) EtcHosts hosts = {};
48 assert_se(etc_hosts_parse(&hosts, f) == 0);
49
50 EtcHostsItemByName *bn;
51 assert_se(bn = hashmap_get(hosts.by_name, "some.where"));
52 assert_se(bn->n_addresses == 3);
53 assert_se(bn->n_allocated >= 3);
54
55 assert_se(bn->addresses[0]->family == AF_INET);
56 assert_se(memcmp(&bn->addresses[0]->address.in,
57 &(struct in_addr) { .s_addr = htobe32(0x01020304) }, 4) == 0);
58 assert_se(bn->addresses[1]->family == AF_INET);
59 assert_se(memcmp(&bn->addresses[1]->address.in,
60 &(struct in_addr) { .s_addr = htobe32(0x01020305) }, 4) == 0);
61 assert_se(bn->addresses[2]->family == AF_INET6);
62 assert_se(memcmp(&bn->addresses[2]->address.in6,
63 &(struct in6_addr) { .s6_addr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5} }, 16 ) == 0);
64
65 assert_se(bn = hashmap_get(hosts.by_name, "some.other"));
66 assert_se(bn->n_addresses == 1);
67 assert_se(bn->n_allocated >= 1);
68 assert_se(bn->addresses[0]->family == AF_INET6);
69 assert_se(memcmp(&bn->addresses[0]->address.in6,
70 &(struct in6_addr) { .s6_addr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5} }, 16 ) == 0);
71
72 assert_se( set_contains(hosts.no_address, "some.where"));
73 assert_se( set_contains(hosts.no_address, "some.other"));
74 assert_se( set_contains(hosts.no_address, "black.listed"));
75 assert_se(!set_contains(hosts.no_address, "foobar.foo.foo"));
76 }
77
78 static void test_parse_file(const char *fname) {
79 _cleanup_(etc_hosts_free) EtcHosts hosts = {};
80 _cleanup_fclose_ FILE *f;
81
82 log_info("/* %s(\"%s\") */", __func__, fname);
83
84 assert_se(f = fopen(fname, "re"));
85 assert_se(etc_hosts_parse(&hosts, f) == 0);
86 }
87
88 int main(int argc, char **argv) {
89 log_set_max_level(LOG_DEBUG);
90 log_parse_environment();
91 log_open();
92
93 if (argc == 1) {
94 test_parse_etc_hosts_system();
95 test_parse_etc_hosts();
96 } else
97 test_parse_file(argv[1]);
98
99 return 0;
100 }