202 lines
7.1 KiB
C++
202 lines
7.1 KiB
C++
/*
|
|
* 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.
|
|
*/
|
|
#include "ILog.h"
|
|
#include "LinuxApiMock.h"
|
|
#include "UartDevice.h"
|
|
#include "UartDeviceTestTool.h"
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
#include <thread>
|
|
namespace UartDeviceMockTest
|
|
{
|
|
const char *gDeviceName = "dev/s1";
|
|
static UartInfo gUartDevice = {
|
|
gDeviceName,
|
|
1152000,
|
|
'N',
|
|
8,
|
|
1,
|
|
'N',
|
|
};
|
|
class UartDeviceMockTest : public testing::Test, public UartDeviceTestTool
|
|
{
|
|
public:
|
|
UartDeviceMockTest()
|
|
{
|
|
}
|
|
virtual ~UartDeviceMockTest()
|
|
{
|
|
}
|
|
static void SetUpTestCase()
|
|
{
|
|
CreateLogModule();
|
|
ILogInit(LOG_INSTANCE_TYPE_END);
|
|
}
|
|
static void TearDownTestCase()
|
|
{
|
|
ILogUnInit();
|
|
}
|
|
virtual void SetUp()
|
|
{
|
|
mLinuxTest = LinuxTest::CreateLinuxTest();
|
|
std::shared_ptr<LinuxApiMock> test = mLinuxTest;
|
|
LinuxApiMock::GetInstance(&test);
|
|
LinuxApiMock::GetInstance()->Init();
|
|
RegisterUartDevice(mLinuxTest, gUartDevice);
|
|
}
|
|
virtual void TearDown()
|
|
{
|
|
LinuxApiMock::GetInstance()->UnInit();
|
|
mLinuxTest = std::make_shared<LinuxTest>();
|
|
std::shared_ptr<LinuxApiMock> test = std::make_shared<LinuxApiMock>();
|
|
LinuxApiMock::GetInstance(&test);
|
|
UnregisterUartDevice(gUartDevice);
|
|
}
|
|
|
|
public:
|
|
std::shared_ptr<LinuxTest> mLinuxTest;
|
|
};
|
|
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_EXAMPLE_AUTO_Demo
|
|
TEST_F(UartDeviceMockTest, UNIT_UartDevice_EXAMPLE_AUTO_Demo)
|
|
{
|
|
void *object = CreateUartDevice(gUartDevice);
|
|
IUartOpen(object);
|
|
const char *SEND_BUF = "TEST";
|
|
ssize_t sendResult = IUartSend(object, SEND_BUF, strlen(SEND_BUF));
|
|
EXPECT_EQ(sendResult, static_cast<ssize_t>(strlen(SEND_BUF)));
|
|
constexpr int RECV_TIME_OUT_MS = 1000;
|
|
constexpr int RECV_BUF_LENGTH = 256;
|
|
char recvBuf[RECV_BUF_LENGTH] = {0};
|
|
ssize_t recvResult = IUartRecv(object, recvBuf, RECV_BUF_LENGTH, RECV_TIME_OUT_MS);
|
|
LogInfo("recvResult = %d\n", recvResult);
|
|
IUartTcflush(object);
|
|
IUartDeviceFree(object);
|
|
}
|
|
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_ParameterEarror
|
|
/**
|
|
* @brief Construct a new test f object
|
|
* Is the interface accurate when testing parameter errors in this test case.
|
|
*/
|
|
TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_ParameterEarror)
|
|
{
|
|
char *deviceName = nullptr;
|
|
static UartInfo uartDevice = {
|
|
deviceName,
|
|
1152000,
|
|
'N',
|
|
8,
|
|
1,
|
|
'N',
|
|
};
|
|
RegisterUartDevice(mLinuxTest, uartDevice);
|
|
void *object = CreateUartDevice(uartDevice);
|
|
IUartOpen(object);
|
|
IUartDeviceFree(object);
|
|
EXPECT_EQ(object, nullptr);
|
|
UnregisterUartDevice(uartDevice);
|
|
}
|
|
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_ParameterEarror2
|
|
/**
|
|
* @brief Construct a new test f object
|
|
* Is the interface accurate when testing parameter errors in this test case.
|
|
*/
|
|
TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_ParameterEarror2)
|
|
{
|
|
// TODO:
|
|
// const char *SEND_BUF = "TEST";
|
|
// char *deviceName = (char *)malloc(strlen(SEND_BUF) + 1);
|
|
// memset(deviceName, 0, strlen(SEND_BUF) + 1);
|
|
// static UartInfo uartDevice = {
|
|
// "deviceName",
|
|
// 1152000,
|
|
// 'N',
|
|
// 8,
|
|
// 1,
|
|
// 'N',
|
|
// };
|
|
// RegisterUartDevice(mLinuxTest, uartDevice);
|
|
// void *object = CreateUartDevice(uartDevice);
|
|
// IUartOpen(object);
|
|
// IUartDeviceFree(object);
|
|
// free(deviceName);
|
|
// deviceName = nullptr;
|
|
}
|
|
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_WriteAllData
|
|
/**
|
|
* @brief Construct a new test f object
|
|
* This test case verifies whether the application will call the write function twice until all data is written when the
|
|
* write function does not write the length of the data.
|
|
*/
|
|
TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_WriteAllData)
|
|
{
|
|
const char *SEND_BUF = "TEST";
|
|
constexpr ssize_t SEND_LENGTH = 1; // less than static_cast<ssize_t>(strlen(SEND_BUF))
|
|
SetSendApiOnce(mLinuxTest, gUartDevice, SEND_LENGTH);
|
|
void *object = CreateUartDevice(gUartDevice);
|
|
IUartOpen(object);
|
|
ssize_t sendResult = IUartSend(object, SEND_BUF, strlen(SEND_BUF));
|
|
EXPECT_EQ(sendResult, static_cast<ssize_t>(strlen(SEND_BUF)));
|
|
IUartDeviceFree(object);
|
|
}
|
|
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_RecvData
|
|
TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_RecvData)
|
|
{
|
|
const char *RECV_BUFF = "TEST";
|
|
SetRecvApiOnce(mLinuxTest, gUartDevice, (void *)RECV_BUFF, strlen(RECV_BUFF));
|
|
void *object = CreateUartDevice(gUartDevice);
|
|
IUartOpen(object);
|
|
constexpr int RECV_TIME_OUT_MS = 1000;
|
|
constexpr int RECV_BUF_LENGTH = 256;
|
|
char recvBuf[RECV_BUF_LENGTH] = {0};
|
|
ssize_t recvResult = IUartRecv(object, recvBuf, RECV_BUF_LENGTH, RECV_TIME_OUT_MS);
|
|
EXPECT_EQ(recvResult, static_cast<ssize_t>(strlen(RECV_BUFF)));
|
|
EXPECT_EQ(0, memcmp(recvBuf, RECV_BUFF, strlen(RECV_BUFF)));
|
|
IUartDeviceFree(object);
|
|
}
|
|
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_EXAMPLE_MultiThreadTest
|
|
/**
|
|
* @brief Construct a new test f object
|
|
* This test case tests whether the correct return value can be obtained when simulating multi-threaded operations on
|
|
* the interface.
|
|
*/
|
|
TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_MultiThreadTest)
|
|
{
|
|
auto openThread = [](void) {
|
|
LogInfo("openThread.\n");
|
|
void *object = CreateUartDevice(gUartDevice);
|
|
IUartOpen(object);
|
|
IUartDeviceFree(object);
|
|
};
|
|
std::thread test1 = std::thread(openThread);
|
|
test1.detach();
|
|
std::thread test2 = std::thread(openThread);
|
|
test2.detach();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
}
|
|
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.UNIT_UartDevice_AUTO_OpenFailed
|
|
TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_OpenFailed)
|
|
{
|
|
SetUartDeviceOpenFailed(mLinuxTest, gUartDevice);
|
|
const char *SEND_BUF = "TEST";
|
|
constexpr ssize_t SEND_LENGTH = 1; // less than static_cast<ssize_t>(strlen(SEND_BUF))
|
|
SetSendApiOnce(mLinuxTest, gUartDevice, SEND_LENGTH);
|
|
void *object = CreateUartDevice(gUartDevice);
|
|
IUartOpen(object);
|
|
ssize_t sendResult = IUartSend(object, SEND_BUF, strlen(SEND_BUF));
|
|
EXPECT_EQ(sendResult, static_cast<ssize_t>(strlen(SEND_BUF)));
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
IUartDeviceFree(object);
|
|
}
|
|
} // namespace UartDeviceMockTest
|