libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit 2ea311232f8703829c20cfdd10b4fc04b93d85cd
parent 19df8b1d5d65661a0ca78c30a42fef370d7f31be
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 16 Jun 2017 19:17:24 +0200

Add(Tests): Add tests for packer_file.c

Diffstat:
Msrc/packer_file.c | 3+++
Mtests/test_packer_file.c | 31+++++++++++++++++++++++++++++++
2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/src/packer_file.c b/src/packer_file.c @@ -129,7 +129,10 @@ bool get_file_information(packer_file_t *file) { return false; if (stat(file->fn, &f_stat) == -1) + { + SET_ERR_STR_FMT("Cannot open file: %s", file->fn); return false; + } file_size = f_stat.st_size; diff --git a/tests/test_packer_file.c b/tests/test_packer_file.c @@ -1,9 +1,40 @@ #include "test.h" TEST(read_files_from_dir) { + mlist_t *files = NULL, *dirs = NULL; + + set_stat_fail(0); + TEST_ASSERT(read_files_from_dir("/tmp/", &files, &dirs) == false, "Error did not raise"); + return TEST_SUCCESS; +} + +# define _TEST_FN "test_get_file_information_libmpm" +TEST(file_get_file_information) { + FILE *fd = fopen("/tmp/" _TEST_FN, "w+"); + + fprintf(fd, "This is the file content"); + fclose(fd); + TEST_ASSERT(get_file_information(NULL) == false, "Error did not raise"); + + packer_file_t *ptr = packer_file_init(_TEST_FN, "/tmp/"); + + TEST_ASSERT(ptr != NULL, "Cannot allocate the packer_file_t ptr"); + /* Fail on chunk = malloc(...) */ + set_malloc_fail(1); + TEST_ASSERT_FMT(get_file_information(ptr) == false, "Error did not raise %s", GET_ERR_STR()); + + /* Fail on file->content = malloc(...) */ + set_malloc_fail(2); + TEST_ASSERT(get_file_information(ptr) == false, "Error did not raise"); + + set_malloc_fail(-1); + packer_file_free(ptr); + free(ptr); + return TEST_SUCCESS; } void register_test_packer_file(void) { reg_test("packer_file", read_files_from_dir); + reg_test("packer_file", file_get_file_information); }