From 0bafe764aa8aa11ec23def28d1c9db212350f42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Mon, 11 Nov 2024 15:17:06 +0100 Subject: [PATCH] libsemanage/tests: misc cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Acked-by: James Carter --- libsemanage/tests/Makefile | 2 +- libsemanage/tests/test_bool.c | 12 +++++----- libsemanage/tests/test_fcontext.c | 21 ++++++----------- libsemanage/tests/test_handle.c | 11 +++++---- libsemanage/tests/test_ibendport.c | 18 +++++++-------- libsemanage/tests/test_iface.c | 12 +++++----- libsemanage/tests/test_node.c | 12 +++++----- libsemanage/tests/test_other.c | 4 +--- libsemanage/tests/test_port.c | 12 +++++----- libsemanage/tests/test_semanage_store.c | 13 +++++------ libsemanage/tests/test_user.c | 12 +++++----- libsemanage/tests/test_utilities.c | 2 +- libsemanage/tests/utilities.c | 10 +++++---- libsemanage/tests/utilities.h | 30 +++++++++++++------------ 14 files changed, 81 insertions(+), 90 deletions(-) diff --git a/libsemanage/tests/Makefile b/libsemanage/tests/Makefile index b086955845..241ff17aac 100644 --- a/libsemanage/tests/Makefile +++ b/libsemanage/tests/Makefile @@ -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 diff --git a/libsemanage/tests/test_bool.c b/libsemanage/tests/test_bool.c index 9356a1ae7b..fdbee73389 100644 --- a/libsemanage/tests/test_bool.c +++ b/libsemanage/tests/test_bool.c @@ -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; diff --git a/libsemanage/tests/test_fcontext.c b/libsemanage/tests/test_fcontext.c index 8943d741d6..291ed261eb 100644 --- a/libsemanage/tests/test_fcontext.c +++ b/libsemanage/tests/test_fcontext.c @@ -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++; diff --git a/libsemanage/tests/test_handle.c b/libsemanage/tests/test_handle.c index 00a6cb9253..704569ebbb 100644 --- a/libsemanage/tests/test_handle.c +++ b/libsemanage/tests/test_handle.c @@ -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); diff --git a/libsemanage/tests/test_ibendport.c b/libsemanage/tests/test_ibendport.c index a6ce7a34a7..ea99c1019a 100644 --- a/libsemanage/tests/test_ibendport.c +++ b/libsemanage/tests/test_ibendport.c @@ -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); diff --git a/libsemanage/tests/test_iface.c b/libsemanage/tests/test_iface.c index 2b84cb8eb3..19f70797e8 100644 --- a/libsemanage/tests/test_iface.c +++ b/libsemanage/tests/test_iface.c @@ -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; diff --git a/libsemanage/tests/test_node.c b/libsemanage/tests/test_node.c index 0db38d9765..e07b597370 100644 --- a/libsemanage/tests/test_node.c +++ b/libsemanage/tests/test_node.c @@ -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; diff --git a/libsemanage/tests/test_other.c b/libsemanage/tests/test_other.c index 0a57e247fd..9ea9737a21 100644 --- a/libsemanage/tests/test_other.c +++ b/libsemanage/tests/test_other.c @@ -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); diff --git a/libsemanage/tests/test_port.c b/libsemanage/tests/test_port.c index e6393d783d..2c0b980b6f 100644 --- a/libsemanage/tests/test_port.c +++ b/libsemanage/tests/test_port.c @@ -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; diff --git a/libsemanage/tests/test_semanage_store.c b/libsemanage/tests/test_semanage_store.c index 2e6c38e1c7..28a76d4f3d 100644 --- a/libsemanage/tests/test_semanage_store.c +++ b/libsemanage/tests/test_semanage_store.c @@ -43,13 +43,12 @@ #include #include -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. diff --git a/libsemanage/tests/test_user.c b/libsemanage/tests/test_user.c index b6fda51a06..776a061cf5 100644 --- a/libsemanage/tests/test_user.c +++ b/libsemanage/tests/test_user.c @@ -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; diff --git a/libsemanage/tests/test_utilities.c b/libsemanage/tests/test_utilities.c index e1ff4e7d74..bbd5af30a7 100644 --- a/libsemanage/tests/test_utilities.c +++ b/libsemanage/tests/test_utilities.c @@ -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' }; diff --git a/libsemanage/tests/utilities.c b/libsemanage/tests/utilities.c index 806869e0a7..91ecf94ad8 100644 --- a/libsemanage/tests/utilities.c +++ b/libsemanage/tests/utilities.c @@ -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(); diff --git a/libsemanage/tests/utilities.h b/libsemanage/tests/utilities.h index 298b328066..4b6b8baed1 100644 --- a/libsemanage/tests/utilities.h +++ b/libsemanage/tests/utilities.h @@ -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,