libicmp/tests/recv/test_process.c
2026-03-08 16:32:04 +01:00

82 lines
1.7 KiB
C

#include <criterion/criterion.h>
#include "icmp.h"
#include "test_helpers.h"
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
static void
dummy_callback(const icmp_reply_t *reply, void *userdata)
{
(void)reply;
(void)userdata;
}
static void
counter_callback(const icmp_reply_t *reply, void *userdata)
{
int *count;
(void)reply;
count = (int *)userdata;
(*count)++;
}
Test(process, null_handle)
{
int ret;
ret = icmp_process(NULL, dummy_callback, NULL, 0);
cr_assert_eq(ret, -1);
}
Test(process, null_callback)
{
icmp_handle_t *h;
int ret;
if (0 == has_net_raw_capability())
cr_skip("Test requires CAP_NET_RAW or root privileges");
h = icmp_create();
cr_assert_not_null(h);
ret = icmp_process(h, NULL, NULL, 0);
cr_assert_eq(ret, -1);
cr_assert_str_eq(icmp_strerror(h), "Callback cannot be NULL");
icmp_destroy(h);
}
Test(process, no_packets_available)
{
icmp_handle_t *h;
int ret;
if (0 == has_net_raw_capability())
cr_skip("Test requires CAP_NET_RAW or root privileges");
h = icmp_create();
cr_assert_not_null(h);
ret = icmp_process(h, dummy_callback, NULL, 0);
cr_assert_eq(ret, 0);
icmp_destroy(h);
}
Test(process, echo_loopback, .disabled = true)
{
icmp_handle_t *h;
struct in_addr dest;
int ret;
int count;
if (0 == has_net_raw_capability())
cr_skip("Test requires CAP_NET_RAW or root privileges");
h = icmp_create();
cr_assert_not_null(h);
inet_pton(AF_INET, "127.0.0.1", &dest);
ret = icmp_send_echo(h, dest, 1234, 1, 64, NULL, 0);
cr_assert_eq(ret, 0);
sleep(1);
count = 0;
ret = icmp_process(h, counter_callback, &count, 0);
cr_assert_geq(ret, 0);
cr_assert_geq(count, 1, "Should receive at least one packet");
icmp_destroy(h);
}