summaryrefslogtreecommitdiffstats
path: root/test/mock/libsystemd_mock.hpp
blob: 34de9a0f4d77ae38728559873a2eefb8955894d6 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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