From 94df2efe2d2221bf0c4d77510142c95283d76f2b Mon Sep 17 00:00:00 2001 From: Alice Frosi Date: Wed, 10 May 2023 15:19:49 +0200 Subject: Clean-up error message and test Refactoring error messages: - standardize error messages and functions - return on error instead of exit - test error when target doesn't exist - include ability to capture stderr and stdout in the tests --- tests/unit/test_errors.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/unit/testutil.h | 7 ++++++- tests/unit/util.c | 18 +++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/unit/test_errors.c b/tests/unit/test_errors.c index 06bae12..51fc3db 100644 --- a/tests/unit/test_errors.c +++ b/tests/unit/test_errors.c @@ -19,6 +19,11 @@ static void setup_error_check() setup(); } +static void setup_stderr() +{ + ck_stderr(); +} + struct gluten_offset test_max_size_data[] = { { OFFSET_DATA, DATA_SIZE }, { OFFSET_RO_DATA, RO_DATA_SIZE }, @@ -105,11 +110,50 @@ START_TEST(test_op_cmp) ck_assert_int_eq(eval(&gluten, ops, &req, notifyfd), -1); } +static struct ttargetnoexisting_data { + struct op op; + char err_msg[BUFSIZ]; +}; + +struct ttargetnoexisting_data test_target_noexisting_data[] = { + { { OP_CONT, { { 0 } } }, "the response id isn't valid" }, + { { OP_BLOCK, { { 0 } } }, "the response id isn't valid" }, + { { OP_RETURN, { { 0 } } }, "the response id isn't valid" }, + { { OP_INJECT, + { .inject = { { OFFSET_DATA, 0 }, { OFFSET_DATA, 0 } } } }, + "the response id isn't valid" }, + { { OP_INJECT_A, + { .inject = { { OFFSET_DATA, 0 }, { OFFSET_DATA, 0 } } } }, + "the response id isn't valid" }, + { { OP_CALL, { .call = { __NR_getpid, false } } }, + "the response id isn't valid" }, + { { OP_LOAD, + { .load = { { OFFSET_SECCOMP_DATA, 1 }, { OFFSET_DATA, 0 }, 0 } } }, + "error opening mem for" }, + { { OP_RESOLVEDFD, + { .resfd = { { OFFSET_SECCOMP_DATA, 1 }, + { OFFSET_DATA, 0 }, + 0, + 0 } } }, + "error reading /proc" }, +}; + +START_TEST(test_target_noexisting) +{ + struct op ops[2]; + + ops[0] = test_target_noexisting_data[_i].op; + ops[1].type = OP_END; + + ck_assert_int_eq(eval(&gluten, ops, &req, notifyfd), -1); + ck_error_msg(test_target_noexisting_data[_i].err_msg); +} Suite *error_suite(void) { Suite *s; TCase *bounds, *gwrite, *gread, *gcmp; + TCase *tnotexist; s = suite_create("Error handling"); @@ -138,6 +182,12 @@ Suite *error_suite(void) sizeof(test_cmp_data) / sizeof(test_cmp_data[0])); suite_add_tcase(s, gcmp); + tnotexist = tcase_create("target not existing"); + tcase_add_checked_fixture(tnotexist, setup_stderr, NULL); + tcase_add_loop_test(tnotexist, test_target_noexisting, 0, + ARRAY_SIZE(test_target_noexisting_data)); + suite_add_tcase(s, tnotexist); + return s; } diff --git a/tests/unit/testutil.h b/tests/unit/testutil.h index ec881c7..861efb0 100644 --- a/tests/unit/testutil.h +++ b/tests/unit/testutil.h @@ -1,6 +1,6 @@ #ifndef TESTUTIL_H #define TESTUTIL_H - +#include #include #include #include @@ -67,6 +67,8 @@ extern struct args_target *at; extern int pipefd[2]; extern pid_t pid; extern char path[PATH_MAX]; +extern char stderr_buff[BUFSIZ]; +extern char stdout_buff[BUFSIZ]; extern struct gluten gluten; @@ -85,4 +87,7 @@ void continue_target(); void mock_syscall_target(); void set_args_no_check(struct args_target *at); void check_target_result_nonegative(); +void ck_error_msg(char *s); +void ck_stderr(); +void ck_stdout(); #endif /* TESTUTIL_H */ diff --git a/tests/unit/util.c b/tests/unit/util.c index 45171f2..5c36c54 100644 --- a/tests/unit/util.c +++ b/tests/unit/util.c @@ -30,6 +30,8 @@ int pipefd[2]; pid_t pid; char path[PATH_MAX] = "/tmp/test-seitan"; struct gluten gluten; +char stderr_buff[BUFSIZ]; +char stdout_buff[BUFSIZ]; int install_notification_filter(struct args_target *at) { @@ -231,3 +233,19 @@ void teardown() munmap(at, sizeof(struct args_target)); unlink(path); } + +void ck_stderr() +{ + setbuf(stderr, stderr_buff); +} + +void ck_stdout() +{ + setbuf(stdout, stdout_buff); +} + +void ck_error_msg(char *s) +{ + ck_assert_msg(strstr(stderr_buff, s) != NULL, "err=\"%s\" doesn't contain \"%s\" ", + stderr_buff, s); +} -- cgit v1.2.3