Skip to content

Commit 5653e9e

Browse files
committed
tests: net: dns: packet: Add test case for recursive name in CNAME
Add checks that we will be able to catch recursive name pointers and abandon the response for CNAME handling. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent d9ff6d9 commit 5653e9e

File tree

1 file changed

+85
-0
lines changed
  • tests/net/lib/dns_packet/src

1 file changed

+85
-0
lines changed

tests/net/lib/dns_packet/src/main.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,91 @@ ZTEST(dns_packet, test_dns_invalid_answer)
12871287
zassert_equal(ret, -EINVAL, "DNS message answer check succeed (%d)", ret);
12881288
}
12891289

1290+
static uint8_t recursive_query_resp_ipv4[] = {
1291+
/* DNS msg header (12 bytes) */
1292+
0x74, 0xe1, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01,
1293+
0x00, 0x00, 0x00, 0x00,
1294+
1295+
/* Query string (westus2-prod-2.notifications.teams.microsoft.com)
1296+
* (length 50)
1297+
*/
1298+
0x0e, 0x77, 0x65, 0x73, 0x74, 0x75, 0x73, 0x32,
1299+
0x2d, 0x70, 0x72, 0x6f, 0x64, 0x2d, 0x32, 0x0d,
1300+
0x6e, 0x6f, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61,
1301+
0x74, 0x69, 0x6f, 0x6e, 0x73, 0x05, 0x74, 0x65,
1302+
0x61, 0x6d, 0x73, 0x09, 0x6d, 0x69, 0x63, 0x72,
1303+
0x6f, 0x73, 0x6f, 0x66, 0x74, 0x03, 0x63, 0x6f,
1304+
0x6d, 0x00,
1305+
1306+
/* Type (2 bytes) */
1307+
0x00, 0x01,
1308+
1309+
/* Class (2 bytes) */
1310+
0x00, 0x01,
1311+
1312+
/* Answer 1 */
1313+
0xc0, 0x0c,
1314+
1315+
/* Answer type (cname) */
1316+
0x00, 0x05,
1317+
1318+
/* Class */
1319+
0x00, 0x01,
1320+
1321+
/* TTL */
1322+
0x00, 0x00, 0x00, 0x04,
1323+
1324+
/* RR data length */
1325+
0x00, 0x02,
1326+
1327+
/* Data */
1328+
0xc0, 0x4e, /* <--- recursive pointer */
1329+
};
1330+
1331+
NET_BUF_POOL_DEFINE(dns_qname_pool_for_test, 2, 128, 0, NULL);
1332+
1333+
ZTEST(dns_packet, test_dns_recursive_query)
1334+
{
1335+
static const uint8_t query[] = {
1336+
/* Query string */
1337+
0x0e, 0x77, 0x65, 0x73, 0x74, 0x75, 0x73, 0x32,
1338+
0x2d, 0x70, 0x72, 0x6f, 0x64, 0x2d, 0x32, 0x0d,
1339+
0x6e, 0x6f, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61,
1340+
0x74, 0x69, 0x6f, 0x6e, 0x73, 0x05, 0x74, 0x65,
1341+
0x61, 0x6d, 0x73, 0x09, 0x6d, 0x69, 0x63, 0x72,
1342+
0x6f, 0x73, 0x6f, 0x66, 0x74, 0x03, 0x63, 0x6f,
1343+
0x6d, 0x00,
1344+
1345+
/* Type */
1346+
0x00, 0x01,
1347+
};
1348+
struct dns_msg_t dns_msg = { 0 };
1349+
uint16_t dns_id = 0;
1350+
int query_idx = -1;
1351+
uint16_t query_hash = 0;
1352+
struct net_buf *dns_cname;
1353+
int ret;
1354+
1355+
dns_cname = net_buf_alloc(&dns_qname_pool_for_test, dns_ctx.buf_timeout);
1356+
zassert_not_null(dns_cname, "Out of mem");
1357+
1358+
dns_msg.msg = recursive_query_resp_ipv4;
1359+
dns_msg.msg_size = sizeof(recursive_query_resp_ipv4);
1360+
1361+
dns_id = dns_unpack_header_id(dns_msg.msg);
1362+
1363+
setup_dns_context(&dns_ctx, 0, dns_id, query, sizeof(query),
1364+
DNS_QUERY_TYPE_A);
1365+
1366+
ret = dns_validate_msg(&dns_ctx, &dns_msg, &dns_id, &query_idx,
1367+
dns_cname, &query_hash);
1368+
zassert_true(ret == DNS_EAI_SYSTEM && errno == ELOOP,
1369+
"[%s] DNS message was valid (%d / %d)",
1370+
"recursive rsp", ret, errno);
1371+
1372+
net_buf_unref(dns_cname);
1373+
}
1374+
12901375
ZTEST_SUITE(dns_packet, NULL, NULL, NULL, NULL, NULL);
12911376
/* TODO:
12921377
* 1) add malformed DNS data (mostly done)

0 commit comments

Comments
 (0)