From 7137086fa0a42abb2420354e1e7e1b99372c4bdd Mon Sep 17 00:00:00 2001 From: James Coglan Date: Wed, 19 Jun 2024 10:39:01 +0100 Subject: [PATCH] resolves: tests for dns_cache_prune() --- src/resolve/test-dns-cache.c | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/resolve/test-dns-cache.c b/src/resolve/test-dns-cache.c index 0dd69a541d..a597a112ad 100644 --- a/src/resolve/test-dns-cache.c +++ b/src/resolve/test-dns-cache.c @@ -752,6 +752,44 @@ TEST(dns_cache_lookup_mdns_multiple_unshared_responses_are_not_cached) { dns_resource_record_unref(rr); } +/* ================================================================ + * dns_cache_prune(), dns_cache_expiry_in_one_second() + * ================================================================ */ + +TEST(dns_cache_prune) { + _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); + _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); + DnsResourceKey *key = NULL; + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com"); + ASSERT_NOT_NULL(key); + answer_add_a(&put_args, key, 0xc0a8017f, 1, DNS_ANSWER_CACHEABLE); + dns_resource_key_unref(key); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns2.example.com"); + ASSERT_NOT_NULL(key); + answer_add_a(&put_args, key, 0x7f01a8cc, 3, DNS_ANSWER_CACHEABLE); + dns_resource_key_unref(key); + + cache_put(&cache, &put_args); + + dns_cache_prune(&cache); + ASSERT_EQ(dns_cache_size(&cache), 2u); + ASSERT_TRUE(dns_cache_expiry_in_one_second(&cache, now(CLOCK_BOOTTIME))); + + sleep(2); + + dns_cache_prune(&cache); + ASSERT_EQ(dns_cache_size(&cache), 1u); + ASSERT_TRUE(dns_cache_expiry_in_one_second(&cache, now(CLOCK_BOOTTIME))); + + sleep(2); + + dns_cache_prune(&cache); + ASSERT_TRUE(dns_cache_is_empty(&cache)); + ASSERT_FALSE(dns_cache_expiry_in_one_second(&cache, now(CLOCK_BOOTTIME))); +} + /* ================================================================ * dns_cache_check_conflicts() * ================================================================ */ -- 2.25.1