/** * SPDX-License-Identifier: Apache-2.0 */ #include #include #include #include #include #include #include /* int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); */ static std::function _open; /* ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count); int close(int fd); */ static std::function _read; static std::function _write; static std::function _close; /* int fsync(int fd); */ static std::function _fsync; /* int unlink(const char *pathname); int stat(const char *pathname, struct stat *buf); int rename(const char *oldpath, const char *newpath); */ static std::function _unlink; static std::function _stat; static std::function _rename; /* int socket(int socket_family, int socket_type, int protocol); int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen); int listen(int sockfd, int backlog); int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); */ static std::function _socket; static std::function _bind; static std::function _listen; static std::function _accept4; class SyscallIOMocker { public: SyscallIOMocker() { _open = [this](const char *pathname, int flags) { return open(pathname, flags); }; _read = [this](int fd, void *buf, size_t count) { return read(fd, buf, count); }; _write = [this](int fd, const void *buf, size_t count) { return write(fd, buf, count); }; _close = [this](int fd){ return close(fd); }; _fsync = [this](int fd){ return fsync(fd); }; _unlink = [this](const char *pathname){ return unlink(pathname); }; _stat = [this](const char *pathname, struct stat *buf) { return stat(pathname, buf); }; _rename = [this](const char *oldpath, const char *newpath){ return rename(oldpath, newpath); }; _socket = [this](int socket_family, int socket_type, int protocol){ return socket(socket_family, socket_type, protocol); }; _bind = [this](int sockfd, const struct sockaddr *addr,socklen_t addrlen){ return bind(sockfd, addr,addrlen); }; _listen = [this](int sockfd, int backlog){ return listen(sockfd, backlog); }; _accept4 = [this](int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { return accept4(sockfd, addr, addrlen, flags); }; } ~SyscallIOMocker() { _open = {}; _read = {}; _write = {}; _close = {}; _fsync = {}; _unlink = {}; _stat = {}; _rename = {}; _socket = {}; _bind = {}; _listen = {}; _accept4 = {}; } MOCK_CONST_METHOD2(open, int(const char *pathname, int flags)); MOCK_CONST_METHOD3(read, ssize_t(int fd, void *buf, size_t count)); MOCK_CONST_METHOD3(write, ssize_t(int fd, const void *buf, size_t count)); MOCK_CONST_METHOD1(close, int(int)); MOCK_CONST_METHOD1(fsync, int(int)); MOCK_CONST_METHOD1(unlink, int(const char *)); MOCK_CONST_METHOD2(stat, int(const char *pathname, struct stat *buf)); MOCK_CONST_METHOD2(rename, int(const char *oldpath, const char *newpath)); MOCK_CONST_METHOD3(socket, int(int socket_family, int socket_type, int protocol)); MOCK_CONST_METHOD3(bind, int(int sockfd, const struct sockaddr *addr,socklen_t addrlen)); MOCK_CONST_METHOD2(listen, int(int sockfd, int backlog)); MOCK_CONST_METHOD4(accept4, int(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)); }; class SyscallIOMockBase { protected: SyscallIOMocker sysiom; }; #ifdef __cplusplus extern "C" { #endif static int open(const char *pathname, int flags, ...) { return _open(pathname, flags); } static ssize_t read(int fd, void *buf, size_t count) { return _read(fd, buf, count); } static ssize_t write(int fd, const void *buf, size_t count) { return _write(fd, buf, count); } static int close(int fd) { return _close(fd); } static int fsync(int fd) { return _fsync(fd); } static int unlink(const char *pathname) { return _unlink(pathname); } static int stat(const char *pathname, struct stat *buf) { return _stat(pathname, buf); } static int rename(const char *oldpath, const char *newpath) { return _rename(oldpath, newpath); } static int socket(int socket_family, int socket_type, int protocol) { return _socket(socket_family, socket_type, protocol); } static int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen) { return _bind(sockfd, addr, addrlen); } static int listen(int sockfd, int backlog) { return _listen(sockfd, backlog); } static int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { return _accept4(sockfd, addr, addrlen, flags); } #ifdef __cplusplus } #endif