From d4631eaa8a53d95b66c0e35ef343b80b03878aab Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Tue, 26 Nov 2024 17:50:02 +0200 Subject: [PATCH] tests: net: dns: Add test for invalid DNS answer parsing Make sure we catch invalid answer during parsing. Signed-off-by: Jukka Rissanen (cherry picked from commit 16669ec4d5f8e0abbfadd06c1c5ec5584005efcd) --- tests/net/lib/dns_packet/src/main.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/net/lib/dns_packet/src/main.c b/tests/net/lib/dns_packet/src/main.c index bdf55c611f3fc6..363bdf3d3b51b8 100644 --- a/tests/net/lib/dns_packet/src/main.c +++ b/tests/net/lib/dns_packet/src/main.c @@ -1266,6 +1266,27 @@ ZTEST(dns_packet, test_dns_flags_len) "DNS message length check failed (%d)", ret); } +static uint8_t invalid_answer_resp_ipv4[18] = { + /* DNS msg header (12 bytes) */ + 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x01, +}; + +ZTEST(dns_packet, test_dns_invalid_answer) +{ + struct dns_msg_t dns_msg = { 0 }; + enum dns_rr_type type; + uint32_t ttl; + int ret; + + dns_msg.msg = invalid_answer_resp_ipv4; + dns_msg.msg_size = sizeof(invalid_answer_resp_ipv4); + dns_msg.answer_offset = 12; + + ret = dns_unpack_answer(&dns_msg, 0, &ttl, &type); + zassert_equal(ret, -EINVAL, "DNS message answer check succeed (%d)", ret); +} + ZTEST_SUITE(dns_packet, NULL, NULL, NULL, NULL, NULL); /* TODO: * 1) add malformed DNS data (mostly done)