diff --git a/Makefile b/Makefile index 14ffe03..4c2c633 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/tests/handle/test_create.c b/tests/handle/test_create.c index 48030ad..4b18f28 100644 --- a/tests/handle/test_create.c +++ b/tests/handle/test_create.c @@ -3,21 +3,7 @@ #include #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) diff --git a/tests/handle/test_destroy.c b/tests/handle/test_destroy.c index 9b88750..68a9e78 100644 --- a/tests/handle/test_destroy.c +++ b/tests/handle/test_destroy.c @@ -4,21 +4,7 @@ #include #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) diff --git a/tests/handle/test_get_fd.c b/tests/handle/test_get_fd.c index b70bb3c..453be0b 100644 --- a/tests/handle/test_get_fd.c +++ b/tests/handle/test_get_fd.c @@ -1,21 +1,7 @@ #include #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) diff --git a/tests/socket/test_socket.c b/tests/socket/test_socket.c index 32d50eb..77cc5d8 100644 --- a/tests/socket/test_socket.c +++ b/tests/socket/test_socket.c @@ -3,25 +3,7 @@ #include #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) diff --git a/tests/test_helpers.h b/tests/test_helpers.h new file mode 100644 index 0000000..63ac835 --- /dev/null +++ b/tests/test_helpers.h @@ -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