libmpm

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

commit 76ca57dbdf67f784dfef260eca60af624b98c7d3
parent 3816006ae960af40257a66cbc07a39f3ec4e597a
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 10 May 2017 12:25:15 +0200

Add(Tests): Add tests for extract_archive error cases

Diffstat:
Msrc/packer.c | 5++---
Mtests/test_packer.c | 53++++++++++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/src/packer.c b/src/packer.c @@ -735,7 +735,7 @@ cleanup: return false; } -static bool read_package_files(char *content, packer_t *ctx, off_t total_size) { +MPX_STATIC bool read_package_files(char *content, packer_t *ctx, off_t total_size) { if (content == NULL || ctx == NULL) return false; @@ -850,9 +850,7 @@ bool packer_extract_archive(packer_t *ctx, const char *dir, char **output_dir) { ctr = t_ctr; if (chdir(dir) == -1) - { goto cleanup; - } *output_dir = packer_create_directory_name(ctx, '-'); @@ -866,6 +864,7 @@ bool packer_extract_archive(packer_t *ctx, const char *dir, char **output_dir) { file = read_packer_file_from_binary(archive, &ctr); if (file == NULL) goto cleanup; + if (packer_file_to_disk(file) == false) goto cleanup; packer_file_free(file); diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -573,16 +573,61 @@ TEST(packer_read_archive_header) { packer_t *ctx = NULL; ctx = packer_init_dir("test/"); - TEST_ASSERT(packer_read_archive_header(ctx) == NULL, "Error did not raise"); + TEST_ASSERT(packer_read_archive_header(ctx) == false, "Error did not raise"); packer_free(ctx); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_malloc_fail(0); - TEST_ASSERT(packer_read_archive_header(ctx) == NULL, "Error did not raise"); + TEST_ASSERT(packer_read_archive_header(ctx) == false, "Error did not raise"); - TEST_ASSERT(packer_read_archive_header(ctx) != NULL, "Something happend"); + TEST_ASSERT(packer_read_archive_header(ctx) != false, "Something happend"); TEST_ASSERT(strcmp(ctx->header->package->name, "test") == 0, "Name is wrong"); TEST_ASSERT(strcmp(ctx->header->package->version, "2.0") == 0, "Name is wrong"); + + packer_free(ctx); + return TEST_SUCCESS; +} + +MPX_STATIC bool read_package_files(char *content, packer_t *ctx, off_t total_size); +TEST(read_package_files) { + char *content = "Truc"; + packer_t *ctx; + + ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + + TEST_ASSERT(read_package_files(NULL, NULL, 0) == false, "Error did not raise"); + + set_malloc_fail(0); + TEST_ASSERT(read_package_files(content, ctx, sizeof(content)) == false, "Error did not raise"); + packer_free(ctx); + return TEST_SUCCESS; +} + +TEST(packer_extract_archive_2) { + packer_t *ctx = NULL; + char *output_dir; + + ctx = packer_init_dir("Test"); + TEST_ASSERT(packer_extract_archive(ctx, NULL, NULL) == false, "Error did not raise"); + packer_free(ctx); + + ctx = packer_init_archive("Not a valid file"); + TEST_ASSERT(packer_extract_archive(ctx, NULL, NULL) == false, "Error did not raise"); + packer_free(ctx); + + ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + set_malloc_fail(0); + TEST_ASSERT(packer_extract_archive(ctx, NULL, NULL) == false, "Error did not raise"); + + TEST_ASSERT(packer_extract_archive(ctx, "/totally/not/valid/dir", NULL) == false, "Error did not raise"); + TEST_ASSERT(packer_extract_archive(ctx, "/", &output_dir) == false, "Error did not raise"); + + set_malloc_fail(12); + TEST_ASSERT(packer_extract_archive(ctx, "/tmp", &output_dir) == false, "Error did not raise"); + + set_mkdir_fail(1); + TEST_ASSERT(packer_extract_archive(ctx, "/tmp", &output_dir) == false, "Error did not raise"); + return TEST_SUCCESS; } @@ -638,9 +683,11 @@ void register_test_packer(void) { reg_test("packer", packer_read_package_header); reg_test("packer", packer_file_init); reg_test("packer", packer_extract_archive_1); + reg_test("packer", packer_extract_archive_2); reg_test("packer", read_packer_file_from_binary); reg_test("packer", packer_file_to_disk); reg_test("packer", packer_create_directory_name); reg_test("packer", packer_read_archive_header); + reg_test("packer", read_package_files); reg_test("packer", packer_create_archive_cleanup); }