test: deduplicate helper function

This commit is contained in:
lohhiiccc 2026-01-25 15:48:39 +01:00
parent 7f79d4c8c4
commit 8fb109e2a0
6 changed files with 25 additions and 65 deletions

View file

@ -65,7 +65,7 @@ test: $(TEST_BIN)
$(TEST_BIN): $(TESTS) $(OBJS_STATIC)
@mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) $(TEST_LDFLAGS) $^ -o $@
$(CC) $(CPPFLAGS) -I tests $(CFLAGS) $(TEST_LDFLAGS) $^ -o $@
.PHONY: install
install: all

View file

@ -3,21 +3,7 @@
#include <unistd.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;
}
#include "test_helpers.h"
/* Test 1: icmp_create() success (skip without privileges) */
Test(handle, create_success)

View file

@ -4,21 +4,7 @@
#include <stdlib.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;
}
#include "test_helpers.h"
/* Test 1: icmp_destroy() with NULL handle does not crash */
Test(handle, destroy_null_handle)

View file

@ -1,21 +1,7 @@
#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;
}
#include "test_helpers.h"
/* Test 1: icmp_get_fd() with NULL handle returns -1 */
Test(handle, get_fd_null_handle)

View file

@ -3,25 +3,7 @@
#include <unistd.h>
#include "internal/icmp_internal.h"
#include "internal/icmp_socket.h"
/* Helper: check if we have CAP_NET_RAW capability */
static int
has_net_raw_capability(void)
{
struct icmp_handle h = {0};
int ret = socket_create(&h);
if (0 == ret)
{
close(h.fd);
return 1;
}
if (ICMP_ERR_PERMISSION == h.last_error)
return 0;
return -1;
}
#include "test_helpers.h"
/* Test 1: socket_create() success (skip without privileges) */
Test(socket, create_success)

20
tests/test_helpers.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef TEST_HELPERS_H
#define TEST_HELPERS_H
#include "icmp.h"
static inline int
has_net_raw_capability(void)
{
icmp_handle_t *h = icmp_create();
if (NULL != h)
{
icmp_destroy(h);
return 1;
}
return 0;
}
#endif