From ba9623361f74be93674f2862903a39ae6d48e96e Mon Sep 17 00:00:00 2001 From: Alice Frosi Date: Wed, 22 Feb 2023 10:40:09 +0100 Subject: fix formatting --- tests/unit/test_action_call.c | 142 +++++++++++++++++++++--------------------- tests/unit/test_actions.c | 67 +++++++++++--------- 2 files changed, 108 insertions(+), 101 deletions(-) (limited to 'tests') diff --git a/tests/unit/test_action_call.c b/tests/unit/test_action_call.c index b039c8a..8621174 100644 --- a/tests/unit/test_action_call.c +++ b/tests/unit/test_action_call.c @@ -21,14 +21,13 @@ struct args_write_file { ssize_t size; }; - static void write_file(char *file, char *t, ssize_t size) { int fd; - fd = open(file, O_CREAT | O_RDWR, S_IWUSR |S_IRUSR); + fd = open(file, O_CREAT | O_RDWR, S_IWUSR | S_IRUSR); ck_assert_int_ge(fd, -1); - write(fd,t, size); + write(fd, t, size); close(fd); } @@ -37,9 +36,9 @@ static int write_file_get_fd(char *file, char *t, ssize_t size) int fd; write_file(file, t, size); - fd = open(file, O_RDONLY, S_IWUSR); - unlink(file); - return fd; + fd = open(file, O_RDONLY, S_IWUSR); + unlink(file); + return fd; } static int write_file_clone(void *a) @@ -57,44 +56,44 @@ static pid_t create_func_ns(int (*fn)(void *), void *arg, struct ns_spec ns[]) int flags = SIGCHLD; unsigned int i; - for (i=0; i < sizeof(sizeof(enum ns_type)); i++) - { + for (i = 0; i < sizeof(sizeof(enum ns_type)); i++) { if (ns[i].type == NS_NONE) continue; - switch(i){ - case NS_CGROUP: - flags |= CLONE_NEWCGROUP; - break; - case NS_IPC: - flags |= CLONE_NEWIPC; - break; - case NS_NET: - flags |= CLONE_NEWNET; - break; - case NS_MOUNT: - flags |= CLONE_NEWNS; - break; - case NS_PID: - flags |= CLONE_NEWPID; - break; - case NS_USER: - flags |= CLONE_NEWUSER; - break; - case NS_UTS: - flags |= CLONE_NEWUTS; - break; - case NS_TIME: - fprintf(stderr, "option NS_TIME not suppoted by clone\n"); - break; - default: - fprintf(stderr, "unrecognized option %d\n", i); + switch (i) { + case NS_CGROUP: + flags |= CLONE_NEWCGROUP; + break; + case NS_IPC: + flags |= CLONE_NEWIPC; + break; + case NS_NET: + flags |= CLONE_NEWNET; + break; + case NS_MOUNT: + flags |= CLONE_NEWNS; + break; + case NS_PID: + flags |= CLONE_NEWPID; + break; + case NS_USER: + flags |= CLONE_NEWUSER; + break; + case NS_UTS: + flags |= CLONE_NEWUTS; + break; + case NS_TIME: + fprintf(stderr, + "option NS_TIME not suppoted by clone\n"); + break; + default: + fprintf(stderr, "unrecognized option %d\n", i); } } child = clone(fn, stack + sizeof(stack), flags, arg); - if (child == -1) { - perror("clone"); - exit(EXIT_FAILURE); - } + if (child == -1) { + perror("clone"); + exit(EXIT_FAILURE); + } return child; } @@ -102,10 +101,9 @@ START_TEST(test_with_open_read_ns) { char test_file[] = "/tmp/test.txt"; char t[PATH_MAX] = "Hello Test"; - struct args_write_file args = { - test_file, t, sizeof(t)}; + struct args_write_file args = { test_file, t, sizeof(t) }; struct act_call call; - int flags = O_RDWR; + int flags = O_RDWR; struct arg_clone c; char buf[PATH_MAX]; unsigned i; @@ -114,29 +112,28 @@ START_TEST(test_with_open_read_ns) int ret; c.args = &call; - count = sizeof(buf); - for(i = 0; i < sizeof(enum ns_type); i++) + count = sizeof(buf); + for (i = 0; i < sizeof(enum ns_type); i++) call.context.ns[i].type = NS_NONE; call.context.ns[NS_MOUNT].type = NS_SPEC_PID; pid = create_func_ns(write_file_clone, (void *)&args, call.context.ns); call.context.ns[NS_MOUNT].pid = pid; - call.nr = SYS_open; - call.args[0] = (void *)&test_file; - call.args[1] = (void *)(long)flags; + call.nr = SYS_open; + call.args[0] = (void *)&test_file; + call.args[1] = (void *)(long)flags; ret = do_call(&c); ck_assert_int_eq(ret, 0); ck_assert_msg(c.ret >= 0, "expect ret %ld should be nonegative", c.ret); - call.nr = SYS_read; - call.args[0] = (void *)(long) c.ret; - call.args[1] = (void *)&buf; - call.args[2] = (void *)count; + call.nr = SYS_read; + call.args[0] = (void *)(long)c.ret; + call.args[1] = (void *)&buf; + call.args[2] = (void *)count; ret = do_call(&c); kill(pid, SIGCONT); ck_assert_int_eq(ret, 0); - ck_assert_msg(c.ret == count, - "expect ret %ld to be %ld",c.ret, count); + ck_assert_msg(c.ret == count, "expect ret %ld to be %ld", c.ret, count); ck_assert_str_eq(t, buf); } END_TEST @@ -154,13 +151,13 @@ START_TEST(test_with_read) c.args = &call; fd = write_file_get_fd(test_file, t, sizeof(t)); - count = sizeof(buf); - for(i = 0; i < sizeof(enum ns_type); i++) + count = sizeof(buf); + for (i = 0; i < sizeof(enum ns_type); i++) call.context.ns[i].type = NS_NONE; - call.nr = SYS_read; - call.args[0] = (void *)(long) fd; - call.args[1] = (void *)&buf; - call.args[2] = (void *)count; + call.nr = SYS_read; + call.args[0] = (void *)(long)fd; + call.args[1] = (void *)&buf; + call.args[2] = (void *)count; ret = do_call(&c); ck_assert_int_eq(ret, 0); @@ -174,34 +171,35 @@ START_TEST(test_with_getppid) struct act_call call; struct arg_clone c; unsigned i; - long pid = (long) getpid(); + long pid = (long)getpid(); int ret; - for(i = 0; i < sizeof(enum ns_type); i++) + for (i = 0; i < sizeof(enum ns_type); i++) call.context.ns[i].type = NS_NONE; - call.nr = SYS_getppid; + call.nr = SYS_getppid; c.args = &call; ret = do_call(&c); ck_assert_int_eq(ret, 0); - ck_assert_msg(c.ret == pid, "expect ret %ld to be equal to %ld", c.ret, pid); + ck_assert_msg(c.ret == pid, "expect ret %ld to be equal to %ld", c.ret, + pid); } END_TEST Suite *action_call_suite(void) { - Suite *s; - TCase *tactions; + Suite *s; + TCase *tactions; - s = suite_create("Perform actions call"); - tactions = tcase_create("action calls"); + s = suite_create("Perform actions call"); + tactions = tcase_create("action calls"); - tcase_add_test(tactions, test_with_getppid); - tcase_add_test(tactions, test_with_read); - tcase_add_test(tactions,test_with_open_read_ns); + tcase_add_test(tactions, test_with_getppid); + tcase_add_test(tactions, test_with_read); + tcase_add_test(tactions, test_with_open_read_ns); - suite_add_tcase(s, tactions); + suite_add_tcase(s, tactions); - return s; + return s; } int main(void) diff --git a/tests/unit/test_actions.c b/tests/unit/test_actions.c index c6fc022..8893722 100644 --- a/tests/unit/test_actions.c +++ b/tests/unit/test_actions.c @@ -70,7 +70,7 @@ static int install_notification_filter() static int create_test_fd() { - return open("/tmp", O_RDWR| O_TMPFILE ); + return open("/tmp", O_RDWR | O_TMPFILE); } static int target() @@ -133,9 +133,8 @@ static void check_target_result(long ret, int err, bool ignore_ret) read(pipefd[0], &buf, 1); if (!ignore_ret) ck_assert_msg(at->ret == ret, - "expect return value %ld to be equal to %ld", - at->ret, - ret); + "expect return value %ld to be equal to %ld", + at->ret, ret); ck_assert_int_eq(at->err, err); ck_assert_int_eq(close(pipefd[0]), 0); } @@ -153,7 +152,7 @@ void target_exit() static bool has_fd(int pid, int fd) { - char path[PATH_MAX + 1]; + char path[PATH_MAX + 1]; snprintf(path, sizeof(path), "/proc/%d/fd/%d", pid, fd); return access(path, F_OK) == 0; @@ -172,11 +171,11 @@ void setup(bool check_fd) { int ret; - signal (SIGCHLD, target_exit); + signal(SIGCHLD, target_exit); ck_assert_int_ne(pipe(pipefd), -1); at = mmap(NULL, sizeof(struct args_target), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); - at-> check_fd = check_fd; + at->check_fd = check_fd; pid = do_clone(target, NULL); ck_assert_int_ge(pid, 0); @@ -213,8 +212,9 @@ START_TEST(test_act_continue) struct action actions[] = { { .type = A_CONT }, }; - int ret = do_actions(NULL, actions, sizeof(actions) / sizeof(actions[0]), -1, - notifyfd, req.id); + int ret = do_actions(NULL, actions, + sizeof(actions) / sizeof(actions[0]), -1, notifyfd, + req.id); ck_assert_msg(ret == 0, strerror(errno)); ck_assert_int_eq(at->err, 0); } @@ -228,8 +228,9 @@ START_TEST(test_act_block) .block = { .error = -1 }, }, }; - int ret = do_actions(NULL, actions, sizeof(actions) / sizeof(actions[0]), -1, - notifyfd, req.id); + int ret = do_actions(NULL, actions, + sizeof(actions) / sizeof(actions[0]), -1, notifyfd, + req.id); ck_assert_msg(ret == 0, strerror(errno)); check_target_result(-1, 0, false); } @@ -243,8 +244,9 @@ START_TEST(test_act_return) .ret = { .type = IMMEDIATE, .value = 1 }, }, }; - int ret = do_actions(NULL, actions, sizeof(actions) / sizeof(actions[0]), -1, - notifyfd, req.id); + int ret = do_actions(NULL, actions, + sizeof(actions) / sizeof(actions[0]), -1, notifyfd, + req.id); ck_assert_msg(ret == 0, strerror(errno)); check_target_result(1, 0, false); } @@ -262,8 +264,9 @@ START_TEST(test_act_return_ref) }; memcpy((uint16_t *)&tmp_data + offset, &v, sizeof(v)); - int ret = do_actions(&tmp_data, actions, sizeof(actions) / sizeof(actions[0]), -1, - notifyfd, req.id); + int ret = do_actions(&tmp_data, actions, + sizeof(actions) / sizeof(actions[0]), -1, notifyfd, + req.id); ck_assert_msg(ret == 0, strerror(errno)); check_target_result(v, 0, false); } @@ -278,8 +281,9 @@ START_TEST(test_act_call) }, { .type = A_CONT }, }; - int ret = do_actions(NULL, actions, sizeof(actions) / sizeof(actions[0]), -1, - notifyfd, req.id); + int ret = do_actions(NULL, actions, + sizeof(actions) / sizeof(actions[0]), -1, notifyfd, + req.id); ck_assert_msg(ret == 0, strerror(errno)); check_target_result(1, 0, true); } @@ -290,12 +294,15 @@ START_TEST(test_act_call_ret) struct action actions[] = { { .type = A_CALL, - .call = { .nr = __NR_getppid, .has_ret = true, .ret_off = 2 }, + .call = { .nr = __NR_getppid, + .has_ret = true, + .ret_off = 2 }, }, { .type = A_CONT }, }; - int ret = do_actions(&tmp_data, actions, sizeof(actions) / sizeof(actions[0]), -1, - notifyfd, req.id); + int ret = do_actions(&tmp_data, actions, + sizeof(actions) / sizeof(actions[0]), -1, notifyfd, + req.id); long r; ck_assert_msg(ret == 0, strerror(errno)); check_target_result(1, 0, true); @@ -312,10 +319,12 @@ static void test_inject(struct action actions[], int n, bool reference) int ret; fd_inj = create_test_fd(); - ck_assert_int_ge(fd_inj,0); + ck_assert_int_ge(fd_inj, 0); if (reference) { - memcpy((uint16_t *)&tmp_data + new_off, &fd_inj, sizeof(fd_inj)); - memcpy((uint16_t *)&tmp_data + old_off, &test_fd, sizeof(test_fd)); + memcpy((uint16_t *)&tmp_data + new_off, &fd_inj, + sizeof(fd_inj)); + memcpy((uint16_t *)&tmp_data + old_off, &test_fd, + sizeof(test_fd)); actions[0].inj.newfd.fd_off = new_off; actions[0].inj.newfd.type = REFERENCE; @@ -335,29 +344,29 @@ static void test_inject(struct action actions[], int n, bool reference) START_TEST(test_act_inject_a) { - struct action actions[] = {{.type = A_INJECT_A} }; + struct action actions[] = { { .type = A_INJECT_A } }; test_inject(actions, sizeof(actions) / sizeof(actions[0]), false); } END_TEST START_TEST(test_act_inject_a_ref) { - struct action actions[] = {{.type = A_INJECT_A} }; + struct action actions[] = { { .type = A_INJECT_A } }; test_inject(actions, sizeof(actions) / sizeof(actions[0]), true); } END_TEST START_TEST(test_act_inject) { - struct action actions[] = { { .type = A_INJECT }}; - test_inject(actions,sizeof(actions) / sizeof(actions[0]), false); + struct action actions[] = { { .type = A_INJECT } }; + test_inject(actions, sizeof(actions) / sizeof(actions[0]), false); } END_TEST START_TEST(test_act_inject_ref) { - struct action actions[] = { { .type = A_INJECT }}; - test_inject(actions,sizeof(actions) / sizeof(actions[0]), true); + struct action actions[] = { { .type = A_INJECT } }; + test_inject(actions, sizeof(actions) / sizeof(actions[0]), true); } END_TEST -- cgit v1.2.3