From c65f788b9f60ee800ca9c364b79e13796c9e9028 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 21 Nov 2018 14:55:07 +0100 Subject: [PATCH] test-resolved-etc-hosts: when parsing an external file, skip other tests This makes timing tests much more useful. So let's simplify things, and just do the parsing of the external file when that is specified. --- src/resolve/test-resolved-etc-hosts.c | 56 +++++++++++++++------------ 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/src/resolve/test-resolved-etc-hosts.c b/src/resolve/test-resolved-etc-hosts.c index da10391d74e..08e178e7f63 100644 --- a/src/resolve/test-resolved-etc-hosts.c +++ b/src/resolve/test-resolved-etc-hosts.c @@ -9,6 +9,8 @@ static void test_parse_etc_hosts_system(void) { _cleanup_fclose_ FILE *f = NULL; + log_info("/* %s */", __func__); + f = fopen("/etc/hosts", "re"); if (!f) { assert_se(errno == -ENOENT); @@ -19,38 +21,32 @@ static void test_parse_etc_hosts_system(void) { assert_se(etc_hosts_parse(&hosts, f) == 0); } -static void test_parse_etc_hosts(const char *fname) { +static void test_parse_etc_hosts(void) { _cleanup_(unlink_tempfilep) char t[] = "/tmp/test-resolved-etc-hosts.XXXXXX"; + log_info("/* %s */", __func__); + int fd; _cleanup_fclose_ FILE *f; - if (fname) { - f = fopen(fname, "re"); - assert_se(f); - } else { - fd = mkostemp_safe(t); - assert_se(fd >= 0); - - f = fdopen(fd, "r+"); - assert_se(f); - 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); - } + fd = mkostemp_safe(t); + assert_se(fd >= 0); + + f = fdopen(fd, "r+"); + assert_se(f); + 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; - EtcHostsItemByName *bn; assert_se(bn = hashmap_get(hosts.by_name, "some.where")); assert_se(bn->n_addresses == 3); @@ -79,14 +75,26 @@ static void test_parse_etc_hosts(const char *fname) { assert_se(!set_contains(hosts.no_address, "foobar.foo.foo")); } +static void test_parse_file(const char *fname) { + _cleanup_(etc_hosts_free) EtcHosts hosts = {}; + _cleanup_fclose_ FILE *f; + + log_info("/* %s(\"%s\") */", __func__, fname); + + assert_se(f = fopen(fname, "re")); + assert_se(etc_hosts_parse(&hosts, f) == 0); +} + int main(int argc, char **argv) { log_set_max_level(LOG_DEBUG); log_parse_environment(); log_open(); - if (argc == 1) + if (argc == 1) { test_parse_etc_hosts_system(); - test_parse_etc_hosts(argv[1]); + test_parse_etc_hosts(); + } else + test_parse_file(argv[1]); return 0; } -- 2.47.3