resolves: tests for dns_cache_prune()
authorJames Coglan <james@neighbourhood.ie>
Wed, 19 Jun 2024 09:39:01 +0000 (10:39 +0100)
committerJames Coglan <james@neighbourhood.ie>
Tue, 23 Jul 2024 13:08:31 +0000 (14:08 +0100)
src/resolve/test-dns-cache.c

index 0dd69a541d25d8331903853f90e43fd287c2a80b..a597a112ad62b0b9ce37a15aa6536b18a435ef50 100644 (file)
@@ -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()
  * ================================================================ */