diff options
author | James Coglan <james@neighbourhood.ie> | 2024-06-24 15:23:23 +0200 |
---|---|---|
committer | Luca Boccassi <bluca@debian.org> | 2024-07-23 16:44:07 +0200 |
commit | 6fd7fc92cf322a35bb0527f37fa12d30d34b9b97 (patch) | |
tree | 828d6b7c01283c3f7fbdb3a1c1fa1f3faf18f05d /src/resolve/test-dns-synthesize.c | |
parent | sd-dhcp-server: persist hardware addresses in leases (diff) | |
download | systemd-6fd7fc92cf322a35bb0527f37fa12d30d34b9b97.tar.xz systemd-6fd7fc92cf322a35bb0527f37fa12d30d34b9b97.zip |
resolved: tests for dns_synthesize_answer()
Diffstat (limited to '')
-rw-r--r-- | src/resolve/test-dns-synthesize.c | 285 |
1 files changed, 285 insertions, 0 deletions
diff --git a/src/resolve/test-dns-synthesize.c b/src/resolve/test-dns-synthesize.c new file mode 100644 index 0000000000..5b89e962d2 --- /dev/null +++ b/src/resolve/test-dns-synthesize.c @@ -0,0 +1,285 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "log.h" +#include "resolved-dns-answer.h" +#include "resolved-dns-question.h" +#include "resolved-dns-rr.h" +#include "resolved-dns-synthesize.h" +#include "resolved-manager.h" +#include "tests.h" + +/* ================================================================ + * dns_synthesize_answer() + * ================================================================ */ + +TEST(dns_synthesize_answer_empty) { + Manager manager = {}; + _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; + + question = dns_question_new(1); + ASSERT_NOT_NULL(question); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); + ASSERT_NOT_NULL(key); + + ASSERT_OK(dns_question_add(question, key, 0)); + + answer = dns_answer_new(0); + ASSERT_NOT_NULL(answer); + + ASSERT_FALSE(dns_synthesize_answer(&manager, question, 0, &answer)); + ASSERT_TRUE(dns_answer_isempty(answer)); +} + +TEST(dns_synthesize_answer_reverse) { + Manager manager = {}; + _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; + + question = dns_question_new(1); + ASSERT_NOT_NULL(question); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "127.0.0.0.in-addr.arpa"); + ASSERT_NOT_NULL(key); + + ASSERT_OK(dns_question_add(question, key, 0)); + + answer = dns_answer_new(0); + ASSERT_NOT_NULL(answer); + + ASSERT_ERROR(dns_synthesize_answer(&manager, question, 0, &answer), ENXIO); + ASSERT_TRUE(dns_answer_isempty(answer)); +} + +TEST(dns_synthesize_answer_localhost) { + Manager manager = {}; + _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; + _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; + + question = dns_question_new(1); + ASSERT_NOT_NULL(question); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "localhost"); + ASSERT_NOT_NULL(key); + + ASSERT_OK(dns_question_add(question, key, 0)); + + ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); + + rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "localhost"); + ASSERT_NOT_NULL(rr); + rr->a.in_addr.s_addr = htobe32(0x7f000001); + + ASSERT_TRUE(dns_answer_contains(answer, rr)); +} + +TEST(dns_synthesize_answer_own_hostname) { + Manager manager = {}; + _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; + _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; + + question = dns_question_new(1); + ASSERT_NOT_NULL(question); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "resolver.local"); + ASSERT_NOT_NULL(key); + + ASSERT_OK(dns_question_add(question, key, 0)); + + manager.full_hostname = (char *)"resolver.local"; + + ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); + + rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "resolver.local"); + ASSERT_NOT_NULL(rr); + + ASSERT_TRUE(dns_answer_match_key(answer, rr->key, NULL)); +} + +TEST(dns_synthesize_answer_stub) { + Manager manager = {}; + _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; + _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; + + question = dns_question_new(1); + ASSERT_NOT_NULL(question); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "_localdnsstub"); + ASSERT_NOT_NULL(key); + + ASSERT_OK(dns_question_add(question, key, 0)); + + ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); + + rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "_localdnsstub"); + ASSERT_NOT_NULL(rr); + + ASSERT_TRUE(dns_answer_match_key(answer, rr->key, NULL)); +} + +TEST(dns_synthesize_answer_localhost_ptr) { + Manager manager = {}; + _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; + _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; + + question = dns_question_new(1); + ASSERT_NOT_NULL(question); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "1.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(key); + + ASSERT_OK(dns_question_add(question, key, 0)); + + ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); + + rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "1.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(rr); + + rr->ptr.name = strdup("localhost"); + ASSERT_NOT_NULL(rr->ptr.name); + ASSERT_TRUE(dns_answer_contains(answer, rr)); +} + +TEST(dns_synthesize_answer_address) { + Manager manager = {}; + _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; + + question = dns_question_new(1); + ASSERT_NOT_NULL(question); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "0.1.254.169.in-addr.arpa"); + ASSERT_NOT_NULL(key); + + ASSERT_OK(dns_question_add(question, key, 0)); + + manager.full_hostname = (char *)"resolver.local"; + manager.llmnr_hostname = (char *)"llmnr.resolver.local"; + manager.mdns_hostname = (char *)"mdns.resolver.local"; + + answer = dns_answer_new(0); + ASSERT_NOT_NULL(answer); + + ASSERT_FALSE(dns_synthesize_answer(&manager, question, 0, &answer)); + ASSERT_TRUE(dns_answer_isempty(answer)); +} + +TEST(dns_synthesize_answer_address_local_hostname) { + Manager manager = {}; + _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; + DnsResourceRecord *rr = NULL; + + question = dns_question_new(1); + ASSERT_NOT_NULL(question); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "2.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(key); + + ASSERT_OK(dns_question_add(question, key, 0)); + + manager.full_hostname = (char *)"resolver.local"; + manager.llmnr_hostname = (char *)"llmnr.resolver.local"; + manager.mdns_hostname = (char *)"mdns.resolver.local"; + + ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); + + rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "2.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(rr); + rr->ptr.name = strdup("resolver.local"); + ASSERT_NOT_NULL(rr->ptr.name); + ASSERT_TRUE(dns_answer_contains(answer, rr)); + dns_resource_record_unref(rr); + + rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "2.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(rr); + rr->ptr.name = strdup("llmnr.resolver.local"); + ASSERT_NOT_NULL(rr->ptr.name); + ASSERT_TRUE(dns_answer_contains(answer, rr)); + dns_resource_record_unref(rr); + + rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "2.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(rr); + rr->ptr.name = strdup("mdns.resolver.local"); + ASSERT_NOT_NULL(rr->ptr.name); + ASSERT_TRUE(dns_answer_contains(answer, rr)); + dns_resource_record_unref(rr); + + rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "2.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(rr); + rr->ptr.name = strdup("localhost"); + ASSERT_NOT_NULL(rr->ptr.name); + ASSERT_TRUE(dns_answer_contains(answer, rr)); + dns_resource_record_unref(rr); +} + +TEST(dns_synthesize_answer_address_local_dns_stub) { + Manager manager = {}; + _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; + _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; + + question = dns_question_new(1); + ASSERT_NOT_NULL(question); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "53.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(key); + + ASSERT_OK(dns_question_add(question, key, 0)); + + manager.full_hostname = (char *)"resolver.local"; + manager.llmnr_hostname = (char *)"llmnr.resolver.local"; + manager.mdns_hostname = (char *)"mdns.resolver.local"; + + ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); + + rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "53.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(rr); + rr->ptr.name = strdup("_localdnsstub"); + ASSERT_NOT_NULL(rr->ptr.name); + ASSERT_TRUE(dns_answer_contains(answer, rr)); +} + +TEST(dns_synthesize_answer_address_local_dns_proxy) { + Manager manager = {}; + _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; + _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; + _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; + + question = dns_question_new(1); + ASSERT_NOT_NULL(question); + + key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "54.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(key); + + ASSERT_OK(dns_question_add(question, key, 0)); + + manager.full_hostname = (char *)"resolver.local"; + manager.llmnr_hostname = (char *)"llmnr.resolver.local"; + manager.mdns_hostname = (char *)"mdns.resolver.local"; + + ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); + + rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "54.0.0.127.in-addr.arpa"); + ASSERT_NOT_NULL(rr); + rr->ptr.name = strdup("_localdnsproxy"); + ASSERT_NOT_NULL(rr->ptr.name); + ASSERT_TRUE(dns_answer_contains(answer, rr)); +} + +DEFINE_TEST_MAIN(LOG_DEBUG); |