lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit f6f560be7451ccf439376071a393596ed24f212a
parent 7f1d6a01106eaad746d693126a17f8bd4dbc9237
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 23 May 2017 17:40:23 +0200

Add(Tests): Add tests for utils, mockup for fork(2)

Diffstat:
Minc/fail_test.h | 3+++
Msrc/m_util.c | 13++++++++-----
Msrc/test.c | 41+++++++++++++++++++++++++++++++----------
Mtests/test_tests.c | 20++++++++++++++++++++
Mtests/test_util.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 125 insertions(+), 15 deletions(-)

diff --git a/inc/fail_test.h b/inc/fail_test.h @@ -30,6 +30,7 @@ # define strcpy(dst, src) fl_strcpy(dst, src) # define strcat(dst, src) fl_strcat(dst, src) # define mkdir(path, mode) fl_mkdir(path, mode) +# define fork fl_fork #include <errno.h> @@ -43,6 +44,7 @@ void *fl_calloc(size_t nmemb, size_t size); char *fl_strcpy(char *dst, const char *src); char *fl_strcat(char *dst, const char *src); int fl_mkdir(const char *pathname, mode_t mode); +pid_t fl_fork(void); void set_malloc_fail(int val); void set_write_fail(int val); @@ -54,6 +56,7 @@ void set_calloc_fail(int val); void set_strcpy_fail(int val); void set_strcat_fail(int val); void set_mkdir_fail(int val); +void set_fork_fail(int val); # endif /* M_FAIL_TEST_H */ #endif /* COMPILE_WITH_TEST */ diff --git a/src/m_util.c b/src/m_util.c @@ -105,11 +105,11 @@ int exec_line(const char *str) { int ret; if (str == NULL) - return 0; + return 1; cmd = strdup(str); if (cmd == NULL) - return 0; + return 1; len = strlen(cmd); for (i = 0, j = 0; i < len; i++) @@ -167,8 +167,11 @@ int exec_list(mlist_t *list) { } end: - for (size_t i = 0; tab[i]; i++) - free(tab[i]); - free(tab); + if (tab != NULL) + { + for (size_t i = 0; tab[i]; i++) + free(tab[i]); + free(tab); + } return status; } diff --git a/src/test.c b/src/test.c @@ -23,16 +23,17 @@ # include <unistd.h> /* Real functions */ -static void *(*real_malloc)(size_t) = &malloc; -static ssize_t (*real_write)(int, const void *, size_t) = &write; -static ssize_t (*real_read)(int, void *, size_t) = &read; -static int (*real_close)(int) = &close; -static char *(*real_strdup)(const char *) = &strdup; -static int (*real_fstat)(int, struct stat *) = &fstat; -static void *(*real_calloc)(size_t, size_t) = &calloc; -static char *(*real_strcpy)(char *, const char *) = &strcpy; -static char *(*real_strcat)(char *, const char *) = &strcat; -static int (*real_mkdir)(const char *, mode_t) = &mkdir; +static void *(*real_malloc)(size_t) = &malloc; +static ssize_t (*real_write)(int, const void *, size_t) = &write; +static ssize_t (*real_read)(int, void *, size_t) = &read; +static int (*real_close)(int) = &close; +static char *(*real_strdup)(const char *) = &strdup; +static int (*real_fstat)(int, struct stat *) = &fstat; +static void *(*real_calloc)(size_t, size_t) = &calloc; +static char *(*real_strcpy)(char *, const char *) = &strcpy; +static char *(*real_strcat)(char *, const char *) = &strcat; +static int (*real_mkdir)(const char *, mode_t) = &mkdir; +static pid_t (*real_fork)(void) = &fork; # include <fail_test.h> @@ -46,6 +47,7 @@ static int g_calloc_fail = -1; static int g_strcpy_fail = -1; static int g_strcat_fail = -1; static int g_mkdir_fail = -1; +static int g_fork_fail = -1; void *fl_malloc(size_t alloc) { if (g_malloc_fail == -1) @@ -164,6 +166,18 @@ int fl_mkdir(const char *path, mode_t mode) { return real_mkdir(path, mode); } +pid_t fl_fork(void) { + if (g_fork_fail == -1) + return real_fork(); + if (g_fork_fail == 0) + { + g_fork_fail = -1; + return -1; + } + g_fork_fail--; + return real_fork(); +} + void set_malloc_fail(int val) { if (g_malloc_fail == -1) g_malloc_fail = val; @@ -213,4 +227,11 @@ void set_mkdir_fail(int val) { if (g_mkdir_fail == -1) g_mkdir_fail = val; } + +void set_fork_fail(int val) { + if (g_fork_fail == -1) { + g_fork_fail = val; + } +} + #endif /* COMPILE_WITH_TEST */ diff --git a/tests/test_tests.c b/tests/test_tests.c @@ -210,6 +210,25 @@ TEST(test_fail_mkdir) { return TEST_SUCCESS; } +TEST(test_fail_fork) { + pid_t ret; + + ret = fork(); + TEST_ASSERT(ret != -1, "Should have succeed"); + if (ret == 0) + exit(0); + + set_fork_fail(1); + ret = fork(); + TEST_ASSERT(ret != -1, "Should have succeed"); + if (ret == 0) + exit(0); + + ret = fork(); + TEST_ASSERT(ret == -1, "Should have failed"); + return TEST_SUCCESS; +} + TEST(test_fail_cleanup) { unlink(TMP_FD_FN); @@ -233,4 +252,5 @@ void register_tests_tests(void) { reg_test("fake_functions", test_fail_strcat); reg_test("fake_functions", test_fail_cleanup); reg_test("fake_functions", test_fail_mkdir); + reg_test("fake_functions", test_fail_fork); } diff --git a/tests/test_util.c b/tests/test_util.c @@ -54,8 +54,71 @@ TEST(file_exist) { return TEST_SUCCESS; } +MPX_STATIC char **str_list_to_array(mlist_t *list); +TEST(str_list_to_array) { + mlist_t *head = NULL; + char **tmp = NULL; + static char *strings[] = { + "Test", + "1", + "2" + }; + + for (size_t i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) + list_add(head, strings[i], sizeof(strings[i])); + + set_malloc_fail(0); + TEST_ASSERT(str_list_to_array(head) == NULL, "Malloc failed did not return an error"); + set_strdup_fail(1); + TEST_ASSERT(str_list_to_array(head) == NULL, "Failed copy did not return an error"); + tmp = str_list_to_array(head); + TEST_ASSERT(tmp != NULL, "An error happened"); + + for (size_t i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) + { + TEST_ASSERT_FMT(strcmp(strings[i], tmp[i]) == 0, "Strings are different (%s/%s)", strings[i], tmp[i]); + free(tmp[i]); + } + free(tmp); + list_free(head, NULL); + return TEST_SUCCESS; +} + +TEST(exec_line) { + set_execute_flags(COMP_FLAGS_NONE); + + TEST_ASSERT(exec_line(NULL) == 1, "Wrong return on NULL parameter"); + set_strdup_fail(0); + TEST_ASSERT(exec_line("test") == 1, "Wrong return on failed copy"); + TEST_ASSERT(exec_line("totally_wrong_binary") != 0, "Wrong execvp return"); + TEST_ASSERT(exec_line("ls /") == 0, "Wrong execvp return"); + return TEST_SUCCESS; +} + +TEST(exec_list) { + mlist_t *head = NULL; + static char *strings[] = { + "ls", + "-la" + }; + + for (size_t i = 0; i < sizeof(strings) / sizeof(strings[0]); i++) + list_add(head, strings[i], sizeof(strings[i])); + + set_malloc_fail(0); + TEST_ASSERT(exec_list(head) == 1, "Malloc failed did not return an error"); + set_fork_fail(0); + TEST_ASSERT(exec_list(head) == 1, "Failed fork did not return an error"); + TEST_ASSERT(exec_list(head) == 0, "An error happened"); + list_free(head, NULL); + return TEST_SUCCESS; +} + void register_util_tests(void) { reg_test("util", init_dir); reg_test("util", recursive_delete); reg_test("util", file_exist); + reg_test("util", str_list_to_array); + reg_test("util", exec_line); + reg_test("util", exec_list); }