]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/test-resolved-etc-hosts.c
6706885ebd0e63b1f74521c0c8d8b1566157746f
[thirdparty/systemd.git] / src / resolve / test-resolved-etc-hosts.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <arpa/inet.h>
4 #include <malloc.h>
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "fs-util.h"
11 #include "log.h"
12 #include "resolved-etc-hosts.h"
13 #include "strv.h"
14 #include "tests.h"
15 #include "tmpfile-util.h"
16
17 static void test_parse_etc_hosts_system(void) {
18 _cleanup_fclose_ FILE *f = NULL;
19
20 log_info("/* %s */", __func__);
21
22 f = fopen("/etc/hosts", "re");
23 if (!f) {
24 assert_se(errno == ENOENT);
25 return;
26 }
27
28 _cleanup_(etc_hosts_free) EtcHosts hosts = {};
29 assert_se(etc_hosts_parse(&hosts, f) == 0);
30 }
31
32 #define address_equal_4(_addr, _address) \
33 ((_addr)->family == AF_INET && \
34 !memcmp(&(_addr)->address.in, &(struct in_addr) { .s_addr = (_address) }, 4))
35
36 #define address_equal_6(_addr, ...) \
37 ((_addr)->family == AF_INET6 && \
38 !memcmp(&(_addr)->address.in6, &(struct in6_addr) { .s6_addr = __VA_ARGS__}, 16) )
39
40 static void test_parse_etc_hosts(void) {
41 _cleanup_(unlink_tempfilep) char
42 t[] = "/tmp/test-resolved-etc-hosts.XXXXXX";
43
44 log_info("/* %s */", __func__);
45
46 int fd;
47 _cleanup_fclose_ FILE *f;
48 const char *s;
49
50 fd = mkostemp_safe(t);
51 assert_se(fd >= 0);
52
53 f = fdopen(fd, "r+");
54 assert_se(f);
55 fputs("1.2.3.4 some.where\n"
56 "1.2.3.5 some.where\n"
57 "1.2.3.6 dash dash-dash.where-dash\n"
58 "1.2.3.7 bad-dash- -bad-dash -bad-dash.bad-\n"
59 "1.2.3.8\n"
60 "1.2.3.9 before.comment # within.comment\n"
61 "1.2.3.10 before.comment#within.comment2\n"
62 "1.2.3.11 before.comment# within.comment3\n"
63 "1.2.3.12 before.comment#\n"
64 "1.2.3 short.address\n"
65 "1.2.3.4.5 long.address\n"
66 "1::2::3 multi.colon\n"
67
68 "::0 some.where some.other\n"
69 "0.0.0.0 deny.listed\n"
70 "::5\t\t\t \tsome.where\tsome.other foobar.foo.foo\t\t\t\n"
71 " \n", f);
72 assert_se(fflush_and_check(f) >= 0);
73 rewind(f);
74
75 _cleanup_(etc_hosts_free) EtcHosts hosts = {};
76 assert_se(etc_hosts_parse(&hosts, f) == 0);
77
78 EtcHostsItemByName *bn;
79 assert_se(bn = hashmap_get(hosts.by_name, "some.where"));
80 assert_se(bn->n_addresses == 3);
81 assert_se(MALLOC_ELEMENTSOF(bn->addresses) >= 3);
82 assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.4")));
83 assert_se(address_equal_4(bn->addresses[1], inet_addr("1.2.3.5")));
84 assert_se(address_equal_6(bn->addresses[2], {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}));
85
86 assert_se(bn = hashmap_get(hosts.by_name, "dash"));
87 assert_se(bn->n_addresses == 1);
88 assert_se(MALLOC_ELEMENTSOF(bn->addresses) >= 1);
89 assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.6")));
90
91 assert_se(bn = hashmap_get(hosts.by_name, "dash-dash.where-dash"));
92 assert_se(bn->n_addresses == 1);
93 assert_se(MALLOC_ELEMENTSOF(bn->addresses) >= 1);
94 assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.6")));
95
96 /* See https://tools.ietf.org/html/rfc1035#section-2.3.1 */
97 FOREACH_STRING(s, "bad-dash-", "-bad-dash", "-bad-dash.bad-")
98 assert_se(!hashmap_get(hosts.by_name, s));
99
100 assert_se(bn = hashmap_get(hosts.by_name, "before.comment"));
101 assert_se(bn->n_addresses == 4);
102 assert_se(MALLOC_ELEMENTSOF(bn->addresses) >= 4);
103 assert_se(address_equal_4(bn->addresses[0], inet_addr("1.2.3.9")));
104 assert_se(address_equal_4(bn->addresses[1], inet_addr("1.2.3.10")));
105 assert_se(address_equal_4(bn->addresses[2], inet_addr("1.2.3.11")));
106 assert_se(address_equal_4(bn->addresses[3], inet_addr("1.2.3.12")));
107
108 assert(!hashmap_get(hosts.by_name, "within.comment"));
109 assert(!hashmap_get(hosts.by_name, "within.comment2"));
110 assert(!hashmap_get(hosts.by_name, "within.comment3"));
111 assert(!hashmap_get(hosts.by_name, "#"));
112
113 assert(!hashmap_get(hosts.by_name, "short.address"));
114 assert(!hashmap_get(hosts.by_name, "long.address"));
115 assert(!hashmap_get(hosts.by_name, "multi.colon"));
116 assert_se(!set_contains(hosts.no_address, "short.address"));
117 assert_se(!set_contains(hosts.no_address, "long.address"));
118 assert_se(!set_contains(hosts.no_address, "multi.colon"));
119
120 assert_se(bn = hashmap_get(hosts.by_name, "some.other"));
121 assert_se(bn->n_addresses == 1);
122 assert_se(MALLOC_ELEMENTSOF(bn->addresses) >= 1);
123 assert_se(address_equal_6(bn->addresses[0], {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}));
124
125 assert_se( set_contains(hosts.no_address, "some.where"));
126 assert_se( set_contains(hosts.no_address, "some.other"));
127 assert_se( set_contains(hosts.no_address, "deny.listed"));
128 assert_se(!set_contains(hosts.no_address, "foobar.foo.foo"));
129 }
130
131 static void test_parse_file(const char *fname) {
132 _cleanup_(etc_hosts_free) EtcHosts hosts = {};
133 _cleanup_fclose_ FILE *f;
134
135 log_info("/* %s(\"%s\") */", __func__, fname);
136
137 assert_se(f = fopen(fname, "re"));
138 assert_se(etc_hosts_parse(&hosts, f) == 0);
139 }
140
141 int main(int argc, char **argv) {
142 test_setup_logging(LOG_DEBUG);
143
144 if (argc == 1) {
145 test_parse_etc_hosts_system();
146 test_parse_etc_hosts();
147 } else
148 test_parse_file(argv[1]);
149
150 return 0;
151 }