73 lines
2.3 KiB
C
73 lines
2.3 KiB
C
#include <criterion/criterion.h>
|
|
#include <string.h>
|
|
#include "internal/icmp_packet_internal.h"
|
|
#include "internal/icmp_packet.h"
|
|
#include "internal/icmp_utils.h"
|
|
|
|
Test(packet_build, echo_request_no_payload)
|
|
{
|
|
uint8_t buffer[8] = {0};
|
|
uint8_t type = 8, code = 0;
|
|
uint16_t id = 0x1234;
|
|
uint16_t seq = 0x0001;
|
|
|
|
int ret = icmp_build_packet(buffer, sizeof(buffer), type, code, id, seq,
|
|
NULL, 0);
|
|
cr_assert_eq(ret, 8, "Expected packet size 8, got %d", ret);
|
|
|
|
struct icmp_header *hdr = (struct icmp_header *)buffer;
|
|
cr_assert_eq(hdr->type, 8, "Expected type 8, got %u", hdr->type);
|
|
cr_assert_eq(hdr->code, 0, "Expected code 0, got %u", hdr->code);
|
|
cr_assert_eq(hdr->un.echo.id, id, "Expected id 0x%04x, got 0x%04x", id,
|
|
hdr->un.echo.id);
|
|
cr_assert_eq(hdr->un.echo.seq, seq, "Expected seq 0x%04x, got 0x%04x", seq,
|
|
hdr->un.echo.seq);
|
|
cr_assert_neq(hdr->checksum, 0, "Checksum should not be zero");
|
|
}
|
|
|
|
Test(packet_build, echo_request_with_payload)
|
|
{
|
|
const char payload[] = "HELLO";
|
|
size_t payload_len = sizeof(payload) - 1;
|
|
uint8_t buffer[8 + 5] = {0};
|
|
uint8_t type = 8, code = 0;
|
|
uint16_t id = 0xabcd, seq = 0x1234;
|
|
|
|
int ret = icmp_build_packet(buffer, sizeof(buffer), type, code, id, seq,
|
|
payload, payload_len);
|
|
cr_assert_eq(ret, 13, "Expected packet size 13, got %d", ret);
|
|
|
|
cr_assert(memcmp(buffer + 8, payload, 5) == 0,
|
|
"Payload not copied correctly");
|
|
}
|
|
|
|
Test(packet_build, checksum_correct)
|
|
{
|
|
uint8_t buffer[16] = {0};
|
|
uint8_t type = 8, code = 0;
|
|
uint16_t id = 0x77aa, seq = 0x0033;
|
|
const char payload[] = "ABC";
|
|
size_t payload_len = 3;
|
|
|
|
int ret = icmp_build_packet(buffer, sizeof(buffer), type, code, id, seq,
|
|
payload, payload_len);
|
|
cr_assert_eq(ret, 11, "Expected packet size 11, got %d", ret);
|
|
|
|
struct icmp_header *hdr = (struct icmp_header *)buffer;
|
|
uint16_t old_checksum = hdr->checksum;
|
|
hdr->checksum = 0;
|
|
uint16_t checksum = icmp_checksum(buffer, ret);
|
|
hdr->checksum = old_checksum;
|
|
cr_assert_eq(old_checksum, checksum,
|
|
"Checksum in packet (%04x) does not match recomputed one (%04x)",
|
|
old_checksum, checksum);
|
|
}
|
|
|
|
Test(packet_build, buffer_too_small)
|
|
{
|
|
uint8_t buf[4];
|
|
const char payload[8] = "ABCDEFG";
|
|
int ret = icmp_build_packet(buf, sizeof(buf), 8, 0, 0x1111, 0x2222,
|
|
payload, 8);
|
|
cr_assert_eq(ret, -1, "Expected -1 for too small buffer, got %d", ret);
|
|
}
|