resolved: tests for dns_packet_extract(); parsing NS records

This commit is contained in:
James Coglan
2024-07-22 09:59:20 +01:00
parent 0afc219da8
commit 8a5e122d95
+235 -1
View File
@@ -1153,7 +1153,241 @@ TEST(packet_reply_a_rdata_truncated) {
}
/* ================================================================
* reply: CNAME, compression
* reply: NS
* ================================================================ */
TEST(packet_reply_ns_single) {
_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, 0x01, 0x00, 0x00,
/* name */ 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
/* NS */ 0x00, 0x02,
/* IN */ 0x00, 0x01,
/* ttl */ 0x00, 0x00, 0x0e, 0x10,
/* rdata */ 0x00, 0x06,
/* name */ 0x03, 'n', 's', '1',
0xc0, 0x0c
};
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_NS, "example.com");
ASSERT_NOT_NULL(rr);
rr->ttl = 3600;
rr->ns.name = strdup("ns1.example.com");
check_answer_contains(packet, rr, DNS_ANSWER_SECTION_AUTHORITY);
dns_resource_record_unref(rr);
}
TEST(packet_reply_ns_multi) {
_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, 0x03, 0x00, 0x00,
/* name */ 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
/* NS */ 0x00, 0x02,
/* IN */ 0x00, 0x01,
/* ttl */ 0x00, 0x00, 0x0e, 0x10,
/* rdata */ 0x00, 0x06,
/* name */ 0x03, 'n', 's', '1',
0xc0, 0x0c,
/* name */ 0xc0, 0x0c,
/* NS */ 0x00, 0x02,
/* IN */ 0x00, 0x01,
/* ttl */ 0x00, 0x00, 0x0e, 0x10,
/* rdata */ 0x00, 0x06,
/* name */ 0x03, 'n', 's', '2',
0xc0, 0x0c,
/* name */ 0xc0, 0x0c,
/* NS */ 0x00, 0x02,
/* IN */ 0x00, 0x01,
/* ttl */ 0x00, 0x00, 0x0e, 0x10,
/* rdata */ 0x00, 0x06,
/* name */ 0x03, 'n', 's', '3',
0xc0, 0x0c
};
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), 3u);
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NS, "example.com");
ASSERT_NOT_NULL(rr);
rr->ttl = 3600;
rr->ns.name = strdup("ns1.example.com");
check_answer_contains(packet, rr, DNS_ANSWER_SECTION_AUTHORITY);
dns_resource_record_unref(rr);
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NS, "example.com");
ASSERT_NOT_NULL(rr);
rr->ttl = 3600;
rr->ns.name = strdup("ns2.example.com");
check_answer_contains(packet, rr, DNS_ANSWER_SECTION_AUTHORITY);
dns_resource_record_unref(rr);
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_NS, "example.com");
ASSERT_NOT_NULL(rr);
rr->ttl = 3600;
rr->ns.name = strdup("ns3.example.com");
check_answer_contains(packet, rr, DNS_ANSWER_SECTION_AUTHORITY);
dns_resource_record_unref(rr);
}
TEST(packet_reply_ns_domain_underflows_rdata) {
_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 */ 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
/* NS */ 0x00, 0x02,
/* IN */ 0x00, 0x01,
/* ttl */ 0x00, 0x00, 0x0e, 0x10,
/* rdata */ 0x00, 0x12,
/* name */ 0x03, 'n', 's', '1',
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_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_ns_domain_overflows_rdata) {
_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 */ 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
/* NS */ 0x00, 0x02,
/* IN */ 0x00, 0x01,
/* ttl */ 0x00, 0x00, 0x0e, 0x10,
/* rdata */ 0x00, 0x06,
/* name */ 0x03, 'n', 's', '1',
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_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_ns_domain_overflows_rdata_compressed) {
_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 */ 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
/* NS */ 0x00, 0x02,
/* IN */ 0x00, 0x01,
/* ttl */ 0x00, 0x00, 0x0e, 0x10,
/* rdata */ 0x00, 0x05,
/* name */ 0x03, 'n', 's', '1',
0xc0, 0x0c
};
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_ns_domain_unterminated) {
_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 */ 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm',
0x00,
/* NS */ 0x00, 0x02,
/* IN */ 0x00, 0x01,
/* ttl */ 0x00, 0x00, 0x0e, 0x10,
/* rdata */ 0x00, 0x10,
/* name */ 0x03, 'n', 's', '1',
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
0x03, 'c', 'o', 'm'
/* missing 0x00 */
};
ASSERT_OK(dns_packet_append_blob(packet, data, sizeof(data), NULL));
ASSERT_ERROR(dns_packet_extract(packet), EMSGSIZE);
ASSERT_EQ(dns_question_size(packet->question), 0u);
ASSERT_EQ(dns_answer_size(packet->answer), 0u);
}
/* ================================================================
* reply: CNAME with A, compression
* ================================================================ */
static void check_cname_reply_compression(const uint8_t *data, size_t len) {