test: deduplicate helper function
This commit is contained in:
parent
7f79d4c8c4
commit
8fb109e2a0
6 changed files with 25 additions and 65 deletions
2
Makefile
2
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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
20
tests/test_helpers.h
Normal 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
|
||||
Loading…
Add table
Reference in a new issue