UartOpen function test.
This commit is contained in:
parent
b99d2c9461
commit
c6ab66d3ec
|
@ -10,8 +10,8 @@ execute_process(COMMAND mv ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11
|
||||||
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=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=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=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=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=socket" CACHE STRING INTERNAL FORCE)
|
||||||
|
|
|
@ -3,14 +3,6 @@
|
||||||
#include <gmock/gmock.h>
|
#include <gmock/gmock.h>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <memory>
|
#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;
|
constexpr int INVALID_HANDLE = -1;
|
||||||
class LinuxApiMock
|
class LinuxApiMock
|
||||||
{
|
{
|
||||||
|
@ -29,6 +21,8 @@ public:
|
||||||
virtual void Init() {}
|
virtual void Init() {}
|
||||||
virtual void UnInit() {}
|
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_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
|
* A simulation interface class used for automated testing in Ubuntu systems, implementing the function of piling on
|
||||||
|
@ -40,6 +34,8 @@ public:
|
||||||
LinuxTest() = default;
|
LinuxTest() = default;
|
||||||
virtual ~LinuxTest() = default;
|
virtual ~LinuxTest() = default;
|
||||||
MOCK_METHOD2(fx_open, int(const char *, int));
|
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:
|
public:
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -29,7 +29,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @brief Ensure that false handles of the test simulation interface are not duplicated.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int mFdMax;
|
int mFdMax;
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
#include "LinuxApiMock.h"
|
#include "LinuxApiMock.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include "LinuxTestImpl.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_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)
|
std::shared_ptr<LinuxTest> LinuxTest::CreateLinuxTest(void)
|
||||||
{
|
{
|
||||||
|
|
|
@ -21,7 +21,23 @@ void LinuxTestImpl::ApiInit(std::shared_ptr<LinuxTest> &mock)
|
||||||
LogInfo("Call __real_fx_open, pathname = %s.\n", pathname);
|
LogInfo("Call __real_fx_open, pathname = %s.\n", pathname);
|
||||||
openFd = __real_fx_open(pathname, flags);
|
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::_))
|
EXPECT_CALL(*mock.get(), fx_open(::testing::_, ::testing::_))
|
||||||
.WillRepeatedly(::testing::DoAll(::testing::WithArgs<0, 1>(::testing::Invoke(api_open)),
|
.WillRepeatedly(::testing::DoAll(::testing::WithArgs<0, 1>(::testing::Invoke(api_open)),
|
||||||
::testing::ReturnPointee(&openFd)));
|
::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)));
|
||||||
}
|
}
|
|
@ -16,6 +16,7 @@
|
||||||
#define LINUX_TEST_IMPL_H
|
#define LINUX_TEST_IMPL_H
|
||||||
#include "HandleManager.h"
|
#include "HandleManager.h"
|
||||||
#include "LinuxApiMock.h"
|
#include "LinuxApiMock.h"
|
||||||
|
#include "WrapApi.h"
|
||||||
class LinuxTestImpl : public HandleManager
|
class LinuxTestImpl : public HandleManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
17
test/utils/LinuxApiMock/src/WrapApi.cpp
Normal file
17
test/utils/LinuxApiMock/src/WrapApi.cpp
Normal 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
|
27
test/utils/LinuxApiMock/src/WrapApi.h
Normal file
27
test/utils/LinuxApiMock/src/WrapApi.h
Normal 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
|
|
@ -51,13 +51,10 @@ public:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::shared_ptr<LinuxTest> mLinuxTest;
|
std::shared_ptr<LinuxTest> mLinuxTest;
|
||||||
// std::shared_ptr<LinuxApiMock> mLinuxApiMock;
|
|
||||||
};
|
};
|
||||||
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.Demo
|
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.Demo
|
||||||
TEST_F(UartDeviceMockTest, Demo)
|
TEST_F(UartDeviceMockTest, Demo)
|
||||||
{
|
{
|
||||||
// EXPECT_CALL(*mLinuxTest.get(), fx_open(deviceName, ::testing::_))
|
|
||||||
// .WillRepeatedly(::testing::DoAll((::testing::Return(1000000000))));
|
|
||||||
void *object = CreateUartDevice(gUartDevice);
|
void *object = CreateUartDevice(gUartDevice);
|
||||||
IUartOpen(object);
|
IUartOpen(object);
|
||||||
// IUartSend(object, nullptr, 0);
|
// IUartSend(object, nullptr, 0);
|
||||||
|
|
|
@ -16,8 +16,14 @@
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
void UartDeviceTestTool::UartDeviceDefaultInit(std::shared_ptr<LinuxTest> &mock, const uart_info &uart)
|
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();
|
int uartFd = mock->GetHandleForMock();
|
||||||
LogInfo("uartFd = %d\n", uartFd);
|
LogInfo("uartFd = %d\n", uartFd);
|
||||||
EXPECT_CALL(*mock.get(), fx_open(uart.mDevice, ::testing::_))
|
EXPECT_CALL(*mock.get(), fx_open(uart.mDevice, ::testing::_))
|
||||||
.WillRepeatedly(::testing::DoAll((::testing::Return(uartFd))));
|
.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)));
|
||||||
}
|
}
|
|
@ -14,11 +14,14 @@
|
||||||
*/
|
*/
|
||||||
#ifndef LINUX_API_H
|
#ifndef LINUX_API_H
|
||||||
#define LINUX_API_H
|
#define LINUX_API_H
|
||||||
|
#include <termios.h>
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
int fx_system(const char *command);
|
int fx_system(const char *command);
|
||||||
int fx_open(const char *pathname, int flags);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -24,3 +24,8 @@
|
||||||
|
|
||||||
int fx_system(const char *command) { return system(command); }
|
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);
|
||||||
|
}
|
|
@ -104,7 +104,7 @@ const StatusCode UartDeviceImpl::SetConfig(void)
|
||||||
1152000, 1000000, 921600, 576000, 500000, 460800, 230400, 115200, 19200, 9600, 4800, 2400, 1200, 300};
|
1152000, 1000000, 921600, 576000, 500000, 460800, 230400, 115200, 19200, 9600, 4800, 2400, 1200, 300};
|
||||||
struct termios options;
|
struct termios options;
|
||||||
|
|
||||||
if (tcgetattr(mFd, &options) != 0) {
|
if (fx_tcgetattr(mFd, &options) != 0) {
|
||||||
perror("SetupSerial 1");
|
perror("SetupSerial 1");
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
|
@ -203,7 +203,7 @@ const StatusCode UartDeviceImpl::SetConfig(void)
|
||||||
options.c_cc[VMIN] = 1;
|
options.c_cc[VMIN] = 1;
|
||||||
tcflush(mFd, TCIFLUSH);
|
tcflush(mFd, TCIFLUSH);
|
||||||
// set the attribute to HiSerial device
|
// set the attribute to HiSerial device
|
||||||
if (tcsetattr(mFd, TCSANOW, &options) != 0) {
|
if (fx_tcsetattr(mFd, TCSANOW, &options) != 0) {
|
||||||
perror("com set error!\n");
|
perror("com set error!\n");
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user