From 7258ef58105b441d935bbc9842dca85e4170bccc Mon Sep 17 00:00:00 2001 From: Liam Waldron Date: Mon, 10 Mar 2025 21:21:03 -0400 Subject: [PATCH] implement basic unit testing --- Makefile | 7 ++++++ src/libglacier.c | 33 ++++++++++++++--------------- tests/Makefile | 9 ++++++++ tests/unit-tests.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 tests/Makefile create mode 100644 tests/unit-tests.c diff --git a/Makefile b/Makefile index 9d320b0..2e9eb0e 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ IDIR = ./include BDIR = ./build SDIR = ./src +TDIR = ./tests include config.mk @@ -34,6 +35,9 @@ lib: $(SDIR)/libglacier.c $(CC) $(SDIR)/libglacier.c -c $(LIBFLAGS) -o $(BDIR)/lib/libglacier.o $(AR) -rc $(BDIR)/lib/libglacier.a build/lib/libglacier.o +test: $(BDIR) $(TDIR)/Makefile + make -f tests/Makefile + install_lib: $(BDIR)/lib/libglacier.a @echo "[INFO]" @echo "[INFO] Installing library to PREFIX/lib..." @@ -59,3 +63,6 @@ install: install_lib install_head clean: rm -rf $(BDIR) + +distclean: $(BDIR) $(TDIR)/test-suite + rm -rf $(BDIR) $(TDIR)/test-suite diff --git a/src/libglacier.c b/src/libglacier.c index 4c909ee..5a01c46 100644 --- a/src/libglacier.c +++ b/src/libglacier.c @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include #include @@ -669,7 +669,7 @@ print_hash(uchar *hash, uint length) * */ -int +/*int stash_hash(uchar *stored_hash, uchar *hash, uint length) { if (sizeof(stored_hash) != 32 && LG_VERBOSE == 1) { @@ -693,7 +693,7 @@ stash_hash(uchar *stored_hash, uchar *hash, uint length) } return 0; -} +} */ /* * hash_file @@ -704,8 +704,8 @@ stash_hash(uchar *stored_hash, uchar *hash, uint length) * */ -int -hash_file(const char *filename, uchar *destination_string) +unsigned char +hash_file(const char *filename) { FILE *data = fopen(filename, "rb"); if (! data && LG_VERBOSE == 1) { @@ -749,20 +749,19 @@ hash_file(const char *filename, uchar *destination_string) fclose(data); - if (sizeof(destination_hash) != EVP_MAX_MD_SIZE && LG_VERBOSE == 1) { - errlog("in hash_file()"); - errlog("destination hash is invalid size"); - errlog("ensure hash is declared with size EVP_MAX_MD_SIZE"); - return 5; - } - else if (sizeof(destination_hash) != EVP_MAX_MD_SIZE) { - return 5; - } - + uchar hash[EVP_MAX_MD_SIZE]; uint hash_length; - if (EVP_DigestFinal_ex(context, destination_hash, &hash_length) != 1) { + if (EVP_DigestFinal_ex(context, hash, &hash_length) != 1) { + if (LG_VERBOSE == 1) { + errlog("in hash_file()"); + errlog("error finalizing digest"); + } EVP_MD_CTX_free(context); - return 6; + return 5; } + + EVP_MD_CTX_free(context); + + return *hash; } diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..a34d67c --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,9 @@ +# +# Makefile +# + +include ../config.mk + +all: unit-tests.c + $(CC) unit-tests.c -o test-suite -lcunit -lconfig ../build/libglacier.a -Wall -Wextra + ./test-suite diff --git a/tests/unit-tests.c b/tests/unit-tests.c new file mode 100644 index 0000000..c1c9b5d --- /dev/null +++ b/tests/unit-tests.c @@ -0,0 +1,53 @@ +/* + * unit-tests.c - Unit tests for libglacier + * + * This file is part of Glacier. + * + * Glacier is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * Glacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Glacier. If + * not, see . + */ + +#include +#include +#include + +#include "../include/config.h" +#include "../include/log.h" +#include "../include/pkgops.h" +#include "../include/runtime.h" +#include "../include/security.h" + +#include + +void +test_is_process_root(void) +{ + CU_ASSERT(is_process_root() == 0); + + uid_t saved_uid = getuid(); + setuid(0); + CU_ASSERT(is_process_root() == 1); + + setuid((int)saved_uid); +} + +int +main() +{ + if (CUE_SUCCESS != CU_initialize_registry()) { + return CU_get_error(); + } + + CU_basic_set_mode(CU_BRM_VERBOSE); + + CU_basic_run_tests(); + return CU_get_error(); +}