summaryrefslogtreecommitdiffstats
path: root/test/mock/syscall_io_mock.hpp
blob: 61e18405078ffacccbe1cad96fe95ae5dde40f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <gmock/gmock.h>
#include <functional>

#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>

/*
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
*/
static std::function<int(const char *pathname, int flags)> _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<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 fsync(int fd);
*/
static std::function<int(int)> _fsync;

/*
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() {
		_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);
		};

		_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 = {};

		_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_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 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