libmpm

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

commit 1e50ed6dd0ed887976cbfcf5fb57e03257c5dea6
parent 092e7adc0c4b75a83defc14dc09520600cc07d60
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed,  3 May 2017 18:54:18 +0200

Fix(Packer): Modify static functions, to implement future archive reading:

Modified some tests too, in order to keep coverage at 100%

Diffstat:
Msrc/packer.c | 15++++++++++-----
Mtests/test_packer.c | 32++++++++++++--------------------
2 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/src/packer.c b/src/packer.c @@ -838,9 +838,8 @@ cleanup: return 0; } -MPX_STATIC bool read_package_header(int fd, packer_t *ctx) +MPX_STATIC bool read_package_header(char *file_content, packer_t *ctx, int *s_ret) { - char *file_content = mpm_read_file_from_fd(fd); int ret = 0, tmp; if (file_content == NULL) @@ -868,13 +867,13 @@ MPX_STATIC bool read_package_header(int fd, packer_t *ctx) tmp = read_package_header_dependencies(file_content + ret, ctx); if (tmp == 0) goto cleanup; + ret += tmp; - free(file_content); + *s_ret = ret; return true; cleanup: - free(file_content); packer_header_free(ctx->header); ctx->header = NULL; return false; @@ -884,6 +883,8 @@ bool packer_read_archive(packer_t *ctx) { int fd; bool ret; + char *archive = NULL; + int cur = 0; if (ctx->type != PACKER_TYPE_ARCHIVE) return false; @@ -892,7 +893,11 @@ bool packer_read_archive(packer_t *ctx) if (fd == -1) return false; - ret = read_package_header(fd, ctx); + archive = mpm_read_file_from_fd(fd); + + printf("%d\n", cur); + ret = read_package_header(archive, ctx, &cur); + printf("%d\n", cur); close(fd); return ret; diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -466,36 +466,28 @@ TEST(packer_read_package_header_package) { return TEST_SUCCESS; } -MPX_STATIC bool read_package_header(int fd, packer_t *ctx); +MPX_STATIC bool read_package_header(char *file_content, packer_t *ctx, int *s_ret); TEST(packer_read_package_header) { packer_t *ctx = packer_init_archive("Test"); - int fd; + char *file = mpm_read_file_from_fn(PACKAGE_OUTPUT_FN); + int ret = 0; + + TEST_ASSERT(read_package_header(NULL, ctx, &ret) == false, "Wrong return"); + TEST_ASSERT(read_package_header("pasmpx", ctx, &ret) == false, "Wrong return"); set_malloc_fail(0); - fd = open(PACKAGE_OUTPUT_FN, O_RDONLY); - TEST_ASSERT(read_package_header(fd, ctx) == false, "Wrong return"); - close(fd); + TEST_ASSERT(read_package_header(file, ctx, &ret) == false, "Wrong return"); set_malloc_fail(1); - fd = open(PACKAGE_OUTPUT_FN, O_RDONLY); - TEST_ASSERT(read_package_header(fd, ctx) == false, "Wrong return"); - close(fd); + TEST_ASSERT(read_package_header(file, ctx, &ret) == false, "Wrong return"); set_malloc_fail(2); - fd = open(PACKAGE_OUTPUT_FN, O_RDONLY); - TEST_ASSERT(read_package_header(fd, ctx) == false, "Wrong return"); - close(fd); - - set_malloc_fail(3); - fd = open(PACKAGE_OUTPUT_FN, O_RDONLY); - TEST_ASSERT(read_package_header(fd, ctx) == false, "Wrong return"); - close(fd); + TEST_ASSERT(read_package_header(file, ctx, &ret) == false, "Wrong return"); - set_malloc_fail(10); - fd = open(PACKAGE_OUTPUT_FN, O_RDONLY); - TEST_ASSERT(read_package_header(fd, ctx) == false, "Wrong return"); - close(fd); + set_malloc_fail(9); + TEST_ASSERT(read_package_header(file, ctx, &ret) == false, "Wrong return"); + free(file); packer_free(ctx); return TEST_SUCCESS;