lib

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

commit 018f1559db0eb0aa3203c5c2ce8de14123bde0f2
parent 8ed9dbdfaf2a01dbe801558975b5284a6fded6eb
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 10 May 2017 12:28:05 +0200

Add(Mockup): Add mockup for mkdir syscall

Diffstat:
Minc/fail_test.h | 5+++++
Msrc/test.c | 20++++++++++++++++++++
Mtests/test_tests.c | 15+++++++++++++++
3 files changed, 40 insertions(+), 0 deletions(-)

diff --git a/inc/fail_test.h b/inc/fail_test.h @@ -29,6 +29,9 @@ # define calloc(memb, size) fl_calloc(memb, size) # define strcpy(dst, src) fl_strcpy(dst, src) # define strcat(dst, src) fl_strcat(dst, src) +# define mkdir(path, mode) fl_mkdir(path, mode) + +#include <errno.h> void *fl_malloc(size_t alloc); ssize_t fl_write(int fd, const void *ptr, size_t len); @@ -39,6 +42,7 @@ int fl_fstat(int fd, struct stat *buf); 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); void set_malloc_fail(int val); void set_write_fail(int val); @@ -49,6 +53,7 @@ void set_fstat_fail(int val); void set_calloc_fail(int val); void set_strcpy_fail(int val); void set_strcat_fail(int val); +void set_mkdir_fail(int val); # endif /* M_FAIL_TEST_H */ #endif /* COMPILE_WITH_TEST */ diff --git a/src/test.c b/src/test.c @@ -32,6 +32,7 @@ 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; # include <fail_test.h> @@ -44,6 +45,7 @@ static int g_fstat_fail = -1; static int g_calloc_fail = -1; static int g_strcpy_fail = -1; static int g_strcat_fail = -1; +static int g_mkdir_fail = -1; void *fl_malloc(size_t alloc) { if (g_malloc_fail == -1) @@ -148,6 +150,20 @@ char *fl_strcat(char *dst, const char *src) { return real_strcat(dst, src); } +int fl_mkdir(const char *path, mode_t mode) { + if (g_mkdir_fail == -1) + return real_mkdir(path, mode); + if (g_mkdir_fail == 0) + { + g_mkdir_fail = -1; + /* Emptying ERRNO, in order to prevent real errors */ + errno = 0; + return -1; + } + g_mkdir_fail--; + return real_mkdir(path, mode); +} + void set_malloc_fail(int val) { if (g_malloc_fail == -1) g_malloc_fail = val; @@ -193,4 +209,8 @@ void set_strcat_fail(int val) { g_strcat_fail = val; } +void set_mkdir_fail(int val) { + if (g_mkdir_fail == -1) + g_mkdir_fail = val; +} #endif /* COMPILE_WITH_TEST */ diff --git a/tests/test_tests.c b/tests/test_tests.c @@ -196,6 +196,20 @@ TEST(test_fail_strcat) { return TEST_SUCCESS; } +TEST(test_fail_mkdir) { + const char *dir = "/tmp/test_fail_mkdir"; + + TEST_ASSERT(mkdir(dir, S_IRWXU) != -1, "Should have succeed") + rmdir(dir); + + set_mkdir_fail(1); + TEST_ASSERT(mkdir(dir, S_IRWXU) != -1, "Should have succeed") + rmdir(dir); + + TEST_ASSERT(mkdir(dir, S_IRWXU) == -1, "Should have failed") + return TEST_SUCCESS; +} + TEST(test_fail_cleanup) { unlink(TMP_FD_FN); @@ -218,4 +232,5 @@ void register_tests_tests(void) { reg_test("fake_functions", test_fail_strcpy); reg_test("fake_functions", test_fail_strcat); reg_test("fake_functions", test_fail_cleanup); + reg_test("fake_functions", test_fail_mkdir); }