libmpm

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

commit ffe8f35d4b3d8d06d09050dd6bf1d520f3cc47b4
parent 2ea311232f8703829c20cfdd10b4fc04b93d85cd
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 19 Jun 2017 18:01:25 +0200

Fix(Tests): Better tests for packer header function

Diffstat:
Msrc/packer.c | 3++-
Mtests/test_packer.c | 49+++++++++++++++++++++++++++++++++++++++++++------
2 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/src/packer.c b/src/packer.c @@ -873,7 +873,9 @@ MPX_STATIC int read_package_header_compilation(char *file, packer_t *ctx) return ret; if (read_conf_opt(file, &ctx->header->compilation.configure, &ret) != true) + { goto cleanup; + } ctx->header->compilation.make = strdup(file + ret); if (ctx->header->compilation.make == NULL) @@ -984,7 +986,6 @@ MPX_STATIC bool read_package_header(char *file_content, packer_t *ctx, int *s_re tmp = read_package_header_dependencies(file_content + ret, ctx); if (tmp == 0) goto cleanup; - ret += tmp; *s_ret = ret; diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -415,10 +415,31 @@ TEST(packer_read_package_header_compilation) { MPX_STATIC int read_package_header_package(const char *file, packer_t *ctx); TEST(packer_read_package_header_package) { + set_malloc_fail(-1); + set_strdup_fail(-1); + char tmp[] = "Name\0Version\0Desc\0"; + packer_t *ctx = packer_init_archive("Test"); + + TEST_ASSERT(ctx != NULL, "Can't init archive"); + + ctx->header = packer_header_init(); - set_malloc_fail(0); TEST_ASSERT(read_package_header_package(tmp, NULL) == 0, "Wrong return"); + + set_strdup_fail(0); + TEST_ASSERT(read_package_header_package(tmp, ctx) == 0, "Wrong return"); + + set_strdup_fail(1); + TEST_ASSERT(read_package_header_package(tmp, ctx) == 0, "Wrong return"); + + set_strdup_fail(2); + TEST_ASSERT(read_package_header_package(tmp, ctx) == 0, "Wrong return"); + + set_strdup_fail(3); + TEST_ASSERT(read_package_header_package(tmp, ctx) == 0, "Wrong return"); + + set_malloc_fail(-1); return TEST_SUCCESS; } MPX_STATIC bool read_package_header(char *file_content, packer_t *ctx, int *s_ret); @@ -429,21 +450,34 @@ TEST(packer_read_package_header) { TEST_ASSERT(read_package_header(NULL, ctx, &ret) == false, "Wrong return"); TEST_ASSERT(read_package_header("pasmpx", ctx, &ret) == false, "Wrong return"); + packer_free(ctx); + ctx = packer_init_archive("Test"); set_malloc_fail(0); TEST_ASSERT(read_package_header(file, ctx, &ret) == false, "Wrong return"); + packer_free(ctx); + free(file); - set_malloc_fail(1); + ctx = packer_init_archive("Test"); + file = mpm_read_file_from_fn(PACKAGE_OUTPUT_FN); + set_strdup_fail(0); TEST_ASSERT(read_package_header(file, ctx, &ret) == false, "Wrong return"); + packer_free(ctx); + free(file); - set_malloc_fail(2); + ctx = packer_init_archive("Test"); + file = mpm_read_file_from_fn(PACKAGE_OUTPUT_FN); + set_strdup_fail(8); TEST_ASSERT(read_package_header(file, ctx, &ret) == false, "Wrong return"); + packer_free(ctx); + free(file); - set_malloc_fail(9); + ctx = packer_init_archive("Test"); + file = mpm_read_file_from_fn(PACKAGE_OUTPUT_FN); + set_strdup_fail(15); TEST_ASSERT(read_package_header(file, ctx, &ret) == false, "Wrong return"); - - free(file); packer_free(ctx); + free(file); return TEST_SUCCESS; } @@ -465,8 +499,11 @@ TEST(packer_file_init) { } TEST(packer_extract_archive_1) { + set_strdup_fail(-1); + packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + SET_ERR_STR(""); TEST_ASSERT_FMT(packer_extract_archive(ctx, "/tmp") == true, "Wrong return: %s\n", GET_ERR_STR()); packer_free(ctx); return TEST_SUCCESS;