]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/test-resolved-etc-hosts.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <netinet/in.h>
6 #include <sys/socket.h>
12 #include "resolved-etc-hosts.h"
15 #include "tmpfile-util.h"
17 TEST(parse_etc_hosts_system
) {
18 _cleanup_fclose_
FILE *f
= NULL
;
20 f
= fopen("/etc/hosts", "re");
22 assert_se(errno
== ENOENT
);
26 _cleanup_(etc_hosts_clear
) EtcHosts hosts
= {};
27 assert_se(etc_hosts_parse(&hosts
, f
) == 0);
30 #define has_4(_set, _address_str) \
31 set_contains(_set, &(struct in_addr_data) { .family = AF_INET, .address.in = { .s_addr = inet_addr(_address_str) } })
33 #define has_6(_set, ...) \
34 set_contains(_set, &(struct in_addr_data) { .family = AF_INET6, .address.in6 = { .s6_addr = __VA_ARGS__ } })
36 TEST(parse_etc_hosts
) {
37 _cleanup_(unlink_tempfilep
) char
38 t
[] = "/tmp/test-resolved-etc-hosts.XXXXXX";
41 _cleanup_fclose_
FILE *f
;
43 fd
= mkostemp_safe(t
);
48 fputs("1.2.3.4 some.where\n"
49 "1.2.3.5 some.where\n"
50 "1.2.3.6 dash dash-dash.where-dash\n"
51 "1.2.3.7 bad-dash- -bad-dash -bad-dash.bad-\n"
53 "1.2.3.9 before.comment # within.comment\n"
54 "1.2.3.10 before.comment#within.comment2\n"
55 "1.2.3.11 before.comment# within.comment3\n"
56 "1.2.3.12 before.comment#\n"
57 "1.2.3 short.address\n"
58 "1.2.3.4.5 long.address\n"
59 "1::2::3 multi.colon\n"
61 "::0 some.where some.other\n"
62 "0.0.0.0 deny.listed\n"
63 "::5\t\t\t \tsome.where\tsome.other foobar.foo.foo\t\t\t\n"
65 assert_se(fflush_and_check(f
) >= 0);
68 _cleanup_(etc_hosts_clear
) EtcHosts hosts
= {};
69 assert_se(etc_hosts_parse(&hosts
, f
) == 0);
71 EtcHostsItemByName
*bn
;
72 assert_se(bn
= hashmap_get(hosts
.by_name
, "some.where"));
73 assert_se(set_size(bn
->addresses
) == 3);
74 assert_se(has_4(bn
->addresses
, "1.2.3.4"));
75 assert_se(has_4(bn
->addresses
, "1.2.3.5"));
76 assert_se(has_6(bn
->addresses
, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}));
78 assert_se(bn
= hashmap_get(hosts
.by_name
, "dash"));
79 assert_se(set_size(bn
->addresses
) == 1);
80 assert_se(has_4(bn
->addresses
, "1.2.3.6"));
82 assert_se(bn
= hashmap_get(hosts
.by_name
, "dash-dash.where-dash"));
83 assert_se(set_size(bn
->addresses
) == 1);
84 assert_se(has_4(bn
->addresses
, "1.2.3.6"));
86 /* See https://tools.ietf.org/html/rfc1035#section-2.3.1 */
87 FOREACH_STRING(s
, "bad-dash-", "-bad-dash", "-bad-dash.bad-")
88 assert_se(!hashmap_get(hosts
.by_name
, s
));
90 assert_se(bn
= hashmap_get(hosts
.by_name
, "before.comment"));
91 assert_se(set_size(bn
->addresses
) == 4);
92 assert_se(has_4(bn
->addresses
, "1.2.3.9"));
93 assert_se(has_4(bn
->addresses
, "1.2.3.10"));
94 assert_se(has_4(bn
->addresses
, "1.2.3.11"));
95 assert_se(has_4(bn
->addresses
, "1.2.3.12"));
97 assert_se(!hashmap_get(hosts
.by_name
, "within.comment"));
98 assert_se(!hashmap_get(hosts
.by_name
, "within.comment2"));
99 assert_se(!hashmap_get(hosts
.by_name
, "within.comment3"));
100 assert_se(!hashmap_get(hosts
.by_name
, "#"));
102 assert_se(!hashmap_get(hosts
.by_name
, "short.address"));
103 assert_se(!hashmap_get(hosts
.by_name
, "long.address"));
104 assert_se(!hashmap_get(hosts
.by_name
, "multi.colon"));
105 assert_se(!set_contains(hosts
.no_address
, "short.address"));
106 assert_se(!set_contains(hosts
.no_address
, "long.address"));
107 assert_se(!set_contains(hosts
.no_address
, "multi.colon"));
109 assert_se(bn
= hashmap_get(hosts
.by_name
, "some.other"));
110 assert_se(set_size(bn
->addresses
) == 1);
111 assert_se(has_6(bn
->addresses
, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}));
113 assert_se( set_contains(hosts
.no_address
, "some.where"));
114 assert_se( set_contains(hosts
.no_address
, "some.other"));
115 assert_se( set_contains(hosts
.no_address
, "deny.listed"));
116 assert_se(!set_contains(hosts
.no_address
, "foobar.foo.foo"));
119 static void test_parse_file_one(const char *fname
) {
120 _cleanup_(etc_hosts_clear
) EtcHosts hosts
= {};
121 _cleanup_fclose_
FILE *f
;
123 log_info("/* %s(\"%s\") */", __func__
, fname
);
125 assert_se(f
= fopen(fname
, "re"));
126 assert_se(etc_hosts_parse(&hosts
, f
) == 0);
130 for (int i
= 1; i
< saved_argc
; i
++)
131 test_parse_file_one(saved_argv
[i
]);
134 DEFINE_TEST_MAIN(LOG_DEBUG
);