HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/resolve/test-dns-packet-extract.c

193,515 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 2c053c299f792d2a2ea4822d20b93e55764e9d0b

Lines 3001–4000 of 4,963

                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x02,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x7a,
        /* version */   0x00,
        /* flags */     0x00, 0x00,
        /* rdata */     0x00, 0x00,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x7a,
        /* version */   0x00,
        /* flags */     0x00, 0x00,
        /* rdata */     0x00, 0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
        ASSERT_NULL(packet->opt);
}
 
TEST(packet_reply_opt_not_root) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x03, 'c', 'o', 'm',
                        0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x00, 0x00,
        /* rdata */     0x00, 0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
        ASSERT_NULL(packet->opt);
}
 
TEST(packet_reply_opt_wrong_section) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x01,     0x00, 0x00,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x00, 0x00,
        /* rdata */     0x00, 0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
        ASSERT_NULL(packet->opt);
}
 
TEST(packet_query_opt_version_ok) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        DnsResourceRecord *rr = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     0x00, 0x00,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x01,
        /* flags */     0x00, 0x00,
        /* rdata */     0x00, 0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
 
        rr = dns_resource_record_new_full(513, DNS_TYPE_OPT, "");
        ASSERT_NOT_NULL(rr);
        rr->ttl = 65536;
 
        ASSERT_TRUE(dns_resource_record_equal(packet->opt, rr));
        dns_resource_record_unref(rr);
 
        ASSERT_EQ(dns_packet_payload_size_max(packet), 513u);
        ASSERT_EQ(dns_packet_do(packet), 0u);
}
 
TEST(packet_reply_opt_version_bad) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x01,
        /* flags */     0x00, 0x00,
        /* rdata */     0x00, 0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_ERROR(dns_packet_extract(packet), EBADMSG);
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
        ASSERT_NULL(packet->opt);
}
 
TEST(packet_reply_opt_with_do) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        DnsResourceRecord *rr = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x10, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
 
        rr = dns_resource_record_new_full(4097, DNS_TYPE_OPT, "");
        ASSERT_NOT_NULL(rr);
        rr->ttl = 32768;
 
        ASSERT_TRUE(dns_resource_record_equal(packet->opt, rr));
        dns_resource_record_unref(rr);
 
        ASSERT_EQ(dns_packet_payload_size_max(packet), 4097u);
        ASSERT_EQ(dns_packet_do(packet), 1u);
}
 
TEST(packet_reply_opt_with_data) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        DnsResourceRecord *rr = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x00, 0x00,
        /* rdata */     0x00, 0x12,
                        0x00, 0x01,
                        0x00, 0x05,
                        'h', 'e', 'l', 'l', 'o',
                        0x00, 0x02,
                        0x00, 0x05,
                        'w', 'o', 'r', 'l', 'd'
        };
 
        const uint8_t opt_data[] = {
                        0x00, 0x01,
                        0x00, 0x05,
                        'h', 'e', 'l', 'l', 'o',
                        0x00, 0x02,
                        0x00, 0x05,
                        'w', 'o', 'r', 'l', 'd'
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
 
        rr = dns_resource_record_new_full(513, DNS_TYPE_OPT, "");
        ASSERT_NOT_NULL(rr);
        rr->ttl = 0;
        rr->opt.data_size = sizeof(opt_data);
        rr->opt.data = calloc(rr->opt.data_size, sizeof(uint8_t));
        ASSERT_NOT_NULL(rr->opt.data);
        memcpy(rr->opt.data, opt_data, sizeof(opt_data));
 
        ASSERT_TRUE(dns_resource_record_equal(packet->opt, rr));
        dns_resource_record_unref(rr);
}
 
TEST(packet_reply_opt_bad_data_size) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x00, 0x00,
        /* rdata */     0x00, 0x13,
                        0x00, 0x01,
                        0x00, 0x05,
                        'h', 'e', 'l', 'l', 'o',
                        0x00, 0x02,
                        0x00, 0x05,
                        'w', 'o', 'r', 'l', 'd'
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_ERROR(dns_packet_extract(packet), EBADMSG);
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
        ASSERT_NULL(packet->opt);
}
 
TEST(packet_query_opt_with_rfc6975_data) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        DnsResourceRecord *rr = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     0x00, 0x00,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x00, 0x00,
        /* rdata */     0x00, 0x05,
                        0x00, 0x05,     /* RFC 6975 defines codes 5, 6, 7 */
                        0x00, 0x01,
                        0xff
        };
 
        const uint8_t opt_data[] = { 0x00, 0x05, 0x00, 0x01, 0xff };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
 
        rr = dns_resource_record_new_full(513, DNS_TYPE_OPT, "");
        ASSERT_NOT_NULL(rr);
        rr->ttl = 0;
        rr->opt.data_size = sizeof(opt_data);
        rr->opt.data = calloc(rr->opt.data_size, sizeof(uint8_t));
        ASSERT_NOT_NULL(rr->opt.data);
        memcpy(rr->opt.data, opt_data, sizeof(opt_data));
 
        ASSERT_TRUE(dns_resource_record_equal(packet->opt, rr));
        dns_resource_record_unref(rr);
}
 
TEST(packet_reply_opt_with_rfc6975_data) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x00, 0x00,
        /* rdata */     0x00, 0x05,
                        0x00, 0x05,     /* RFC 6975 defines codes 5, 6, 7 */
                        0x00, 0x01,
                        0xff
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
        ASSERT_NULL(packet->opt);
}
 
/* ================================================================
 * dns_packet_ede_rcode()
 * ================================================================ */
 
TEST(packet_ede_rcode_empty) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        int ret_ede_rcode;
        char *ret_ede_msg;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
 
        ASSERT_ERROR(dns_packet_ede_rcode(packet, &ret_ede_rcode, &ret_ede_msg), ENOENT);
}
 
TEST(packet_ede_rcode_ede_option_code) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        _cleanup_free_ char *ret_ede_msg;
        int ret_ede_rcode;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x12,
                        0x00, 0x0f,     /* extended DNS error */
                        0x00, 0x0e,     /* length */
                        0x00, 0x06,     /* DNSSEC bogus */
                        'D', 'N', 'S', 'S', 'E', 'C', ' ', 'b', 'o', 'g', 'u', 's'
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
 
        ASSERT_EQ(dns_packet_ede_rcode(packet, &ret_ede_rcode, &ret_ede_msg), 0);
        ASSERT_EQ(ret_ede_rcode, DNS_EDE_RCODE_DNSSEC_BOGUS);
        ASSERT_STREQ(ret_ede_msg, "DNSSEC bogus");
}
 
TEST(packet_ede_rcode_ede_valid_utf8) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        _cleanup_free_ char *ret_ede_msg;
        int ret_ede_rcode;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x0d,
                        0x00, 0x0f,     /* extended DNS error */
                        0x00, 0x09,     /* length */
                        0x00, 0x06,     /* DNSSEC bogus */
                        'b', 0xc3, 0xb8, 'g', 0xc3, 0xbc, 's'
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
 
        ASSERT_EQ(dns_packet_ede_rcode(packet, &ret_ede_rcode, &ret_ede_msg), 0);
        ASSERT_EQ(ret_ede_rcode, DNS_EDE_RCODE_DNSSEC_BOGUS);
        ASSERT_STREQ(ret_ede_msg, "bøgüs");
}
 
TEST(packet_ede_rcode_ede_invalid_utf8) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        _cleanup_free_ char *ret_ede_msg;
        int ret_ede_rcode;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x0d,
                        0x00, 0x0f,     /* extended DNS error */
                        0x00, 0x09,     /* length */
                        0x00, 0x06,     /* DNSSEC bogus */
                        'b', 0xc3, 0xb8, 'g', 0xc3, 0xbc, 0xff
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
 
        ASSERT_EQ(dns_packet_ede_rcode(packet, &ret_ede_rcode, &ret_ede_msg), 0);
        ASSERT_EQ(ret_ede_rcode, DNS_EDE_RCODE_DNSSEC_BOGUS);
        ASSERT_STREQ(ret_ede_msg, "b\\303\\270g\\303\\274\\377");
}
 
TEST(packet_ede_rcode_non_ede_code) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        int ret_ede_rcode;
        char *ret_ede_msg;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x12,
                        0x00, 0x01,     /* not EDE option code */
                        0x00, 0x0e,     /* length */
                        0x00, 0x06,     /* DNSSEC bogus */
                        'D', 'N', 'S', 'S', 'E', 'C', ' ', 'b', 'o', 'g', 'u', 's'
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
 
        ASSERT_ERROR(dns_packet_ede_rcode(packet, &ret_ede_rcode, &ret_ede_msg), ENOENT);
}
 
TEST(packet_ede_rcode_malformed_ede_payload) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        int ret_ede_rcode;
        char *ret_ede_msg;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x12,
                        0x00, 0x0f,     /* extended DNS error */
                        0x00, 0x0d,     /* length */
                        0x00, 0x06,     /* DNSSEC bogus */
                        'D', 'N', 'S', 'S', 'E', 'C', ' ', 'b', 'o', 'g', 'u', 's'
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
 
        ASSERT_ERROR(dns_packet_ede_rcode(packet, &ret_ede_rcode, &ret_ede_msg), ENOENT);
}
 
/* ================================================================
 * dns_packet_has_nsid_request()
 * ================================================================ */
 
TEST(packet_has_nsid_request_no_match) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
 
        ASSERT_FALSE(dns_packet_has_nsid_request(packet));
}
 
TEST(packet_has_nsid_request_match_empty_option) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x04,
                        0x00, 0x03,     /* NSID option code */
                        0x00, 0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
 
        ASSERT_TRUE(dns_packet_has_nsid_request(packet));
}
 
TEST(packet_has_nsid_request_match_multiple) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x08,
                        0x00, 0x03,     /* NSID option code */
                        0x00, 0x00,
                        0x00, 0x03,     /* NSID option code */
                        0x00, 0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
 
        ASSERT_ERROR(dns_packet_has_nsid_request(packet), EBADMSG);
}
 
TEST(packet_has_nsid_request_match_not_empty) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x00,
        /* OPT */       0x00, 0x29,
        /* udp max */   0x02, 0x01,
        /* rcode */     0x00,
        /* version */   0x00,
        /* flags */     0x80, 0x00,
        /* rdata */     0x00, 0x05,
                        0x00, 0x03,     /* NSID option code */
                        0x00, 0x01,
                        0xff
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
 
        ASSERT_ERROR(dns_packet_has_nsid_request(packet), EBADMSG);
}
 
/* ================================================================
 * reply: RRSIG
 * ================================================================ */
 
TEST(packet_reply_rrsig_for_a) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        DnsResourceRecord *rr = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x02,     0x00, 0x00,     0x00, 0x00,
 
        /* name */      0x03, 'w', 'w', 'w',
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00,
        /* A */         0x00, 0x01,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x04,
        /* ip */        0xc0, 0xa8, 0x01, 0x7f,
 
        /* name */      0xc0, 0x0c,
        /* RRSIG */     0x00, 0x2e,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x27,
        /* type */      0x00, 0x01,
        /* algo */      0x04,
        /* labels */    0x03,
        /* orig ttl */  0x00, 0x00, 0x0e, 0x10,
        /* expiry */    0x66, 0x8a, 0xa1, 0x57,
        /* inception */ 0x66, 0x63, 0x14, 0x57,
        /* key tag */   0x12, 0x34,
        /* signer */    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00,
        /* signature */ 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
        };
 
        const uint8_t signature[] = {
                0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 2u);
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
        ASSERT_NOT_NULL(rr);
        rr->ttl = 3600;
        rr->a.in_addr.s_addr = htobe32(0xc0a8017f);
 
        check_answer_contains(packet, rr, DNS_ANSWER_SECTION_ANSWER | DNS_ANSWER_CACHEABLE);
        dns_resource_record_unref(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 = 3;
        rr->rrsig.original_ttl = 3600;
        rr->rrsig.expiration = 1720361303;
        rr->rrsig.inception = 1717769303;
        rr->rrsig.key_tag = 0x1234;
        rr->rrsig.signer = strdup("example.com");
 
        rr->rrsig.signature_size = sizeof(signature);
        rr->rrsig.signature = memdup(signature, rr->rrsig.signature_size);
 
        check_answer_contains(packet, rr, DNS_ANSWER_SECTION_ANSWER | DNS_ANSWER_CACHEABLE);
        dns_resource_record_unref(rr);
}
 
TEST(packet_reply_rrsig_no_compression) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x02,     0x00, 0x00,     0x00, 0x00,
 
        /* name */      0x03, 'w', 'w', 'w',
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00,
        /* A */         0x00, 0x01,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x04,
        /* ip */        0xc0, 0xa8, 0x01, 0x7f,
 
        /* name */      0xc0, 0x0c,
        /* RRSIG */     0x00, 0x2e,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x1c,
        /* type */      0x00, 0x01,
        /* algo */      0x04,
        /* labels */    0x03,
        /* orig ttl */  0x00, 0x00, 0x0e, 0x10,
        /* expiry */    0x66, 0x8a, 0xa1, 0x57,
        /* inception */ 0x66, 0x63, 0x14, 0x57,
        /* key tag */   0x12, 0x34,
        /* signer */    0xc0, 0x10,
        /* signature */ 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_ERROR(dns_packet_extract(packet), EBADMSG);
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
}
 
TEST(packet_reply_rrsig_signature_underflow) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x02,     0x00, 0x00,     0x00, 0x00,
 
        /* name */      0x03, 'w', 'w', 'w',
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00,
        /* A */         0x00, 0x01,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x04,
        /* ip */        0xc0, 0xa8, 0x01, 0x7f,
 
        /* name */      0xc0, 0x0c,
        /* RRSIG */     0x00, 0x2e,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x28,
        /* type */      0x00, 0x01,
        /* algo */      0x04,
        /* labels */    0x03,
        /* orig ttl */  0x00, 0x00, 0x0e, 0x10,
        /* expiry */    0x66, 0x8a, 0xa1, 0x57,
        /* inception */ 0x66, 0x63, 0x14, 0x57,
        /* key tag */   0x12, 0x34,
        /* signer */    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00,
        /* signature */ 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_ERROR(dns_packet_extract(packet), EBADMSG);
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
}
 
TEST(packet_reply_rrsig_signer_overflow) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x02,     0x00, 0x00,     0x00, 0x00,
 
        /* name */      0x03, 'w', 'w', 'w',
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00,
        /* A */         0x00, 0x01,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x04,
        /* ip */        0xc0, 0xa8, 0x01, 0x7f,
 
        /* name */      0xc0, 0x0c,
        /* RRSIG */     0x00, 0x2e,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x1e,
        /* type */      0x00, 0x01,
        /* algo */      0x04,
        /* labels */    0x03,
        /* orig ttl */  0x00, 0x00, 0x0e, 0x10,
        /* expiry */    0x66, 0x8a, 0xa1, 0x57,
        /* inception */ 0x66, 0x63, 0x14, 0x57,
        /* key tag */   0x12, 0x34,
        /* signer */    0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00,
        /* signature */ 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_ERROR(dns_packet_extract(packet), EBADMSG);
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
}
 
/* ================================================================
 * reply: SVCB/HTTPS
 * ================================================================ */
 
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(packet_reply_svcb_alias_mode) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        DnsResourceRecord *rr = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x04, '_', '4', '4', '3',
                        0x04, '_', 'w', 's', 's',
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00,
        /* SVCB */      0x00, 0x40,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x14,
        /* priority */  0x00, 0x00,
        /* target */    0x04, 's', 'o', 'c', 'k',
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_OK(dns_packet_extract(packet));
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 1u);
 
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SVCB, "_443._wss.example.com");
        ASSERT_NOT_NULL(rr);
        rr->ttl = 3600;
        rr->svcb.priority = 0;
        rr->svcb.target_name = strdup("sock.example.com");
 
        check_answer_contains(packet, rr, DNS_ANSWER_SECTION_ADDITIONAL);
        dns_resource_record_unref(rr);
}
 
TEST(packet_reply_svcb_compressed_target) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,
                        0x00, 0x00,     0x00, 0x00,     0x00, 0x00,     0x00, 0x01,
 
        /* name */      0x04, '_', '4', '4', '3',
                        0x04, '_', 'w', 's', 's',
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
                        0x03, 'c', 'o', 'm',
                        0x00,
        /* SVCB */      0x00, 0x40,
        /* IN */        0x00, 0x01,
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
        /* rdata */     0x00, 0x09,
        /* priority */  0x00, 0x00,
        /* target */    0x04, 's', 'o', 'c', 'k',
                        0xc0, 0x16
        };
 
        ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
 
        ASSERT_ERROR(dns_packet_extract(packet), EBADMSG);
        ASSERT_EQ(dns_question_size(packet->question), 0u);
        ASSERT_EQ(dns_answer_size(packet->answer), 0u);
}
 
/* RFC 9460 says that alias-mode RRs SHOULD NOT have the same owner and target.
 * We accept this when parsing messages. */
 
TEST(packet_reply_svcb_alias_mode_same_owner_and_target) {
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
        DnsResourceRecord *rr = NULL;
 
        ASSERT_OK(dns_packet_new(&packet, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX));
        ASSERT_NOT_NULL(packet);
        dns_packet_truncate(packet, 0);
 
        const uint8_t data[] = {
                        0x00, 0x42,     BIT_QR | BIT_AA, DNS_RCODE_SUCCESS,