2018-07-31 11:01:21 +02:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
|
|
|
|
|
|
|
#include "fd-util.h"
|
|
|
|
#include "fileio.h"
|
|
|
|
#include "fs-util.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "resolved-etc-hosts.h"
|
|
|
|
|
|
|
|
static void test_parse_etc_hosts_system(void) {
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
|
|
|
|
2018-08-03 16:48:53 +02:00
|
|
|
f = fopen("/etc/hosts", "re");
|
2018-07-31 11:01:21 +02:00
|
|
|
if (!f) {
|
|
|
|
assert_se(errno == -ENOENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cleanup_(etc_hosts_free) EtcHosts hosts = {};
|
|
|
|
assert_se(etc_hosts_parse(&hosts, f) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_parse_etc_hosts(const char *fname) {
|
|
|
|
_cleanup_(unlink_tempfilep) char
|
|
|
|
t[] = "/tmp/test-resolved-etc-hosts.XXXXXX";
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
_cleanup_fclose_ FILE *f;
|
|
|
|
|
|
|
|
if (fname) {
|
2018-08-03 16:48:53 +02:00
|
|
|
f = fopen(fname, "re");
|
2018-07-31 11:01:21 +02:00
|
|
|
assert_se(f);
|
|
|
|
} else {
|
|
|
|
fd = mkostemp_safe(t);
|
|
|
|
assert_se(fd >= 0);
|
|
|
|
|
|
|
|
f = fdopen(fd, "r+");
|
2018-08-02 18:00:28 +02:00
|
|
|
assert_se(f);
|
2018-07-31 11:01:21 +02:00
|
|
|
fputs("1.2.3.4 some.where\n", f);
|
|
|
|
fputs("1.2.3.5 some.where\n", f);
|
|
|
|
fputs("::0 some.where some.other\n", f);
|
|
|
|
fputs("0.0.0.0 black.listed\n", f);
|
|
|
|
fputs("::5 some.where some.other foobar.foo.foo\n", f);
|
|
|
|
fputs(" \n", f);
|
|
|
|
fflush(f);
|
|
|
|
rewind(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
_cleanup_(etc_hosts_free) EtcHosts hosts = {};
|
|
|
|
assert_se(etc_hosts_parse(&hosts, f) == 0);
|
|
|
|
|
|
|
|
if (fname)
|
|
|
|
return;
|
2018-08-02 18:00:36 +02:00
|
|
|
|
2018-07-31 11:01:21 +02:00
|
|
|
EtcHostsItemByName *bn;
|
|
|
|
assert_se(bn = hashmap_get(hosts.by_name, "some.where"));
|
|
|
|
assert_se(bn->n_addresses == 3);
|
|
|
|
assert_se(bn->n_allocated >= 3);
|
|
|
|
|
|
|
|
assert_se(bn->addresses[0]->family == AF_INET);
|
|
|
|
assert_se(memcmp(&bn->addresses[0]->address.in,
|
|
|
|
&(struct in_addr) { .s_addr = htobe32(0x01020304) }, 4) == 0);
|
|
|
|
assert_se(bn->addresses[1]->family == AF_INET);
|
|
|
|
assert_se(memcmp(&bn->addresses[1]->address.in,
|
|
|
|
&(struct in_addr) { .s_addr = htobe32(0x01020305) }, 4) == 0);
|
|
|
|
assert_se(bn->addresses[2]->family == AF_INET6);
|
|
|
|
assert_se(memcmp(&bn->addresses[2]->address.in6,
|
|
|
|
&(struct in6_addr) { .s6_addr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5} }, 16 ) == 0);
|
|
|
|
|
|
|
|
assert_se(bn = hashmap_get(hosts.by_name, "some.other"));
|
|
|
|
assert_se(bn->n_addresses == 1);
|
|
|
|
assert_se(bn->n_allocated >= 1);
|
|
|
|
assert_se(bn->addresses[0]->family == AF_INET6);
|
|
|
|
assert_se(memcmp(&bn->addresses[0]->address.in6,
|
|
|
|
&(struct in6_addr) { .s6_addr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5} }, 16 ) == 0);
|
resolved: keep addresses mapped to ::0 in a separate set
We'd store every 0.0.0.0 and ::0 entry as a structure without any addresses
allocated. This is a somewhat common use case, let's optimize it a bit.
This gives some memory savings and a bit faster response time too:
'time build/test-resolved-etc-hosts hosts' goes from 7.7s to 5.6s, and
memory use as reported by valgrind for ~10000 hosts is reduced
==18097== total heap usage: 29,902 allocs, 29,902 frees, 2,136,437 bytes allocated
==18240== total heap usage: 19,955 allocs, 19,955 frees, 1,556,021 bytes allocated
Also rename 'suppress' to 'found' (with reverse meaning). I think this makes
the intent clearer.
2018-07-31 15:09:13 +02:00
|
|
|
|
|
|
|
assert_se( set_contains(hosts.no_address, "some.where"));
|
|
|
|
assert_se( set_contains(hosts.no_address, "some.other"));
|
|
|
|
assert_se( set_contains(hosts.no_address, "black.listed"));
|
|
|
|
assert_se(!set_contains(hosts.no_address, "foobar.foo.foo"));
|
2018-07-31 11:01:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
log_set_max_level(LOG_DEBUG);
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
test_parse_etc_hosts_system();
|
|
|
|
test_parse_etc_hosts(argv[1]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|