Ipc config test.

This commit is contained in:
fancy 2023-09-14 07:54:42 -07:00
parent 523a6b8b99
commit 395f30e076
12 changed files with 120 additions and 27 deletions

View File

@ -27,7 +27,7 @@ aux_source_directory(./src SRC_FILES)
set(TARGET_NAME IpcConfig) set(TARGET_NAME IpcConfig)
add_library(${TARGET_NAME} STATIC ${SRC_FILES}) add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} ReturnCode Log) target_link_libraries(${TARGET_NAME} Config StatusCode Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target( add_custom_target(

View File

@ -1,20 +0,0 @@
#ifndef IPCCONFIG_H
#define IPCCONFIG_H
#include "StatusCode.h"
#include <memory>
enum class IpcConfigKey
{
TEST_NUM = 0,
END
};
class IpcConfig
{
public:
IpcConfig() = default;
virtual ~IpcConfig() = default;
static std::shared_ptr<IpcConfig> &GetInstance(std::shared_ptr<IpcConfig> *impl = nullptr);
const StatusCode Init(void);
const StatusCode UnInit(void);
const int GetInt(const IpcConfigKey &key);
};
#endif

View File

@ -5,11 +5,17 @@ const StatusCode IpcConfig::Init(void)
{ {
memset(&mAllData, 0, sizeof(Config_s)); memset(&mAllData, 0, sizeof(Config_s));
mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH); mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH);
if (nullptr == mCfg)
{
LogError("Open config file failed.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
ReadAllConfigParameters(); ReadAllConfigParameters();
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
const StatusCode IpcConfig::UnInit(void) const StatusCode IpcConfig::UnInit(void)
{ {
CloseConfigFile(mCfg);
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
const int IpcConfig::GetInt(const IpcConfigKey &key) const int IpcConfig::GetInt(const IpcConfigKey &key)

View File

@ -7,7 +7,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TEST_LINUX_MOCK}")
execute_process(COMMAND sh build_gtest.sh ${TARGET_PLATFORM} ${CMAKE_SOURCE_DIR_IPCSDK} ${PLATFORM_PATH} WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/) execute_process(COMMAND sh build_gtest.sh ${TARGET_PLATFORM} ${CMAKE_SOURCE_DIR_IPCSDK} ${PLATFORM_PATH} WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/)
# add_subdirectory(test_utils) # add_subdirectory(test_utils)
# add_subdirectory(application) # add_subdirectory(application)
# add_subdirectory(component) add_subdirectory(middleware)
add_subdirectory(utils) add_subdirectory(utils)
add_subdirectory(hal) add_subdirectory(hal)

View File

@ -0,0 +1,4 @@
# cmake_minimum_required(VERSION 2.8.0)
add_subdirectory(IpcConfig)

View File

@ -0,0 +1,58 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
.
./src
./include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${MIDDLEWARE_SOURCE_PATH}/IpcConfig/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib
${LIBS_OUTPUT_PATH}
${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
aux_source_directory(. SRC_FILES)
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME IpcConfigTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} IpcConfig gtest gmock pthread)
if(${COVERAGE_ON} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
IpcConfigTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/IpcConfig
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make IpcConfigTest_code_check
WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/
)
endif()

View File

@ -0,0 +1,9 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,18 @@
#include "ILog.h"
#include "IIpcConfig.h"
// #include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace IpcConfigTest
{
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.Demo
TEST(IpcConfigTest, Demo)
{
CreateLogModule();
CreateIpcConfig();
ILogInit(LOG_INSTANCE_TYPE_END);
IIpcConfig::GetInstance()->Init();
IIpcConfig::GetInstance()->UnInit();
ILogUnInit();
DestroyLogModule();
}
} // namespace IpcConfigTest

View File

@ -1,5 +1,6 @@
#include "Config.h" #include "Config.h"
#include "ConfigImpl.h" #include "ConfigImpl.h"
#include "ILog.h"
#include <stddef.h> #include <stddef.h>
const StatusCode ConfigInit(void) const StatusCode ConfigInit(void)
{ {
@ -15,6 +16,11 @@ VConfig *OpenConfigFile(const char *fileName)
} }
void CloseConfigFile(VConfig *cfg) void CloseConfigFile(VConfig *cfg)
{ {
if (NULL == cfg)
{
LogError("NULL config poniter.\n");
return;
}
((Config *)cfg)->close(cfg); ((Config *)cfg)->close(cfg);
} }
const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value) const StatusCode ConfigGetInt(VConfig *cfg, const char *name, int *value)

View File

@ -33,10 +33,10 @@ static StatusCode NewConfigCode(const long int code)
} }
const StatusCode CreateConfigCode(const long int code) const StatusCode CreateConfigCode(const long int code)
{ {
if (STATUS_CODE_OK <= code && code < STATUS_CODE_END) // if (STATUS_CODE_OK <= code && code < STATUS_CODE_END)
{ // {
return CreateStatusCode(code); // return CreateStatusCode(code);
} // }
if (STATUS_CODE_END <= code && code < CONFIG_CODE_END) if (STATUS_CODE_END <= code && code < CONFIG_CODE_END)
{ {
return NewConfigCode(code); return NewConfigCode(code);

View File

@ -39,6 +39,7 @@ static void ConfigImplInit(Config *cfg)
} }
Config *NewConfig(const char *fileName) Config *NewConfig(const char *fileName)
{ {
LogInfo("Config file name = %s\n", fileName);
Config *cfg = (Config *)malloc(sizeof(Config)); Config *cfg = (Config *)malloc(sizeof(Config));
ConfigImplInit(cfg); ConfigImplInit(cfg);
config_init(&(cfg->cfg)); config_init(&(cfg->cfg));
@ -46,10 +47,12 @@ Config *NewConfig(const char *fileName)
CONFIG_OPTION_SEMICOLON_SEPARATORS | CONFIG_OPTION_SEMICOLON_SEPARATORS |
CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS | CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS |
CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE)); CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE));
if (access(fileName, F_OK)) #define FIEL_EXIST 0
if (FIEL_EXIST == access(fileName, F_OK))
{ {
if (!config_read_file(&(cfg->cfg), fileName)) if (!config_read_file(&(cfg->cfg), fileName))
{ {
LogError("Read file failed[%s].\n", fileName);
fprintf(stderr, "%s:%d - %s\n", config_error_file(&(cfg->cfg)), fprintf(stderr, "%s:%d - %s\n", config_error_file(&(cfg->cfg)),
config_error_line(&(cfg->cfg)), config_error_text(&(cfg->cfg))); config_error_line(&(cfg->cfg)), config_error_text(&(cfg->cfg)));
// config_destroy(&(cfg->cfg)); // config_destroy(&(cfg->cfg));
@ -58,6 +61,7 @@ Config *NewConfig(const char *fileName)
} }
else else
{ {
LogInfo("Config file doesn't exist.\n");
/* Write out the new configuration. */ /* Write out the new configuration. */
if (!config_write_file(&(cfg->cfg), fileName)) if (!config_write_file(&(cfg->cfg), fileName))
{ {

View File

@ -19,6 +19,14 @@ static const char *_PrintStringCode_(const StatusCode this)
} }
static const bool CodeEqual(const StatusCode code, const char *value) static const bool CodeEqual(const StatusCode code, const char *value)
{ {
if (STATUS_CODE_OK >= code.mStatusCode || code.mStatusCode >= STATUS_CODE_END)
{
return false;
}
if (strlen(value) != strlen(StatusCodeString[code.mStatusCode]))
{
return false;
}
if (memcmp(value, StatusCodeString[code.mStatusCode], strlen(value)) == 0) if (memcmp(value, StatusCodeString[code.mStatusCode], strlen(value)) == 0)
{ {
return true; return true;