From 84936ba0b9fd89c554927b004d7076303bd37315 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 --- tests/net/lib/dns_packet/src/main.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/net/lib/dns_packet/src/main.c b/tests/net/lib/dns_packet/src/main.c index 460def50066d..797ed71043fe 100644 --- a/tests/net/lib/dns_packet/src/main.c +++ b/tests/net/lib/dns_packet/src/main.c @@ -50,6 +50,13 @@ static uint8_t query_mdns[] = { static uint16_t tid1 = 0xda0f; +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, +}; + + static int eval_query(const char *dname, uint16_t tid, enum dns_rr_type type, uint8_t *expected, uint16_t expected_len) { @@ -1260,6 +1267,21 @@ static void test_dns_flags_len(void) "DNS message length check failed (%d)", ret); } +static void test_dns_invalid_answer(void) +{ + 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); +} + void test_main(void) { ztest_test_suite(dns_tests, @@ -1271,7 +1293,8 @@ void test_main(void) ztest_unit_test(test_dns_id_len), ztest_unit_test(test_dns_flags_len), ztest_unit_test(test_dns_malformed_responses), - ztest_unit_test(test_dns_valid_responses) + ztest_unit_test(test_dns_valid_responses), + ztest_unit_test(test_dns_invalid_answer) ); ztest_run_test_suite(dns_tests);