Skip to content

Commit 0bafe76

Browse files
cgzonesjwcart2
authored andcommitted
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>
1 parent 6d55e80 commit 0bafe76

14 files changed

+81
-90
lines changed

libsemanage/tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CILS = $(sort $(wildcard *.cil))
55
###########################################################################
66

77
EXECUTABLE = libsemanage-tests
8-
CFLAGS += -g -O0 -Wall -W -Wundef -Wmissing-noreturn -Wmissing-format-attribute -Wno-unused-parameter
8+
CFLAGS += -g -O0 -Wall -W -Wundef -Wmissing-noreturn -Wmissing-format-attribute
99
override CFLAGS += -I../src -I../include
1010
override LDLIBS += -lcunit -lbz2 -laudit -lselinux -lsepol
1111

libsemanage/tests/test_bool.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ static void test_bool_count_local(void);
5555
static void test_bool_iterate_local(void);
5656
static void test_bool_list_local(void);
5757

58-
extern semanage_handle_t *sh;
59-
6058
int boolean_test_init(void)
6159
{
6260
if (create_test_store() < 0) {
@@ -601,9 +599,10 @@ static void test_bool_count(void)
601599
}
602600

603601
/* Function bool_iterate */
604-
unsigned int counter_bool_iterate = 0;
602+
static unsigned int counter_bool_iterate = 0;
605603

606-
static int handler_bool_iterate(const semanage_bool_t *record, void *varg)
604+
static int handler_bool_iterate(__attribute__((unused)) const semanage_bool_t *record,
605+
__attribute__((unused)) void *varg)
607606
{
608607
counter_bool_iterate++;
609608
return 0;
@@ -857,9 +856,10 @@ static void test_bool_count_local(void)
857856
}
858857

859858
/* Function bool_iterate_local */
860-
unsigned int counter_bool_iterate_local = 0;
859+
static unsigned int counter_bool_iterate_local = 0;
861860

862-
static int handler_bool_iterate_local(const semanage_bool_t *record, void *varg)
861+
static int handler_bool_iterate_local(__attribute__((unused)) const semanage_bool_t *record,
862+
__attribute__((unused)) void *varg)
863863
{
864864
counter_bool_iterate_local++;
865865
return 0;

libsemanage/tests/test_fcontext.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,19 @@
2121
#include "utilities.h"
2222
#include "test_fcontext.h"
2323

24-
char FCONTEXTS[] =
24+
static const char FCONTEXTS[] =
2525
"/etc/selinux(/.*) -s system_u:object_r:first_t:s0\n"
2626
"/etc/selinux/targeted -- system_u:object_r:second_t:s0\n"
2727
"/etc/selinux(/.*) -b system_u:object_r:third_t:s0\n";
28-
unsigned int FCONTEXTS_LEN = sizeof(FCONTEXTS);
28+
static const unsigned int FCONTEXTS_LEN = sizeof(FCONTEXTS);
2929

3030
#define FCONTEXTS_COUNT 3
3131

3232
#define FCONTEXT1_EXPR "/etc/selinux(/.*)"
3333
#define FCONTEXT1_TYPE SEMANAGE_FCONTEXT_SOCK
34-
#define FCONTEXT1_CON "system_u:object_r:first_t:s0"
3534

3635
#define FCONTEXT2_EXPR "/etc/selinux/targeted"
3736
#define FCONTEXT2_TYPE SEMANAGE_FCONTEXT_REG
38-
#define FCONTEXT2_CON "system_u:object_r:second_t:s0"
39-
40-
#define FCONTEXT3_EXPR "/etc/selinux(/.*)"
41-
#define FCONTEXT3_TYPE SEMANAGE_FCONTEXT_BLOCK
42-
#define FCONTEXT3_CON "system_u:object_r:third_t:s0"
4337

4438
#define FCONTEXT_NONEXISTENT_EXPR "/asdf"
4539
#define FCONTEXT_NONEXISTENT_TYPE SEMANAGE_FCONTEXT_ALL
@@ -71,8 +65,6 @@ static void test_fcontext_count_local(void);
7165
static void test_fcontext_iterate_local(void);
7266
static void test_fcontext_list_local(void);
7367

74-
extern semanage_handle_t *sh;
75-
7668
static int write_file_contexts(const char *data, unsigned int data_len)
7769
{
7870
FILE *fptr = fopen("test-policy/store/active/file_contexts", "w+");
@@ -653,9 +645,10 @@ static void test_fcontext_count(void)
653645
}
654646

655647
/* Function semanage_fcontext_iterate */
656-
unsigned int counter_fcontext_iterate = 0;
648+
static unsigned int counter_fcontext_iterate = 0;
657649

658-
static int handler_fcontext_iterate(const semanage_fcontext_t *record, void *varg)
650+
static int handler_fcontext_iterate(const semanage_fcontext_t *record,
651+
__attribute__((unused)) void *varg)
659652
{
660653
CU_ASSERT_PTR_NOT_NULL(record);
661654
counter_fcontext_iterate++;
@@ -934,10 +927,10 @@ static void test_fcontext_count_local(void)
934927
}
935928

936929
/* Function semanage_fcontext_iterate_local */
937-
unsigned int counter_fcontext_iterate_local = 0;
930+
static unsigned int counter_fcontext_iterate_local = 0;
938931

939932
static int handler_fcontext_iterate_local(const semanage_fcontext_t *record,
940-
void *varg)
933+
__attribute__((unused)) void *varg)
941934
{
942935
CU_ASSERT_PTR_NOT_NULL(record);
943936
counter_fcontext_iterate_local++;

libsemanage/tests/test_handle.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ static void test_msg_set_callback(void);
3434
static void test_root(void);
3535
static void test_select_store(void);
3636

37-
extern semanage_handle_t *sh;
38-
3937
int handle_test_init(void)
4038
{
4139
if (create_test_store() < 0) {
@@ -234,10 +232,11 @@ static void test_mls_enabled(void)
234232
}
235233

236234
/* Function semanage_set_callback */
237-
int msg_set_callback_count = 0;
235+
static int msg_set_callback_count = 0;
238236

239-
static void helper_msg_set_callback(void *varg, semanage_handle_t *handle,
240-
const char *fmt, ...)
237+
static void helper_msg_set_callback(__attribute__((unused)) void *varg,
238+
__attribute__((unused)) semanage_handle_t *handle,
239+
__attribute__((unused)) const char *fmt, ...)
241240
{
242241
msg_set_callback_count++;
243242
}
@@ -300,7 +299,7 @@ static void helper_select_store(const char *name, enum semanage_connect_type typ
300299
/* FIXME: the storename parameter of semanage_select_store should be
301300
* 'const char *'
302301
*/
303-
semanage_select_store(sh, (char *) name, type);
302+
semanage_select_store(sh, name, type);
304303

305304
int res = semanage_connect(sh);
306305

libsemanage/tests/test_ibendport.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ static void test_ibendport_count_local(void);
4646
static void test_ibendport_iterate_local(void);
4747
static void test_ibendport_list_local(void);
4848

49-
extern semanage_handle_t *sh;
50-
5149
int ibendport_test_init(void)
5250
{
5351
if (create_test_store() < 0) {
@@ -254,25 +252,25 @@ static void test_ibendport_count(void)
254252
}
255253

256254
/* Function semanage_ibendport_iterate */
257-
unsigned int helper_ibendport_iterate_counter = 0;
255+
static unsigned int helper_ibendport_iterate_counter = 0;
258256

259-
static int helper_ibendport_iterate(const semanage_ibendport_t *ibendport,
257+
static int helper_ibendport_iterate(__attribute__((unused)) const semanage_ibendport_t *ibendport,
260258
void *fn_arg)
261259
{
262260
CU_ASSERT(fn_arg == (void *) 42);
263261
helper_ibendport_iterate_counter++;
264262
return 0;
265263
}
266264

267-
static int helper_ibendport_iterate_error(const semanage_ibendport_t *ibendport,
265+
static int helper_ibendport_iterate_error(__attribute__((unused)) const semanage_ibendport_t *ibendport,
268266
void *fn_arg)
269267
{
270268
CU_ASSERT(fn_arg == (void *) 42);
271269
helper_ibendport_iterate_counter++;
272270
return -1;
273271
}
274272

275-
static int helper_ibendport_iterate_break(const semanage_ibendport_t *ibendport,
273+
static int helper_ibendport_iterate_break(__attribute__((unused)) const semanage_ibendport_t *ibendport,
276274
void *fn_arg)
277275
{
278276
CU_ASSERT(fn_arg == (void *) 42);
@@ -439,25 +437,25 @@ static void test_ibendport_count_local(void)
439437
}
440438

441439
/* Function semanage_ibendport_iterate_local */
442-
unsigned int helper_ibendport_iterate_local_counter = 0;
440+
static unsigned int helper_ibendport_iterate_local_counter = 0;
443441

444-
static int helper_ibendport_iterate_local(const semanage_ibendport_t *ibendport,
442+
static int helper_ibendport_iterate_local(__attribute__((unused)) const semanage_ibendport_t *ibendport,
445443
void *fn_arg)
446444
{
447445
CU_ASSERT(fn_arg == (void *) 42);
448446
helper_ibendport_iterate_local_counter++;
449447
return 0;
450448
}
451449

452-
static int helper_ibendport_iterate_local_error(const semanage_ibendport_t *ibendport,
450+
static int helper_ibendport_iterate_local_error(__attribute__((unused)) const semanage_ibendport_t *ibendport,
453451
void *fn_arg)
454452
{
455453
CU_ASSERT(fn_arg == (void *) 42);
456454
helper_ibendport_iterate_local_counter++;
457455
return -1;
458456
}
459457

460-
static int helper_ibendport_iterate_local_break(const semanage_ibendport_t *ibendport,
458+
static int helper_ibendport_iterate_local_break(__attribute__((unused)) const semanage_ibendport_t *ibendport,
461459
void *fn_arg)
462460
{
463461
CU_ASSERT(fn_arg == (void *) 42);

libsemanage/tests/test_iface.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ static void test_iface_count_local(void);
6161
static void test_iface_iterate_local(void);
6262
static void test_iface_list_local(void);
6363

64-
extern semanage_handle_t *sh;
65-
6664
int iface_test_init(void)
6765
{
6866
if (create_test_store() < 0) {
@@ -492,9 +490,10 @@ static void test_iface_count(void)
492490

493491
/* Function semanage_iface_iterate */
494492

495-
unsigned int counter_iface_iterate = 0;
493+
static unsigned int counter_iface_iterate = 0;
496494

497-
static int handler_iface_iterate(const semanage_iface_t *record, void *varg)
495+
static int handler_iface_iterate(__attribute__((unused)) const semanage_iface_t *record,
496+
__attribute__((unused)) void *varg)
498497
{
499498
counter_iface_iterate++;
500499
return 0;
@@ -632,9 +631,10 @@ static void test_iface_count_local(void)
632631
}
633632

634633
/* Function semanage_iface_iterate_local */
635-
unsigned int counter_iface_iterate_local = 0;
634+
static unsigned int counter_iface_iterate_local = 0;
636635

637-
static int handler_iface_iterate_local(const semanage_iface_t *record, void *varg)
636+
static int handler_iface_iterate_local(__attribute__((unused)) const semanage_iface_t *record,
637+
__attribute__((unused)) void *varg)
638638
{
639639
counter_iface_iterate_local++;
640640
return 0;

libsemanage/tests/test_node.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ static void test_node_count_local(void);
6767
static void test_node_iterate_local(void);
6868
static void test_node_list_local(void);
6969

70-
extern semanage_handle_t *sh;
71-
7270
int node_test_init(void)
7371
{
7472
if (create_test_store() < 0) {
@@ -620,9 +618,10 @@ static void test_node_count(void)
620618
}
621619

622620
/* Function semanage_node_iterate */
623-
unsigned int counter_node_iterate = 0;
621+
static unsigned int counter_node_iterate = 0;
624622

625-
static int handler_node_iterate(const semanage_node_t *record, void *varg)
623+
static int handler_node_iterate(__attribute__((unused)) const semanage_node_t *record,
624+
__attribute__((unused)) void *varg)
626625
{
627626
counter_node_iterate++;
628627
return 0;
@@ -777,9 +776,10 @@ static void test_node_count_local(void)
777776
}
778777

779778
/* Function semanage_node_iterate_local */
780-
unsigned int counter_node_iterate_local = 0;
779+
static unsigned int counter_node_iterate_local = 0;
781780

782-
static int handler_node_iterate_local(const semanage_node_t *record, void *varg)
781+
static int handler_node_iterate_local(__attribute__((unused)) const semanage_node_t *record,
782+
__attribute__((unused)) void *varg)
783783
{
784784
counter_node_iterate_local++;
785785
return 0;

libsemanage/tests/test_other.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ void test_semanage_context(void);
2727
/* debug.h */
2828
void test_debug(void);
2929

30-
extern semanage_handle_t *sh;
31-
3230
int other_test_init(void)
3331
{
3432
return 0;
@@ -116,7 +114,7 @@ void test_debug(void)
116114
CU_ASSERT(semanage_module_info_create(sh, &modinfo) >= 0);
117115

118116
/* test */
119-
CU_ASSERT(semanage_module_info_set_priority(sh, modinfo, -42) < 0);
117+
CU_ASSERT(semanage_module_info_set_priority(sh, modinfo, (uint16_t)-42) < 0);
120118

121119
/* cleanup */
122120
semanage_module_info_destroy(sh, modinfo);

libsemanage/tests/test_port.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ static void test_port_list_local(void);
6565
/* internal */
6666
static void test_port_validate_local(void);
6767

68-
extern semanage_handle_t *sh;
69-
7068
int port_test_init(void)
7169
{
7270
if (create_test_store() < 0) {
@@ -539,9 +537,10 @@ static void test_port_count(void)
539537
}
540538

541539
/* Function semanage_port_iterate */
542-
unsigned int counter_port_iterate = 0;
540+
static unsigned int counter_port_iterate = 0;
543541

544-
static int handler_port_iterate(const semanage_port_t *record, void *varg)
542+
static int handler_port_iterate(__attribute__((unused)) const semanage_port_t *record,
543+
__attribute__((unused)) void *varg)
545544
{
546545
counter_port_iterate++;
547546
return 0;
@@ -716,9 +715,10 @@ static void test_port_count_local(void)
716715
}
717716

718717
/* Function semanage_port_iterate_local */
719-
unsigned int counter_port_iterate_local = 0;
718+
static unsigned int counter_port_iterate_local = 0;
720719

721-
static int handler_port_iterate_local(const semanage_port_t *record, void *varg)
720+
static int handler_port_iterate_local(__attribute__((unused)) const semanage_port_t *record,
721+
__attribute__((unused)) void *varg)
722722
{
723723
counter_port_iterate_local++;
724724
return 0;

libsemanage/tests/test_semanage_store.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@
4343
#include <unistd.h>
4444
#include <CUnit/Basic.h>
4545

46-
extern semanage_handle_t *sh;
47-
const char *rootpath = "./test-policy";
48-
const char *polpath = "./test-policy/store/";
49-
const char *readlockpath = "./test-policy/store/semanage.read.LOCK";
50-
const char *translockpath = "./test-policy/store/semanage.trans.LOCK";
51-
const char *actpath = "./test-policy/store/active";
52-
const char *modpath = "./test-policy/store/active/modules";
46+
static const char *const rootpath = "./test-policy";
47+
static const char *const polpath = "./test-policy/store/";
48+
static const char *const readlockpath = "./test-policy/store/semanage.read.LOCK";
49+
static const char *const translockpath = "./test-policy/store/semanage.trans.LOCK";
50+
static const char *const actpath = "./test-policy/store/active";
51+
static const char *const modpath = "./test-policy/store/active/modules";
5352

5453
/* The suite initialization function.
5554
* Returns zero on success, non-zero otherwise.

libsemanage/tests/test_user.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ static void test_user_count_local(void);
5050
static void test_user_iterate_local(void);
5151
static void test_user_list_local(void);
5252

53-
extern semanage_handle_t *sh;
54-
5553
int user_test_init(void)
5654
{
5755
if (create_test_store() < 0) {
@@ -515,9 +513,10 @@ static void test_user_count(void)
515513
}
516514

517515
/* Function semanage_user_iterate */
518-
unsigned int counter_user_iterate = 0;
516+
static unsigned int counter_user_iterate = 0;
519517

520-
static int handler_user_iterate(const semanage_user_t *record, void *varg)
518+
static int handler_user_iterate(__attribute__((unused)) const semanage_user_t *record,
519+
__attribute__((unused)) void *varg)
521520
{
522521
counter_user_iterate++;
523522
return 0;
@@ -648,9 +647,10 @@ static void test_user_count_local(void)
648647
}
649648

650649
/* Function semanage_user_iterate_local */
651-
unsigned int counter_user_iterate_local = 0;
650+
static unsigned int counter_user_iterate_local = 0;
652651

653-
static int handler_user_iterate_local(const semanage_user_t *record, void *varg)
652+
static int handler_user_iterate_local(__attribute__((unused)) const semanage_user_t *record,
653+
__attribute__((unused)) void *varg)
654654
{
655655
counter_user_iterate_local++;
656656
return 0;

libsemanage/tests/test_utilities.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void test_semanage_str_replace(void);
4747
static void test_semanage_findval(void);
4848
static void test_slurp_file_filter(void);
4949

50-
char fname[] = {
50+
static char fname[] = {
5151
'T', 'E', 'S', 'T', '_', 'T', 'E', 'M', 'P', '_', 'X', 'X', 'X', 'X',
5252
'X', 'X', '\0'
5353
};

0 commit comments

Comments
 (0)