libglacier/tests/unit-tests.c

138 lines
3.7 KiB
C

/*
* 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)
{
if (is_process_root() != 0) {
CU_FAIL("is_process_root() with UID != 0 failed");
}
else if (is_process_root() == 0) {
CU_PASS("is_process_root() with UID != 0 passed");
}
}
void
test_init_config(void)
{
CU_ASSERT_TRUE(gl_init_config());
}
void
test_verify_signature(void)
{
/* This test assumes that both files exist in the test environment */
CU_ASSERT_EQUAL(gl_verify_signature("test_files/package.tar", "test_files/package.tar.sig"), 0);
/* Test with non-existent files */
CU_ASSERT_EQUAL(gl_verify_signature("non_existent_file.tar", "non_existent_file.tar.sig"), 2);
}
void
test_check_integrity(void)
{
/* This test assumes that test_files/package.tar exists in the test environment
with a known hash value for testing */
const char *valid_hash = "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
/* Test file existence check */
CU_ASSERT_EQUAL(gl_check_integrity("test_files/package.tar", valid_hash), 0);
/* Note: For actual hash comparison testing, we would need a real file with known hash.
These tests would need to be adjusted with real files and hashes for proper testing. */
}
void
test_get_system_profile(void)
{
/* Ensure that gl_get_system_profile doesn't return NULL */
CU_ASSERT_PTR_NOT_NULL(gl_get_system_profile());
const char *profile = gl_get_system_profile();
CU_ASSERT_PTR_NOT_NULL(profile);
CU_ASSERT_STRING_NOT_EQUAL(profile, "");
}
int
main(void)
{
if (CUE_SUCCESS != CU_initialize_registry()) {
return CU_get_error();
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_pSuite runtime_tests = CU_add_suite("Runtime Functions Suite", NULL, NULL);
if (! runtime_tests) {
CU_cleanup_registry();
return CU_get_error();
}
CU_pSuite config_tests = CU_add_suite("Configuration Functions Suite", NULL, NULL);
if (! config_tests) {
CU_cleanup_registry();
return CU_get_error();
}
if (! CU_add_test(runtime_tests, "test of is_process_root()", test_is_process_root)) {
CU_cleanup_registry();
return CU_get_error();
}
if (! CU_add_test(config_tests, "test of gl_init_config()", test_init_config)) {
CU_cleanup_registry();
return CU_get_error();
}
CU_pSuite security_tests = CU_add_suite("Security Functions Suite", NULL, NULL);
if (! security_tests) {
CU_cleanup_registry();
return CU_get_error();
}
if (! CU_add_test(security_tests, "test of gl_verify_signature()", test_verify_signature)) {
CU_cleanup_registry();
return CU_get_error();
}
if (! CU_add_test(security_tests, "test of gl_check_integrity()", test_check_integrity)) {
CU_cleanup_registry();
return CU_get_error();
}
if (! CU_add_test(runtime_tests, "test of gl_get_system_profile()", test_get_system_profile)) {
CU_cleanup_registry();
return CU_get_error();
}
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}