From c6ab66d3ec379999732f14151c2113396a81e3f0 Mon Sep 17 00:00:00 2001 From: fancy <258828110.@qq.com> Date: Thu, 25 Jan 2024 15:38:02 -0800 Subject: [PATCH] UartOpen function test. --- test/CMakeLists.txt | 4 +-- .../utils/LinuxApiMock/include/LinuxApiMock.h | 12 +++------ test/utils/LinuxApiMock/src/HandleManager.h | 2 +- test/utils/LinuxApiMock/src/LinuxApiMock.cpp | 13 ++++----- test/utils/LinuxApiMock/src/LinuxTestImpl.cpp | 16 +++++++++++ test/utils/LinuxApiMock/src/LinuxTestImpl.h | 1 + test/utils/LinuxApiMock/src/WrapApi.cpp | 17 ++++++++++++ test/utils/LinuxApiMock/src/WrapApi.h | 27 +++++++++++++++++++ .../src_mock/UartDevice_Mock_Test.cpp | 3 --- .../tool/src/UartDeviceTestTool.cpp | 6 +++++ utils/LinuxApi/include/LinuxApi.h | 3 +++ utils/LinuxApi/src/LinuxApi.c | 7 ++++- utils/UartDevice/src/UartDeviceImpl.cpp | 4 +-- 13 files changed, 90 insertions(+), 25 deletions(-) create mode 100644 test/utils/LinuxApiMock/src/WrapApi.cpp create mode 100644 test/utils/LinuxApiMock/src/WrapApi.h diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6fe33be..fb2e446 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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) diff --git a/test/utils/LinuxApiMock/include/LinuxApiMock.h b/test/utils/LinuxApiMock/include/LinuxApiMock.h index b06c5f2..f79f3f4 100644 --- a/test/utils/LinuxApiMock/include/LinuxApiMock.h +++ b/test/utils/LinuxApiMock/include/LinuxApiMock.h @@ -3,14 +3,6 @@ #include #include #include -#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: /** diff --git a/test/utils/LinuxApiMock/src/HandleManager.h b/test/utils/LinuxApiMock/src/HandleManager.h index 914953c..6697cbe 100644 --- a/test/utils/LinuxApiMock/src/HandleManager.h +++ b/test/utils/LinuxApiMock/src/HandleManager.h @@ -29,7 +29,7 @@ public: private: /** - * @brief + * @brief Ensure that false handles of the test simulation interface are not duplicated. * */ int mFdMax; diff --git a/test/utils/LinuxApiMock/src/LinuxApiMock.cpp b/test/utils/LinuxApiMock/src/LinuxApiMock.cpp index 14c08b1..d957f24 100644 --- a/test/utils/LinuxApiMock/src/LinuxApiMock.cpp +++ b/test/utils/LinuxApiMock/src/LinuxApiMock.cpp @@ -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::CreateLinuxTest(void) { diff --git a/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp b/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp index 568ed9d..d87b445 100644 --- a/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp +++ b/test/utils/LinuxApiMock/src/LinuxTestImpl.cpp @@ -21,7 +21,23 @@ void LinuxTestImpl::ApiInit(std::shared_ptr &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))); } \ No newline at end of file diff --git a/test/utils/LinuxApiMock/src/LinuxTestImpl.h b/test/utils/LinuxApiMock/src/LinuxTestImpl.h index 39a1d1b..1496576 100644 --- a/test/utils/LinuxApiMock/src/LinuxTestImpl.h +++ b/test/utils/LinuxApiMock/src/LinuxTestImpl.h @@ -16,6 +16,7 @@ #define LINUX_TEST_IMPL_H #include "HandleManager.h" #include "LinuxApiMock.h" +#include "WrapApi.h" class LinuxTestImpl : public HandleManager { public: diff --git a/test/utils/LinuxApiMock/src/WrapApi.cpp b/test/utils/LinuxApiMock/src/WrapApi.cpp new file mode 100644 index 0000000..78869c2 --- /dev/null +++ b/test/utils/LinuxApiMock/src/WrapApi.cpp @@ -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 \ 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..dc819c1 --- /dev/null +++ b/test/utils/LinuxApiMock/src/WrapApi.h @@ -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 \ 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 index d4f5b88..80e1cd2 100644 --- a/test/utils/UartDevice/src_mock/UartDevice_Mock_Test.cpp +++ b/test/utils/UartDevice/src_mock/UartDevice_Mock_Test.cpp @@ -51,13 +51,10 @@ public: public: std::shared_ptr mLinuxTest; - // std::shared_ptr 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); diff --git a/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp b/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp index 4a733d8..d018dfc 100644 --- a/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp +++ b/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp @@ -16,8 +16,14 @@ #include "ILog.h" void UartDeviceTestTool::UartDeviceDefaultInit(std::shared_ptr &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))); } \ No newline at end of file diff --git a/utils/LinuxApi/include/LinuxApi.h b/utils/LinuxApi/include/LinuxApi.h index c367562..3421b47 100644 --- a/utils/LinuxApi/include/LinuxApi.h +++ b/utils/LinuxApi/include/LinuxApi.h @@ -14,11 +14,14 @@ */ #ifndef LINUX_API_H #define LINUX_API_H +#include #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 diff --git a/utils/LinuxApi/src/LinuxApi.c b/utils/LinuxApi/src/LinuxApi.c index bb565c7..8d93ec1 100644 --- a/utils/LinuxApi/src/LinuxApi.c +++ b/utils/LinuxApi/src/LinuxApi.c @@ -23,4 +23,9 @@ #include int fx_system(const char *command) { return system(command); } -int fx_open(const char *pathname, int flags) { return open(pathname, flags); } \ No newline at end of file +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); +} \ No newline at end of file diff --git a/utils/UartDevice/src/UartDeviceImpl.cpp b/utils/UartDevice/src/UartDeviceImpl.cpp index cb1aa05..c7b1809 100644 --- a/utils/UartDevice/src/UartDeviceImpl.cpp +++ b/utils/UartDevice/src/UartDeviceImpl.cpp @@ -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); }