libmpm

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

commit ca6ec28bbce91b9c9da748cedf1b7cd61972c289
parent d1df2c37312cb477426972cf861b7e60584dba15
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 25 Apr 2017 19:34:00 +0200

Add(Tests): Add test for packer

Diffstat:
Msrc/packer.c | 15++++++++++++++-
Mtests/test_packer.c | 42++++++++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/src/packer.c b/src/packer.c @@ -481,6 +481,13 @@ static packer_file_t *packer_file_init(const char *file, const char *dir) { return NULL; ret->fn = malloc(strlen(file) + strlen(dir) + 1); + + if (ret->fn == NULL) + { + free(ret); + return NULL; + } + if (strcpy(ret->fn, dir) == NULL) goto error; @@ -535,6 +542,12 @@ static bool get_file_information(packer_file_t *file) { file->file_size = stream.total_out; file->content = malloc(file->file_size); + if (file->content == NULL) + { + free(chunk); + return false; + } + memcpy(file->content, chunk, file->file_size); free(file_content); @@ -593,7 +606,7 @@ error: return false; } -static bool write_packer_sources(FILE *fd, packer_t *ctx, const char *dir_name) { +MPX_STATIC bool write_packer_sources(FILE *fd, packer_t *ctx, const char *dir_name) { mlist_t *files_list = NULL; mlist_t *dirs = NULL; mlist_t *tmp = NULL; diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -304,6 +304,46 @@ TEST(packer_create_archive_wrong_type) { return TEST_SUCCESS; } +TEST(packer_create_archive_2) { + packer_t *ptr; + + ptr = packer_init_dir("packer/right/"); + TEST_ASSERT(packer_read_dir(ptr) == true, "An error happened"); + + set_malloc_fail(2); + TEST_ASSERT(packer_create_archive(ptr, "package.mpx") == false, + "Error did not raise") + + set_malloc_fail(3); + TEST_ASSERT(packer_create_archive(ptr, "package.mpx") == false, + "Error did not raise") + + set_malloc_fail(8); + TEST_ASSERT(packer_create_archive(ptr, "package.mpx") == false, + "Error did not raise") + + set_malloc_fail(15); + TEST_ASSERT(packer_create_archive(ptr, "package.mpx") == false, + "Error did not raise") + + packer_free(ptr); + return TEST_SUCCESS; +} + +MPX_STATIC bool write_packer_sources(FILE *fd, packer_t *ctx, const char *dir_name); +TEST(packer_write_packer_sources) { + FILE *fd = fopen("package_2.mpx", "w+"); + packer_t *ptr; + + ptr = packer_init_dir("packer/right/"); + + set_malloc_fail(0); + TEST_ASSERT(write_packer_sources(fd, ptr, "srcs/") == false, "Error did not raise"); + packer_free(ptr); + fclose(fd); + return TEST_SUCCESS; +} + TEST(packer_read_archive_1) { packer_t *ctx; @@ -486,6 +526,8 @@ void register_test_packer(void) { reg_test("packer", packer_header_comp_init); reg_test("packer", packer_header_deps_init); reg_test("packer", packer_create_archive_1); + reg_test("packer", packer_create_archive_2); + reg_test("packer", packer_write_packer_sources); reg_test("packer", packer_create_archive_wrong_fn); reg_test("packer", packer_create_archive_wrong_type); reg_test("packer", packer_read_archive_1);