Commit 3f751d85 authored by Jukka Rissanen's avatar Jukka Rissanen Committed by Anas Nashif
Browse files

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: default avatarJukka Rissanen <jukka.rissanen@nordicsemi.no>
parent 1500de81
Loading
Loading
Loading
Loading
+85 −0
Changes for tests/net/lib/dns_packet/src/main.c: 85 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1287,6 +1287,91 @@ ZTEST(dns_packet, test_dns_invalid_answer)
	zassert_equal(ret, -EINVAL, "DNS message answer check succeed (%d)", ret);
}

static uint8_t recursive_query_resp_ipv4[] = {
	/* DNS msg header (12 bytes) */
	0x74, 0xe1, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01,
	0x00, 0x00, 0x00, 0x00,

	/* Query string (westus2-prod-2.notifications.teams.microsoft.com)
	 * (length 50)
	 */
	0x0e, 0x77, 0x65, 0x73,	0x74, 0x75, 0x73, 0x32,
	0x2d, 0x70, 0x72, 0x6f, 0x64, 0x2d, 0x32, 0x0d,
	0x6e, 0x6f, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61,
	0x74, 0x69, 0x6f, 0x6e, 0x73, 0x05, 0x74, 0x65,
	0x61, 0x6d, 0x73, 0x09, 0x6d, 0x69, 0x63, 0x72,
	0x6f, 0x73, 0x6f, 0x66, 0x74, 0x03, 0x63, 0x6f,
	0x6d, 0x00,

	/* Type (2 bytes) */
	0x00, 0x01,

	/* Class (2 bytes) */
	0x00, 0x01,

	/* Answer 1 */
	0xc0, 0x0c,

	/* Answer type (cname) */
	0x00, 0x05,

	/* Class */
	0x00, 0x01,

	/* TTL */
	0x00, 0x00, 0x00, 0x04,

	/* RR data length */
	0x00, 0x02,

	/* Data */
	0xc0, 0x4e, /* <--- recursive pointer */
};

NET_BUF_POOL_DEFINE(dns_qname_pool_for_test, 2, 128, 0, NULL);

ZTEST(dns_packet, test_dns_recursive_query)
{
	static const uint8_t query[] = {
		/* Query string */
		0x0e, 0x77, 0x65, 0x73,	0x74, 0x75, 0x73, 0x32,
		0x2d, 0x70, 0x72, 0x6f, 0x64, 0x2d, 0x32, 0x0d,
		0x6e, 0x6f, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61,
		0x74, 0x69, 0x6f, 0x6e, 0x73, 0x05, 0x74, 0x65,
		0x61, 0x6d, 0x73, 0x09, 0x6d, 0x69, 0x63, 0x72,
		0x6f, 0x73, 0x6f, 0x66, 0x74, 0x03, 0x63, 0x6f,
		0x6d, 0x00,

		/* Type */
		0x00, 0x01,
	};
	struct dns_msg_t dns_msg = { 0 };
	uint16_t dns_id = 0;
	int query_idx = -1;
	uint16_t query_hash = 0;
	struct net_buf *dns_cname;
	int ret;

	dns_cname = net_buf_alloc(&dns_qname_pool_for_test, dns_ctx.buf_timeout);
	zassert_not_null(dns_cname, "Out of mem");

	dns_msg.msg = recursive_query_resp_ipv4;
	dns_msg.msg_size = sizeof(recursive_query_resp_ipv4);

	dns_id = dns_unpack_header_id(dns_msg.msg);

	setup_dns_context(&dns_ctx, 0, dns_id, query, sizeof(query),
			  DNS_QUERY_TYPE_A);

	ret = dns_validate_msg(&dns_ctx, &dns_msg, &dns_id, &query_idx,
			       dns_cname, &query_hash);
	zassert_true(ret == DNS_EAI_SYSTEM && errno == ELOOP,
		     "[%s] DNS message was valid (%d / %d)",
		     "recursive rsp", ret, errno);

	net_buf_unref(dns_cname);
}

ZTEST_SUITE(dns_packet, NULL, NULL, NULL, NULL, NULL);
/* TODO:
 *	1) add malformed DNS data (mostly done)