tests: net: socket: Check length for AF_PACKET SOCK_DGRAM socket

This test checks that the length returned is not altered
even if the payload resembles a valid IPv4 or IPv6 length.

Signed-off-by: Christoph Seitz <christoph.seitz@infineon.com>
This commit is contained in:
Christoph Seitz 2025-03-09 23:05:16 +01:00 committed by Benjamin Cabé
parent 5992208605
commit b01b77aa44

View File

@ -424,6 +424,38 @@ ZTEST(socket_packet, test_packet_sockets_dgram)
zassert_mem_equal(data_to_send, data_to_receive, sizeof(data_to_send),
"Data mismatch");
/* Send specially crafted payload to mimic IPv4 and IPv6 length field,
* to ckeck correct length returned.
*/
uint8_t payload_ip_length[64], receive_ip_length[64];
memset(payload_ip_length, 0, sizeof(payload_ip_length));
/* Set ipv4 and ipv6 length fields to represent IP payload with the
* length of 1 byte.
*/
payload_ip_length[3] = 21;
payload_ip_length[5] = 1;
ret = zsock_sendto(sock2, payload_ip_length, sizeof(payload_ip_length), 0,
(const struct sockaddr *)&dst, sizeof(struct sockaddr_ll));
zassert_equal(ret, sizeof(payload_ip_length), "Cannot send all data (%d)", -errno);
k_msleep(10);
memset(&src, 0, sizeof(src));
errno = 0;
iter = 0;
do {
ret = zsock_recvfrom(sock2, receive_ip_length, sizeof(receive_ip_length), 0,
(struct sockaddr *)&src, &addrlen);
k_msleep(10);
iter++;
} while (ret < 0 && errno == EAGAIN && iter < max_iter);
zassert_equal(ret, ARRAY_SIZE(payload_ip_length), "Cannot receive all data (%d)", -errno);
zassert_mem_equal(payload_ip_length, receive_ip_length, sizeof(payload_ip_length),
"Data mismatch");
zsock_close(sock1);
zsock_close(sock2);
}