libicmp/tests/handle/test_get_fd.c
lohhiiccc b7be251a18 feat(handle): add public api init and destroy utilities
- Added `icmp_create`.
 - Added `icmp_destroy`.
 - Added `icmp_get_fd`.
 - Added unit tests.
2026-01-25 00:23:13 +01:00

83 lines
1.6 KiB
C

#include <criterion/criterion.h>
#include "icmp.h"
#include "internal/icmp_internal.h"
/* Helper: check if we have CAP_NET_RAW capability */
static int
has_net_raw_capability(void)
{
icmp_handle_t *h = icmp_create();
if (NULL != h)
{
icmp_destroy(h);
return 1;
}
return 0;
}
/* Test 1: icmp_get_fd() with NULL handle returns -1 */
Test(handle, get_fd_null_handle)
{
int fd = icmp_get_fd(NULL);
cr_assert_eq(fd, -1, "Expected -1 for NULL handle");
}
/* Test 2: icmp_get_fd() returns valid file descriptor */
Test(handle, get_fd_valid_handle)
{
if (0 == has_net_raw_capability())
{
cr_skip("Test requires CAP_NET_RAW or root privileges");
}
icmp_handle_t *h = icmp_create();
cr_assert_not_null(h);
int fd = icmp_get_fd(h);
cr_assert_geq(fd, 0, "Expected valid file descriptor (>= 0)");
icmp_destroy(h);
}
/* Test 3: icmp_get_fd() is consistent across calls */
Test(handle, get_fd_consistent)
{
if (0 == has_net_raw_capability())
{
cr_skip("Test requires CAP_NET_RAW or root privileges");
}
icmp_handle_t *h = icmp_create();
cr_assert_not_null(h);
int fd1 = icmp_get_fd(h);
int fd2 = icmp_get_fd(h);
cr_assert_eq(fd1, fd2, "FD should be consistent across calls");
icmp_destroy(h);
}
/* Test 4: icmp_get_fd() returns same FD as internal structure */
Test(handle, get_fd_matches_internal)
{
if (0 == has_net_raw_capability())
{
cr_skip("Test requires CAP_NET_RAW or root privileges");
}
icmp_handle_t *h = icmp_create();
cr_assert_not_null(h);
struct icmp_handle *handle = (struct icmp_handle *)h;
int fd_public = icmp_get_fd(h);
cr_assert_eq(fd_public, handle->fd,
"Public FD should match internal FD");
icmp_destroy(h);
}