hunting/test/utils/LinuxApiMock/include/LinuxApiMock.h
2024-06-15 08:39:16 +08:00

71 lines
3.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;
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);
};
/**
* 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 *));
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);
public:
static std::shared_ptr<LinuxTest> CreateLinuxTest(void);
};
#endif