]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolved: tests for dns_cache_lookup() for NXDOMAIN
authorJames Coglan <james@neighbourhood.ie>
Thu, 23 May 2024 08:53:42 +0000 (09:53 +0100)
committerJames Coglan <james@neighbourhood.ie>
Tue, 23 Jul 2024 13:08:31 +0000 (14:08 +0100)
src/resolve/test-dns-cache.c

index ab76bf506300bde3ea2ece08370faacbfee31ded..4d87fd85b3ed1d86db1e227c702b9a1e9c03eb63 100644 (file)
@@ -83,6 +83,12 @@ static void put_args_unrefp(PutArgs *args) {
         dns_packet_unref(args->full_packet);
 }
 
+static char* checked_strdup(const char *str) {
+        char *copy = strdup(str);
+        ASSERT_NOT_NULL(copy);
+        return copy;
+}
+
 static void answer_add_a(PutArgs *args, DnsResourceKey *key, int addr, int ttl, DnsAnswerFlags flags) {
         _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
 
@@ -98,8 +104,7 @@ static void answer_add_cname(PutArgs *args, DnsResourceKey *key, const char *ali
 
         rr = dns_resource_record_new(key);
         ASSERT_NOT_NULL(rr);
-        rr->cname.name = strdup(alias);
-        ASSERT_NOT_NULL(rr->cname.name);
+        rr->cname.name = checked_strdup(alias);
         rr->ttl = ttl;
         dns_answer_add(args->answer, rr, 1, flags, NULL);
 }
@@ -311,7 +316,7 @@ TEST(dns_a_to_cname_success_escaped_name_returns_error) {
  * dns_cache_lookup()
  * ================================================================ */
 
-TEST(dns_cache_lookup_single) {
+TEST(dns_cache_lookup_success) {
         _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
         _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
         _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
@@ -348,6 +353,49 @@ TEST(dns_cache_lookup_single) {
         ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
 }
 
+TEST(dns_cache_lookup_nxdomain) {
+        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
+        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
+        _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
+        _cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL;
+        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
+        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
+        int query_flags, ret_rcode;
+        uint64_t ret_query_flags;
+
+        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_NXDOMAIN;
+        dns_answer_add_soa(put_args.answer, "example.com", 3600, 0);
+        cache_put(&cache, &put_args);
+
+        ASSERT_EQ(dns_cache_size(&cache), 1u);
+
+        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
+        ASSERT_NOT_NULL(key);
+        query_flags = 0;
+        ASSERT_TRUE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL));
+
+        ASSERT_EQ(cache.n_hit, 1u);
+        ASSERT_EQ(cache.n_miss, 0u);
+
+        ASSERT_EQ(ret_rcode, DNS_RCODE_NXDOMAIN);
+        ASSERT_EQ(ret_query_flags, (SD_RESOLVED_AUTHENTICATED | SD_RESOLVED_CONFIDENTIAL));
+
+        ASSERT_EQ(dns_answer_size(ret_answer), 1u);
+
+        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "example.com");
+        ASSERT_NOT_NULL(rr);
+        rr->soa.mname = checked_strdup("example.com");
+        rr->soa.rname = checked_strdup("root.example.com");
+        rr->soa.serial = 1;
+        rr->soa.refresh = 1;
+        rr->soa.retry = 1;
+        rr->soa.expire = 1;
+        rr->soa.minimum = 3600;
+        ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
+}
+
 /* ================================================================
  * dns_cache_dump_to_json()
  * ================================================================ */