lib

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

commit d27f2da424778fac5713df80605dabe1711181d2
parent ed62831b740a07cc7255a64edb316479a2aea6d1
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Sun, 28 May 2017 15:53:25 +0200

Add(Mockups): Add stat(2) mockup

Diffstat:
Minc/fail_test.h | 4+++-
Msrc/test.c | 4++++
Mtests/test_tests.c | 17+++++++++++++++++
3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/inc/fail_test.h b/inc/fail_test.h @@ -26,11 +26,12 @@ # define close(fd) fl_close(fd) # define strdup(str) fl_strdup(str) # define fstat(fd, buf) fl_fstat(fd, buf) +# define stat(fd, buf) fl_stat(fd, buf) # 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) -# define fork fl_fork +# define fork fl_fork # define MOCK_SET_DECL(fn_name, type, ...) \ void set_##fn_name##_fail(int val); \ @@ -42,6 +43,7 @@ MOCK_SET_DECL(read, ssize_t, int, void *, size_t); MOCK_SET_DECL(close, int, int); MOCK_SET_DECL(strdup, char *, const char *); MOCK_SET_DECL(fstat, int, int, struct stat *); +MOCK_SET_DECL(stat, int, const char *, struct stat *); MOCK_SET_DECL(calloc, void *, size_t, size_t); MOCK_SET_DECL(strcpy, char *, char *, const char *); MOCK_SET_DECL(strcat, char *, char *, const char *); diff --git a/src/test.c b/src/test.c @@ -68,6 +68,10 @@ MOCK_SET_IMP(fstat, int, -1, int fd, struct stat *buf) { MOCK_REAL_CALL(fstat, fd, buf); } +MOCK_SET_IMP(stat, int, -1, const char *fn, struct stat *buf) { + MOCK_REAL_CALL(stat, fn, buf); +} + MOCK_SET_IMP(calloc, void *, NULL, size_t nmemb, size_t size) { MOCK_REAL_CALL(calloc, nmemb, size) } diff --git a/tests/test_tests.c b/tests/test_tests.c @@ -169,6 +169,22 @@ TEST(test_fail_fstat) { return TEST_SUCCESS; } +TEST(test_fail_stat) { + struct stat buf; + int ret; + + set_stat_fail(1); + ret = stat("Makefile", &buf); + TEST_ASSERT(ret != -1, "stat failed"); + ret = stat("Makefile", &buf); + TEST_ASSERT(ret == -1, "stat succeed"); + ret = stat("Makefile", &buf); + TEST_ASSERT(ret != -1, "stat failed"); + + return TEST_SUCCESS; +} + + TEST(test_fail_strcpy) { const char *src = "Hello !"; char dst[sizeof(src)]; @@ -267,6 +283,7 @@ void register_tests_tests(void) { reg_test("fake_functions", test_fail_close); reg_test("fake_functions", test_fail_strdup); reg_test("fake_functions", test_fail_fstat); + reg_test("fake_functions", test_fail_stat); reg_test("fake_functions", test_fail_strcpy); reg_test("fake_functions", test_fail_strcat); reg_test("fake_functions", test_fail_cleanup);