| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <arpa/inet.h> |
| 4 |
#include <netinet/in.h> |
| 5 |
#include <sys/socket.h> |
| 6 |
|
| 7 |
#include "argv-util.h" |
| 8 |
#include "fd-util.h" |
| 9 |
#include "fileio.h" |
| 10 |
#include "hashmap.h" |
| 11 |
#include "log.h" |
| 12 |
#include "resolved-etc-hosts.h" |
| 13 |
#include "set.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "tests.h" |
| 16 |
#include "tmpfile-util.h" |
| 17 |
|
| 18 |
TEST(parse_etc_hosts_system) { |
| 19 |
_cleanup_fclose_ FILE *f = NULL; |
| 20 |
|
| 21 |
f = fopen("/etc/hosts", "re"); |
| 22 |
if (!f) { |
| 23 |
assert_se(errno == ENOENT); |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
_cleanup_(etc_hosts_clear) EtcHosts hosts = {}; |
| 28 |
assert_se(etc_hosts_parse(&hosts, f) == 0); |
| 29 |
} |
| 30 |
|
| 31 |
#define in_addr_4(_address_str) \ |
| 32 |
(&(struct in_addr_data) { .family = AF_INET, .address.in = { .s_addr = inet_addr(_address_str) } }) |
| 33 |
|
| 34 |
#define in_addr_6(...) \ |
| 35 |
(&(struct in_addr_data) { .family = AF_INET6, .address.in6 = { .s6_addr = __VA_ARGS__ } }) |
| 36 |
|
| 37 |
#define has_4(_set, _address_str) \ |
| 38 |
set_contains(_set, in_addr_4(_address_str)) |
| 39 |
|
| 40 |
#define has_6(_set, ...) \ |
| 41 |
set_contains(_set, in_addr_6(__VA_ARGS__)) |
| 42 |
|
| 43 |
TEST(parse_etc_hosts) { |
| 44 |
_cleanup_(unlink_tempfilep) char |
| 45 |
t[] = "/tmp/test-resolved-etc-hosts.XXXXXX"; |
| 46 |
|
| 47 |
int fd; |
| 48 |
_cleanup_fclose_ FILE *f = NULL; |
| 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_clear) 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(set_size(bn->addresses) == 3); |
| 81 |
assert_se(has_4(bn->addresses, "1.2.3.4")); |
| 82 |
assert_se(has_4(bn->addresses, "1.2.3.5")); |
| 83 |
assert_se(has_6(bn->addresses, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5})); |
| 84 |
|
| 85 |
assert_se(bn = hashmap_get(hosts.by_name, "dash")); |
| 86 |
assert_se(set_size(bn->addresses) == 1); |
| 87 |
assert_se(has_4(bn->addresses, "1.2.3.6")); |
| 88 |
|
| 89 |
assert_se(bn = hashmap_get(hosts.by_name, "dash-dash.where-dash")); |
| 90 |
assert_se(set_size(bn->addresses) == 1); |
| 91 |
assert_se(has_4(bn->addresses, "1.2.3.6")); |
| 92 |
|
| 93 |
/* See https://tools.ietf.org/html/rfc1035#section-2.3.1 */ |
| 94 |
FOREACH_STRING(s, "bad-dash-", "-bad-dash", "-bad-dash.bad-") |
| 95 |
ASSERT_FALSE(hashmap_contains(hosts.by_name, s)); |
| 96 |
|
| 97 |
assert_se(bn = hashmap_get(hosts.by_name, "before.comment")); |
| 98 |
assert_se(set_size(bn->addresses) == 4); |
| 99 |
assert_se(has_4(bn->addresses, "1.2.3.9")); |
| 100 |
assert_se(has_4(bn->addresses, "1.2.3.10")); |
| 101 |
assert_se(has_4(bn->addresses, "1.2.3.11")); |
| 102 |
assert_se(has_4(bn->addresses, "1.2.3.12")); |
| 103 |
|
| 104 |
ASSERT_FALSE(hashmap_contains(hosts.by_name, "within.comment")); |
| 105 |
ASSERT_FALSE(hashmap_contains(hosts.by_name, "within.comment2")); |
| 106 |
ASSERT_FALSE(hashmap_contains(hosts.by_name, "within.comment3")); |
| 107 |
ASSERT_FALSE(hashmap_contains(hosts.by_name, "#")); |
| 108 |
|
| 109 |
ASSERT_FALSE(hashmap_contains(hosts.by_name, "short.address")); |
| 110 |
ASSERT_FALSE(hashmap_contains(hosts.by_name, "long.address")); |
| 111 |
ASSERT_FALSE(hashmap_contains(hosts.by_name, "multi.colon")); |
| 112 |
ASSERT_FALSE(set_contains(hosts.no_address, "short.address")); |
| 113 |
ASSERT_FALSE(set_contains(hosts.no_address, "long.address")); |
| 114 |
ASSERT_FALSE(set_contains(hosts.no_address, "multi.colon")); |
| 115 |
|
| 116 |
assert_se(bn = hashmap_get(hosts.by_name, "some.other")); |
| 117 |
assert_se(set_size(bn->addresses) == 1); |
| 118 |
assert_se(has_6(bn->addresses, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5})); |
| 119 |
|
| 120 |
EtcHostsItemByAddress *ba; |
| 121 |
assert_se(ba = hashmap_get(hosts.by_address, in_addr_4("1.2.3.6"))); |
| 122 |
assert_se(set_size(ba->names) == 2); |
| 123 |
assert_se(set_contains(ba->names, "dash")); |
| 124 |
assert_se(set_contains(ba->names, "dash-dash.where-dash")); |
| 125 |
assert_se(streq(ba->canonical_name, "dash")); |
| 126 |
|
| 127 |
assert_se(ba = hashmap_get(hosts.by_address, in_addr_6({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}))); |
| 128 |
assert_se(set_size(ba->names) == 3); |
| 129 |
assert_se(set_contains(ba->names, "some.where")); |
| 130 |
assert_se(set_contains(ba->names, "some.other")); |
| 131 |
assert_se(set_contains(ba->names, "foobar.foo.foo")); |
| 132 |
assert_se(streq(ba->canonical_name, "some.where")); |
| 133 |
|
| 134 |
assert_se( set_contains(hosts.no_address, "some.where")); |
| 135 |
assert_se( set_contains(hosts.no_address, "some.other")); |
| 136 |
assert_se( set_contains(hosts.no_address, "deny.listed")); |
| 137 |
assert_se(!set_contains(hosts.no_address, "foobar.foo.foo")); |
| 138 |
} |
| 139 |
|
| 140 |
static void test_parse_file_one(const char *fname) { |
| 141 |
_cleanup_(etc_hosts_clear) EtcHosts hosts = {}; |
| 142 |
_cleanup_fclose_ FILE *f = NULL; |
| 143 |
|
| 144 |
log_info("/* %s(\"%s\") */", __func__, fname); |
| 145 |
|
| 146 |
assert_se(f = fopen(fname, "re")); |
| 147 |
assert_se(etc_hosts_parse(&hosts, f) == 0); |
| 148 |
} |
| 149 |
|
| 150 |
TEST(parse_file) { |
| 151 |
for (int i = 1; i < saved_argc; i++) |
| 152 |
test_parse_file_one(saved_argv[i]); |
| 153 |
} |
| 154 |
|
| 155 |
DEFINE_TEST_MAIN(LOG_DEBUG); |