libmpm

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

commit 0f2dc56924ffd4207de3a7654915de12c02d96a5
parent b15aa0cade0879db010cead6314f70950fbf3af6
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Sat,  1 Apr 2017 22:10:25 +0200

Add(Tests): Add test for good archive configuration

Diffstat:
MMakefile | 6++++--
Msrc/packer.c | 14+++++++++++---
Atests/packer/right/package.json | 23+++++++++++++++++++++++
Mtests/test_packer.c | 10++++++++++
4 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -85,12 +85,14 @@ doc: check: make -C lib/libmorphux/ test $(MAKE) fclean all OFLAGS="-DCOMPILE_WITH_TEST -Wno-error -std=gnu99 -g" - make -C tests clean check + make -C tests fclean all + cd tests && ./test coverage: make -C lib/libmorphux/ test $(MAKE) fclean all OFLAGS="-std=gnu99 -g -O0 -coverage -lcov -DCOMPILE_WITH_TEST" - make -C tests coverage check + make -C tests coverage + cd tests && ./test gcov -o src/ $(SRCS) make -C tests fclean diff --git a/src/packer.c b/src/packer.c @@ -23,6 +23,7 @@ static packer_t *packer_init(const char *str) { if (ret == NULL) return NULL; + ret->json = NULL; ret->str = strdup(str); if (ret->str == NULL) goto cleanup; @@ -58,6 +59,7 @@ packer_t *packer_init_archive(const char *path) { void packer_free(packer_t *ptr) { if (ptr != NULL) { + json_object_put(ptr->json); free(ptr->str); free(ptr); } @@ -67,6 +69,7 @@ bool packer_read_dir(packer_t *ctx) { struct stat st; int fd; char *file_c = NULL; + char *old_pwd = getenv("PWD"); assert(ctx != NULL); @@ -79,19 +82,22 @@ bool packer_read_dir(packer_t *ctx) { fd = open(PACKER_DEF_CONF_FN, O_RDONLY); if (fd == -1) goto error; + if (fstat(fd, &st) == -1) goto error; - file_c = malloc(st.st_size); + file_c = malloc(st.st_size + 1); if (file_c == NULL) goto error; if (read(fd, file_c, st.st_size) == -1) goto error; - ctx->json = json_tokener_parse(file_c); + file_c[st.st_size] = 0; + ctx->json = json_tokener_parse(file_c); if (ctx->json == NULL) goto error; + chdir(old_pwd); free(file_c); close(fd); return true; @@ -99,6 +105,8 @@ bool packer_read_dir(packer_t *ctx) { error: if (file_c != NULL) free(file_c); - close(fd); + if (fd != -1) + close(fd); + chdir(old_pwd); return false; } diff --git a/tests/packer/right/package.json b/tests/packer/right/package.json @@ -0,0 +1,23 @@ +{ + "package": { + "name": "test", + "version": "2.0", + "description": "This is a description. Yeah I know, mind blowing." + }, + "compilation": { + "configure": [ + {"mandir": "/usr/share/man"}, + "with-shared", + "without-debug", + "without-normal", + "enable-pc-files", + "enable-widec" + ], + "make": "", + "test": "", + "install": "" + }, + "dependencies": [ + "something-5.5" + ] +} diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -59,10 +59,20 @@ TEST(packer_read_dir_no_configuration) { return TEST_SUCCESS; } +TEST(packer_right) { + packer_t *ptr; + + ptr = packer_init_dir("packer/right/"); + TEST_ASSERT(packer_read_dir(ptr) == true, "An error happened"); + packer_free(ptr); + return TEST_SUCCESS; +} + void register_test_packer(void) { 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); reg_test("packer", packer_read_dir_no_configuration); + reg_test("packer", packer_right); }