implement basic unit testing

This commit is contained in:
Liam Waldron 2025-03-10 21:21:03 -04:00
parent 60996e3de2
commit 7258ef5810
4 changed files with 85 additions and 17 deletions

View File

@ -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

View File

@ -20,7 +20,7 @@
#include <errno.h>
#include <libconfig.h>
#include <locale.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@ -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;
}

9
tests/Makefile Normal file
View File

@ -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

53
tests/unit-tests.c Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <libconfig.h>
#include <unistd.h>
#include "../include/config.h"
#include "../include/log.h"
#include "../include/pkgops.h"
#include "../include/runtime.h"
#include "../include/security.h"
#include <CUnit/Basic.h>
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();
}