Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2020, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : :
31 : : #include <string.h>
32 : : #include <stdbool.h>
33 : : #include <stdint.h>
34 : : #include <stdarg.h>
35 : : #include <stddef.h>
36 : : #include <setjmp.h>
37 : : #include <cmocka.h>
38 : : #include <errno.h>
39 : : #include <sys/socket.h>
40 : : #include <sys/stat.h>
41 : :
42 : : #include "control-wrappers.h"
43 : :
44 : : static int enable_mocks = 0;
45 : :
46 : :
47 : : FILE *
48 : : __real_fdopen(int fd, const char *mode);
49 : :
50 : : FILE *
51 : 8 : __wrap_fdopen(int fd, const char *mode)
52 : : {
53 [ - + ]: 8 : if (enable_mocks) {
54 : 0 : FILE *file = (FILE*)mock();
55 [ # # ]: 0 : if (file == NULL) {
56 : 0 : errno = ENOENT;
57 : : }
58 : :
59 : 0 : return file;
60 : : }
61 : :
62 : 8 : return __real_fdopen(fd, mode);
63 : : }
64 : :
65 : : int
66 : 0 : __wrap_ioctl(int fd, int request, ...)
67 : : {
68 : : int result;
69 : :
70 : 0 : check_expected(fd);
71 : 0 : check_expected(request);
72 : :
73 : 0 : result = (int)mock();
74 : :
75 [ # # ]: 0 : if (result != 0) {
76 : 0 : errno = result;
77 : 0 : result = -1;
78 : : }
79 : :
80 : 0 : return result;
81 : : }
82 : :
83 : : int
84 : : __real_open(const char *pathname, int flags);
85 : :
86 : : int
87 : 15 : __wrap_open(const char *pathname, int flags)
88 : : {
89 : : int result;
90 : :
91 [ + + ]: 15 : if (enable_mocks) {
92 : 5 : check_expected(pathname);
93 : 5 : result = mock();
94 [ + + ]: 5 : if (result == -1)
95 : 1 : errno = ENOENT;
96 : 5 : return result;
97 : : }
98 : :
99 : 10 : return __real_open(pathname, flags);
100 : : }
101 : :
102 : : int
103 : : __real_close(int fd);
104 : :
105 : : int
106 : 18 : __wrap_close(int fd)
107 : : {
108 : : int result;
109 : :
110 : 18 : check_expected(fd);
111 : 18 : result = mock();
112 [ - + ]: 18 : if (result != 0)
113 : : {
114 : 0 : errno = result;
115 : 0 : result = 1;
116 : : }
117 : 18 : return result;
118 : : }
119 : :
120 : : int
121 : : __real_access(const char *pathname, int mode);
122 : :
123 : : int
124 : 9 : __wrap_access(const char *pathname, int mode)
125 : : {
126 : : int result;
127 : :
128 [ - + ]: 9 : if (enable_mocks) {
129 : 0 : check_expected(pathname);
130 : :
131 : 0 : result = mock();
132 [ # # ]: 0 : if (result != 0) {
133 : 0 : errno = result;
134 : 0 : result = -1;
135 : : }
136 : 0 : return result;
137 : : }
138 : 9 : return __real_access(pathname, mode);
139 : : }
140 : :
141 : : size_t
142 : 13 : __wrap_read(int fd, void *buf, size_t count)
143 : : {
144 : 13 : int result = -1;
145 : : struct mock_read_params *params;
146 : :
147 : 13 : check_expected(fd);
148 : 13 : params = (struct mock_read_params *)mock();
149 : :
150 [ + + ]: 13 : if (params->result > 0) {
151 : 12 : memcpy(buf, params->data, params->result);
152 : 12 : result = params->result;
153 : : }
154 [ + - ]: 1 : else if (params->result < 0) {
155 : 1 : errno = -params->result;
156 : 1 : result = -1;
157 : : }
158 : :
159 : 13 : return result;
160 : : }
161 : :
162 : : /*
163 : : * Wrap the write call.
164 : : *
165 : : * Checks the passed FD is expected
166 : : * Mock result is amount of written data to report
167 : : * limited to count, negative values are errnos to
168 : : * set and return -1.
169 : : */
170 : : size_t
171 : 12 : __wrap_write(int fd, const void *buf, size_t count)
172 : : {
173 : : int result;
174 : :
175 : 12 : check_expected(fd);
176 : 12 : check_expected(buf);
177 : 12 : result = mock();
178 : :
179 [ + + ]: 12 : if (result > (int)count)
180 : : result = count;
181 [ + - ]: 1 : else if (result < 0) {
182 : 1 : errno = -result;
183 : 1 : result = -1;
184 : : }
185 : 12 : return result;
186 : : }
187 : :
188 : : int
189 : 14 : __wrap_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
190 : : {
191 : : int result;
192 : 14 : check_expected(sockfd);
193 : 14 : check_expected(addr);
194 : 14 : result = mock();
195 [ + + ]: 14 : if (result) {
196 : 1 : errno = result;
197 : 1 : result = -1;
198 : : }
199 : 14 : return result;
200 : : }
201 : :
202 : : int
203 : 27 : __wrap_select(int nfds, fd_set *readfds, fd_set *writefds,
204 : : fd_set *exceptfds, struct timeval *timeout)
205 : : {
206 : : struct mock_select_params *params;
207 : 27 : check_expected(timeout);
208 : 27 : params = (struct mock_select_params *)mock();
209 [ + + ]: 27 : if (readfds)
210 : 14 : memcpy(readfds, ¶ms->readfds, sizeof(fd_set));
211 [ + + ]: 27 : if (writefds)
212 : 13 : memcpy(writefds, ¶ms->writefds, sizeof(fd_set));
213 [ - + ]: 27 : if (exceptfds)
214 : 0 : memcpy(exceptfds, ¶ms->exceptfds, sizeof(fd_set));
215 : 27 : return params->result;
216 : : }
217 : :
218 : : int
219 : : __real_mkdir(const char *pathname, mode_t mode);
220 : :
221 : : int
222 : 2 : __wrap_mkdir(const char *pathname, mode_t mode)
223 : : {
224 : : int result;
225 [ - + ]: 2 : if (enable_mocks) {
226 : 0 : check_expected(pathname);
227 : 0 : result = mock();
228 [ # # ]: 0 : if (result != 0){
229 : 0 : errno = result;
230 : 0 : result = -1;
231 : : }
232 : 0 : return result;
233 : : }
234 : 2 : return __real_mkdir(pathname, mode);
235 : : }
236 : :
237 : : int
238 : 8 : __wrap_flock(int fd, int operation)
239 : : {
240 : : int result;
241 : 8 : check_expected(fd);
242 : 8 : check_expected(operation);
243 : 8 : result = mock();
244 [ + + ]: 8 : if (result != 0) {
245 : 2 : errno = result;
246 : 2 : result = -1;
247 : : }
248 : 8 : return result;
249 : : }
250 : :
251 : : int
252 : 0 : __wrap___xmknod(int ver, const char *pathname, mode_t mode, dev_t * dev)
253 : : {
254 : : int result;
255 : 0 : check_expected(pathname);
256 : 0 : result = mock();
257 [ # # ]: 0 : if (result != 0)
258 : : {
259 : 0 : errno = result;
260 : 0 : result = -1;
261 : : }
262 : 0 : return result;
263 : : }
264 : :
265 : : int
266 : 1 : __wrap_unlink(const char *pathname)
267 : : {
268 : : int result;
269 : 1 : check_expected(pathname);
270 : 1 : result = mock();
271 [ - + ]: 1 : if (result != 0)
272 : : {
273 : 0 : errno = result;
274 : 0 : result = -1;
275 : : }
276 : 1 : return result;
277 : : }
278 : :
279 : : int
280 : 14 : __wrap_socket(int domain, int type, int protocol)
281 : : {
282 : : int result;
283 : :
284 : 14 : check_expected(domain);
285 : 14 : check_expected(type);
286 : 14 : check_expected(protocol);
287 : :
288 : 14 : result = mock();
289 : :
290 [ - + ]: 14 : if (result < 0) {
291 : 0 : errno = -result;
292 : 0 : result = -1;
293 : : }
294 : 14 : return result;
295 : : }
296 : :
297 : : int
298 : 10 : __wrap_glob(const char *pattern, int flags,
299 : : int (*errfunc) (const char *epath, int eerrno),
300 : : glob_t *pglob)
301 : : {
302 : 10 : check_expected(pattern);
303 : 10 : int result = mock();
304 [ + + ]: 10 : if (result == 0)
305 : : {
306 : 6 : pglob->gl_pathc = mock();
307 : 6 : pglob->gl_pathv = (char **)mock();
308 : : }
309 : 10 : return result;
310 : : }
311 : :
312 : : void
313 : 6 : __wrap_globfree(glob_t *pglob)
314 : : {
315 [ + - ]: 6 : if (pglob->gl_pathv) {
316 : 6 : test_free(*(pglob->gl_pathv));
317 : : }
318 : 6 : }
319 : :
320 : :
321 : : int
322 : 0 : __wrap_stat(const char *pathname, struct stat *statbuf)
323 : : {
324 : 0 : check_expected(pathname);
325 : 0 : int result = mock();
326 : : printf("__wrap_stat result = %d\n", result);
327 [ # # ]: 0 : if (result == -1) {
328 : : printf("__wrap_stat, in error path\n");
329 : :
330 : 0 : errno = mock();
331 : 0 : return result;
332 : : }
333 : 0 : statbuf = mock_ptr_type(struct stat *);
334 : 0 : return result;
335 : : }
336 : :
337 : :
338 : 3 : void enable_control_mocks()
339 : : {
340 : 3 : enable_mocks = true;
341 : 3 : }
342 : :
343 : 3 : void disable_control_mocks()
344 : : {
345 : 3 : enable_mocks = 0;
346 : 3 : }
|