libicmp/tests/handle/test_get_fd.c
2026-01-25 15:48:39 +01:00

69 lines
1.5 KiB
C

#include <criterion/criterion.h>
#include "icmp.h"
#include "internal/icmp_internal.h"
#include "test_helpers.h"
/* 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);
}