libmpm

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

commit 900d757faade1336d9f9b0ac987ffed328810d03
parent 88c829598712b6a9c041e951369bc29c8ed5a500
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Sun, 16 Apr 2017 16:28:58 +0200

Add(Packer): read_archive test

Diffstat:
Minc/packer.h | 11+++++++++++
Msrc/packer.c | 14+++++++++++---
Mtests/test_packer.c | 12+++++++++++-
3 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/inc/packer.h b/inc/packer.h @@ -134,4 +134,15 @@ bool packer_read_dir(packer_t *ctx); */ bool packer_create_archive(packer_t *ctx, const char *archive_path); +/*! + * \brief Read a .mpx archive + * + * \param[in] ctx Already initialized packer_t struct + * + * Read an .mpx archive in order to extract compilation informations off it + * + * \return True on success, false on failure + */ +bool packer_read_archive(packer_t *ctx); + #endif /* PACKER_H */ diff --git a/src/packer.c b/src/packer.c @@ -267,7 +267,7 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) goto cleanup; if (json_object_get_string_len(tmp) == 0) - ctx->header->compilation->test = strdup(PACKER_INST_DEF); + ctx->header->compilation->install = strdup(PACKER_INST_DEF); else ctx->header->compilation->install = strdup(json_object_get_string(tmp)); } @@ -451,7 +451,7 @@ MPX_STATIC void write_package_header(FILE *fd, packer_t *ctx) { fwrite(&list_len, sizeof(u32_t), 1, fd); list_for_each(h->dependencies->list, tmp, tmp_str) { - fprintf(fd, "%s%c", tmp_str, 0); + fprintf(fd, "%s%c", tmp_str, '\0'); } } @@ -475,8 +475,16 @@ static bool read_package_header(int fd, packer_t *ctx) { (void)ctx; char *file_content = mpm_read_file_from_fd(fd); - printf("%s\n", file_content); + + if (strncmp(file_content, "MPX", 3) != 0) + goto cleanup; + + free(file_content); return true; + +cleanup: + free(file_content); + return false; } bool packer_read_archive(packer_t *ctx) diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -331,13 +331,22 @@ TEST(packer_read_archive_3) { return TEST_SUCCESS; } +TEST(packer_read_archive_4) { + packer_t *ctx; + + ctx = packer_init_archive("/bin/ls"); + TEST_ASSERT(packer_read_archive(ctx) == false, "Wrong return"); + packer_free(ctx); + return TEST_SUCCESS; +} + TEST(packer_create_archive_cleanup) { unlink(PACKAGE_OUTPUT_FN); return TEST_SUCCESS; } void register_test_packer(void) { - reg_test("packer", packer_init_dir); + reg_test("packer", packer_init_dir); reg_test("packer", packer_init_archive); reg_test("packer", packer_read_dir_wrong_type); reg_test("packer", packer_read_dir_wrong_dir); @@ -373,5 +382,6 @@ void register_test_packer(void) { reg_test("packer", packer_read_archive_1); reg_test("packer", packer_read_archive_2); reg_test("packer", packer_read_archive_3); + reg_test("packer", packer_read_archive_4); reg_test("packer", packer_create_archive_cleanup); }