From 8717257069249cdb217575bb3a3512d35bfadafb Mon Sep 17 00:00:00 2001 From: Alice Frosi Date: Wed, 12 Apr 2023 14:26:59 +0200 Subject: Refactor tests to use same struct arg as the filter --- tests/unit/Makefile | 4 ++-- tests/unit/test_filter.c | 46 ++++++++++++++---------------------------- tests/unit/test_filter_build.c | 1 + tests/unit/test_operations.c | 9 ++++++--- tests/unit/testutil.h | 4 +--- tests/unit/util.c | 24 ++++++++++++++++++---- 6 files changed, 45 insertions(+), 43 deletions(-) (limited to 'tests') diff --git a/tests/unit/Makefile b/tests/unit/Makefile index ae9631b..ef702dc 100644 --- a/tests/unit/Makefile +++ b/tests/unit/Makefile @@ -34,11 +34,11 @@ AUDIT_ARCH := $(shell echo $(AUDIT_ARCH) | sed 's/PPCLE/PPC64LE/') CFLAGS += -Wall -Wextra -pedantic CFLAGS += -I$(COMMON_DIR) -I$(OP_DIR) -I$(COOKER_DIR) -I$(DBG_DIR) CFLAGS += -lcheck -CFLAGS += -DSEITAN_AUDIT_ARCH=AUDIT_ARCH_$(AUDIT_ARCH) -DTMP_DATA_SIZE=1000 +CFLAGS += -DSEITAN_AUDIT_ARCH=AUDIT_ARCH_$(AUDIT_ARCH) -DTMP_DATA_SIZE=1000000 test: test-filter test-operations test-op-call -build-filter-build: test_filter_build.c $ (SRCS_FILTER_BUILD) $(HEADERS_FILTER_BUILD) +build-filter-build: test_filter_build.c $(SRCS_FILTER_BUILD) $(HEADERS_FILTER_BUILD) $(CC) $(CFLAGS) -o filter-build $(SRCS_FILTER) \ test_filter_build.c diff --git a/tests/unit/test_filter.c b/tests/unit/test_filter.c index 5745ba2..0ece692 100644 --- a/tests/unit/test_filter.c +++ b/tests/unit/test_filter.c @@ -22,7 +22,6 @@ static int generate_install_filter(struct args_target *at) { - unsigned int i; struct bpf_call calls[1]; struct syscall_entry table[] = { { .count = 1, .nr = at->nr, .entry = &calls[0] } @@ -30,23 +29,7 @@ static int generate_install_filter(struct args_target *at) struct sock_filter filter[30]; unsigned int size; - for (i = 0; i < 6; i++) { - if (at->args[i] == NULL) { - calls[0].args[i].cmp = NO_CHECK; - continue; - } - calls[0].args[i].cmp = at->cmp[i]; - switch (at->type[i]) { - case U32: - calls[0].args[i].value.v32 = (uint32_t)at->args[i]; - calls[0].args[i].type = U32; - break; - case U64: - calls[0].args[i].value.v64 = (uint64_t)at->args[i]; - calls[0].args[i].type = U64; - break; - } - } + memcpy(&calls[0].args, &at->args, sizeof(calls[0].args)); size = create_bfp_program(table, filter, 1); return install_filter(filter, size); } @@ -72,9 +55,9 @@ START_TEST(with_getsid) at->check_fd = false; at->nr = __NR_getsid; set_args_no_check(at); - at->args[0] = &id; - at->type[0] = U32; - at->cmp[0] = EQ; + at->args[0].type = U32; + at->args[0].value.v32 = id; + at->args[0].cmp = EQ; at->install_filter = generate_install_filter; setup(); mock_syscall_target(); @@ -83,19 +66,19 @@ END_TEST START_TEST(with_getpriority) { - int which = 12345; + int which = 0x12345; id_t who = PRIO_PROCESS; at = mmap(NULL, sizeof(struct args_target), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); at->check_fd = false; at->nr = __NR_getpriority; set_args_no_check(at); - at->args[0] = &which; - at->type[0] = U32; - at->cmp[0] = EQ; - at->args[1] = &who; - at->type[1] = U32; - at->cmp[1] = EQ; + at->args[0].value.v32 = which; + at->args[0].type = U32; + at->args[0].cmp = EQ; + at->args[1].value.v32 = who; + at->args[1].type = U32; + at->args[1].cmp = EQ; at->install_filter = generate_install_filter; setup(); mock_syscall_target(); @@ -108,7 +91,7 @@ static int target_lseek() /* Open the device on the target, but the arg0 isn't in the filter */ ck_assert_int_ge(fd, 0); - at->args[0] = (void *)(long)fd; + at->args[0].value.v64 = fd; return target(); } @@ -120,8 +103,9 @@ static void test_lseek(off_t offset) at->nr = __NR_lseek; at->target = target_lseek; set_args_no_check(at); - at->args[1] = (void *)offset; - at->type[1] = U64; + at->args[1].value.v64 = offset; + at->args[1].type = U64; + at->args[1].cmp = EQ; at->install_filter = generate_install_filter; setup(); mock_syscall_target(); diff --git a/tests/unit/test_filter_build.c b/tests/unit/test_filter_build.c index 343d020..bf79e3f 100644 --- a/tests/unit/test_filter_build.c +++ b/tests/unit/test_filter_build.c @@ -100,6 +100,7 @@ START_TEST(test_single_instr_two_args) }; size = create_bfp_program(table, result, sizeof(table) / sizeof(table[0])); + bpf_disasm_all(result, size); ck_assert_uint_eq(size, sizeof(expected) / sizeof(expected[0])); ck_assert(filter_eq(expected, result, sizeof(expected) / sizeof(expected[0]))); diff --git a/tests/unit/test_operations.c b/tests/unit/test_operations.c index bb098b8..13f767c 100644 --- a/tests/unit/test_operations.c +++ b/tests/unit/test_operations.c @@ -77,9 +77,12 @@ void setup_target_connect() MAP_SHARED | MAP_ANONYMOUS, -1, 0); at->check_fd = false; at->nr = __NR_connect; - at->args[0] = (void *)(long)fd; - at->args[1] = (void *)&addr; - at->args[2] = (void *)(long)len; + at->args[0].value.v32 = fd; + at->args[0].type = U32; + at->args[1].value.v64 = &addr; + at->args[1].type = U64; + at->args[2].value.v32 = len; + at->args[2].type = U32; at->install_filter = install_notification_filter; setup(); } diff --git a/tests/unit/testutil.h b/tests/unit/testutil.h index 4bd5550..c2f2153 100644 --- a/tests/unit/testutil.h +++ b/tests/unit/testutil.h @@ -19,9 +19,7 @@ struct args_target { bool open_path; int fd; int nr; - enum arg_cmp cmp[6]; - enum arg_type type[6]; - void *args[6]; + struct arg args[6]; int (*install_filter)(struct args_target *at); int (*target)(void *); }; diff --git a/tests/unit/util.c b/tests/unit/util.c index 89d5d73..3406c3b 100644 --- a/tests/unit/util.c +++ b/tests/unit/util.c @@ -49,15 +49,31 @@ int install_notification_filter(struct args_target *at) filter, (unsigned short)(sizeof(filter) / sizeof(filter[0]))); } +static void parse_args_target(void *args[]) +{ + for (unsigned int i = 0; i < 6; i++) { + switch (at->args[i].type) { + case U64: + args[i] = (void *)at->args[i].value.v64; + break; + case U32: + args[i] = (void *)(long)at->args[i].value.v32; + break; + } + } +} + int target() { - int buf = 0; + void *args[6]; + int buf = 0; if (at->install_filter(at) < 0) { return -1; } - at->ret = syscall(at->nr, at->args[0], at->args[1], at->args[2], - at->args[3], at->args[4], at->args[5]); + parse_args_target(args); + at->ret = syscall(at->nr, args[0], args[1], args[2], args[3], args[4], + args[5]); at->err = errno; if (at->open_path) { if ((at->fd = open(path, O_CREAT | O_RDONLY)) < 0) { @@ -188,7 +204,7 @@ void mock_syscall_target() void set_args_no_check(struct args_target *at) { for (unsigned int i = 0; i < 6; i++) - at->cmp[i] = NO_CHECK; + at->args[i].cmp = NO_CHECK; } void setup() -- cgit v1.2.3