lib

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

commit 8ed9dbdfaf2a01dbe801558975b5284a6fded6eb
parent 81cb8709314629e7d63b2349f44009439ef032c3
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 28 Apr 2017 19:00:18 +0200

Add(Mockup): Add strcpy and strcat mockup functions

Diffstat:
Minc/fail_test.h | 7++++++-
Msrc/test.c | 38++++++++++++++++++++++++++++++++++++++
Mtests/test_tests.c | 30++++++++++++++++++++++++++++++
3 files changed, 74 insertions(+), 1 deletion(-)

diff --git a/inc/fail_test.h b/inc/fail_test.h @@ -27,6 +27,8 @@ # define strdup(str) fl_strdup(str) # define fstat(fd, buf) fl_fstat(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) void *fl_malloc(size_t alloc); ssize_t fl_write(int fd, const void *ptr, size_t len); @@ -35,6 +37,8 @@ int fl_close(int fd); char *fl_strdup(const char *str); 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); void set_malloc_fail(int val); void set_write_fail(int val); @@ -43,7 +47,8 @@ void set_close_fail(int val); void set_strdup_fail(int val); void set_fstat_fail(int val); void set_calloc_fail(int val); - +void set_strcpy_fail(int val); +void set_strcat_fail(int val); # endif /* M_FAIL_TEST_H */ #endif /* COMPILE_WITH_TEST */ diff --git a/src/test.c b/src/test.c @@ -30,6 +30,8 @@ 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; # include <fail_test.h> @@ -40,6 +42,8 @@ static int g_close_fail = -1; static int g_strdup_fail = -1; static int g_fstat_fail = -1; static int g_calloc_fail = -1; +static int g_strcpy_fail = -1; +static int g_strcat_fail = -1; void *fl_malloc(size_t alloc) { if (g_malloc_fail == -1) @@ -120,6 +124,30 @@ int fl_fstat(int fd, struct stat *buf) { return real_fstat(fd, buf); } +char *fl_strcpy(char *dst, const char *src) { + if (g_strcpy_fail == -1) + return real_strcpy(dst, src); + if (g_strcpy_fail == 0) + { + g_strcpy_fail = -1; + return NULL; + } + g_strcpy_fail--; + return real_strcpy(dst, src); +} + +char *fl_strcat(char *dst, const char *src) { + if (g_strcat_fail == -1) + return real_strcat(dst, src); + if (g_strcat_fail == 0) + { + g_strcat_fail = -1; + return NULL; + } + g_strcat_fail--; + return real_strcat(dst, src); +} + void set_malloc_fail(int val) { if (g_malloc_fail == -1) g_malloc_fail = val; @@ -155,4 +183,14 @@ void set_fstat_fail(int val) { g_fstat_fail = val; } +void set_strcpy_fail(int val) { + if (g_strcpy_fail == -1) + g_strcpy_fail = val; +} + +void set_strcat_fail(int val) { + if (g_strcat_fail == -1) + g_strcat_fail = val; +} + #endif /* COMPILE_WITH_TEST */ diff --git a/tests/test_tests.c b/tests/test_tests.c @@ -169,6 +169,34 @@ TEST(test_fail_fstat) { return TEST_SUCCESS; } +TEST(test_fail_strcpy) { + const char *src = "Hello !"; + char dst[sizeof(src)]; + + TEST_ASSERT((strcpy(dst, src) != NULL), "Should have succeed"); + set_strcpy_fail(1); + TEST_ASSERT((strcpy(dst, src) != NULL), "Should have succeed"); + TEST_ASSERT((strcpy(dst, src) == NULL), "Should have failed"); + + return TEST_SUCCESS; +} + +TEST(test_fail_strcat) { + const char *src = "Hello !"; + char dst[sizeof(src) + 5]; + + strcpy(dst, "Test"); + TEST_ASSERT((strcat(dst, src) != NULL), "Should have succeed"); + set_strcat_fail(1); + strcpy(dst, "Test"); + TEST_ASSERT((strcat(dst, src) != NULL), "Should have succeed"); + strcpy(dst, "Test"); + TEST_ASSERT((strcat(dst, src) == NULL), "Should have failed"); + + return TEST_SUCCESS; +} + + TEST(test_fail_cleanup) { unlink(TMP_FD_FN); return TEST_SUCCESS; @@ -187,5 +215,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_strcpy); + reg_test("fake_functions", test_fail_strcat); reg_test("fake_functions", test_fail_cleanup); }