Precommit (#1)

* first commit

* cleanup
This commit is contained in:
tompzf
2025-11-04 13:28:06 +01:00
committed by GitHub
parent dba45dc636
commit 6ed4b1534e
898 changed files with 256340 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
# Define project
project(RepositoryTests VERSION 1.0 LANGUAGES CXX)
# Find the IDL compiler
# REMARKS The compiler can only be found after it has build. Having both the compiler and the unittest project build, causes an
# error during the scanning phase of CMake.
#find_program(SDVIDL NAMES "sdv_idl_compiler" PATHS "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/../../bin/" NO_CACHE)
set (SDVIDL "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/../../bin/sdv_idl_compiler")
message("Use IDL compiler: ${SDVIDL}")
# Compile the IDL
add_custom_command(
OUTPUT ${PROJECT_SOURCE_DIR}/generated/IComponent.h
DEPENDS sdv_idl_compiler
MAIN_DEPENDENCY IComponent.idl
COMMENT "Build IComponent.idl"
COMMAND ${SDVIDL} "${PROJECT_SOURCE_DIR}/IComponent.idl" "-I${PROJECT_SOURCE_DIR}/../../../export" "-O${PROJECT_SOURCE_DIR}/generated"
VERBATIM
)
# build module example for the test
add_library(UnitTest_Repository_test_module SHARED test_component.cpp generated/IComponent.h)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_options(UnitTest_Repository_test_module PUBLIC -fPIC)
target_link_libraries(UnitTest_Repository_test_module GTest::GTest)
if (WIN32)
target_link_libraries(UnitTest_Repository_test_module Ws2_32 Winmm Rpcrt4.lib)
else()
target_link_libraries(UnitTest_Repository_test_module ${CMAKE_DL_LIBS} rt)
endif()
else()
target_link_libraries(UnitTest_Repository_test_module GTest::GTest Rpcrt4.lib)
endif()
set_target_properties(UnitTest_Repository_test_module PROPERTIES PREFIX "")
set_target_properties(UnitTest_Repository_test_module PROPERTIES SUFFIX ".sdv")
# Define target
add_executable(UnitTest_Repository repository_test.cpp generated/IComponent.h "mock.h" "main.cpp")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_options(UnitTest_Repository PUBLIC -fPIC)
target_link_libraries(UnitTest_Repository GTest::GTest)
if (WIN32)
target_link_libraries(UnitTest_Repository Ws2_32 Winmm Rpcrt4.lib)
else()
target_link_libraries(UnitTest_Repository ${CMAKE_DL_LIBS} rt)
endif()
else()
target_link_libraries(UnitTest_Repository GTest::GTest Rpcrt4.lib)
endif()
# Add the test
add_test(NAME UnitTest_Repository COMMAND UnitTest_Repository WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
# Execute the test
add_custom_command(TARGET UnitTest_Repository POST_BUILD
COMMAND ${CMAKE_COMMAND} -E env TEST_EXECUTION_MODE=CMake "$<TARGET_FILE:UnitTest_Repository>" --gtest_output=xml:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/UnitTest_Repository.xml
VERBATIM
)
# Build dependencies
add_dependencies(UnitTest_Repository_test_module dependency_sdv_components)
add_dependencies(UnitTest_Repository dependency_sdv_components)
add_dependencies(UnitTest_Repository UnitTest_Repository_test_module)

View File

@@ -0,0 +1,17 @@
#include <interfaces/core.idl>
/**
* @brief Example interface
*/
interface ITestLock
{
/**
* @brief Lock Mutex
*/
void Lock();
/**
* @brief Unlock Mutex
*/
void Unlock();
};

View File

@@ -0,0 +1,26 @@
#include <gtest/gtest.h>
#include "../../../global/process_watchdog.h"
#include "mock.h"
#include "../../../sdv_services/core/toml_parser/parser_toml.cpp"
#include "../../../sdv_services/core/toml_parser/lexer_toml.cpp"
#include "../../../sdv_services/core/toml_parser/parser_node_toml.cpp"
#include "../../../sdv_services/core/toml_parser/character_reader_utf_8.cpp"
#include "../../../sdv_services/core/module_control.cpp"
#include "../../../sdv_services/core/module.cpp"
#include "../../../sdv_services/core/repository.cpp"
#include "../../../sdv_services/core/iso_monitor.cpp"
#include "../../../sdv_services/core/object_lifetime_control.cpp"
#include "../../../sdv_services/core/app_config.cpp"
#include "../../../sdv_services/core/installation_manifest.cpp"
#if defined(_WIN32) && defined(_UNICODE)
extern "C" int wmain(int argc, wchar_t* argv[])
#else
extern "C" int main(int argc, char* argv[])
#endif
{
CProcessWatchdog watchdog;
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,73 @@
#ifndef MOCK_H
#define MOCK_H
#include <interfaces/app.h>
#define DO_NOT_INCLUDE_IN_UNIT_TEST
#include "../../../sdv_services/core/app_control.h"
#include "../../../sdv_services/core/sdv_core.h"
class CMock
{
public:
//void DestroyModuleObjects(sdv::core::TModuleID) {}
bool IsStandaloneApplication() { return true; }
bool IsEssentialApplication() { return false; }
bool IsMainApplication() { return false; }
bool IsIsolatedApplication() { return false; }
bool IsMaintenanceApplication() { return false; }
bool IsExternalApplication() { return false; }
bool IsConsoleSilent() { return true; }
bool IsConsoleVerbose() { return false; }
uint32_t GetInstanceID() { return 1000u; }
void RequestShutdown() {}
sdv::app::EAppOperationState GetOperationState() const { return sdv::app::EAppOperationState::running; }
std::filesystem::path GetInstallDir() const { return std::filesystem::path(); }
std::filesystem::path FindInstalledModule(const std::filesystem::path&) const { return {}; }
std::optional<CInstallManifest::SComponent> FindInstalledComponent(const std::string&) const { return {}; }
std::string FindInstalledModuleManifest(const std::filesystem::path&) { return {}; }
};
inline CMock& GetMock()
{
static CMock mock;
return mock;
}
#define CAppControl CMock
#define GetAppControl GetMock
#define GetAppConfig GetMock
#include "../../../sdv_services/core/toml_parser/parser_toml.h"
#include "../../../sdv_services/core/toml_parser/parser_node_toml.h"
#include "../../../sdv_services/core/module_control.h"
#include "../../../sdv_services/core/repository.h"
#include "../../../sdv_services/core/app_config.h"
inline std::filesystem::path GetCoreDirectory() { return "../../bin"; }
class CHelper
{
public:
CHelper(CModuleControl& modulectrl, CRepository& repository) { m_pModuleControl = &modulectrl; m_pRepository = &repository; }
~CHelper() {}
static CModuleControl& GetModuleControl()
{
if (!m_pModuleControl) throw std::bad_exception();
return *m_pModuleControl;
}
static CRepository& GetRepository()
{
if (!m_pRepository) throw std::bad_exception();
return *m_pRepository;
}
inline static CModuleControl* m_pModuleControl = nullptr;
inline static CRepository* m_pRepository = nullptr;
};
inline CModuleControl& GetModuleControl() { return CHelper::GetModuleControl(); }
inline CRepository& GetRepository() { return CHelper::GetRepository(); }
#endif // !defined MOCK_H

View File

@@ -0,0 +1,115 @@
#include <gtest/gtest.h>
#include <fstream>
#include "mock.h"
#include "generated/IComponent.h"
#include <interfaces/repository.h>
#include "../../../global/exec_dir_helper.h"
const uint32_t LoopCount = 100; //< amount of loops used for concurrency tests. more loops means more thorough deadlock check at the cost of increased runtime
// used to supply external objects for testing pruposes. Implements IObjectControl to make sure this is NOT called for externally supplied objects
class TestExternalObject : public sdv::IInterfaceAccess, public sdv::IObjectControl
{
public:
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(sdv::IObjectControl)
END_SDV_INTERFACE_MAP()
virtual void Initialize([[maybe_unused]] const sdv::u8string& ssObjectConfig)
{
FAIL() << "Error: Initialize should not be called by Repo Service!";
//m_eObjectStatus = sdv::EObjectStatus::initialization_failure;
}
virtual sdv::EObjectStatus GetStatus() const
{
return m_eObjectStatus;
}
/**
* @brief Set the component operation mode. Overload of sdv::IObjectControl::SetOperationMode.
* @param[in] eMode The operation mode, the component should run in.
*/
void SetOperationMode(sdv::EOperationMode eMode)
{
switch (eMode)
{
case sdv::EOperationMode::configuring:
if (m_eObjectStatus == sdv::EObjectStatus::running || m_eObjectStatus == sdv::EObjectStatus::initialized)
m_eObjectStatus = sdv::EObjectStatus::configuring;
break;
case sdv::EOperationMode::running:
if (m_eObjectStatus == sdv::EObjectStatus::configuring || m_eObjectStatus == sdv::EObjectStatus::initialized)
m_eObjectStatus = sdv::EObjectStatus::running;
break;
default:
break;
}
}
virtual void Shutdown()
{
m_eObjectStatus = sdv::EObjectStatus::shutdown_in_progress;
FAIL() << "Error: Shutdown should not be called by Repo Service!";
//m_eObjectStatus = sdv::EObjectStatus::destruction_pending;
}
sdv::EObjectStatus m_eObjectStatus = sdv::EObjectStatus::initialization_pending;
};
TEST(RepositoryTest, LoadNonexistentModule)
{
CRepository repository; // Must be created first
CModuleControl modulectrl;
CHelper helper(modulectrl, repository);
EXPECT_FALSE(modulectrl.Load((GetExecDirectory() / "TestFooBar.sdv").generic_u8string()));
}
TEST(RepositoryTest, CreateNonexistentClass)
{
CRepository repository; // Must be created first
CModuleControl modulectrl;
CHelper helper(modulectrl, repository);
ASSERT_TRUE(modulectrl.Load((GetExecDirectory() / "UnitTest_Repository_test_module.sdv").generic_u8string()));
EXPECT_FALSE(repository.CreateObject2("TestFooBar", nullptr, nullptr));
repository.DestroyObject2("TestFooBar");
}
TEST(RepositoryTest, GetNonexistentObject)
{
CRepository repository; // Must be created first
CModuleControl modulectrl;
CHelper helper(modulectrl, repository);
ASSERT_TRUE(modulectrl.Load((GetExecDirectory() / "UnitTest_Repository_test_module.sdv").generic_u8string()));
bool bRes = repository.CreateObject2("Example_Object", nullptr, nullptr);
EXPECT_TRUE(bRes);
EXPECT_EQ(nullptr, repository.GetObject("TestFooBar"));
repository.DestroyObject2("Example_Object");
}
TEST(RepositoryTest, InstantiateAndGet)
{
CRepository repository; // Must be created first
CModuleControl modulectrl;
CHelper helper(modulectrl, repository);
ASSERT_TRUE(modulectrl.Load((GetExecDirectory() / "UnitTest_Repository_test_module.sdv").generic_u8string()));
bool bRes = repository.CreateObject2("Example_Object", nullptr, nullptr);
EXPECT_TRUE(bRes);
EXPECT_NE(nullptr, repository.GetObject("Example_Object"));
repository.DestroyObject2("Example_Object");
EXPECT_EQ(nullptr, repository.GetObject("Example_Object"));
}
TEST(RepositoryTest, InstantiateInitFail)
{
CRepository repository; // Must be created first
CModuleControl modulectrl;
CHelper helper(modulectrl, repository);
ASSERT_TRUE(modulectrl.Load((GetExecDirectory() / "UnitTest_Repository_test_module.sdv").generic_u8string()));
bool bRes = repository.CreateObject2("TestObject_IObjectControlFail", nullptr, nullptr);
EXPECT_FALSE(bRes);
EXPECT_EQ(nullptr, repository.GetObject("TestObject_IObjectControlFail"));
repository.DestroyObject2("TestObject_IObjectControlFail");
}

View File

@@ -0,0 +1,190 @@
#include <gtest/gtest.h>
#include <iostream>
#include <mutex>
#include <functional>
#include <thread>
#include <support/component_impl.h>
#include "generated/IComponent.h"
#include "../../../global/tracefifo/trace_fifo.cpp"
class CTestLockService
: public sdv::CSdvObject, public ITestLock
{
public:
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(ITestLock)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::Device)
DECLARE_OBJECT_CLASS_NAME("TestLockService")
void Lock() override
{
m_mutex.lock();
}
void Unlock() override
{
m_mutex.unlock();
}
private:
std::recursive_mutex m_mutex;
};
DEFINE_SDV_OBJECT(CTestLockService)
/**
* @brief Example component testing IObjectControl
*/
class CTestObjectControl
: public sdv::CSdvObject
, public sdv::IObjectControl
{
public:
~CTestObjectControl()
{
EXPECT_EQ(m_eObjectStatus, sdv::EObjectStatus::destruction_pending);
}
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(sdv::IObjectControl)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::Device)
DECLARE_OBJECT_CLASS_NAME("Example_Object")
/**
* @brief Initialization method. On success, a subsequent call to GetStatus returns EObjectStatus::running
* @param[in] ssObjectConfig Optional configuration string.
*/
virtual void Initialize([[maybe_unused]] const sdv::u8string& ssObjectConfig)
{
EXPECT_EQ(m_eObjectStatus, sdv::EObjectStatus::initialization_pending);
m_eObjectStatus = sdv::EObjectStatus::initialized;
}
/**
* @brief Gets the current status of the object
* @return EObjectStatus The current status of the object
*/
virtual sdv::EObjectStatus GetStatus() const
{
return m_eObjectStatus;
}
/**
* @brief Set the component operation mode. Overload of sdv::IObjectControl::SetOperationMode.
* @param[in] eMode The operation mode, the component should run in.
*/
void SetOperationMode(sdv::EOperationMode eMode)
{
switch (eMode)
{
case sdv::EOperationMode::configuring:
if (m_eObjectStatus == sdv::EObjectStatus::running || m_eObjectStatus == sdv::EObjectStatus::initialized)
m_eObjectStatus = sdv::EObjectStatus::configuring;
break;
case sdv::EOperationMode::running:
if (m_eObjectStatus == sdv::EObjectStatus::configuring || m_eObjectStatus == sdv::EObjectStatus::initialized)
m_eObjectStatus = sdv::EObjectStatus::running;
break;
default:
break;
}
}
/**
* @brief Shutdown method called before the object is destroyed.
* @attention Implement calls to other SDV objects here as this is no longer considered safe during the destructor of the object!
* After a call to shutdown any threads/callbacks/etc that could call other SDV objects need to have been stopped.
* The SDV object itself is to remain in a state where it can respond to calls to its interfaces as other objects may still call it during the shutdown sequence!
* Any subsequent call to GetStatus should return EObjectStatus::destruction_pending
*/
virtual void Shutdown()
{
m_eObjectStatus = sdv::EObjectStatus::destruction_pending;
}
private:
sdv::EObjectStatus m_eObjectStatus = sdv::EObjectStatus::initialization_pending;
};
DEFINE_SDV_OBJECT(CTestObjectControl)
/**
* @brief Example component testing IObjectControl
*/
class CTestObjectControlFail : public sdv::CSdvObject, public sdv::IObjectControl
{
public:
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(sdv::IObjectControl)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::Device)
DECLARE_OBJECT_CLASS_NAME("TestObject_IObjectControlFail")
/**
* @brief Initialization method. On success, a subsequent call to GetStatus returns EObjectStatus::running
* @param[in] ssObjectConfig Optional configuration string.
*/
virtual void Initialize([[maybe_unused]] const sdv::u8string& ssObjectConfig)
{
m_eObjectStatus = sdv::EObjectStatus::initialization_failure;
}
/**
* @brief Gets the current status of the object
* @return EObjectStatus The current status of the object
*/
virtual sdv::EObjectStatus GetStatus() const
{
return m_eObjectStatus;
}
/**
* @brief Set the component operation mode. Overload of sdv::IObjectControl::SetOperationMode.
* @param[in] eMode The operation mode, the component should run in.
*/
void SetOperationMode(sdv::EOperationMode eMode)
{
switch (eMode)
{
case sdv::EOperationMode::configuring:
if (m_eObjectStatus == sdv::EObjectStatus::running || m_eObjectStatus == sdv::EObjectStatus::initialized)
m_eObjectStatus = sdv::EObjectStatus::configuring;
break;
case sdv::EOperationMode::running:
if (m_eObjectStatus == sdv::EObjectStatus::configuring || m_eObjectStatus == sdv::EObjectStatus::initialized)
m_eObjectStatus = sdv::EObjectStatus::running;
break;
default:
break;
}
}
/**
* @brief Shutdown method called before the object is destroyed.
* @attention Implement calls to other SDV objects here as this is no longer considered safe during the destructor of the object!
* After a call to shutdown any threads/callbacks/etc that could call other SDV objects need to have been stopped.
* The SDV object itself is to remain in a state where it can respond to calls to its interfaces as other objects may still call it during the shutdown sequence!
* Any subsequent call to GetStatus should return EObjectStatus::destruction_pending
*/
virtual void Shutdown()
{
m_eObjectStatus = sdv::EObjectStatus::shutdown_in_progress;
m_eObjectStatus = sdv::EObjectStatus::destruction_pending;
}
private:
sdv::EObjectStatus m_eObjectStatus = sdv::EObjectStatus::initialization_pending;
};
DEFINE_SDV_OBJECT(CTestObjectControlFail)