summaryrefslogtreecommitdiffstats
path: root/lib/isc/tests/uv_wrap.h
blob: 968a6244d1b03a811966157bda11826a42cbcb2b (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

#if HAVE_CMOCKA
#include <inttypes.h>
#include <sched.h> /* IWYU pragma: keep */
#include <setjmp.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <uv.h>

#include <isc/atomic.h>

#define UNIT_TESTING
#include <cmocka.h>

#include "../netmgr/uv-compat.h"

/* uv_udp_t */

int
__wrap_uv_udp_open(uv_udp_t *handle, uv_os_sock_t sock);
int
__wrap_uv_udp_bind(uv_udp_t *handle, const struct sockaddr *addr,
		   unsigned int flags);
#if UV_VERSION_HEX >= UV_VERSION(1, 27, 0)
int
__wrap_uv_udp_connect(uv_udp_t *handle, const struct sockaddr *addr);
int
__wrap_uv_udp_getpeername(const uv_udp_t *handle, struct sockaddr *name,
			  int *namelen);
#endif /* UV_VERSION_HEX >= UV_VERSION(1, 27, 0) */
int
__wrap_uv_udp_getsockname(const uv_udp_t *handle, struct sockaddr *name,
			  int *namelen);
int
__wrap_uv_udp_send(uv_udp_send_t *req, uv_udp_t *handle, const uv_buf_t bufs[],
		   unsigned int nbufs, const struct sockaddr *addr,
		   uv_udp_send_cb send_cb);
int
__wrap_uv_udp_recv_start(uv_udp_t *handle, uv_alloc_cb alloc_cb,
			 uv_udp_recv_cb recv_cb);
int
__wrap_uv_udp_recv_stop(uv_udp_t *handle);

/* uv_tcp_t */
int
__wrap_uv_tcp_open(uv_tcp_t *handle, uv_os_sock_t sock);
int
__wrap_uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr,
		   unsigned int flags);
int
__wrap_uv_tcp_getsockname(const uv_tcp_t *handle, struct sockaddr *name,
			  int *namelen);
int
__wrap_uv_tcp_getpeername(const uv_tcp_t *handle, struct sockaddr *name,
			  int *namelen);
int
__wrap_uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle,
		      const struct sockaddr *addr, uv_connect_cb cb);

/* uv_stream_t */
int
__wrap_uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb);
int
__wrap_uv_accept(uv_stream_t *server, uv_stream_t *client);

/* uv_handle_t */
int
__wrap_uv_send_buffer_size(uv_handle_t *handle, int *value);
int
__wrap_uv_recv_buffer_size(uv_handle_t *handle, int *value);
int
__wrap_uv_fileno(const uv_handle_t *handle, uv_os_fd_t *fd);

/* uv_timer_t */
/* FIXME */
/*
 * uv_timer_init
 * uv_timer_start
 */

static atomic_int __state_uv_udp_open = 0;

int
__wrap_uv_udp_open(uv_udp_t *handle, uv_os_sock_t sock) {
	if (atomic_load(&__state_uv_udp_open) == 0) {
		return (uv_udp_open(handle, sock));
	}
	return (atomic_load(&__state_uv_udp_open));
}

static atomic_int __state_uv_udp_bind = 0;

int
__wrap_uv_udp_bind(uv_udp_t *handle, const struct sockaddr *addr,
		   unsigned int flags) {
	if (atomic_load(&__state_uv_udp_bind) == 0) {
		return (uv_udp_bind(handle, addr, flags));
	}
	return (atomic_load(&__state_uv_udp_bind));
}

static atomic_int __state_uv_udp_connect = 0;
#if UV_VERSION_HEX >= UV_VERSION(1, 27, 0)
int
__wrap_uv_udp_connect(uv_udp_t *handle, const struct sockaddr *addr) {
	if (atomic_load(&__state_uv_udp_connect) == 0) {
		return (uv_udp_connect(handle, addr));
	}
	return (atomic_load(&__state_uv_udp_connect));
}
#endif /* UV_VERSION_HEX >= UV_VERSION(1, 27, 0) */

static atomic_int __state_uv_udp_getpeername = 0;
#if UV_VERSION_HEX >= UV_VERSION(1, 27, 0)
int
__wrap_uv_udp_getpeername(const uv_udp_t *handle, struct sockaddr *name,
			  int *namelen) {
	if (atomic_load(&__state_uv_udp_getpeername) == 0) {
		return (uv_udp_getpeername(handle, name, namelen));
	}
	return (atomic_load(&__state_uv_udp_getpeername));
}
#endif /* UV_VERSION_HEX >= UV_VERSION(1, 27, 0) */

static atomic_int __state_uv_udp_getsockname = 0;
int
__wrap_uv_udp_getsockname(const uv_udp_t *handle, struct sockaddr *name,
			  int *namelen) {
	if (atomic_load(&__state_uv_udp_getsockname) == 0) {
		return (uv_udp_getsockname(handle, name, namelen));
	}
	return (atomic_load(&__state_uv_udp_getsockname));
}

static atomic_int __state_uv_udp_send = 0;
int
__wrap_uv_udp_send(uv_udp_send_t *req, uv_udp_t *handle, const uv_buf_t bufs[],
		   unsigned int nbufs, const struct sockaddr *addr,
		   uv_udp_send_cb send_cb) {
	if (atomic_load(&__state_uv_udp_send) == 0) {
		return (uv_udp_send(req, handle, bufs, nbufs, addr, send_cb));
	}
	return (atomic_load(&__state_uv_udp_send));
}

static atomic_int __state_uv_udp_recv_start = 0;
int
__wrap_uv_udp_recv_start(uv_udp_t *handle, uv_alloc_cb alloc_cb,
			 uv_udp_recv_cb recv_cb) {
	if (atomic_load(&__state_uv_udp_recv_start) == 0) {
		return (uv_udp_recv_start(handle, alloc_cb, recv_cb));
	}
	return (atomic_load(&__state_uv_udp_recv_start));
}

static atomic_int __state_uv_udp_recv_stop = 0;
int
__wrap_uv_udp_recv_stop(uv_udp_t *handle) {
	if (atomic_load(&__state_uv_udp_recv_stop) == 0) {
		return (uv_udp_recv_stop(handle));
	}
	return (atomic_load(&__state_uv_udp_recv_stop));
}

static atomic_int __state_uv_tcp_open = 0;
int
__wrap_uv_tcp_open(uv_tcp_t *handle, uv_os_sock_t sock) {
	if (atomic_load(&__state_uv_tcp_open) == 0) {
		return (uv_tcp_open(handle, sock));
	}
	return (atomic_load(&__state_uv_tcp_open));
}

static atomic_int __state_uv_tcp_bind = 0;
int
__wrap_uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr,
		   unsigned int flags) {
	if (atomic_load(&__state_uv_tcp_bind) == 0) {
		return (uv_tcp_bind(handle, addr, flags));
	}
	return (atomic_load(&__state_uv_tcp_bind));
}

static atomic_int __state_uv_tcp_getsockname = 0;
int
__wrap_uv_tcp_getsockname(const uv_tcp_t *handle, struct sockaddr *name,
			  int *namelen) {
	if (atomic_load(&__state_uv_tcp_getsockname) == 0) {
		return (uv_tcp_getsockname(handle, name, namelen));
	}
	return (atomic_load(&__state_uv_tcp_getsockname));
}

static atomic_int __state_uv_tcp_getpeername = 0;
int
__wrap_uv_tcp_getpeername(const uv_tcp_t *handle, struct sockaddr *name,
			  int *namelen) {
	if (atomic_load(&__state_uv_tcp_getpeername) == 0) {
		return (uv_tcp_getpeername(handle, name, namelen));
	}
	return (atomic_load(&__state_uv_tcp_getpeername));
}

static atomic_int __state_uv_tcp_connect = 0;
int
__wrap_uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle,
		      const struct sockaddr *addr, uv_connect_cb cb) {
	if (atomic_load(&__state_uv_tcp_connect) == 0) {
		return (uv_tcp_connect(req, handle, addr, cb));
	}
	return (atomic_load(&__state_uv_tcp_connect));
}

static atomic_int __state_uv_listen = 0;
int
__wrap_uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb) {
	if (atomic_load(&__state_uv_listen) == 0) {
		return (uv_listen(stream, backlog, cb));
	}
	return (atomic_load(&__state_uv_listen));
}

static atomic_int __state_uv_accept = 0;
int
__wrap_uv_accept(uv_stream_t *server, uv_stream_t *client) {
	if (atomic_load(&__state_uv_accept) == 0) {
		return (uv_accept(server, client));
	}
	return (atomic_load(&__state_uv_accept));
}

static atomic_int __state_uv_send_buffer_size = 0;
int
__wrap_uv_send_buffer_size(uv_handle_t *handle, int *value) {
	if (atomic_load(&__state_uv_send_buffer_size) == 0) {
		return (uv_send_buffer_size(handle, value));
	}
	return (atomic_load(&__state_uv_send_buffer_size));
}

static atomic_int __state_uv_recv_buffer_size = 0;
int
__wrap_uv_recv_buffer_size(uv_handle_t *handle, int *value) {
	if (atomic_load(&__state_uv_recv_buffer_size) == 0) {
		return (uv_recv_buffer_size(handle, value));
	}
	return (atomic_load(&__state_uv_recv_buffer_size));
}

static atomic_int __state_uv_fileno = 0;
int
__wrap_uv_fileno(const uv_handle_t *handle, uv_os_fd_t *fd) {
	if (atomic_load(&__state_uv_fileno) == 0) {
		return (uv_fileno(handle, fd));
	}
	return (atomic_load(&__state_uv_fileno));
}

#define uv_udp_open(...) __wrap_uv_udp_open(__VA_ARGS__)
#define uv_udp_bind(...) __wrap_uv_udp_bind(__VA_ARGS__)
#if UV_VERSION_HEX >= UV_VERSION(1, 27, 0)
#define uv_udp_connect(...)	__wrap_uv_udp_connect(__VA_ARGS__)
#define uv_udp_getpeername(...) __wrap_uv_udp_getpeername(__VA_ARGS__)
#endif /* UV_VERSION_HEX >= UV_VERSION(1, 27, 0) */
#define uv_udp_getsockname(...) __wrap_uv_udp_getsockname(__VA_ARGS__)
#define uv_udp_send(...)	__wrap_uv_udp_send(__VA_ARGS__)
#define uv_udp_recv_start(...)	__wrap_uv_udp_recv_start(__VA_ARGS__)
#define uv_udp_recv_stop(...)	__wrap_uv_udp_recv_stop(__VA_ARGS__)

#define uv_tcp_open(...)	__wrap_uv_tcp_open(__VA_ARGS__)
#define uv_tcp_bind(...)	__wrap_uv_tcp_bind(__VA_ARGS__)
#define uv_tcp_getsockname(...) __wrap_uv_tcp_getsockname(__VA_ARGS__)
#define uv_tcp_getpeername(...) __wrap_uv_tcp_getpeername(__VA_ARGS__)
#define uv_tcp_connect(...)	__wrap_uv_tcp_connect(__VA_ARGS__)

#define uv_listen(...) __wrap_uv_listen(__VA_ARGS__)
#define uv_accept(...) __wrap_uv_accept(__VA_ARGS__)

#define uv_send_buffer_size(...) __wrap_uv_send_buffer_size(__VA_ARGS__)
#define uv_recv_buffer_size(...) __wrap_uv_recv_buffer_size(__VA_ARGS__)
#define uv_fileno(...)		 __wrap_uv_fileno(__VA_ARGS__)

#define RESET_RETURN                                           \
	{                                                      \
		atomic_store(&__state_uv_udp_open, 0);         \
		atomic_store(&__state_uv_udp_bind, 0);         \
		atomic_store(&__state_uv_udp_connect, 0);      \
		atomic_store(&__state_uv_udp_getpeername, 0);  \
		atomic_store(&__state_uv_udp_getsockname, 0);  \
		atomic_store(&__state_uv_udp_send, 0);         \
		atomic_store(&__state_uv_udp_recv_start, 0);   \
		atomic_store(&__state_uv_udp_recv_stop, 0);    \
		atomic_store(&__state_uv_tcp_open, 0);         \
		atomic_store(&__state_uv_tcp_bind, 0);         \
		atomic_store(&__state_uv_tcp_getpeername, 0);  \
		atomic_store(&__state_uv_tcp_getsockname, 0);  \
		atomic_store(&__state_uv_tcp_connect, 0);      \
		atomic_store(&__state_uv_listen, 0);           \
		atomic_store(&__state_uv_accept, 0);           \
		atomic_store(&__state_uv_send_buffer_size, 0); \
		atomic_store(&__state_uv_recv_buffer_size, 0); \
		atomic_store(&__state_uv_fileno, 0);           \
	}

#define WILL_RETURN(func, value) atomic_store(&__state_##func, value)

#endif /* HAVE_CMOCKA */