aboutsummaryrefslogtreecommitdiffstats
path: root/test/mock
diff options
context:
space:
mode:
Diffstat (limited to 'test/mock')
-rw-r--r--test/mock/libpthread_mock.hpp93
-rw-r--r--test/mock/libsystemd_mock.hpp208
-rw-r--r--test/mock/syscall_io_mock.hpp156
3 files changed, 457 insertions, 0 deletions
diff --git a/test/mock/libpthread_mock.hpp b/test/mock/libpthread_mock.hpp
new file mode 100644
index 0000000..61ce43c
--- /dev/null
+++ b/test/mock/libpthread_mock.hpp
@@ -0,0 +1,93 @@
+#include <gmock/gmock.h>
+#include <functional>
+
+#include <signal.h>
+
+/*
+int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);
+*/
+static std::function<int(int how, const sigset_t *set, sigset_t *oldset)> _pthread_sigmask;
+
+/*
+int pthread_mutex_lock(pthread_mutex_t *mutex);
+int pthread_mutex_trylock(pthread_mutex_t *mutex);
+int pthread_mutex_unlock(pthread_mutex_t *mutex);
+
+static std::function<int(pthread_mutex_t *mutex)> _pthread_mutex_lock;
+static std::function<int(pthread_mutex_t *mutex)> _pthread_mutex_trylock;
+static std::function<int(pthread_mutex_t *mutex)> _pthread_mutex_unlock;
+*/
+
+class LibpthreadMocker {
+public:
+ LibpthreadMocker() {
+ _pthread_sigmask = [this](int how, const sigset_t *set, sigset_t *oldset)
+ {
+ return pthread_sigmask(how, set, oldset);
+ };
+
+/*
+ _pthread_mutex_lock = [this](pthread_mutex_t *mutex)
+ {
+ return pthread_mutex_lock(mutex);
+ };
+ _pthread_mutex_trylock = [this](pthread_mutex_t *mutex)
+ {
+ return pthread_mutex_trylock(mutex);
+ };
+ _pthread_mutex_unlock = [this](pthread_mutex_t *mutex)
+ {
+ return pthread_mutex_unlock(mutex);
+ };
+*/
+ }
+
+ ~LibpthreadMocker() {
+ _pthread_sigmask = {};
+
+/*
+ _pthread_mutex_lock = {};
+ _pthread_mutex_trylock = {};
+ _pthread_mutex_unlock = {};
+*/
+ }
+
+ MOCK_CONST_METHOD3(pthread_sigmask, int(int how, const sigset_t *set, sigset_t *oldset));
+/*
+ MOCK_CONST_METHOD1(pthread_mutex_lock, int(pthread_mutex_t *mutex));
+ MOCK_CONST_METHOD1(pthread_mutex_trylock, int(pthread_mutex_t *mutex));
+ MOCK_CONST_METHOD1(pthread_mutex_unlock, int(pthread_mutex_t *mutex));
+*/
+};
+
+class LibpthreadMockBase {
+protected:
+ LibpthreadMocker lpm;
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset)
+{
+ return _pthread_sigmask(how, set, oldset);
+}
+
+/*
+static int pthread_mutex_lock(pthread_mutex_t *mutex)
+{
+ return _pthread_mutex_lock(mutex);
+}
+static int pthread_mutex_trylock(pthread_mutex_t *mutex)
+{
+ return _pthread_mutex_trylock(mutex);
+}
+static int pthread_mutex_unlock(pthread_mutex_t *mutex)
+{
+ return _pthread_mutex_unlock(mutex);
+}
+*/
+#ifdef __cplusplus
+}
+#endif
diff --git a/test/mock/libsystemd_mock.hpp b/test/mock/libsystemd_mock.hpp
new file mode 100644
index 0000000..34de9a0
--- /dev/null
+++ b/test/mock/libsystemd_mock.hpp
@@ -0,0 +1,208 @@
+#include <gmock/gmock.h>
+#include <functional>
+
+#include <systemd/sd-event.h>
+
+
+/*
+int sd_event_add_signal(sd_event *event, sd_event_source **source,
+ int signal, sd_event_signal_handler_t handler, void *userdata);
+*/
+static std::function<int(sd_event *event, sd_event_source **source,
+ int signal, sd_event_signal_handler_t handler, void *userdata)> _sd_event_add_signal;
+
+/*
+int sd_event_add_io(sd_event *event, sd_event_source **source, int fd,
+ uint32_t events, sd_event_io_handler_t handler, void *userdata);
+int sd_event_source_get_io_fd(sd_event_source *source);
+int sd_event_source_set_io_fd_own(sd_event_source *source, int b);
+*/
+static std::function<int(sd_event *event, sd_event_source **source, int fd,
+ uint32_t events, sd_event_io_handler_t handler, void *userdata)>
+ _sd_event_add_io;
+static std::function<int(sd_event_source *source)> _sd_event_source_get_io_fd;
+static std::function<int(sd_event_source *source, int b)> _sd_event_source_set_io_fd_own;
+
+
+/*
+int sd_event_source_set_time(sd_event_source *source, uint64_t usec);
+int sd_event_now(sd_event *event, clockid_t clock, uint64_t *usec);
+int sd_event_add_time( sd_event *event,
+ sd_event_source **source,
+ clockid_t clock,
+ uint64_t usec,
+ uint64_t accuracy,
+ sd_event_time_handler_t handler,
+ void *userdata);
+int sd_event_source_set_enabled(sd_event_source *source, int enabled);
+*/
+static std::function<int(sd_event_source *source, uint64_t usec)> _sd_event_source_set_time;
+static std::function<int(sd_event *event, clockid_t clock, uint64_t *usec)> _sd_event_now;
+static std::function<int(sd_event *event, sd_event_source **source, clockid_t clock,
+ uint64_t usec, uint64_t accuracy, sd_event_time_handler_t handler, void *userdata)> _sd_event_add_time;
+static std::function<int(sd_event_source *source, int enabled)> _sd_event_source_set_enabled;
+
+
+/*
+sd_event_source* sd_event_source_unref(sd_event_source *source);
+sd_event_source* sd_event_source_ref(sd_event_source *source);
+sd_event_source* sd_event_source_disable_unref(sd_event_source *source);
+*/
+static std::function<sd_event_source*(sd_event_source *source)> _sd_event_source_unref;
+static std::function<sd_event_source*(sd_event_source *source)> _sd_event_source_ref;
+static std::function<sd_event_source*(sd_event_source *source)> _sd_event_source_disable_unref;
+
+
+class LibsystemdMocker {
+public:
+ LibsystemdMocker() {
+ _sd_event_add_signal
+ = [this](sd_event *event, sd_event_source **source, int signal,
+ sd_event_signal_handler_t handler, void *userdata) {
+ return sd_event_add_signal(event, source, signal, handler, userdata);
+ };
+
+ _sd_event_add_io
+ = [this](sd_event *event, sd_event_source **source, int fd,
+ uint32_t events, sd_event_io_handler_t handler, void *userdata) {
+ return sd_event_add_io(event, source, fd, events, handler, userdata);
+ };
+ _sd_event_source_get_io_fd = [this](sd_event_source *source) {
+ return sd_event_source_get_io_fd(source);
+ };
+ _sd_event_source_set_io_fd_own = [this](sd_event_source *source, int b) {
+ return sd_event_source_set_io_fd_own(source, b);
+ };
+
+ _sd_event_source_set_time = [this](sd_event_source *source, uint64_t usec) {
+ return sd_event_source_set_time(source, usec);
+ };
+ _sd_event_now = [this](sd_event *event, clockid_t clock, uint64_t *usec) {
+ return sd_event_now(event, clock, usec);
+ };
+ _sd_event_add_time
+ = [this](sd_event *event, sd_event_source **source, clockid_t clock, uint64_t usec,
+ uint64_t accuracy, sd_event_time_handler_t handler, void *userdata) {
+ return sd_event_add_time(event, source, clock, usec, accuracy, handler, userdata);
+ };
+ _sd_event_source_set_enabled = [this](sd_event_source *source, int enabled) {
+ return sd_event_source_set_enabled(source, enabled);
+ };
+
+ _sd_event_source_unref = [this](sd_event_source *source) {
+ return sd_event_source_unref(source);
+ };
+ _sd_event_source_ref = [this](sd_event_source *source) {
+ return sd_event_source_ref(source);
+ };
+ _sd_event_source_disable_unref = [this](sd_event_source *source) {
+ return sd_event_source_disable_unref(source);
+ };
+ }
+
+ ~LibsystemdMocker() {
+ _sd_event_add_signal = {};
+
+ _sd_event_add_io = {};
+ _sd_event_source_get_io_fd = {};
+ _sd_event_source_set_io_fd_own = {};
+
+ _sd_event_source_set_time = {};
+ _sd_event_now = {};
+ _sd_event_add_time = {};
+ _sd_event_source_set_enabled = {};
+
+ _sd_event_source_unref = {};
+ _sd_event_source_ref = {};
+ _sd_event_source_disable_unref = {};
+ }
+
+ MOCK_CONST_METHOD5(sd_event_add_signal, int(sd_event *event, sd_event_source **source, int signal, sd_event_signal_handler_t handler, void *userdata));
+
+ MOCK_CONST_METHOD6(sd_event_add_io, int(sd_event *event, sd_event_source **source, int fd, uint32_t events, sd_event_io_handler_t handler, void *userdata));
+ MOCK_CONST_METHOD1(sd_event_source_get_io_fd, int(sd_event_source *source));
+ MOCK_CONST_METHOD2(sd_event_source_set_io_fd_own, int(sd_event_source *source, int b));
+
+ MOCK_CONST_METHOD2(sd_event_source_set_time, int(sd_event_source *source, uint64_t usec));
+ MOCK_CONST_METHOD3(sd_event_now, int(sd_event *event, clockid_t clock, uint64_t *usec));
+ MOCK_CONST_METHOD7(sd_event_add_time, int(sd_event *event,sd_event_source **source, clockid_t clock, uint64_t usec, uint64_t accuracy, sd_event_time_handler_t handler, void *userdata));
+ MOCK_CONST_METHOD2(sd_event_source_set_enabled, int(sd_event_source *source, int enabled));
+
+ MOCK_CONST_METHOD1(sd_event_source_unref, sd_event_source*(sd_event_source *source));
+ MOCK_CONST_METHOD1(sd_event_source_ref, sd_event_source*(sd_event_source *source));
+ MOCK_CONST_METHOD1(sd_event_source_disable_unref, sd_event_source*(sd_event_source *source));
+};
+
+class LibsystemdMockBase {
+protected:
+ LibsystemdMocker lsm;
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int sd_event_add_signal( sd_event *event,
+ sd_event_source **source,
+ int signal,
+ sd_event_signal_handler_t handler,
+ void *userdata)
+{
+ return _sd_event_add_signal(event, source, signal, handler, userdata);
+}
+
+int sd_event_add_io(sd_event *event, sd_event_source **source, int fd,
+ uint32_t events, sd_event_io_handler_t handler, void *userdata)
+{
+ return _sd_event_add_io(event, source, fd, events, handler, userdata);
+}
+
+int sd_event_source_get_io_fd( sd_event_source *source)
+{
+ return _sd_event_source_get_io_fd(source);
+}
+
+int sd_event_source_set_io_fd_own(sd_event_source *source, int b)
+{
+ return _sd_event_source_set_io_fd_own(source, b);
+}
+
+int sd_event_source_set_time(sd_event_source *source, uint64_t usec)
+{
+ return _sd_event_source_set_time(source, usec);
+}
+
+int sd_event_now(sd_event *event, clockid_t clock, uint64_t *usec)
+{
+ return _sd_event_now(event, clock, usec);
+}
+
+int sd_event_add_time(sd_event *event, sd_event_source **source, clockid_t clock, uint64_t usec,
+ uint64_t accuracy, sd_event_time_handler_t handler, void *userdata)
+{
+ return _sd_event_add_time(event, source, clock, usec, accuracy, handler, userdata);
+}
+
+int sd_event_source_set_enabled(sd_event_source *source, int enabled)
+{
+ return _sd_event_source_set_enabled(source, enabled);
+}
+
+sd_event_source* sd_event_source_unref(sd_event_source *source)
+{
+ return _sd_event_source_unref(source);
+}
+
+sd_event_source* sd_event_source_ref(sd_event_source *source)
+{
+ return _sd_event_source_ref(source);
+}
+
+sd_event_source* sd_event_source_disable_unref(sd_event_source *source)
+{
+ return _sd_event_source_disable_unref(source);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/test/mock/syscall_io_mock.hpp b/test/mock/syscall_io_mock.hpp
new file mode 100644
index 0000000..75efdb1
--- /dev/null
+++ b/test/mock/syscall_io_mock.hpp
@@ -0,0 +1,156 @@
+#include <gmock/gmock.h>
+#include <functional>
+
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+/*
+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<ssize_t(int fd, void *buf, size_t count)> _read;
+static std::function<ssize_t(int fd, const void *buf, size_t count)> _write;
+static std::function<int(int)> _close;
+
+/*
+int unlink(const char *pathname);
+int stat(const char *pathname, struct stat *buf);
+*/
+static std::function<int(const char *)> _unlink;
+static std::function<int(const char *pathname, struct stat *buf)> _stat;
+
+
+/*
+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<int(int socket_family, int socket_type, int protocol)> _socket;
+static std::function<int(int sockfd, const struct sockaddr *addr,socklen_t addrlen)> _bind;
+static std::function<int(int sockfd, int backlog)> _listen;
+static std::function<int(int sockfd, struct sockaddr *addr,
+ socklen_t *addrlen, int flags)> _accept4;
+
+class SyscallIOMocker {
+public:
+ SyscallIOMocker() {
+ _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);
+ };
+
+ _unlink = [this](const char *pathname){
+ return unlink(pathname);
+ };
+ _stat = [this](const char *pathname, struct stat *buf) {
+ return stat(pathname, buf);
+ };
+
+ _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() {
+ _read = {};
+ _write = {};
+ _close = {};
+
+ _unlink = {};
+ _stat = {};
+
+ _socket = {};
+ _bind = {};
+ _listen = {};
+ _accept4 = {};
+ }
+
+ 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(unlink, int(const char *));
+ MOCK_CONST_METHOD2(stat, int(const char *pathname, struct stat *buf));
+
+ 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
+ssize_t read(int fd, void *buf, size_t count)
+{
+ return _read(fd, buf, count);
+}
+
+ssize_t write(int fd, const void *buf, size_t count)
+{
+ return _write(fd, buf, count);
+}
+
+int close(int fd)
+{
+ return _close(fd);
+}
+
+int unlink(const char *pathname)
+{
+ return _unlink(pathname);
+}
+
+int stat(const char *pathname, struct stat *buf)
+{
+ return _stat(pathname, buf);
+}
+
+int socket(int socket_family, int socket_type, int protocol)
+{
+ return _socket(socket_family, socket_type, protocol);
+}
+
+int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen)
+{
+ return _bind(sockfd, addr, addrlen);
+}
+
+int listen(int sockfd, int backlog)
+{
+ return _listen(sockfd, backlog);
+}
+
+int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
+{
+ return _accept4(sockfd, addr, addrlen, flags);
+}
+
+#ifdef __cplusplus
+}
+#endif