/*
* 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)
{
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(init_config());
}
void
test_verify_signature(void)
{
/* This test assumes that both files exist in the test environment */
CU_ASSERT_EQUAL(verify_signature("test_files/package.tar", "test_files/package.tar.sig"), 0);
/* Test with non-existent files */
CU_ASSERT_EQUAL(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 */
char *valid_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; /* Empty file hash */
/* Test file existence check */
CU_ASSERT_EQUAL(check_integrity("non_existent_file.tar", valid_hash), 2);
/* 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 get_system_profile doesn't return NULL */
CU_ASSERT_PTR_NOT_NULL(get_system_profile());
/* Ensure the profile format seems correct (contains a dash) */
const char *profile = get_system_profile();
CU_ASSERT_TRUE(strchr(profile, '-') != NULL);
}
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 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 verify_signature()", test_verify_signature)) {
CU_cleanup_registry();
return CU_get_error();
}
if (! CU_add_test(security_tests, "test of check_integrity()", test_check_integrity)) {
CU_cleanup_registry();
return CU_get_error();
}
if (! CU_add_test(runtime_tests, "test of 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();
}