]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolved: tests for dns_resource_key_reduce()
authorJames Coglan <james@neighbourhood.ie>
Wed, 29 May 2024 12:51:31 +0000 (13:51 +0100)
committerLuca Boccassi <bluca@debian.org>
Tue, 23 Jul 2024 11:44:35 +0000 (12:44 +0100)
src/resolve/test-dns-rr.c

index 28b2a357c4f8d12cdd14f5e5b159e9babb837b9e..fe3bc3666a8449ea6209d1ee574425b4b6354064 100644 (file)
@@ -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);