]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/test-resolved-etc-hosts.c
721bf8732ef620461eb94a9525752af2df2027c2
1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <netinet/in.h>
5 #include <sys/socket.h>
11 #include "resolved-etc-hosts.h"
14 #include "tmpfile-util.h"
16 static void test_parse_etc_hosts_system(void) {
17 _cleanup_fclose_
FILE *f
= NULL
;
19 log_info("/* %s */", __func__
);
21 f
= fopen("/etc/hosts", "re");
23 assert_se(errno
== ENOENT
);
27 _cleanup_(etc_hosts_free
) EtcHosts hosts
= {};
28 assert_se(etc_hosts_parse(&hosts
, f
) == 0);
31 #define address_equal_4(_addr, _address) \
32 ((_addr)->family == AF_INET && \
33 !memcmp(&(_addr)->address.in, &(struct in_addr) { .s_addr = (_address) }, 4))
35 #define address_equal_6(_addr, ...) \
36 ((_addr)->family == AF_INET6 && \
37 !memcmp(&(_addr)->address.in6, &(struct in6_addr) { .s6_addr = __VA_ARGS__}, 16) )
39 static void test_parse_etc_hosts(void) {
40 _cleanup_(unlink_tempfilep
) char
41 t
[] = "/tmp/test-resolved-etc-hosts.XXXXXX";
43 log_info("/* %s */", __func__
);
46 _cleanup_fclose_
FILE *f
;
49 fd
= mkostemp_safe(t
);
54 fputs("1.2.3.4 some.where\n"
55 "1.2.3.5 some.where\n"
56 "1.2.3.6 dash dash-dash.where-dash\n"
57 "1.2.3.7 bad-dash- -bad-dash -bad-dash.bad-\n"
59 "1.2.3.9 before.comment # within.comment\n"
60 "1.2.3.10 before.comment#within.comment2\n"
61 "1.2.3.11 before.comment# within.comment3\n"
62 "1.2.3.12 before.comment#\n"
63 "1.2.3 short.address\n"
64 "1.2.3.4.5 long.address\n"
65 "1::2::3 multi.colon\n"
67 "::0 some.where some.other\n"
68 "0.0.0.0 deny.listed\n"
69 "::5\t\t\t \tsome.where\tsome.other foobar.foo.foo\t\t\t\n"
71 assert_se(fflush_and_check(f
) >= 0);
74 _cleanup_(etc_hosts_free
) EtcHosts hosts
= {};
75 assert_se(etc_hosts_parse(&hosts
, f
) == 0);
77 EtcHostsItemByName
*bn
;
78 assert_se(bn
= hashmap_get(hosts
.by_name
, "some.where"));
79 assert_se(bn
->n_addresses
== 3);
80 assert_se(bn
->n_allocated
>= 3);
81 assert_se(address_equal_4(bn
->addresses
[0], inet_addr("1.2.3.4")));
82 assert_se(address_equal_4(bn
->addresses
[1], inet_addr("1.2.3.5")));
83 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 assert_se(bn
= hashmap_get(hosts
.by_name
, "dash"));
86 assert_se(bn
->n_addresses
== 1);
87 assert_se(bn
->n_allocated
>= 1);
88 assert_se(address_equal_4(bn
->addresses
[0], inet_addr("1.2.3.6")));
90 assert_se(bn
= hashmap_get(hosts
.by_name
, "dash-dash.where-dash"));
91 assert_se(bn
->n_addresses
== 1);
92 assert_se(bn
->n_allocated
>= 1);
93 assert_se(address_equal_4(bn
->addresses
[0], inet_addr("1.2.3.6")));
95 /* See https://tools.ietf.org/html/rfc1035#section-2.3.1 */
96 FOREACH_STRING(s
, "bad-dash-", "-bad-dash", "-bad-dash.bad-")
97 assert_se(!hashmap_get(hosts
.by_name
, s
));
99 assert_se(bn
= hashmap_get(hosts
.by_name
, "before.comment"));
100 assert_se(bn
->n_addresses
== 4);
101 assert_se(bn
->n_allocated
>= 4);
102 assert_se(address_equal_4(bn
->addresses
[0], inet_addr("1.2.3.9")));
103 assert_se(address_equal_4(bn
->addresses
[1], inet_addr("1.2.3.10")));
104 assert_se(address_equal_4(bn
->addresses
[2], inet_addr("1.2.3.11")));
105 assert_se(address_equal_4(bn
->addresses
[3], inet_addr("1.2.3.12")));
107 assert(!hashmap_get(hosts
.by_name
, "within.comment"));
108 assert(!hashmap_get(hosts
.by_name
, "within.comment2"));
109 assert(!hashmap_get(hosts
.by_name
, "within.comment3"));
110 assert(!hashmap_get(hosts
.by_name
, "#"));
112 assert(!hashmap_get(hosts
.by_name
, "short.address"));
113 assert(!hashmap_get(hosts
.by_name
, "long.address"));
114 assert(!hashmap_get(hosts
.by_name
, "multi.colon"));
115 assert_se(!set_contains(hosts
.no_address
, "short.address"));
116 assert_se(!set_contains(hosts
.no_address
, "long.address"));
117 assert_se(!set_contains(hosts
.no_address
, "multi.colon"));
119 assert_se(bn
= hashmap_get(hosts
.by_name
, "some.other"));
120 assert_se(bn
->n_addresses
== 1);
121 assert_se(bn
->n_allocated
>= 1);
122 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 assert_se( set_contains(hosts
.no_address
, "some.where"));
125 assert_se( set_contains(hosts
.no_address
, "some.other"));
126 assert_se( set_contains(hosts
.no_address
, "deny.listed"));
127 assert_se(!set_contains(hosts
.no_address
, "foobar.foo.foo"));
130 static void test_parse_file(const char *fname
) {
131 _cleanup_(etc_hosts_free
) EtcHosts hosts
= {};
132 _cleanup_fclose_
FILE *f
;
134 log_info("/* %s(\"%s\") */", __func__
, fname
);
136 assert_se(f
= fopen(fname
, "re"));
137 assert_se(etc_hosts_parse(&hosts
, f
) == 0);
140 int main(int argc
, char **argv
) {
141 test_setup_logging(LOG_DEBUG
);
144 test_parse_etc_hosts_system();
145 test_parse_etc_hosts();
147 test_parse_file(argv
[1]);