94 lines
4.0 KiB
C++
94 lines
4.0 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 "GtestUsing.h"
|
|
#include <memory>
|
|
constexpr int INVALID_HANDLE = -1;
|
|
constexpr int MOCK_SELECT_TIME_OUT = 0;
|
|
constexpr int MOCK_SYSTEM_RESULT_SUCCESS = 0;
|
|
constexpr int MOCK_SYSTEM_RESULT_FAILURE = -1;
|
|
class LinuxApiMock
|
|
{
|
|
public:
|
|
LinuxApiMock() = default;
|
|
virtual ~LinuxApiMock() = default;
|
|
static std::shared_ptr<LinuxApiMock> &GetInstance(std::shared_ptr<LinuxApiMock> *impl = nullptr);
|
|
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);
|
|
virtual int fx_fstat(int fd, struct stat *statbuf);
|
|
virtual int fx_access(const char *pathname, int mode);
|
|
virtual FILE *fx_fopen(const char *pathname, const char *mode);
|
|
virtual int fx_system(const char *command);
|
|
};
|
|
/**
|
|
* 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 *));
|
|
MOCK_METHOD2(fx_fstat, int(int, struct stat *));
|
|
MOCK_METHOD2(fx_access, int(const char *, int));
|
|
MOCK_METHOD2(fx_fopen, FILE *(const char *, const char *));
|
|
MOCK_METHOD1(fx_system, int(const char *));
|
|
|
|
public:
|
|
/**
|
|
* @brief Get the Handle For Mock object
|
|
* Obtain a globally unique test handle throughout the entire testing cycle, and the handle generated through this
|
|
* interface can avoid handle conflicts between different testing modules. Attention needs to be paid to whether it
|
|
* conflicts with non test handles generated by the system.
|
|
* @return int
|
|
*/
|
|
virtual int GetHandleForMock(void);
|
|
|
|
private:
|
|
static void InitSDCardDefaultStatus(std::shared_ptr<LinuxTest> &mock);
|
|
|
|
public:
|
|
/**
|
|
* @brief The functions from here on are all used in the simulator under the Ubuntu system. When cross-compiling, it
|
|
* is forbidden to use the following functions directly.
|
|
*/
|
|
/**
|
|
* @brief Create an instance of the LinuxTest class. The LinuxTest class instance can only be created through this
|
|
* function.
|
|
*/
|
|
static std::shared_ptr<LinuxTest> CreateLinuxTest(void);
|
|
/**
|
|
* @brief Controls the return value when the system function executes a shell command. This function can only simply
|
|
* control the return value and cannot capture and analyze the parameters of the shell command.
|
|
* @param mock
|
|
* @param command shell command
|
|
* @param result return value of system function
|
|
*/
|
|
static void SetSystemCommandResult(std::shared_ptr<LinuxTest> &mock, const std::string &command, const int &result);
|
|
};
|
|
#endif |