From: James Coglan Date: Wed, 29 May 2024 12:51:31 +0000 (+0100) Subject: resolved: tests for dns_resource_key_reduce() X-Git-Tag: v257-rc1~843^2~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6104ba9d42bc508d4003f4d2d29d3d1753cb67ce;p=thirdparty%2Fsystemd.git resolved: tests for dns_resource_key_reduce() --- diff --git a/src/resolve/test-dns-rr.c b/src/resolve/test-dns-rr.c index 28b2a357c4f..fe3bc3666a8 100644 --- a/src/resolve/test-dns-rr.c +++ b/src/resolve/test-dns-rr.c @@ -696,4 +696,73 @@ TEST(dns_resource_key_to_string) { ASSERT_STREQ(ans, "www.example.com IN CNAME"); } +/* ================================================================ + * dns_resource_key_reduce() + * ================================================================ */ + +TEST(dns_resource_key_reduce_same_pointer) { + DnsResourceKey *a = NULL, *b = NULL; + + a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); + ASSERT_NOT_NULL(a); + b = a; + + ASSERT_TRUE(dns_resource_key_reduce(&a, &b)); + ASSERT_TRUE(a == b); + + dns_resource_key_unref(a); +} + +TEST(dns_resource_key_reduce_same_values) { + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL; + + a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); + ASSERT_NOT_NULL(a); + b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); + ASSERT_NOT_NULL(b); + + ASSERT_TRUE(a != b); + + ASSERT_TRUE(dns_resource_key_reduce(&a, &b)); + ASSERT_TRUE(a == b); +} + +TEST(dns_resource_key_reduce_different_values) { + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL; + + a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); + ASSERT_NOT_NULL(a); + b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); + ASSERT_NOT_NULL(b); + + ASSERT_TRUE(a != b); + + ASSERT_FALSE(dns_resource_key_reduce(&a, &b)); + ASSERT_TRUE(a != b); +} + +TEST(dns_resource_key_reduce_refcount) { + _cleanup_(dns_resource_key_unrefp) DnsResourceKey *a = NULL, *b = NULL, *c = NULL; + + a = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); + ASSERT_NOT_NULL(a); + b = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); + ASSERT_NOT_NULL(b); + c = b; + + ASSERT_TRUE(a != b); + + a->n_ref = 3; + b->n_ref = 2; + + ASSERT_TRUE(dns_resource_key_reduce(&a, &b)); + ASSERT_TRUE(a == b); + + ASSERT_EQ(a->n_ref, 4u); + ASSERT_EQ(c->n_ref, 1u); + + /* set refcount so that objects will be freed */ + a->n_ref = 2; +} + DEFINE_TEST_MAIN(LOG_DEBUG);