118 lines
3.6 KiB
C++
118 lines
3.6 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.
|
|
*/
|
|
#ifndef LINUX_API_MOCK_H
|
|
#define LINUX_API_MOCK_H
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
#include <memory>
|
|
using ::testing::_;
|
|
using ::testing::Action;
|
|
using ::testing::ActionInterface;
|
|
using ::testing::Assign;
|
|
using ::testing::ByMove;
|
|
using ::testing::ByRef;
|
|
using ::testing::DefaultValue;
|
|
using ::testing::DoAll;
|
|
using ::testing::DoDefault;
|
|
using ::testing::IgnoreResult;
|
|
using ::testing::Invoke;
|
|
using ::testing::InvokeWithoutArgs;
|
|
using ::testing::MakePolymorphicAction;
|
|
using ::testing::PolymorphicAction;
|
|
using ::testing::Return;
|
|
using ::testing::ReturnNew;
|
|
using ::testing::ReturnNull;
|
|
using ::testing::ReturnPointee;
|
|
using ::testing::ReturnRef;
|
|
using ::testing::ReturnRefOfCopy;
|
|
using ::testing::ReturnRoundRobin;
|
|
using ::testing::SaveArg;
|
|
using ::testing::SetArgPointee;
|
|
using ::testing::SetArgumentPointee;
|
|
using ::testing::Unused;
|
|
using ::testing::WithArgs;
|
|
using ::testing::internal::BuiltInDefaultValue;
|
|
constexpr int INVALID_HANDLE = -1;
|
|
constexpr int MOCK_SELECT_TIME_OUT = 0;
|
|
class LinuxApiMock
|
|
{
|
|
public:
|
|
LinuxApiMock() = default;
|
|
virtual ~LinuxApiMock() = default;
|
|
static std::shared_ptr<LinuxApiMock> &GetInstance(std::shared_ptr<LinuxApiMock> *impl = nullptr)
|
|
{
|
|
static auto instance = std::make_shared<LinuxApiMock>();
|
|
if (impl) {
|
|
// The non-thread-safe changing is only for gtest.
|
|
instance = *impl;
|
|
}
|
|
return instance;
|
|
}
|
|
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);
|
|
virtual ssize_t fx_write(int fd, const void *buf, size_t count);
|
|
virtual ssize_t fx_read(int fd, void *buf, size_t count);
|
|
virtual int fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
|
|
|
public:
|
|
/**
|
|
* @brief
|
|
*
|
|
*/
|
|
// virtual void ApiLock(void) {}
|
|
/**
|
|
* @brief
|
|
*
|
|
*/
|
|
// virtual void ApiUnlock(void) {}
|
|
};
|
|
/**
|
|
* A simulation interface class used for automated testing in Ubuntu systems, implementing the function of piling on
|
|
* some Linux standard interfaces.
|
|
*/
|
|
class LinuxTest : public LinuxApiMock, public std::enable_shared_from_this<LinuxTest>
|
|
{
|
|
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 *));
|
|
MOCK_METHOD3(fx_write, ssize_t(int, const void *, size_t));
|
|
MOCK_METHOD3(fx_read, ssize_t(int, void *, size_t));
|
|
MOCK_METHOD5(fx_select, int(int, fd_set *, fd_set *, fd_set *, struct timeval *));
|
|
|
|
public:
|
|
/**
|
|
* @brief Get the Handle For Mock object
|
|
*
|
|
* @return int
|
|
*/
|
|
virtual int GetHandleForMock(void)
|
|
{
|
|
return INVALID_HANDLE;
|
|
}
|
|
|
|
public:
|
|
static std::shared_ptr<LinuxTest> CreateLinuxTest(void);
|
|
};
|
|
#endif |