Improve:HalTestTool
This commit is contained in:
parent
6610558788
commit
f02d2b4a6d
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
|
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
|
||||||
|
include(${CMAKE_CURRENT_SOURCE_DIR}/build/hal.cmake)
|
||||||
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
|
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
|
||||||
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
|
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
|
||||||
|
|
||||||
|
@ -16,9 +17,6 @@ include_directories(
|
||||||
# ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/build/lib
|
# ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/build/lib
|
||||||
# )
|
# )
|
||||||
|
|
||||||
add_definitions(-DSD_CARD_DEV=\"${SD_CARD_DEV}\")
|
|
||||||
add_definitions(-DSD_CARD_MOUNT_PATH=\"${SD_CARD_MOUNT_PATH}\")
|
|
||||||
|
|
||||||
aux_source_directory(./abstract ABSTRACT_SRC_FILES)
|
aux_source_directory(./abstract ABSTRACT_SRC_FILES)
|
||||||
aux_source_directory(./src IMPL_SRC_FILES)
|
aux_source_directory(./src IMPL_SRC_FILES)
|
||||||
|
|
||||||
|
|
2
hal/build/hal.cmake
Normal file
2
hal/build/hal.cmake
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
add_definitions(-DSD_CARD_DEV=\"${SD_CARD_DEV}\")
|
||||||
|
add_definitions(-DSD_CARD_MOUNT_PATH=\"${SD_CARD_MOUNT_PATH}\")
|
|
@ -78,7 +78,7 @@ StatusCode HalMakePtr::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl)
|
||||||
}
|
}
|
||||||
StatusCode HalMakePtr::CreateWifiHal(std::shared_ptr<VWifiHal> &impl)
|
StatusCode HalMakePtr::CreateWifiHal(std::shared_ptr<VWifiHal> &impl)
|
||||||
{
|
{
|
||||||
LogWarning("CreateWifiHal.\n");
|
LogInfo("CreateWifiHal.\n");
|
||||||
impl = std::make_shared<WifiHal>();
|
impl = std::make_shared<WifiHal>();
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
const char *SD_CARD_DEVICE = SD_CARD_DEV;
|
||||||
SdCardHal::SdCardHal()
|
SdCardHal::SdCardHal()
|
||||||
{
|
{
|
||||||
mThreadRuning = false;
|
mThreadRuning = false;
|
||||||
|
@ -48,16 +49,15 @@ void SdCardHal::UnInit(void)
|
||||||
}
|
}
|
||||||
void SdCardHal::DevDetectingThread(void)
|
void SdCardHal::DevDetectingThread(void)
|
||||||
{
|
{
|
||||||
constexpr int SLEEP_TIME_MS = 100;
|
constexpr int SLEEP_TIME_MS = 1000;
|
||||||
SdCardHalStatus status = SdCardHalStatus::END;
|
SdCardHalStatus status = SdCardHalStatus::END;
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
// const char *dev = "/dev/mmcblk1p1";
|
// const char *SD_CARD_DEVICE = "/SD_CARD_DEVICE/mmcblk1p1";
|
||||||
const char *dev = SD_CARD_DEV;
|
|
||||||
mThreadRuning = true;
|
mThreadRuning = true;
|
||||||
while (mThreadRuning) {
|
while (mThreadRuning) {
|
||||||
fd = fx_open(dev, O_RDONLY);
|
fd = fx_open(SD_CARD_DEVICE, O_RDONLY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
LogInfo("sdCardHal: %s open failed.\n", dev);
|
LogInfo("sdCardHal: %s open failed.\n", SD_CARD_DEVICE);
|
||||||
if (SdCardHalStatus::PULL_OUT != status) {
|
if (SdCardHalStatus::PULL_OUT != status) {
|
||||||
status = SdCardHalStatus::PULL_OUT;
|
status = SdCardHalStatus::PULL_OUT;
|
||||||
ReportDetecedChangedResult(status);
|
ReportDetecedChangedResult(status);
|
||||||
|
@ -65,8 +65,8 @@ void SdCardHal::DevDetectingThread(void)
|
||||||
goto CONTINUE;
|
goto CONTINUE;
|
||||||
}
|
}
|
||||||
struct stat sdStat;
|
struct stat sdStat;
|
||||||
if (fstat(fd, &sdStat) < 0) {
|
if (fx_fstat(fd, &sdStat) < 0) {
|
||||||
LogInfo("sdCardHal: %s fstat failed.\n", dev);
|
LogInfo("sdCardHal: %s fstat failed.\n", SD_CARD_DEVICE);
|
||||||
if (SdCardHalStatus::ERROR != status) {
|
if (SdCardHalStatus::ERROR != status) {
|
||||||
status = SdCardHalStatus::ERROR;
|
status = SdCardHalStatus::ERROR;
|
||||||
ReportDetecedChangedResult(status);
|
ReportDetecedChangedResult(status);
|
||||||
|
@ -74,14 +74,14 @@ void SdCardHal::DevDetectingThread(void)
|
||||||
goto CONTINUE;
|
goto CONTINUE;
|
||||||
}
|
}
|
||||||
if (!S_ISBLK(sdStat.st_mode)) {
|
if (!S_ISBLK(sdStat.st_mode)) {
|
||||||
LogInfo("sdCardHal: %s is not block device.\n", dev);
|
LogInfo("sdCardHal: %s is not block device.\n", SD_CARD_DEVICE);
|
||||||
if (SdCardHalStatus::PULL_OUT != status) {
|
if (SdCardHalStatus::PULL_OUT != status) {
|
||||||
status = SdCardHalStatus::PULL_OUT;
|
status = SdCardHalStatus::PULL_OUT;
|
||||||
ReportDetecedChangedResult(status);
|
ReportDetecedChangedResult(status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LogInfo("sdCardHal: %s is inserted.\n", dev);
|
LogInfo("sdCardHal: %s is inserted.\n", SD_CARD_DEVICE);
|
||||||
if (SdCardHalStatus::INSERTED != status) {
|
if (SdCardHalStatus::INSERTED != status) {
|
||||||
status = SdCardHalStatus::INSERTED;
|
status = SdCardHalStatus::INSERTED;
|
||||||
ReportDetecedChangedResult(status);
|
ReportDetecedChangedResult(status);
|
||||||
|
@ -100,7 +100,7 @@ void SdCardHal::ReportDetecedChangedResult(const SdCardHalStatus &status)
|
||||||
}
|
}
|
||||||
monitor->ReportEvent(status);
|
monitor->ReportEvent(status);
|
||||||
if (SdCardHalStatus::INSERTED == status) {
|
if (SdCardHalStatus::INSERTED == status) {
|
||||||
LogInfo("mount sd dev %s.\n", SD_CARD_MOUNT_PATH);
|
LogInfo("mount sd SD_CARD_DEVICE %s.\n", SD_CARD_MOUNT_PATH);
|
||||||
constexpr int BUF_LENGTH = 128;
|
constexpr int BUF_LENGTH = 128;
|
||||||
char cmd[BUF_LENGTH] = {0};
|
char cmd[BUF_LENGTH] = {0};
|
||||||
snprintf(cmd, BUF_LENGTH, "mount %s %s", SD_CARD_DEV, SD_CARD_MOUNT_PATH);
|
snprintf(cmd, BUF_LENGTH, "mount %s %s", SD_CARD_DEV, SD_CARD_MOUNT_PATH);
|
||||||
|
|
|
@ -20,19 +20,12 @@ endif()
|
||||||
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
|
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
|
||||||
message("linux mock api will be set.")
|
message("linux mock api will be set.")
|
||||||
set(TEST_LINUX_MOCK "-Wl,--wrap=fx_open" CACHE STRING INTERNAL FORCE)
|
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_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_tcsetattr" CACHE STRING INTERNAL FORCE)
|
||||||
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=gethostbyname" CACHE STRING INTERNAL FORCE)
|
|
||||||
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=connect" CACHE STRING INTERNAL FORCE)
|
|
||||||
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=socket" 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_select" CACHE STRING INTERNAL FORCE)
|
||||||
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_open" CACHE STRING INTERNAL FORCE)
|
|
||||||
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_read" 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_write" CACHE STRING INTERNAL FORCE)
|
||||||
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_close" CACHE STRING INTERNAL FORCE)
|
|
||||||
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fclose" 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_read" CACHE STRING INTERNAL FORCE)
|
||||||
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fcntl" CACHE STRING INTERNAL FORCE)
|
|
||||||
endif()
|
endif()
|
||||||
# Mock Linux api.
|
# Mock Linux api.
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TEST_LINUX_MOCK}")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TEST_LINUX_MOCK}")
|
||||||
|
|
|
@ -49,6 +49,7 @@ void HunttingCameraTest::SetUp()
|
||||||
LinuxApiMock::GetInstance(&test);
|
LinuxApiMock::GetInstance(&test);
|
||||||
LinuxApiMock::GetInstance()->Init();
|
LinuxApiMock::GetInstance()->Init();
|
||||||
McuManagerTestTool::Init(mLinuxTest);
|
McuManagerTestTool::Init(mLinuxTest);
|
||||||
|
HalTestTool::InitSdCardHal(mLinuxTest);
|
||||||
TestManager::Init();
|
TestManager::Init();
|
||||||
}
|
}
|
||||||
void HunttingCameraTest::TearDown()
|
void HunttingCameraTest::TearDown()
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
|
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
|
||||||
|
include(${HAL_SOURCE_PATH}/build/hal.cmake)
|
||||||
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
|
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
|
||||||
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
|
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
|
||||||
|
|
||||||
|
@ -11,6 +12,7 @@ include_directories(
|
||||||
${UTILS_SOURCE_PATH}/LedControl/include
|
${UTILS_SOURCE_PATH}/LedControl/include
|
||||||
${HAL_SOURCE_PATH}/src
|
${HAL_SOURCE_PATH}/src
|
||||||
${TEST_SOURCE_PATH}
|
${TEST_SOURCE_PATH}
|
||||||
|
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
|
||||||
)
|
)
|
||||||
# link_directories(
|
# link_directories(
|
||||||
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
|
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
|
||||||
|
|
|
@ -17,12 +17,14 @@
|
||||||
#include "GtestUsing.h"
|
#include "GtestUsing.h"
|
||||||
#include "IHalCpp.h"
|
#include "IHalCpp.h"
|
||||||
#include "LedControl.h"
|
#include "LedControl.h"
|
||||||
|
#include "LinuxApiMock.h"
|
||||||
class HalTestTool
|
class HalTestTool
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HalTestTool() = default;
|
HalTestTool() = default;
|
||||||
virtual ~HalTestTool() = default;
|
virtual ~HalTestTool() = default;
|
||||||
void Init(void);
|
void Init(void);
|
||||||
|
void InitSdCardHal(std::shared_ptr<LinuxTest> &mock);
|
||||||
void UnInit(void);
|
void UnInit(void);
|
||||||
/**
|
/**
|
||||||
* @brief Set the All Keys Result object
|
* @brief Set the All Keys Result object
|
||||||
|
@ -67,6 +69,7 @@ private: // About camera hal
|
||||||
void InitAllCamerasMock(std::map<CameraType, std::shared_ptr<VCameraHal>> &allCameras);
|
void InitAllCamerasMock(std::map<CameraType, std::shared_ptr<VCameraHal>> &allCameras);
|
||||||
void InitCamerasMock(std::shared_ptr<VCameraHal> &vMock);
|
void InitCamerasMock(std::shared_ptr<VCameraHal> &vMock);
|
||||||
|
|
||||||
|
private: // About sd card hal
|
||||||
public:
|
public:
|
||||||
static std::shared_ptr<VKeyHal> MakeKeyHalTest(const std::string &keyName);
|
static std::shared_ptr<VKeyHal> MakeKeyHalTest(const std::string &keyName);
|
||||||
static std::shared_ptr<VLedHal> MakeLedHalTest(const std::string &ledName);
|
static std::shared_ptr<VLedHal> MakeLedHalTest(const std::string &ledName);
|
||||||
|
@ -77,5 +80,6 @@ private:
|
||||||
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeys;
|
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeys;
|
||||||
std::map<std::string, std::shared_ptr<VLedHal>> mAllLeds;
|
std::map<std::string, std::shared_ptr<VLedHal>> mAllLeds;
|
||||||
std::map<CameraType, std::shared_ptr<VCameraHal>> mAllCameras;
|
std::map<CameraType, std::shared_ptr<VCameraHal>> mAllCameras;
|
||||||
|
std::shared_ptr<VSdCardHal> mSdCardHal;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
|
@ -14,12 +14,13 @@
|
||||||
*/
|
*/
|
||||||
#include "HalMakePtrTest.h"
|
#include "HalMakePtrTest.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
void OverrideHalMakePtrObject(std::shared_ptr<HalCppMock> &halMock)
|
void OverrideHalMakePtrObject(std::shared_ptr<HalCppMock> &halMock, std::shared_ptr<SdCardHalMock> &sdCardHalMock)
|
||||||
{
|
{
|
||||||
std::shared_ptr<HalMakePtr> impl = std::make_shared<HalMakePtrTest>();
|
std::shared_ptr<HalMakePtr> impl = std::make_shared<HalMakePtrTest>();
|
||||||
std::shared_ptr<HalMakePtrTest> test = std::dynamic_pointer_cast<HalMakePtrTest>(impl);
|
std::shared_ptr<HalMakePtrTest> test = std::dynamic_pointer_cast<HalMakePtrTest>(impl);
|
||||||
if (test) {
|
if (test) {
|
||||||
test->mHalCppMock = halMock;
|
test->mHalCppMock = halMock;
|
||||||
|
test->mSdCardHalMock = sdCardHalMock;
|
||||||
}
|
}
|
||||||
HalMakePtr::GetInstance(&impl);
|
HalMakePtr::GetInstance(&impl);
|
||||||
}
|
}
|
||||||
|
@ -29,18 +30,18 @@ void CancelOverrideHalMakePtrObject(void)
|
||||||
std::shared_ptr<HalMakePtrTest> test = std::dynamic_pointer_cast<HalMakePtrTest>(tmp);
|
std::shared_ptr<HalMakePtrTest> test = std::dynamic_pointer_cast<HalMakePtrTest>(tmp);
|
||||||
if (test) {
|
if (test) {
|
||||||
test->mHalCppMock.reset();
|
test->mHalCppMock.reset();
|
||||||
|
test->mSdCardHalMock.reset();
|
||||||
}
|
}
|
||||||
std::shared_ptr<HalMakePtr> impl = std::make_shared<HalMakePtrTest>();
|
std::shared_ptr<HalMakePtr> impl = std::make_shared<HalMakePtrTest>();
|
||||||
HalMakePtr::GetInstance(&impl);
|
HalMakePtr::GetInstance(&impl);
|
||||||
}
|
}
|
||||||
HalMakePtrTest::HalMakePtrTest()
|
HalMakePtrTest::HalMakePtrTest()
|
||||||
{
|
{
|
||||||
//
|
|
||||||
}
|
}
|
||||||
HalMakePtrTest::~HalMakePtrTest()
|
HalMakePtrTest::~HalMakePtrTest()
|
||||||
{
|
{
|
||||||
//
|
|
||||||
mHalCppMock.reset();
|
mHalCppMock.reset();
|
||||||
|
mSdCardHalMock.reset();
|
||||||
}
|
}
|
||||||
StatusCode HalMakePtrTest::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl)
|
StatusCode HalMakePtrTest::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl)
|
||||||
{
|
{
|
||||||
|
@ -53,3 +54,8 @@ StatusCode HalMakePtrTest::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl)
|
||||||
}
|
}
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
|
StatusCode HalMakePtrTest::CreateSdCardHal(std::shared_ptr<VSdCardHal> &impl)
|
||||||
|
{
|
||||||
|
impl = mSdCardHalMock;
|
||||||
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
|
}
|
|
@ -18,7 +18,8 @@
|
||||||
#include "HalMakePtr.h"
|
#include "HalMakePtr.h"
|
||||||
#include "HalTestTool.h"
|
#include "HalTestTool.h"
|
||||||
#include "KeyControlMock.h"
|
#include "KeyControlMock.h"
|
||||||
void OverrideHalMakePtrObject(std::shared_ptr<HalCppMock> &halMock);
|
#include "SdCardHalMock.h"
|
||||||
|
void OverrideHalMakePtrObject(std::shared_ptr<HalCppMock> &halMock, std::shared_ptr<SdCardHalMock> &sdCardHalMock);
|
||||||
void CancelOverrideHalMakePtrObject(void);
|
void CancelOverrideHalMakePtrObject(void);
|
||||||
class HalMakePtrTest : public HalMakePtr
|
class HalMakePtrTest : public HalMakePtr
|
||||||
{
|
{
|
||||||
|
@ -26,8 +27,10 @@ public:
|
||||||
HalMakePtrTest();
|
HalMakePtrTest();
|
||||||
virtual ~HalMakePtrTest();
|
virtual ~HalMakePtrTest();
|
||||||
StatusCode CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl) override;
|
StatusCode CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl) override;
|
||||||
|
StatusCode CreateSdCardHal(std::shared_ptr<VSdCardHal> &impl) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::shared_ptr<HalCppMock> mHalCppMock;
|
std::shared_ptr<HalCppMock> mHalCppMock;
|
||||||
|
std::shared_ptr<SdCardHalMock> mSdCardHalMock;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
|
@ -20,14 +20,27 @@
|
||||||
#include "KeyControl.h"
|
#include "KeyControl.h"
|
||||||
#include "KeyControlMock.h"
|
#include "KeyControlMock.h"
|
||||||
#include "LedControlMock.h"
|
#include "LedControlMock.h"
|
||||||
|
#include "SdCardHalMock.h"
|
||||||
#include "WifiHalMock.h"
|
#include "WifiHalMock.h"
|
||||||
#include <thread>
|
#include <thread>
|
||||||
void HalTestTool::Init(void)
|
void HalTestTool::Init(void)
|
||||||
{
|
{
|
||||||
mHalMock = std::make_shared<HalCppMock>();
|
mHalMock = std::make_shared<HalCppMock>();
|
||||||
|
mSdCardHal = std::make_shared<SdCardHalMock>();
|
||||||
HalMockInit(mHalMock);
|
HalMockInit(mHalMock);
|
||||||
std::shared_ptr<HalCppMock> mock = std::dynamic_pointer_cast<HalCppMock>(mHalMock);
|
// TODO: 初始化mSdCardHal
|
||||||
OverrideHalMakePtrObject(mock);
|
std::shared_ptr<HalCppMock> halMock = std::dynamic_pointer_cast<HalCppMock>(mHalMock);
|
||||||
|
std::shared_ptr<SdCardHalMock> sdCardHalMock = std::dynamic_pointer_cast<SdCardHalMock>(mSdCardHal);
|
||||||
|
OverrideHalMakePtrObject(halMock, sdCardHalMock);
|
||||||
|
}
|
||||||
|
void HalTestTool::InitSdCardHal(std::shared_ptr<LinuxTest> &mock)
|
||||||
|
{
|
||||||
|
std::shared_ptr<SdCardHalMock> sdCardHal = std::dynamic_pointer_cast<SdCardHalMock>(mSdCardHal);
|
||||||
|
if (nullptr == sdCardHal) {
|
||||||
|
LogError("SdCardHalMock is null.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sdCardHal->SetLinuxTest(mock);
|
||||||
}
|
}
|
||||||
void HalTestTool::UnInit(void)
|
void HalTestTool::UnInit(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,3 +13,21 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
#include "SdCardHalMock.h"
|
#include "SdCardHalMock.h"
|
||||||
|
#include "ILog.h"
|
||||||
|
extern const char *SD_CARD_DEVICE;
|
||||||
|
SdCardHalMock::SdCardHalMock()
|
||||||
|
{
|
||||||
|
mDevFd = -1;
|
||||||
|
}
|
||||||
|
void SdCardHalMock::SetLinuxTest(std::shared_ptr<LinuxTest> &mock)
|
||||||
|
{
|
||||||
|
constexpr int FSTAT_SUCCESS = 0;
|
||||||
|
mLinuxTest = mock;
|
||||||
|
mDevFd = mLinuxTest->GetHandleForMock();
|
||||||
|
EXPECT_CALL(*mock.get(), fx_open(SD_CARD_DEVICE, _)).WillRepeatedly(DoAll((Return(mDevFd))));
|
||||||
|
auto fstatFunc = [=, &mock](int fd, struct stat *statbuf) {
|
||||||
|
statbuf->st_mode = S_IFBLK | 0600;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mock.get(), fx_fstat(mDevFd, _))
|
||||||
|
.WillRepeatedly(DoAll(WithArgs<0, 1>(Invoke(fstatFunc)), Return(FSTAT_SUCCESS)));
|
||||||
|
}
|
|
@ -15,11 +15,17 @@
|
||||||
#ifndef SD_CARD_HAL_MOCK_H
|
#ifndef SD_CARD_HAL_MOCK_H
|
||||||
#define SD_CARD_HAL_MOCK_H
|
#define SD_CARD_HAL_MOCK_H
|
||||||
#include "IHalCpp.h"
|
#include "IHalCpp.h"
|
||||||
|
#include "LinuxApiMock.h"
|
||||||
#include "SdCardHal.h"
|
#include "SdCardHal.h"
|
||||||
class SdCardHalMock : public SdCardHal
|
class SdCardHalMock : public SdCardHal
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SdCardHalMock() = default;
|
SdCardHalMock();
|
||||||
virtual ~SdCardHalMock() = default;
|
virtual ~SdCardHalMock() = default;
|
||||||
|
void SetLinuxTest(std::shared_ptr<LinuxTest> &mock);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<LinuxTest> mLinuxTest;
|
||||||
|
int mDevFd;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
|
@ -17,6 +17,7 @@ include_directories(
|
||||||
${MIDDLEWARE_SOURCE_PATH}/AppManager/src
|
${MIDDLEWARE_SOURCE_PATH}/AppManager/src
|
||||||
${TEST_SOURCE_PATH}
|
${TEST_SOURCE_PATH}
|
||||||
${TEST_SOURCE_PATH}/hal/tool/include
|
${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/googletest/include
|
||||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
|
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
|
||||||
)
|
)
|
||||||
|
|
|
@ -15,6 +15,7 @@ include_directories(
|
||||||
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src
|
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src
|
||||||
${TEST_SOURCE_PATH}
|
${TEST_SOURCE_PATH}
|
||||||
${TEST_SOURCE_PATH}/hal/tool/include
|
${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/googletest/include
|
||||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
|
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
|
||||||
)
|
)
|
||||||
|
|
|
@ -16,6 +16,7 @@ include_directories(
|
||||||
${MIDDLEWARE_SOURCE_PATH}/MediaManager/include
|
${MIDDLEWARE_SOURCE_PATH}/MediaManager/include
|
||||||
${TEST_SOURCE_PATH}
|
${TEST_SOURCE_PATH}
|
||||||
${TEST_SOURCE_PATH}/hal/tool/include
|
${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/googletest/include
|
||||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
|
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
|
||||||
)
|
)
|
||||||
|
|
|
@ -23,39 +23,16 @@ class LinuxApiMock
|
||||||
public:
|
public:
|
||||||
LinuxApiMock() = default;
|
LinuxApiMock() = default;
|
||||||
virtual ~LinuxApiMock() = default;
|
virtual ~LinuxApiMock() = default;
|
||||||
static std::shared_ptr<LinuxApiMock> &GetInstance(std::shared_ptr<LinuxApiMock> *impl = nullptr)
|
static std::shared_ptr<LinuxApiMock> &GetInstance(std::shared_ptr<LinuxApiMock> *impl = nullptr);
|
||||||
{
|
virtual void Init();
|
||||||
static auto instance = std::make_shared<LinuxApiMock>();
|
virtual void UnInit();
|
||||||
if (impl) {
|
|
||||||
// The non-thread-safe changing is only for gtest.
|
|
||||||
instance = *impl;
|
|
||||||
}
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
virtual void Init()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
virtual void UnInit()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
virtual int fx_open(const char *pathname, int flags);
|
virtual int fx_open(const char *pathname, int flags);
|
||||||
virtual int fx_tcgetattr(int fd, struct termios *termios_p);
|
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 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_write(int fd, const void *buf, size_t count);
|
||||||
virtual ssize_t fx_read(int fd, 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_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
||||||
|
virtual int fx_fstat(int fd, struct stat *statbuf);
|
||||||
public:
|
|
||||||
/**
|
|
||||||
* @brief
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
// virtual void ApiLock(void) {}
|
|
||||||
/**
|
|
||||||
* @brief
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
// virtual void ApiUnlock(void) {}
|
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* A simulation interface class used for automated testing in Ubuntu systems, implementing the function of piling on
|
* A simulation interface class used for automated testing in Ubuntu systems, implementing the function of piling on
|
||||||
|
@ -72,17 +49,17 @@ public:
|
||||||
MOCK_METHOD3(fx_write, ssize_t(int, const void *, size_t));
|
MOCK_METHOD3(fx_write, ssize_t(int, const void *, size_t));
|
||||||
MOCK_METHOD3(fx_read, ssize_t(int, 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_METHOD5(fx_select, int(int, fd_set *, fd_set *, fd_set *, struct timeval *));
|
||||||
|
MOCK_METHOD2(fx_fstat, int(int, struct stat *));
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Get the Handle For Mock object
|
* @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
|
* @return int
|
||||||
*/
|
*/
|
||||||
virtual int GetHandleForMock(void)
|
virtual int GetHandleForMock(void);
|
||||||
{
|
|
||||||
return INVALID_HANDLE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static std::shared_ptr<LinuxTest> CreateLinuxTest(void);
|
static std::shared_ptr<LinuxTest> CreateLinuxTest(void);
|
||||||
|
|
|
@ -16,6 +16,21 @@
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include "LinuxTestImpl.h"
|
#include "LinuxTestImpl.h"
|
||||||
#include "WrapApi.h"
|
#include "WrapApi.h"
|
||||||
|
std::shared_ptr<LinuxApiMock> &LinuxApiMock::GetInstance(std::shared_ptr<LinuxApiMock> *impl)
|
||||||
|
{
|
||||||
|
static auto instance = std::make_shared<LinuxApiMock>();
|
||||||
|
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)
|
int LinuxApiMock::fx_open(const char *pathname, int flags)
|
||||||
{
|
{
|
||||||
// LogInfo("Call the real api.\n");
|
// LogInfo("Call the real api.\n");
|
||||||
|
@ -45,9 +60,17 @@ int LinuxApiMock::fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set
|
||||||
// LogInfo("Call the real api.\n");
|
// LogInfo("Call the real api.\n");
|
||||||
return __real_fx_select(nfds, readfds, writefds, exceptfds, timeout);
|
return __real_fx_select(nfds, readfds, writefds, exceptfds, timeout);
|
||||||
}
|
}
|
||||||
|
int LinuxApiMock::fx_fstat(int fd, struct stat *statbuf)
|
||||||
|
{
|
||||||
|
return __real_fx_fstat(fd, statbuf);
|
||||||
|
}
|
||||||
std::shared_ptr<LinuxTest> LinuxTest::CreateLinuxTest(void)
|
std::shared_ptr<LinuxTest> LinuxTest::CreateLinuxTest(void)
|
||||||
{
|
{
|
||||||
std::shared_ptr<LinuxTest> test = std::make_shared<LinuxTestImpl>();
|
std::shared_ptr<LinuxTest> test = std::make_shared<LinuxTestImpl>();
|
||||||
LinuxTestImpl::ApiInit(test);
|
LinuxTestImpl::ApiInit(test);
|
||||||
return test;
|
return test;
|
||||||
}
|
}
|
||||||
|
int LinuxTest::GetHandleForMock(void)
|
||||||
|
{
|
||||||
|
return INVALID_HANDLE;
|
||||||
|
}
|
|
@ -91,6 +91,18 @@ void LinuxTestImpl::ApiInit(std::shared_ptr<LinuxTest> &mock)
|
||||||
WithArgs<0, 1, 2, 3, 4>(Invoke(api_select)),
|
WithArgs<0, 1, 2, 3, 4>(Invoke(api_select)),
|
||||||
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
|
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
|
||||||
ReturnPointee(&selectResult)));
|
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<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiLock),
|
||||||
|
WithArgs<0, 1>(Invoke(api_fstat)),
|
||||||
|
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
|
||||||
|
ReturnPointee(&fstatResult)));
|
||||||
}
|
}
|
||||||
void LinuxTestImpl::Init()
|
void LinuxTestImpl::Init()
|
||||||
{
|
{
|
||||||
|
|
|
@ -41,6 +41,11 @@ int __wrap_fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *except
|
||||||
{
|
{
|
||||||
return LinuxApiMock::GetInstance()->fx_select(nfds, readfds, writefds, exceptfds, timeout);
|
return LinuxApiMock::GetInstance()->fx_select(nfds, readfds, writefds, exceptfds, timeout);
|
||||||
}
|
}
|
||||||
|
int __wrap_fx_fstat(int fd, struct stat *statbuf)
|
||||||
|
{
|
||||||
|
// TODO: 在此处加锁,优化打桩时,接口需要调用真实接口并返回真实接口的返回值返回全局变量的多线程安全问题。
|
||||||
|
return LinuxApiMock::GetInstance()->fx_fstat(fd, statbuf);
|
||||||
|
}
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
|
@ -28,6 +28,7 @@ int __real_fx_tcsetattr(int fd, int optional_actions, const struct termios *term
|
||||||
ssize_t __real_fx_write(int fd, const void *buf, size_t count);
|
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);
|
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_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
||||||
|
int __real_fx_fstat(int fd, struct stat *statbuf);
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#define LINUX_API_H
|
#define LINUX_API_H
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
@ -30,6 +31,7 @@ int fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
|
||||||
ssize_t fx_write(int fd, const void *buf, size_t count);
|
ssize_t fx_write(int fd, const void *buf, size_t count);
|
||||||
ssize_t fx_read(int fd, void *buf, size_t count);
|
ssize_t fx_read(int fd, void *buf, size_t count);
|
||||||
int fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
int fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
||||||
|
int fx_fstat(int fd, struct stat *statbuf);
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -50,3 +50,7 @@ int fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, st
|
||||||
{
|
{
|
||||||
return select(nfds, readfds, writefds, exceptfds, timeout);
|
return select(nfds, readfds, writefds, exceptfds, timeout);
|
||||||
}
|
}
|
||||||
|
int fx_fstat(int fd, struct stat *statbuf)
|
||||||
|
{
|
||||||
|
return fstat(fd, statbuf);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user