diff options
author | James Coglan <james@neighbourhood.ie> | 2024-05-24 11:58:30 +0200 |
---|---|---|
committer | James Coglan <james@neighbourhood.ie> | 2024-07-23 15:08:31 +0200 |
commit | 6dd377ad708317aba2e0c3cab37db6313098c2c9 (patch) | |
tree | 29ba2a027db673a3186190f6c1b137e1cd512e45 /src/resolve/test-dns-cache.c | |
parent | resolved: test cache misses (diff) | |
download | systemd-6dd377ad708317aba2e0c3cab37db6313098c2c9.tar.xz systemd-6dd377ad708317aba2e0c3cab37db6313098c2c9.zip |
resolved: tests for dns_cache_dump()
Diffstat (limited to 'src/resolve/test-dns-cache.c')
-rw-r--r-- | src/resolve/test-dns-cache.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/resolve/test-dns-cache.c b/src/resolve/test-dns-cache.c index f94a405d04..150b118e52 100644 --- a/src/resolve/test-dns-cache.c +++ b/src/resolve/test-dns-cache.c @@ -3,6 +3,9 @@ #include <sys/socket.h> #include "dns-type.h" +#include "fd-util.h" +#include "fileio.h" +#include "fs-util.h" #include "log.h" #include "resolve-util.h" #include "resolved-def.h" @@ -12,6 +15,7 @@ #include "resolved-dns-packet.h" #include "resolved-dns-rr.h" #include "tests.h" +#include "tmpfile-util.h" static DnsCache new_cache(void) { return (DnsCache) {}; @@ -450,6 +454,92 @@ TEST(dns_cache_lookup_any_always_misses) { } /* ================================================================ + * dns_cache_dump() + * ================================================================ */ + +static int cmpstring(const void *a, const void *b) { + ASSERT_NOT_NULL(a); + ASSERT_NOT_NULL(b); + + return strcmp(*(const char **)a, *(const char **)b); +} + +static void check_dump_contents(FILE *f, const char **expected, size_t n) { + char *actual[n]; + rewind(f); + + for (size_t i = 0; i < n; i++) { + size_t length = read_line(f, 1024, &actual[i]); + ASSERT_GT(length, 0u); + } + + qsort(actual, n, sizeof(char *), cmpstring); + + for (size_t i = 0; i < n; i++) + ASSERT_STREQ(actual[i], expected[i]); + + for (size_t i = 0; i < n; i++) + free(actual[i]); +} + +TEST(dns_cache_dump_single_a) { + _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); + _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); + + put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); + ASSERT_NOT_NULL(put_args.key); + put_args.rcode = DNS_RCODE_SUCCESS; + answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); + cache_put(&cache, &put_args); + + ASSERT_EQ(dns_cache_size(&cache), 1u); + + _cleanup_(unlink_tempfilep) char p[] = "/tmp/dns-cache-dump-single-a-XXXXXX"; + _cleanup_fclose_ FILE *f = NULL; + fmkostemp_safe(p, "r+", &f); + dns_cache_dump(&cache, f); + + const char *expected[] = { + "\twww.example.com IN A 192.168.1.127" + }; + check_dump_contents(f, expected, 1); +} + +TEST(dns_cache_dump_a_with_cname) { + _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); + _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + + put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); + ASSERT_NOT_NULL(put_args.key); + put_args.rcode = DNS_RCODE_SUCCESS; + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com"); + ASSERT_NOT_NULL(key); + answer_add_cname(&put_args, key, "example.com", 3600, DNS_ANSWER_CACHEABLE); + + dns_resource_key_unref(key); + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); + ASSERT_NOT_NULL(key); + answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); + + cache_put(&cache, &put_args); + + ASSERT_EQ(dns_cache_size(&cache), 2u); + + _cleanup_(unlink_tempfilep) char p[] = "/tmp/dns-cache-dump-a-with-cname-XXXXXX"; + _cleanup_fclose_ FILE *f = NULL; + fmkostemp_safe(p, "r+", &f); + dns_cache_dump(&cache, f); + + const char *expected[] = { + "\texample.com IN A 192.168.1.127", + "\twww.example.com IN CNAME example.com" + }; + check_dump_contents(f, expected, 2); +} + +/* ================================================================ * dns_cache_dump_to_json() * ================================================================ */ |