HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/resolve/test-dns-rr.c

97,637 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 18361be85b63b1af23a3dbb459cc4a82a80fd337

Lines 2001–2555 of 2,555

        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
 
        a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
        ASSERT_NOT_NULL(a);
        a->rrsig.type_covered = DNS_TYPE_A;
        a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
        a->rrsig.labels = 3;
        a->rrsig.original_ttl = 3600;
        a->rrsig.expiration = 1720361303;
        a->rrsig.inception = 1717769303;
        a->rrsig.key_tag = 0x1234;
        a->rrsig.signer = strdup("example.com");
 
        const uint8_t signature[] = {
                0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
        };
        a->rrsig.signature_size = sizeof(signature);
        a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
 
        b = dns_resource_record_copy(a);
        ASSERT_NOT_NULL(b);
        free(b->rrsig.signer);
        b->rrsig.signer = strdup("www.example.com");
        ASSERT_FALSE(dns_resource_record_equal(a, b));
}
 
TEST(dns_resource_record_equal_rrsig_bad_signature) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
 
        a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
        ASSERT_NOT_NULL(a);
        a->rrsig.type_covered = DNS_TYPE_A;
        a->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
        a->rrsig.labels = 3;
        a->rrsig.original_ttl = 3600;
        a->rrsig.expiration = 1720361303;
        a->rrsig.inception = 1717769303;
        a->rrsig.key_tag = 0x1234;
        a->rrsig.signer = strdup("example.com");
 
        const uint8_t signature[] = {
                0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
        };
        a->rrsig.signature_size = sizeof(signature);
        a->rrsig.signature = memdup(signature, a->rrsig.signature_size);
 
        b = dns_resource_record_copy(a);
        ASSERT_NOT_NULL(b);
        b->rrsig.signature_size -= 1;
        ASSERT_FALSE(dns_resource_record_equal(a, b));
}
 
/* ================================================================
 * dns_resource_record_equal() : SVCB
 * ================================================================ */
 
static DnsSvcParam* add_svcb_param(DnsResourceRecord *rr, uint16_t key, const char *value, size_t len) {
        DnsSvcParam *param = calloc(1, offsetof(DnsSvcParam, value) + len);
        ASSERT_NOT_NULL(param);
 
        param->key = key;
        param->length = len;
 
        if (value)
                memcpy(param->value, value, len);
 
        LIST_APPEND(params, rr->svcb.params, param);
        return param;
}
 
TEST(dns_resource_record_equal_svcb_copy) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
 
        a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
        ASSERT_NOT_NULL(a);
        a->svcb.priority = 9;
        a->svcb.target_name = strdup("sock.example.com");
 
        add_svcb_param(a, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
        add_svcb_param(a, 99, NULL, 0);
 
        b = dns_resource_record_copy(a);
        ASSERT_NOT_NULL(b);
        ASSERT_TRUE(dns_resource_record_equal(a, b));
}
 
TEST(dns_resource_record_equal_svcb_bad_priority) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
 
        a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
        ASSERT_NOT_NULL(a);
        a->svcb.priority = 9;
        a->svcb.target_name = strdup("sock.example.com");
 
        b = dns_resource_record_copy(a);
        ASSERT_NOT_NULL(b);
        b->svcb.priority = 8;
        ASSERT_FALSE(dns_resource_record_equal(a, b));
}
 
TEST(dns_resource_record_equal_svcb_bad_target_name) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
 
        a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
        ASSERT_NOT_NULL(a);
        a->svcb.priority = 9;
        a->svcb.target_name = strdup("sock.example.com");
 
        b = dns_resource_record_copy(a);
        ASSERT_NOT_NULL(b);
        free(b->svcb.target_name);
        b->svcb.target_name = strdup("other.example.com");
        ASSERT_FALSE(dns_resource_record_equal(a, b));
}
 
TEST(dns_resource_record_equal_svcb_param_missing) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
 
        a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
        ASSERT_NOT_NULL(a);
        a->svcb.priority = 9;
        a->svcb.target_name = strdup("sock.example.com");
 
        b = dns_resource_record_copy(a);
        ASSERT_NOT_NULL(b);
 
        add_svcb_param(a, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
        add_svcb_param(a, 99, NULL, 0);
 
        add_svcb_param(b, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
 
        ASSERT_FALSE(dns_resource_record_equal(a, b));
}
 
TEST(dns_resource_record_equal_svcb_param_extra) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
 
        a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
        ASSERT_NOT_NULL(a);
        a->svcb.priority = 9;
        a->svcb.target_name = strdup("sock.example.com");
 
        b = dns_resource_record_copy(a);
        ASSERT_NOT_NULL(b);
 
        add_svcb_param(a, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
 
        add_svcb_param(b, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
        add_svcb_param(b, 99, NULL, 0);
 
        ASSERT_FALSE(dns_resource_record_equal(a, b));
}
 
TEST(dns_resource_record_equal_svcb_param_different) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *a = NULL, *b = NULL;
 
        a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
        ASSERT_NOT_NULL(a);
        a->svcb.priority = 9;
        a->svcb.target_name = strdup("sock.example.com");
 
        b = dns_resource_record_copy(a);
        ASSERT_NOT_NULL(b);
 
        add_svcb_param(a, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
        add_svcb_param(b, DNS_SVC_PARAM_KEY_ALPN, "\x04xmpp", 5);
 
        ASSERT_FALSE(dns_resource_record_equal(a, b));
}
 
/* ================================================================
 * dns_resource_record_to_string()
 * ================================================================ */
 
TEST(dns_resource_record_to_string_a) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *str;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
        ASSERT_NOT_NULL(rr);
        rr->a.in_addr.s_addr = htobe32(0xc0a8017f);
 
        str = dns_resource_record_to_string(rr);
        ASSERT_STREQ(str, "www.example.com IN A 192.168.1.127");
}
 
TEST(dns_resource_record_to_string_aaaa) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *str;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
        ASSERT_NOT_NULL(rr);
        rr->aaaa.in6_addr = (struct in6_addr) {
                .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03 }
        };
 
        str = dns_resource_record_to_string(rr);
        ASSERT_STREQ(str, "www.example.com IN AAAA ff02::1:3");
}
 
TEST(dns_resource_record_to_string_ns) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *str;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NS, "www.example.com");
        ASSERT_NOT_NULL(rr);
        rr->ns.name = strdup("ns1.example.com");
 
        str = dns_resource_record_to_string(rr);
        ASSERT_STREQ(str, "www.example.com IN NS ns1.example.com");
}
 
TEST(dns_resource_record_to_string_cname) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *str;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
        ASSERT_NOT_NULL(rr);
        rr->ns.name = strdup("example.com");
 
        str = dns_resource_record_to_string(rr);
        ASSERT_STREQ(str, "www.example.com IN CNAME example.com");
}
 
TEST(dns_resource_record_to_string_soa) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *str;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
        ASSERT_NOT_NULL(rr);
        rr->soa.mname = strdup("ns0.example.com");
        rr->soa.rname = strdup("ns0.example.com");
        rr->soa.serial = 1111111111;
        rr->soa.refresh = 86400;
        rr->soa.retry = 7200;
        rr->soa.expire = 4000000;
        rr->soa.minimum = 3600;
 
        str = dns_resource_record_to_string(rr);
        ASSERT_STREQ(str, "www.example.com IN SOA ns0.example.com ns0.example.com 1111111111 86400 7200 4000000 3600");
}
 
TEST(dns_resource_record_to_json_soa) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "www.example.com");
        ASSERT_NOT_NULL(rr);
        rr->soa.mname = strdup("ns0.example.com");
        rr->soa.rname = strdup("hostmaster.example.com");
        rr->soa.serial = 1111111111;
        rr->soa.refresh = 86400;
        rr->soa.retry = 7200;
        rr->soa.expire = 4000000;
        rr->soa.minimum = 3600;
 
        ASSERT_OK(dns_resource_record_to_json(rr, &j));
        ASSERT_STREQ(sd_json_variant_string(sd_json_variant_by_key(j, "mname")), "ns0.example.com");
        ASSERT_STREQ(sd_json_variant_string(sd_json_variant_by_key(j, "rname")), "hostmaster.example.com");
        ASSERT_EQ(sd_json_variant_unsigned(sd_json_variant_by_key(j, "serial")), UINT64_C(1111111111));
        ASSERT_EQ(sd_json_variant_unsigned(sd_json_variant_by_key(j, "refresh")), UINT64_C(86400));
        ASSERT_EQ(sd_json_variant_unsigned(sd_json_variant_by_key(j, "retry")), UINT64_C(7200));
        ASSERT_EQ(sd_json_variant_unsigned(sd_json_variant_by_key(j, "expire")), UINT64_C(4000000));
        ASSERT_EQ(sd_json_variant_unsigned(sd_json_variant_by_key(j, "minimum")), UINT64_C(3600));
}
 
TEST(dns_resource_record_to_json_openpgpkey) {
        static const uint8_t data[] = { 0, 1, 2, 3 };
 
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr =
                ASSERT_NOT_NULL(dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_OPENPGPKEY, "www.example.com"));
        ASSERT_NOT_NULL(rr->generic.data = memdup(data, sizeof(data)));
        rr->generic.data_size = sizeof(data);
 
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
        ASSERT_OK(dns_resource_record_to_json(rr, &j));
 
        sd_json_variant *key = ASSERT_NOT_NULL(sd_json_variant_by_key(j, "key"));
        ASSERT_EQ(sd_json_variant_unsigned(sd_json_variant_by_key(key, "type")), (uint64_t) DNS_TYPE_OPENPGPKEY);
        ASSERT_STREQ(sd_json_variant_string(sd_json_variant_by_key(j, "data")), "AAECAw==");
}
 
TEST(dns_resource_record_to_string_ptr) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *str;
 
        union in_addr_union addr = { .in.s_addr = htobe32(0xc0a8017f) };
 
        ASSERT_OK(dns_resource_record_new_reverse(&rr, AF_INET, &addr, "www.example.com"));
        ASSERT_NOT_NULL(rr);
 
        str = dns_resource_record_to_string(rr);
        ASSERT_STREQ(str, "127.1.168.192.in-addr.arpa IN PTR www.example.com");
}
 
TEST(dns_resource_record_to_string_hinfo) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *str;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_HINFO, "www.example.com");
        ASSERT_NOT_NULL(rr);
        rr->hinfo.cpu = strdup("Intel x64");
        rr->hinfo.os = strdup("GNU/Linux");
 
        str = dns_resource_record_to_string(rr);
        ASSERT_STREQ(str, "www.example.com IN HINFO Intel x64 GNU/Linux");
}
 
TEST(dns_resource_record_to_string_mx) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *str;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_MX, "mail.example.com");
        ASSERT_NOT_NULL(rr);
        rr->mx.priority = 6;
        rr->mx.exchange= strdup("exchange.example.com");
 
        str = dns_resource_record_to_string(rr);
        ASSERT_STREQ(str, "mail.example.com IN MX 6 exchange.example.com");
}
 
TEST(dns_resource_record_to_string_srv) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *str;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SRV, "_ldap._tcp.example.com");
        ASSERT_NOT_NULL(rr);
        rr->ttl = 3601;
        rr->srv.priority = 17185;
        rr->srv.weight = 25976;
        rr->srv.port = 389;
        rr->srv.name = strdup("cloud.example.com");
 
        str = dns_resource_record_to_string(rr);
        ASSERT_STREQ(str, "_ldap._tcp.example.com IN SRV 17185 25976 389 cloud.example.com");
}
 
TEST(dns_resource_record_to_string_svcb) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        DnsSvcParam *param = NULL;
        const char *str;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
        ASSERT_NOT_NULL(rr);
        rr->ttl = 3601;
        rr->svcb.priority = 9;
        rr->svcb.target_name = strdup("sock.example.com");
 
        add_svcb_param(rr, DNS_SVC_PARAM_KEY_MANDATORY, "\x00\x01\x00\x03", 4);
        add_svcb_param(rr, DNS_SVC_PARAM_KEY_ALPN, "\x09websocket", 10);
        add_svcb_param(rr, DNS_SVC_PARAM_KEY_NO_DEFAULT_ALPN, NULL, 0);
        add_svcb_param(rr, DNS_SVC_PARAM_KEY_PORT, "\x01\xbb", 2);
 
        param = add_svcb_param(rr, DNS_SVC_PARAM_KEY_IPV4HINT, NULL, 2 * sizeof(struct in_addr));
        param->value_in_addr[0].s_addr = htobe32(0x7284fd3a);
        param->value_in_addr[1].s_addr = htobe32(0x48bcc7c0);
 
        param = add_svcb_param(rr, DNS_SVC_PARAM_KEY_IPV6HINT, NULL, sizeof(struct in6_addr));
        param->value_in6_addr[0] = (struct in6_addr) { .s6_addr = { 0xf2, 0x34, 0x32, 0x2e, 0xb8, 0x25, 0x38, 0x35, 0x2f, 0xd7, 0xdb, 0x7b, 0x28, 0x7e, 0x60, 0xbb } };
 
        /* undefined key */
        add_svcb_param(rr, 99, NULL, 0);
 
        str = dns_resource_record_to_string(rr);
        ASSERT_STREQ(str, "_443._wss.example.com IN SVCB 9 sock.example.com mandatory=\"\\000\\001\\000\\003\" alpn=\"websocket\" no-default-alpn port=443 ipv4hint=114.132.253.58,72.188.199.192 ipv6hint=f234:322e:b825:3835:2fd7:db7b:287e:60bb key99");
}
 
/* ================================================================
 * dns_resource_record_to_wire_format()
 * ================================================================ */
 
TEST(dns_resource_record_to_wire_format) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        int r;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
        ASSERT_NOT_NULL(rr);
        rr->ttl = 3600;
        rr->cname.name = strdup("example.com");
 
        ASSERT_OK(dns_resource_record_to_wire_format(rr, true));
 
        const uint8_t data[] = {
        /* name */      0x03, 'w', 'w', 'w',
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00,
        /* CNAME */     0x00, 0x05,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x0d,
        /* name */      0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00
        };
 
        ASSERT_EQ(rr->wire_format_size, sizeof(data));
        ASSERT_EQ(rr->wire_format_rdata_offset, 27u);
        ASSERT_EQ(rr->wire_format_canonical, true);
 
        r = memcmp(rr->wire_format, data, sizeof(data));
        ASSERT_EQ(r, 0);
}
 
/* ================================================================
 * dns_resource_record_signer()
 * dns_resource_record_is_signer()
 * dns_resource_record_source()
 * dns_resource_record_is_synthetic()
 * ================================================================ */
 
static void prepare_rrsig(DnsResourceRecord **rr) {
        *rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_RRSIG, "www.example.com");
        ASSERT_NOT_NULL(*rr);
 
        (*rr)->ttl = 3600;
        (*rr)->rrsig.type_covered = DNS_TYPE_A;
        (*rr)->rrsig.algorithm = DNSSEC_ALGORITHM_ECC;
        (*rr)->rrsig.labels = 1;
        (*rr)->rrsig.original_ttl = 3600;
        (*rr)->rrsig.expiration = 1720361303;
        (*rr)->rrsig.inception = 1717769303;
        (*rr)->rrsig.key_tag = 0x1234;
        (*rr)->rrsig.signer = strdup("example.com");
 
        /* normally set by dnssec_rrsig_prepare() during dnssec_verify_rrset() */
        (*rr)->n_skip_labels_signer = 1;
        (*rr)->n_skip_labels_source = 2;
}
 
TEST(dns_resource_record_signer) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *signer = NULL;
 
        prepare_rrsig(&rr);
 
        ASSERT_OK(dns_resource_record_signer(rr, &signer));
        ASSERT_STREQ(signer, "example.com");
}
 
TEST(dns_resource_record_is_signer) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
 
        prepare_rrsig(&rr);
 
        ASSERT_TRUE(dns_resource_record_is_signer(rr, "example.com"));
        ASSERT_FALSE(dns_resource_record_is_signer(rr, "www.example.com"));
        ASSERT_FALSE(dns_resource_record_is_signer(rr, "com"));
}
 
TEST(dns_resource_record_source) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
        const char *source = NULL;
 
        prepare_rrsig(&rr);
 
        ASSERT_OK(dns_resource_record_source(rr, &source));
        ASSERT_STREQ(source, "com");
}
 
TEST(dns_resource_record_is_synthetic) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
 
        prepare_rrsig(&rr);
 
        ASSERT_TRUE(dns_resource_record_is_synthetic(rr));
 
        rr->n_skip_labels_source = -1;
        ASSERT_ERROR(dns_resource_record_is_synthetic(rr), ENODATA);
 
        rr->n_skip_labels_source = 0;
        ASSERT_FALSE(dns_resource_record_is_synthetic(rr));
 
        rr->n_skip_labels_source = 1;
        ASSERT_TRUE(dns_resource_record_is_synthetic(rr));
 
        rr->key->_name = strdup("*.example.com");
        ASSERT_FALSE(dns_resource_record_is_synthetic(rr));
}
 
/* ================================================================
 * dns_resource_record_clamp_ttl()
 * ================================================================ */
 
TEST(dns_resource_record_clamp_ttl_in_place) {
        _cleanup_free_ void *wire_format = NULL;
        DnsResourceRecord *rr = NULL, *orig = NULL;
        size_t wire_format_size;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
        ASSERT_NOT_NULL(rr);
        orig = rr;
        rr->ttl = 3600;
        rr->a.in_addr.s_addr = htobe32(0xc0a8017f);
 
        ASSERT_FALSE(dns_resource_record_clamp_ttl(&rr, 4800));
        ASSERT_EQ(rr->ttl, 3600u);
 
        ASSERT_OK(dns_resource_record_to_wire_format(rr, false));
        ASSERT_NOT_NULL(rr->wire_format);
        wire_format_size = rr->wire_format_size;
        ASSERT_NOT_NULL(wire_format = memdup(rr->wire_format, wire_format_size));
 
        ASSERT_TRUE(dns_resource_record_clamp_ttl(&rr, 2400));
        ASSERT_EQ(rr->ttl, 2400u);
        ASSERT_OK(dns_resource_record_to_wire_format(rr, false));
        ASSERT_EQ(rr->wire_format_size, wire_format_size);
        ASSERT_NE(memcmp(rr->wire_format, wire_format, wire_format_size), 0);
 
        ASSERT_TRUE(rr == orig);
 
        dns_resource_record_unref(rr);
}
 
TEST(dns_resource_record_clamp_ttl_copy) {
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL, *orig = NULL;
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
        ASSERT_NOT_NULL(rr);
        orig = dns_resource_record_ref(rr);
        rr->ttl = 3600;
 
        ASSERT_FALSE(dns_resource_record_clamp_ttl(&rr, 4800));
        ASSERT_EQ(rr->ttl, 3600u);
 
        ASSERT_TRUE(dns_resource_record_clamp_ttl(&rr, 2400));
        ASSERT_EQ(rr->ttl, 2400u);
 
        ASSERT_FALSE(rr == orig);
        ASSERT_EQ(orig->ttl, 3600u);
}
 
static void test_from_json(const char *text, int expected) {
        log_notice("Trying to parse as JSON RR: %s", text);
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
        ASSERT_OK(sd_json_parse(text, /* flags= */ 0, &j, /* reterr_line= */ NULL, /* reterr_column= */ NULL));
        ASSERT_EQ(dns_resource_record_from_json(j, NULL), expected);
}
 
TEST(from_bad_json) {
        test_from_json("{}", -EBADMSG);
        test_from_json("{\"key\":{}}", -ENXIO);
        test_from_json("{\"key\":{\"name\":\"foobar\",\"type\":9}}", -EOPNOTSUPP);
        test_from_json("{\"key\":{\"name\":\"foobar\"}}", -ENXIO);
        test_from_json("{\"key\":{\"type\":9}}", -ENXIO);
        test_from_json("{\"key\":{\"name\":\"foobar\",\"type\":1}}", -ENXIO);
        test_from_json("{\"key\":{\"name\":\"foobar\",\"type\":1},\"address\":[1,2,3,4]}", 0);
        test_from_json("{\"key\":{\"name\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"type\":1},\"address\":[1,2,3,4]}", 0);
        test_from_json("{\"key\":{\"name\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"type\":1},\"address\":[1,2,3,4]}", -EBADMSG);
        test_from_json("{\"key\":{\"name\":\"a.a\",\"type\":1},\"address\":[1,2,3,4]}", 0);
        test_from_json("{\"key\":{\"name\":\"a..a\",\"type\":1},\"address\":[1,2,3,4]}", -EBADMSG);
}
 
DEFINE_TEST_MAIN(LOG_DEBUG);