nt9856x/code/application/sifarsdk/test/test_utils/linux_mock/LinuxApiTest.cpp

75 lines
3.3 KiB
C++

#include "LinuxApiTest.h"
#include "Log.h"
void FILE_Mock::FileFopen(void)
{
mOnlyForTest = new int(0);
// if (mPathname)
// {
// mFile = __real_fopen(mPathname, mMode);
// }
}
int FILE_Mock::FileClose(void)
{
// if (mFile)
// {
// return __real_fclose(mFile);
// }
delete mOnlyForTest;
mOnlyForTest = nullptr;
return SUCCESSFUL;
}
LinuxApiTest::LinuxApiTest()
{
mOpenFd[NETWORK_MODULE_USB_NODE] = OpenFd::USB_FD;
mOpenFd[MCU_DEVICE_PATH_MOCK] = OpenFd::MCU_FD;
}
const int LinuxApiTest::CreateOpenFd(const char *pathname)
{
LogInfo("CreateOpenFd path = %s\n", pathname);
int fd = static_cast<int>(mOpenFd[pathname]);
return fd;
}
FILE *LinuxApiTest::CreateMockFile(const char *pathname, const char *mode)
{
LogInfo("LinuxApiTest::CreateMockFile, pathname = %s\n", pathname);
FILE_Mock *file = new FILE_Mock(pathname, mode);
file->FileFopen();
return (FILE *)file;
}
void LinuxApiTest::DestroyMockFile(FILE *file)
{
FILE_Mock *MockFile = (FILE_Mock *)file;
MockFile->FileClose();
delete MockFile;
}
void LinuxApiTest::DefaultApiInit(std::shared_ptr<LinuxApiTest> &mock)
{
EXPECT_CALL(*mock.get(), fopen(::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(nullptr)));
EXPECT_CALL(*mock.get(), fclose(::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(FAILED)));
EXPECT_CALL(*mock.get(), fprintf_gpio(::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(FPRINTF_LENGTH_DEFAULT)));
EXPECT_CALL(*mock.get(), fprintf_dir(::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(FPRINTF_LENGTH_DEFAULT)));
EXPECT_CALL(*mock.get(), fread(::testing::_, ::testing::_, ::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(0)));
EXPECT_CALL(*mock.get(), open(::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(DONOT_USE_MOCK_OPEN)));
EXPECT_CALL(*mock.get(), write(::testing::_, ::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(MOCK_WRITE_FAILED)));
EXPECT_CALL(*mock.get(), close(::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(DONOT_USE_MOCK_OPEN)));
EXPECT_CALL(*mock.get(), read(::testing::_, ::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(MOCK_READ_NOTHING)));
EXPECT_CALL(*mock.get(), read_block(::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(false)));
EXPECT_CALL(*mock.get(), select(::testing::_, ::testing::_, ::testing::_, ::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(MOCK_SELECT_CALL_REAL_SELECT)));
EXPECT_CALL(*mock.get(), gethostbyname(::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return((struct hostent *)DONOT_USE_MOCK_GETHOSTBYNAME)));
EXPECT_CALL(*mock.get(), connect(::testing::_, ::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(DONOT_USE_MOCK_CONNECT)));
EXPECT_CALL(*mock.get(), socket(::testing::_, ::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(DONOT_USE_MOCK_SOCKET)));
}