summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Coglan <james@neighbourhood.ie>2024-05-21 17:26:31 +0200
committerJames Coglan <james@neighbourhood.ie>2024-07-23 15:08:31 +0200
commit186184ad14ca8aa685ae7da2d06b47134d474580 (patch)
tree95a698a3156e524edc34f01b79a547d394063036
parentresolved: tests for dns_cache_put() with non-matching class, type, name (diff)
downloadsystemd-186184ad14ca8aa685ae7da2d06b47134d474580.tar.xz
systemd-186184ad14ca8aa685ae7da2d06b47134d474580.zip
resolved: tests for dns_cache_put(); CNAME success and name error
-rw-r--r--src/resolve/test-dns-cache.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/resolve/test-dns-cache.c b/src/resolve/test-dns-cache.c
index 9ec5abd0d3..f8d88403ac 100644
--- a/src/resolve/test-dns-cache.c
+++ b/src/resolve/test-dns-cache.c
@@ -93,6 +93,17 @@ static void answer_add_a(PutArgs *args, DnsResourceKey *key, int addr, int ttl,
dns_answer_add(args->answer, rr, 1, flags, NULL);
}
+static void answer_add_cname(PutArgs *args, DnsResourceKey *key, const char *alias, int ttl, DnsAnswerFlags flags) {
+ _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
+
+ rr = dns_resource_record_new(key);
+ ASSERT_NOT_NULL(rr);
+ rr->cname.name = strdup(alias);
+ ASSERT_NOT_NULL(rr->cname.name);
+ rr->ttl = ttl;
+ dns_answer_add(args->answer, rr, 1, flags, NULL);
+}
+
TEST(dns_a_success_is_cached) {
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
@@ -232,4 +243,38 @@ TEST(dns_a_success_not_cacheable_is_not_cached) {
ASSERT_TRUE(dns_cache_is_empty(&cache));
}
+TEST(dns_a_to_cname_success_is_cached) {
+ _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);
+
+ ASSERT_OK(cache_put(&cache, &put_args));
+ ASSERT_FALSE(dns_cache_is_empty(&cache));
+}
+
+TEST(dns_a_to_cname_success_escaped_name_returns_error) {
+ _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);
+
+ ASSERT_ERROR(cache_put(&cache, &put_args), EINVAL);
+ ASSERT_TRUE(dns_cache_is_empty(&cache));
+}
+
DEFINE_TEST_MAIN(LOG_DEBUG);