libsemanage/tests: misc cleanup

* Explicitly mark unused function parameters and drop compiler warning
  override
* Move declaration of `semanage_handle_t *sh` from individual source
  files to utilities.h, since it is defined in utilities.c
* Declare file local variables static
* Drop unused macros
* Avoid casts dropping const qualifier
* Avoid usage of reserved identifier names (leading underscores)
* Silence UBSAN underflow warning by adding explicit cast

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Christian Göttsche 2024-11-11 15:17:06 +01:00 committed by James Carter
parent 6d55e80283
commit 0bafe764aa
14 changed files with 81 additions and 90 deletions

View File

@ -5,7 +5,7 @@ CILS = $(sort $(wildcard *.cil))
###########################################################################
EXECUTABLE = libsemanage-tests
CFLAGS += -g -O0 -Wall -W -Wundef -Wmissing-noreturn -Wmissing-format-attribute -Wno-unused-parameter
CFLAGS += -g -O0 -Wall -W -Wundef -Wmissing-noreturn -Wmissing-format-attribute
override CFLAGS += -I../src -I../include
override LDLIBS += -lcunit -lbz2 -laudit -lselinux -lsepol

View File

@ -55,8 +55,6 @@ static void test_bool_count_local(void);
static void test_bool_iterate_local(void);
static void test_bool_list_local(void);
extern semanage_handle_t *sh;
int boolean_test_init(void)
{
if (create_test_store() < 0) {
@ -601,9 +599,10 @@ static void test_bool_count(void)
}
/* Function bool_iterate */
unsigned int counter_bool_iterate = 0;
static unsigned int counter_bool_iterate = 0;
static int handler_bool_iterate(const semanage_bool_t *record, void *varg)
static int handler_bool_iterate(__attribute__((unused)) const semanage_bool_t *record,
__attribute__((unused)) void *varg)
{
counter_bool_iterate++;
return 0;
@ -857,9 +856,10 @@ static void test_bool_count_local(void)
}
/* Function bool_iterate_local */
unsigned int counter_bool_iterate_local = 0;
static unsigned int counter_bool_iterate_local = 0;
static int handler_bool_iterate_local(const semanage_bool_t *record, void *varg)
static int handler_bool_iterate_local(__attribute__((unused)) const semanage_bool_t *record,
__attribute__((unused)) void *varg)
{
counter_bool_iterate_local++;
return 0;

View File

@ -21,25 +21,19 @@
#include "utilities.h"
#include "test_fcontext.h"
char FCONTEXTS[] =
static const char FCONTEXTS[] =
"/etc/selinux(/.*) -s system_u:object_r:first_t:s0\n"
"/etc/selinux/targeted -- system_u:object_r:second_t:s0\n"
"/etc/selinux(/.*) -b system_u:object_r:third_t:s0\n";
unsigned int FCONTEXTS_LEN = sizeof(FCONTEXTS);
static const unsigned int FCONTEXTS_LEN = sizeof(FCONTEXTS);
#define FCONTEXTS_COUNT 3
#define FCONTEXT1_EXPR "/etc/selinux(/.*)"
#define FCONTEXT1_TYPE SEMANAGE_FCONTEXT_SOCK
#define FCONTEXT1_CON "system_u:object_r:first_t:s0"
#define FCONTEXT2_EXPR "/etc/selinux/targeted"
#define FCONTEXT2_TYPE SEMANAGE_FCONTEXT_REG
#define FCONTEXT2_CON "system_u:object_r:second_t:s0"
#define FCONTEXT3_EXPR "/etc/selinux(/.*)"
#define FCONTEXT3_TYPE SEMANAGE_FCONTEXT_BLOCK
#define FCONTEXT3_CON "system_u:object_r:third_t:s0"
#define FCONTEXT_NONEXISTENT_EXPR "/asdf"
#define FCONTEXT_NONEXISTENT_TYPE SEMANAGE_FCONTEXT_ALL
@ -71,8 +65,6 @@ static void test_fcontext_count_local(void);
static void test_fcontext_iterate_local(void);
static void test_fcontext_list_local(void);
extern semanage_handle_t *sh;
static int write_file_contexts(const char *data, unsigned int data_len)
{
FILE *fptr = fopen("test-policy/store/active/file_contexts", "w+");
@ -653,9 +645,10 @@ static void test_fcontext_count(void)
}
/* Function semanage_fcontext_iterate */
unsigned int counter_fcontext_iterate = 0;
static unsigned int counter_fcontext_iterate = 0;
static int handler_fcontext_iterate(const semanage_fcontext_t *record, void *varg)
static int handler_fcontext_iterate(const semanage_fcontext_t *record,
__attribute__((unused)) void *varg)
{
CU_ASSERT_PTR_NOT_NULL(record);
counter_fcontext_iterate++;
@ -934,10 +927,10 @@ static void test_fcontext_count_local(void)
}
/* Function semanage_fcontext_iterate_local */
unsigned int counter_fcontext_iterate_local = 0;
static unsigned int counter_fcontext_iterate_local = 0;
static int handler_fcontext_iterate_local(const semanage_fcontext_t *record,
void *varg)
__attribute__((unused)) void *varg)
{
CU_ASSERT_PTR_NOT_NULL(record);
counter_fcontext_iterate_local++;

View File

@ -34,8 +34,6 @@ static void test_msg_set_callback(void);
static void test_root(void);
static void test_select_store(void);
extern semanage_handle_t *sh;
int handle_test_init(void)
{
if (create_test_store() < 0) {
@ -234,10 +232,11 @@ static void test_mls_enabled(void)
}
/* Function semanage_set_callback */
int msg_set_callback_count = 0;
static int msg_set_callback_count = 0;
static void helper_msg_set_callback(void *varg, semanage_handle_t *handle,
const char *fmt, ...)
static void helper_msg_set_callback(__attribute__((unused)) void *varg,
__attribute__((unused)) semanage_handle_t *handle,
__attribute__((unused)) const char *fmt, ...)
{
msg_set_callback_count++;
}
@ -300,7 +299,7 @@ static void helper_select_store(const char *name, enum semanage_connect_type typ
/* FIXME: the storename parameter of semanage_select_store should be
* 'const char *'
*/
semanage_select_store(sh, (char *) name, type);
semanage_select_store(sh, name, type);
int res = semanage_connect(sh);

View File

@ -46,8 +46,6 @@ static void test_ibendport_count_local(void);
static void test_ibendport_iterate_local(void);
static void test_ibendport_list_local(void);
extern semanage_handle_t *sh;
int ibendport_test_init(void)
{
if (create_test_store() < 0) {
@ -254,9 +252,9 @@ static void test_ibendport_count(void)
}
/* Function semanage_ibendport_iterate */
unsigned int helper_ibendport_iterate_counter = 0;
static unsigned int helper_ibendport_iterate_counter = 0;
static int helper_ibendport_iterate(const semanage_ibendport_t *ibendport,
static int helper_ibendport_iterate(__attribute__((unused)) const semanage_ibendport_t *ibendport,
void *fn_arg)
{
CU_ASSERT(fn_arg == (void *) 42);
@ -264,7 +262,7 @@ static int helper_ibendport_iterate(const semanage_ibendport_t *ibendport,
return 0;
}
static int helper_ibendport_iterate_error(const semanage_ibendport_t *ibendport,
static int helper_ibendport_iterate_error(__attribute__((unused)) const semanage_ibendport_t *ibendport,
void *fn_arg)
{
CU_ASSERT(fn_arg == (void *) 42);
@ -272,7 +270,7 @@ static int helper_ibendport_iterate_error(const semanage_ibendport_t *ibendport,
return -1;
}
static int helper_ibendport_iterate_break(const semanage_ibendport_t *ibendport,
static int helper_ibendport_iterate_break(__attribute__((unused)) const semanage_ibendport_t *ibendport,
void *fn_arg)
{
CU_ASSERT(fn_arg == (void *) 42);
@ -439,9 +437,9 @@ static void test_ibendport_count_local(void)
}
/* Function semanage_ibendport_iterate_local */
unsigned int helper_ibendport_iterate_local_counter = 0;
static unsigned int helper_ibendport_iterate_local_counter = 0;
static int helper_ibendport_iterate_local(const semanage_ibendport_t *ibendport,
static int helper_ibendport_iterate_local(__attribute__((unused)) const semanage_ibendport_t *ibendport,
void *fn_arg)
{
CU_ASSERT(fn_arg == (void *) 42);
@ -449,7 +447,7 @@ static int helper_ibendport_iterate_local(const semanage_ibendport_t *ibendport,
return 0;
}
static int helper_ibendport_iterate_local_error(const semanage_ibendport_t *ibendport,
static int helper_ibendport_iterate_local_error(__attribute__((unused)) const semanage_ibendport_t *ibendport,
void *fn_arg)
{
CU_ASSERT(fn_arg == (void *) 42);
@ -457,7 +455,7 @@ static int helper_ibendport_iterate_local_error(const semanage_ibendport_t *iben
return -1;
}
static int helper_ibendport_iterate_local_break(const semanage_ibendport_t *ibendport,
static int helper_ibendport_iterate_local_break(__attribute__((unused)) const semanage_ibendport_t *ibendport,
void *fn_arg)
{
CU_ASSERT(fn_arg == (void *) 42);

View File

@ -61,8 +61,6 @@ static void test_iface_count_local(void);
static void test_iface_iterate_local(void);
static void test_iface_list_local(void);
extern semanage_handle_t *sh;
int iface_test_init(void)
{
if (create_test_store() < 0) {
@ -492,9 +490,10 @@ static void test_iface_count(void)
/* Function semanage_iface_iterate */
unsigned int counter_iface_iterate = 0;
static unsigned int counter_iface_iterate = 0;
static int handler_iface_iterate(const semanage_iface_t *record, void *varg)
static int handler_iface_iterate(__attribute__((unused)) const semanage_iface_t *record,
__attribute__((unused)) void *varg)
{
counter_iface_iterate++;
return 0;
@ -632,9 +631,10 @@ static void test_iface_count_local(void)
}
/* Function semanage_iface_iterate_local */
unsigned int counter_iface_iterate_local = 0;
static unsigned int counter_iface_iterate_local = 0;
static int handler_iface_iterate_local(const semanage_iface_t *record, void *varg)
static int handler_iface_iterate_local(__attribute__((unused)) const semanage_iface_t *record,
__attribute__((unused)) void *varg)
{
counter_iface_iterate_local++;
return 0;

View File

@ -67,8 +67,6 @@ static void test_node_count_local(void);
static void test_node_iterate_local(void);
static void test_node_list_local(void);
extern semanage_handle_t *sh;
int node_test_init(void)
{
if (create_test_store() < 0) {
@ -620,9 +618,10 @@ static void test_node_count(void)
}
/* Function semanage_node_iterate */
unsigned int counter_node_iterate = 0;
static unsigned int counter_node_iterate = 0;
static int handler_node_iterate(const semanage_node_t *record, void *varg)
static int handler_node_iterate(__attribute__((unused)) const semanage_node_t *record,
__attribute__((unused)) void *varg)
{
counter_node_iterate++;
return 0;
@ -777,9 +776,10 @@ static void test_node_count_local(void)
}
/* Function semanage_node_iterate_local */
unsigned int counter_node_iterate_local = 0;
static unsigned int counter_node_iterate_local = 0;
static int handler_node_iterate_local(const semanage_node_t *record, void *varg)
static int handler_node_iterate_local(__attribute__((unused)) const semanage_node_t *record,
__attribute__((unused)) void *varg)
{
counter_node_iterate_local++;
return 0;

View File

@ -27,8 +27,6 @@ void test_semanage_context(void);
/* debug.h */
void test_debug(void);
extern semanage_handle_t *sh;
int other_test_init(void)
{
return 0;
@ -116,7 +114,7 @@ void test_debug(void)
CU_ASSERT(semanage_module_info_create(sh, &modinfo) >= 0);
/* test */
CU_ASSERT(semanage_module_info_set_priority(sh, modinfo, -42) < 0);
CU_ASSERT(semanage_module_info_set_priority(sh, modinfo, (uint16_t)-42) < 0);
/* cleanup */
semanage_module_info_destroy(sh, modinfo);

View File

@ -65,8 +65,6 @@ static void test_port_list_local(void);
/* internal */
static void test_port_validate_local(void);
extern semanage_handle_t *sh;
int port_test_init(void)
{
if (create_test_store() < 0) {
@ -539,9 +537,10 @@ static void test_port_count(void)
}
/* Function semanage_port_iterate */
unsigned int counter_port_iterate = 0;
static unsigned int counter_port_iterate = 0;
static int handler_port_iterate(const semanage_port_t *record, void *varg)
static int handler_port_iterate(__attribute__((unused)) const semanage_port_t *record,
__attribute__((unused)) void *varg)
{
counter_port_iterate++;
return 0;
@ -716,9 +715,10 @@ static void test_port_count_local(void)
}
/* Function semanage_port_iterate_local */
unsigned int counter_port_iterate_local = 0;
static unsigned int counter_port_iterate_local = 0;
static int handler_port_iterate_local(const semanage_port_t *record, void *varg)
static int handler_port_iterate_local(__attribute__((unused)) const semanage_port_t *record,
__attribute__((unused)) void *varg)
{
counter_port_iterate_local++;
return 0;

View File

@ -43,13 +43,12 @@
#include <unistd.h>
#include <CUnit/Basic.h>
extern semanage_handle_t *sh;
const char *rootpath = "./test-policy";
const char *polpath = "./test-policy/store/";
const char *readlockpath = "./test-policy/store/semanage.read.LOCK";
const char *translockpath = "./test-policy/store/semanage.trans.LOCK";
const char *actpath = "./test-policy/store/active";
const char *modpath = "./test-policy/store/active/modules";
static const char *const rootpath = "./test-policy";
static const char *const polpath = "./test-policy/store/";
static const char *const readlockpath = "./test-policy/store/semanage.read.LOCK";
static const char *const translockpath = "./test-policy/store/semanage.trans.LOCK";
static const char *const actpath = "./test-policy/store/active";
static const char *const modpath = "./test-policy/store/active/modules";
/* The suite initialization function.
* Returns zero on success, non-zero otherwise.

View File

@ -50,8 +50,6 @@ static void test_user_count_local(void);
static void test_user_iterate_local(void);
static void test_user_list_local(void);
extern semanage_handle_t *sh;
int user_test_init(void)
{
if (create_test_store() < 0) {
@ -515,9 +513,10 @@ static void test_user_count(void)
}
/* Function semanage_user_iterate */
unsigned int counter_user_iterate = 0;
static unsigned int counter_user_iterate = 0;
static int handler_user_iterate(const semanage_user_t *record, void *varg)
static int handler_user_iterate(__attribute__((unused)) const semanage_user_t *record,
__attribute__((unused)) void *varg)
{
counter_user_iterate++;
return 0;
@ -648,9 +647,10 @@ static void test_user_count_local(void)
}
/* Function semanage_user_iterate_local */
unsigned int counter_user_iterate_local = 0;
static unsigned int counter_user_iterate_local = 0;
static int handler_user_iterate_local(const semanage_user_t *record, void *varg)
static int handler_user_iterate_local(__attribute__((unused)) const semanage_user_t *record,
__attribute__((unused)) void *varg)
{
counter_user_iterate_local++;
return 0;

View File

@ -47,7 +47,7 @@ static void test_semanage_str_replace(void);
static void test_semanage_findval(void);
static void test_slurp_file_filter(void);
char fname[] = {
static char fname[] = {
'T', 'E', 'S', 'T', '_', 'T', 'E', 'M', 'P', '_', 'X', 'X', 'X', 'X',
'X', 'X', '\0'
};

View File

@ -24,14 +24,16 @@
#include "utilities.h"
int test_store_enabled = 0;
static int test_store_enabled = 0;
semanage_handle_t *sh = NULL;
/* Silence any error output caused by our tests
* by using this dummy function to catch messages.
*/
void test_msg_handler(void *varg, semanage_handle_t *handle, const char *fmt,
void test_msg_handler(__attribute__((unused)) void *varg,
__attribute__((unused)) semanage_handle_t *handle,
__attribute__((unused)) const char *fmt,
...)
{
}
@ -213,7 +215,7 @@ void helper_handle_create(void) {
semanage_set_create_store(sh, 1);
semanage_set_reload(sh, 0);
semanage_set_store_root(sh, "");
semanage_select_store(sh, (char *) "store",
semanage_select_store(sh, "store",
SEMANAGE_CON_DIRECT);
}
}
@ -271,7 +273,7 @@ void setup_handle_invalid_store(level_t level) {
helper_handle_create();
semanage_select_store(sh, (char *) "", SEMANAGE_CON_INVALID);
semanage_select_store(sh, "", SEMANAGE_CON_INVALID);
if (level >= SH_CONNECT)
helper_connect();

View File

@ -34,13 +34,13 @@
#define CU_ASSERT_CONTEXT_EQUAL(CON1,CON2) \
do { \
char *__str; \
char *__str2; \
CU_ASSERT(semanage_context_to_string(sh, CON1, &__str) >= 0); \
CU_ASSERT(semanage_context_to_string(sh, CON2, &__str2) >= 0); \
CU_ASSERT_STRING_EQUAL(__str, __str2); \
free(__str2); \
free(__str); \
char *str__; \
char *str2__; \
CU_ASSERT(semanage_context_to_string(sh, CON1, &str__) >= 0); \
CU_ASSERT(semanage_context_to_string(sh, CON2, &str2__) >= 0); \
CU_ASSERT_STRING_EQUAL(str__, str2__); \
free(str2__); \
free(str__); \
} while (0)
@ -49,9 +49,9 @@
#undef CU_ASSERT_FATAL
#define CU_ASSERT_FATAL(value) do { \
int _value = (value); \
CU_ASSERT(_value); \
assert(_value); \
int value_ = (value); \
CU_ASSERT(value_); \
assert(value_); \
} while (0)
#undef CU_FAIL_FATAL
@ -62,18 +62,20 @@
#undef CU_ASSERT_PTR_NOT_NULL_FATAL
#define CU_ASSERT_PTR_NOT_NULL_FATAL(value) do { \
const void *_value = (value); \
CU_ASSERT_PTR_NOT_NULL(_value); \
assert(_value != NULL); \
const void *value_ = (value); \
CU_ASSERT_PTR_NOT_NULL(value_); \
assert(value_ != NULL); \
} while (0)
#endif /* __CHECKER__ */
#define I_NULL -1
#define I_NULL (-1)
#define I_FIRST 0
#define I_SECOND 1
#define I_THIRD 2
extern semanage_handle_t *sh;
typedef enum { SH_NULL, SH_HANDLE, SH_CONNECT, SH_TRANS } level_t;
void test_msg_handler(void *varg, semanage_handle_t *handle, const char *fmt,