UartOpen function test.

This commit is contained in:
fancy 2024-01-25 15:38:02 -08:00
parent b99d2c9461
commit c6ab66d3ec
13 changed files with 90 additions and 25 deletions

View File

@ -10,8 +10,8 @@ execute_process(COMMAND mv ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11
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=tcgetattr" CACHE STRING INTERNAL FORCE)
# set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=tcsetattr" 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=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)

View File

@ -3,14 +3,6 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#ifdef __cplusplus
extern "C" {
#endif
// Modify all the real api.
int __real_fx_open(const char *pathname, int flags);
#ifdef __cplusplus
}
#endif
constexpr int INVALID_HANDLE = -1;
class LinuxApiMock
{
@ -29,6 +21,8 @@ public:
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);
};
/**
* A simulation interface class used for automated testing in Ubuntu systems, implementing the function of piling on
@ -40,6 +34,8 @@ 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 *));
public:
/**

View File

@ -29,7 +29,7 @@ public:
private:
/**
* @brief
* @brief Ensure that false handles of the test simulation interface are not duplicated.
*
*/
int mFdMax;

View File

@ -1,15 +1,12 @@
#include "LinuxApiMock.h"
#include "ILog.h"
#include "LinuxTestImpl.h"
#ifdef __cplusplus
extern "C" {
#endif
// Modify all the wrap api.
int __wrap_fx_open(const char *pathname, int flags) { return LinuxApiMock::GetInstance()->fx_open(pathname, flags); }
#ifdef __cplusplus
}
#endif
int LinuxApiMock::fx_open(const char *pathname, int flags) { return __real_fx_open(pathname, flags); }
int LinuxApiMock::fx_tcgetattr(int fd, struct termios *termios_p) { 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);
}
std::shared_ptr<LinuxTest> LinuxTest::CreateLinuxTest(void)
{

View File

@ -21,7 +21,23 @@ void LinuxTestImpl::ApiInit(std::shared_ptr<LinuxTest> &mock)
LogInfo("Call __real_fx_open, pathname = %s.\n", pathname);
openFd = __real_fx_open(pathname, flags);
};
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);
};
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_open(::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::WithArgs<0, 1>(::testing::Invoke(api_open)),
::testing::ReturnPointee(&openFd)));
EXPECT_CALL(*mock.get(), fx_tcgetattr(::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::WithArgs<0, 1>(::testing::Invoke(api_tcgetattr)),
::testing::ReturnPointee(&resultTcgetattr)));
EXPECT_CALL(*mock.get(), fx_tcsetattr(::testing::_, ::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::WithArgs<0, 1, 2>(::testing::Invoke(api_tcsetattr)),
::testing::ReturnPointee(&resultTcsetattr)));
}

View File

@ -16,6 +16,7 @@
#define LINUX_TEST_IMPL_H
#include "HandleManager.h"
#include "LinuxApiMock.h"
#include "WrapApi.h"
class LinuxTestImpl : public HandleManager
{
public:

View File

@ -0,0 +1,17 @@
#include "WrapApi.h"
#include "LinuxApiMock.h"
#ifdef __cplusplus
extern "C" {
#endif
int __wrap_fx_open(const char *pathname, int flags) { return LinuxApiMock::GetInstance()->fx_open(pathname, flags); }
int __wrap_fx_tcgetattr(int fd, struct termios *termios_p)
{
return LinuxApiMock::GetInstance()->fx_tcgetattr(fd, termios_p);
}
int __wrap_fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
{
return LinuxApiMock::GetInstance()->fx_tcsetattr(fd, optional_actions, termios_p);
}
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,27 @@
/*
* 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
#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);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -51,13 +51,10 @@ public:
public:
std::shared_ptr<LinuxTest> mLinuxTest;
// std::shared_ptr<LinuxApiMock> mLinuxApiMock;
};
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.Demo
TEST_F(UartDeviceMockTest, Demo)
{
// EXPECT_CALL(*mLinuxTest.get(), fx_open(deviceName, ::testing::_))
// .WillRepeatedly(::testing::DoAll((::testing::Return(1000000000))));
void *object = CreateUartDevice(gUartDevice);
IUartOpen(object);
// IUartSend(object, nullptr, 0);

View File

@ -16,8 +16,14 @@
#include "ILog.h"
void UartDeviceTestTool::UartDeviceDefaultInit(std::shared_ptr<LinuxTest> &mock, const uart_info &uart)
{
constexpr int TCGETATTR_SUCCEED = 0;
constexpr int TCSETATTR_SUCCEED = 0;
int uartFd = mock->GetHandleForMock();
LogInfo("uartFd = %d\n", uartFd);
EXPECT_CALL(*mock.get(), fx_open(uart.mDevice, ::testing::_))
.WillRepeatedly(::testing::DoAll((::testing::Return(uartFd))));
EXPECT_CALL(*mock.get(), fx_tcgetattr(uartFd, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(TCGETATTR_SUCCEED)));
EXPECT_CALL(*mock.get(), fx_tcsetattr(uartFd, ::testing::_, ::testing::_))
.WillRepeatedly(::testing::DoAll(::testing::Return(TCSETATTR_SUCCEED)));
}

View File

@ -14,11 +14,14 @@
*/
#ifndef LINUX_API_H
#define LINUX_API_H
#include <termios.h>
#ifdef __cplusplus
extern "C" {
#endif
int fx_system(const char *command);
int fx_open(const char *pathname, int flags);
int fx_tcgetattr(int fd, struct termios *termios_p);
int fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
#ifdef __cplusplus
}
#endif

View File

@ -23,4 +23,9 @@
#include <unistd.h>
int fx_system(const char *command) { return system(command); }
int fx_open(const char *pathname, int flags) { return open(pathname, flags); }
int fx_open(const char *pathname, int flags) { return open(pathname, flags); }
int fx_tcgetattr(int fd, struct termios *termios_p) { return tcgetattr(fd, termios_p); }
int fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
{
return tcsetattr(fd, optional_actions, termios_p);
}

View File

@ -104,7 +104,7 @@ const StatusCode UartDeviceImpl::SetConfig(void)
1152000, 1000000, 921600, 576000, 500000, 460800, 230400, 115200, 19200, 9600, 4800, 2400, 1200, 300};
struct termios options;
if (tcgetattr(mFd, &options) != 0) {
if (fx_tcgetattr(mFd, &options) != 0) {
perror("SetupSerial 1");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
@ -203,7 +203,7 @@ const StatusCode UartDeviceImpl::SetConfig(void)
options.c_cc[VMIN] = 1;
tcflush(mFd, TCIFLUSH);
// set the attribute to HiSerial device
if (tcsetattr(mFd, TCSANOW, &options) != 0) {
if (fx_tcsetattr(mFd, TCSANOW, &options) != 0) {
perror("com set error!\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}