Skip to content

Commit

Permalink
libsemanage/tests: misc cleanup
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Acked-by: James Carter <[email protected]>
  • Loading branch information
cgzones authored and jwcart2 committed Nov 21, 2024
1 parent 6d55e80 commit 0bafe76
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 90 deletions.
2 changes: 1 addition & 1 deletion libsemanage/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions libsemanage/tests/test_bool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 7 additions & 14 deletions libsemanage/tests/test_fcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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+");
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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++;
Expand Down
11 changes: 5 additions & 6 deletions libsemanage/tests/test_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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);

Expand Down
18 changes: 8 additions & 10 deletions libsemanage/tests/test_ibendport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -254,25 +252,25 @@ 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);
helper_ibendport_iterate_counter++;
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);
helper_ibendport_iterate_counter++;
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);
Expand Down Expand Up @@ -439,25 +437,25 @@ 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);
helper_ibendport_iterate_local_counter++;
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);
helper_ibendport_iterate_local_counter++;
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);
Expand Down
12 changes: 6 additions & 6 deletions libsemanage/tests/test_iface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions libsemanage/tests/test_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions libsemanage/tests/test_other.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions libsemanage/tests/test_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 6 additions & 7 deletions libsemanage/tests/test_semanage_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions libsemanage/tests/test_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libsemanage/tests/test_utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
Expand Down
Loading

0 comments on commit 0bafe76

Please sign in to comment.