diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..cbd8e4c --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,63 @@ + +# cmake_minimum_required(VERSION 2.8.0) +# Compile gtest for test code. +if (EXISTS "${EXTERNAL_LIBS_OUTPUT_PATH}/libgtest.a") + message("googletest compile ok.") +else() + execute_process(COMMAND sh build_gtest.sh ${TARGET_PLATFORM} ${PLATFORM_PATH} ${PLATFORM_PATH} WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/) + execute_process(COMMAND mkdir ${PLATFORM_PATH}/output_files + WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/) + execute_process(COMMAND mkdir ${PLATFORM_PATH}/output_files/libs + WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/) + execute_process(COMMAND mkdir ${PLATFORM_PATH}/output_files/libs/external + WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/) + execute_process(COMMAND mv ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib/libgtest.a ${EXTERNAL_LIBS_OUTPUT_PATH}/libgtest.a + WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/) + execute_process(COMMAND mv ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib/libgmock.a ${EXTERNAL_LIBS_OUTPUT_PATH}/libgmock.a + WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/) +endif() + +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + message("linux mock api will be set.") + set(TEST_LINUX_MOCK "-Wl,--wrap=fx_open" CACHE STRING INTERNAL FORCE) + set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_fstat" CACHE STRING INTERNAL FORCE) + set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_tcgetattr" CACHE STRING INTERNAL FORCE) + set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_tcsetattr" CACHE STRING INTERNAL FORCE) + set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_select" CACHE STRING INTERNAL FORCE) + set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_write" CACHE STRING INTERNAL FORCE) + set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_read" CACHE STRING INTERNAL FORCE) + set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_access" CACHE STRING INTERNAL FORCE) + set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_fopen" CACHE STRING INTERNAL FORCE) +endif() +# Mock Linux api. +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TEST_LINUX_MOCK}") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TEST_LINUX_MOCK}") +# build gtest and gmock +# add_custom_command( +# OUTPUT ${EXTERNAL_LIBS_OUTPUT_PATH}/libgtest.a +# OUTPUT ${EXTERNAL_LIBS_OUTPUT_PATH}/libgmock.a +# COMMAND echo "Build google test for test code." +# COMMAND sh build_gtest.sh ${TARGET_PLATFORM} ${PLATFORM_PATH} ${PLATFORM_PATH} +# COMMAND mv ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib/libgtest.a ${EXTERNAL_LIBS_OUTPUT_PATH}/libgtest.a +# COMMAND mv ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib/libgmock.a ${EXTERNAL_LIBS_OUTPUT_PATH}/libgmock.a +# WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/ +# ) +# # add_custom_target( +# # google_test +# # DEPENDS libgtest.a libgmock.a +# # ) +# add_custom_target( +# libgtest.a +# DEPENDS ${EXTERNAL_LIBS_OUTPUT_PATH}/libgtest.a +# ) +# add_custom_target( +# libgmock.a +# DEPENDS ${EXTERNAL_LIBS_OUTPUT_PATH}/libgmock.a +# ) + +add_subdirectory(all) +add_subdirectory(application) +add_subdirectory(middleware) +add_subdirectory(utils) +add_subdirectory(hal) + diff --git a/test/GtestUsing.h b/test/GtestUsing.h new file mode 100644 index 0000000..e829a58 --- /dev/null +++ b/test/GtestUsing.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef GTEST_USING_H +#define GTEST_USING_H +#include +#include +using ::testing::_; +using ::testing::Action; +using ::testing::ActionInterface; +using ::testing::AnyNumber; +using ::testing::Assign; +using ::testing::AtLeast; +using ::testing::ByMove; +using ::testing::ByRef; +using ::testing::DefaultValue; +using ::testing::DoAll; +using ::testing::DoDefault; +using ::testing::IgnoreResult; +using ::testing::Invoke; +using ::testing::InvokeWithoutArgs; +using ::testing::MakePolymorphicAction; +using ::testing::PolymorphicAction; +using ::testing::Return; +using ::testing::ReturnNew; +using ::testing::ReturnNull; +using ::testing::ReturnPointee; +using ::testing::ReturnRef; +using ::testing::ReturnRefOfCopy; +using ::testing::ReturnRoundRobin; +using ::testing::SaveArg; +using ::testing::SetArgPointee; +using ::testing::SetArgReferee; +using ::testing::SetArgumentPointee; +using ::testing::Unused; +using ::testing::WithArgs; +using ::testing::internal::BuiltInDefaultValue; +// using ::testing::Mock::VerifyAndClearExpectations; +using ::testing::Mock; +#endif \ No newline at end of file diff --git a/test/all/CMakeLists.txt b/test/all/CMakeLists.txt new file mode 100644 index 0000000..584121f --- /dev/null +++ b/test/all/CMakeLists.txt @@ -0,0 +1,28 @@ +# 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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES) +# aux_source_directory(${TEST_SOURCE_PATH}/middleware/IpcConfig/src SRC_FILES) +set(TARGET_NAME AllTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} IpcConfig gtest gmock pthread) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() \ No newline at end of file diff --git a/test/all/mainTest.cpp b/test/all/mainTest.cpp new file mode 100644 index 0000000..fb1d91b --- /dev/null +++ b/test/all/mainTest.cpp @@ -0,0 +1,9 @@ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/application/CMakeLists.txt b/test/application/CMakeLists.txt new file mode 100644 index 0000000..bb37899 --- /dev/null +++ b/test/application/CMakeLists.txt @@ -0,0 +1,4 @@ + +add_subdirectory(HuntingCamera) +add_subdirectory(MissionManager) +# add_subdirectory(VersionReleaseTool) \ No newline at end of file diff --git a/test/application/HuntingCamera/CMakeLists.txt b/test/application/HuntingCamera/CMakeLists.txt new file mode 100644 index 0000000..8131a8c --- /dev/null +++ b/test/application/HuntingCamera/CMakeLists.txt @@ -0,0 +1,96 @@ +# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake) +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +include(${APPLICATION_SOURCE_PATH}/HuntingCamera/build/hunting_camera.cmake) +set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin) + +include_directories( + ./src + ./tool/include + ${HUNTTING_MAIN_INCLUDE_PATH} + ${MIDDLEWARE_SOURCE_PATH}/McuManager/include + ${MIDDLEWARE_SOURCE_PATH}/McuManager/src + ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include + ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src + ${UTILS_SOURCE_PATH}/McuProtocol/include + ${UTILS_SOURCE_PATH}/UartDevice/include + ${UTILS_SOURCE_PATH}/LedControl/include + ${TEST_SOURCE_PATH} + ${TEST_SOURCE_PATH}/application/MissionManager/tool/include + ${TEST_SOURCE_PATH}/middleware/McuManager/tool/include + ${TEST_SOURCE_PATH}/middleware/AppManager/tool/include + ${TEST_SOURCE_PATH}/middleware/DeviceManager/tool/include + ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include + ${TEST_SOURCE_PATH}/utils/TestManager/include + ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include + ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include + ${TEST_SOURCE_PATH}/hal/tool/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} + ${TEST_TOOLS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES_MAIN) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +set(TARGET_NAME HuntingCameraTest) +add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} McuProtocolTestTool UartDeviceTestTool) +target_link_libraries(${TARGET_NAME} HuntingMainLib MissionManagerTestTool McuManagerTestTool McuAskBaseTestTool + AppManagerTestTool HalTestTool DeviceManagerTestTool TestManager UartDevice Hal LinuxApiMock gtest gmock pthread) +target_link_libraries(${TARGET_NAME} UpgradeTool Servers) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + HuntingCameraTest_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${SRC_FILES} + ${SRC_FILES_MAIN} + ${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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/application/HuntingCamera +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make HuntingCameraTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + HuntingCameraTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/application/HuntingCamera +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make HuntingCameraTest_code_check + COMMAND make HuntingCameraTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +# if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") +# add_subdirectory(tool) +# endif() \ No newline at end of file diff --git a/test/application/HuntingCamera/mainTest.cpp b/test/application/HuntingCamera/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/application/HuntingCamera/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/application/HuntingCamera/src/HuntingCamera_Test.cpp b/test/application/HuntingCamera/src/HuntingCamera_Test.cpp new file mode 100644 index 0000000..8490917 --- /dev/null +++ b/test/application/HuntingCamera/src/HuntingCamera_Test.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "GtestUsing.h" +#include "ILog.h" +#include "MainThread.h" +#include "McuManagerTestTool.h" +#include "TestManager.h" +#include +namespace HuntingCameraTest +{ +} // namespace HuntingCameraTest \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/AppManager_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/AppManager_Mock_Test.cpp new file mode 100644 index 0000000..e9ec00b --- /dev/null +++ b/test/application/HuntingCamera/src_mock/AppManager_Mock_Test.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "AppManagerTestTool.h" +#include "GtestUsing.h" +#include "HalTestTool.h" +#include "HuntingCameraTest.h" +#include "ILog.h" +#include "MainThread.h" +#include "McuManagerTestTool.h" +#include "MissionManagerTestTool.h" +#include "TestManager.h" +#include +namespace AppManager_Mock_Test +{ +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_EXAMPLE_SetSdCardStatus + */ +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_SetSdCardStatus) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 6); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + AppManagerTestTool::MockAppClientConnect(); + HalTestTool::MockSdCardRemove(mLinuxTest); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + HalTestTool::MockSdCardInsert(mLinuxTest); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MainThread::GetInstance()->Runing(); +} +} // namespace AppManager_Mock_Test \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/AppMonitor_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/AppMonitor_Mock_Test.cpp new file mode 100644 index 0000000..8d0ad3c --- /dev/null +++ b/test/application/HuntingCamera/src_mock/AppMonitor_Mock_Test.cpp @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "AppManagerTestTool.h" +#include "GtestUsing.h" +#include "HalTestTool.h" +#include "HuntingCameraTest.h" +#include "ILog.h" +#include "MainThread.h" +#include "McuManagerTestTool.h" +#include "MissionManagerTestTool.h" +#include "TestManager.h" +#include +namespace AppMonitor_Mock_Test +{ +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetProductInfo +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetProductInfo) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockGetProductInfo(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetDeviceAttr +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetDeviceAttr) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockGetDeviceAttr(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetMediaInfo +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetMediaInfo) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockGetMediaInfo(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetSdCardInfo +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetSdCardInfo) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockGetSdCardInfo(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetBatteryInfo +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetBatteryInfo) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockGetBatteryInfo(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_SetDateTime +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_SetDateTime) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetDateTime(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_SetTimeZone +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_SetTimeZone) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetTimeZone(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_MockUploadFiles +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_MockUploadFiles) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockUploadFiles(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_McuOpenFailed +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_McuOpenFailed) +{ + McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // MockUploadFiles(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetParamValue +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetParamValue) +{ + McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockGetParamValue("all"); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetCapability +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetCapability) +{ + McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockGetCapability(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetLockVideoStatus +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetLockVideoStatus) +{ + McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockGetLockVideoStatus(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetStorageInfo +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetStorageInfo) +{ + McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockGetStorageInfo(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetStorageFileList +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetStorageFileList) +{ + McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockGetStorageFileList(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_SetParamValue +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_SetParamValue) +{ + McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetParamValue(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_EnterRecorder +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_EnterRecorder) +{ + McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockEnterRecorder(); + MainThread::GetInstance()->Runing(); +} +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_AppPlayback +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_AppPlayback) +{ + McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockAppPlayback(); + MainThread::GetInstance()->Runing(); +} +} // namespace AppMonitor_Mock_Test \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp new file mode 100644 index 0000000..1a0dc5d --- /dev/null +++ b/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "AppManagerTestTool.h" +#include "GtestUsing.h" +#include "HalTestTool.h" +#include "HuntingCameraTest.h" +#include "ILog.h" +#include "MainThread.h" +#include "McuManagerTestTool.h" +#include "MissionManagerTestTool.h" +#include "TestManager.h" +#include +namespace DeviceManager_Mock_Test +{ +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlHold + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlHold) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + HalTestTool::MockKeyClick("reset", 1000); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MainThread::GetInstance()->Runing(); +} +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControl2 + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControl2) +{ + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000); + HalTestTool::MockKeyClick("reset", 10); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MainThread::GetInstance()->Runing(); +} +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlClick + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlClick) +{ + SetAllCamerasResult(mAllCamerasMock); + MockOtherSideIpcMissionReply(IpcMission::TEST); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + HalTestTool::MockKeyClick("reset", 200); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MainThread::GetInstance()->Runing(); +} +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_LedControl + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_LedControl) +{ + SetAllLedsResult(mAllLedsMock); + MockOtherSideIpcMissionReply(IpcMission::TEST); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MainThread::GetInstance()->Runing(); +} +} // namespace DeviceManager_Mock_Test \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp b/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp new file mode 100644 index 0000000..579e0ad --- /dev/null +++ b/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "HuntingCameraTest.h" +#include "GtestUsing.h" +#include "ILog.h" +#include "MainThread.h" +#include +const char *KEY_RESET = "reset"; +const char *LED_DEVICE_STATUS = "device_status"; +void MainThreadTest::CustomizationInit(void) +{ + // Do nothing here to make sure test tool work. +} +HuntingCameraTest::HuntingCameraTest() +{ +} +HuntingCameraTest::~HuntingCameraTest() +{ +} +void HuntingCameraTest::SetUpTestCase() +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); +} +void HuntingCameraTest::TearDownTestCase() +{ + ILogUnInit(); +} +void HuntingCameraTest::SetUp() +{ + CreateAllCamerasMcok(); + std::shared_ptr mainThread = std::make_shared(); + MainThread::GetInstance(&mainThread); + HalTestTool::Init(); + CreateAllKeysMcok(); + CreateAllLedsMcok(); + AppManagerTestTool::Init(); + MissionManagerTestTool::Init(); + mLinuxTest = LinuxTest::CreateLinuxTest(); + std::shared_ptr test = mLinuxTest; + LinuxApiMock::GetInstance(&test); + LinuxApiMock::GetInstance()->Init(); + McuManagerTestTool::Init(mLinuxTest); + HalTestTool::InitSdCardHal(mLinuxTest); + DeviceManagerTestTool::Init(); + TestManager::Init(); +} +void HuntingCameraTest::TearDown() +{ + TestManager::UnInit(); + MissionManagerTestTool::UnInit(); + AppManagerTestTool::UnInit(); + HalTestTool::UnInit(); + LinuxApiMock::GetInstance()->UnInit(); + mLinuxTest = std::make_shared(); + std::shared_ptr test = std::make_shared(); + LinuxApiMock::GetInstance(&test); + McuManagerTestTool::UnInit(); + MainThread::GetInstance()->UnInit(); + std::shared_ptr mainThread = std::make_shared(); + MainThread::GetInstance(&mainThread); + DeviceManagerTestTool::UnInit(); + DestroyAllCamerasMock(); + DestroyAllKeysMock(); + DestroyAllLedsMock(); +} +void HuntingCameraTest::CreateAllCamerasMcok(void) +{ + std::shared_ptr camera = HalTestTool::MakeCameraHalTest(CameraType::MAIN_CAMERA); + mAllCamerasMock[CameraType::MAIN_CAMERA] = camera; +} +void HuntingCameraTest::DestroyAllCamerasMock(void) +{ + mAllCamerasMock.clear(); +} +void HuntingCameraTest::CreateAllKeysMcok(void) +{ + std::shared_ptr key = HalTestTool::MakeKeyHalTest(KEY_RESET); + mAllKeysMock[KEY_RESET] = key; + HalTestTool::SetAllKeysResult(mAllKeysMock); +} +void HuntingCameraTest::DestroyAllKeysMock(void) +{ + + mAllKeysMock.clear(); +} +void HuntingCameraTest::CreateAllLedsMcok(void) +{ + std::shared_ptr led = HalTestTool::MakeLedHalTest(LED_DEVICE_STATUS); + mAllLedsMock[LED_DEVICE_STATUS] = led; +} +void HuntingCameraTest::DestroyAllLedsMock(void) +{ + mAllLedsMock.clear(); +} \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/HuntingCameraTest.h b/test/application/HuntingCamera/src_mock/HuntingCameraTest.h new file mode 100644 index 0000000..2921507 --- /dev/null +++ b/test/application/HuntingCamera/src_mock/HuntingCameraTest.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HUNTING_CAMERA_TEST_H +#define HUNTING_CAMERA_TEST_H +#include "AppManagerTestTool.h" +#include "DeviceManagerTestTool.h" +#include "GtestUsing.h" +#include "HalTestTool.h" +#include "LinuxApiMock.h" +#include "MainThread.h" +#include "McuManagerTestTool.h" +#include "MissionManagerTestTool.h" +#include "TestManager.h" +#include +class MainThreadTest : public MainThread +{ +public: + MainThreadTest() = default; + virtual ~MainThreadTest() = default; + void CustomizationInit(void) override; +}; +class HuntingCameraTest : public testing::Test, + public TestManager, + public MissionManagerTestTool, + public McuManagerTestTool, + public AppManagerTestTool, + public DeviceManagerTestTool, + virtual public HalTestTool +{ +public: + HuntingCameraTest(); + virtual ~HuntingCameraTest(); + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; + +private: + void CreateAllCamerasMcok(void); + void DestroyAllCamerasMock(void); + void CreateAllKeysMcok(void); + void DestroyAllKeysMock(void); + void CreateAllLedsMcok(void); + void DestroyAllLedsMock(void); + +protected: + std::shared_ptr mLinuxTest; + std::map> mAllCamerasMock; + std::map> mAllKeysMock; + std::map> mAllLedsMock; +}; + +#endif \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/McuManager_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/McuManager_Mock_Test.cpp new file mode 100644 index 0000000..5c71b95 --- /dev/null +++ b/test/application/HuntingCamera/src_mock/McuManager_Mock_Test.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "AppManagerTestTool.h" +#include "GtestUsing.h" +#include "HalTestTool.h" +#include "HuntingCameraTest.h" +#include "ILog.h" +#include "MainThread.h" +#include "McuManagerTestTool.h" +#include "MissionManagerTestTool.h" +#include "TestManager.h" +#include +namespace McuManager_Mock_Test +{ +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_UartOpenSuccessButReadNothing + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_UartOpenSuccessButReadNothing) +{ + McuManagerTestTool::MockMcuDeviceOpenSuccessButReadNothing(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MainThread::GetInstance()->Runing(); + RemoveUpgradeFile(); +} +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_OnMissionStateStart + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_OnMissionStateStart) +{ + McuManagerTestTool::MockOtherSideIpcMissionReply(IpcMission::ON); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MainThread::GetInstance()->Runing(); +} +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_PirTriggeredMissionStateStart + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_PirTriggeredMissionStateStart) +{ + McuManagerTestTool::MockOtherSideIpcMissionReply(IpcMission::PIR_TRIGGERED); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MainThread::GetInstance()->Runing(); +} +} // namespace McuManager_Mock_Test \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/MediaManager_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/MediaManager_Mock_Test.cpp new file mode 100644 index 0000000..dc5844e --- /dev/null +++ b/test/application/HuntingCamera/src_mock/MediaManager_Mock_Test.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "AppManagerTestTool.h" +#include "GtestUsing.h" +#include "HalTestTool.h" +#include "HuntingCameraTest.h" +#include "ILog.h" +#include "MainThread.h" +#include "McuManagerTestTool.h" +#include "MissionManagerTestTool.h" +#include "TestManager.h" +#include +namespace MediaManager_Mock_Test +{ +// ../output_files/test/bin/HuntingCameraTest +// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_EXAMPLE_MediaReprot +TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_MediaReprot) +{ + SetAllCamerasResult(mAllCamerasMock); + MockReportCameraEvent("/tmp/test.MP4", CameraType::MAIN_CAMERA); + CreateUpgradeFile(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 4); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // MockAppPlayback(); + MainThread::GetInstance()->Runing(); + RemoveUpgradeFile(); +} +} // namespace MediaManager_Mock_Test \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/TestMissionState_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/TestMissionState_Mock_Test.cpp new file mode 100644 index 0000000..f2e0240 --- /dev/null +++ b/test/application/HuntingCamera/src_mock/TestMissionState_Mock_Test.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "AppManagerTestTool.h" +#include "GtestUsing.h" +#include "HalTestTool.h" +#include "HuntingCameraTest.h" +#include "ILog.h" +#include "MainThread.h" +#include "McuManagerTestTool.h" +#include "MissionManagerTestTool.h" +#include "TestManager.h" +#include +namespace TestMissionState_Mock_Test +{ +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_TestMissionResetKeyCapture + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_TestMissionResetKeyCapture) +{ + McuManagerTestTool::MockOtherSideIpcMissionReply(IpcMission::TEST); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + HalTestTool::MockKeyClick("reset", 200); // Simulate pressing a button. + MainThread::GetInstance()->Runing(); +} +} \ No newline at end of file diff --git a/test/application/MissionManager/CMakeLists.txt b/test/application/MissionManager/CMakeLists.txt new file mode 100644 index 0000000..5d541c1 --- /dev/null +++ b/test/application/MissionManager/CMakeLists.txt @@ -0,0 +1,4 @@ + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/application/MissionManager/tool/include/MissionManagerTestTool.h b/test/application/MissionManager/tool/include/MissionManagerTestTool.h new file mode 100644 index 0000000..5cd774b --- /dev/null +++ b/test/application/MissionManager/tool/include/MissionManagerTestTool.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MISSION_MANAGER_TEST_TOOL_H +#define MISSION_MANAGER_TEST_TOOL_H +#include "GtestUsing.h" +#include "IMissionManager.h" +#include +class MissionManagerTestTool +{ +public: + MissionManagerTestTool() = default; + virtual ~MissionManagerTestTool() = default; + void Init(void); + void UnInit(void); + +protected: + void CreateUpgradeFile(void); + void RemoveUpgradeFile(void); + +private: + std::shared_ptr mMissionManagerMock; +}; +#endif \ No newline at end of file diff --git a/test/application/VersionReleaseTool/CMakeLists.txt b/test/application/VersionReleaseTool/CMakeLists.txt new file mode 100644 index 0000000..c75b2c1 --- /dev/null +++ b/test/application/VersionReleaseTool/CMakeLists.txt @@ -0,0 +1,86 @@ +# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake) +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +# include(${APPLICATION_SOURCE_PATH}/VersionRelease/build/hunting_camera.cmake) +set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR_IPCSDK}/tools/version_release) + +include_directories( + ./src + # ${HUNTTING_MAIN_INCLUDE_PATH} + # ${MIDDLEWARE_SOURCE_PATH}/McuManager/include + # ${MIDDLEWARE_SOURCE_PATH}/McuManager/src + ${UTILS_SOURCE_PATH}/UpgradeTool/include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${TEST_SOURCE_PATH} + # ${TEST_SOURCE_PATH}/application/MissionManager/tool/include + # ${TEST_SOURCE_PATH}/middleware/McuManager/tool/include + # ${TEST_SOURCE_PATH}/middleware/AppManager/tool/include + # ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include + # ${TEST_SOURCE_PATH}/utils/TestManager/include + # ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include + # ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include + # ${TEST_SOURCE_PATH}/hal/tool/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES_MAIN) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +add_definitions(-DHUNTING_UPGRADE_BUILD_FILE="../ipc-sdk/middleware/HuntingUpgrade/build/hunting_upgrade.cmake") + +set(TARGET_NAME VersionReleaseTool) +add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} UpgradeTool StatusCode Log gtest gmock pthread) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + VersionReleaseTool_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${SRC_FILES} + ${SRC_FILES_MAIN} + ${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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/application/VersionRelease +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make VersionReleaseTool_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + VersionReleaseTool_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/application/VersionRelease +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make VersionReleaseTool_code_check + COMMAND make VersionReleaseTool_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) \ No newline at end of file diff --git a/test/application/VersionReleaseTool/mainTest.cpp b/test/application/VersionReleaseTool/mainTest.cpp new file mode 100644 index 0000000..6f2dadb --- /dev/null +++ b/test/application/VersionReleaseTool/mainTest.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ArgvAnalysis.h" +#include "ILog.h" +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + ArgvAnalysis::GetInstance()->Analyze(argc, argv); + testing::InitGoogleTest(&argc, argv); + int result = RUN_ALL_TESTS(); + ILogUnInit(); + DestroyLogModule(); + return result; +} \ No newline at end of file diff --git a/test/application/VersionReleaseTool/src/ArgvAnalysis.cpp b/test/application/VersionReleaseTool/src/ArgvAnalysis.cpp new file mode 100644 index 0000000..49d968b --- /dev/null +++ b/test/application/VersionReleaseTool/src/ArgvAnalysis.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ArgvAnalysis.h" +#include "ILog.h" +const char *V_SOURCE_FILE = "v_source_file"; +const char *V_OUTPUT_FILE = "v_output_file"; +std::shared_ptr &ArgvAnalysis::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) { + if (instance.use_count() == 1) { + LogInfo("Instance changed succeed.\n"); + instance = *impl; + } + else { + LogError("Can't changing the instance becase of using by some one.\n"); + } + } + return instance; +} +void ArgvAnalysis::Analyze(int argc, char *argv[]) +{ + LogInfo("argc: %d\n", argc); + for (int i = 1; i < argc; ++i) { + std::string arg = argv[i]; + size_t pos = arg.find('='); + + if (pos != std::string::npos) { + std::string key = arg.substr(0, pos); + std::string value = arg.substr(pos + 1); + + // remove'--' + if (key.substr(0, 2) == "--") { + key = key.substr(2); + } + + mOptions[key] = value; + } + else { + LogError("Invalid argument format: %s\n", arg.c_str()); + return; + } + } + + for (const auto &pair : mOptions) { + LogInfo("Key: %s, Value: %s\n", pair.first.c_str(), pair.second.c_str()); + } +} +std::string ArgvAnalysis::GetSourceFile(void) +{ + auto it = mOptions.find(V_SOURCE_FILE); + if (it != mOptions.end()) { + return it->second; + } + LogWarning("Can't find the source file.\n"); + return ""; +} +std::string ArgvAnalysis::GetOutputFile(void) +{ + auto it = mOptions.find(V_OUTPUT_FILE); + if (it != mOptions.end()) { + return it->second; + } + LogWarning("Can't find the source file.\n"); + return ""; +} \ No newline at end of file diff --git a/test/application/VersionReleaseTool/src/ArgvAnalysis.h b/test/application/VersionReleaseTool/src/ArgvAnalysis.h new file mode 100644 index 0000000..c4af302 --- /dev/null +++ b/test/application/VersionReleaseTool/src/ArgvAnalysis.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef ARGV_ANALYSIS_H +#define ARGV_ANALYSIS_H +#include "StatusCode.h" +#include +#include +class ArgvAnalysis +{ +public: + ArgvAnalysis() = default; + virtual ~ArgvAnalysis() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + void Analyze(int argc, char *argv[]); + std::string GetSourceFile(void); + std::string GetOutputFile(void); + +private: + std::map mOptions; +}; +#endif \ No newline at end of file diff --git a/test/application/VersionReleaseTool/src/VersionBase.cpp b/test/application/VersionReleaseTool/src/VersionBase.cpp new file mode 100644 index 0000000..0f5cc58 --- /dev/null +++ b/test/application/VersionReleaseTool/src/VersionBase.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "VersionBase.h" +#include +#include +#include +#include +#include +void VersionBase::GetVersion(const std::string &fileName, const std::regex &pattern, std::string &version) +{ + std::ifstream file(fileName); + if (!file.is_open()) { + std::cerr << "无法打开文件: " << fileName << std::endl; + return; + } + + std::string line; + std::string versionGet; + // std::regex pattern(R"(add_definitions\(-DHUNTING_CAMERA_VERSION=\"([^"]+)\"\))"); + + while (std::getline(file, line)) { + std::smatch match; + if (std::regex_search(line, match, pattern)) { + // 如果找到匹配项,match[1]将包含捕获的版本号 + versionGet = match[1].str(); + break; // 找到后退出循环 + } + } + + file.close(); + + if (versionGet.empty()) { + std::cerr << "在文件中未找到指定的行或版本号!" << std::endl; + return; + } + + std::cout << "提取的版本号: " << versionGet << std::endl; + return; +} \ No newline at end of file diff --git a/test/application/VersionReleaseTool/src/VersionBase.h b/test/application/VersionReleaseTool/src/VersionBase.h new file mode 100644 index 0000000..e610013 --- /dev/null +++ b/test/application/VersionReleaseTool/src/VersionBase.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef VERSION_BASE_H +#define VERSION_BASE_H +#include +#include +class VersionBase +{ +public: + VersionBase() = default; + virtual ~VersionBase() = default; + void GetVersion(const std::string &fileName, const std::regex &pattern, std::string &version); +}; +#endif \ No newline at end of file diff --git a/test/application/VersionReleaseTool/src/VersionReleaseTool.cpp b/test/application/VersionReleaseTool/src/VersionReleaseTool.cpp new file mode 100644 index 0000000..9dd946e --- /dev/null +++ b/test/application/VersionReleaseTool/src/VersionReleaseTool.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "VersionReleaseTool.h" +#include "ArgvAnalysis.h" +#include "GtestUsing.h" +#include "ILog.h" +#include "UpgradeTool.h" +#include +#include +#include +#include +#include +#include +namespace VersionReleaseTool +{ +/** + * @brief Construct a new TEST object + * ../output_files/test/bin/VersionReleaseTool --v_source_file=./test.bin --v_output_file=./test_output.bin + */ +static void GetVersion(std::string &versiond) +{ + + const std::string filename = HUNTING_UPGRADE_BUILD_FILE; + std::ifstream file(filename); + if (!file.is_open()) { + std::cerr << "无法打开文件: " << filename << std::endl; + return ; + } + + std::string line; + std::string version; + std::regex pattern(R"(add_definitions\(-DHUNTING_CAMERA_VERSION=\"([^"]+)\"\))"); + + while (std::getline(file, line)) { + std::smatch match; + if (std::regex_search(line, match, pattern)) { + // 如果找到匹配项,match[1]将包含捕获的版本号 + version = match[1].str(); + break; // 找到后退出循环 + } + } + + file.close(); + + if (version.empty()) { + std::cerr << "在文件中未找到指定的行或版本号!" << std::endl; + return ; + } + + std::cout << "提取的版本号: " << version << std::endl; + return ; + + +} +TEST(VersionReleaseTool, Version) +{ + std::string sourceFile = ArgvAnalysis::GetInstance()->GetSourceFile(); + std::string outputFile = ArgvAnalysis::GetInstance()->GetOutputFile(); + std::string version; + GetVersion(version); + UpgradeTool::GetInstance()->PackFile(sourceFile, outputFile, "1.0.0", "product", "project", "upgradeType"); +} +} // namespace VersionReleaseTool \ No newline at end of file diff --git a/test/application/VersionReleaseTool/src/VersionReleaseTool.h b/test/application/VersionReleaseTool/src/VersionReleaseTool.h new file mode 100644 index 0000000..f1caf98 --- /dev/null +++ b/test/application/VersionReleaseTool/src/VersionReleaseTool.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef VERSION_RELEASE_TOOL_H +#define VERSION_RELEASE_TOOL_H + +#endif \ No newline at end of file diff --git a/test/hal/CMakeLists.txt b/test/hal/CMakeLists.txt new file mode 100644 index 0000000..044fbda --- /dev/null +++ b/test/hal/CMakeLists.txt @@ -0,0 +1,71 @@ +# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake) +set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin) + +include_directories( + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${HAL_SOURCE_PATH}/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} + ${TEST_TOOLS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES) +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME HalTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} HalTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} Hal Log LinuxApiMock) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + HalTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/hal +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make HalTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + HalTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/hal +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make HalTest_code_check + COMMAND make HalTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/hal/mainTest.cpp b/test/hal/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/hal/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/hal/src/IHalTest.cpp b/test/hal/src/IHalTest.cpp new file mode 100644 index 0000000..e628205 --- /dev/null +++ b/test/hal/src/IHalTest.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "IHal.h" +#include "ILog.h" +#include +#include +namespace IHalTest +{ +// ../output_files/test/bin/IHalTest --gtest_filter=IHalTest.Demo +TEST(IHalTest, Demo) +{ + ILogInit(LOG_EASYLOGGING); + CreateHalModule(); + StatusCode code = IHalInit(); + if (IsCodeOK(code)) { + PrintStringCode(code); + } + IHalUnInit(); + DestroyHalModule(); + ILogUnInit(); +} +} // namespace IHalTest \ No newline at end of file diff --git a/test/hal/tool/include/HalTestTool.h b/test/hal/tool/include/HalTestTool.h new file mode 100644 index 0000000..0235b27 --- /dev/null +++ b/test/hal/tool/include/HalTestTool.h @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HAL_TEST_TOOL_H +#define HAL_TEST_TOOL_H +#include "GtestUsing.h" +#include "IHalCpp.h" +#include "LedControl.h" +#include "LinuxApiMock.h" +class HalTestTool +{ +public: + HalTestTool() = default; + virtual ~HalTestTool() = default; + void Init(void); + void InitSdCardHal(std::shared_ptr &mock); + void UnInit(void); + /** + * @brief Set the All Keys Result object + * Due to the abstract design of the HAL for the number and type of buttons, it is necessary to externally + * determine the instances of the buttons. + * @param allKeys The instances of the buttons. + */ + void SetAllKeysResult(std::map> &allKeys); + void SetAllLedsResult(std::map> &allLeds); + void SetAllCamerasResult(std::map> &allCameras); + void MockKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs = 200); + void SetLedStateExpectations(const std::string &ledName, const LedState &state, const unsigned int &aliveTimeMs, + const unsigned int &blinkTimeMs); + void MockReportCameraEvent(const std::string &fileName, const CameraType &cameraType); + +protected: + virtual void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs); + +private: + void KeyEventHappendOnce(std::shared_ptr &vMock, const unsigned int &pressingTimeMs); + +private: + void HalMockInit(std::shared_ptr &vMock); + +private: // About key hal + void SetAllKeysResult(std::shared_ptr &vMock, std::map> &allKeys); + std::shared_ptr SearchKey(const std::string &keyName); + void InitAllKeysMock(std::map> &allKeys); + void InitKeysMock(std::shared_ptr &vMock); + +private: // About led hal + void SetAllLedsResult(std::shared_ptr &vMock, std::map> &allLeds); + std::shared_ptr &SearchLed(const std::string &ledName); + void InitAllLedsMock(std::map> &allLeds); + void InitLedsMock(std::shared_ptr &vMock); + +private: // About camera hal + void SetAllCamerasResult(std::shared_ptr &vMock, + std::map> &allCameras); + std::shared_ptr SearchCamera(const CameraType &cameraType); + void InitAllCamerasMock(std::map> &allCameras); + void InitCamerasMock(std::shared_ptr &vMock); + +protected: // About sd card hal + void MockSdCardRemove(std::shared_ptr &mock); + void MockSdCardInsert(std::shared_ptr &mock); + +public: + static std::shared_ptr MakeKeyHalTest(const std::string &keyName); + static std::shared_ptr MakeLedHalTest(const std::string &ledName); + static std::shared_ptr MakeCameraHalTest(const CameraType &type); + +private: + std::shared_ptr mHalMock; + std::map> mAllKeys; + std::map> mAllLeds; + std::map> mAllCameras; + std::shared_ptr mSdCardHal; +}; +#endif \ No newline at end of file diff --git a/test/middleware/AppManager/CMakeLists.txt b/test/middleware/AppManager/CMakeLists.txt new file mode 100644 index 0000000..ea265eb --- /dev/null +++ b/test/middleware/AppManager/CMakeLists.txt @@ -0,0 +1,87 @@ +# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake) +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +include(${MIDDLEWARE_SOURCE_PATH}/AppManager/build/app_manager.cmake) +set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin) + +include_directories( + ./src + ./include + ./tool/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/KeyControl/include + ${UTILS_SOURCE_PATH}/LedControl/include + ${UTILS_SOURCE_PATH}/WebServer/include + ${HAL_SOURCE_PATH}/include + ${MIDDLEWARE_SOURCE_PATH}/AppManager/include + ${MIDDLEWARE_SOURCE_PATH}/AppManager/src + ${TEST_SOURCE_PATH} + ${TEST_SOURCE_PATH}/hal/tool/include + ${TEST_SOURCE_PATH}/utils/LinuxApiMock/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} + ${TEST_TOOLS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES_MAIN) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +set(TARGET_NAME AppManagerTest) +add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} AppManagerTestTool HalTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} AppManager Servers KeyControl LedControl Hal LinuxApiMock) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + AppManagerTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make AppManagerTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + AppManagerTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make AppManagerTest_code_check + COMMAND make AppManagerTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/middleware/AppManager/mainTest.cpp b/test/middleware/AppManager/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/middleware/AppManager/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/middleware/AppManager/src/AppManager_Test.cpp b/test/middleware/AppManager/src/AppManager_Test.cpp new file mode 100644 index 0000000..f65bc77 --- /dev/null +++ b/test/middleware/AppManager/src/AppManager_Test.cpp @@ -0,0 +1,384 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "AppManagerTestTool.h" +#include "HalTestTool.h" +#include "IAppManager.h" +#include "ILog.h" +#include +#include +#include +namespace AppManagerTest +{ +class AppManagerTest : public testing::Test, public AppManagerTestTool, public HalTestTool +{ +public: + AppManagerTest() : mAppParam(APP_MANAGER_DEVICE_IP, APP_MANAGER_HTTP_SERVER_PORT, APP_MANAGER_TCP_SERVER_PORT) + { + } + virtual ~AppManagerTest() + { + } + static void SetUpTestCase() + { + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + } + static void TearDownTestCase() + { + ILogUnInit(); + DestroyLogModule(); + } + virtual void SetUp() + { + HalTestTool::Init(); + AppManagerTestTool::Init(); + CreateHalCppModule(); + CreateAppManagerModule(); + } + virtual void TearDown() + { + AppManagerTestTool::UnInit(); + HalTestTool::UnInit(); + DestroyAppManagerModule(); + DestroyHalCppModule(); + } + +protected: + const AppParam mAppParam; +}; +// ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_Demo0 +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_Demo0) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockAppClientConnect(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetRecordingStatus(RecordingStatus::RECORDING_START); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_Demo +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_Demo) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetProductInfo(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_Upload +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_Upload) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockUploadFiles(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetDeviceAttr +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetDeviceAttr) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetDeviceAttr(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetMediaInfo +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetMediaInfo) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetMediaInfo(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetSdCardInfo +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetSdCardInfo) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetSdCardInfo(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetBatteryInfo +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetBatteryInfo) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetBatteryInfo(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetDateTime +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetDateTime) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetDateTime(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetTimeZone +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetTimeZone) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetTimeZone(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamValue +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamValue) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetParamValue("all"); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamValue2 +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamValue2) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetParamValue("rec"); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamItems +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamItems) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetParamItems("all"); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamItems2 +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamItems2) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetParamItems("mic"); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetCapability +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetCapability) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetCapability(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetLockVideoStatus +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetLockVideoStatus) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetLockVideoStatus(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetStorageInfo +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetStorageInfo) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetStorageInfo(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetStorageFileList +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetStorageFileList) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + AppManagerTestTool::MockGetStorageFileList(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetParamValue +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetParamValue) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetParamValue(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_EnterRecorder +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_EnterRecorder) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockEnterRecorder(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_AppPlayback +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_AppPlayback) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockAppPlayback(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetRecordingStatus +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetRecordingStatus) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockAppClientConnect(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetRecordingStatus(RecordingStatus::RECORDING_START); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetMicrophoneStatus +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetMicrophoneStatus) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockAppClientConnect(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetMicrophoneStatus(MicrophoneStatus::ON); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetBatteryStatus +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetBatteryStatus) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockAppClientConnect(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetBatteryStatus(BatteryStatus::CHARGING, 20); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetSdCardStatus +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetSdCardStatus) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockAppClientConnect(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockSetSdCardStatus(SdCardStatus::NOT_INSERTED); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_DeletedFileMessage +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_DeletedFileMessage) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockAppClientConnect(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockDeletedFileMessage("file_name", StorageFileType::VIDEO); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IAppManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/AppManagerTest +// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_CreatedFileMessage +TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_CreatedFileMessage) +{ + std::shared_ptr monitor = AppManagerTestTool::MakeMonitorMock(); + IAppManager::GetInstance()->Init(mAppParam); + IAppManager::GetInstance()->SetAppMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockAppClientConnect(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockCreatedFileMessage("file_name", StorageFileType::VIDEO); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IAppManager::GetInstance()->UnInit(); +} +} // namespace AppManagerTest \ No newline at end of file diff --git a/test/middleware/AppManager/tool/include/AppManagerTestTool.h b/test/middleware/AppManager/tool/include/AppManagerTestTool.h new file mode 100644 index 0000000..7031bc0 --- /dev/null +++ b/test/middleware/AppManager/tool/include/AppManagerTestTool.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef APP_MANAGER_TEST_TOOL_H +#define APP_MANAGER_TEST_TOOL_H +#include "GtestUsing.h" +#include "IAppManager.h" +#include +class AppManagerTestTool +{ +public: + AppManagerTestTool(); + virtual ~AppManagerTestTool() = default; + void Init(void); + void UnInit(void); + +protected: // About http + void MockGetProductInfo(void); + void MockGetDeviceAttr(void); + void MockGetMediaInfo(void); + void MockGetSdCardInfo(void); + void MockGetBatteryInfo(void); + void MockSetDateTime(void); + void MockSetTimeZone(void); + void MockUploadFiles(void); + void MockGetParamValue(const std::string ¶mName); + void MockGetParamItems(const std::string ¶mName); + void MockGetCapability(void); + void MockGetLockVideoStatus(void); + void MockGetStorageInfo(void); + void MockGetStorageFileList(void); + void MockSetParamValue(void); + void MockEnterRecorder(void); + void MockAppPlayback(void); + void MockMonitorSetFileList(std::vector &files); + +protected: // About TCP + void MockAppClientConnect(void); + void MockSetRecordingStatus(const RecordingStatus &status); + void MockSetMicrophoneStatus(const MicrophoneStatus &status); + void MockSetBatteryStatus(const BatteryStatus &status, const int &batteryCapacity); + void MockSetSdCardStatus(const SdCardStatus &status); + void MockDeletedFileMessage(const std::string &file, const StorageFileType &type); + void MockCreatedFileMessage(const std::string &file, const StorageFileType &type); + +private: + void AppManagerMockInit(std::shared_ptr &vMock); + void AppMonitorInit(std::shared_ptr &vMock); + +private: + std::shared_ptr mAppManagerMock; + std::shared_ptr mAppMonitorMock; + void *mAppClientTool; + std::shared_ptr mAppClient; + +public: + static std::shared_ptr MakeMonitorMock(void); +}; +#endif \ No newline at end of file diff --git a/test/middleware/CMakeLists.txt b/test/middleware/CMakeLists.txt new file mode 100644 index 0000000..5afda67 --- /dev/null +++ b/test/middleware/CMakeLists.txt @@ -0,0 +1,9 @@ + +# cmake_minimum_required(VERSION 2.8.0) +add_subdirectory(IpcConfig) +add_subdirectory(McuManager) +add_subdirectory(McuAskBase) +add_subdirectory(DeviceManager) +add_subdirectory(AppManager) +add_subdirectory(MediaManager) +add_subdirectory(HuntingUpgrade) \ No newline at end of file diff --git a/test/middleware/DeviceManager/CMakeLists.txt b/test/middleware/DeviceManager/CMakeLists.txt new file mode 100644 index 0000000..c159c9e --- /dev/null +++ b/test/middleware/DeviceManager/CMakeLists.txt @@ -0,0 +1,85 @@ +# 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 + ./tool/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/KeyControl/include + ${UTILS_SOURCE_PATH}/LedControl/include + ${HAL_SOURCE_PATH}/include + ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include + ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src + ${TEST_SOURCE_PATH} + ${TEST_SOURCE_PATH}/hal/tool/include + ${TEST_SOURCE_PATH}/utils/LinuxApiMock/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} + ${TEST_TOOLS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES_MAIN) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +set(TARGET_NAME DeviceManagerTest) +add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} DeviceManager DeviceManagerTestTool HalTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} Hal LinuxApiMock) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + DeviceManagerTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/DeviceManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make DeviceManagerTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + DeviceManagerTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/DeviceManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make DeviceManagerTest_code_check + COMMAND make DeviceManagerTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/middleware/DeviceManager/mainTest.cpp b/test/middleware/DeviceManager/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/middleware/DeviceManager/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp new file mode 100644 index 0000000..ebe37f0 --- /dev/null +++ b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "DeviceManagerTestTool.h" +#include "HalTestTool.h" +#include "IDeviceManager.h" +#include "ILog.h" +#include +#include +#include +namespace DeviceManagerTest +{ +const char *KEY_TEST = "KEY_TEST"; +const char *LED_TEST = "LED_TEST"; +class DeviceManagerTest : public testing::Test, virtual public HalTestTool, public DeviceManagerTestTool +{ +public: + DeviceManagerTest() + { + } + virtual ~DeviceManagerTest() + { + } + static void SetUpTestCase() + { + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + } + static void TearDownTestCase() + { + ILogUnInit(); + } + virtual void SetUp() + { + CreateAllKeysMcok(); + HalTestTool::Init(); + DeviceManagerTestTool::Init(); + CreateHalCppModule(); + CreateDeviceManagerModule(); + // CreateMcuManager(); + } + virtual void TearDown() + { + HalTestTool::UnInit(); + DeviceManagerTestTool::UnInit(); + DestroyDeviceManagerModule(); + DestroyAllKeysMock(); + } + +private: + void CreateAllKeysMcok(void) + { + std::shared_ptr key = HalTestTool::MakeKeyHalTest(KEY_TEST); + mAllKeysMock[KEY_TEST] = key; + std::shared_ptr led = HalTestTool::MakeLedHalTest(LED_TEST); + mAllLedsMock[LED_TEST] = led; + } + void DestroyAllKeysMock(void) + { + + mAllKeysMock.clear(); + mAllLedsMock.clear(); + } + +public: + // std::shared_ptr mLinuxTest; +protected: + std::map> mAllKeysMock; + std::map> mAllLedsMock; +}; +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyShortPress +/** + * @brief Construct a new test f object + * This test case demonstrates whether DeviceManager can reprocess key events and report them to the application when + * they are triggered. + */ +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyShortPress) +{ + SetAllKeysResult(mAllKeysMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + MockKeyClick(KEY_TEST); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IDeviceManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyInvalidShort +/** + * @brief Construct a new test f object + * This test case demonstrates whether DeviceManager can reprocess key events and report them to the application when + * they are triggered. + */ +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyInvalidShort) +{ + SetAllKeysResult(mAllKeysMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + MockKeyClick(KEY_TEST, 50); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IDeviceManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyLongPress +/** + * @brief Construct a new test f object + * This test case demonstrates whether DeviceManager can reprocess key events and report them to the application when + * they are triggered. + */ +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyLongPress) +{ + SetAllKeysResult(mAllKeysMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + MockKeyClick(KEY_TEST, 1000); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IDeviceManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_SetKeyMonitor +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_SetKeyMonitor) +{ + SetAllKeysResult(mAllKeysMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + MockKeyClick(KEY_TEST, 1000); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + MockKeyClick(KEY_TEST, 1000); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IDeviceManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed) +{ + SetAllLedsResult(mAllLedsMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IDeviceManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed2 +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed2) +{ + SetAllLedsResult(mAllLedsMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + constexpr int BLINK_TIME = 500; + ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, BLINK_TIME); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IDeviceManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed3 +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed3) +{ + SetAllLedsResult(mAllLedsMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + constexpr int BLINK_TIME = 500; + constexpr int KEEP_ALIVE = 1000; + ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE, BLINK_TIME); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IDeviceManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4 +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4) +{ + SetAllLedsResult(mAllLedsMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + constexpr int KEEP_ALIVE = 1000; + ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE, LED_NOT_BLINK); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IDeviceManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5 +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5) +{ + SetAllLedsResult(mAllLedsMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + std::shared_ptr ledControl = + DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + DeviceManagerTestTool::DestoryLedControl(ledControl); + DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IDeviceManager::GetInstance()->UnInit(); +} +} // namespace DeviceManagerTest \ No newline at end of file diff --git a/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h b/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h new file mode 100644 index 0000000..56f0a52 --- /dev/null +++ b/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef DEVICE_MANAGER_TEST_TOOL_H +#define DEVICE_MANAGER_TEST_TOOL_H +#include "DeviceManager.h" +#include "GtestUsing.h" +#include "HalTestTool.h" +#include "LedControl.h" +class DeviceManagerTool : public DeviceManager +{ +public: + DeviceManagerTool() = default; + virtual ~DeviceManagerTool() = default; + const StatusCode SetAllKeysMonitor(std::shared_ptr &monitor) override; + +private: + virtual const StatusCode SetAllKeysMonitorTrace(std::shared_ptr &monitor) + { + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); + } +}; +class DeviceManagerMock : public DeviceManagerTool +{ +public: + DeviceManagerMock() = default; + virtual ~DeviceManagerMock() = default; + MOCK_METHOD1(SetAllKeysMonitorTrace, const StatusCode(std::shared_ptr &)); +}; +class KeyMonitorMock : public VKeyMonitor +{ +public: + KeyMonitorMock() = default; + virtual ~KeyMonitorMock() = default; + MOCK_METHOD3(KeyEventReport, void(const std::string &, const VirtualKeyEvent &, const unsigned int &)); +}; +class DeviceManagerTestTool : virtual public HalTestTool +{ +public: + DeviceManagerTestTool() = default; + virtual ~DeviceManagerTestTool() = default; + void Init(void); + void UnInit(void); + +public: + std::shared_ptr ControlLed(const std::string &ledName, const LedState &state, + const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs); + void DestoryLedControl(std::shared_ptr &ledControl); + +protected: + void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) override; + +private: + void DeviceManagerMockInit(std::shared_ptr &mock); + void KeyMonitorInit(std::shared_ptr &mock, const std::string &keyName, + const unsigned int &pressingTimeMs); + +public: +private: + std::shared_ptr mDeviceManagerMock; + std::shared_ptr mKeyMonitorMock; +}; +#endif \ No newline at end of file diff --git a/test/middleware/HuntingUpgrade/CMakeLists.txt b/test/middleware/HuntingUpgrade/CMakeLists.txt new file mode 100644 index 0000000..ae94240 --- /dev/null +++ b/test/middleware/HuntingUpgrade/CMakeLists.txt @@ -0,0 +1,84 @@ +# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake) +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +include(${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/build/hunting_upgrade.cmake) +set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin) + +include_directories( + ./src + ./include + ./tool/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/StatusCode/include + # ${UTILS_SOURCE_PATH}/LinuxApi/include + # ${HAL_SOURCE_PATH}/include + ${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/include + ${TEST_SOURCE_PATH} + # ${TEST_SOURCE_PATH}/hal/tool/include + # ${TEST_SOURCE_PATH}/utils/LinuxApiMock/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} + ${TEST_TOOLS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES_MAIN) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +set(TARGET_NAME HuntingUpgradeTest) +add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} HuntingUpgradeTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} HuntingUpgrade UpgradeTool Log LinuxApiMock) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + HuntingUpgradeTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/HuntingUpgrade +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make HuntingUpgradeTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + HuntingUpgradeTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/HuntingUpgrade +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make HuntingUpgradeTest_code_check + COMMAND make HuntingUpgradeTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/middleware/HuntingUpgrade/mainTest.cpp b/test/middleware/HuntingUpgrade/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/middleware/HuntingUpgrade/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/middleware/HuntingUpgrade/src/HuntingUpgrade_Test.cpp b/test/middleware/HuntingUpgrade/src/HuntingUpgrade_Test.cpp new file mode 100644 index 0000000..92cbd45 --- /dev/null +++ b/test/middleware/HuntingUpgrade/src/HuntingUpgrade_Test.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "HuntingUpgradeTestTool.h" +#include "IHuntingUpgrade.h" +#include "ILog.h" +#include +#include +#include +namespace HuntingUpgradeTest +{ +class HuntingUpgradeTest : public testing::Test, public HuntingUpgradeTestTool +{ +public: + HuntingUpgradeTest() + { + } + virtual ~HuntingUpgradeTest() + { + } + static void SetUpTestCase() + { + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + } + static void TearDownTestCase() + { + ILogUnInit(); + DestroyLogModule(); + } + virtual void SetUp() + { + // HalTestTool::Init(); + HuntingUpgradeTestTool::Init(); + // CreateHalCppModule(); + // CreateHuntingUpgradeModule(); + CreateHuntingUpgradeModule(); + } + virtual void TearDown() + { + HuntingUpgradeTestTool::UnInit(); + // HalTestTool::UnInit(); + // DestroyHuntingUpgradeModule(); + // DestroyHalCppModule(); + DestroyHuntingUpgradeModule(); + } +}; +/** + * @brief + * ../output_files/test/bin/HuntingUpgradeTest --gtest_filter=HuntingUpgradeTest.HS_RH_UNIT_HuntingUpgrade_EXAMPLE_Demo0 + */ +TEST_F(HuntingUpgradeTest, HS_RH_UNIT_HuntingUpgrade_EXAMPLE_Demo0) +{ + HuntingUpgradeTestTool::CreateUpgradeFile(); + IHuntingUpgrade::GetInstance()->CheckUpgradeFile(); +} +} // namespace HuntingUpgradeTest \ No newline at end of file diff --git a/test/middleware/HuntingUpgrade/tool/include/HuntingUpgradeTestTool.h b/test/middleware/HuntingUpgrade/tool/include/HuntingUpgradeTestTool.h new file mode 100644 index 0000000..141622e --- /dev/null +++ b/test/middleware/HuntingUpgrade/tool/include/HuntingUpgradeTestTool.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HUNTING_UPGRADE_TEST_TOOL_H +#define HUNTING_UPGRADE_TEST_TOOL_H +#include "GtestUsing.h" +class HuntingUpgradeTestTool +{ +public: + HuntingUpgradeTestTool() = default; + virtual ~HuntingUpgradeTestTool() = default; + +protected: + void Init(void); + void UnInit(void); + void CreateUpgradeFile(void); + +private: + bool CheckDirectory(const char *filepath); +}; +#endif \ No newline at end of file diff --git a/test/middleware/IpcConfig/CMakeLists.txt b/test/middleware/IpcConfig/CMakeLists.txt new file mode 100644 index 0000000..d30bac5 --- /dev/null +++ b/test/middleware/IpcConfig/CMakeLists.txt @@ -0,0 +1,84 @@ + +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +include(${MIDDLEWARE_SOURCE_PATH}/IpcConfig/build/ipc_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin) + +include_directories( + . + ./src + ./include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/LinuxApi/include + ${UTILS_SOURCE_PATH}/ConfigBase/include + ${MIDDLEWARE_SOURCE_PATH}/IpcConfig/include + ${MIDDLEWARE_SOURCE_PATH}/IpcConfig/src + ${TEST_SOURCE_PATH} + ${TEST_SOURCE_PATH}/middleware/IpcConfig/tool/include + ${TEST_SOURCE_PATH}/utils/LinuxApiMock/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} + ${TEST_TOOLS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES_MAIN) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +set(TARGET_NAME IpcConfigTest) +add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} IpcConfigTestTool LinuxApi gtest gmock pthread) +target_link_libraries(${TARGET_NAME} IpcConfig LinuxApiMock) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() +if ("${COMPILE_IMPROVE_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/IpcConfig +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make IpcConfigTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + IpcConfigTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/IpcConfig +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make IpcConfigTest_code_check + COMMAND make IpcConfigTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/middleware/IpcConfig/mainTest.cpp b/test/middleware/IpcConfig/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/middleware/IpcConfig/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/middleware/IpcConfig/src/IpcConfig_Test.cpp b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp new file mode 100644 index 0000000..9831828 --- /dev/null +++ b/test/middleware/IpcConfig/src/IpcConfig_Test.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "IIpcConfig.h" +#include "ILog.h" +#include "IpcConfigImpl.h" +#include "LinuxApi.h" +// #include +#include +namespace IpcConfigTest +{ +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigDemo.Demo +TEST(IpcConfigDemo, Demo) +{ + CreateLogModule(); + CreateIpcConfigModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + IIpcConfig::GetInstance()->Init(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + const int workMode = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::WORK_MODE); + LogInfo("Get workMode = %d\n", workMode); + const int wworkMode = static_cast(WorkMode::MODE_PIC_VIDEO); + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::WORK_MODE, wworkMode); + + const int continuousShot = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::CONTINUOUS_SHOT); + LogInfo("Get continuousShot = %d\n", continuousShot); + const int ccontinuousShot = CONTINUOUS_SHOT_TWO_PIC; + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::CONTINUOUS_SHOT, ccontinuousShot); + + const int burstPhotoInterval = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::BURST_PHOTO_INTERVAL); + LogInfo("Get burstPhotoInterval = %d\n", burstPhotoInterval); + const int bburstPhotoInterval = BURST_PHOTO_INTERVAL_DEFAULT; + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::BURST_PHOTO_INTERVAL, bburstPhotoInterval); + + const std::string imageSize = IIpcConfig::GetInstance()->GetString(IpcConfigKey::IMGAE_SIZE); + LogInfo("Get image_size = %s\n", imageSize.c_str()); + const std::string iimageSize = "410*410"; + IIpcConfig::GetInstance()->SetString(IpcConfigKey::IMGAE_SIZE, iimageSize); + + const int videoSize = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::VIDEO_LENGTH); + LogInfo("Get video_size = %d\n", videoSize); + const int vvideoSize = VIDEO_SIZE_10; + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::VIDEO_LENGTH, vvideoSize); + + const int infraredIampPower = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::INFRARED_POWER); + LogInfo("Get infrared_lamp_power = %d\n", infraredIampPower); + const int iinfraredIampPower = INFRARED_IAMP_POWER_LOW; + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::INFRARED_POWER, iinfraredIampPower); + + const int delayed = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_DELAYED); + LogInfo("Get delayed = %d\n", delayed); + const int ddelayed = DELAYED_MAX; + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_DELAYED, ddelayed); + + const int pirSensitivity = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_SENSITIVITY); + LogInfo("Get pir_sensitivity = %d\n", pirSensitivity); + const int ppirSensitivity = PIR_SENSITIVITY_MAX; + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_SENSITIVITY, ppirSensitivity); + + const bool storageLoopSwitch = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::STORAGE_LOOP_SWITCH); + LogInfo("Get storage_loop_switch = %d\n", storageLoopSwitch); + const bool sstorageLoopSwitch = false; + IIpcConfig::GetInstance()->SetBool(IpcConfigKey::STORAGE_LOOP_SWITCH, sstorageLoopSwitch); + + const bool factoryReset = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::FACTORY_RESET_FLAG); + LogInfo("Get factory_reset = %d\n", factoryReset); + const bool ffactoryReset = false; + IIpcConfig::GetInstance()->SetBool(IpcConfigKey::FACTORY_RESET_FLAG, ffactoryReset); + + const bool formattingSDCard = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::FORMATTING_SD_CARD); + LogInfo("Get formatting_SD_card = %d\n", formattingSDCard); + const bool fformattingSDCard = false; + IIpcConfig::GetInstance()->SetBool(IpcConfigKey::FORMATTING_SD_CARD, fformattingSDCard); + + const std::string workInterval = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WORK_INTERVAL); + LogInfo("Get work_Interval = %s\n", workInterval.c_str()); + const std::string wworkInterval = "06:00:01-06:00:00"; + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WORK_INTERVAL, wworkInterval); + + const std::string drakMode = IIpcConfig::GetInstance()->GetString(IpcConfigKey::DARK_MODE); + LogInfo("Get dark_mode = %s\n", drakMode.c_str()); + const std::string ddrakMode = "18:00:00-06:00:00"; + IIpcConfig::GetInstance()->SetString(IpcConfigKey::DARK_MODE, ddrakMode); + + short testShort = IIpcConfig::GetInstance()->GetShort(IpcConfigKey::TEST_SHORT); + LogInfo("Get test_short = %d\n", testShort); + const short numShort = 888; + IIpcConfig::GetInstance()->SetShort(IpcConfigKey::TEST_SHORT, numShort); + + long testLong = IIpcConfig::GetInstance()->GetLong(IpcConfigKey::TEST_LONG); + LogInfo("Get test_long = %d\n", testLong); + const long numLong = 777; + IIpcConfig::GetInstance()->SetLong(IpcConfigKey::TEST_LONG, numLong); + + long long testLLong = IIpcConfig::GetInstance()->GetLLong(IpcConfigKey::TEST_LLONG); + LogInfo("Get test_llong = %d\n", testLLong); + const long long numLLong = 666; + IIpcConfig::GetInstance()->SetLLong(IpcConfigKey::TEST_LLONG, numLLong); + + char testChar = IIpcConfig::GetInstance()->GetChar(IpcConfigKey::TEST_CHAR); + LogInfo("Get test_char = %c\n", testChar); + const char numChar = 'A'; + IIpcConfig::GetInstance()->SetChar(IpcConfigKey::TEST_CHAR, numChar); + + float testFloat = IIpcConfig::GetInstance()->GetFloat(IpcConfigKey::TEST_FLOAT); + LogInfo("Get test_float = %lf\n", testFloat); + const float numFloat = 1.123456; + IIpcConfig::GetInstance()->SetFloat(IpcConfigKey::TEST_FLOAT, numFloat); + + IIpcConfig::GetInstance()->ConfigFileSave(); + IIpcConfig::GetInstance()->UnInit(); + ILogUnInit(); + DestroyIpcConfigModule(); + DestroyLogModule(); +} +} // namespace IpcConfigTest \ No newline at end of file diff --git a/test/middleware/IpcConfig/src_mock/IpcConfigTest.cpp b/test/middleware/IpcConfig/src_mock/IpcConfigTest.cpp new file mode 100644 index 0000000..cf4b85e --- /dev/null +++ b/test/middleware/IpcConfig/src_mock/IpcConfigTest.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "IpcConfigTest.h" +#include "IIpcConfig.h" +#include "ILog.h" +#include "LinuxApi.h" +#include +IpcConfigTest::IpcConfigTest() +{ +} +IpcConfigTest::~IpcConfigTest() +{ +} +void IpcConfigTest::SetUpTestCase() +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); +} +void IpcConfigTest::TearDownTestCase() +{ + ILogUnInit(); + DestroyLogModule(); +} +void IpcConfigTest::SetUp() +{ + mLinuxTest = LinuxTest::CreateLinuxTest(); + std::shared_ptr test = mLinuxTest; + LinuxApiMock::GetInstance(&test); + IpcConfigTestTool::Init(mLinuxTest); + CreateIpcConfigModule(); + IIpcConfig::GetInstance()->Init(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); +} +void IpcConfigTest::TearDown() +{ + IIpcConfig::GetInstance()->UnInit(); + DestroyIpcConfigModule(); + RemoveConfigFile(); + IpcConfigTestTool::UnInit(); + mLinuxTest = std::make_shared(); + std::shared_ptr test = std::make_shared(); + LinuxApiMock::GetInstance(&test); +} +void IpcConfigTest::RemoveConfigFile(void) +{ + constexpr int FIEL_EXIST = 0; + if (FIEL_EXIST == access(IPC_CONFIG_FILE_PATH, F_OK)) { + constexpr int BUFF_SIZE = 128; + char cmd[BUFF_SIZE] = {0}; + snprintf(cmd, BUFF_SIZE, "rm -f %s", IPC_CONFIG_FILE_PATH); + fx_system(cmd); + } +} \ No newline at end of file diff --git a/test/middleware/IpcConfig/src_mock/IpcConfigTest.h b/test/middleware/IpcConfig/src_mock/IpcConfigTest.h new file mode 100644 index 0000000..6a050d8 --- /dev/null +++ b/test/middleware/IpcConfig/src_mock/IpcConfigTest.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef IPC_CONFIG_TEST_H +#define IPC_CONFIG_TEST_H +#include "GtestUsing.h" +#include "IpcConfigTestTool.h" +class IpcConfigTest : public testing::Test, public IpcConfigTestTool +{ +public: + IpcConfigTest(); + virtual ~IpcConfigTest(); + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp(); + virtual void TearDown(); + +private: + void RemoveConfigFile(void); + +public: + std::shared_ptr mLinuxTest; +}; +#endif \ No newline at end of file diff --git a/test/middleware/IpcConfig/src_mock/IpcConfig_Mock_Test.cpp b/test/middleware/IpcConfig/src_mock/IpcConfig_Mock_Test.cpp new file mode 100644 index 0000000..ef0b2ba --- /dev/null +++ b/test/middleware/IpcConfig/src_mock/IpcConfig_Mock_Test.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "IpcConfigTest.h" +#include "IIpcConfig.h" +#include "ILog.h" +#include "IpcConfigImpl.h" +extern const char *CONFIG_WIFI_SSID_DEFAULT; +extern const char *CONFIG_WIFI_PASSWORD_DEFAULT; +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_WifiSsid +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_WifiSsid) +{ + const char *MODIFIED_STRING = "modified_string"; + const char *MODIFIED_STRING_SHORTER = "modified"; + const char *LONG_STRING = + "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL" + "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"; + std::string config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID); + LogInfo("Get wifi ssid = %s\n", config.c_str()); + EXPECT_STREQ(config.c_str(), CONFIG_WIFI_SSID_DEFAULT); + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, MODIFIED_STRING); + config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID); + LogInfo("Get wifiSsid = %s\n", config.c_str()); + EXPECT_STREQ(config.c_str(), MODIFIED_STRING); + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, LONG_STRING); + config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID); + EXPECT_STREQ(config.c_str(), MODIFIED_STRING); + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, ""); + config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID); + EXPECT_STREQ(config.c_str(), ""); + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, MODIFIED_STRING_SHORTER); + config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID); + EXPECT_STREQ(config.c_str(), MODIFIED_STRING_SHORTER); + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, MODIFIED_STRING); + config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID); + EXPECT_STREQ(config.c_str(), MODIFIED_STRING); +} +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_WifiPassword +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_WifiPassword) +{ + const char *MODIFIED_STRING = "99999999"; + const char *MODIFIED_STRING_SHORTER = "999"; + const char *LONG_STRING = + "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL" + "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"; + std::string config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD); + LogInfo("Get wifi password = %s\n", config.c_str()); + EXPECT_STREQ(config.c_str(), CONFIG_WIFI_PASSWORD_DEFAULT); + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, MODIFIED_STRING); + config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD); + LogInfo("Get wifi password = %s\n", config.c_str()); + EXPECT_STREQ(config.c_str(), MODIFIED_STRING); + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, LONG_STRING); + config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD); + EXPECT_STREQ(config.c_str(), MODIFIED_STRING); + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, ""); + config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD); + EXPECT_STREQ(config.c_str(), ""); + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, MODIFIED_STRING_SHORTER); + config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD); + EXPECT_STREQ(config.c_str(), MODIFIED_STRING_SHORTER); + IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, MODIFIED_STRING); + config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD); + EXPECT_STREQ(config.c_str(), MODIFIED_STRING); +} +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_WorkMode +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_WorkMode) +{ + WorkMode config = IIpcConfig::GetInstance()->GetWorkMode(); + EXPECT_EQ(static_cast(config), static_cast(CONFIG_WORK_MODE_DEFAULT)); + IIpcConfig::GetInstance()->SetWorkMode(WorkMode::MODE_PIC_VIDEO); + config = IIpcConfig::GetInstance()->GetWorkMode(); + EXPECT_EQ(static_cast(config), static_cast(WorkMode::MODE_PIC_VIDEO)); +} +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_ContinueShot +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_ContinueShot) +{ + int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::CONTINUOUS_SHOT); + EXPECT_EQ(config, CONFIG_CONTINUE_SHOT_DEFAULT); + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::CONTINUOUS_SHOT, 2); + config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::CONTINUOUS_SHOT); + EXPECT_EQ(config, 2); +} +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_BurstPhotoInterval +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_BurstPhotoInterval) +{ + int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::BURST_PHOTO_INTERVAL); + EXPECT_EQ(config, CONFIG_BURST_PHOTO_INTERVAL_DEFAULT); + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::BURST_PHOTO_INTERVAL, 10); + config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::BURST_PHOTO_INTERVAL); + EXPECT_EQ(config, 10); +} +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_ImageSize +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_ImageSize) +{ + int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::IMGAE_SIZE); + EXPECT_EQ(config, CONFIG_IMAGE_SIZE_DEFAULT); + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::IMGAE_SIZE, 16); + config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::IMGAE_SIZE); + EXPECT_EQ(config, 16); +} +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_VideoLength +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_VideoLength) +{ + int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::VIDEO_LENGTH); + EXPECT_EQ(config, CONFIG_VIDEO_SIZE_DEFAULT); + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::VIDEO_LENGTH, 15); + config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::VIDEO_LENGTH); + EXPECT_EQ(config, 15); +} +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_PirDelayed +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_PirDelayed) +{ + int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_DELAYED); + EXPECT_EQ(config, CONFIG_PIR_DELAYED_DEFAULT); + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_DELAYED, 15); + config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_DELAYED); + EXPECT_EQ(config, 15); +} +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_StorageLoop +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_StorageLoop) +{ + ConfigSwitch config = IIpcConfig::GetInstance()->GetSwitch(IpcConfigKey::STORAGE_LOOP_SWITCH); + EXPECT_EQ(static_cast(config), + static_cast(CONFIG_STORAGE_LOOP_DEFAULT == true ? ConfigSwitch::ON : ConfigSwitch::OFF)); + IIpcConfig::GetInstance()->SetSwitch(IpcConfigKey::STORAGE_LOOP_SWITCH, ConfigSwitch::OFF); + config = IIpcConfig::GetInstance()->GetSwitch(IpcConfigKey::STORAGE_LOOP_SWITCH); + EXPECT_EQ(static_cast(config), static_cast(ConfigSwitch::OFF)); +} +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_InfraredPower +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_InfraredPower) +{ + ConfigLevel config = IIpcConfig::GetInstance()->GetLevel(IpcConfigKey::INFRARED_POWER); + EXPECT_EQ(static_cast(config), static_cast(CONFIG_INFRARED_POWER_DEFAULT)); + IIpcConfig::GetInstance()->SetLevel(IpcConfigKey::INFRARED_POWER, ConfigLevel::HIGHT); + config = IIpcConfig::GetInstance()->GetLevel(IpcConfigKey::INFRARED_POWER); + EXPECT_EQ(static_cast(config), static_cast(ConfigSwitch::OFF)); +} +// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_PirSensitivity +TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_PirSensitivity) +{ + int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_SENSITIVITY); + EXPECT_EQ(config, CONFIG_PIR_SENSITIVITY_DEFAULT); + IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_SENSITIVITY, 0); + config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_SENSITIVITY); + EXPECT_EQ(config, 0); +} \ No newline at end of file diff --git a/test/middleware/IpcConfig/tool/include/IpcConfigTestTool.h b/test/middleware/IpcConfig/tool/include/IpcConfigTestTool.h new file mode 100644 index 0000000..50af013 --- /dev/null +++ b/test/middleware/IpcConfig/tool/include/IpcConfigTestTool.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef IPC_CONFIG_TEST_TOOL_H +#define IPC_CONFIG_TEST_TOOL_H +#include "GtestUsing.h" +#include "LinuxApiMock.h" +class IpcConfigTestTool +{ +public: + IpcConfigTestTool() = default; + virtual ~IpcConfigTestTool() = default; + void Init(std::shared_ptr &mock); + void UnInit(void); + +private: + void MockMountedFile(void); + FILE *OpenMockMountedFile(const char *mode); + +private: + std::shared_ptr mLinuxTest; + FILE *mFile = nullptr; +}; +#endif \ No newline at end of file diff --git a/test/middleware/McuAskBase/CMakeLists.txt b/test/middleware/McuAskBase/CMakeLists.txt new file mode 100644 index 0000000..5d541c1 --- /dev/null +++ b/test/middleware/McuAskBase/CMakeLists.txt @@ -0,0 +1,4 @@ + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/middleware/McuAskBase/tool/include/McuAskBaseTestTool.h b/test/middleware/McuAskBase/tool/include/McuAskBaseTestTool.h new file mode 100644 index 0000000..45b7e39 --- /dev/null +++ b/test/middleware/McuAskBase/tool/include/McuAskBaseTestTool.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MCU_ASK_BASE_TEST_TOOL_H +#define MCU_ASK_BASE_TEST_TOOL_H +#include "GtestUsing.h" +#include "McuAskBase.h" +class McuAskBaseTest : public McuAskBase +{ +public: + McuAskBaseTest(const McuAskBlock isBlock, const McuAskReply needReply, + const unsigned int timeoutMs = DEFAULT_ASK_TIMEOUT) + : McuAskBase(isBlock, needReply, timeoutMs) + { + // + } + virtual ~McuAskBaseTest() = default; + ASK_RESULT Blocking(void) override; + bool NeedReply(void) override; + void ReplyFinished(const bool result) override; + bool IfTimeout(const unsigned int &integrationTimeMs) override; + +protected: + virtual void BlockingTrace(void) + { + } + virtual void NeedReplyTrace(void) + { + } + virtual void ReplyFinishedTrace(const bool result) + { + } + virtual void IfTimeoutTrace(const unsigned int &integrationTimeMs) + { + } +}; +class McuAskBaseTestTool : public McuAskBaseTest +{ +public: + McuAskBaseTestTool(const McuAskBlock isBlock, const McuAskReply needReply, + const unsigned int timeoutMs = DEFAULT_ASK_TIMEOUT) + : McuAskBaseTest(isBlock, needReply, timeoutMs) + { + // + } + virtual ~McuAskBaseTestTool() = default; + MOCK_METHOD0(BlockingTrace, void(void)); + MOCK_METHOD0(NeedReplyTrace, void(void)); + MOCK_METHOD1(ReplyFinishedTrace, void(const bool)); + MOCK_METHOD1(IfTimeoutTrace, void(const unsigned int &)); + +public: + void McuAskDefaultFeatures(std::shared_ptr &mock); + void McuAskDoNothing(std::shared_ptr &mock); + void NoNeedToBlocking(std::shared_ptr &mock); +}; +#endif \ No newline at end of file diff --git a/test/middleware/McuManager/CMakeLists.txt b/test/middleware/McuManager/CMakeLists.txt new file mode 100644 index 0000000..c1ef705 --- /dev/null +++ b/test/middleware/McuManager/CMakeLists.txt @@ -0,0 +1,92 @@ +# 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 + ./tool/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/UartDevice/include + ${UTILS_SOURCE_PATH}/McuProtocol/include + ${MIDDLEWARE_SOURCE_PATH}/McuManager/include + ${MIDDLEWARE_SOURCE_PATH}/McuManager/src + ${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include + ${TEST_SOURCE_PATH} + ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include + ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include + ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include + ${TEST_SOURCE_PATH}/middleware/McuAskBase/tool/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} + ${TEST_TOOLS_OUTPUT_PATH} +) + +if (DEFINED MCU_UART_DEVICE) + add_definitions(-DMCU_UART_DEVICE=\"${MCU_UART_DEVICE}\") +else() + message(FATAL_ERROR "You set define MCU_UART_DEVICE in toolchan .cmake file to tell what uart device to contrl.") +endif() + +aux_source_directory(. SRC_FILES_MAIN) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +set(TARGET_NAME McuManagerTest) +add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} McuAskBase McuManager McuManagerTestTool McuAskBaseTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} McuProtocolTestTool UartDeviceTestTool UartDevice LinuxApiMock) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + McuManagerTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/McuManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make McuManagerTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + McuManagerTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/McuManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make McuManagerTest_code_check + COMMAND make McuManagerTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() +define_file_name(${TARGET_NAME}) + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/middleware/McuManager/mainTest.cpp b/test/middleware/McuManager/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/middleware/McuManager/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/middleware/McuManager/src/McuManager_Test.cpp b/test/middleware/McuManager/src/McuManager_Test.cpp new file mode 100644 index 0000000..551f333 --- /dev/null +++ b/test/middleware/McuManager/src/McuManager_Test.cpp @@ -0,0 +1,254 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ILog.h" +#include "IMcuManager.h" +#include "McuAskBase.h" +#include "McuAskBaseTestTool.h" +#include +#include +#include +namespace McuManagerTest +{ +class McuManagerTest : public testing::Test +{ +public: + McuManagerTest() + { + } + virtual ~McuManagerTest() + { + } + static void SetUpTestCase() + { + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + } + static void TearDownTestCase() + { + ILogUnInit(); + } + virtual void SetUp() + { + CreateMcuManager(); + IMcuManager::GetInstance()->Init(); + } + virtual void TearDown() + { + IMcuManager::GetInstance()->UnInit(); + DestroyMcuManager(); + } +}; +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_GetIpcMission +TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_GetIpcMission) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY) + { + mDataReply = IpcMission::END; + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + EXPECT_GT(static_cast(mDataReply), 0); + EXPECT_LT(static_cast(mDataReply), static_cast(IpcMission::END)); + } + else { + LogError("Ask data falied.\n"); + } + } + }; + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + IMcuManager::GetInstance()->GetIpcMission(ask); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission +TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission) +{ + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission) override + { + LogInfo("RecvIpcMissionEvent %s\n", IMcuManager::GetInstance()->PrintIpcMissionString(mission)); + std::shared_ptr> ask = std::dynamic_pointer_cast>(recv); + ask->mDataReply = ASK_RESULT::SUCCEED; + recv->ReplyFinished(true); + } + }; + // IMcuManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + // IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_CutOffPowerSupply +TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_CutOffPowerSupply) +{ + class McuAskTest : public McuAskBaseTestTool + { + public: + McuAskTest() + : McuAskBaseTestTool(McuAskBlock::UNRELATED, + McuAskReply::NEED_NOT_REPLY) // using McuAskReply::NEED_NOT_REPLY + { + } + virtual ~McuAskTest() = default; + }; + // IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->CutOffPowerSupply(ask); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + // IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog +TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, + McuAskReply::NEED_REPLY) // using McuAskBlock::BLOCK, + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + EXPECT_LT(static_cast(mDataReply), static_cast(ASK_RESULT::SUCCEED)); + } + else { + LogError("Ask data falied.\n"); + } + } + }; + // IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->SetFeedingCycleForWatchDog(ask, 1, 1, 1); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_FeedWatchDog +TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_FeedWatchDog) +{ + class McuAskTest : public McuAskBaseTestTool + { + public: + McuAskTest() + : McuAskBaseTestTool(McuAskBlock::UNRELATED, + McuAskReply::NEED_NOT_REPLY) // using McuAskReply::NEED_NOT_REPLY + { + } + virtual ~McuAskTest() = default; + }; + // IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->FeedWatchDog(ask); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + // IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_SetDataTime +TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_SetDataTime) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, + McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + EXPECT_LT(static_cast(mDataReply), static_cast(ASK_RESULT::SUCCEED)); + } + else { + LogError("Ask data falied.\n"); + } + } + }; + // IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + McuAskDateTime value(2014, 1, 15, 0, 0, 0); + StatusCode code = IMcuManager::GetInstance()->SetDateTime(ask, value); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_SetPirSensitivity +TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_SetPirSensitivity) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, + McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + EXPECT_LT(static_cast(mDataReply), static_cast(ASK_RESULT::SUCCEED)); + } + else { + LogError("Ask data falied.\n"); + } + } + }; + // IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->SetPirSensitivity(ask, 1); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // IMcuManager::GetInstance()->UnInit(); +} +} // namespace McuManagerTest \ No newline at end of file diff --git a/test/middleware/McuManager/src_mock/McuManagerMockTest.cpp b/test/middleware/McuManager/src_mock/McuManagerMockTest.cpp new file mode 100644 index 0000000..c116866 --- /dev/null +++ b/test/middleware/McuManager/src_mock/McuManagerMockTest.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "McuManagerMockTest.h" +McuManagerMockTest::McuManagerMockTest() +{ +} +McuManagerMockTest::~McuManagerMockTest() +{ +} +void McuManagerMockTest::SetUpTestCase() +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); +} +void McuManagerMockTest::TearDownTestCase() +{ + ILogUnInit(); +} +void McuManagerMockTest::SetUp() +{ + mLinuxTest = LinuxTest::CreateLinuxTest(); + std::shared_ptr test = mLinuxTest; + LinuxApiMock::GetInstance(&test); + LinuxApiMock::GetInstance()->Init(); + McuManagerTestTool::Init(mLinuxTest); + CreateMcuManager(); +} +void McuManagerMockTest::TearDown() +{ + LinuxApiMock::GetInstance()->UnInit(); + mLinuxTest = std::make_shared(); + std::shared_ptr test = std::make_shared(); + LinuxApiMock::GetInstance(&test); + McuManagerTestTool::UnInit(); + DestroyMcuManager(); +} \ No newline at end of file diff --git a/test/middleware/McuManager/src_mock/McuManagerMockTest.h b/test/middleware/McuManager/src_mock/McuManagerMockTest.h new file mode 100644 index 0000000..620cdf3 --- /dev/null +++ b/test/middleware/McuManager/src_mock/McuManagerMockTest.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MCU_MANAGER_MOCK_TEST_H +#define MCU_MANAGER_MOCK_TEST_H +#include "ILog.h" +#include "IMcuManager.h" +#include "LinuxApiMock.h" +#include "McuManagerTestTool.h" +#include +#include +class McuManagerMockTest : public testing::Test, public McuManagerTestTool +{ +public: + McuManagerMockTest(); + virtual ~McuManagerMockTest(); + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp(); + virtual void TearDown(); + +public: + std::shared_ptr mLinuxTest; +}; +#endif \ No newline at end of file diff --git a/test/middleware/McuManager/src_mock/McuManager_AbnormalData_Test.cpp b/test/middleware/McuManager/src_mock/McuManager_AbnormalData_Test.cpp new file mode 100644 index 0000000..e47e98f --- /dev/null +++ b/test/middleware/McuManager/src_mock/McuManager_AbnormalData_Test.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "McuAskBase.h" +#include "McuAskBaseTestTool.h" +#include "McuManagerMockTest.h" +#include +const unsigned char McuRecvData[] = { + 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x08, 0x41, 0x01, 0x00, 0x0A, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00, + 0x0A, 0x41, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x41, 0x07, 0x00, 0x0C, 0x35, 0x88, 0xFA, + 0xC1, 0x00, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x41, 0x07, 0x00, 0x0C, 0x35, 0x88, 0xFA, + 0xC1, 0x00, 0x00, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, + 0x41, 0x07, 0x00, 0x0C, 0x35, 0x88, 0xFA, 0xC1, 0x00, 0x00, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A}; +const unsigned char McuSendData_GetDateTime[] = { + 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x41, 0x07, 0x00, 0x0C, 0x35, 0x88}; +const unsigned char McuSendData_OtherSideReplyGetIpcMission[] = { + 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x0D, 0x03, 0x09, 0xBA}; +namespace McuManager_AbnormalData_Test +{ +/** + * @brief Construct a new test f object + * ../output_files/test/bin/McuManagerTest + * --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_ProtocolLengthIsWrong + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_ProtocolLengthIsWrong) +{ + IMcuManager::GetInstance()->Init(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + McuManagerTestTool::MockOtherSideSendData(mLinuxTest, McuRecvData, sizeof(McuRecvData)); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IMcuManager::GetInstance()->UnInit(); +} +/** + * @brief Construct a new test f object + * ../output_files/test/bin/McuManagerTest + * --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_OtherSideGetDateTime + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_OtherSideGetDateTime) +{ + IMcuManager::GetInstance()->Init(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + McuManagerTestTool::MockOtherSideSendData(mLinuxTest, McuSendData_GetDateTime, sizeof(McuSendData_GetDateTime)); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IMcuManager::GetInstance()->UnInit(); +} +/** + * @brief Construct a new test f object + * ../output_files/test/bin/McuManagerTest + * --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_OtherSideReplyGetIpcMission + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_OtherSideReplyGetIpcMission) +{ + IMcuManager::GetInstance()->Init(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + McuManagerTestTool::MockOtherSideSendData( + mLinuxTest, McuSendData_OtherSideReplyGetIpcMission, sizeof(McuSendData_OtherSideReplyGetIpcMission)); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IMcuManager::GetInstance()->UnInit(); +} +} // namespace McuManager_AbnormalData_Test \ No newline at end of file diff --git a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp new file mode 100644 index 0000000..0dc3693 --- /dev/null +++ b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp @@ -0,0 +1,1112 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "McuAskBase.h" +#include "McuAskBaseTestTool.h" +#include "McuManagerMockTest.h" +#include +namespace McuManager_Mock_Test +{ +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_GetIpcMission +/** + * @brief Construct a new test f object + * This test is an example that demonstrates how to obtain IpcMission data and add the business code after obtaining the + * data in the abstract interface. + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_GetIpcMission) +{ + /** + * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the + * abstract interface. + */ + class McuAskTest : public McuAsk, public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY) + { + } // using McuAskBlock::BLOCK + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBase::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + LogInfo("GetIpcMission will block here.\n"); + IMcuManager::GetInstance()->GetIpcMission(ask); + LogInfo("GetIpcMission finished.\n"); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_GetIpcMission2 +/** + * @brief Construct a new test f object + * This test is an example that demonstrates how to obtain IpcMission data and add the business code after obtaining the + * data in the abstract interface. + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_GetIpcMission2) +{ + /** + * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the + * abstract interface. + */ + class McuAskTest : public McuAsk, public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY) + { + } // using McuAskBlock::NOT_BLOCK + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBase::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + LogInfo("GetIpcMission will not block here.\n"); + IMcuManager::GetInstance()->GetIpcMission(ask); + LogInfo("GetIpcMission finished.\n"); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMission +/** + * @brief Construct a new test f object + * This example demonstrates how to use a monitor to capture event information sent by an MCU. You must assign a value + * to the processing result (mDataReply member) and call the ReplyFinished function to complete the entire process. + */ +constexpr unsigned int TEST_SERIAL_NUMBER = 2; +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMission) +{ + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission) override + { + LogInfo("RecvIpcMissionEvent\n"); + // std::shared_ptr> ask = std::dynamic_pointer_cast>(recv); + // ask->mDataReply = ASK_RESULT::SUCCEED; + recv->ReplyFinished(true); + // EXPECT_EQ(TEST_SERIAL_NUMBER, recv->mSerialNumber); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideAskIpcMission(mLinuxTest, TEST_SERIAL_NUMBER); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_OtherSideSendHeartBeat +/** + * @brief Construct a new test f object + * This example demonstrates how to use a monitor to capture event information sent by an MCU. You must assign a value + * to the processing result (mDataReply member) and call the ReplyFinished function to complete the entire process. + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_OtherSideSendHeartBeat) +{ + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvMcuHeartBeatEvent(std::shared_ptr &recv) override + { + LogInfo("RecvMcuHeartBeatEvent\n"); + // std::shared_ptr> ask = std::dynamic_pointer_cast>(recv); + // ask->mDataReply = ASK_RESULT::SUCCEED; + recv->ReplyFinished(true); + // EXPECT_EQ(TEST_SERIAL_NUMBER, recv->mSerialNumber); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideAskHeartBeat(mLinuxTest, TEST_SERIAL_NUMBER); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_CutOffPowerSupply +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_CutOffPowerSupply) +{ + class McuAskTest : public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::UNRELATED, McuAskReply::NEED_NOT_REPLY) + { + } // using McuAskReply::NEED_NOT_REPLY + virtual ~McuAskTest() = default; + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + StatusCode code = IMcuManager::GetInstance()->CutOffPowerSupply(ask); + if (IsCodeOK(code) == true) { + LogInfo("Write data to mcu succeed.\n"); + } + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_FeedWatchDog +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_FeedWatchDog) +{ + class McuAskTest : public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::UNRELATED, McuAskReply::NEED_NOT_REPLY) + { + } // using McuAskReply::NEED_NOT_REPLY + virtual ~McuAskTest() = default; + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + StatusCode code = IMcuManager::GetInstance()->FeedWatchDog(ask); + if (IsCodeOK(code) == true) { + LogInfo("Write data to mcu succeed.\n"); + } + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_SetFeedingCycleForWatchDog +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_SetFeedingCycleForWatchDog) +{ + class McuAskTest : public McuAsk, public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY) + { + } // using McuAskBlock::BLOCK + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBase::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + StatusCode code = IMcuManager::GetInstance()->SetFeedingCycleForWatchDog(ask, 0, 0, 0); + if (IsCodeOK(code) == true) { + LogInfo("Write data to mcu succeed.\n"); + } + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_SetDateTime +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_SetDateTime) +{ + /** + * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the + * abstract interface. + */ + class McuAskTest : public McuAsk, public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY) + { + } // using McuAskBlock::NOT_BLOCK + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBase::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + McuAskDateTime value(2014, 1, 15, 0, 0, 0); + IMcuManager::GetInstance()->SetDateTime(ask, value); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_SetPirSensitivity +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_SetPirSensitivity) +{ + /** + * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the + * abstract interface. + */ + class McuAskTest : public McuAsk, public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY) + { + } // using McuAskBlock::NOT_BLOCK + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBase::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + IMcuManager::GetInstance()->SetPirSensitivity(ask, 9); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_ContorlInfraredLight +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_ContorlInfraredLight) +{ + /** + * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the + * abstract interface. + */ + class McuAskTest : public McuAsk, public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY) + { + } // using McuAskBlock::NOT_BLOCK + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBase::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + IMcuManager::GetInstance()->ContorlInfraredLight(ask, ControlLight::TRUN_ON); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_GetPhotosensitivityValue +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_GetPhotosensitivityValue) +{ + /** + * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the + * abstract interface. + */ + class McuAskTest : public McuAsk, public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY) + { + } // using McuAskBlock::NOT_BLOCK + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBase::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + IMcuManager::GetInstance()->GetPhotosensitivityValue(ask); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_GetIpcMission +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_GetIpcMission) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY) + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + IMcuManager::GetInstance()->GetIpcMission(ask); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + IMcuManager::GetInstance()->UnInit(); +} +/** + * @brief This use case demonstrates that only the serial port was opened and written successfully, and no data was + * read. + * Run Test: + * ../output_files/test/bin/McuManagerTest + * --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_GetIpcMission_1 + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_GetIpcMission_1) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY) + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + McuManagerTestTool::MockMcuDeviceOpenSuccessButReadNothing(mLinuxTest); + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + IMcuManager::GetInstance()->GetIpcMission(ask); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_GetIpcMission +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_GetIpcMission2) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY) + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + IMcuManager::GetInstance()->GetIpcMission(ask); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_CutOffPowerSupply +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_CutOffPowerSupply) +{ + class McuAskTest : public McuAskBaseTestTool + { + public: + McuAskTest() + : McuAskBaseTestTool(McuAskBlock::UNRELATED, + McuAskReply::NEED_NOT_REPLY) // using McuAskReply::NEED_NOT_REPLY + { + } + virtual ~McuAskTest() = default; + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->CutOffPowerSupply(ask); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_FeedWatchDog +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_FeedWatchDog) +{ + class McuAskTest : public McuAskBaseTestTool + { + public: + McuAskTest() + : McuAskBaseTestTool(McuAskBlock::UNRELATED, + McuAskReply::NEED_NOT_REPLY) // using McuAskReply::NEED_NOT_REPLY + { + } + virtual ~McuAskTest() = default; + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->FeedWatchDog(ask); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_FeedWatchDogThread +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_FeedWatchDogThread) +{ + IMcuManager::GetInstance()->Init(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, + McuAskReply::NEED_REPLY) // using McuAskBlock::BLOCK, + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->SetFeedingCycleForWatchDog(ask, 1, 1, 1); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog2 +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog2) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() + : McuAskBaseTestTool(McuAskBlock::NOT_BLOCK, + McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->SetFeedingCycleForWatchDog(ask, 1, 1, 1); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrite +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrite) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, + McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask ASK_RESULT succeed, mDataReply = %d.\n", static_cast(mDataReply)); + } + else { + LogError("Ask data falied.\n"); + } + } + }; + class McuAskTest2 : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest2() : McuAskBaseTestTool(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY) + { + } // using McuAskBlock::NOT_BLOCK + virtual ~McuAskTest2() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask IpcMission succeed, mDataReply = %d.\n", static_cast(mDataReply)); + } + else { + LogError("Ask data falied.\n"); + } + } + }; + auto threadTest1 = [](McuManagerMockTest *test) { + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->SetFeedingCycleForWatchDog(ask, 1, 1, 1); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + EXPECT_EQ(test->CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + }; + auto threadTest2 = [](McuManagerMockTest *test) { + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + IMcuManager::GetInstance()->GetIpcMission(ask); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + EXPECT_EQ(test->CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + }; + IMcuManager::GetInstance()->Init(); + std::thread thread_1 = std::thread(threadTest1, this); + std::thread thread_2 = std::thread(threadTest2, this); + if (thread_1.joinable()) { + thread_1.join(); + } + if (thread_2.joinable()) { + thread_2.join(); + } + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetDataTime +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetDataTime) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, + McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + McuAskDateTime value(2014, 1, 15, 0, 0, 0); + StatusCode code = IMcuManager::GetInstance()->SetDateTime(ask, value); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetDataTime2 +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetDataTime2) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() + : McuAskBaseTestTool(McuAskBlock::NOT_BLOCK, + McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + McuAskDateTime value(2014, 1, 15, 0, 0, 0); + StatusCode code = IMcuManager::GetInstance()->SetDateTime(ask, value); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetPirSensitivity +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetPirSensitivity) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, + McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->SetPirSensitivity(ask, 1); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetPirSensitivity2 +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetPirSensitivity2) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() + : McuAskBaseTestTool(McuAskBlock::NOT_BLOCK, + McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDefaultFeatures(testTool); + StatusCode code = IMcuManager::GetInstance()->SetPirSensitivity(ask, 2); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_GetIpcMissionFailed +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_GetIpcMissionFailed) +{ + class McuAskTest : public McuAsk, public McuAskBaseTestTool + { + public: + McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY) + { + } + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBaseTestTool::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + } + else { + LogError("Ask data falied.\n"); + } + } + }; + McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + std::shared_ptr testTool = std::dynamic_pointer_cast(ask); + testTool->McuAskDoNothing(testTool); + testTool->NoNeedToBlocking(testTool); + IMcuManager::GetInstance()->GetIpcMission(ask); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission) +{ + constexpr unsigned int TEST_SERIAL_NUMBER = 99; + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission) override + { + LogInfo("RecvIpcMissionEvent\n"); + std::shared_ptr> recvData = std::dynamic_pointer_cast>(recv); + recv->ReplyFinished(true); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideAskIpcMission(mLinuxTest, TEST_SERIAL_NUMBER); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +/** + * @brief Construct a new test f object + * This test case simulates the state machine receiving serial port data before it is fully started. At this point, the + * serial port data should be cached until the state machine starts and registers the serial port monitor, and reports + * the serial port data when registering the serial port monitor. + * Run: ../output_files/test/bin/McuManagerTest + * --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission2 + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission2) +{ + constexpr unsigned int TEST_SERIAL_NUMBER = 99; + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission) override + { + LogInfo("RecvIpcMissionEvent\n"); + std::shared_ptr> recvData = std::dynamic_pointer_cast>(recv); + recv->ReplyFinished(true); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideAskIpcMission(mLinuxTest, TEST_SERIAL_NUMBER); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetIntervalStart +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetIntervalStart) +{ + constexpr unsigned int TEST_SERIAL_NUMBER = 99; + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvGetIntervalStartEvent(std::shared_ptr &recv) override + { + LogInfo("RecvGetIntervalStartEvent\n"); + std::shared_ptr> recvData = + std::dynamic_pointer_cast>(recv); + EXPECT_NE(recvData, nullptr) << "recvData is not McuGetIntervalStart."; + if (recvData) { + recvData->mDataRecvReply.mHour = 10; + recvData->mDataRecvReply.mMin = 10; + recvData->mDataRecvReply.mSecond = 10; + recv->ReplyFinished(true); + return; + } + recv->ReplyFinished(false); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideGetIntervalStart(mLinuxTest, TEST_SERIAL_NUMBER); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +/** + * @brief Construct a new test f object + * This test case simulates the state machine receiving serial port data before it is fully started. At this point, the + * serial port data should be cached until the state machine starts and registers the serial port monitor, and reports + * the serial port data when registering the serial port monitor. + * Run: ../output_files/test/bin/McuManagerTest + * --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetIntervalStart2 + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetIntervalStart2) +{ + constexpr unsigned int TEST_SERIAL_NUMBER = 99; + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvGetIntervalStartEvent(std::shared_ptr &recv) override + { + LogInfo("RecvGetIntervalStartEvent\n"); + std::shared_ptr> recvData = + std::dynamic_pointer_cast>(recv); + EXPECT_NE(recvData, nullptr) << "recvData is not McuGetIntervalStart."; + if (recvData) { + recvData->mDataRecvReply.mHour = 10; + recvData->mDataRecvReply.mMin = 10; + recvData->mDataRecvReply.mSecond = 10; + recv->ReplyFinished(true); + return; + } + recv->ReplyFinished(false); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideGetIntervalStart(mLinuxTest, TEST_SERIAL_NUMBER); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetDateTime +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetDateTime) +{ + constexpr unsigned int TEST_SERIAL_NUMBER = 99; + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvGetDateTime(std::shared_ptr &recv) override + { + LogInfo("RecvGetDateTime\n"); + std::shared_ptr> recvData = + std::dynamic_pointer_cast>(recv); + EXPECT_NE(recvData, nullptr) << "recvData is not McuReplyDateTime."; + if (recvData) { + recvData->mDataRecvReply.mYear = 2024; + recvData->mDataRecvReply.mMon = 10; + recvData->mDataRecvReply.mDay = 10; + recvData->mDataRecvReply.mHour = 10; + recvData->mDataRecvReply.mMin = 10; + recvData->mDataRecvReply.mSecond = 10; + recv->ReplyFinished(true); + return; + } + recv->ReplyFinished(false); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideGetDateTime(mLinuxTest, TEST_SERIAL_NUMBER); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +/** + * @brief Construct a new test f object + * This test case simulates the state machine receiving serial port data before it is fully started. At this point, the + * serial port data should be cached until the state machine starts and registers the serial port monitor, and reports + * the serial port data when registering the serial port monitor. + * Run: ../output_files/test/bin/McuManagerTest + * --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetDateTime2 + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetDateTime2) +{ + constexpr unsigned int TEST_SERIAL_NUMBER = 99; + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvGetDateTime(std::shared_ptr &recv) override + { + LogInfo("RecvGetDateTime\n"); + std::shared_ptr> recvData = + std::dynamic_pointer_cast>(recv); + EXPECT_NE(recvData, nullptr) << "recvData is not McuReplyDateTime."; + if (recvData) { + recvData->mDataRecvReply.mYear = 2024; + recvData->mDataRecvReply.mMon = 10; + recvData->mDataRecvReply.mDay = 10; + recvData->mDataRecvReply.mHour = 10; + recvData->mDataRecvReply.mMin = 10; + recvData->mDataRecvReply.mSecond = 10; + recv->ReplyFinished(true); + return; + } + recv->ReplyFinished(false); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideGetDateTime(mLinuxTest, TEST_SERIAL_NUMBER); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensitivity +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensitivity) +{ + constexpr unsigned int TEST_SERIAL_NUMBER = 99; + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvGetPirSensitivity(std::shared_ptr &recv) override + { + LogInfo("RecvGetPirSensitivity\n"); + std::shared_ptr> recvData = + std::dynamic_pointer_cast>(recv); + EXPECT_NE(recvData, nullptr) << "recvData is not McuGetPirSensitivity."; + if (recvData) { + recvData->mDataRecvReply.mSensitivity = 9; + recv->ReplyFinished(true); + return; + } + recv->ReplyFinished(false); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideGetPriSensitivity(mLinuxTest, TEST_SERIAL_NUMBER); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +/** + * @brief Construct a new test f object + * This test case simulates the state machine receiving serial port data before it is fully started. At this point, the + * serial port data should be cached until the state machine starts and registers the serial port monitor, and reports + * the serial port data when registering the serial port monitor. + * Run: ../output_files/test/bin/McuManagerTest + * --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensitivity2 + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensitivity2) +{ + constexpr unsigned int TEST_SERIAL_NUMBER = 99; + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvGetPirSensitivity(std::shared_ptr &recv) override + { + LogInfo("RecvGetPirSensitivity\n"); + std::shared_ptr> recvData = + std::dynamic_pointer_cast>(recv); + EXPECT_NE(recvData, nullptr) << "recvData is not McuGetPirSensitivity."; + if (recvData) { + recvData->mDataRecvReply.mSensitivity = 9; + recv->ReplyFinished(true); + return; + } + recv->ReplyFinished(false); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideGetPriSensitivity(mLinuxTest, TEST_SERIAL_NUMBER); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +} // namespace McuManager_Mock_Test \ No newline at end of file diff --git a/test/middleware/McuManager/tool/include/McuManagerTestTool.h b/test/middleware/McuManager/tool/include/McuManagerTestTool.h new file mode 100644 index 0000000..d1edb25 --- /dev/null +++ b/test/middleware/McuManager/tool/include/McuManagerTestTool.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MCU_MANAGER_TEST_TOOL_H +#define MCU_MANAGER_TEST_TOOL_H +#include "GtestUsing.h" +#include "LinuxApiMock.h" +#include "McuManagerImpl.h" +#include "McuProtocolTestTool.h" +class McuManagerImplTest : public McuManagerImpl +{ +public: + McuManagerImplTest() = default; + virtual ~McuManagerImplTest() = default; + MOCK_METHOD1(DeleteMcuAsk, void(std::shared_ptr &)); + bool CheckAskExist(const std::shared_ptr &ask); +}; +class McuManagerTestTool : virtual public McuProtocolTestTool +{ +public: + McuManagerTestTool() = default; + virtual ~McuManagerTestTool() = default; + bool CheckAskExist(const std::shared_ptr &ask); + +protected: + void Init(std::shared_ptr &mock); + void UnInit(void); + void MockOtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskHeartBeat(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideGetIntervalStart(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideGetDateTime(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideGetPriSensitivity(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideSendData(std::shared_ptr &mock, const void *data, const size_t &size); + void MockMcuDeviceOpenFailed(std::shared_ptr &mock); + void MockMcuDeviceOpenSuccessButReadNothing(std::shared_ptr &mock); + void MockOtherSideIpcMissionReply(const IpcMission &replyIpcMission); + +private: + std::shared_ptr mMcuManagerMock; +}; +#endif \ No newline at end of file diff --git a/test/middleware/MediaManager/CMakeLists.txt b/test/middleware/MediaManager/CMakeLists.txt new file mode 100644 index 0000000..75ce039 --- /dev/null +++ b/test/middleware/MediaManager/CMakeLists.txt @@ -0,0 +1,86 @@ +# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake) +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +include(${MIDDLEWARE_SOURCE_PATH}/AppManager/build/app_manager.cmake) +set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin) + +include_directories( + ./src + ./include + ./tool/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/StatusCode/include + # ${UTILS_SOURCE_PATH}/KeyControl/include + ${UTILS_SOURCE_PATH}/LedControl/include + # ${UTILS_SOURCE_PATH}/WebServer/include + ${HAL_SOURCE_PATH}/include + ${MIDDLEWARE_SOURCE_PATH}/MediaManager/include + ${TEST_SOURCE_PATH} + ${TEST_SOURCE_PATH}/hal/tool/include + ${TEST_SOURCE_PATH}/utils/LinuxApiMock/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} + ${TEST_TOOLS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES_MAIN) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +set(TARGET_NAME MediaManagerTest) +add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} MediaManager MediaManagerTestTool HalTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} Hal LedControl LinuxApiMock) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + MediaManagerTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make MediaManagerTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + MediaManagerTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make MediaManagerTest_code_check + COMMAND make MediaManagerTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/middleware/MediaManager/mainTest.cpp b/test/middleware/MediaManager/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/middleware/MediaManager/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/middleware/MediaManager/src/MediaManager_Test.cpp b/test/middleware/MediaManager/src/MediaManager_Test.cpp new file mode 100644 index 0000000..720b9ec --- /dev/null +++ b/test/middleware/MediaManager/src/MediaManager_Test.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "GtestUsing.h" +#include "HalTestTool.h" +#include "ILog.h" +#include "IMediaManager.h" +#include "MediaManagerTestTool.h" +#include +#include +#include +namespace MediaManagerTest +{ +class MediaManagerTest : public testing::Test, public MediaManagerTestTool, public HalTestTool +{ +public: + MediaManagerTest() + { + } + virtual ~MediaManagerTest() + { + } + static void SetUpTestCase() + { + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + } + static void TearDownTestCase() + { + ILogUnInit(); + } + virtual void SetUp() + { + CreateAllCamerasMcok(); + HalTestTool::Init(); + MediaManagerTestTool::Init(); + CreateHalCppModule(); + CreateMediaManagerModule(); + } + virtual void TearDown() + { + MediaManagerTestTool::UnInit(); + HalTestTool::UnInit(); + DestroyMediaManagerModule(); + DestroyHalCppModule(); + DestroyAllCamerasMock(); + } + +private: + void CreateAllCamerasMcok(void) + { + std::shared_ptr camera = HalTestTool::MakeCameraHalTest(CameraType::MAIN_CAMERA); + mAllCamerasMock[CameraType::MAIN_CAMERA] = camera; + } + void DestroyAllCamerasMock(void) + { + mAllCamerasMock.clear(); + } + +protected: + // const AppParam mAppParam; + std::map> mAllCamerasMock; +}; +// ../output_files/test/bin/MediaManagerTest --gtest_filter=MediaManagerTest.INTEGRATION_MediaManager_EXAMPLE_Demo0 +TEST_F(MediaManagerTest, INTEGRATION_MediaManager_EXAMPLE_Demo0) +{ + SetAllCamerasResult(mAllCamerasMock); + std::shared_ptr monitor = MediaManagerTestTool::MakeMonitorMock(); + IMediaManager::GetInstance()->Init(); + IMediaManager::GetInstance()->SetMediaMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MockReportCameraEvent("test name", CameraType::MAIN_CAMERA); + // MockAppClientConnect(); + // std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // MockSetRecordingStatus(RecordingStatus::RECORDING_START); + // std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + // IAppManager::GetInstance()->UnInit(); + IMediaManager::GetInstance()->UnInit(); +} +} // namespace MediaManagerTest \ No newline at end of file diff --git a/test/middleware/MediaManager/tool/MediaManagerTestTool.h b/test/middleware/MediaManager/tool/MediaManagerTestTool.h new file mode 100644 index 0000000..e69de29 diff --git a/test/middleware/MediaManager/tool/include/MediaManagerTestTool.h b/test/middleware/MediaManager/tool/include/MediaManagerTestTool.h new file mode 100644 index 0000000..94723df --- /dev/null +++ b/test/middleware/MediaManager/tool/include/MediaManagerTestTool.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MEDIA_MANAGER_TEST_TOOL_H +#define MEDIA_MANAGER_TEST_TOOL_H +#include "GtestUsing.h" +#include "IMediaManager.h" +#include +class MediaManagerTestTool +{ +public: + MediaManagerTestTool() = default; + virtual ~MediaManagerTestTool() = default; + void Init(void); + void UnInit(void); + +private: + void MediaManagerMockInit(std::shared_ptr &vMock); + void MediaMonitorInit(std::shared_ptr &vMock); + +private: + std::shared_ptr mMediaManagerMock; + std::shared_ptr mMediaMonitorMock; + +public: + static std::shared_ptr MakeMonitorMock(void); +}; +#endif \ No newline at end of file diff --git a/test/test_develop.md b/test/test_develop.md new file mode 100644 index 0000000..43bb77d --- /dev/null +++ b/test/test_develop.md @@ -0,0 +1,87 @@ +# 1. 测试用例开发规范 + +  便于对测试用例进行管理,指定测试用例开发规范。 + +## 1.1. 命名规则 + +  制定命名规则,是为了规范测试用例行为,为自动化测试提供管理遍历,因为googletest支持根据测试用例名字通配符检索运行目标测试用例。 + +### 1.1.1. 基本概念 + +**硬件仿真**:对接口属性/数据进行模拟/检测的能力,常见于芯片板级依赖的接口,例如:串口,GPIO等,还有芯片SDK依赖接口,例如:音视频编解码接口; + +**单元测试**:单元测试指对单一功能接口的测试,一般不需要或少量使用gtest的mock功能即可完成测试,不需要实际链接外部接口,对模块内部代码功能进行有效验证; + +**用例所属模块**:测试时锁定目标测试用例的作用; + +**集成测试**:集成测试指对复杂业务进行测试,往往需要跨多层级多模块,需要大量使用gtest的mock功能才能完成的复杂逻辑测试,一般不针对代码本身而是针对产品定义的功能进行有效验证; + +**测试用例**: + +1. EXAMPLE:对模块接口进行使用演示的测试用例; +2. AUTO:自动化运行的测试用例; +3. STRESS:压力测试用例,一般不在单次测试中执行; + +### 1.1.2. 测试用例命名: + +1. 仿真:硬件仿真,使用hardware simulation的缩写:**HS**,硬件仿真只能运行在Ubuntu系统进行仿真测试;真实硬件接口使用real hardware的缩写:**RH**,真实硬件接口测试用例只能运行在开发板进行真机测试; +2. 测试用例类型:含单元测试(UNIT)和集成测试(INTEGRATION); +3. 用例所属模块:大小驼峰; +4. 测试用例属性:EXAMPLE/AUTO/STRESS +5. 测试用例小名; + +示例: + +该测试用例标识属于SharedData模块的单元测试,作为example具有演示接口使用规范的作用,测试用例名为Deme7。 + +``` +TEST(SharedDataTest, UNIT_SharedData_EXAMPLE_Demo7) +{ + // TODO: +} +``` +### 1.1.3. 测试源码文件命名: +1. 每个测试可执行文件都有一个标准mian函数,文件名统一为:mainTest.cpp; +``` +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); // 此处执行所有的测试用例; +} +``` +2. 针对源码文件,对应的测试代码文件名为:源码文件名_Test.cpp; +``` +例如:cjson.c 对应的测试源码文件为cjson_Test.cpp +``` +3. 适配层源码文件的TEST文件命名区分Simulator版本和常规版本,Simulator版本在ubuntu模拟仿真环境测试(执行代码会调用芯片SDK接口),常规版本必须运行在开发板: +``` +├── CMakeLists.txt +├── mainTest.cpp +├── src +│   └── BoardMain_Test.cpp // 常规测试代码,可运行在开发板系统; +└── src_mock + └── BoardMain_Simulator_Test.cpp // 运行过程会调用芯片依赖的API,需要对这些API进行mock处理,实现Linux x86平台的仿真测试; +``` +4. + +## 1.2. TestTool 测试工具 + +   每个独立模块(SDK内具有独立CMakeLists.txt的代码模块)都有对应的TestTool模块,主要用于对该模块的接口进行gmock处理,且对外暴露接口具备复用属性。一般类命名:模块名称+TestTool。 + +## 1.3. 目录结构 + +  所有测试代码位于< test >目录下,< test >下的目录结构保持和< SDK >一致,表示对应sdk代码模块的测试代码; + +如果是芯片平台的test目录,需要区分板载测试代码和x86 Linux系统的测试代码;如下: +``` +└── hal + ├── CMakeLists.txt + ├── mainTest.cpp + ├── src // 芯片平台的测试代码; + ├── src_mock // Linux x86测试代码 需要对板载接口打桩进行测试; + └── tool // TestTool:该模块需要对外复用的测试代码; +``` \ No newline at end of file diff --git a/test/utils/CMakeLists.txt b/test/utils/CMakeLists.txt new file mode 100644 index 0000000..251acc6 --- /dev/null +++ b/test/utils/CMakeLists.txt @@ -0,0 +1,11 @@ +#Compile gtest for test code. +add_subdirectory(Config) +add_subdirectory(Log) +add_subdirectory(SharedData) +add_subdirectory(WebServer) +add_subdirectory(UartDevice) +add_subdirectory(LinuxApiMock) +add_subdirectory(McuProtocol) +add_subdirectory(FxHttpServer) +add_subdirectory(TestManager) +add_subdirectory(TcpModule) \ No newline at end of file diff --git a/test/utils/Config/CMakeLists.txt b/test/utils/Config/CMakeLists.txt new file mode 100644 index 0000000..643cf25 --- /dev/null +++ b/test/utils/Config/CMakeLists.txt @@ -0,0 +1,49 @@ +# 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 + ${UTILS_SOURCE_PATH}/Config/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES) +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME ConfigTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} ConfigBase gtest gmock pthread) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + ConfigTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/Config +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make ConfigTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() \ No newline at end of file diff --git a/test/utils/Config/mainTest.cpp b/test/utils/Config/mainTest.cpp new file mode 100644 index 0000000..fb1d91b --- /dev/null +++ b/test/utils/Config/mainTest.cpp @@ -0,0 +1,9 @@ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/utils/Config/src/Config_Test.cpp b/test/utils/Config/src/Config_Test.cpp new file mode 100644 index 0000000..90c537a --- /dev/null +++ b/test/utils/Config/src/Config_Test.cpp @@ -0,0 +1,18 @@ +#include "ILog.h" +#include "Config.h" +// #include +#include +namespace ConfigTest +{ + // ../output_files/test/bin/ConfigTest --gtest_filter=ConfigTest.Demo + TEST(ConfigTest, Demo) + { + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + VConfig *config = OpenConfigFile("./config"); + int value = 0; + ConfigGetInt(config, "number", &value); + ILogUnInit(); + DestroyLogModule(); + } +} // namespace ConfigTest \ No newline at end of file diff --git a/test/utils/FxHttpServer/CMakeLists.txt b/test/utils/FxHttpServer/CMakeLists.txt new file mode 100644 index 0000000..906b109 --- /dev/null +++ b/test/utils/FxHttpServer/CMakeLists.txt @@ -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( + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/FxHttpServer/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES) +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME FxHttpServerTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} FxHttpServer gtest gmock pthread Log) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + FxHttpServerTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/FxHttpServer +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + FxHttpServerTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/FxHttpServer +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make FxHttpServerTest_code_check + COMMAND make FxHttpServerTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) \ No newline at end of file diff --git a/test/utils/FxHttpServer/mainTest.cpp b/test/utils/FxHttpServer/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/utils/FxHttpServer/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/utils/FxHttpServer/src/FxHttpServer_Test.cpp b/test/utils/FxHttpServer/src/FxHttpServer_Test.cpp new file mode 100644 index 0000000..78fda04 --- /dev/null +++ b/test/utils/FxHttpServer/src/FxHttpServer_Test.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "FxHttpServer.h" +#include "ILog.h" +#include +#include +#include +namespace FxHttpServerTest +{ +static const char *gResponse = " {\ +\"result\":0,\ +\"info\":{\ +\"status\":0,\ +\"free\":12000,\ +\"total\":\"64000\"\ +}\ +}"; +void HttpHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context) +{ + if (url) { + LogInfo("url = %s\n", url); + if (memcmp(url, "/set", strlen("/set")) == 0) { + responseHandle(gResponse, context); + } + } +} +// ../output_files/test/bin/FxHttpServerTest --gtest_filter=FxHttpServerTest.INTEGRATION_AppManager_EXAMPLE_Demo +TEST(FxHttpServerTest, INTEGRATION_AppManager_EXAMPLE_Demo) +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + FxHttpServerInit(HttpHandle, 8080); + std::this_thread::sleep_for(std::chrono::milliseconds(1000 * 10)); + FxHttpServerUnInit(); + ILogUnInit(); +} +} // namespace FxHttpServerTest \ No newline at end of file diff --git a/test/utils/LinuxApiMock/CMakeLists.txt b/test/utils/LinuxApiMock/CMakeLists.txt new file mode 100644 index 0000000..f921bac --- /dev/null +++ b/test/utils/LinuxApiMock/CMakeLists.txt @@ -0,0 +1,52 @@ +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ./src + ./include + ${UTILS_SOURCE_PATH}/Log/include + ${TEST_SOURCE_PATH} + ${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}/libconfig/libconfig-1.7.3/lib/.libs +# ) + +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME LinuxApiMock) +add_library(${TARGET_NAME} STATIC ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} LinuxApi gtest gmock Log) + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + LinuxApiMock_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${SRC_FILES} + ${CLANG_TIDY_CONFIG} + -p ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/LinuxApiMock +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + LinuxApiMock_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/LinuxApiMock +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make LinuxApiMock_code_check + COMMAND make LinuxApiMock_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) \ No newline at end of file diff --git a/test/utils/LinuxApiMock/README.md b/test/utils/LinuxApiMock/README.md new file mode 100644 index 0000000..8ab3986 --- /dev/null +++ b/test/utils/LinuxApiMock/README.md @@ -0,0 +1,3 @@ +# 1. Linux 系统函数模拟接口 + +   为了方便在ubuntu系统进行代码的调试运行,对Linux标准函数进行多态转换。 \ No newline at end of file diff --git a/test/utils/LinuxApiMock/include/LinuxApiMock.h b/test/utils/LinuxApiMock/include/LinuxApiMock.h new file mode 100644 index 0000000..f1d6eae --- /dev/null +++ b/test/utils/LinuxApiMock/include/LinuxApiMock.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef LINUX_API_MOCK_H +#define LINUX_API_MOCK_H +#include "GtestUsing.h" +#include +constexpr int INVALID_HANDLE = -1; +constexpr int MOCK_SELECT_TIME_OUT = 0; +class LinuxApiMock +{ +public: + LinuxApiMock() = default; + virtual ~LinuxApiMock() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual void Init(); + virtual void UnInit(); + virtual int fx_open(const char *pathname, int flags); + virtual int fx_tcgetattr(int fd, struct termios *termios_p); + virtual int fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p); + virtual ssize_t fx_write(int fd, const void *buf, size_t count); + virtual ssize_t fx_read(int fd, void *buf, size_t count); + virtual int fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); + virtual int fx_fstat(int fd, struct stat *statbuf); + virtual int fx_access(const char *pathname, int mode); + virtual FILE *fx_fopen(const char *pathname, const char *mode); +}; +/** + * A simulation interface class used for automated testing in Ubuntu systems, implementing the function of piling on + * some Linux standard interfaces. + */ +class LinuxTest : public LinuxApiMock, public std::enable_shared_from_this +{ +public: + LinuxTest() = default; + virtual ~LinuxTest() = default; + MOCK_METHOD2(fx_open, int(const char *, int)); + MOCK_METHOD2(fx_tcgetattr, int(int, struct termios *)); + MOCK_METHOD3(fx_tcsetattr, int(int, int, const struct termios *)); + MOCK_METHOD3(fx_write, ssize_t(int, const void *, size_t)); + MOCK_METHOD3(fx_read, ssize_t(int, void *, size_t)); + MOCK_METHOD5(fx_select, int(int, fd_set *, fd_set *, fd_set *, struct timeval *)); + MOCK_METHOD2(fx_fstat, int(int, struct stat *)); + MOCK_METHOD2(fx_access, int(const char *, int)); + MOCK_METHOD2(fx_fopen, FILE *(const char *, const char *)); + +public: + /** + * @brief Get the Handle For Mock object + * Obtain a globally unique test handle throughout the entire testing cycle, and the handle generated through this + * interface can avoid handle conflicts between different testing modules. Attention needs to be paid to whether it + * conflicts with non test handles generated by the system. + * @return int + */ + virtual int GetHandleForMock(void); + +public: + static std::shared_ptr CreateLinuxTest(void); +}; +#endif \ No newline at end of file diff --git a/test/utils/LinuxApiMock/src/HandleManager.cpp b/test/utils/LinuxApiMock/src/HandleManager.cpp new file mode 100644 index 0000000..39d08bd --- /dev/null +++ b/test/utils/LinuxApiMock/src/HandleManager.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "HandleManager.h" +HandleManager::HandleManager() +{ + mFdMax = 1000; +} +int HandleManager::GetHandleForMock(void) +{ + int value = mFdMax; + mFdMax++; + return value; +} \ No newline at end of file diff --git a/test/utils/LinuxApiMock/src/HandleManager.h b/test/utils/LinuxApiMock/src/HandleManager.h new file mode 100644 index 0000000..6697cbe --- /dev/null +++ b/test/utils/LinuxApiMock/src/HandleManager.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HANDLE_MANAGER_H +#define HANDLE_MANAGER_H +#include "LinuxApiMock.h" +/** + * @brief Handle management class, which manages all handles in the test program to prevent unclear objects pointed to + * by handles during simulation. + * + */ +class HandleManager : virtual public LinuxTest +{ +public: + HandleManager(); + virtual ~HandleManager() = default; + int GetHandleForMock(void) override; + +private: + /** + * @brief Ensure that false handles of the test simulation interface are not duplicated. + * + */ + int mFdMax; +}; +#endif \ No newline at end of file diff --git a/test/utils/LinuxApiMock/src/LinuxApiMock.cpp b/test/utils/LinuxApiMock/src/LinuxApiMock.cpp new file mode 100644 index 0000000..3af63f0 --- /dev/null +++ b/test/utils/LinuxApiMock/src/LinuxApiMock.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "LinuxApiMock.h" +#include "ILog.h" +#include "LinuxTestImpl.h" +#include "WrapApi.h" +std::shared_ptr &LinuxApiMock::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) { + // The non-thread-safe changing is only for gtest. + instance = *impl; + } + return instance; +} +void LinuxApiMock::Init() +{ +} +void LinuxApiMock::UnInit() +{ +} +int LinuxApiMock::fx_open(const char *pathname, int flags) +{ + // LogInfo("Call the real api.\n"); + return __real_fx_open(pathname, flags); +} +int LinuxApiMock::fx_tcgetattr(int fd, struct termios *termios_p) +{ + // LogInfo("Call the real api.\n"); + return __real_fx_tcgetattr(fd, termios_p); +} +int LinuxApiMock::fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p) +{ + return __real_fx_tcsetattr(fd, optional_actions, termios_p); +} +ssize_t LinuxApiMock::fx_write(int fd, const void *buf, size_t count) +{ + // LogInfo("Call the real api.\n"); + return __real_fx_write(fd, buf, count); +} +ssize_t LinuxApiMock::fx_read(int fd, void *buf, size_t count) +{ + // LogInfo("Call the real api.\n"); + return __real_fx_read(fd, buf, count); +} +int LinuxApiMock::fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) +{ + // LogInfo("Call the real api.\n"); + return __real_fx_select(nfds, readfds, writefds, exceptfds, timeout); +} +int LinuxApiMock::fx_fstat(int fd, struct stat *statbuf) +{ + return __real_fx_fstat(fd, statbuf); +} +int LinuxApiMock::fx_access(const char *pathname, int mode) +{ + return __real_fx_access(pathname, mode); +} +FILE *LinuxApiMock::fx_fopen(const char *pathname, const char *mode) +{ + return __real_fx_fopen(pathname, mode); +} +std::shared_ptr LinuxTest::CreateLinuxTest(void) +{ + std::shared_ptr test = std::make_shared(); + LinuxTestImpl::ApiInit(test); + return test; +} +int LinuxTest::GetHandleForMock(void) +{ + return INVALID_HANDLE; +} \ No newline at end of file diff --git a/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp b/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp new file mode 100644 index 0000000..5fc68e4 --- /dev/null +++ b/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "LinuxTestImpl.h" +#include "ILog.h" +#include +/** + * @brief The simulated interface has been subjected to lock serial processing to ensure that the return value can be + * safely returned through multiple threads. However, this may affect the timing of the test code, which is not very + * reasonable. If the behavior of the simulated interface is redefined, the impact can be ignored. + * @param mock + */ +void LinuxTestImpl::ApiInit(std::shared_ptr &mock) +{ + LogInfo("ApiInit\n"); + static int openFd = -1; + auto api_open = [=](const char *pathname, int flags) { + LogInfo("Call __real_fx_open, pathname = %s.\n", pathname); + openFd = __real_fx_open(pathname, flags); + LogInfo("openFd = %d\n", openFd); + }; + EXPECT_CALL(*mock.get(), fx_open(_, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1>(Invoke(api_open)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&openFd))); + static int resultTcgetattr = -1; + auto api_tcgetattr = [=](int fd, struct termios *termios_p) { + resultTcgetattr = __real_fx_tcgetattr(fd, termios_p); + LogInfo("resultTcgetattr = %d\n", resultTcgetattr); + }; + EXPECT_CALL(*mock.get(), fx_tcgetattr(_, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1>(Invoke(api_tcgetattr)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&resultTcgetattr))); + static int resultTcsetattr = -1; + auto api_tcsetattr = [=](int fd, int optional_actions, const struct termios *termios_p) { + resultTcsetattr = __real_fx_tcsetattr(fd, optional_actions, termios_p); + LogInfo("resultTcsetattr = %d\n", resultTcsetattr); + }; + EXPECT_CALL(*mock.get(), fx_tcsetattr(_, _, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1, 2>(Invoke(api_tcsetattr)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&resultTcsetattr))); + static int writeLength = -1; + auto api_write = [=](int fd, const void *buf, size_t count) { + writeLength = __real_fx_write(fd, buf, count); + LogInfo("writeLength = %d\n", writeLength); + }; + EXPECT_CALL(*mock.get(), fx_write(_, _, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1, 2>(Invoke(api_write)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&writeLength))); + static int readLength = -1; + auto api_read = [=](int fd, void *buf, size_t count) { + readLength = __real_fx_read(fd, buf, count); + LogInfo("readLength = %d\n", readLength); + }; + EXPECT_CALL(*mock.get(), fx_read(_, _, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1, 2>(Invoke(api_read)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&readLength))); + static int selectResult = -1; + auto api_select = [=](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { + selectResult = __real_fx_select(nfds, readfds, writefds, exceptfds, timeout); + LogInfo("selectResult = %d\n", selectResult); + }; + EXPECT_CALL(*mock.get(), fx_select(_, _, _, _, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1, 2, 3, 4>(Invoke(api_select)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&selectResult))); + static int fstatResult = -1; + auto api_fstat = [=](int fd, struct stat *statbuf) { + LogInfo("Call __real_fx_fstat, fd = %d.\n", fd); + fstatResult = __real_fx_fstat(fd, statbuf); + LogInfo("fstatResult = %d\n", fstatResult); + }; + EXPECT_CALL(*mock.get(), fx_fstat(_, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1>(Invoke(api_fstat)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&fstatResult))); + static int accessResult = -1; + auto api_access = [=](const char *pathname, int mode) { + accessResult = __real_fx_access(pathname, mode); + LogInfo("accessResult = %d\n", accessResult); + }; + EXPECT_CALL(*mock.get(), fx_access(_, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1>(Invoke(api_access)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&accessResult))); + static FILE *fopenResult = nullptr; + auto api_fopen = [=](const char *pathname, const char *mode) { + fopenResult = __real_fx_fopen(pathname, mode); + }; + EXPECT_CALL(*mock.get(), fx_fopen(_, _)) + .WillRepeatedly( + DoAll(Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiLock), + WithArgs<0, 1>(Invoke(api_fopen)), + Invoke((std::dynamic_pointer_cast(mock)).get(), &LinuxTestImpl::ApiUnlockThread), + ReturnPointee(&fopenResult))); +} +void LinuxTestImpl::Init() +{ +} +void LinuxTestImpl::UnInit() +{ + if (mApiThread.joinable()) { + mApiThread.join(); + } +} +void LinuxTestImpl::ApiLock(void) +{ + /** + * @brief This has been optimized and does not require locking; Place the lock position in the WrapApi file, and + * lock the Mock function to ensure that it returns the value of the global variable before it can return, to avoid + * the problem of obtaining incorrect return values due to multithreading timing errors. + */ + // mApiMutex.lock(); + // LogInfo("lock api.\n"); +} +void LinuxTestImpl::ApiUnlock(void) +{ + // mApiMutex.unlock(); + // LogInfo("unlock api.\n"); +} +void LinuxTestImpl::ApiUnlockThread(void) +{ + // LogInfo("ApiUnlockThread\n"); + // if (mApiThread.joinable()) { + // mApiThread.join(); + // } + // auto api_unlock = [](std::shared_ptr test) { + // std::this_thread::sleep_for(std::chrono::milliseconds(API_LOCK_TIME_MS)); + // test->ApiUnlock(); + // }; + // std::shared_ptr test = std::dynamic_pointer_cast(LinuxTest::shared_from_this()); + // mApiThread = std::thread(api_unlock, test); +} \ No newline at end of file diff --git a/test/utils/LinuxApiMock/src/LinuxTestImpl.h b/test/utils/LinuxApiMock/src/LinuxTestImpl.h new file mode 100644 index 0000000..1a5623c --- /dev/null +++ b/test/utils/LinuxApiMock/src/LinuxTestImpl.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef LINUX_TEST_IMPL_H +#define LINUX_TEST_IMPL_H +#include "HandleManager.h" +#include "LinuxApiMock.h" +#include "WrapApi.h" +#include +#include +/** + * @brief The simulated interface will be locked by default to ensure that the return value of the interface can be + * correctly obtained by the application in the default state. This is not very reasonable and should be optimized. + * This is the blocking time after locking, during which it is necessary to ensure that the application can obtain a + * global return value. + */ +constexpr int API_LOCK_TIME_MS = 5; +class LinuxTestImpl : public HandleManager +{ +public: + LinuxTestImpl() = default; + virtual ~LinuxTestImpl() = default; + void Init() override; + void UnInit() override; + void ApiLock(void); + void ApiUnlock(void); + +private: + void ApiUnlockThread(void); + +public: + static void ApiInit(std::shared_ptr &mock); + +private: + std::mutex mApiMutex; + std::thread mApiThread; +}; +#endif \ No newline at end of file diff --git a/test/utils/LinuxApiMock/src/WrapApi.cpp b/test/utils/LinuxApiMock/src/WrapApi.cpp new file mode 100644 index 0000000..731ffea --- /dev/null +++ b/test/utils/LinuxApiMock/src/WrapApi.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "WrapApi.h" +#include "LinuxApiMock.h" +#include +#ifdef __cplusplus +extern "C" { +#endif +int __wrap_fx_open(const char *pathname, int flags) +{ + static std::mutex gMutex; + std::lock_guard locker(gMutex); + return LinuxApiMock::GetInstance()->fx_open(pathname, flags); +} +int __wrap_fx_tcgetattr(int fd, struct termios *termios_p) +{ + static std::mutex gMutex; + std::lock_guard locker(gMutex); + return LinuxApiMock::GetInstance()->fx_tcgetattr(fd, termios_p); +} +int __wrap_fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p) +{ + static std::mutex gMutex; + std::lock_guard locker(gMutex); + return LinuxApiMock::GetInstance()->fx_tcsetattr(fd, optional_actions, termios_p); +} +ssize_t __wrap_fx_write(int fd, const void *buf, size_t count) +{ + static std::mutex gMutex; + std::lock_guard locker(gMutex); + return LinuxApiMock::GetInstance()->fx_write(fd, buf, count); +} +ssize_t __wrap_fx_read(int fd, void *buf, size_t count) +{ + static std::mutex gMutex; + std::lock_guard locker(gMutex); + return LinuxApiMock::GetInstance()->fx_read(fd, buf, count); +} +int __wrap_fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) +{ + static std::mutex gMutex; + std::lock_guard locker(gMutex); // TODO: The select function can cause severe blocking here. + return LinuxApiMock::GetInstance()->fx_select(nfds, readfds, writefds, exceptfds, timeout); +} +int __wrap_fx_fstat(int fd, struct stat *statbuf) +{ + static std::mutex gMutex; + std::lock_guard locker(gMutex); + return LinuxApiMock::GetInstance()->fx_fstat(fd, statbuf); +} +int __wrap_fx_access(const char *pathname, int mode) +{ + static std::mutex gMutex; + std::lock_guard locker(gMutex); + return LinuxApiMock::GetInstance()->fx_access(pathname, mode); +} +FILE *__wrap_fx_fopen(const char *pathname, const char *mode) +{ + static std::mutex gMutex; + std::lock_guard locker(gMutex); + return LinuxApiMock::GetInstance()->fx_fopen(pathname, mode); +} +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/test/utils/LinuxApiMock/src/WrapApi.h b/test/utils/LinuxApiMock/src/WrapApi.h new file mode 100644 index 0000000..734729c --- /dev/null +++ b/test/utils/LinuxApiMock/src/WrapApi.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef WRAP_API_H +#define WRAP_API_H +#include +#include +#include +#include +#include +#ifdef __cplusplus +extern "C" { +#endif +// Modify all the real api. +int __real_fx_open(const char *pathname, int flags); +int __real_fx_tcgetattr(int fd, struct termios *termios_p); +int __real_fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p); +ssize_t __real_fx_write(int fd, const void *buf, size_t count); +ssize_t __real_fx_read(int fd, void *buf, size_t count); +int __real_fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); +int __real_fx_fstat(int fd, struct stat *statbuf); +int __real_fx_access(const char *pathname, int mode); +FILE *__real_fx_fopen(const char *pathname, const char *mode); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/test/utils/Log/CMakeLists.txt b/test/utils/Log/CMakeLists.txt new file mode 100644 index 0000000..f15f9da --- /dev/null +++ b/test/utils/Log/CMakeLists.txt @@ -0,0 +1,48 @@ +# 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 + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include +) + +link_directories( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES) +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME LogTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} Log gtest gmock pthread) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + LogTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/Log +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make LogTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() \ No newline at end of file diff --git a/test/utils/Log/mainTest.cpp b/test/utils/Log/mainTest.cpp new file mode 100644 index 0000000..fb1d91b --- /dev/null +++ b/test/utils/Log/mainTest.cpp @@ -0,0 +1,9 @@ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/utils/Log/src/ILogTest.cpp b/test/utils/Log/src/ILogTest.cpp new file mode 100644 index 0000000..4e3bb5b --- /dev/null +++ b/test/utils/Log/src/ILogTest.cpp @@ -0,0 +1,17 @@ +#include "ILog.h" +// #include +#include +namespace ILogTest +{ + // ../output_files/test/bin/LogTest --gtest_filter=ILogTest.Demo + TEST(ILogTest, Demo) + { + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + LogInfo("hello world."); + LogError("create ... failed."); + LogDebug("a = %d b = %s", 124, "apple"); + ILogUnInit(); + DestroyLogModule(); + } +} // namespace ILogTest \ No newline at end of file diff --git a/test/utils/McuProtocol/CMakeLists.txt b/test/utils/McuProtocol/CMakeLists.txt new file mode 100644 index 0000000..5d541c1 --- /dev/null +++ b/test/utils/McuProtocol/CMakeLists.txt @@ -0,0 +1,4 @@ + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h new file mode 100644 index 0000000..6803e75 --- /dev/null +++ b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MCU_PROTOCOL_TEST_TOOL_H +#define MCU_PROTOCOL_TEST_TOOL_H +#include "LinuxApiMock.h" +#include "UartDeviceTestTool.h" +#include +#include +#include +constexpr int READ_PRINT = 0; +constexpr int WRITE_PRINT = 1; +constexpr bool PROTOCOL_HANDLED = true; +constexpr bool PROTOCOL_NOT_HANDLED = false; +using ProtocolHandleFunc = std::function &, const int &, const void *, size_t)>; +class McuProtocolTestTool : virtual public UartDeviceTestTool +{ +public: + McuProtocolTestTool(); + virtual ~McuProtocolTestTool() = default; + void Init(std::shared_ptr &mock, const UartInfo &uart); + void UnInit(void); + void MockOtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskHeartBeat(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskGetIntervalStart(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskGetDateTime(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskGetPirSensitivity(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskSendAnyData(std::shared_ptr &mock, const void *data, const size_t &size); + void ReadNothingAnyTime(std::shared_ptr &mock); + void ReadOnceSelectSucceed(std::shared_ptr &mock, const int &uartFd); + void MockOtherSideIpcMissionReply(const unsigned char &replyIpcMission); + +private: + void InitProtocolBuff(void); + void CheckSerialNumber(const void *buf, const size_t &count); + void ChecCRC16Code(const void *buf, const size_t &count); + void ResetCheckCode(const void *buf, const size_t &count); + /** + * @brief The function of initializing the fx_select function can quickly return when selecting without waiting for + * the timeout to end. + * @param mock + * @param uartFd + */ + void SelectInit(std::shared_ptr &mock, const int &uartFd); + void ReplySelectSucceed(std::shared_ptr &mock, const int &uartFd); + void ReplySelectTimeOut(std::shared_ptr &mock, const int &uartFd); + bool MonitorProtocolPacket(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool IpcMissionProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + void IpcMissionProtocolInit(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool CutOffPowerSupplyProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, + size_t count); + bool FeedWatchDogProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool FeedingCycleProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + void FeedingCycleProtocolInit(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool SetDataTimeProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + void SetDataTimeProtocolInit(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool SetPirSensitivityProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, + size_t count); + void SetPirSensitivityProtocolInit(std::shared_ptr &mock, const int &uartFd, const void *buf, + size_t count); + bool ContorlInfraredLightHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + void ContorlInfraredLightInit(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool GetPhotosensitivityValueHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, + size_t count); + void GetPhotosensitivityValueInit(std::shared_ptr &mock, const int &uartFd, const void *buf, + size_t count); + void LockProtocolHandle(void); + void UnlockProtocolHandle(void); + void UnlockThread(void); + void PipeSelectTimeoutForProtocolHandleImmediately(void); + +private: + void OtherSideAskIpcMissionHandle(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskIpcMissionInit(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskHeartBeatHandle(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskHeartBeatInit(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskGetIntervalStartHandle(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskGetIntervalStartInit(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskGetDateTimeHandle(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskGetDateTimeInit(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskGetPirSensitivityHandle(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskGetPirSensitivityInit(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskSendSomeDataHandle(std::shared_ptr &mock, const int &uartFd, const void *data, + const size_t &size); + void OtherSideAskSendSomeDataInit(std::shared_ptr &mock, const int &uartFd, const void *data, + const size_t &size); + +private: + static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log); + +private: + std::mutex mMutex; + bool mThreadRuning; + std::thread mLockThread; + std::thread mUnLockThread; + std::list mSerialNumberList; + bool mPipeFdMockSelectInit; + int mPipeFdMockSelect[2]; + std::list mProtocolHandle; + std::shared_ptr mUart; + int mUartFd; +}; +#endif \ No newline at end of file diff --git a/test/utils/SharedData/CMakeLists.txt b/test/utils/SharedData/CMakeLists.txt new file mode 100644 index 0000000..2f59cae --- /dev/null +++ b/test/utils/SharedData/CMakeLists.txt @@ -0,0 +1,62 @@ +# 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( + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/SharedData/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} +) + + + + + +aux_source_directory(. SRC_FILES) +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME SharedDataTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} SharedData gtest gmock pthread Log) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + SharedDataTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/SharedData +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + SharedDataTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/SharedData +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make SharedDataTest_code_check + COMMAND make SharedDataTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) \ No newline at end of file diff --git a/test/utils/SharedData/mainTest.cpp b/test/utils/SharedData/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/utils/SharedData/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/utils/SharedData/src/SharedData_Test.cpp b/test/utils/SharedData/src/SharedData_Test.cpp new file mode 100644 index 0000000..0a39f4f --- /dev/null +++ b/test/utils/SharedData/src/SharedData_Test.cpp @@ -0,0 +1,347 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ILog.h" +#include "SharedData.h" +#include +#include +namespace SharedDataTest +{ +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo +TEST(SharedDataTest, Demo) +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataMinor) { + int writableData = 99; + IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); + ISetWritableData(sharedDataMinor, &writableData, sizeof(int)); + } + if (nullptr != sharedDataPrimary) { + int readableData = -1; + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + IGetReadableData(sharedDataPrimary, &readableData, sizeof(int)); + LogInfo("readableData = %d\n", readableData); + IShareDataFree(sharedDataPrimary); + } + if (nullptr != sharedDataMinor) { + IShareDataFree(sharedDataMinor); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo2 +TEST(SharedDataTest, Demo2) +{ + constexpr int WRITABLE_DATA = 189; + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataMinor) { + IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); + ISetWritableData(sharedDataMinor, (void *)&WRITABLE_DATA, sizeof(int)); + } + if (nullptr != sharedDataPrimary) { + int readableData = 0; + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + StatusCode code = IGetReadableData(sharedDataPrimary, &readableData, sizeof(int)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_EQ(readableData, WRITABLE_DATA); + IShareDataFree(sharedDataPrimary); + } + if (nullptr != sharedDataMinor) { + IShareDataFree(sharedDataMinor); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo3 +TEST(SharedDataTest, Demo3) +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataMinor) { + constexpr int WRITABLE_DATA_LENGTH = 9; + char writableData[WRITABLE_DATA_LENGTH] = {0}; + IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(writableData)); + ISetWritableData(sharedDataMinor, writableData, sizeof(writableData)); + } + if (nullptr != sharedDataPrimary) { + constexpr int DEFAULT_DATA = 0; + int readableData = DEFAULT_DATA; + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + StatusCode code = IGetReadableData(sharedDataPrimary, &readableData, sizeof(int)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_NE(code.mStatusCode, STATUS_CODE_OK); + EXPECT_EQ(readableData, DEFAULT_DATA); + IShareDataFree(sharedDataPrimary); + } + if (nullptr != sharedDataMinor) { + IShareDataFree(sharedDataMinor); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo4 +TEST(SharedDataTest, Demo4) +{ + constexpr int WRITABLE_DATA = 189; + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataMinor) { + IMakeSharedData(sharedDataMinor, sizeof(int) * 2, sizeof(int)); + ISetWritableData(sharedDataMinor, (void *)&WRITABLE_DATA, sizeof(int)); + } + if (nullptr != sharedDataPrimary) { + constexpr int DEFAULT_DATA = 0; + int readableData = DEFAULT_DATA; + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + StatusCode code = IGetReadableData(sharedDataPrimary, &readableData, sizeof(int)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); + EXPECT_EQ(readableData, WRITABLE_DATA); + IShareDataFree(sharedDataPrimary); + } + if (nullptr != sharedDataMinor) { + IShareDataFree(sharedDataMinor); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo5 +TEST(SharedDataTest, Demo5) +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataPrimary) { + constexpr int DEFAULT_DATA = 0; + int readableData = DEFAULT_DATA; + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + StatusCode code = IGetReadableData(sharedDataPrimary, &readableData, sizeof(int)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_NE(code.mStatusCode, STATUS_CODE_OK); + EXPECT_EQ(readableData, DEFAULT_DATA); + IShareDataFree(sharedDataPrimary); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo6 +TEST(SharedDataTest, Demo6) +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataPrimary) { + constexpr int DEFAULT_DATA = 0; + int readableData = DEFAULT_DATA; + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + StatusCode code = IGetReadableData(sharedDataPrimary, &readableData, sizeof(int)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_NE(code.mStatusCode, STATUS_CODE_OK); + EXPECT_EQ(readableData, DEFAULT_DATA); + IShareDataFree(sharedDataPrimary); + } + if (nullptr != sharedDataMinor) { + IShareDataFree(sharedDataMinor); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo7 +TEST(SharedDataTest, Demo7) +{ + constexpr int WRITABLE_DATA = 189; + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataPrimary) { + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); + } + if (nullptr != sharedDataMinor) { + constexpr int DEFAULT_DATA = 0; + int readableData = DEFAULT_DATA; + IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); + StatusCode code = IGetReadableData(sharedDataMinor, &readableData, sizeof(int)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); + EXPECT_EQ(readableData, WRITABLE_DATA); + IShareDataFree(sharedDataMinor); + } + if (nullptr != sharedDataPrimary) { + IShareDataFree(sharedDataPrimary); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.UNIT_SharedData_DEME_Demo7 +TEST(SharedDataTest, UNIT_SharedData_DEME_Demo7) +{ + constexpr int WRITABLE_DATA = 189; + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataPrimary) { + IMakeSharedData(sharedDataPrimary, sizeof(int) * 2, sizeof(int)); + ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); + } + if (nullptr != sharedDataMinor) { + constexpr int DEFAULT_DATA = 0; + int readableData = DEFAULT_DATA; + IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); + StatusCode code = IGetReadableData(sharedDataMinor, &readableData, sizeof(int)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_NE(code.mStatusCode, STATUS_CODE_OK); + EXPECT_EQ(readableData, DEFAULT_DATA); + IShareDataFree(sharedDataMinor); + } + if (nullptr != sharedDataPrimary) { + IShareDataFree(sharedDataPrimary); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.UNIT_SharedData_DEME_Demo8 +TEST(SharedDataTest, UNIT_SharedData_DEME_Demo8) +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataPrimary) { + int writableData[2] = {0}; + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int) * 2); + ISetWritableData(sharedDataPrimary, &writableData, sizeof(writableData)); + } + if (nullptr != sharedDataMinor) { + constexpr int DEFAULT_DATA = 0; + int readableData = DEFAULT_DATA; + IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); + StatusCode code = IGetReadableData(sharedDataMinor, &readableData, sizeof(int)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_NE(code.mStatusCode, STATUS_CODE_OK); + EXPECT_EQ(readableData, DEFAULT_DATA); + IShareDataFree(sharedDataMinor); + } + if (nullptr != sharedDataPrimary) { + IShareDataFree(sharedDataPrimary); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.UNIT_SharedData_DEME_Demo9 +TEST(SharedDataTest, UNIT_SharedData_DEME_Demo9) +{ + constexpr int WRITABLE_DATA = 189; + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataPrimary) { + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); + } + if (nullptr != sharedDataMinor) { + constexpr int DEFAULT_DATA = 0; + int readableData = DEFAULT_DATA; + IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int) * 2); + StatusCode code = IGetReadableData(sharedDataMinor, &readableData, sizeof(int)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_NE(code.mStatusCode, STATUS_CODE_OK); + EXPECT_EQ(readableData, DEFAULT_DATA); + IShareDataFree(sharedDataMinor); + } + if (nullptr != sharedDataPrimary) { + IShareDataFree(sharedDataPrimary); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.UNIT_SharedData_DEME_Demo10 +TEST(SharedDataTest, UNIT_SharedData_DEME_Demo10) +{ + constexpr int WRITABLE_DATA = 189; + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + if (nullptr != sharedDataPrimary) { + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); + } + if (nullptr != sharedDataMinor) { + int readableData[2] = {0}; + IMakeSharedData(sharedDataMinor, sizeof(int) * 2, sizeof(int)); + StatusCode code = IGetReadableData(sharedDataMinor, readableData, sizeof(readableData)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_NE(code.mStatusCode, STATUS_CODE_OK); + IShareDataFree(sharedDataMinor); + } + if (nullptr != sharedDataPrimary) { + IShareDataFree(sharedDataPrimary); + } + ILogUnInit(); +} +// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.UNIT_SharedData_DEME_Demo11 +TEST(SharedDataTest, UNIT_SharedData_DEME_Demo11) +{ + constexpr int WRITABLE_DATA = 189; + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + constexpr int PROJECT_ID = 9; + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); + if (nullptr != sharedDataPrimary) { + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); + } + if (nullptr != sharedDataMinor) { + int readableData = 0; + StatusCode code = IGetReadableData(sharedDataMinor, &readableData, sizeof(int)); + PrintStringCode(code); + LogInfo("readableData = %d\n", readableData); + EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); + EXPECT_EQ(readableData, 189); + IShareDataFree(sharedDataMinor); + } + if (nullptr != sharedDataPrimary) { + IShareDataFree(sharedDataPrimary); + } + ILogUnInit(); +} +} // namespace SharedDataTest \ No newline at end of file diff --git a/test/utils/TcpModule/CMakeLists.txt b/test/utils/TcpModule/CMakeLists.txt new file mode 100644 index 0000000..0619c71 --- /dev/null +++ b/test/utils/TcpModule/CMakeLists.txt @@ -0,0 +1,70 @@ +# 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( + ./tool/include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/TcpModule/include + ${TEST_SOURCE_PATH}/utils/LinuxApiMock/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +set(TARGET_NAME TcpModuleTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} TcpModule gtest gmock pthread Log) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + target_link_libraries(${TARGET_NAME} LinuxApiMock) +endif() +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + TcpModuleTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/TcpModule +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + TcpModuleTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/TcpModule +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make TcpModuleTest_code_check + COMMAND make TcpModuleTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +# if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") +# add_subdirectory(tool) +# endif() \ No newline at end of file diff --git a/test/utils/TcpModule/mainTest.cpp b/test/utils/TcpModule/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/utils/TcpModule/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/utils/TcpModule/src/TcpModule_Test.cpp b/test/utils/TcpModule/src/TcpModule_Test.cpp new file mode 100644 index 0000000..cf89f72 --- /dev/null +++ b/test/utils/TcpModule/src/TcpModule_Test.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ILog.h" +#include "TcpModule.h" +#include +#include +#include +namespace TcpModuleTest +{ +// ../output_files/test/bin/TcpModuleTest --gtest_filter=TcpModuleTest.UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject +/** + * TcpModule module api will not crash when object is illegal. + */ +TEST(TcpModuleTest, UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject) +{ + TcpServerParam tcpServerparam = { + .mIp = "127.0.0.1", + .mPort = 9876, + .mAcceptClientFunc = [](void *object, const char *ip) -> bool { + LogInfo("accept client, peer ip: %s\n", ip); + if (nullptr != object) { + AcceptClientWrite(object, "client accept send data.", strlen("client accept send data.")); + } + return true; + }, + .mClientAcceptParam = { + .mReadFunc = [](const void *data, const ssize_t len, const void *object) -> void { + LogInfo("client accept read data: %s\n", (char *)data); + }, + .mClosedFunc = [](const void *object) -> void { + LogInfo("client accept closed.\n"); + }, + }, + }; + TcpClientParam param = { + .mIp = "127.0.0.1", + .mPort = 9876, + .mReadFunc = [](const void *data, const ssize_t len, const void *context) -> void { + LogInfo("read data: %s", (char *)data); + return; + }, + .mClosedFunc = [](const void *object) -> void { + LogInfo("tcp client closed.\n"); + }, + }; + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + void *tcpServer = CreateTcpServer(tcpServerparam); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + void *tcpClient = CreateTcpClient(param); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + TcpClientWrite(tcpClient, "client send data.", strlen("client send data.")); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + if (nullptr != tcpClient) { + FreeTcpClient(tcpClient); + } + if (nullptr != tcpServer) { + FreeTcpServer(tcpServer); + } + ILogUnInit(); +} +} // namespace TcpModuleTest \ No newline at end of file diff --git a/test/utils/TestManager/CMakeLists.txt b/test/utils/TestManager/CMakeLists.txt new file mode 100644 index 0000000..7b2b91b --- /dev/null +++ b/test/utils/TestManager/CMakeLists.txt @@ -0,0 +1,53 @@ +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ./src + ./include + ${APPLICATION_SOURCE_PATH}/HuntingCamera/src + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/StatusCode/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}/libconfig/libconfig-1.7.3/lib/.libs +# ) + +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME TestManager) +add_library(${TARGET_NAME} STATIC ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} LinuxApi gtest gmock Log) + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + TestManager_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${SRC_FILES} + ${CLANG_TIDY_CONFIG} + -p ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/TestManager +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + TestManager_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/TestManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make TestManager_code_check + COMMAND make TestManager_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) \ No newline at end of file diff --git a/test/utils/TestManager/include/TestManager.h b/test/utils/TestManager/include/TestManager.h new file mode 100644 index 0000000..c1706c2 --- /dev/null +++ b/test/utils/TestManager/include/TestManager.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef TEST_MANAGER_H +#define TEST_MANAGER_H +#include +#include +class TestManager +{ +public: + TestManager() = default; + virtual ~TestManager() = default; + void Init(void); + void UnInit(void); + void ResetTimeOut(const unsigned int &timeOutMs); + void StartTimer(void); + void StopTimer(void); + void Timer(void); + +private: + std::mutex mMutex; + unsigned int mTimeOutMs; + unsigned int mSleepingTime; + bool mRuning; + std::thread mTimerThread; +}; +#endif \ No newline at end of file diff --git a/test/utils/TestManager/src/TestManager.cpp b/test/utils/TestManager/src/TestManager.cpp new file mode 100644 index 0000000..819d153 --- /dev/null +++ b/test/utils/TestManager/src/TestManager.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "TestManager.h" +#include "ILog.h" +#include "MainThread.h" +constexpr int TIMER_SLEEP_MS = 100; +void TestManager::Init(void) +{ + mSleepingTime = 0; + mTimeOutMs = 0; + mRuning = false; +} +void TestManager::UnInit(void) +{ + StopTimer(); + if (mTimerThread.joinable()) { + mTimerThread.join(); + } +} +void TestManager::ResetTimeOut(const unsigned int &timeOutMs) +{ + // + mTimeOutMs = timeOutMs; + StartTimer(); +} +void TestManager::StartTimer(void) +{ + std::lock_guard locker(mMutex); + if (mTimerThread.joinable()) { + LogInfo("Timer has started.\n"); + return; + } + auto timerThread = [](TestManager *timer) { + // + timer->Timer(); + }; + mTimerThread = std::thread(timerThread, this); +} +void TestManager::StopTimer(void) +{ + // + mRuning = false; +} +void TestManager::Timer(void) +{ + mRuning = true; + while (mRuning) { + std::this_thread::sleep_for(std::chrono::milliseconds(TIMER_SLEEP_MS)); + mSleepingTime += TIMER_SLEEP_MS; + if (mSleepingTime >= mTimeOutMs) { + LogInfo("Test time out.\n"); + mRuning = false; + MainThread::GetInstance()->Exit(); + } + } +} \ No newline at end of file diff --git a/test/utils/UartDevice/CMakeLists.txt b/test/utils/UartDevice/CMakeLists.txt new file mode 100644 index 0000000..62952dd --- /dev/null +++ b/test/utils/UartDevice/CMakeLists.txt @@ -0,0 +1,72 @@ +# 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( + ./tool/include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/UartDevice/include + ${TEST_SOURCE_PATH} + ${TEST_SOURCE_PATH}/utils/LinuxApiMock/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} + ${TEST_TOOLS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +set(TARGET_NAME UartDeviceTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} UartDevice UartDeviceTestTool gtest gmock pthread Log) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + target_link_libraries(${TARGET_NAME} LinuxApiMock) +endif() +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + UartDeviceTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/UartDevice +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + UartDeviceTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/UartDevice +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make UartDeviceTest_code_check + COMMAND make UartDeviceTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt") + add_subdirectory(tool) +endif() \ No newline at end of file diff --git a/test/utils/UartDevice/mainTest.cpp b/test/utils/UartDevice/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/utils/UartDevice/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/utils/UartDevice/src/UartDevice_Test.cpp b/test/utils/UartDevice/src/UartDevice_Test.cpp new file mode 100644 index 0000000..8d64497 --- /dev/null +++ b/test/utils/UartDevice/src/UartDevice_Test.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ILog.h" +#include "UartDevice.h" +#include +#include +namespace UartDeviceTest +{ +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceTest.UNIT_UartDevice_AUTO_IllegalObject +/** + * UartDevice module api will not crash when object is illegal. + */ +TEST(UartDeviceTest, UNIT_UartDevice_AUTO_IllegalObject) +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + + IUartOpen(nullptr); + IUartSend(nullptr, nullptr, 0); + IUartRecv(nullptr, nullptr, 0, 0); + IUartTcflush(nullptr); + IUartDeviceFree(nullptr); + + char illegalObject[100] = {0}; + void *object = &illegalObject[10]; + IUartOpen(object); + IUartSend(object, nullptr, 0); + IUartRecv(object, nullptr, 0, 0); + IUartTcflush(object); + IUartDeviceFree(object); + ILogUnInit(); +} +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceTest.UNIT_UartDevice_EXAMPLE_Demo +TEST(UartDeviceTest, UNIT_UartDevice_EXAMPLE_Demo) +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + UartInfo device = { + "dev/s1", + }; + void *object = CreateUartDevice(device); + IUartOpen(object); + // IUartSend(object, nullptr, 0); + // IUartRecv(object, nullptr, 0, 0); + // IUartTcflush(object); + IUartDeviceFree(object); + ILogUnInit(); +} +} // namespace UartDeviceTest \ No newline at end of file diff --git a/test/utils/UartDevice/src_mock/UartDevice_Mock_Test.cpp b/test/utils/UartDevice/src_mock/UartDevice_Mock_Test.cpp new file mode 100644 index 0000000..acbc857 --- /dev/null +++ b/test/utils/UartDevice/src_mock/UartDevice_Mock_Test.cpp @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ILog.h" +#include "LinuxApiMock.h" +#include "UartDevice.h" +#include "UartDeviceTestTool.h" +#include +#include +#include +namespace UartDeviceMockTest +{ +const char *gDeviceName = "dev/s1"; +static UartInfo gUartDevice = { + gDeviceName, + 1152000, + 'N', + 8, + 1, + 'N', +}; +class UartDeviceMockTest : public testing::Test, public UartDeviceTestTool +{ +public: + UartDeviceMockTest() + { + } + virtual ~UartDeviceMockTest() + { + } + static void SetUpTestCase() + { + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + } + static void TearDownTestCase() + { + ILogUnInit(); + } + virtual void SetUp() + { + mLinuxTest = LinuxTest::CreateLinuxTest(); + std::shared_ptr test = mLinuxTest; + LinuxApiMock::GetInstance(&test); + LinuxApiMock::GetInstance()->Init(); + RegisterUartDevice(mLinuxTest, gUartDevice); + } + virtual void TearDown() + { + LinuxApiMock::GetInstance()->UnInit(); + mLinuxTest = std::make_shared(); + std::shared_ptr test = std::make_shared(); + LinuxApiMock::GetInstance(&test); + UnregisterUartDevice(gUartDevice); + } + +public: + std::shared_ptr mLinuxTest; +}; +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_EXAMPLE_AUTO_Demo +TEST_F(UartDeviceMockTest, UNIT_UartDevice_EXAMPLE_AUTO_Demo) +{ + void *object = CreateUartDevice(gUartDevice); + IUartOpen(object); + const char *SEND_BUF = "TEST"; + ssize_t sendResult = IUartSend(object, SEND_BUF, strlen(SEND_BUF)); + EXPECT_EQ(sendResult, static_cast(strlen(SEND_BUF))); + constexpr int RECV_TIME_OUT_MS = 1000; + constexpr int RECV_BUF_LENGTH = 256; + char recvBuf[RECV_BUF_LENGTH] = {0}; + ssize_t recvResult = IUartRecv(object, recvBuf, RECV_BUF_LENGTH, RECV_TIME_OUT_MS); + LogInfo("recvResult = %d\n", recvResult); + IUartTcflush(object); + IUartDeviceFree(object); +} +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_ParameterEarror +/** + * @brief Construct a new test f object + * Is the interface accurate when testing parameter errors in this test case. + */ +TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_ParameterEarror) +{ + char *deviceName = nullptr; + static UartInfo uartDevice = { + deviceName, + 1152000, + 'N', + 8, + 1, + 'N', + }; + RegisterUartDevice(mLinuxTest, uartDevice); + void *object = CreateUartDevice(uartDevice); + IUartOpen(object); + IUartDeviceFree(object); + EXPECT_EQ(object, nullptr); + UnregisterUartDevice(uartDevice); +} +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_ParameterEarror2 +/** + * @brief Construct a new test f object + * Is the interface accurate when testing parameter errors in this test case. + */ +TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_ParameterEarror2) +{ + // TODO: + // const char *SEND_BUF = "TEST"; + // char *deviceName = (char *)malloc(strlen(SEND_BUF) + 1); + // memset(deviceName, 0, strlen(SEND_BUF) + 1); + // static UartInfo uartDevice = { + // "deviceName", + // 1152000, + // 'N', + // 8, + // 1, + // 'N', + // }; + // RegisterUartDevice(mLinuxTest, uartDevice); + // void *object = CreateUartDevice(uartDevice); + // IUartOpen(object); + // IUartDeviceFree(object); + // free(deviceName); + // deviceName = nullptr; +} +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_WriteAllData +/** + * @brief Construct a new test f object + * This test case verifies whether the application will call the write function twice until all data is written when the + * write function does not write the length of the data. + */ +TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_WriteAllData) +{ + const char *SEND_BUF = "TEST"; + constexpr ssize_t SEND_LENGTH = 1; // less than static_cast(strlen(SEND_BUF)) + SetSendApiOnce(mLinuxTest, gUartDevice, SEND_LENGTH); + void *object = CreateUartDevice(gUartDevice); + IUartOpen(object); + ssize_t sendResult = IUartSend(object, SEND_BUF, strlen(SEND_BUF)); + EXPECT_EQ(sendResult, static_cast(strlen(SEND_BUF))); + IUartDeviceFree(object); +} +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_RecvData +TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_RecvData) +{ + const char *RECV_BUFF = "TEST"; + SetRecvApiOnce(mLinuxTest, gUartDevice, (void *)RECV_BUFF, strlen(RECV_BUFF)); + void *object = CreateUartDevice(gUartDevice); + IUartOpen(object); + constexpr int RECV_TIME_OUT_MS = 1000; + constexpr int RECV_BUF_LENGTH = 256; + char recvBuf[RECV_BUF_LENGTH] = {0}; + ssize_t recvResult = IUartRecv(object, recvBuf, RECV_BUF_LENGTH, RECV_TIME_OUT_MS); + EXPECT_EQ(recvResult, static_cast(strlen(RECV_BUFF))); + EXPECT_EQ(0, memcmp(recvBuf, RECV_BUFF, strlen(RECV_BUFF))); + IUartDeviceFree(object); +} +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_EXAMPLE_MultiThreadTest +/** + * @brief Construct a new test f object + * This test case tests whether the correct return value can be obtained when simulating multi-threaded operations on + * the interface. + */ +TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_MultiThreadTest) +{ + auto openThread = [](void) { + LogInfo("openThread.\n"); + void *object = CreateUartDevice(gUartDevice); + IUartOpen(object); + IUartDeviceFree(object); + }; + std::thread test1 = std::thread(openThread); + test1.detach(); + std::thread test2 = std::thread(openThread); + test2.detach(); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); +} +// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_OpenFailed +TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_OpenFailed) +{ + SetUartDeviceOpenFailed(mLinuxTest, gUartDevice); + const char *SEND_BUF = "TEST"; + constexpr ssize_t SEND_LENGTH = 1; // less than static_cast(strlen(SEND_BUF)) + SetSendApiOnce(mLinuxTest, gUartDevice, SEND_LENGTH); + void *object = CreateUartDevice(gUartDevice); + IUartOpen(object); + ssize_t sendResult = IUartSend(object, SEND_BUF, strlen(SEND_BUF)); + EXPECT_EQ(sendResult, static_cast(strlen(SEND_BUF))); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IUartDeviceFree(object); +} +} // namespace UartDeviceMockTest \ No newline at end of file diff --git a/test/utils/UartDevice/tool/README.md b/test/utils/UartDevice/tool/README.md new file mode 100644 index 0000000..d55e6cc --- /dev/null +++ b/test/utils/UartDevice/tool/README.md @@ -0,0 +1,3 @@ +# Tool目录 + +  基于模块独立的测试辅助工具,可被外界模块复用。 \ No newline at end of file diff --git a/test/utils/UartDevice/tool/include/UartDeviceTestTool.h b/test/utils/UartDevice/tool/include/UartDeviceTestTool.h new file mode 100644 index 0000000..03a4ed1 --- /dev/null +++ b/test/utils/UartDevice/tool/include/UartDeviceTestTool.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef UART_DEVICE_TEST_TOOL_H +#define UART_DEVICE_TEST_TOOL_H +#include "LinuxApiMock.h" +#include "UartDevice.h" +#include +constexpr int UART_DEVICE_READ_NOTHING = 0; +class UartDeviceTestTool +{ +public: + UartDeviceTestTool() = default; + virtual ~UartDeviceTestTool() = default; + /** + * @brief Enable the UartDevice module to run and debug on Ubuntu + * + * @param mock + * @param uart + * @return int Return a virtual handle, and the simulated test code uses this handle to bind the device. + */ + int RegisterUartDevice(std::shared_ptr &mock, const UartInfo &uart); + /** + * @brief Get the Device Mock Fd object + * + * @param uart + * @return int fd of uart device. + */ + int GetDeviceMockFd(const UartInfo &uart); + /** + * @brief + * + * @param uart + */ + void UnregisterUartDevice(const UartInfo &uart); + /** + * @brief Set the Send Api object + * Set the length of a successful send. + * @param mock + * @param uart + * @param length The length of a successful send. + */ + void SetSendApiOnce(std::shared_ptr &mock, const UartInfo &uart, const ssize_t &length); + /** + * @brief Set the Recv Api Once object + * Set the data received at once. + * @param mock + * @param uart + * @param recvBuff The data that will be received. + * @param length The length of data that will be received. + */ + void SetRecvApiOnce(std::shared_ptr &mock, const UartInfo &uart, void *recvBuff, const ssize_t &length); + /** + * @brief Set the Uart Device Open Failed object + * + * @param mock + * @param uart + */ + void SetUartDeviceOpenFailed(std::shared_ptr &mock, const UartInfo &uart); + /** + * @brief Set the Uart Device Read Nothing object + * + * @param mock + * @param uart + */ + void SetUartDeviceReadNothing(std::shared_ptr &mock, const UartInfo &uart); + /** + * @brief Set the Uart Device Time Out object + * + * @param mock + * @param uart + */ + void SetUartDeviceTimeOut(std::shared_ptr &mock, const UartInfo &uart); + +private: + std::map mDeviceMap; +}; +#endif \ No newline at end of file diff --git a/test/utils/WebServer/CMakeLists.txt b/test/utils/WebServer/CMakeLists.txt new file mode 100644 index 0000000..5a84238 --- /dev/null +++ b/test/utils/WebServer/CMakeLists.txt @@ -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( + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/WebServer/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES) +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME WebServerTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} WebServer gtest gmock pthread Log) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + WebServerTest_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 ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/WebServer +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + WebServerTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/WebServer +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make WebServerTest_code_check + COMMAND make WebServerTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) \ No newline at end of file diff --git a/test/utils/WebServer/mainTest.cpp b/test/utils/WebServer/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/utils/WebServer/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/utils/WebServer/src/WebServer_Test.cpp b/test/utils/WebServer/src/WebServer_Test.cpp new file mode 100644 index 0000000..20b2856 --- /dev/null +++ b/test/utils/WebServer/src/WebServer_Test.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ILog.h" +#include "WebServer.h" +#include +#include +#include +namespace WebServerTest +{ +// ../output_files/test/bin/WebServerTest --gtest_filter=WebServerTest.Demo +TEST(WebServerTest, Demo) +{ + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + WebServerParam web = {.mIp = "192.168.1.29", .mPort = 8080}; + WebServerInit(web); + // std::this_thread::sleep_for(std::chrono::milliseconds(1000 * 10)); + WebServerExit(); + WebServerUnInit(); + ILogUnInit(); +} +} // namespace WebServerTest \ No newline at end of file diff --git a/tools/README.md b/tools/README.md new file mode 100644 index 0000000..550aa8c --- /dev/null +++ b/tools/README.md @@ -0,0 +1,24 @@ +# 1. 工具 + +## 1.1. 概述 +  该目录存放使用到的工具。 + +## 1.2. delete_test_code.sh + +  删除测试工具代码。 + +使用方法: +``` +$ cd //tools/shell +$ ./delete_test_code.sh ../../test/ +``` + +## 1.3. delete_sdk_code.sh + +  根据需要,删除SDK当中若干模块的src代码。 + +使用方法: +``` +$ cd //tools/shell +$ ./delete_sdk_code.sh +``` \ No newline at end of file diff --git a/tools/clang-tidy/.gitignore b/tools/clang-tidy/.gitignore new file mode 100644 index 0000000..40ba0fc --- /dev/null +++ b/tools/clang-tidy/.gitignore @@ -0,0 +1 @@ +llvm-project \ No newline at end of file diff --git a/tools/clang-tidy/CMakeLists.txt b/tools/clang-tidy/CMakeLists.txt new file mode 100644 index 0000000..d23d542 --- /dev/null +++ b/tools/clang-tidy/CMakeLists.txt @@ -0,0 +1,7 @@ + +add_custom_target( + compile_llvm + COMMAND echo "Compile llvm." + COMMAND git clone https://github.com/llvm/llvm-project.git + WORKING_DIRECTORY ${IPC_SDK_PATH}/tools/clang-tidy +) \ No newline at end of file diff --git a/tools/clang-tidy/Makefile b/tools/clang-tidy/Makefile new file mode 100644 index 0000000..b7fd1d3 --- /dev/null +++ b/tools/clang-tidy/Makefile @@ -0,0 +1,15 @@ +all: + @if [ ! -d "llvm-project" ]; then \ + echo "llvm-project not exist"; \ + git clone https://github.com/llvm/llvm-project.git; \ + cd llvm-project/ && mkdir build; \ + cd build && cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" ../llvm; \ + fi + @if [ ! -d "llvm-project/build" ]; then \ + cd llvm-project/ && mkdir build; \ + fi +# @git clone https://github.com/llvm/llvm-project.git +# @cd llvm-project/ && mkdir build + @cd llvm-project/build && cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" ../llvm + @cd llvm-project/build && make -j8 + @mv llvm-project ${HOME}/ \ No newline at end of file diff --git a/tools/clang-tidy/README.md b/tools/clang-tidy/README.md new file mode 100644 index 0000000..9af8870 --- /dev/null +++ b/tools/clang-tidy/README.md @@ -0,0 +1,9 @@ +# 1. llvm系列工具 + +   git clone时可能会非常慢,且频繁失败(网速快时可正常),失败后请多次重试。最好在保证网速的情况下进行下载源码。 + +## 1.1. 使用说明 + +1. 在本目录执行make;会下载源码,编译,并把llvm源码包(含生成的工具文件)拷贝到**用户根目录**,以方便不同目录的源码工程可以复用llvm工具; + +**如果安装时使用普通用户执行make compile_llvm,编译时使用跟用户权限(或者加sudo),可能会提示命令不存在。** \ No newline at end of file diff --git a/tools/clang-tidy/clang-tidy b/tools/clang-tidy/clang-tidy new file mode 100755 index 0000000..1ef09fe Binary files /dev/null and b/tools/clang-tidy/clang-tidy differ diff --git a/tools/cmake/.gitignore b/tools/cmake/.gitignore new file mode 100644 index 0000000..a4a8c05 --- /dev/null +++ b/tools/cmake/.gitignore @@ -0,0 +1 @@ +cmake-3.27.4 \ No newline at end of file diff --git a/tools/cmake/Makefile b/tools/cmake/Makefile new file mode 100644 index 0000000..f88374e --- /dev/null +++ b/tools/cmake/Makefile @@ -0,0 +1,8 @@ +all: + @sudo apt-get update + @sudo apt-get install openssl + @sudo apt-get install libssl-dev + @tar zxvf cmake-3.27.4.tar.gz + @cd cmake-3.27.4/ && ./bootstrap + @cd cmake-3.27.4/ && make + @cd cmake-3.27.4/ && sudo make install \ No newline at end of file diff --git a/tools/cmake/cmake-3.27.4.tar.gz b/tools/cmake/cmake-3.27.4.tar.gz new file mode 100755 index 0000000..23d4cb3 Binary files /dev/null and b/tools/cmake/cmake-3.27.4.tar.gz differ diff --git a/tools/shell/delete_sdk_code.sh b/tools/shell/delete_sdk_code.sh new file mode 100755 index 0000000..e7f53cc --- /dev/null +++ b/tools/shell/delete_sdk_code.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# 查找目录列表 +search_dirs=("../../middleware" "../../application" "../../utils") +# search_dirs=("../../test-utils") + +# 保留目录列表 +# preserve_dirs=("../../test-utils/LinuxApi" "../../test-utils/ConfigBase") +preserve_dirs=( + "../../test-utils/Log") + +# 遍历查找目录 +for search_dir in "${search_dirs[@]}"; do + echo "正在处理目录: $search_dir" + + # 使用find命令遍历目录并删除文件 + find "$search_dir" -type d | while read -r dir; do + # 检查目录是否在保留列表中 + is_preserve=false + for preserve_dir in "${preserve_dirs[@]}"; do + if [[ "$dir" == *"$preserve_dir"* ]]; then + is_preserve=true + break + fi + done + + # 如果目录不在保留列表中,则删除src目录和CMakeLists.txt + if [[ $is_preserve == false ]]; then + src_dir="$dir/src" + cmake_file="$dir/CMakeLists.txt" + + # 如果src目录存在,则删除它 + if [[ -d "$src_dir" ]]; then + echo "删除目录: $src_dir" + rm -rf "$src_dir" + # 如果CMakeLists.txt文件存在,则删除它 + if [[ -f "$cmake_file" ]]; then + echo "删除文件: $cmake_file" + rm "$cmake_file" + fi + fi + fi + done +done + +echo "处理完成" \ No newline at end of file diff --git a/tools/shell/delete_test_code.sh b/tools/shell/delete_test_code.sh new file mode 100755 index 0000000..2206019 --- /dev/null +++ b/tools/shell/delete_test_code.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# 检查参数 +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + echo "Example: $0 /path/to/search" + exit 1 +fi + +# 检查目录是否存在 +if [ ! -d "$1" ]; then + echo "Error: Directory '$1' does not exist." + exit 1 +fi + +# 递归遍历并删除文件 +find "$1" -type d -name "tool" | while read -r tool_dir; do + # 在tool目录下查找src目录 + src_dir="$tool_dir/src" + if [ -d "$src_dir" ]; then + # 删除src目录下的所有文件 + find "$src_dir" -type f -delete + echo "Deleted all files in '$src_dir'" + else + echo "No 'src' directory found in '$tool_dir'" + fi + + # 检查CMakeLists.txt文件是否存在,然后删除 + cmake_file="$tool_dir/CMakeLists.txt" + if [ -f "$cmake_file" ]; then + rm "$cmake_file" + if [ $? -eq 0 ]; then + echo "Deleted 'CMakeLists.txt' in '$tool_dir'" + else + echo "Error deleting 'CMakeLists.txt' in '$tool_dir'" + fi + fi +done + +echo "Operation completed." \ No newline at end of file diff --git a/tools/shell/find_and_kill.sh b/tools/shell/find_and_kill.sh new file mode 100755 index 0000000..657981a --- /dev/null +++ b/tools/shell/find_and_kill.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# 指定要查找并杀死的进程名称列表 +target_processes=("rkipc" "rkwifi_server" "wpa_supplicant" "udhcpc") + +# 遍历进程名称列表 +for target_process in "${target_processes[@]}"; do + # 使用pgrep命令查找目标进程的PID + pids=$(pgrep -f "$target_process") + + # 检查是否找到目标进程 + if [ -z "$pids" ]; then + echo "未找到目标进程: $target_process" + else + echo "找到以下目标进程并准备杀死它们:" + for pid in $pids; do + echo "进程: $target_process,PID: $pid" + # 使用kill命令杀死目标进程 + kill -9 "$pid" + done + echo "所有$target_process进程已被杀死" + fi +done \ No newline at end of file diff --git a/tools/version_release/VersionReleaseTool b/tools/version_release/VersionReleaseTool new file mode 100755 index 0000000..95da273 Binary files /dev/null and b/tools/version_release/VersionReleaseTool differ